]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/common/v2_0/tests/ftp_test.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / net / common / v2_0 / tests / ftp_test.c
1 //==========================================================================
2 //
3 //      tests/ftp_test.c
4 //
5 //      Simple FTP 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-01-10
24 // Purpose:      
25 // Description:  
26 //              
27 //
28 //####DESCRIPTIONEND####
29 //
30 //==========================================================================
31 // FTP test code
32
33 #include <network.h>
34
35 #ifndef CYGPKG_LIBC_STDIO
36 #define perror(s) diag_printf(#s ": %s\n", strerror(errno))
37 #endif
38
39 #define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
40 static char stack[STACK_SIZE];
41 static cyg_thread thread_data;
42 static cyg_handle_t thread_handle;
43
44 extern void
45 cyg_test_exit(void);
46
47 void
48 pexit(char *s)
49 {
50     perror(s);
51     cyg_test_exit();
52 }
53
54 static void
55 ftp_test(struct bootp *bp)
56 {
57     int s, slen;
58     socklen_t len;
59     struct sockaddr_in host, local;
60     struct servent *sent;
61     char buf[256];
62     char _USER[] = "USER anonymous\r\n";
63     char _PASS[] = "PASS none@abc.com\r\n";
64     char _QUIT[] = "QUIT\r\n";
65
66     s = socket(AF_INET, SOCK_STREAM, 0);
67     if (s < 0) {
68         pexit("stream socket");
69     }
70     sent = getservbyname("ftp", "tcp");
71     if (sent == (struct servent *)0) {
72         pexit("getservbyname");
73     }
74     // Set up host address
75     host.sin_family = AF_INET;
76     host.sin_len = sizeof(host);
77     host.sin_addr = bp->bp_siaddr;
78     host.sin_port = sent->s_port; // Network order already
79     if (connect(s, (struct sockaddr *)&host, sizeof(host)) < 0) {
80         pexit("connect");
81     }
82     len = sizeof(local);
83     if (getsockname(s, (struct sockaddr *)&local, &len) < 0) {
84         pexit("getsockname");
85     }
86     diag_printf("connected to %s.%d", inet_ntoa(host.sin_addr), ntohs(host.sin_port));
87     diag_printf(" as %s.%d\n", inet_ntoa(local.sin_addr), ntohs(local.sin_port));
88     if ((len = read(s, buf, sizeof(buf))) < 0) {
89         pexit("read 1");
90     }
91     buf[len] = '\0';
92     diag_printf(">> %s", buf);
93     // Try and log in as 'anonymous'
94     slen = strlen(_USER);
95     if ((len = write(s, _USER, slen)) != slen) {
96         if (len < 0) {
97             pexit("write 1");
98         } else {
99             diag_printf("wrote only %d bytes\n", len);
100         }
101     }
102     if ((len = read(s, buf, sizeof(buf))) < 0) {
103         pexit("read 1");
104     }
105     buf[len] = '\0';
106     diag_printf(">> %s", buf);
107     slen = strlen(_PASS);
108     if ((len = write(s, _PASS, slen)) != slen) {
109         if (len < 0) {
110             pexit("write 1");
111         } else {
112             diag_printf("wrote only %d bytes\n", len);
113         }
114     }
115     if ((len = read(s, buf, sizeof(buf))) < 0) {
116         pexit("read 2");
117     }
118     buf[len] = '\0';
119     diag_printf(">> %s", buf);
120     slen = strlen(_QUIT);
121     if ((len = write(s, _QUIT, slen)) != slen) {
122         if (len < 0) {
123             pexit("write 1");
124         } else {
125             diag_printf("wrote only %d bytes\n", len);
126         }
127     }
128     while ((len = read(s, buf, sizeof(buf))) > 0) {
129         buf[len] = '\0';
130         diag_printf(">> %s", buf);
131     }
132     if (len < 0) {
133         perror("read 3");
134     }
135     close(s);
136 }
137
138 void
139 net_test(cyg_addrword_t param)
140 {
141     diag_printf("Start FTP test\n");
142     init_all_network_interfaces();
143 #ifdef CYGHWR_NET_DRIVER_ETH0
144     if (eth0_up) {
145         ftp_test(&eth0_bootp_data);
146     }
147 #endif
148 #ifdef CYGHWR_NET_DRIVER_ETH1
149     if (eth1_up) {
150         ftp_test(&eth1_bootp_data);
151     }
152 #endif
153     cyg_test_exit();
154 }
155
156 void
157 cyg_start(void)
158 {
159     // Create a main thread, so we can run the scheduler and have time 'pass'
160     cyg_thread_create(10,                // Priority - just a number
161                       net_test,          // entry
162                       0,                 // entry parameter
163                       "Network test",    // Name
164                       &stack[0],         // Stack
165                       STACK_SIZE,        // Size
166                       &thread_handle,    // Handle
167                       &thread_data       // Thread data structure
168             );
169     cyg_thread_resume(thread_handle);  // Start it
170     cyg_scheduler_start();
171 }