Imported Upstream version 7.59.0
[platform/upstream/curl.git] / tests / server / tftpd.c
index b9a0562..a8b5651 100644 (file)
@@ -15,7 +15,7 @@
  */
 
 /*
- * Copyright (c) 1983 Regents of the University of California.
+ * Copyright (c) 1983, 2016 Regents of the University of California.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -47,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>
@@ -60,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
@@ -83,9 +78,7 @@
 #endif
 
 #include <setjmp.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
+
 #ifdef HAVE_PWD_H
 #include <pwd.h>
 #endif
@@ -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 {
@@ -204,16 +199,8 @@ static curl_socklen_t fromlen;
 
 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() */
+static unsigned int timeout;
+static unsigned int maxtimeout = 5 * TIMEOUT;
 
 #ifdef ENABLE_IPV6
 static bool use_ipv6 = FALSE;
@@ -221,7 +208,7 @@ static bool use_ipv6 = FALSE;
 static const char *ipv_inuse = "IPv4";
 
 const  char *serverlogfile = DEFAULT_LOGFILE;
-static char *pidname= (char *)".tftpd.pid";
+static const char *pidname = ".tftpd.pid";
 static int serverlogslocked = 0;
 static int wrotepidfile = 0;
 
@@ -230,7 +217,7 @@ static sigjmp_buf timeoutbuf;
 #endif
 
 #if defined(HAVE_ALARM) && defined(SIGALRM)
-static int rexmtval = TIMEOUT;
+static const unsigned int rexmtval = TIMEOUT;
 #endif
 
 /* do-nothing macro replacement for systems which lack siginterrupt() */
@@ -259,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;
@@ -277,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);
@@ -372,41 +354,53 @@ 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)
 {
 #ifdef SIGHUP
   /* ignore SIGHUP signal */
-  if((old_sighup_handler = signal(SIGHUP, SIG_IGN)) == SIG_ERR)
-    logmsg("cannot install SIGHUP handler: %s", strerror(ERRNO));
+  old_sighup_handler = signal(SIGHUP, SIG_IGN);
+  if(old_sighup_handler == SIG_ERR)
+    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));
+  old_sigpipe_handler = signal(SIGPIPE, SIG_IGN);
+  if(old_sigpipe_handler == SIG_ERR)
+    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));
+  old_sigint_handler = signal(SIGINT, exit_signal_handler);
+  if(old_sigint_handler == SIG_ERR)
+    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));
+  old_sigterm_handler = signal(SIGTERM, exit_signal_handler);
+  if(old_sigterm_handler == SIG_ERR)
+    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 */
+  old_sigbreak_handler = signal(SIGBREAK, exit_signal_handler);
+  if(old_sigbreak_handler == SIG_ERR)
+    logmsg("cannot install SIGBREAK handler: %s", strerror(errno));
+  else
+    siginterrupt(SIGBREAK, 1);
+#endif
 }
 
 static void restore_signal_handlers(void)
@@ -427,6 +421,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
 }
 
 /*
@@ -466,7 +464,7 @@ static int readit(struct testcase *test, struct tftphdr **dpp,
   current = !current;             /* "incr" current */
 
   b = &bfs[current];              /* look at new buffer */
-  if (b->counter == BF_FREE)      /* if it's empty */
+  if(b->counter == BF_FREE)      /* if it's empty */
     read_ahead(test, convert);    /* fill it */
 
   *dpp = &b->buf.hdr;             /* set caller's ptr */
