1 //==========================================================================
5 // Simple test of PING (ICMP) and networking support
7 //==========================================================================
8 //####BSDCOPYRIGHTBEGIN####
10 // -------------------------------------------
12 // Portions of this software may have been derived from OpenBSD or other sources,
13 // and are covered by the appropriate copyright disclaimers included herein.
15 // -------------------------------------------
17 //####BSDCOPYRIGHTEND####
18 //==========================================================================
19 //#####DESCRIPTIONBEGIN####
22 // Contributors: gthomas, andrew.lunn@ascom.ch
28 //####DESCRIPTIONEND####
30 //==========================================================================
35 #ifdef CYGPKG_NET_INET6
36 #include <netinet/ip6.h>
37 #include <netinet/icmp6.h>
40 #include <pkgconf/system.h>
41 #include <pkgconf/net.h>
43 #include <cyg/infra/testcase.h>
45 #ifdef CYGBLD_DEVS_ETH_DEVICE_H // Get the device config if it exists
46 #include CYGBLD_DEVS_ETH_DEVICE_H // May provide CYGTST_DEVS_ETH_TEST_NET_REALTIME
49 #ifdef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS // do we use the rt test?
50 # ifdef CYGTST_DEVS_ETH_TEST_NET_REALTIME // Get the test ancilla if it exists
51 # include CYGTST_DEVS_ETH_TEST_NET_REALTIME
55 // Fill in the blanks if necessary
65 #ifndef TNR_PRINT_ACTIVITY
66 # define TNR_PRINT_ACTIVITY()
71 #ifndef CYGPKG_LIBC_STDIO
72 #define perror(s) diag_printf(#s ": %s\n", strerror(errno))
75 #define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
76 static char stack[STACK_SIZE];
77 static cyg_thread thread_data;
78 static cyg_handle_t thread_handle;
81 #define MAX_PACKET 4096
85 #define PACKET_ADD ((MAX_SEND - MIN_PACKET)/NUM_PINGS)
88 static unsigned char pkt1[MAX_PACKET], pkt2[MAX_PACKET];
90 #define UNIQUEID 0x1234
95 CYG_TEST_FAIL_FINISH(s);
98 // Compute INET checksum
100 inet_cksum(u_short *addr, int len)
102 register int nleft = len;
103 register u_short *w = addr;
104 register u_short answer;
105 register u_int sum = 0;
106 u_short odd_byte = 0;
109 * Our algorithm is simple, using a 32 bit accumulator (sum),
110 * we add sequential 16 bit words to it, and at the end, fold
111 * back all the carry bits from the top 16 bits into the lower
119 /* mop up an odd byte, if necessary */
121 *(u_char *)(&odd_byte) = *(u_char *)w;
126 * add back carry outs from top 16 bits to low 16 bits
128 sum = (sum >> 16) + (sum & 0x0000ffff); /* add hi 16 to low 16 */
129 sum += (sum >> 16); /* add carry */
130 answer = ~sum; /* truncate to 16 bits */
135 show_icmp(unsigned char *pkt, int len,
136 struct sockaddr_in *from, struct sockaddr_in *to)
138 cyg_tick_count_t *tp, tv;
141 tv = cyg_current_time();
142 ip = (struct ip *)pkt;
143 if ((len < sizeof(*ip)) || ip->ip_v != IPVERSION) {
144 diag_printf("%s: Short packet or not IP! - Len: %d, Version: %d\n",
145 inet_ntoa(from->sin_addr), len, ip->ip_v);
148 icmp = (struct icmp *)(pkt + sizeof(*ip));
149 len -= (sizeof(*ip) + 8);
150 tp = (cyg_tick_count_t *)&icmp->icmp_data;
151 if (icmp->icmp_type != ICMP_ECHOREPLY) {
152 diag_printf("%s: Invalid ICMP - type: %d\n",
153 inet_ntoa(from->sin_addr), icmp->icmp_type);
156 if (icmp->icmp_id != UNIQUEID) {
157 diag_printf("%s: ICMP received for wrong id - sent: %x, recvd: %x\n",
158 inet_ntoa(from->sin_addr), UNIQUEID, icmp->icmp_id);
160 diag_printf("%d bytes from %s: ", len, inet_ntoa(from->sin_addr));
161 diag_printf("icmp_seq=%d", icmp->icmp_seq);
162 diag_printf(", time=%dms\n", (int)(tv - *tp)*10);
163 return (from->sin_addr.s_addr == to->sin_addr.s_addr);
167 ping_host(int s, struct sockaddr_in *host)
169 struct icmp *icmp = (struct icmp *)pkt1;
170 int icmp_len = MIN_PACKET;
171 int seq, ok_recv, bogus_recv;
172 cyg_tick_count_t *tp;
174 struct sockaddr_in from;
180 diag_printf("PING server %s\n", inet_ntoa(host->sin_addr));
181 for (seq = 0; seq < NUM_PINGS; seq++, icmp_len += PACKET_ADD ) {
184 icmp->icmp_type = ICMP_ECHO;
186 icmp->icmp_cksum = 0;
187 icmp->icmp_seq = seq;
188 icmp->icmp_id = 0x1234;
190 tp = (cyg_tick_count_t *)&icmp->icmp_data;
191 *tp++ = cyg_current_time();
193 for (i = sizeof(*tp); i < icmp_len; i += sizeof(*dp)) {
197 icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
199 if (sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host, sizeof(*host)) < 0) {
204 // Wait for a response
205 fromlen = sizeof(from);
206 len = recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from, &fromlen);
210 icmp_len = MIN_PACKET - PACKET_ADD; // just in case - long routes
212 if (show_icmp(pkt2, len, &from, host)) {
220 diag_printf("Sent %d packets, received %d OK, %d bad\n", NUM_PINGS, ok_recv, bogus_recv);
223 #ifdef CYGPKG_NET_INET6
225 show6_icmp(unsigned char *pkt, int len,
226 const struct sockaddr_in6 *from, const struct sockaddr_in6 *to)
228 cyg_tick_count_t *tp, tv;
229 struct icmp6_hdr *icmp;
230 char fromnamebuf[128];
234 error = getnameinfo((struct sockaddr *)from,sizeof(*from),
235 fromnamebuf, sizeof(fromnamebuf),
239 perror ("getnameinfo(from)");
243 error = getnameinfo((struct sockaddr *)to,sizeof(*to),
244 tonamebuf, sizeof(tonamebuf),
248 perror ("getnameinfo(to)");
252 tv = cyg_current_time();
253 icmp = (struct icmp6_hdr *)pkt;
254 tp = (cyg_tick_count_t *)&icmp->icmp6_data8[4];
256 if (icmp->icmp6_type != ICMP6_ECHO_REPLY) {
259 if (icmp->icmp6_id != UNIQUEID) {
260 diag_printf("%s: ICMP received for wrong id - sent: %x, recvd: %x\n",
261 fromnamebuf, UNIQUEID, icmp->icmp6_id);
263 diag_printf("%d bytes from %s: ", len, fromnamebuf);
264 diag_printf("icmp_seq=%d", icmp->icmp6_seq);
265 diag_printf(", time=%dms\n", (int)(tv - *tp)*10);
266 return (!memcmp(&from->sin6_addr, &to->sin6_addr,sizeof(from->sin6_addr)));
270 ping6_host(int s, const struct sockaddr_in6 *host)
272 struct icmp6_hdr *icmp = (struct icmp6_hdr *)pkt1;
274 int seq, ok_recv, bogus_recv;
275 cyg_tick_count_t *tp;
277 struct sockaddr_in6 from;
285 error = getnameinfo((struct sockaddr *)host,sizeof(*host),
286 namebuf, sizeof(namebuf),
290 perror ("getnameinfo");
292 diag_printf("PING6 server %s\n", namebuf);
294 for (seq = 0; seq < NUM_PINGS; seq++) {
296 icmp->icmp6_type = ICMP6_ECHO_REQUEST;
297 icmp->icmp6_code = 0;
298 icmp->icmp6_cksum = 0;
299 icmp->icmp6_seq = seq;
300 icmp->icmp6_id = UNIQUEID;
303 tp = (cyg_tick_count_t *)&icmp->icmp6_data8[4];
304 *tp++ = cyg_current_time();
306 for (i = sizeof(*tp); i < icmp_len; i += sizeof(*dp)) {
310 icmp->icmp6_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
312 if (sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host,
313 sizeof(*host)) < 0) {
317 // Wait for a response. We get our own ECHO_REQUEST and the responce
319 while (!echo_responce) {
320 fromlen = sizeof(from);
321 len = recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from,
327 if (show6_icmp(pkt2, len, &from, host)) {
334 diag_printf("Sent %d packets, received %d OK, %d bad\n", NUM_PINGS,
335 ok_recv, bogus_recv);
339 ping6_test( struct sockaddr_in6 *host)
343 struct sockaddr_in6 addr;
346 if ((p = getprotobyname("ipv6-icmp")) == (struct protoent *)0) {
347 perror("getprotobyname");
350 s = socket(AF_INET6, SOCK_RAW, p->p_proto);
357 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
361 // Now try a bogus host
362 memcpy(&addr,host,sizeof(addr));
363 addr.sin6_addr.s6_addr[15] = addr.sin6_addr.s6_addr[15] + 32;
364 ping6_host(s, &addr);
368 #ifdef CYGPKG_PROFILE_GPROF
369 #include <cyg/profile/profile.h>
371 extern char _stext, _etext; // Defined by the linker
376 // This starts up the system-wide profiling, gathering
377 // profile information on all of the code, with a 16 byte
378 // "bucket" size, at a rate of 100us/profile hit.
379 // Note: a bucket size of 16 will give pretty good function
380 // resolution. Much smaller and the buffer becomes
381 // much too large for very little gain.
382 // Note: a timer period of 100us is also a reasonable
383 // compromise. Any smaller and the overhead of
384 // handling the timter (profile) interrupt could
385 // swamp the system. A fast processor might get
386 // by with a smaller value, but a slow one could
387 // even be swamped by this value. If the value is
388 // too large, the usefulness of the profile is reduced.
389 profile_on(&_stext, &_etext, 16, 100);
394 ping_test(struct bootp *bp)
398 struct sockaddr_in host;
401 if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
402 pexit("getprotobyname");
405 s = socket(AF_INET, SOCK_RAW, p->p_proto);
412 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
413 // Set up host address
414 host.sin_family = AF_INET;
415 host.sin_len = sizeof(host);
416 host.sin_addr = bp->bp_siaddr;
419 // Now try a bogus host
420 host.sin_addr.s_addr = htonl(ntohl(host.sin_addr.s_addr) + 32);
425 net_test(cyg_addrword_t p)
427 #ifdef CYGPKG_NET_INET6
428 struct sockaddr_in6 ipv6router;
431 diag_printf("Start PING test\n");
433 init_all_network_interfaces();
434 #ifdef CYGPKG_PROFILE_GPROF
437 #ifdef CYGHWR_NET_DRIVER_ETH0
439 ping_test(ð0_bootp_data);
442 #ifdef CYGHWR_NET_DRIVER_ETH1
444 ping_test(ð1_bootp_data);
447 #ifdef CYGPKG_NET_INET6
448 if (cyg_net_get_ipv6_advrouter(&ipv6router)) {
449 ping6_test(&ipv6router);
451 CYG_TEST_FAIL("No router advertisement recieved");
454 TNR_PRINT_ACTIVITY();
455 CYG_TEST_PASS_FINISH("Ping test OK");
461 // Create a main thread, so we can run the scheduler and have time 'pass'
462 cyg_thread_create(10, // Priority - just a number
464 0, // entry parameter
465 "Network test", // Name
468 &thread_handle, // Handle
469 &thread_data // Thread data structure
471 cyg_thread_resume(thread_handle); // Start it
472 cyg_scheduler_start();