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;
179 diag_printf("PING server %s\n", inet_ntoa(host->sin_addr));
180 for (seq = 0; seq < NUM_PINGS; seq++, icmp_len += PACKET_ADD ) {
183 icmp->icmp_type = ICMP_ECHO;
185 icmp->icmp_cksum = 0;
186 icmp->icmp_seq = seq;
187 icmp->icmp_id = 0x1234;
189 tp = (cyg_tick_count_t *)&icmp->icmp_data;
190 *tp++ = cyg_current_time();
192 for (i = sizeof(*tp); i < icmp_len; i += sizeof(*dp)) {
196 icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
198 if (sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host, sizeof(*host)) < 0) {
203 // Wait for a response
204 fromlen = sizeof(from);
205 len = recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from, &fromlen);
209 icmp_len = MIN_PACKET - PACKET_ADD; // just in case - long routes
211 if (show_icmp(pkt2, len, &from, host)) {
219 diag_printf("Sent %d packets, received %d OK, %d bad\n", NUM_PINGS, ok_recv, bogus_recv);
222 #ifdef CYGPKG_NET_INET6
224 show6_icmp(unsigned char *pkt, int len,
225 const struct sockaddr_in6 *from, const struct sockaddr_in6 *to)
227 cyg_tick_count_t *tp, tv;
228 struct icmp6_hdr *icmp;
229 char fromnamebuf[128];
233 error = getnameinfo((struct sockaddr *)from,sizeof(*from),
234 fromnamebuf, sizeof(fromnamebuf),
238 perror ("getnameinfo(from)");
242 error = getnameinfo((struct sockaddr *)to,sizeof(*to),
243 tonamebuf, sizeof(tonamebuf),
247 perror ("getnameinfo(to)");
251 tv = cyg_current_time();
252 icmp = (struct icmp6_hdr *)pkt;
253 tp = (cyg_tick_count_t *)&icmp->icmp6_data8[4];
255 if (icmp->icmp6_type != ICMP6_ECHO_REPLY) {
258 if (icmp->icmp6_id != UNIQUEID) {
259 diag_printf("%s: ICMP received for wrong id - sent: %x, recvd: %x\n",
260 fromnamebuf, UNIQUEID, icmp->icmp6_id);
262 diag_printf("%d bytes from %s: ", len, fromnamebuf);
263 diag_printf("icmp_seq=%d", icmp->icmp6_seq);
264 diag_printf(", time=%dms\n", (int)(tv - *tp)*10);
265 return (!memcmp(&from->sin6_addr, &to->sin6_addr,sizeof(from->sin6_addr)));
269 ping6_host(int s, const struct sockaddr_in6 *host)
271 struct icmp6_hdr *icmp = (struct icmp6_hdr *)pkt1;
273 int seq, ok_recv, bogus_recv;
274 cyg_tick_count_t *tp;
276 struct sockaddr_in6 from;
284 error = getnameinfo((struct sockaddr *)host,sizeof(*host),
285 namebuf, sizeof(namebuf),
289 perror ("getnameinfo");
291 diag_printf("PING6 server %s\n", namebuf);
293 for (seq = 0; seq < NUM_PINGS; seq++) {
295 icmp->icmp6_type = ICMP6_ECHO_REQUEST;
296 icmp->icmp6_code = 0;
297 icmp->icmp6_cksum = 0;
298 icmp->icmp6_seq = seq;
299 icmp->icmp6_id = UNIQUEID;
302 tp = (cyg_tick_count_t *)&icmp->icmp6_data8[4];
303 *tp++ = cyg_current_time();
305 for (i = sizeof(*tp); i < icmp_len; i += sizeof(*dp)) {
309 icmp->icmp6_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
311 if (sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host,
312 sizeof(*host)) < 0) {
316 // Wait for a response. We get our own ECHO_REQUEST and the responce
318 while (!echo_responce) {
319 fromlen = sizeof(from);
320 len = recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from,
326 if (show6_icmp(pkt2, len, &from, host)) {
333 diag_printf("Sent %d packets, received %d OK, %d bad\n", NUM_PINGS,
334 ok_recv, bogus_recv);
338 ping6_test( struct sockaddr_in6 *host)
342 struct sockaddr_in6 addr;
345 if ((p = getprotobyname("ipv6-icmp")) == (struct protoent *)0) {
346 perror("getprotobyname");
349 s = socket(AF_INET6, SOCK_RAW, p->p_proto);
356 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
360 // Now try a bogus host
361 memcpy(&addr,host,sizeof(addr));
362 addr.sin6_addr.s6_addr[15] = addr.sin6_addr.s6_addr[15] + 32;
363 ping6_host(s, &addr);
367 #ifdef CYGPKG_PROFILE_GPROF
368 #include <cyg/profile/profile.h>
370 extern char _stext, _etext; // Defined by the linker
375 // This starts up the system-wide profiling, gathering
376 // profile information on all of the code, with a 16 byte
377 // "bucket" size, at a rate of 100us/profile hit.
378 // Note: a bucket size of 16 will give pretty good function
379 // resolution. Much smaller and the buffer becomes
380 // much too large for very little gain.
381 // Note: a timer period of 100us is also a reasonable
382 // compromise. Any smaller and the overhead of
383 // handling the timter (profile) interrupt could
384 // swamp the system. A fast processor might get
385 // by with a smaller value, but a slow one could
386 // even be swamped by this value. If the value is
387 // too large, the usefulness of the profile is reduced.
388 profile_on(&_stext, &_etext, 16, 100);
393 ping_test(struct bootp *bp)
397 struct sockaddr_in host;
400 if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
401 pexit("getprotobyname");
404 s = socket(AF_INET, SOCK_RAW, p->p_proto);
411 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
412 // Set up host address
413 host.sin_family = AF_INET;
414 host.sin_len = sizeof(host);
415 host.sin_addr = bp->bp_siaddr;
418 // Now try a bogus host
419 host.sin_addr.s_addr = htonl(ntohl(host.sin_addr.s_addr) + 32);
424 net_test(cyg_addrword_t p)
426 #ifdef CYGPKG_NET_INET6
427 struct sockaddr_in6 ipv6router;
430 diag_printf("Start PING test\n");
432 init_all_network_interfaces();
433 #ifdef CYGPKG_PROFILE_GPROF
436 #ifdef CYGHWR_NET_DRIVER_ETH0
438 ping_test(ð0_bootp_data);
441 #ifdef CYGHWR_NET_DRIVER_ETH1
443 ping_test(ð1_bootp_data);
446 #ifdef CYGPKG_NET_INET6
447 if (cyg_net_get_ipv6_advrouter(&ipv6router)) {
448 ping6_test(&ipv6router);
450 CYG_TEST_FAIL("No router advertisement recieved");
453 TNR_PRINT_ACTIVITY();
454 CYG_TEST_PASS_FINISH("Ping test OK");
460 // Create a main thread, so we can run the scheduler and have time 'pass'
461 cyg_thread_create(10, // Priority - just a number
463 0, // entry parameter
464 "Network test", // Name
467 &thread_handle, // Handle
468 &thread_data // Thread data structure
470 cyg_thread_resume(thread_handle); // Start it
471 cyg_scheduler_start();