1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// tests/addr_test.c
|
4 |
|
|
//
|
5 |
|
|
// Test network "address" functions
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####BSDALTCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// Portions of this software may have been derived from FreeBSD, OpenBSD,
|
11 |
|
|
// or other sources, and if so are covered by the appropriate copyright
|
12 |
|
|
// and license included herein.
|
13 |
|
|
// -------------------------------------------
|
14 |
|
|
// ####BSDALTCOPYRIGHTEND####
|
15 |
|
|
//==========================================================================
|
16 |
|
|
//#####DESCRIPTIONBEGIN####
|
17 |
|
|
//
|
18 |
|
|
// Author(s): gthomas
|
19 |
|
|
// Contributors: gthomas, andrew.lunn@ascom.ch
|
20 |
|
|
// Date: 2002-03-19
|
21 |
|
|
// Purpose:
|
22 |
|
|
// Description:
|
23 |
|
|
//
|
24 |
|
|
//
|
25 |
|
|
//####DESCRIPTIONEND####
|
26 |
|
|
//
|
27 |
|
|
//==========================================================================
|
28 |
|
|
|
29 |
|
|
// Networking library test code
|
30 |
|
|
|
31 |
|
|
#include <network.h>
|
32 |
|
|
#include <cyg/infra/testcase.h>
|
33 |
|
|
|
34 |
|
|
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
|
35 |
|
|
static char stack[STACK_SIZE];
|
36 |
|
|
static cyg_thread thread_data;
|
37 |
|
|
static cyg_handle_t thread_handle;
|
38 |
|
|
|
39 |
|
|
void
|
40 |
|
|
pexit(char *s)
|
41 |
|
|
{
|
42 |
|
|
char msg[256];
|
43 |
|
|
|
44 |
|
|
diag_sprintf(msg, "%s: %s", s, strerror(errno));
|
45 |
|
|
CYG_TEST_FAIL_FINISH(msg);
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
static void
|
49 |
|
|
walk_addrs(struct addrinfo *ai, char *title)
|
50 |
|
|
{
|
51 |
|
|
char addr_buf[256];
|
52 |
|
|
|
53 |
|
|
diag_printf("INFO: %s\n", title);
|
54 |
|
|
while (ai) {
|
55 |
|
|
_inet_ntop(ai->ai_addr, addr_buf, sizeof(addr_buf));
|
56 |
|
|
diag_printf("INFO: Family: %2d, Socket: %d, Addr: %s, Port: %d\n",
|
57 |
|
|
ai->ai_family, ai->ai_socktype, addr_buf, _inet_port(ai->ai_addr));
|
58 |
|
|
switch (ai->ai_family) {
|
59 |
|
|
case AF_INET:
|
60 |
|
|
if (ai->ai_addrlen != sizeof(struct sockaddr_in)) {
|
61 |
|
|
CYG_TEST_FAIL("ai_addrlen wrong for an IPv4 address");
|
62 |
|
|
}
|
63 |
|
|
break;
|
64 |
|
|
#ifdef CYGPKG_NET_INET6
|
65 |
|
|
case AF_INET6:
|
66 |
|
|
if (ai->ai_addrlen != sizeof(struct sockaddr_in6)) {
|
67 |
|
|
CYG_TEST_FAIL("ai_addrlen wrong for an IPv6 address");
|
68 |
|
|
}
|
69 |
|
|
break;
|
70 |
|
|
#endif
|
71 |
|
|
default:
|
72 |
|
|
CYG_TEST_FAIL("Unknown address family");
|
73 |
|
|
}
|
74 |
|
|
ai = ai->ai_next;
|
75 |
|
|
}
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
void
|
79 |
|
|
net_test(CYG_ADDRWORD data)
|
80 |
|
|
{
|
81 |
|
|
int err;
|
82 |
|
|
struct addrinfo *addrs, hints;
|
83 |
|
|
size_t hostlen = 128;
|
84 |
|
|
size_t servlen = 64;
|
85 |
|
|
char host[hostlen];
|
86 |
|
|
char serv[servlen];
|
87 |
|
|
|
88 |
|
|
bzero(&hints, sizeof(hints));
|
89 |
|
|
hints.ai_family = PF_UNSPEC;
|
90 |
|
|
hints.ai_socktype = SOCK_STREAM;
|
91 |
|
|
hints.ai_flags = AI_PASSIVE|AI_NUMERICHOST;
|
92 |
|
|
if ((err = getaddrinfo(NULL, "7734", &hints, &addrs)) != EAI_NONE) {
|
93 |
|
|
diag_printf("<ERROR> can't getaddrinfo(): %s\n", gai_strerror(err));
|
94 |
|
|
pexit("getaddrinfo");
|
95 |
|
|
}
|
96 |
|
|
walk_addrs(addrs, "all passive");
|
97 |
|
|
|
98 |
|
|
err = getnameinfo(addrs->ai_addr, addrs->ai_addrlen,
|
99 |
|
|
NULL, 0, serv,servlen, 0);
|
100 |
|
|
if (err != EAI_NONE) {
|
101 |
|
|
diag_printf("<ERROR> can't getnameinfo(): %s\n", gai_strerror(err));
|
102 |
|
|
pexit("getnameinfo");
|
103 |
|
|
}
|
104 |
|
|
diag_printf("INFO: service: %s\n",serv);
|
105 |
|
|
freeaddrinfo(addrs);
|
106 |
|
|
|
107 |
|
|
bzero(&hints, sizeof(hints));
|
108 |
|
|
hints.ai_family = PF_UNSPEC;
|
109 |
|
|
hints.ai_socktype = SOCK_STREAM;
|
110 |
|
|
hints.ai_flags = AI_NUMERICHOST;
|
111 |
|
|
if ((err = getaddrinfo(NULL, "7734", &hints, &addrs)) != EAI_NONE) {
|
112 |
|
|
diag_printf("<ERROR> can't getaddrinfo(): %s\n", gai_strerror(err));
|
113 |
|
|
pexit("getaddrinfo");
|
114 |
|
|
}
|
115 |
|
|
walk_addrs(addrs, "all active");
|
116 |
|
|
freeaddrinfo(addrs);
|
117 |
|
|
|
118 |
|
|
bzero(&hints, sizeof(hints));
|
119 |
|
|
hints.ai_family = PF_INET;
|
120 |
|
|
hints.ai_socktype = SOCK_STREAM;
|
121 |
|
|
hints.ai_flags = AI_PASSIVE|AI_NUMERICHOST;
|
122 |
|
|
if ((err = getaddrinfo(NULL, "7734", &hints, &addrs)) != EAI_NONE) {
|
123 |
|
|
diag_printf("<ERROR> can't getaddrinfo(): %s\n", gai_strerror(err));
|
124 |
|
|
pexit("getaddrinfo");
|
125 |
|
|
}
|
126 |
|
|
walk_addrs(addrs, "IPv4 passive");
|
127 |
|
|
freeaddrinfo(addrs);
|
128 |
|
|
|
129 |
|
|
bzero(&hints, sizeof(hints));
|
130 |
|
|
hints.ai_family = PF_INET;
|
131 |
|
|
hints.ai_socktype = SOCK_STREAM;
|
132 |
|
|
hints.ai_flags = AI_PASSIVE|AI_NUMERICHOST;
|
133 |
|
|
if ((err = getaddrinfo("192.168.1.2", "7734", &hints, &addrs)) != EAI_NONE) {
|
134 |
|
|
diag_printf("<ERROR> can't getaddrinfo(): %s\n", gai_strerror(err));
|
135 |
|
|
pexit("getaddrinfo");
|
136 |
|
|
}
|
137 |
|
|
walk_addrs(addrs, "IPv4 passive 192.168.1.2");
|
138 |
|
|
|
139 |
|
|
err = getnameinfo(addrs->ai_addr, addrs->ai_addrlen,
|
140 |
|
|
host, hostlen, serv, servlen, NI_NUMERICHOST);
|
141 |
|
|
if (err != EAI_NONE) {
|
142 |
|
|
diag_printf("<ERROR> can't getnameinfo(): %s\n", gai_strerror(err));
|
143 |
|
|
pexit("getnameinfo");
|
144 |
|
|
}
|
145 |
|
|
diag_printf("INFO: host: %s service: %s\n",host, serv);
|
146 |
|
|
freeaddrinfo(addrs);
|
147 |
|
|
|
148 |
|
|
bzero(&hints, sizeof(hints));
|
149 |
|
|
hints.ai_family = PF_UNSPEC;
|
150 |
|
|
hints.ai_socktype = SOCK_STREAM;
|
151 |
|
|
hints.ai_flags = AI_PASSIVE|AI_NUMERICHOST;
|
152 |
|
|
if ((err = getaddrinfo("192.168.1.2", "7734", &hints, &addrs)) != EAI_NONE) {
|
153 |
|
|
diag_printf("<ERROR> can't getaddrinfo(): %s\n", gai_strerror(err));
|
154 |
|
|
pexit("getaddrinfo");
|
155 |
|
|
}
|
156 |
|
|
walk_addrs(addrs, "all passive 192.168.1.2");
|
157 |
|
|
freeaddrinfo(addrs);
|
158 |
|
|
|
159 |
|
|
#ifdef CYGPKG_NET_INET6
|
160 |
|
|
bzero(&hints, sizeof(hints));
|
161 |
|
|
hints.ai_family = PF_INET6;
|
162 |
|
|
hints.ai_socktype = SOCK_STREAM;
|
163 |
|
|
hints.ai_flags = AI_PASSIVE|AI_NUMERICHOST;
|
164 |
|
|
if ((err = getaddrinfo(NULL, "7734", &hints, &addrs)) != EAI_NONE) {
|
165 |
|
|
diag_printf("<ERROR> can't getaddrinfo(): %s\n", gai_strerror(err));
|
166 |
|
|
pexit("getaddrinfo");
|
167 |
|
|
}
|
168 |
|
|
walk_addrs(addrs, "IPv6 passive");
|
169 |
|
|
|
170 |
|
|
err = getnameinfo(addrs->ai_addr, addrs->ai_addrlen,
|
171 |
|
|
NULL, 0, serv,servlen, 0);
|
172 |
|
|
if (err != EAI_NONE) {
|
173 |
|
|
diag_printf("<ERROR> can't getnameinfo(): %s\n", gai_strerror(err));
|
174 |
|
|
pexit("getnameinfo");
|
175 |
|
|
}
|
176 |
|
|
diag_printf("INFO: service: %s\n",serv);
|
177 |
|
|
freeaddrinfo(addrs);
|
178 |
|
|
|
179 |
|
|
bzero(&hints, sizeof(hints));
|
180 |
|
|
hints.ai_family = PF_UNSPEC;
|
181 |
|
|
hints.ai_socktype = SOCK_STREAM;
|
182 |
|
|
hints.ai_flags = AI_NUMERICHOST;
|
183 |
|
|
if ((err = getaddrinfo(NULL, "7734", &hints, &addrs)) != EAI_NONE) {
|
184 |
|
|
diag_printf("<ERROR> can't getaddrinfo(): %s\n", gai_strerror(err));
|
185 |
|
|
pexit("getaddrinfo");
|
186 |
|
|
}
|
187 |
|
|
walk_addrs(addrs, "all passive");
|
188 |
|
|
freeaddrinfo(addrs);
|
189 |
|
|
|
190 |
|
|
bzero(&hints, sizeof(hints));
|
191 |
|
|
hints.ai_family = PF_UNSPEC;
|
192 |
|
|
hints.ai_socktype = SOCK_STREAM;
|
193 |
|
|
hints.ai_flags = AI_NUMERICHOST;
|
194 |
|
|
if ((err = getaddrinfo("fe80::260:97ff:feb0:866e", "7734", &hints, &addrs))
|
195 |
|
|
!= EAI_NONE) {
|
196 |
|
|
diag_printf("<ERROR> can't getaddrinfo(): %s\n", gai_strerror(err));
|
197 |
|
|
pexit("getaddrinfo");
|
198 |
|
|
}
|
199 |
|
|
walk_addrs(addrs, "all passive fe80::260:97ff:feb0:866e");
|
200 |
|
|
|
201 |
|
|
err = getnameinfo(addrs->ai_addr, addrs->ai_addrlen,
|
202 |
|
|
host, hostlen, serv, servlen, NI_NUMERICHOST);
|
203 |
|
|
if (err != EAI_NONE) {
|
204 |
|
|
diag_printf("<ERROR> can't getnameinfo(): %s\n", gai_strerror(err));
|
205 |
|
|
pexit("getnameinfo");
|
206 |
|
|
}
|
207 |
|
|
diag_printf("INFO: host: %s service: %s\n",host, serv);
|
208 |
|
|
freeaddrinfo(addrs);
|
209 |
|
|
#endif
|
210 |
|
|
|
211 |
|
|
bzero(&hints, sizeof(hints));
|
212 |
|
|
hints.ai_family = PF_UNSPEC;
|
213 |
|
|
hints.ai_flags = AI_PASSIVE|AI_NUMERICHOST;
|
214 |
|
|
if ((err = getaddrinfo(NULL, "ftp", &hints, &addrs)) != EAI_NONE) {
|
215 |
|
|
diag_printf("<ERROR> can't getaddrinfo(): %s\n", gai_strerror(err));
|
216 |
|
|
pexit("getaddrinfo");
|
217 |
|
|
}
|
218 |
|
|
walk_addrs(addrs, "all snmp/udp");
|
219 |
|
|
|
220 |
|
|
err = getnameinfo(addrs->ai_addr, addrs->ai_addrlen,
|
221 |
|
|
NULL, 0, serv,servlen, 0);
|
222 |
|
|
if (err != EAI_NONE) {
|
223 |
|
|
diag_printf("<ERROR> can't getnameinfo(): %s\n", gai_strerror(err));
|
224 |
|
|
pexit("getnameinfo");
|
225 |
|
|
}
|
226 |
|
|
diag_printf("INFO: service: %s\n",serv);
|
227 |
|
|
freeaddrinfo(addrs);
|
228 |
|
|
|
229 |
|
|
bzero(&hints, sizeof(hints));
|
230 |
|
|
hints.ai_family = PF_UNSPEC;
|
231 |
|
|
hints.ai_socktype = SOCK_DGRAM;
|
232 |
|
|
hints.ai_flags = AI_PASSIVE|AI_NUMERICHOST;
|
233 |
|
|
if ((err = getaddrinfo(NULL, "snmp", &hints, &addrs)) != EAI_NONE) {
|
234 |
|
|
diag_printf("<ERROR> can't getaddrinfo(): %s\n", gai_strerror(err));
|
235 |
|
|
pexit("getaddrinfo");
|
236 |
|
|
}
|
237 |
|
|
walk_addrs(addrs, "all snmp/udp");
|
238 |
|
|
|
239 |
|
|
err = getnameinfo(addrs->ai_addr, addrs->ai_addrlen,
|
240 |
|
|
NULL, 0, serv,servlen, 0);
|
241 |
|
|
if (err != EAI_NONE) {
|
242 |
|
|
diag_printf("<ERROR> can't getnameinfo(): %s\n", gai_strerror(err));
|
243 |
|
|
pexit("getnameinfo");
|
244 |
|
|
}
|
245 |
|
|
diag_printf("INFO: service: %s\n",serv);
|
246 |
|
|
freeaddrinfo(addrs);
|
247 |
|
|
|
248 |
|
|
bzero(&hints, sizeof(hints));
|
249 |
|
|
hints.ai_family = PF_UNSPEC;
|
250 |
|
|
hints.ai_socktype = 0;
|
251 |
|
|
hints.ai_flags = AI_NUMERICHOST;
|
252 |
|
|
if ((err = getaddrinfo(NULL, "snmp", &hints, &addrs)) != EAI_NONE) {
|
253 |
|
|
diag_printf("<ERROR> can't getaddrinfo(): %s\n", gai_strerror(err));
|
254 |
|
|
pexit("getaddrinfo");
|
255 |
|
|
}
|
256 |
|
|
walk_addrs(addrs, "all snmp/*");
|
257 |
|
|
|
258 |
|
|
err = getnameinfo(addrs->ai_addr, addrs->ai_addrlen,
|
259 |
|
|
NULL, 0, serv,servlen, NI_NUMERICSERV);
|
260 |
|
|
if (err != EAI_NONE) {
|
261 |
|
|
diag_printf("<ERROR> can't getnameinfo(): %s\n", gai_strerror(err));
|
262 |
|
|
pexit("getnameinfo");
|
263 |
|
|
}
|
264 |
|
|
diag_printf("INFO: service: %s\n",serv);
|
265 |
|
|
freeaddrinfo(addrs);
|
266 |
|
|
|
267 |
|
|
CYG_TEST_PASS_FINISH("Address [library] test OK");
|
268 |
|
|
|
269 |
|
|
}
|
270 |
|
|
void
|
271 |
|
|
cyg_start(void)
|
272 |
|
|
{
|
273 |
|
|
// Create a main thread, so we can run the scheduler and have time 'pass'
|
274 |
|
|
cyg_thread_create(10, // Priority - just a number
|
275 |
|
|
net_test, // entry
|
276 |
|
|
0, // entry parameter
|
277 |
|
|
"Network test", // Name
|
278 |
|
|
&stack[0], // Stack
|
279 |
|
|
STACK_SIZE, // Size
|
280 |
|
|
&thread_handle, // Handle
|
281 |
|
|
&thread_data // Thread data structure
|
282 |
|
|
);
|
283 |
|
|
cyg_thread_resume(thread_handle); // Start it
|
284 |
|
|
cyg_scheduler_start();
|
285 |
|
|
}
|