1个回答
展开全部
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
using namespace std;
const char* dns_parse(const char* host) {
struct addrinfo hints;
struct addrinfo *result = NULL;
struct addrinfo *rp = NULL;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow any */
hints.ai_socktype = 0; /* Any */
hints.ai_flags = 0; /* No flags */
hints.ai_protocol = 0; /* Any protocol */
int error = getaddrinfo(host, NULL, &hints, &result);
if (error != 0) {
if (result) {
freeaddrinfo(result);
}
return "";
}
const char* ip = "";
const char* ipv6 = "";
const char* ipv4 = "";
char tmp[INET6_ADDRSTRLEN * 2] = {0};
for (rp = result; rp != NULL; rp = rp->ai_next) {
if (rp->ai_family == AF_INET && strlen(ipv4) == 0) {
const char *ret = inet_ntop(rp->ai_family,
&((struct sockaddr_in*)rp->ai_addr)->sin_addr,
tmp, (INET6_ADDRSTRLEN * 2));
if (ret != NULL) {
ipv4 = ret;
}
} else if (rp->ai_family == AF_INET6 && strlen(ipv6) == 0) {
const char *ret = inet_ntop(rp->ai_family,
&((struct sockaddr_in6*)rp->ai_addr)->sin6_addr,
tmp, (INET6_ADDRSTRLEN * 2));
if (ret != NULL) {
ipv6 = ret;
}
}
}
if (result) {
freeaddrinfo(result); // Not needed anymore
}
if (strlen(ipv6) > 0) {
ip = ipv6;
} else if (strlen(ipv4) > 0) {
ip = ipv4;
} else {
return "";
}
return ip;
}
void save_to_file(char* data, int len, const char* file_path) {
FILE *fp = fopen(file_path, "wb");
if (!fp) {
return;
}
fwrite(data, sizeof(char), len, fp);
fclose(fp);
}
int main() {
const char* ip = dns_parse("www.baidu.com");
printf("%s\n", ip);
int len = strlen(ip);
char* data = (char*)malloc(len + 1);
memcpy(data, ip, len);
data[len] = '\0';
save_to_file(data, len + 1, "a.txt");
free(data);
return 0;
}
#include <string.h>
#include <malloc.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
using namespace std;
const char* dns_parse(const char* host) {
struct addrinfo hints;
struct addrinfo *result = NULL;
struct addrinfo *rp = NULL;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow any */
hints.ai_socktype = 0; /* Any */
hints.ai_flags = 0; /* No flags */
hints.ai_protocol = 0; /* Any protocol */
int error = getaddrinfo(host, NULL, &hints, &result);
if (error != 0) {
if (result) {
freeaddrinfo(result);
}
return "";
}
const char* ip = "";
const char* ipv6 = "";
const char* ipv4 = "";
char tmp[INET6_ADDRSTRLEN * 2] = {0};
for (rp = result; rp != NULL; rp = rp->ai_next) {
if (rp->ai_family == AF_INET && strlen(ipv4) == 0) {
const char *ret = inet_ntop(rp->ai_family,
&((struct sockaddr_in*)rp->ai_addr)->sin_addr,
tmp, (INET6_ADDRSTRLEN * 2));
if (ret != NULL) {
ipv4 = ret;
}
} else if (rp->ai_family == AF_INET6 && strlen(ipv6) == 0) {
const char *ret = inet_ntop(rp->ai_family,
&((struct sockaddr_in6*)rp->ai_addr)->sin6_addr,
tmp, (INET6_ADDRSTRLEN * 2));
if (ret != NULL) {
ipv6 = ret;
}
}
}
if (result) {
freeaddrinfo(result); // Not needed anymore
}
if (strlen(ipv6) > 0) {
ip = ipv6;
} else if (strlen(ipv4) > 0) {
ip = ipv4;
} else {
return "";
}
return ip;
}
void save_to_file(char* data, int len, const char* file_path) {
FILE *fp = fopen(file_path, "wb");
if (!fp) {
return;
}
fwrite(data, sizeof(char), len, fp);
fclose(fp);
}
int main() {
const char* ip = dns_parse("www.baidu.com");
printf("%s\n", ip);
int len = strlen(ip);
char* data = (char*)malloc(len + 1);
memcpy(data, ip, len);
data[len] = '\0';
save_to_file(data, len + 1, "a.txt");
free(data);
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询