1 //==========================================================================
5 // Simple test of DHCP (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
28 //####DESCRIPTIONEND####
30 //==========================================================================
36 #include <pkgconf/system.h>
37 #include <pkgconf/net.h>
41 #include <cyg/infra/testcase.h>
43 #ifdef CYGBLD_DEVS_ETH_DEVICE_H // Get the device config if it exists
44 #include CYGBLD_DEVS_ETH_DEVICE_H // May provide CYGTST_DEVS_ETH_TEST_NET_REALTIME
47 #ifdef CYGPKG_NET_TESTS_USE_RT_TEST_HARNESS // do we use the rt test?
48 # ifdef CYGTST_DEVS_ETH_TEST_NET_REALTIME // Get the test ancilla if it exists
49 # include CYGTST_DEVS_ETH_TEST_NET_REALTIME
53 // Fill in the blanks if necessary
63 #ifndef TNR_PRINT_ACTIVITY
64 # define TNR_PRINT_ACTIVITY()
69 #define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
70 static char stack[STACK_SIZE];
71 static cyg_thread thread_data;
72 static cyg_handle_t thread_handle;
75 #define MAX_PACKET 4096
79 #define PACKET_ADD ((MAX_SEND - MIN_PACKET)/NUM_PINGS)
82 static unsigned char pkt1[MAX_PACKET], pkt2[MAX_PACKET];
84 #define UNIQUEID 0x1234
89 CYG_TEST_FAIL_FINISH(s);
92 // Compute INET checksum
94 inet_cksum(u_short *addr, int len)
96 register int nleft = len;
97 register u_short *w = addr;
98 register u_short answer;
99 register u_int sum = 0;
100 u_short odd_byte = 0;
103 * Our algorithm is simple, using a 32 bit accumulator (sum),
104 * we add sequential 16 bit words to it, and at the end, fold
105 * back all the carry bits from the top 16 bits into the lower
113 /* mop up an odd byte, if necessary */
115 *(u_char *)(&odd_byte) = *(u_char *)w;
120 * add back carry outs from top 16 bits to low 16 bits
122 sum = (sum >> 16) + (sum & 0x0000ffff); /* add hi 16 to low 16 */
123 sum += (sum >> 16); /* add carry */
124 answer = ~sum; /* truncate to 16 bits */
129 show_icmp(unsigned char *pkt, int len,
130 struct sockaddr_in *from, struct sockaddr_in *to)
132 cyg_tick_count_t *tp, tv;
135 tv = cyg_current_time();
136 ip = (struct ip *)pkt;
137 if ((len < sizeof(*ip)) || ip->ip_v != IPVERSION) {
138 diag_printf("%s: Short packet or not IP! - Len: %d, Version: %d\n",
139 inet_ntoa(from->sin_addr), len, ip->ip_v);
142 icmp = (struct icmp *)(pkt + sizeof(*ip));
143 len -= (sizeof(*ip) + 8);
144 tp = (cyg_tick_count_t *)&icmp->icmp_data;
145 if (icmp->icmp_type != ICMP_ECHOREPLY) {
146 diag_printf("%s: Invalid ICMP - type: %d\n",
147 inet_ntoa(from->sin_addr), icmp->icmp_type);
150 if (icmp->icmp_id != UNIQUEID) {
151 diag_printf("%s: ICMP received for wrong id - sent: %x, recvd: %x\n",
152 inet_ntoa(from->sin_addr), UNIQUEID, icmp->icmp_id);
154 diag_printf("%d bytes from %s: ", len, inet_ntoa(from->sin_addr));
155 diag_printf("icmp_seq=%d", icmp->icmp_seq);
156 diag_printf(", time=%dms\n", (int)(tv - *tp)*10);
157 return (from->sin_addr.s_addr == to->sin_addr.s_addr);
161 ping_host(int s, struct sockaddr_in *host)
163 struct icmp *icmp = (struct icmp *)pkt1;
164 int icmp_len = MIN_PACKET;
165 int seq, ok_recv, bogus_recv;
166 cyg_tick_count_t *tp;
168 struct sockaddr_in from;
173 diag_printf("PING server %s\n", inet_ntoa(host->sin_addr));
174 for (seq = 0; seq < NUM_PINGS; seq++, icmp_len += PACKET_ADD ) {
177 icmp->icmp_type = ICMP_ECHO;
179 icmp->icmp_cksum = 0;
180 icmp->icmp_seq = seq;
181 icmp->icmp_id = 0x1234;
183 tp = (cyg_tick_count_t *)&icmp->icmp_data;
184 *tp++ = cyg_current_time();
186 for (i = sizeof(*tp); i < icmp_len; i += sizeof(*dp)) {
190 icmp->icmp_cksum = inet_cksum( (u_short *)icmp, icmp_len+8);
192 if (sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host, sizeof(*host)) < 0) {
197 // Wait for a response
198 fromlen = sizeof(from);
199 len = recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from, &fromlen);
203 icmp_len = MIN_PACKET - PACKET_ADD; // go back to small packet size
206 if (show_icmp(pkt2, len, &from, host)) {
217 ping_test(struct bootp *bp)
221 struct sockaddr_in host;
224 if ((p = getprotobyname("icmp")) == (struct protoent *)0) {
225 pexit("getprotobyname");
228 s = socket(AF_INET, SOCK_RAW, p->p_proto);
235 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
236 // Set up host address
237 host.sin_family = AF_INET;
238 host.sin_len = sizeof(host);
239 host.sin_addr = bp->bp_siaddr;
242 // Now try a bogus host
243 host.sin_addr.s_addr = htonl(ntohl(host.sin_addr.s_addr) + 32);
249 net_test(cyg_addrword_t p)
251 #ifndef CYGPKG_NET_DHCP
252 CYG_TEST_NA( "DHCP is not enabled" );
253 CYG_TEST_EXIT( "DHCP is not enabled" );
256 diag_printf("Start DHCP test\n");
258 init_all_network_interfaces();
260 for ( i = 10; i > 0; i-- ) {
261 #ifdef CYGHWR_NET_DRIVER_ETH0
263 ping_test(ð0_bootp_data);
266 // Now do the DHCP renewal ritual...
267 if ( cyg_semaphore_trywait( &dhcp_needs_attention )) {
268 if ( ! dhcp_bind() ) {
271 CYG_TEST_FAIL_FINISH( "Rebind after lease failed");
275 #ifdef CYGHWR_NET_DRIVER_ETH1
277 ping_test(ð1_bootp_data);
281 #ifndef CYGOPT_NET_DHCP_DHCP_THREAD
282 // Now do the DHCP renewal ritual...
283 if ( cyg_semaphore_trywait( &dhcp_needs_attention )) {
284 if ( ! dhcp_bind() ) {
287 CYG_TEST_FAIL_FINISH( "Rebind after lease failed");
291 #endif // not CYGOPT_NET_DHCP_DHCP_THREAD
293 if ( 1 == (i & 3) ) {
294 dhcp_release(); // relinquish leases
296 init_all_network_interfaces();
301 TNR_PRINT_ACTIVITY();
302 CYG_TEST_PASS_FINISH("Dhcp test OK");
309 // Create a main thread, so we can run the scheduler and have time 'pass'
310 cyg_thread_create(10, // Priority - just a number
312 0, // entry parameter
313 "Network test", // Name
316 &thread_handle, // Handle
317 &thread_data // Thread data structure
319 cyg_thread_resume(thread_handle); // Start it
320 cyg_scheduler_start();