Skip to contentSkip to conversation

tftp server, second try

PATCH New Magnus Damm 1 participant 1 message Aug 15, 2004 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tftp server, second try

Hi again,

Attached is a patch that adds a tftp server to the slirp networking code, ie the patch makes it possible for the guest to read files from the host if the user network implementation enabled with -user-net.

This is the second try, the first patch contained a fatal bug that did not show on my PowerPC box (misplaced parenthesis, aargh) - I recently tried the code on a x86 box and realized that all read requests were treated as access violations... Sorry about that. Please try this patch instead, it has been tested on CVS today and 0.60.0 under Linux/PPC and 0.60.0 under Linux/x86. This patch applies both to CVS and 0.60.0.

The option -tftp is used to tell the tftp server which files that should be exported as read only data. Default is no exports, "-tftp /" will make all files on your host available to your guest. The tftp server only accepts absolute filenames, no relative paths are allowed.

If someone wants to test the code I would be happy to hand over a precompiled x86 image containing a tftp client useful for testing.

Thanks.

/ magnus

Makefile.target +1 -1
diff -urN qemu-0.6.0/Makefile.target qemu-0.6.0-tftp/Makefile.target
@@ -260,7 +260,7 @@
 DEFINES+=-I$(SRC_PATH)/slirp
 SLIRP_OBJS=cksum.o if.o ip_icmp.o ip_input.o ip_output.o \
 slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o \
-tcp_subr.o tcp_timer.o udp.o bootp.o debug.o
+tcp_subr.o tcp_timer.o udp.o bootp.o debug.o tftp.o
 VL_OBJS+=$(addprefix slirp/, $(SLIRP_OBJS))
 endif
 
slirp/slirp.h +1 -0
diff -urN qemu-0.6.0/slirp/slirp.h qemu-0.6.0-tftp/slirp/slirp.h
@@ -195,6 +195,7 @@
 #endif
 
 #include "bootp.h"
+#include "tftp.h"
 #include "libslirp.h"
 
 extern struct ttys *ttys_unit[MAX_INTERFACES];
slirp/tftp.c +343 -0

343 lines not on this page · raw message

slirp/tftp.h +30 -0
diff -urN qemu-0.6.0/slirp/tftp.h qemu-0.6.0-tftp/slirp/tftp.h
@@ -0,0 +1,30 @@
+/* tftp defines */
+
+#define TFTP_SESSIONS_MAX 3
+
+#define TFTP_SERVER	69
+
+#define TFTP_RRQ    1
+#define TFTP_WRQ    2
+#define TFTP_DATA   3
+#define TFTP_ACK    4
+#define TFTP_ERROR  5
+
+#define TFTP_FILENAME_MAX 512
+
+struct tftp_t {
+  struct ip ip;
+  struct udphdr udp;
+  u_int16_t tp_op;
+  union {
+    struct { 
+      u_int16_t tp_block_nr;
+      u_int8_t tp_buf[512];
+    } tp_data;
+    u_int8_t tp_buf[512 + 2];
+  } x;
+};
+
+extern char *tftp_prefix;
+
+void tftp_input(struct mbuf *m);
slirp/udp.c +8 -0
diff -urN qemu-0.6.0/slirp/udp.c qemu-0.6.0-tftp/slirp/udp.c
@@ -153,6 +153,14 @@
             goto bad;
         }
 
+        /*
+         *  handle TFTP
+         */
+        if (ntohs(uh->uh_dport) == TFTP_SERVER) {
+            tftp_input(m);
+            goto bad;
+        }
+
 	/*
 	 * Locate pcb for datagram.
 	 */
vl.c +11 -0
diff -urN qemu-0.6.0/vl.c qemu-0.6.0-tftp/vl.c
@@ -2097,6 +2097,7 @@
            "-tun-fd fd      use this fd as already opened tap/tun interface\n"
 #ifdef CONFIG_SLIRP
            "-user-net       use user mode network stack [default if no tap/tun script]\n"
+           "-tftp prefix    allow tftp access to files starting with prefix [only with -user-net enabled]\n"
 #endif
            "-dummy-net      use dummy network stack\n"
            "\n"
@@ -2164,6 +2165,7 @@
     QEMU_OPTION_n,
     QEMU_OPTION_tun_fd,
     QEMU_OPTION_user_net,
+    QEMU_OPTION_tftp,
     QEMU_OPTION_dummy_net,
 
     QEMU_OPTION_kernel,
@@ -2214,6 +2216,7 @@
     { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd },
 #ifdef CONFIG_SLIRP
     { "user-net", 0, QEMU_OPTION_user_net },
+    { "tftp", HAS_ARG, QEMU_OPTION_tftp },
 #endif
     { "dummy-net", 0, QEMU_OPTION_dummy_net },
 
@@ -2454,9 +2457,17 @@
                     }
                 }
                 break;
+#ifdef CONFIG_SLIRP
+            case QEMU_OPTION_tftp:
+	      {
+		extern const char *tftp_prefix;
+		tftp_prefix = optarg;
+	      }
+	      break;
             case QEMU_OPTION_user_net:
                 net_if_type = NET_IF_USER;
                 break;
+#endif
             case QEMU_OPTION_dummy_net:
                 net_if_type = NET_IF_DUMMY;
                 break;