summaryrefslogtreecommitdiff
path: root/src/Sockets/tests/resolve.cpp
blob: 802b91016adcc2deb1964efd9a50bed526f6fb65 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <sys/types.h>
#ifndef _WIN32
#include <sys/socket.h>
#include <netdb.h>
#endif
#include <string.h>
#include <string>
#include <Utility.h>
#include <Ipv4Address.h>
#include <Ipv6Address.h>
#include <assert.h>
#include <SocketHandler.h>
#include <StdoutLog.h>


int main(int argc, char *argv[])
{
	StdoutLog log;
	SocketHandler h(&log);
/*
	h.EnableResolver(9999);

//	printf("Waiting for resolver ...");
	while (!h.ResolverReady())
		;
//	printf(" resolver ready!\n");
*/
	std::string hostname = argc < 2 ? "www.ipv6.org" : argv[1];

	{
		printf("Using hostname              : %s\n", hostname.c_str());
		printf("------------------------------------------- normal (old) Utility::u2ip\n");
		ipaddr_t oa;
		if (!Utility::u2ip(hostname, oa))
			printf("Ipv4 lookup failed\n");
#ifdef ENABLE_IPV6
		in6_addr oa6;
		if (!Utility::u2ip(hostname, oa6))
			printf("Ipv6 lookup failed\n");
#endif
		std::string oname;
		Utility::l2ip(oa, oname);
		printf("Ipv4                        : %s (old)\n", oname.c_str());

#ifdef ENABLE_IPV6
		std::string oname6;
		Utility::l2ip(oa6, oname6);
		printf("Ipv6                        : %s (old)\n", oname6.c_str());
#endif

		printf("------------------------------------------- new Utility::u2ip, Utility::reverse\n");
		struct sockaddr_in sa;
		if (!Utility::u2ip(hostname, sa))
			printf("Ipv4 lookup failed\n");

		ipaddr_t a;
		memcpy(&a, &sa.sin_addr, sizeof(a));
		std::string l2ipname;
		Utility::l2ip(a, l2ipname);
		printf("Ipv4                        : %s\n", l2ipname.c_str());

		std::string numeric;
		Utility::reverse((struct sockaddr *)&sa, sizeof(sa), numeric, NI_NUMERICHOST);
		printf("Ipv4 numeric                : %s\n", numeric.c_str());

		std::string rname;
		if (!Utility::reverse( (struct sockaddr *)&sa, sizeof(sa), rname))
			printf("Reverse Ipv4 failed\n");
		else
		printf("Ipv4 Utility::reverse       : %s\n", rname.c_str());

#ifdef ENABLE_IPV6
		printf("------------------------------------------- new Utility::u2ip, Utility::reverse (Ipv6)\n");
		struct sockaddr_in6 sa6;
		if (!Utility::u2ip(hostname, sa6))
			printf("Ipv6 lookup failed\n");

		std::string l2ipname6;
		Utility::l2ip(sa6.sin6_addr, l2ipname6);
		printf("Ipv6                        : %s\n", l2ipname6.c_str());

		std::string numeric6;
		Utility::reverse((struct sockaddr *)&sa6, sizeof(sa6), numeric6, NI_NUMERICHOST);
		printf("Ipv6 numeric                : %s\n", numeric6.c_str());

		std::string rname6;
		if (!Utility::reverse( (struct sockaddr *)&sa6, sizeof(sa6), rname6))
			printf("Reverse Ipv6 failed\n");
		else
		printf("Ipv6 Utility::reverse       : %s\n", rname6.c_str());
#endif

		printf("-------------------------------------------\n");
		in_addr ia;
		/** Resolve hostname. */
//static	bool Resolve(const std::string& hostname,struct in_addr& a);
		/** Reverse resolve (IP to hostname). */
//static	bool Reverse(struct in_addr& a,std::string& name);
		/** Convert address struct to text. */
//static	std::string Convert(struct in_addr& a);
		std::string name;
		if (!Ipv4Address::Resolve(hostname, ia))
			printf("Ipv4 lookup failed (Ipv4Address)\n");
		memcpy(&a, &ia, sizeof(a));
		Utility::l2ip(a, name);
		printf("Ipv4                        : %s (Ipv4Address)\n", name.c_str());
		assert(name == l2ipname);
		if (!Ipv4Address::Reverse(ia, name))
			printf("Reverse Ipv4 lookup failed (Ipv4Address)\n");
		else
		printf("Reverse Ipv4                : %s\n", name.c_str());
		assert(name == rname);
		assert(Ipv4Address::Convert(ia) == l2ipname);

#ifdef ENABLE_IPV6
		printf("-------------------------------------------\n");
		/** Resolve hostname. */
//static	bool Resolve(const std::string& hostname,struct in6_addr& a);
		/** Reverse resolve (IP to hostname). */
//static	bool Reverse(struct in6_addr& a,std::string& name);
		/** Convert address struct to text. */
//static	std::string Convert(struct in6_addr& a,bool mixed = false);
		if (!Ipv6Address::Resolve(hostname, oa6))
			printf("Ipv6 lookup failed (Ipv4Address)\n");
		Utility::l2ip(oa6, name);
		assert(name == l2ipname6);
		printf("Ipv6                        : %s (Ipv6Address)\n", name.c_str());
		if (!Ipv6Address::Reverse(oa6, name))
			printf("Reverse Ipv6 lookup failed (Ipv4Address)\n");
		else
		printf("Reverse Ipv6                : %s\n", name.c_str());
		assert(name == rname6);
		std::string mixed_false = Ipv6Address::Convert(oa6, false);
		std::string mixed_true = Ipv6Address::Convert(oa6, true);
		printf("Ipv6Address::Convert(false) : %s\n", mixed_false.c_str());
		printf("Ipv6Address::Convert(true ) : %s\n", mixed_true.c_str());
		assert(mixed_false == l2ipname6);
#endif
/*
		printf("-------------------------------------------\n");
		int protocol;
		Utility::u2service("tcp", protocol);
		printf("tcp: %d\n", protocol);
		Utility::u2service("udp", protocol);
		printf("udp: %d\n", protocol);
		Utility::u2service("echo", protocol);
		printf("echo: %d\n", protocol);
*/
	}
	printf("\n");
	printf("OK\n");

//	sleep(100);
}