someone said
[ghsmtp.git] / jnk / dns.cpp
blobd6402ff2010a838b3818fcd308929e54680fa911
1 // scratch program to mess with getaddrinfo
3 #include <cstdlib>
4 #include <iostream>
6 #include <arpa/inet.h>
7 #include <netdb.h>
9 #include <glog/logging.h>
11 using namespace google;
12 using namespace std;
14 int main(int argc, char* argv[])
16 InitGoogleLogging(argv[0]);
17 InstallFailureSignalHandler();
19 if (1 == argc) {
20 return 0;
23 addrinfo hints{};
24 hints.ai_family = AF_UNSPEC;
25 hints.ai_socktype = SOCK_STREAM;
26 hints.ai_flags = AI_CANONNAME;
27 hints.ai_protocol = 0; // any protocol
29 addrinfo* result;
30 int err = getaddrinfo(argv[1], nullptr, &hints, &result);
31 if (err) {
32 cerr << "getaddrinfo failure: " << gai_strerror(err) << '\n';
33 return 1;
36 if (0 == result) {
37 cerr << "no results\n";
38 return 2;
41 for (addrinfo* rp = result; rp; rp = rp->ai_next) {
42 switch (rp->ai_family) {
43 case AF_INET:
44 cout << "AF_INET";
45 break;
46 case AF_INET6:
47 cout << "AF_INET6";
48 break;
49 default:
50 cout << "unknown ai_family " << rp->ai_family << '\n';
51 return 3;
54 switch (rp->ai_socktype) {
55 case SOCK_STREAM:
56 cout << " SOCK_STREAM";
57 break;
58 case SOCK_DGRAM:
59 cout << " SOCK_DGRAM";
60 break;
61 default:
62 cout << "unknown ai_socktype " << rp->ai_socktype << '\n';
63 return 4;
66 switch (rp->ai_protocol) {
67 case IPPROTO_TCP:
68 cout << " IPPROTO_TCP";
69 break;
70 case IPPROTO_UDP:
71 cout << " IPPROTO_UDP";
72 break;
73 default:
74 cout << "unknown ai_protocol " << rp->ai_protocol << '\n';
75 return 5;
78 cout << '\n';
80 if (rp->ai_canonname)
81 cout << "canonname == " << rp->ai_canonname << '\n';
83 char net_addr[INET6_ADDRSTRLEN];
85 switch (rp->ai_addrlen) {
86 case sizeof(sockaddr_in): {
87 CHECK(rp->ai_family == AF_INET);
88 sockaddr_in* sa = reinterpret_cast<struct sockaddr_in*>(rp->ai_addr);
89 inet_ntop(AF_INET, &(sa->sin_addr), net_addr, sizeof net_addr);
90 break;
92 case sizeof(sockaddr_in6): {
93 CHECK(rp->ai_family == AF_INET6);
94 sockaddr_in6* sa = reinterpret_cast<struct sockaddr_in6*>(rp->ai_addr);
95 inet_ntop(AF_INET6, &(sa->sin6_addr), net_addr, sizeof net_addr);
96 break;
98 default:
99 cout << "unknown ai_addrlen" << rp->ai_addrlen << '\n';
100 return 6;
103 char host[NI_MAXHOST];
104 char serv[NI_MAXSERV];
106 cout << "addr == " << net_addr << '\n';
108 if (AF_INET == rp->ai_family) {
109 sockaddr_in adr;
110 adr.sin_family = AF_INET;
111 PCHECK(inet_pton(AF_INET, net_addr, &adr.sin_addr) == 1);
112 err = getnameinfo(reinterpret_cast<sockaddr*>(&adr), sizeof adr, host,
113 sizeof host, serv, sizeof serv, 0);
114 if (err) {
115 cerr << "4 getnameinfo failure: " << gai_strerror(err) << '\n';
116 return 7;
120 if (AF_INET6 == rp->ai_family) {
121 sockaddr_in6 adr;
122 adr.sin6_family = AF_INET6;
123 PCHECK(inet_pton(AF_INET6, net_addr, &adr.sin6_addr) == 1);
124 err = getnameinfo(reinterpret_cast<sockaddr*>(&adr), sizeof adr, host,
125 sizeof host, serv, sizeof serv, 0);
126 if (err) {
127 cerr << "6 getnameinfo failure: " << gai_strerror(err) << '\n';
128 return 8;
132 cout << "reverse == " << host << '\n';
135 freeaddrinfo(result);