Revert "Update to 7.44.0"
[platform/upstream/curl.git] / tests / server / tftpd.c
index a06b815..2555793 100644 (file)
@@ -5,7 +5,6 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * $Id$
  *
  * Trivial file transfer protocol server.
  *
@@ -48,9 +47,7 @@
  * SUCH DAMAGE.
  */
 
-#define CURL_NO_OLDIES
-
-#include "setup.h" /* portability help from the lib directory */
+#include "server_setup.h"
 
 #ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
@@ -61,9 +58,6 @@
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
 #ifdef HAVE_NETINET_IN_H
 #include <netinet/in.h>
 #endif
@@ -84,9 +78,7 @@
 #endif
 
 #include <setjmp.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
+
 #ifdef HAVE_PWD_H
 #include <pwd.h>
 #endif
@@ -97,6 +89,7 @@
 #include "curlx.h" /* from the private lib dir */
 #include "getpart.h"
 #include "util.h"
+#include "server_sockaddr.h"
 
 /* include memdebug.h last */
 #include "memdebug.h"
@@ -114,8 +107,10 @@ struct testcase {
   size_t bufsize; /* size of the data in buffer */
   char *rptr;     /* read pointer into the buffer */
   size_t rcount;  /* amount of data left to read of the file */
-  long num;       /* test case number */
+  long testno;    /* test case number */
   int ofile;      /* file descriptor for output file when uploading to us */
+
+  int writedelay; /* number of seconds between each packet */
 };
 
 struct formats {
@@ -128,6 +123,11 @@ struct errmsg {
   const char *e_msg;
 };
 
+typedef union {
+  struct tftphdr hdr;
+  char storage[PKTSIZE];
+} tftphdr_storage_t;
+
 /*
  * bf.counter values in range [-1 .. SEGSIZE] represents size of data in the
  * bf.buf buffer. Additionally it can also hold flags BF_ALLOC or BF_FREE.
@@ -135,7 +135,7 @@ struct errmsg {
 
 struct bf {
   int counter;            /* size of data in buffer, or flag */
-  char buf[PKTSIZE];      /* room for data packet */
+  tftphdr_storage_t buf;  /* room for data packet */
 };
 
 #define BF_ALLOC -3       /* alloc'd but not yet filled */
@@ -191,10 +191,10 @@ static int current;     /* index of buffer in use */
 static int newline = 0;    /* fillbuf: in middle of newline expansion */
 static int prevchar = -1;  /* putbuf: previous char (cr check) */
 
-static char buf[PKTSIZE];
-static char ackbuf[PKTSIZE];
+static tftphdr_storage_t buf;
+static tftphdr_storage_t ackbuf;
 
-static struct sockaddr_in from;
+static srvr_sockaddr_union_t from;
 static curl_socklen_t fromlen;
 
 static curl_socket_t peer = CURL_SOCKET_BAD;
@@ -202,14 +202,6 @@ static curl_socket_t peer = CURL_SOCKET_BAD;
 static int timeout;
 static int maxtimeout = 5 * TIMEOUT;
 
-static unsigned short sendblock; /* block count used by sendtftp() */
-static struct tftphdr *sdp;      /* data buffer used by sendtftp() */
-static struct tftphdr *sap;      /* ack buffer  used by sendtftp() */
-
-static unsigned short recvblock; /* block count used by recvtftp() */
-static struct tftphdr *rdp;      /* data buffer used by recvtftp() */
-static struct tftphdr *rap;      /* ack buffer  used by recvtftp() */
-
 #ifdef ENABLE_IPV6
 static bool use_ipv6 = FALSE;
 #endif
@@ -254,6 +246,10 @@ static SIGHANDLER_T old_sigint_handler  = SIG_ERR;
 static SIGHANDLER_T old_sigterm_handler = SIG_ERR;
 #endif
 
+#if defined(SIGBREAK) && defined(WIN32)
+static SIGHANDLER_T old_sigbreak_handler = SIG_ERR;
+#endif
+
 /* var which if set indicates that the program should finish execution */
 
 SIG_ATOMIC_T got_exit_signal = 0;