@@ -475,7 +473,7 @@ static int readit(struct testcase *test, struct tftphdr **dpp,
 
 /*
  * fill the input buffer, doing ascii conversions if requested
- * conversions are  lf -> cr,lf  and cr -> cr, nul
+ * conversions are  lf -> cr, lf  and cr -> cr, nul
  */
 static void read_ahead(struct testcase *test,
                        int convert /* if true, convert to ascii */)
@@ -487,13 +485,13 @@ static void read_ahead(struct testcase *test,
   struct tftphdr *dp;
 
   b = &bfs[nextone];              /* look at "next" buffer */
-  if (b->counter != BF_FREE)      /* nop if not free */
+  if(b->counter != BF_FREE)      /* nop if not free */
     return;
   nextone = !nextone;             /* "incr" next buffer ptr */
 
   dp = &b->buf.hdr;
 
-  if (convert == 0) {
+  if(convert == 0) {
     /* The former file reading code did this:
        b->counter = read(fileno(file), dp->th_data, SEGSIZE); */
     size_t copy_n = MIN(SEGSIZE, test->rcount);
@@ -507,9 +505,9 @@ static void read_ahead(struct testcase *test,
   }
 
   p = dp->th_data;
-  for (i = 0 ; i < SEGSIZE; i++) {
-    if (newline) {
-      if (prevchar == '\n')
+  for(i = 0 ; i < SEGSIZE; i++) {
+    if(newline) {
+      if(prevchar == '\n')
         c = '\n';       /* lf to cr,lf */
       else
         c = '\0';       /* cr to cr,nul */
@@ -517,13 +515,13 @@ static void read_ahead(struct testcase *test,
     }
     else {
       if(test->rcount) {
-        c=test->rptr[0];
+        c = test->rptr[0];
         test->rptr++;
         test->rcount--;
       }
       else
         break;
-      if (c == '\n' || c == '\r') {
+      if(c == '\n' || c == '\r') {
         prevchar = c;
         c = '\r';
         newline = 1;
@@ -537,12 +535,12 @@ 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 */
   current = !current;             /* switch to other buffer */
-  if (bfs[current].counter != BF_FREE)     /* if not free */
+  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 =  &bfs[current].buf.hdr;
@@ -551,7 +549,7 @@ static int writeit(struct testcase *test, struct tftphdr **dpp,
 
 /*
  * Output a buffer to a file, converting from netascii if requested.
- * CR,NUL -> CR  and CR,LF => LF.
+ * CR, NUL -> CR  and CR, LF => LF.
  * Note spec is undefined if we get CR as last byte of file or a
  * CR followed by anything else.  In this case we leave it alone.
  */
@@ -566,13 +564,17 @@ static ssize_t write_behind(struct testcase *test, int convert)
   struct tftphdr *dp;
 
   b = &bfs[nextone];
-  if (b->counter < -1)            /* anything to flush? */
+  if(b->counter < -1)            /* anything to flush? */
     return 0;                     /* just nop if nothing to do */
 
   if(!test->ofile) {
     char outfile[256];
-    snprintf(outfile, sizeof(outfile), "log/upload.%ld", test->num);
-    test->ofile=open(outfile, O_CREAT|O_RDWR, 0777);
+    snprintf(outfile, sizeof(outfile), "log/upload.%ld", test->testno);
+#ifdef WIN32
+    test->ofile = open(outfile, O_CREAT|O_RDWR|O_BINARY, 0777);
+#else
+    test->ofile = open(outfile, O_CREAT|O_RDWR, 0777);
+#endif
     if(test->ofile == -1) {
       logmsg("Couldn't create and/or open file %s for upload!", outfile);
       return -1; /* failure! */
@@ -585,27 +587,28 @@ static ssize_t write_behind(struct testcase *test, int convert)
   nextone = !nextone;             /* incr for next time */
   writebuf = dp->th_data;
 
-  if (count <= 0)
+  if(count <= 0)
     return -1;                    /* nak logic? */
 
-  if (convert == 0)
+  if(convert == 0)
     return write(test->ofile, writebuf, count);
 
   p = writebuf;
   ct = count;
-  while (ct--) {                  /* loop over the buffer */
+  while(ct--) {                   /* loop over the buffer */
     c = *p++;                     /* pick up a character */
-    if (prevchar == '\r') {       /* if prev char was cr */
-      if (c == '\n')              /* if have cr,lf then just */
+    if(prevchar == '\r') {        /* if prev char was cr */
+      if(c == '\n')               /* if have cr,lf then just */
         lseek(test->ofile, -1, SEEK_CUR); /* smash lf on top of the cr */
       else
-        if (c == '\0')            /* if have cr,nul then */
+        if(c == '\0')             /* if have cr,nul then */
           goto skipit;            /* just skip over the putc */
       /* else just fall through and allow it */
     }
     /* formerly
        putc(c, file); */
-    write(test->ofile, &c, 1);
+    if(1 != write(test->ofile, &c, 1))
+      break;
     skipit:
     prevchar = c;
   }
@@ -636,13 +639,13 @@ static int synchnet(curl_socket_t f /* socket to flush */)
   srvr_sockaddr_union_t fromaddr;
   curl_socklen_t fromaddrlen;
 
-  for (;;) {
+  for(;;) {
 #if defined(HAVE_IOCTLSOCKET)
     (void) ioctlsocket(f, FIONREAD, &i);
 #else
     (void) ioctl(f, FIONREAD, &i);
 #endif
-    if (i) {
+    if(i) {
       j++;
 #ifdef ENABLE_IPV6
       if(!use_ipv6)
@@ -775,7 +778,7 @@ int main(int argc, char **argv)
   }
 
   flag = 1;
-  if (0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
+  if(0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
             (void *)&flag, sizeof(flag))) {
     error = SOCKERRNO;
     logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
@@ -818,7 +821,7 @@ int main(int argc, char **argv)
 
   logmsg("Running %s version on port UDP/%d", ipv_inuse, (int)port);
 
-  for (;;) {
+  for(;;) {
     fromlen = sizeof(from);
 #ifdef ENABLE_IPV6
     if(!use_ipv6)
@@ -832,7 +835,7 @@ int main(int argc, char **argv)
                           &from.sa, &fromlen);
     if(got_exit_signal)
       break;
-    if (n < 0) {
+    if(n < 0) {
       logmsg("recvfrom");
       result = 3;
       break;
@@ -866,7 +869,7 @@ int main(int argc, char **argv)
         result = 2;
         break;
       }
-      if (connect(peer, &from.sa, sizeof(from.sa6)) < 0) {
+      if(connect(peer, &from.sa, sizeof(from.sa6)) < 0) {
         logmsg("connect: fail");
         result = 1;
         break;
@@ -878,12 +881,11 @@ int main(int argc, char **argv)
 
     tp = &buf.hdr;
     tp->th_opcode = ntohs(tp->th_opcode);
-    if (tp->th_opcode == opcode_RRQ || tp->th_opcode == opcode_WRQ) {
+    if(tp->th_opcode == opcode_RRQ || tp->th_opcode == opcode_WRQ) {
       memset(&test, 0, sizeof(test));
-      if (do_tftp(&test, tp, n) < 0)
+      if(do_tftp(&test, tp, n) < 0)
         break;
-      if(test.buffer)
-        free(test.buffer);
+      free(test.buffer);
     }
     sclose(peer);
     peer = CURL_SOCKET_BAD;
@@ -955,11 +957,16 @@ 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
+  const char *option = "mode"; /* mode is implicit */
+  int toggle = 1;
 
   /* 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;
@@ -970,53 +977,160 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
 
   cp = (char *)&tp->th_stuff;
   filename = cp;
-again:
-  while (cp < &buf.storage[size]) {
-    if (*cp == '\0')
+  do {
+    bool endofit = true;
+    while(cp < &buf.storage[size]) {
+      if(*cp == '\0') {
+        endofit = false;
+        break;
+      }
+      cp++;
+    }
+    if(endofit)
+      /* no more options */
       break;
-    cp++;
-  }
-  if (*cp) {
+
+    /* before increasing pointer, make sure it is still within the legal
+       space */
+    if((cp + 1) < &buf.storage[size]) {
+      ++cp;
+      if(first) {
+        /* store the mode since we need it later */
+        mode = cp;
+        first = 0;
+      }
+      if(toggle)
+        /* name/value pair: */
+        fprintf(server, "%s: %s\n", option, cp);
+      else {
+        /* store the name pointer */
+        option = cp;
+      }
+      toggle ^= 1;
+    }
+    else
+      /* No more options */
+      break;
+  } while(1);
+
+  if(*cp) {
     nak(EBADOP);
     fclose(server);
     return 3;
   }
-  if (first) {
-    mode = ++cp;
-    first = 0;
-    goto again;
-  }
+
   /* store input protocol */
   fprintf(server, "filename: %s\n", filename);
 
-  for (cp = mode; cp && *cp; cp++)
+  for(cp = mode; cp && *cp; cp++)
     if(ISUPPER(*cp))
       *cp = (char)tolower((int)*cp);
 
   /* store input protocol */
-  fprintf(server, "mode: %s\n", mode);
   fclose(server);
 
-  for (pf = formata; pf->f_mode; pf++)
-    if (strcmp(pf->f_mode, mode) == 0)
+  for(pf = formata; pf->f_mode; pf++)
+    if(strcmp(pf->f_mode, mode) == 0)
       break;
-  if (!pf->f_mode) {
+  if(!pf->f_mode) {
     nak(EBADOP);
     return 2;
   }
   ecode = validate_access(test, filename, tp->th_opcode);
-  if (ecode) {
+  if(ecode) {
     nak(ecode);
     return 1;
   }
-  if (tp->th_opcode == opcode_WRQ)
+
+#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;
+    }
+    free(orgcmd);
+  }
+
+  return 0; /* OK! */
+}
+
+
 /*
  * Validate file access.
  */
@@ -1032,7 +1146,8 @@ static int validate_access(struct testcase *test,
 
   if(!strncmp("verifiedserver", filename, 14)) {
     char weare[128];
-    size_t count = sprintf(weare, "WE ROOLZ: %ld\r\n", (long)getpid());
+    size_t count = snprintf(weare, sizeof(weare),
+                            "WE ROOLZ: %ld\r\n", (long)getpid());
 
     logmsg("Are-we-friendly question received");
     test->buffer = strdup(weare);
@@ -1067,17 +1182,19 @@ 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);
 
     if(0 != partno)
-      sprintf(partbuf, "data%ld", partno);
+      snprintf(partbuf, sizeof(partbuf), "data%ld", partno);
 
     if(file) {
-      FILE *stream=fopen(file, "rb");
+      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);
@@ -1120,31 +1237,40 @@ static void sendtftp(struct testcase *test, struct formats *pf)
 {
   int size;
   ssize_t n;
+  /* These are volatile to live through a siglongjmp */
+  volatile unsigned short sendblock; /* block count */
+  struct tftphdr * volatile sdp = r_init(); /* data buffer */
+  struct tftphdr * const sap = &ackbuf.hdr; /* ack buffer */
+
   sendblock = 1;
 #if defined(HAVE_ALARM) && defined(SIGALRM)
   mysignal(SIGALRM, timer);
 #endif
-  sdp = r_init();
-  sap = &ackbuf.hdr;
   do {
-    size = readit(test, &sdp, pf->f_convert);
-    if (size < 0) {
-      nak(ERRNO + 100);
+    size = readit(test, (struct tftphdr **)&sdp, pf->f_convert);
+    if(size < 0) {
+      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) {
+    if(swrite(peer, sdp, size + 4) != size + 4) {
       logmsg("write");
       return;
     }
     read_ahead(test, pf->f_convert);
-    for ( ; ; ) {
+    for(;;) {
 #ifdef HAVE_ALARM
       alarm(rexmtval);        /* read the ack */
 #endif
@@ -1154,32 +1280,32 @@ static void sendtftp(struct testcase *test, struct formats *pf)
 #endif
       if(got_exit_signal)
         return;
-      if (n < 0) {
+      if(n < 0) {
         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) {
+      if(sap->th_opcode == opcode_ERROR) {
         logmsg("got ERROR");
         return;
       }
 
-      if (sap->th_opcode == opcode_ACK) {
-        if (sap->th_block == sendblock) {
+      if(sap->th_opcode == opcode_ACK) {
+        if(sap->th_block == sendblock) {
           break;
         }
         /* Re-synchronize with the other side */
         (void) synchnet(peer);
-        if (sap->th_block == (sendblock-1)) {
+        if(sap->th_block == (sendblock-1)) {
           goto send_data;
         }
       }
 
     }
     sendblock++;
-  } while (size == SEGSIZE);
+  } while(size == SEGSIZE);
 }
 
 /*
@@ -1188,27 +1314,32 @@ 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 = &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.storage[0], 4) != 4) {
+    if(swrite(peer, &ackbuf.storage[0], 4) != 4) {
       logmsg("write: fail\n");
       goto abort;
     }
     write_behind(test, pf->f_convert);
-    for ( ; ; ) {
+    for(;;) {
 #ifdef HAVE_ALARM
       alarm(rexmtval);
 #endif
@@ -1218,38 +1349,39 @@ send_ack:
 #endif
       if(got_exit_signal)
         goto abort;
-      if (n < 0) {                       /* really? */
+      if(n < 0) {                       /* really? */
         logmsg("read: fail\n");
         goto abort;
       }
-      rdp->th_opcode = ntohs((u_short)rdp->th_opcode);
-      rdp->th_block = ntohs((u_short)rdp->th_block);
-      if (rdp->th_opcode == opcode_ERROR)
+      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) {
-        if (rdp->th_block == recvblock) {
+      if(rdp->th_opcode == opcode_DATA) {
+        if(rdp->th_block == recvblock) {
           break;                         /* normal */
         }
         /* Re-synchronize with the other side */
         (void) synchnet(peer);
-        if (rdp->th_block == (recvblock-1))
+        if(rdp->th_block == (recvblock-1))
           goto send_ack;                 /* rexmit */
       }
     }
 
     size = writeit(test, &rdp, (int)(n - 4), pf->f_convert);
-    if (size != (n-4)) {                 /* ahem */
-      if (size < 0)
-        nak(ERRNO + 100);
+    if(size != (n-4)) {                 /* ahem */
+      if(size < 0)
+        nak(errno + 100);
       else
         nak(ENOSPACE);
       goto abort;
     }
-  } while (size == SEGSIZE);
+  } 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);
+  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 */
@@ -1262,9 +1394,9 @@ send_ack:
 #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 */
+  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:
@@ -1273,7 +1405,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)
 {
@@ -1282,19 +1414,21 @@ static void nak(int error)
   struct errmsg *pe;
 
   tp = &buf.hdr;
-  tp->th_opcode = htons((u_short)opcode_ERROR);
-  tp->th_code = htons((u_short)error);
-  for (pe = errmsgs; pe->e_code >= 0; pe++)
-    if (pe->e_code == error)
+  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;
-  if (pe->e_code < 0) {
+  if(pe->e_code < 0) {
     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.storage[0], length) != length)
+  if(swrite(peer, &buf.storage[0], length) != length)
     logmsg("nak: fail\n");
 }