Browse Source

Added TFTP test code (currently just dumps file to console).

tags/v0.9.3
Michael Brown 18 years ago
parent
commit
d1a123b1f4
4 changed files with 67 additions and 5 deletions
  1. 10
    0
      src/include/gpxe/tftp.h
  2. 6
    3
      src/net/udp/tftp.c
  3. 16
    2
      src/tests/dhcptest.c
  4. 35
    0
      src/tests/tftptest.c

+ 10
- 0
src/include/gpxe/tftp.h View File

@@ -119,6 +119,12 @@ struct tftp_session {
119 119
 	 * (i.e. that no blocks have yet been received).
120 120
 	 */
121 121
 	int state;
122
+	/** Requested data block size
123
+	 *
124
+	 * This is the "blksize" option requested from the TFTP
125
+	 * server.  It may or may not be honoured.
126
+	 */
127
+	unsigned int request_blksize;
122 128
 	/** Data block size
123 129
 	 *
124 130
 	 * This is the "blksize" option negotiated with the TFTP
@@ -140,4 +146,8 @@ struct tftp_session {
140 146
 	struct retry_timer timer;
141 147
 };
142 148
 
149
+/* Function prototypes */
150
+
151
+extern struct async_operation * tftp_get ( struct tftp_session *tftp );
152
+
143 153
 #endif /* _GPXE_TFTP_H */

+ 6
- 3
src/net/udp/tftp.c View File

@@ -207,9 +207,10 @@ static int tftp_send_rrq ( struct tftp_session *tftp, void *buf, size_t len ) {
207 207
 	end = ( buf + len );
208 208
 	if ( data > end )
209 209
 		goto overflow;
210
-	data += snprintf ( data, ( end - data ),
211
-			   "%s%coctet%cblksize%c%d%ctsize%c0",
212
-			   tftp->filename, 0, 0, 0, tftp->blksize, 0, 0 ) + 1;
210
+	data += ( snprintf ( data, ( end - data ),
211
+			     "%s%coctet%cblksize%c%d%ctsize%c0",
212
+			     tftp->filename, 0, 0, 0,
213
+			     tftp->request_blksize, 0, 0 ) + 1 );
213 214
 	if ( data > end )
214 215
 		goto overflow;
215 216
 	rrq->opcode = htons ( TFTP_RRQ );
@@ -456,6 +457,8 @@ struct async_operation * tftp_get ( struct tftp_session *tftp ) {
456 457
 	tftp->timer.expired = tftp_timer_expired;
457 458
 	tftp->state = -1;
458 459
 	tftp->blksize = TFTP_DEFAULT_BLKSIZE;
460
+	if ( ! tftp->request_blksize )
461
+		tftp->request_blksize = TFTP_MAX_BLKSIZE;
459 462
 
460 463
 	/* Open UDP connection */
461 464
 	if ( ( rc = udp_open ( &tftp->udp, 0 ) ) != 0 ) {

+ 16
- 2
src/tests/dhcptest.c View File

@@ -57,6 +57,21 @@ static int test_dhcp_hello ( char *helloname ) {
57 57
 	return 0;
58 58
 }
59 59
 
60
+static int test_dhcp_tftp ( char *tftpname ) {
61
+	union {
62
+		struct sockaddr_in sin;
63
+		struct sockaddr_tcpip st;
64
+	} target;
65
+
66
+	memset ( &target, 0, sizeof ( target ) );
67
+	target.sin.sin_family = AF_INET;
68
+	target.sin.sin_port = htons ( 69 );
69
+	find_global_dhcp_ipv4_option ( DHCP_EB_SIADDR,
70
+				       &target.sin.sin_addr );
71
+
72
+	return test_tftp ( &target.st, tftpname );
73
+}
74
+
60 75
 static int test_dhcp_boot ( struct net_device *netdev, char *filename ) {
61 76
 	if ( strncmp ( filename, "aoe:", 4 ) == 0 ) {
62 77
 		return test_dhcp_aoe_boot ( netdev, &filename[4] );
@@ -65,8 +80,7 @@ static int test_dhcp_boot ( struct net_device *netdev, char *filename ) {
65 80
 	} else if ( strncmp ( filename, "hello:", 6 ) == 0 ) {
66 81
 		return test_dhcp_hello ( &filename[6] );
67 82
 	} else {
68
-		printf ( "Don't know how to boot %s\n", filename );
69
-		return -EPROTONOSUPPORT;
83
+		return test_dhcp_tftp ( filename );
70 84
 	}
71 85
 }
72 86
 

+ 35
- 0
src/tests/tftptest.c View File

@@ -0,0 +1,35 @@
1
+#include <stdint.h>
2
+#include <string.h>
3
+#include <console.h>
4
+#include <gpxe/udp.h>
5
+#include <gpxe/tftp.h>
6
+#include <gpxe/async.h>
7
+
8
+static void test_tftp_callback ( struct tftp_session *tftp __unused,
9
+				 unsigned int block __unused,
10
+				 void *data, size_t len ) {
11
+	unsigned int i;
12
+	char c;
13
+
14
+	for ( i = 0 ; i < len ; i++ ) {
15
+		c = * ( ( char * ) data + i );
16
+		if ( c == '\r' ) {
17
+			/* Print nothing */
18
+		} else if ( ( c == '\n' ) || ( c >= 32 ) || ( c <= 126 ) ) {
19
+			putchar ( c );
20
+		} else {
21
+			putchar ( '.' );
22
+		}
23
+	}	
24
+}
25
+
26
+int test_tftp ( struct sockaddr_tcpip *target, const char *filename ) {
27
+	struct tftp_session tftp;
28
+
29
+	memset ( &tftp, 0, sizeof ( tftp ) );
30
+	udp_connect ( &tftp.udp, target );
31
+	tftp.filename = filename;
32
+	tftp.callback = test_tftp_callback;
33
+
34
+	return async_wait ( tftp_get ( &tftp ) );
35
+}

Loading…
Cancel
Save