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