summaryrefslogtreecommitdiff
path: root/ecos/packages/net/common/current/tests/tftp_client_test.c
blob: a35e186e02c534e585c1ed1ce6c94af67371e806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//==========================================================================
//
//      tests/tftp_client_test.c
//
//      Simple TFTP client test
//
//==========================================================================
// ####BSDALTCOPYRIGHTBEGIN####                                             
// -------------------------------------------                              
// Portions of this software may have been derived from FreeBSD, OpenBSD,   
// or other sources, and if so are covered by the appropriate copyright     
// and license included herein.                                             
// -------------------------------------------                              
// ####BSDALTCOPYRIGHTEND####                                               
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):    gthomas
// Contributors: gthomas
// Date:         2000-04-07
// Purpose:      
// Description:  
//              
//
//####DESCRIPTIONEND####
//
//==========================================================================
// TFTP test code

#include <network.h>
#include <tftp_support.h>

// Note: the TFTP client calls need at least (SEGSIZE==512)+4
// additional bytes of workspace, thus the padding.
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL+0x1000)
static char stack[STACK_SIZE];
static cyg_thread thread_data;
static cyg_handle_t thread_handle;

#define min(x,y) (x<y ? x : y)

extern void
cyg_test_exit(void);

void
pexit(char *s)
{
    perror(s);
    cyg_test_exit();
}

static char buf[32*1024];

#define GETFILE "/tftpboot/tftp_get"
#define PUTFILE "/tftpboot/tftp_put"

static void
tftp_test(struct bootp *bp)
{
    int res, err, len;
    struct sockaddr_in host;
#ifdef CYGPKG_NET_INET6
    struct sockaddr_in6 ipv6router;
    char server[64];
#endif

    memset((char *)&host, 0, sizeof(host));
    host.sin_len = sizeof(host);
    host.sin_family = AF_INET;
    host.sin_addr = bp->bp_siaddr;
    host.sin_port = 0;
    diag_printf("Trying tftp_get %s %16s...\n", GETFILE, inet_ntoa(host.sin_addr));
    res = tftp_get( GETFILE, &host, buf, sizeof(buf), TFTP_OCTET, &err);
    diag_printf("res = %d, err = %d\n", res, err);
    if (res > 0) {
        diag_dump_buf(buf, min(res,1024));
    }
    len = res;
    diag_printf("Trying tftp_put %s %16s, length %d\n",
                PUTFILE, inet_ntoa(host.sin_addr), len);
    res = tftp_put( PUTFILE, &host, buf, len, TFTP_OCTET, &err);
    diag_printf("put - res: %d\n", res);

#ifdef CYGPKG_NET_INET6
    // Wait for router solicit process to happen.
    if (!cyg_net_get_ipv6_advrouter(&ipv6router)) {
      diag_printf("No router advertisement recieved\n");
      cyg_test_exit();
    }

    getnameinfo((struct sockaddr *)&ipv6router,sizeof(ipv6router),
		server, sizeof(server), 0 ,0 ,NI_NUMERICHOST);

    diag_printf("Trying tftp_get %s using IPv6 from %16s...\n", GETFILE, server);

    res = tftp_client_get( GETFILE, server, 0, buf, sizeof(buf), 
			   TFTP_OCTET, &err);
    diag_printf("IPv6 res = %d, err = %d\n", res, err);
    if (res > 0) {
        diag_dump_buf(buf, min(res,1024));
    }
    len = res;
    diag_printf("Trying tftp_put %s using IPv6 to %16s, length %d\n",
                PUTFILE, server, len);
    res = tftp_client_put( PUTFILE, server, 0, buf, len, TFTP_OCTET, &err);
    diag_printf("put - res: %d\n", res);
#endif
}

void
net_test(cyg_addrword_t param)
{
    diag_printf("Start TFTP test\n");
    init_all_network_interfaces();
#ifdef CYGHWR_NET_DRIVER_ETH0
    if (eth0_up) {
        tftp_test(&eth0_bootp_data);
    }
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
    if (eth1_up) {
        tftp_test(&eth1_bootp_data);
    }
#endif
    cyg_test_exit();
}

void
cyg_start(void)
{
    // Create a main thread, so we can run the scheduler and have time 'pass'
    cyg_thread_create(10,                // Priority - just a number
                      net_test,          // entry
                      0,                 // entry parameter
                      "Network test",    // Name
                      &stack[0],         // Stack
                      STACK_SIZE,        // Size
                      &thread_handle,    // Handle
                      &thread_data       // Thread data structure
            );
    cyg_thread_resume(thread_handle);  // Start it
    cyg_scheduler_start();
}