1 //==========================================================================
3 // tests/tftp_client_test.c
5 // Simple TFTP client test
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 //==========================================================================
34 #include <tftp_support.h>
36 // Note: the TFTP client calls need at least (SEGSIZE==512)+4
37 // additional bytes of workspace, thus the padding.
38 #define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL+0x1000)
39 static char stack[STACK_SIZE];
40 static cyg_thread thread_data;
41 static cyg_handle_t thread_handle;
43 #define min(x,y) (x<y ? x : y)
55 static char buf[32*1024];
57 #define GETFILE "/tftpboot/tftp_get"
58 #define PUTFILE "/tftpboot/tftp_put"
61 tftp_test(struct bootp *bp)
64 struct sockaddr_in host;
65 #ifdef CYGPKG_NET_INET6
66 struct sockaddr_in6 ipv6router;
70 memset((char *)&host, 0, sizeof(host));
71 host.sin_len = sizeof(host);
72 host.sin_family = AF_INET;
73 host.sin_addr = bp->bp_siaddr;
75 diag_printf("Trying tftp_get %s %16s...\n", GETFILE, inet_ntoa(host.sin_addr));
76 res = tftp_get( GETFILE, &host, buf, sizeof(buf), TFTP_OCTET, &err);
77 diag_printf("res = %d, err = %d\n", res, err);
79 diag_dump_buf(buf, min(res,1024));
82 diag_printf("Trying tftp_put %s %16s, length %d\n",
83 PUTFILE, inet_ntoa(host.sin_addr), len);
84 res = tftp_put( PUTFILE, &host, buf, len, TFTP_OCTET, &err);
85 diag_printf("put - res: %d\n", res);
87 #ifdef CYGPKG_NET_INET6
88 // Wait for router solicit process to happen.
89 if (!cyg_net_get_ipv6_advrouter(&ipv6router)) {
90 diag_printf("No router advertisement received\n");
94 getnameinfo((struct sockaddr *)&ipv6router,sizeof(ipv6router),
95 server, sizeof(server), 0 ,0 ,NI_NUMERICHOST);
97 diag_printf("Trying tftp_get %s using IPv6 from %16s...\n", GETFILE, server);
99 res = tftp_client_get( GETFILE, server, 0, buf, sizeof(buf),
101 diag_printf("IPv6 res = %d, err = %d\n", res, err);
103 diag_dump_buf(buf, min(res,1024));
106 diag_printf("Trying tftp_put %s using IPv6 to %16s, length %d\n",
107 PUTFILE, server, len);
108 res = tftp_client_put( PUTFILE, server, 0, buf, len, TFTP_OCTET, &err);
109 diag_printf("put - res: %d\n", res);
114 net_test(cyg_addrword_t param)
116 diag_printf("Start TFTP test\n");
117 init_all_network_interfaces();
118 #ifdef CYGHWR_NET_DRIVER_ETH0
120 tftp_test(ð0_bootp_data);
123 #ifdef CYGHWR_NET_DRIVER_ETH1
125 tftp_test(ð1_bootp_data);
134 // Create a main thread, so we can run the scheduler and have time 'pass'
135 cyg_thread_create(10, // Priority - just a number
137 0, // entry parameter
138 "Network test", // Name
141 &thread_handle, // Handle
142 &thread_data // Thread data structure
144 cyg_thread_resume(thread_handle); // Start it
145 cyg_scheduler_start();