big svn cleanup
[anytun.git] / src / Sockets / tests / resolve.cpp
blob802b91016adcc2deb1964efd9a50bed526f6fb65
1 #include <sys/types.h>
2 #ifndef _WIN32
3 #include <sys/socket.h>
4 #include <netdb.h>
5 #endif
6 #include <string.h>
7 #include <string>
8 #include <Utility.h>
9 #include <Ipv4Address.h>
10 #include <Ipv6Address.h>
11 #include <assert.h>
12 #include <SocketHandler.h>
13 #include <StdoutLog.h>
16 int main(int argc, char *argv[])
18 StdoutLog log;
19 SocketHandler h(&log);
21 h.EnableResolver(9999);
23 // printf("Waiting for resolver ...");
24 while (!h.ResolverReady())
26 // printf(" resolver ready!\n");
28 std::string hostname = argc < 2 ? "www.ipv6.org" : argv[1];
31 printf("Using hostname : %s\n", hostname.c_str());
32 printf("------------------------------------------- normal (old) Utility::u2ip\n");
33 ipaddr_t oa;
34 if (!Utility::u2ip(hostname, oa))
35 printf("Ipv4 lookup failed\n");
36 #ifdef ENABLE_IPV6
37 in6_addr oa6;
38 if (!Utility::u2ip(hostname, oa6))
39 printf("Ipv6 lookup failed\n");
40 #endif
41 std::string oname;
42 Utility::l2ip(oa, oname);
43 printf("Ipv4 : %s (old)\n", oname.c_str());
45 #ifdef ENABLE_IPV6
46 std::string oname6;
47 Utility::l2ip(oa6, oname6);
48 printf("Ipv6 : %s (old)\n", oname6.c_str());
49 #endif
51 printf("------------------------------------------- new Utility::u2ip, Utility::reverse\n");
52 struct sockaddr_in sa;
53 if (!Utility::u2ip(hostname, sa))
54 printf("Ipv4 lookup failed\n");
56 ipaddr_t a;
57 memcpy(&a, &sa.sin_addr, sizeof(a));
58 std::string l2ipname;
59 Utility::l2ip(a, l2ipname);
60 printf("Ipv4 : %s\n", l2ipname.c_str());
62 std::string numeric;
63 Utility::reverse((struct sockaddr *)&sa, sizeof(sa), numeric, NI_NUMERICHOST);
64 printf("Ipv4 numeric : %s\n", numeric.c_str());
66 std::string rname;
67 if (!Utility::reverse( (struct sockaddr *)&sa, sizeof(sa), rname))
68 printf("Reverse Ipv4 failed\n");
69 else
70 printf("Ipv4 Utility::reverse : %s\n", rname.c_str());
72 #ifdef ENABLE_IPV6
73 printf("------------------------------------------- new Utility::u2ip, Utility::reverse (Ipv6)\n");
74 struct sockaddr_in6 sa6;
75 if (!Utility::u2ip(hostname, sa6))
76 printf("Ipv6 lookup failed\n");
78 std::string l2ipname6;
79 Utility::l2ip(sa6.sin6_addr, l2ipname6);
80 printf("Ipv6 : %s\n", l2ipname6.c_str());
82 std::string numeric6;
83 Utility::reverse((struct sockaddr *)&sa6, sizeof(sa6), numeric6, NI_NUMERICHOST);
84 printf("Ipv6 numeric : %s\n", numeric6.c_str());
86 std::string rname6;
87 if (!Utility::reverse( (struct sockaddr *)&sa6, sizeof(sa6), rname6))
88 printf("Reverse Ipv6 failed\n");
89 else
90 printf("Ipv6 Utility::reverse : %s\n", rname6.c_str());
91 #endif
93 printf("-------------------------------------------\n");
94 in_addr ia;
95 /** Resolve hostname. */
96 //static bool Resolve(const std::string& hostname,struct in_addr& a);
97 /** Reverse resolve (IP to hostname). */
98 //static bool Reverse(struct in_addr& a,std::string& name);
99 /** Convert address struct to text. */
100 //static std::string Convert(struct in_addr& a);
101 std::string name;
102 if (!Ipv4Address::Resolve(hostname, ia))
103 printf("Ipv4 lookup failed (Ipv4Address)\n");
104 memcpy(&a, &ia, sizeof(a));
105 Utility::l2ip(a, name);
106 printf("Ipv4 : %s (Ipv4Address)\n", name.c_str());
107 assert(name == l2ipname);
108 if (!Ipv4Address::Reverse(ia, name))
109 printf("Reverse Ipv4 lookup failed (Ipv4Address)\n");
110 else
111 printf("Reverse Ipv4 : %s\n", name.c_str());
112 assert(name == rname);
113 assert(Ipv4Address::Convert(ia) == l2ipname);
115 #ifdef ENABLE_IPV6
116 printf("-------------------------------------------\n");
117 /** Resolve hostname. */
118 //static bool Resolve(const std::string& hostname,struct in6_addr& a);
119 /** Reverse resolve (IP to hostname). */
120 //static bool Reverse(struct in6_addr& a,std::string& name);
121 /** Convert address struct to text. */
122 //static std::string Convert(struct in6_addr& a,bool mixed = false);
123 if (!Ipv6Address::Resolve(hostname, oa6))
124 printf("Ipv6 lookup failed (Ipv4Address)\n");
125 Utility::l2ip(oa6, name);
126 assert(name == l2ipname6);
127 printf("Ipv6 : %s (Ipv6Address)\n", name.c_str());
128 if (!Ipv6Address::Reverse(oa6, name))
129 printf("Reverse Ipv6 lookup failed (Ipv4Address)\n");
130 else
131 printf("Reverse Ipv6 : %s\n", name.c_str());
132 assert(name == rname6);
133 std::string mixed_false = Ipv6Address::Convert(oa6, false);
134 std::string mixed_true = Ipv6Address::Convert(oa6, true);
135 printf("Ipv6Address::Convert(false) : %s\n", mixed_false.c_str());
136 printf("Ipv6Address::Convert(true ) : %s\n", mixed_true.c_str());
137 assert(mixed_false == l2ipname6);
138 #endif
140 printf("-------------------------------------------\n");
141 int protocol;
142 Utility::u2service("tcp", protocol);
143 printf("tcp: %d\n", protocol);
144 Utility::u2service("udp", protocol);
145 printf("udp: %d\n", protocol);
146 Utility::u2service("echo", protocol);
147 printf("echo: %d\n", protocol);
150 printf("\n");
151 printf("OK\n");
153 // sleep(100);