OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [common/] [current/] [tests/] [ftp_test.c] - Blame information for rev 856

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.