@@ -272,15 +268,6 @@ static struct tftphdr *w_init(void);
 
 static struct tftphdr *r_init(void);
 
-static int readit(struct testcase *test,
-                  struct tftphdr **dpp,
-                  int convert);
-
-static int writeit(struct testcase *test,
-                   struct tftphdr **dpp,
-                   int ct,
-                   int convert);
-
 static void read_ahead(struct testcase *test, int convert);
 
 static ssize_t write_behind(struct testcase *test, int convert);
@@ -367,13 +354,13 @@ static void justtimeout(int signum)
 
 static RETSIGTYPE exit_signal_handler(int signum)
 {
-  int old_errno = ERRNO;
+  int old_errno = errno;
   if(got_exit_signal == 0) {
     got_exit_signal = 1;
     exit_signal = signum;
   }
   (void)signal(signum, exit_signal_handler);
-  SET_ERRNO(old_errno);
+  errno = old_errno;
 }
 
 static void install_signal_handlers(void)
@@ -381,27 +368,34 @@ static void install_signal_handlers(void)
 #ifdef SIGHUP
   /* ignore SIGHUP signal */
   if((old_sighup_handler = signal(SIGHUP, SIG_IGN)) == SIG_ERR)
-    logmsg("cannot install SIGHUP handler: %s", strerror(ERRNO));
+    logmsg("cannot install SIGHUP handler: %s", strerror(errno));
 #endif
 #ifdef SIGPIPE
   /* ignore SIGPIPE signal */
   if((old_sigpipe_handler = signal(SIGPIPE, SIG_IGN)) == SIG_ERR)
-    logmsg("cannot install SIGPIPE handler: %s", strerror(ERRNO));
+    logmsg("cannot install SIGPIPE handler: %s", strerror(errno));
 #endif
 #ifdef SIGINT
   /* handle SIGINT signal with our exit_signal_handler */
   if((old_sigint_handler = signal(SIGINT, exit_signal_handler)) == SIG_ERR)
-    logmsg("cannot install SIGINT handler: %s", strerror(ERRNO));
+    logmsg("cannot install SIGINT handler: %s", strerror(errno));
   else
     siginterrupt(SIGINT, 1);
 #endif
 #ifdef SIGTERM
   /* handle SIGTERM signal with our exit_signal_handler */
   if((old_sigterm_handler = signal(SIGTERM, exit_signal_handler)) == SIG_ERR)
-    logmsg("cannot install SIGTERM handler: %s", strerror(ERRNO));
+    logmsg("cannot install SIGTERM handler: %s", strerror(errno));
   else
     siginterrupt(SIGTERM, 1);
 #endif
+#if defined(SIGBREAK) && defined(WIN32)
+  /* handle SIGBREAK signal with our exit_signal_handler */
+  if((old_sigbreak_handler = signal(SIGBREAK, exit_signal_handler)) == SIG_ERR)
+    logmsg("cannot install SIGBREAK handler: %s", strerror(errno));
+  else
+    siginterrupt(SIGBREAK, 1);
+#endif
 }
 
 static void restore_signal_handlers(void)
@@ -422,6 +416,10 @@ static void restore_signal_handlers(void)
   if(SIG_ERR != old_sigterm_handler)
     (void)signal(SIGTERM, old_sigterm_handler);
 #endif
+#if defined(SIGBREAK) && defined(WIN32)
+  if(SIG_ERR != old_sigbreak_handler)
+    (void)signal(SIGBREAK, old_sigbreak_handler);
+#endif
 }
 
 /*
@@ -436,7 +434,7 @@ static struct tftphdr *rw_init(int x)
   current = 0;
   bfs[1].counter = BF_FREE;
   nextone = x;                    /* ahead or behind? */
-  return (struct tftphdr *)bfs[0].buf;
+  return &bfs[0].buf.hdr;
 }
 
 static struct tftphdr *w_init(void)
@@ -464,7 +462,7 @@ static int readit(struct testcase *test, struct tftphdr **dpp,
   if (b->counter == BF_FREE)      /* if it's empty */
     read_ahead(test, convert);    /* fill it */
 
-  *dpp = (struct tftphdr *)b->buf;        /* set caller's ptr */
+  *dpp = &b->buf.hdr;             /* set caller's ptr */
   return b->counter;
 }
 
