]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/common/v2_0/tests/tftp_client_test.c
TX53 Release 2011-12-20
[karo-tx-redboot.git] / packages / net / common / v2_0 / tests / tftp_client_test.c
1 //==========================================================================
2 //
3 //      tests/tftp_client_test.c
4 //
5 //      Simple TFTP client test
6 //
7 //==========================================================================
8 //####BSDCOPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 //
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.
14 //
15 // -------------------------------------------
16 //
17 //####BSDCOPYRIGHTEND####
18 //==========================================================================
19 //#####DESCRIPTIONBEGIN####
20 //
21 // Author(s):    gthomas
22 // Contributors: gthomas
23 // Date:         2000-04-07
24 // Purpose:      
25 // Description:  
26 //              
27 //
28 //####DESCRIPTIONEND####
29 //
30 //==========================================================================
31 // TFTP test code
32
33 #include <network.h>
34 #include <tftp_support.h>
35
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;
42
43 #define min(x,y) (x<y ? x : y)
44
45 extern void
46 cyg_test_exit(void);
47
48 void
49 pexit(char *s)
50 {
51     perror(s);
52     cyg_test_exit();
53 }
54
55 static char buf[32*1024];
56
57 #define GETFILE "/tftpboot/tftp_get"
58 #define PUTFILE "/tftpboot/tftp_put"
59
60 static void
61 tftp_test(struct bootp *bp)
62 {
63     int res, err, len;
64     struct sockaddr_in host;
65 #ifdef CYGPKG_NET_INET6
66     struct sockaddr_in6 ipv6router;
67     char server[64];
68 #endif
69
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;
74     host.sin_port = 0;
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);
78     if (res > 0) {
79         diag_dump_buf(buf, min(res,1024));
80     }
81     len = res;
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);
86
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");
91       cyg_test_exit();
92     }
93
94     getnameinfo((struct sockaddr *)&ipv6router,sizeof(ipv6router),
95                 server, sizeof(server), 0 ,0 ,NI_NUMERICHOST);
96
97     diag_printf("Trying tftp_get %s using IPv6 from %16s...\n", GETFILE, server);
98
99     res = tftp_client_get( GETFILE, server, 0, buf, sizeof(buf), 
100                            TFTP_OCTET, &err);
101     diag_printf("IPv6 res = %d, err = %d\n", res, err);
102     if (res > 0) {
103         diag_dump_buf(buf, min(res,1024));
104     }
105     len = res;
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);
110 #endif
111 }
112
113 void
114 net_test(cyg_addrword_t param)
115 {
116     diag_printf("Start TFTP test\n");
117     init_all_network_interfaces();
118 #ifdef CYGHWR_NET_DRIVER_ETH0
119     if (eth0_up) {
120         tftp_test(&eth0_bootp_data);
121     }
122 #endif
123 #ifdef CYGHWR_NET_DRIVER_ETH1
124     if (eth1_up) {
125         tftp_test(&eth1_bootp_data);
126     }
127 #endif
128     cyg_test_exit();
129 }
130
131 void
132 cyg_start(void)
133 {
134     // Create a main thread, so we can run the scheduler and have time 'pass'
135     cyg_thread_create(10,                // Priority - just a number
136                       net_test,          // entry
137                       0,                 // entry parameter
138                       "Network test",    // Name
139                       &stack[0],         // Stack
140                       STACK_SIZE,        // Size
141                       &thread_handle,    // Handle
142                       &thread_data       // Thread data structure
143             );
144     cyg_thread_resume(thread_handle);  // Start it
145     cyg_scheduler_start();
146 }
147
148