我想查询特定服务器,并以与通过getaddrinfo
获取结果相同的方式获取结果。我想获得一个addrinfo
结构,所以我可以拥有ip,port和指向下一个结果的指针。
我正在使用下面的代码,它会查询我想要的服务器,并获得结果。但是每个结果都在另一个结构中,并且它们不会相互指向(不在列表中)。
这是代码:
static int my_getaddrinfo(const char *dns_server_s, const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) {
int retValue = 1;
struct __res_state result;
char ip[16];
memset(ip, '\0', sizeof(ip));
res_ninit(&result);
struct in_addr addr;
inet_aton(dns_server_s, &addr);
result.nsaddr_list[0].sin_addr = addr;
result.nsaddr_list[0].sin_family = AF_INET;
result.nsaddr_list[0].sin_port = htons(NS_DEFAULTPORT);
result.nscount = 1;
u_char answer[NS_PACKETSZ];
int len = res_nquery(&result, node, ns_c_in, ns_t_a, answer, sizeof(answer));
ns_msg handle;
ns_initparse(answer, len, &handle);
if(ns_msg_count(handle, ns_s_an) > 0) {
ns_rr rr;
if(ns_parserr(&handle, ns_s_an, 0, &rr) == 0) {
strcpy(ip, inet_ntoa(*(struct in_addr *)ns_rr_rdata(rr)));
getaddrinfo(ip, service, hints, res);
retValue = 0;
}
}
return retValue;
}
是否有可能以我想要的方式获得结果?类似的东西 addrinfo struct?
修改
我可以看到我得到三个答案ns_msg_count(handle, ns_s_an) = 3
并且要访问每个答案,我应该致电ns_parserr(&handle, ns_s_an, answer_index, &rr)
但正如我所说,我希望将这些答案作为一个列表,就像我通过调用getaddrinfo
得到它们一样。
答案 0 :(得分:3)
getaddrinfo返回的不仅仅是ip地址,还会将服务名称解析为端口号,它可以支持不同的协议,主要是tcp和udp。因此,您需要通过调用getservbyname_r
来解析服务名称,并为每个ip,端口和协议组合手动构造结果addrinfo
。这是一个解析ip和port的简单版本,但它只返回TCP的addrinfo。您可以将其扩展为包含UDP。
另一个功能是getaddrinfo
也支持IPV6,在这种情况下,您需要使用res_nquery
而不是T_AAAA
来调用T_A
来解析IPV6地址。
以下是一个示例,请注意它会返回struct addrinfo
的链接列表,就像getaddrinfo
一样,所以当您完成后,结果应该是免费的freeaddrinfo
。
static int my_getaddrinfo(const char *dns_server,
const char *node, const char *service,
const struct addrinfo *hints, struct addrinfo **res) {
int ret;
// get dns server sockaddr
struct addrinfo hint = {0};
struct addrinfo *ai = NULL;
hint.ai_family = AF_INET;
hint.ai_socktype = SOCK_DGRAM;
hint.ai_protocol = IPPROTO_UDP;
ret = getaddrinfo(dns_server, "domain", &hint, &ai);
if (ret != 0) {
puts("getaddrinfo dns error");
return 1;
}
if (!ai) {
printf("getaddrinfo returned no result\n");
return 1;
}
freeaddrinfo(ai);
int port = 0;
// get service port
if (service) {
struct servent srv, *sres;
char buf[1024];
ret = getservbyname_r(service, NULL, &srv, buf, sizeof buf, &sres);
if (ret != 0) {
printf("getservbyname error\n");
return 1;
}
port = sres->s_port;
}
struct __res_state p = {0};
res_state state = &p;
unsigned char ans[NS_MAXMSG];
ns_msg msg;
ns_rr rr;
char line[1024];
int len ;
ret = res_ninit(state);
if (ret != 0) {
printf("res_ninit error\n");
return 1;
}
state->nscount = 1;
memset(state->nsaddr_list, 0, sizeof state->nsaddr_list);
memcpy(state->nsaddr_list, ai->ai_addr, sizeof state->nsaddr_list[0]);
ret = res_nquery(state, node, C_IN, T_A, ans, sizeof ans);
if (ret < 0) {
printf("res_nquery error\n");
return 1;
}
len = ret;
ret = ns_initparse(ans, len, &msg);
if (ret != 0) {
printf("ns_initparse error\n");
return 1;
}
len = ns_msg_count(msg, ns_s_an);
if (len == 0) {
printf("no address found\n");
return 0;
}
struct addrinfo *head = NULL;
struct addrinfo *cur = NULL;
struct addrinfo *prev = NULL;
struct sockaddr_in *sin;
for (int i = 0; i < len; i++) {
ret = ns_parserr(&msg, ns_s_an, i, &rr);
if (ret != 0) {
printf("ns_parserr error\n");
}
if (ns_rr_rdlen(rr) != NS_INADDRSZ) {
continue;
}
cur = malloc(sizeof *cur + sizeof(struct sockaddr_in));
memset(cur, 0, sizeof *cur);
cur->ai_family = AF_INET;
cur->ai_socktype = SOCK_STREAM;
cur->ai_protocol = IPPROTO_TCP;
cur->ai_addrlen = sizeof(struct sockaddr_in);
cur->ai_addr = (void*)(cur + 1);
cur->ai_canonname = NULL;
cur->ai_next = NULL;
sin = (struct sockaddr_in*)(cur->ai_addr);
sin->sin_family = cur->ai_family;
sin->sin_port = port;
memcpy(&sin->sin_addr, ns_rr_rdata(rr), sizeof sin->sin_addr);
if (prev)
prev->ai_next = cur;
if (head == NULL)
head = cur;
prev = cur;
}
*res = head;
return 0;
}
int main(int argc, char *argv[])
{
const char *node = "bing.com";
struct addrinfo *res = NULL;
if (argc == 2)
node = argv[1];
int ret = my_getaddrinfo("8.8.8.8", node, "http", NULL, &res);
if (ret != 0) {
puts("getaddrinfo error");
return 1;
}
// do stuff with res
struct addrinfo *rp;
struct sockaddr_in *sin;
char p[1024];
for (rp = res; rp != NULL; rp = rp->ai_next) {
sin = (struct sockaddr_in*)rp->ai_addr;
const char *s = inet_ntop(rp->ai_family,
&sin->sin_addr, p, sizeof p);
printf("Got %s: %d\n", s, ntohs(sin->sin_port));
}
freeaddrinfo(res);
return 0;
}
示例:
$ ./a.out bing.com
Got 204.79.197.200: 80
Got 13.107.21.200: 80
$ ./a.out google.com
Got 172.217.24.14: 80