@@ -486,7 +484,7 @@ static void read_ahead(struct testcase *test,
     return;
   nextone = !nextone;             /* "incr" next buffer ptr */
 
-  dp = (struct tftphdr *)b->buf;
+  dp = &b->buf.hdr;
 
   if (convert == 0) {
     /* The former file reading code did this:
@@ -532,7 +530,7 @@ static void read_ahead(struct testcase *test,
 /* Update count associated with the buffer, get new buffer from the queue.
    Calls write_behind only if next buffer not available.
  */
-static int writeit(struct testcase *test, struct tftphdr **dpp,
+static int writeit(struct testcase *test, struct tftphdr * volatile *dpp,
                    int ct, int convert)
 {
   bfs[current].counter = ct;      /* set size of data to write */
@@ -540,7 +538,7 @@ static int writeit(struct testcase *test, struct tftphdr **dpp,
   if (bfs[current].counter != BF_FREE)     /* if not free */
     write_behind(test, convert);     /* flush it */
   bfs[current].counter = BF_ALLOC;        /* mark as alloc'd */
-  *dpp =  (struct tftphdr *)bfs[current].buf;
+  *dpp =  &bfs[current].buf.hdr;
   return ct;                      /* this is a lie of course */
 }
 
@@ -566,7 +564,7 @@ static ssize_t write_behind(struct testcase *test, int convert)
 
   if(!test->ofile) {
     char outfile[256];
-    snprintf(outfile, sizeof(outfile), "log/upload.%ld", test->num);
+    snprintf(outfile, sizeof(outfile), "log/upload.%ld", test->testno);
     test->ofile=open(outfile, O_CREAT|O_RDWR, 0777);
     if(test->ofile == -1) {
       logmsg("Couldn't create and/or open file %s for upload!", outfile);
@@ -576,7 +574,7 @@ static ssize_t write_behind(struct testcase *test, int convert)
 
   count = b->counter;             /* remember byte count */
   b->counter = BF_FREE;           /* reset flag */
-  dp = (struct tftphdr *)b->buf;
+  dp = &b->buf.hdr;
   nextone = !nextone;             /* incr for next time */
   writebuf = dp->th_data;
 
@@ -600,7 +598,8 @@ static ssize_t write_behind(struct testcase *test, int convert)
     }
     /* formerly
        putc(c, file); */
-    write(test->ofile, &c, 1);
+    if(1 != write(test->ofile, &c, 1))
+      break;
     skipit:
     prevchar = c;
   }
@@ -628,7 +627,7 @@ static int synchnet(curl_socket_t f /* socket to flush */)
 #endif
   int j = 0;
   char rbuf[PKTSIZE];
-  struct sockaddr_in fromaddr;
+  srvr_sockaddr_union_t fromaddr;
   curl_socklen_t fromaddrlen;
 
   for (;;) {
@@ -639,9 +638,16 @@ static int synchnet(curl_socket_t f /* socket to flush */)
 #endif
     if (i) {
       j++;
-      fromaddrlen = sizeof(fromaddr);
-      (void)recvfrom(f, rbuf, sizeof(rbuf), 0,
-                     (struct sockaddr *)&fromaddr, &fromaddrlen);
+#ifdef ENABLE_IPV6
+      if(!use_ipv6)
+#endif
+        fromaddrlen = sizeof(fromaddr.sa4);
+#ifdef ENABLE_IPV6
+      else
+        fromaddrlen = sizeof(fromaddr.sa6);
+#endif
+      (void) recvfrom(f, rbuf, sizeof(rbuf), 0,
+                      &fromaddr.sa, &fromaddrlen);
     }
     else
       break;
@@ -651,11 +657,7 @@ static int synchnet(curl_socket_t f /* socket to flush */)
 
 int main(int argc, char **argv)
 {
-  struct sockaddr_in me;
-#ifdef ENABLE_IPV6
-  struct sockaddr_in6 me6;
-#endif /* ENABLE_IPV6 */
-
+  srvr_sockaddr_union_t me;
   struct tftphdr *tp;
   ssize_t n = 0;
   int arg = 1;
@@ -779,19 +781,19 @@ int main(int argc, char **argv)
 #ifdef ENABLE_IPV6
   if(!use_ipv6) {
 #endif
-    memset(&me, 0, sizeof(me));
-    me.sin_family = AF_INET;
-    me.sin_addr.s_addr = INADDR_ANY;
-    me.sin_port = htons(port);
-    rc = bind(sock, (struct sockaddr *) &me, sizeof(me));
+    memset(&me.sa4, 0, sizeof(me.sa4));
+    me.sa4.sin_family = AF_INET;
+    me.sa4.sin_addr.s_addr = INADDR_ANY;
+    me.sa4.sin_port = htons(port);
+    rc = bind(sock, &me.sa, sizeof(me.sa4));
 #ifdef ENABLE_IPV6
   }
   else {
-    memset(&me6, 0, sizeof(me6));
-    me6.sin6_family = AF_INET6;
-    me6.sin6_addr = in6addr_any;
-    me6.sin6_port = htons(port);
-    rc = bind(sock, (struct sockaddr *) &me6, sizeof(me6));
+    memset(&me.sa6, 0, sizeof(me.sa6));
+    me.sa6.sin6_family = AF_INET6;
+    me.sa6.sin6_addr = in6addr_any;
+    me.sa6.sin6_port = htons(port);
+    rc = bind(sock, &me.sa, sizeof(me.sa6));
   }
 #endif /* ENABLE_IPV6 */
   if(0 != rc) {
@@ -812,8 +814,16 @@ int main(int argc, char **argv)
 
   for (;;) {
     fromlen = sizeof(from);
-    n = (ssize_t)recvfrom(sock, buf, sizeof(buf), 0,
-                          (struct sockaddr *)&from, &fromlen);
+#ifdef ENABLE_IPV6
+    if(!use_ipv6)
+#endif
+      fromlen = sizeof(from.sa4);
+#ifdef ENABLE_IPV6
+    else
+      fromlen = sizeof(from.sa6);
+#endif
+    n = (ssize_t)recvfrom(sock, &buf.storage[0], sizeof(buf.storage), 0,
+                          &from.sa, &fromlen);
     if(got_exit_signal)
       break;
     if (n < 0) {
@@ -825,23 +835,42 @@ int main(int argc, char **argv)
     set_advisor_read_lock(SERVERLOGS_LOCK);
     serverlogslocked = 1;
 
-    from.sin_family = AF_INET;
-
-    peer = socket(AF_INET, SOCK_DGRAM, 0);
-    if(CURL_SOCKET_BAD == peer) {
-      logmsg("socket");
-      result = 2;
-      break;
+#ifdef ENABLE_IPV6
+    if(!use_ipv6) {
+#endif
+      from.sa4.sin_family = AF_INET;
+      peer = socket(AF_INET, SOCK_DGRAM, 0);
+      if(CURL_SOCKET_BAD == peer) {
+        logmsg("socket");
+        result = 2;
+        break;
+      }
+      if(connect(peer, &from.sa, sizeof(from.sa4)) < 0) {
+        logmsg("connect: fail");
+        result = 1;
+        break;
+      }
+#ifdef ENABLE_IPV6
     }
-
-    if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) {
-      logmsg("connect: fail");
-      result = 1;
-      break;
+    else {
+      from.sa6.sin6_family = AF_INET6;
+      peer = socket(AF_INET6, SOCK_DGRAM, 0);
+      if(CURL_SOCKET_BAD == peer) {
+        logmsg("socket");
+        result = 2;
+        break;
+      }
+      if(connect(peer, &from.sa, sizeof(from.sa6)) < 0) {
+        logmsg("connect: fail");
+        result = 1;
+        break;
+      }
     }
+#endif
+
     maxtimeout = 5*TIMEOUT;
 
-    tp = (struct tftphdr *)buf;
+    tp = &buf.hdr;
     tp->th_opcode = ntohs(tp->th_opcode);
     if (tp->th_opcode == opcode_RRQ || tp->th_opcode == opcode_WRQ) {
       memset(&test, 0, sizeof(test));
@@ -920,11 +949,14 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
   char *filename, *mode = NULL;
   int error;
   FILE *server;
+#ifdef USE_WINSOCK
+  DWORD recvtimeout, recvtimeoutbak;
+#endif
 
   /* Open request dump file. */
   server = fopen(REQUEST_DUMP, "ab");
   if(!server) {
-    error = ERRNO;
+    error = errno;
     logmsg("fopen() failed with error: %d %s", error, strerror(error));
     logmsg("Error opening file: %s", REQUEST_DUMP);
     return -1;
@@ -936,7 +968,7 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
   cp = (char *)&tp->th_stuff;
   filename = cp;
 again:
-  while (cp < buf + size) {
+  while (cp < &buf.storage[size]) {
     if (*cp == '\0')
       break;
     cp++;
@@ -974,14 +1006,97 @@ again:
     nak(ecode);
     return 1;
   }
+
+#ifdef USE_WINSOCK
+  recvtimeout = sizeof(recvtimeoutbak);
+  getsockopt(peer, SOL_SOCKET, SO_RCVTIMEO,
+             (char*)&recvtimeoutbak, (int*)&recvtimeout);
+  recvtimeout = TIMEOUT*1000;
+  setsockopt(peer, SOL_SOCKET, SO_RCVTIMEO,
+             (const char*)&recvtimeout, sizeof(recvtimeout));
+#endif
+
   if (tp->th_opcode == opcode_WRQ)
     recvtftp(test, pf);
   else
     sendtftp(test, pf);
 
+#ifdef USE_WINSOCK
+  recvtimeout = recvtimeoutbak;
+  setsockopt(peer, SOL_SOCKET, SO_RCVTIMEO,
+             (const char*)&recvtimeout, sizeof(recvtimeout));
+#endif
+
   return 0;
 }
 
+/* Based on the testno, parse the correct server commands. */
+static int parse_servercmd(struct testcase *req)
+{
+  FILE *stream;
+  char *filename;
+  int error;
+
+  filename = test2file(req->testno);
+
+  stream=fopen(filename, "rb");
+  if(!stream) {
+    error = errno;
+    logmsg("fopen() failed with error: %d %s", error, strerror(error));
+    logmsg("  [1] Error opening file: %s", filename);
+    logmsg("  Couldn't open test file %ld", req->testno);
+    return 1; /* done */
+  }
+  else {
+    char *orgcmd = NULL;
+    char *cmd = NULL;
+    size_t cmdsize = 0;
+    int num=0;
+
+    /* get the custom server control "commands" */
+    error = getpart(&orgcmd, &cmdsize, "reply", "servercmd", stream);
+    fclose(stream);
+    if(error) {
+      logmsg("getpart() failed with error: %d", error);
+      return 1; /* done */
+    }
+
+    cmd = orgcmd;
+    while(cmd && cmdsize) {
+      char *check;
+      if(1 == sscanf(cmd, "writedelay: %d", &num)) {
+        logmsg("instructed to delay %d secs between packets", num);
+        req->writedelay = num;
+      }
+      else {
+        logmsg("Unknown <servercmd> instruction found: %s", cmd);
+      }
+      /* try to deal with CRLF or just LF */
+      check = strchr(cmd, '\r');
+      if(!check)
+        check = strchr(cmd, '\n');
+
+      if(check) {
+        /* get to the letter following the newline */
+        while((*check == '\r') || (*check == '\n'))
+          check++;
+
+        if(!*check)
+          /* if we reached a zero, get out */
+          break;
+        cmd = check;
+      }
+      else
+        break;
+    }
+    if(orgcmd)
+      free(orgcmd);
+  }
+
+  return 0; /* OK! */
+}
+
+
 /*
  * Validate file access.
  */
@@ -1032,7 +1147,9 @@ static int validate_access(struct testcase *test,
 
     logmsg("requested test number %ld part %ld", testno, partno);
 
-    test->num = testno;
+    test->testno = testno;
+
+    (void)parse_servercmd(test);
 
     file = test2file(testno);
 
@@ -1042,7 +1159,7 @@ static int validate_access(struct testcase *test,
     if(file) {
       FILE *stream=fopen(file, "rb");
       if(!stream) {
-        error = ERRNO;
+        error = errno;
         logmsg("fopen() failed with error: %d %s", error, strerror(error));
         logmsg("Error opening file: %s", file);
         logmsg("Couldn't open test file: %s", file);
@@ -1085,24 +1202,35 @@ static void sendtftp(struct testcase *test, struct formats *pf)
 {
   int size;
   ssize_t n;
+  /* This is volatile to live through a siglongjmp */
+  volatile unsigned short sendblock; /* block count */
+  struct tftphdr *sdp;      /* data buffer */
+  struct tftphdr *sap;      /* ack buffer */
+
   sendblock = 1;
 #if defined(HAVE_ALARM) && defined(SIGALRM)
   mysignal(SIGALRM, timer);
 #endif
   sdp = r_init();
-  sap = (struct tftphdr *)ackbuf;
+  sap = &ackbuf.hdr;
   do {
     size = readit(test, &sdp, pf->f_convert);
     if (size < 0) {
-      nak(ERRNO + 100);
+      nak(errno + 100);
       return;
     }
-    sdp->th_opcode = htons((u_short)opcode_DATA);
-    sdp->th_block = htons((u_short)sendblock);
+    sdp->th_opcode = htons((unsigned short)opcode_DATA);
+    sdp->th_block = htons(sendblock);
     timeout = 0;
 #ifdef HAVE_SIGSETJMP
     (void) sigsetjmp(timeoutbuf, 1);
 #endif
+    if(test->writedelay) {
+      logmsg("Pausing %d seconds before %d bytes", test->writedelay,
+             size);
+      wait_ms(1000*test->writedelay);
+    }
+
     send_data:
     if (swrite(peer, sdp, size + 4) != size + 4) {
       logmsg("write");
@@ -1113,7 +1241,7 @@ static void sendtftp(struct testcase *test, struct formats *pf)
 #ifdef HAVE_ALARM
       alarm(rexmtval);        /* read the ack */
 #endif
-      n = sread(peer, ackbuf, sizeof (ackbuf));
+      n = sread(peer, &ackbuf.storage[0], sizeof(ackbuf.storage));
 #ifdef HAVE_ALARM
       alarm(0);
 #endif
@@ -1123,8 +1251,8 @@ static void sendtftp(struct testcase *test, struct formats *pf)
         logmsg("read: fail");
         return;
       }
-      sap->th_opcode = ntohs((u_short)sap->th_opcode);
-      sap->th_block = ntohs((u_short)sap->th_block);
+      sap->th_opcode = ntohs((unsigned short)sap->th_opcode);
+      sap->th_block = ntohs(sap->th_block);
 
       if (sap->th_opcode == opcode_ERROR) {
         logmsg("got ERROR");
@@ -1153,22 +1281,27 @@ static void sendtftp(struct testcase *test, struct formats *pf)
 static void recvtftp(struct testcase *test, struct formats *pf)
 {
   ssize_t n, size;
+  /* These are volatile to live through a siglongjmp */
+  volatile unsigned short recvblock; /* block count */
+  struct tftphdr * volatile rdp;     /* data buffer */
+  struct tftphdr *rap;      /* ack buffer */
+
   recvblock = 0;
+  rdp = w_init();
 #if defined(HAVE_ALARM) && defined(SIGALRM)
   mysignal(SIGALRM, timer);
 #endif
-  rdp = w_init();
-  rap = (struct tftphdr *)ackbuf;
+  rap = &ackbuf.hdr;
   do {
     timeout = 0;
-    rap->th_opcode = htons((u_short)opcode_ACK);
-    rap->th_block = htons((u_short)recvblock);
+    rap->th_opcode = htons((unsigned short)opcode_ACK);
+    rap->th_block = htons(recvblock);
     recvblock++;
 #ifdef HAVE_SIGSETJMP
     (void) sigsetjmp(timeoutbuf, 1);
 #endif
 send_ack:
-    if (swrite(peer, ackbuf, 4) != 4) {
+    if (swrite(peer, &ackbuf.storage[0], 4) != 4) {
       logmsg("write: fail\n");
       goto abort;
     }
@@ -1187,8 +1320,8 @@ send_ack:
         logmsg("read: fail\n");
         goto abort;
       }
-      rdp->th_opcode = ntohs((u_short)rdp->th_opcode);
-      rdp->th_block = ntohs((u_short)rdp->th_block);
+      rdp->th_opcode = ntohs((unsigned short)rdp->th_opcode);
+      rdp->th_block = ntohs(rdp->th_block);
       if (rdp->th_opcode == opcode_ERROR)
         goto abort;
       if (rdp->th_opcode == opcode_DATA) {
@@ -1205,7 +1338,7 @@ send_ack:
     size = writeit(test, &rdp, (int)(n - 4), pf->f_convert);
     if (size != (n-4)) {                 /* ahem */
       if (size < 0)
-        nak(ERRNO + 100);
+        nak(errno + 100);
       else
         nak(ENOSPACE);
       goto abort;
@@ -1213,23 +1346,24 @@ send_ack:
   } while (size == SEGSIZE);
   write_behind(test, pf->f_convert);
 
-  rap->th_opcode = htons((u_short)opcode_ACK);  /* send the "final" ack */
-  rap->th_block = htons((u_short)recvblock);
-  (void) swrite(peer, ackbuf, 4);
+  rap->th_opcode = htons((unsigned short)opcode_ACK);  /* send the "final" ack */
+  rap->th_block = htons(recvblock);
+  (void) swrite(peer, &ackbuf.storage[0], 4);
 #if defined(HAVE_ALARM) && defined(SIGALRM)
   mysignal(SIGALRM, justtimeout);        /* just abort read on timeout */
   alarm(rexmtval);
 #endif
-  n = sread(peer, buf, sizeof(buf));     /* normally times out and quits */
+  /* normally times out and quits */
+  n = sread(peer, &buf.storage[0], sizeof(buf.storage));
 #ifdef HAVE_ALARM
   alarm(0);
 #endif
   if(got_exit_signal)
     goto abort;
-  if (n >= 4 &&                          /* if read some data */
-      rdp->th_opcode == opcode_DATA &&   /* and got a data block */
-      recvblock == rdp->th_block) {      /* then my last ack was lost */
-    (void) swrite(peer, ackbuf, 4);      /* resend final ack */
+  if (n >= 4 &&                               /* if read some data */
+      rdp->th_opcode == opcode_DATA &&        /* and got a data block */
+      recvblock == rdp->th_block) {           /* then my last ack was lost */
+    (void) swrite(peer, &ackbuf.storage[0], 4);  /* resend final ack */
   }
 abort:
   return;
@@ -1237,7 +1371,7 @@ abort:
 
 /*
  * Send a nak packet (error message).  Error code passed in is one of the
- * standard TFTP codes, or a UNIX errno offset by 100.
+ * standard TFTP codes, or a Unix errno offset by 100.
  */
 static void nak(int error)
 {
@@ -1245,9 +1379,9 @@ static void nak(int error)
   int length;
   struct errmsg *pe;
 
-  tp = (struct tftphdr *)buf;
-  tp->th_opcode = htons((u_short)opcode_ERROR);
-  tp->th_code = htons((u_short)error);
+  tp = &buf.hdr;
+  tp->th_opcode = htons((unsigned short)opcode_ERROR);
+  tp->th_code = htons((unsigned short)error);
   for (pe = errmsgs; pe->e_code >= 0; pe++)
     if (pe->e_code == error)
       break;
@@ -1255,10 +1389,12 @@ static void nak(int error)
     pe->e_msg = strerror(error - 100);
     tp->th_code = EUNDEF;   /* set 'undef' errorcode */
   }
-  strcpy(tp->th_msg, pe->e_msg);
   length = (int)strlen(pe->e_msg);
-  tp->th_msg[length] = '\0';
+
+  /* we use memcpy() instead of strcpy() in order to avoid buffer overflow
+   * report from glibc with FORTIFY_SOURCE */
+  memcpy(tp->th_msg, pe->e_msg, length + 1);
   length += 5;
-  if (swrite(peer, buf, length) != length)
+  if (swrite(peer, &buf.storage[0], length) != length)
     logmsg("nak: fail\n");
 }