2012-12-17 22:22:25 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#define satosin(x) ((struct sockaddr_in *) &(x))
|
|
|
|
#define SOCKADDR(x) (satosin(x)->sin_addr.s_addr)
|
|
|
|
#define SOCKADDR_2(x) (satosin(x)->sin_addr)
|
|
|
|
#define SOCKPORT(x) (satosin(x)->sin_port)
|
|
|
|
#define SOCKFAMILY(x) (satosin(x)->sin_family)
|
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
#define ASSERT(X) { if(!(X)) printf("ASSERTION FAILED: %s @%s:%d\n", # X, __FILE__, __LINE__); }
|
|
|
|
#define CLR() { hbuf[0] = 0; sbuf[0] = 0; }
|
|
|
|
|
2012-12-17 22:22:25 +00:00
|
|
|
int main() {
|
2015-12-06 13:01:07 +00:00
|
|
|
struct sockaddr_in a = {0}, *sa = &a;
|
2012-12-17 22:22:25 +00:00
|
|
|
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
|
2015-12-06 13:01:07 +00:00
|
|
|
a.sin_port = htons(80);
|
|
|
|
memcpy( &a.sin_addr , (char[]) {127,0,0,1}, 4);
|
2012-12-17 22:22:25 +00:00
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
if ((ret = getnameinfo((void*)sa, 0, hbuf, sizeof(hbuf), sbuf,
|
2012-12-17 22:22:25 +00:00
|
|
|
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
|
|
|
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
|
|
|
else
|
|
|
|
printf("%s\n", gai_strerror(ret));
|
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
ASSERT(ret == EAI_FAMILY);
|
|
|
|
CLR();
|
2012-12-17 22:22:25 +00:00
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
if ((ret = getnameinfo((void*)sa, sizeof a, hbuf, sizeof(hbuf), sbuf,
|
2012-12-17 22:22:25 +00:00
|
|
|
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
|
|
|
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
|
|
|
else
|
|
|
|
printf("%s\n", gai_strerror(ret));
|
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
ASSERT(ret == EAI_FAMILY);
|
|
|
|
CLR();
|
2012-12-17 22:22:25 +00:00
|
|
|
|
|
|
|
SOCKFAMILY(a) = AF_INET;
|
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
if ((ret = getnameinfo((void*)sa, sizeof a, hbuf, 1, sbuf,
|
2012-12-17 22:22:25 +00:00
|
|
|
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
|
|
|
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
|
|
|
else
|
|
|
|
printf("%s\n", gai_strerror(ret));
|
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
ASSERT(ret == EAI_OVERFLOW);
|
|
|
|
CLR();
|
2012-12-17 22:22:25 +00:00
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
if ((ret = getnameinfo((void*)sa, sizeof a, hbuf, 0, sbuf,
|
2012-12-17 22:22:25 +00:00
|
|
|
1, NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
|
|
|
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
|
|
|
else
|
|
|
|
printf("%s\n", gai_strerror(ret));
|
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
ASSERT(ret == EAI_OVERFLOW);
|
|
|
|
CLR();
|
2012-12-17 22:22:25 +00:00
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
if ((ret = getnameinfo((void*)sa, sizeof(a) - 1, hbuf, 0, sbuf,
|
2012-12-17 22:22:25 +00:00
|
|
|
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
|
|
|
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
|
|
|
else
|
|
|
|
printf("%s\n", gai_strerror(ret));
|
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
ASSERT(ret == EAI_FAMILY);
|
|
|
|
CLR();
|
|
|
|
|
|
|
|
if ((ret = getnameinfo((void*)sa, sizeof a, hbuf, 0, sbuf,
|
|
|
|
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
|
|
|
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
|
|
|
else
|
|
|
|
printf("%s\n", gai_strerror(ret));
|
2012-12-17 22:22:25 +00:00
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
ASSERT(ret == 0 && !strcmp("80", sbuf));
|
|
|
|
CLR();
|
|
|
|
|
|
|
|
if ((ret = getnameinfo((void*)sa, sizeof a, hbuf, sizeof hbuf, sbuf,
|
2012-12-17 22:22:25 +00:00
|
|
|
0, NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
|
|
|
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
|
|
|
else
|
|
|
|
printf("%s\n", gai_strerror(ret));
|
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
ASSERT(ret == 0 && !strcmp("127.0.0.1",hbuf));
|
|
|
|
CLR();
|
2012-12-17 22:22:25 +00:00
|
|
|
|
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
if ((ret = getnameinfo((void*)sa, sizeof a, hbuf, sizeof(hbuf), sbuf,
|
2012-12-17 22:22:25 +00:00
|
|
|
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
|
|
|
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
|
|
|
else
|
|
|
|
printf("%s\n", gai_strerror(ret));
|
|
|
|
|
2015-12-06 13:01:07 +00:00
|
|
|
ASSERT(ret == 0 && !strcmp("127.0.0.1",hbuf) && !strcmp("80", sbuf));
|
|
|
|
CLR();
|
|
|
|
|
|
|
|
struct sockaddr_in6 b = {0}, *sb = &b;
|
|
|
|
b.sin6_port = htons(8080);
|
|
|
|
b.sin6_family = AF_INET6;
|
|
|
|
|
|
|
|
memcpy(&b.sin6_addr,"\0\0\0\0\0\0\0\0\0\0\xff\xff\xc0\xa8\1\2", 16);
|
|
|
|
|
|
|
|
if ((ret = getnameinfo((void*)sb, sizeof b, hbuf, sizeof(hbuf), sbuf,
|
|
|
|
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
|
|
|
|
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
|
|
|
else
|
|
|
|
printf("%s\n", gai_strerror(ret));
|
|
|
|
|
|
|
|
ASSERT(ret == 0 && !strcmp("192.168.1.2",hbuf) && !strcmp("8080", sbuf));
|
|
|
|
CLR();
|
|
|
|
|
|
|
|
b.sin6_scope_id = 3;
|
|
|
|
memcpy(&b.sin6_addr,"\0\0\xaa\0\0\0\0\0\0\0\0\xff\xc0\xa8\1\2", 16);
|
|
|
|
|
|
|
|
if ((ret = getnameinfo((void*)sb, sizeof b, hbuf, sizeof(hbuf), sbuf,
|
|
|
|
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV | NI_NUMERICSCOPE)) == 0)
|
|
|
|
printf("host=%s, serv=%s\n", hbuf, sbuf);
|
|
|
|
else
|
|
|
|
printf("%s\n", gai_strerror(ret));
|
|
|
|
|
|
|
|
ASSERT(ret == 0);
|
2012-12-17 22:22:25 +00:00
|
|
|
|
2021-12-12 15:59:34 +00:00
|
|
|
b.sin6_port = 0;
|
|
|
|
b.sin6_scope_id = 0;
|
|
|
|
memcpy(&b.sin6_addr,"\0\0\0\0" "\0\0\0\0" "\0\0\0\0" "\0\0\0\1", 16);
|
|
|
|
|
|
|
|
if ((ret = getnameinfo((void*)sb, sizeof b, hbuf, sizeof(hbuf), NULL,
|
|
|
|
0, NI_NUMERICHOST)) == 0)
|
|
|
|
printf("host=%s\n", hbuf);
|
|
|
|
else
|
|
|
|
printf("%s\n", gai_strerror(ret));
|
|
|
|
|
|
|
|
ASSERT(ret == 0);
|
|
|
|
|
2012-12-17 22:22:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|