[syslinux:lwip] pxe: make tftp_pktbuf a dynamic buffer

syslinux-bot for H. Peter Anvin hpa at zytor.com
Sun Apr 24 21:03:22 PDT 2011


Commit-ID:  d05e01cf06aac976a2ba274ec7a0fbd9830782a6
Gitweb:     http://syslinux.zytor.com/commit/d05e01cf06aac976a2ba274ec7a0fbd9830782a6
Author:     H. Peter Anvin <hpa at zytor.com>
AuthorDate: Sat, 23 Apr 2011 21:04:58 -0700
Committer:  H. Peter Anvin <hpa at zytor.com>
CommitDate: Sat, 23 Apr 2011 21:06:51 -0700

pxe: make tftp_pktbuf a dynamic buffer

We don't need tftp_pktbuf for the TCP-based protocols, so allocate it
on demand.  It should be possible to get rid of it for TFTP as well.

Signed-off-by: H. Peter Anvin <hpa at zytor.com>


---
 core/fs/pxe/gpxeurl.c |    7 ++++++-
 core/fs/pxe/pxe.h     |    4 +---
 core/fs/pxe/tftp.c    |   29 ++++++++++++++++++++---------
 core/fs/pxe/tftp.h    |    6 ++++++
 4 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/core/fs/pxe/gpxeurl.c b/core/fs/pxe/gpxeurl.c
index 1615281..6f456bb 100644
--- a/core/fs/pxe/gpxeurl.c
+++ b/core/fs/pxe/gpxeurl.c
@@ -8,6 +8,8 @@ static void gpxe_close_file(struct inode *inode)
 
     file_close.FileHandle = socket->tftp_remoteport;
     pxe_call(PXENV_FILE_CLOSE, &file_close);
+
+    free(socket->tftp_pktbuf);
 }
 
 /**
@@ -68,6 +70,10 @@ void gpxe_open(struct inode *inode, const char *url)
     struct pxe_pvt_inode *socket = PVT(inode);
     int err;
 
+    socket->tftp_pktbuf = malloc(PKTBUF_SIZE);
+    if (!socket->tftp_pktbuf)
+	return;
+
     snprintf(lowurl, sizeof lowurl, "%s", url);
     file_open.Status        = PXENV_STATUS_BAD_FUNC;
     file_open.FileName      = FAR_PTR(lowurl);
@@ -82,4 +88,3 @@ void gpxe_open(struct inode *inode, const char *url)
 }
 
 #endif /* GPXE */
-
diff --git a/core/fs/pxe/pxe.h b/core/fs/pxe/pxe.h
index 7d5c43e..db1a566 100644
--- a/core/fs/pxe/pxe.h
+++ b/core/fs/pxe/pxe.h
@@ -26,8 +26,6 @@
 /*
  * Some basic defines...
  */
-#define TFTP_BLOCKSIZE_LG2 9
-#define TFTP_BLOCKSIZE  (1 << TFTP_BLOCKSIZE_LG2)
 #define PKTBUF_SIZE     2048			/*  */
 
 #define is_digit(c)     (((c) >= '0') && ((c) <= '9'))
@@ -141,7 +139,7 @@ struct pxe_pvt_inode {
     uint8_t  tftp_unused[3];   /* Currently unused */
     void (*fill_buffer)(struct inode *inode);
     void (*close)(struct inode *inode);
-    char     tftp_pktbuf[PKTBUF_SIZE];
+    char    *tftp_pktbuf;      /* Packet buffer */
 } __attribute__ ((packed));
 
 #define PVT(i) ((struct pxe_pvt_inode *)((i)->pvt))
diff --git a/core/fs/pxe/tftp.c b/core/fs/pxe/tftp.c
index c5a757c..89b21ae 100644
--- a/core/fs/pxe/tftp.c
+++ b/core/fs/pxe/tftp.c
@@ -37,6 +37,7 @@ static void tftp_close_file(struct inode *inode)
 	netconn_delete(socket->conn);
 	socket->conn = NULL;
     }
+    free(socket->tftp_pktbuf);
 }
 
 /**
@@ -313,7 +314,6 @@ wait_pkt:
 
     /* filesize <- -1 == unknown */
     inode->size = -1;
-    /* Default blksize unless blksize option negotiated */
     socket->tftp_blksize = TFTP_BLOCKSIZE;
     buffersize = nbuf_len - 2;	  /* bytes after opcode */
     if (buffersize < 0)
@@ -349,8 +349,13 @@ wait_pkt:
             goto wait_pkt;
         socket->tftp_lastpkt = blk_num;
         if (buffersize > TFTP_BLOCKSIZE)
-            goto err_reply;  /* Corrupt */
-        else if (buffersize < TFTP_BLOCKSIZE) {
+            goto err_reply;	/* Corrupt */
+
+	socket->tftp_pktbuf = malloc(TFTP_BLOCKSIZE);
+	if (!socket->tftp_pktbuf)
+	    goto err_reply;	/* Internal error */
+
+        if (buffersize < TFTP_BLOCKSIZE) {
             /*
              * This is the final EOF packet, already...
              * We know the filesize, but we also want to
@@ -435,23 +440,29 @@ wait_pkt:
             }
 	    *opdata_ptr = opdata;
 	}
+
+	/* Parsing successful, allocate buffer */
+	socket->tftp_pktbuf = malloc(socket->tftp_blksize);
+	if (!socket->tftp_pktbuf)
+	    goto err_reply;
 	break;
 
     default:
 	printf("TFTP unknown opcode %d\n", ntohs(opcode));
 	goto err_reply;
     }
+
+err_reply:
+    /* Build the TFTP error packet */
+    tftp_error(inode, TFTP_EOPTNEG, "TFTP protocol error");
+    inode->size = 0;
+
 done:
     if (!inode->size) {
+	free(socket->tftp_pktbuf);
 	netconn_delete(socket->conn);
 	socket->conn = NULL;
     }
     return;
 
-err_reply:
-    /* Build the TFTP error packet */
-    tftp_error(inode, TFTP_EOPTNEG, "TFTP protocol error");
-    printf("TFTP server sent an incomprehesible reply\n");
-    kaboom();
 }
-
diff --git a/core/fs/pxe/tftp.h b/core/fs/pxe/tftp.h
index b8b7f10..114c221 100644
--- a/core/fs/pxe/tftp.h
+++ b/core/fs/pxe/tftp.h
@@ -23,6 +23,12 @@
 #define TFTP_PORT	 69
 
 /*
+ * TFTP default block size
+ */
+#define TFTP_BLOCKSIZE_LG2 9
+#define TFTP_BLOCKSIZE  (1 << TFTP_BLOCKSIZE_LG2)
+
+/*
  * TFTP operation codes
  */
 #define TFTP_RRQ	 htons(1)		// Read rest



More information about the Syslinux-commits mailing list