3 #include <sys/socket.h>
9 #include <Ipv4Address.h>
10 #include <Ipv6Address.h>
12 #include <SocketHandler.h>
13 #include <StdoutLog.h>
16 int main(int argc
, char *argv
[])
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");
34 if (!Utility::u2ip(hostname
, oa
))
35 printf("Ipv4 lookup failed\n");
38 if (!Utility::u2ip(hostname
, oa6
))
39 printf("Ipv6 lookup failed\n");
42 Utility::l2ip(oa
, oname
);
43 printf("Ipv4 : %s (old)\n", oname
.c_str());
47 Utility::l2ip(oa6
, oname6
);
48 printf("Ipv6 : %s (old)\n", oname6
.c_str());
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");
57 memcpy(&a
, &sa
.sin_addr
, sizeof(a
));
59 Utility::l2ip(a
, l2ipname
);
60 printf("Ipv4 : %s\n", l2ipname
.c_str());
63 Utility::reverse((struct sockaddr
*)&sa
, sizeof(sa
), numeric
, NI_NUMERICHOST
);
64 printf("Ipv4 numeric : %s\n", numeric
.c_str());
67 if (!Utility::reverse( (struct sockaddr
*)&sa
, sizeof(sa
), rname
))
68 printf("Reverse Ipv4 failed\n");
70 printf("Ipv4 Utility::reverse : %s\n", rname
.c_str());
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());
83 Utility::reverse((struct sockaddr
*)&sa6
, sizeof(sa6
), numeric6
, NI_NUMERICHOST
);
84 printf("Ipv6 numeric : %s\n", numeric6
.c_str());
87 if (!Utility::reverse( (struct sockaddr
*)&sa6
, sizeof(sa6
), rname6
))
88 printf("Reverse Ipv6 failed\n");
90 printf("Ipv6 Utility::reverse : %s\n", rname6
.c_str());
93 printf("-------------------------------------------\n");
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);
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");
111 printf("Reverse Ipv4 : %s\n", name
.c_str());
112 assert(name
== rname
);
113 assert(Ipv4Address::Convert(ia
) == l2ipname
);
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");
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
);
140 printf("-------------------------------------------\n");
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);