Imported Upstream version 7.40.0
[platform/upstream/curl.git] / tests / server / tftpd.c
index 10f88a1..2555793 100644 (file)
@@ -107,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 {
@@ -200,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
@@ -274,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);
@@ -545,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 */
@@ -579,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);
@@ -1045,6 +1030,73 @@ again:
   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.
  */
@@ -1095,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);
 
@@ -1148,6 +1202,11 @@ 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);
@@ -1166,6 +1225,12 @@ static void sendtftp(struct testcase *test, struct formats *pf)
 #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");
@@ -1216,11 +1281,16 @@ 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;
@@ -1301,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)
 {