Imported Upstream version 7.53.1
[platform/upstream/curl.git] / tests / server / sockfilt.c
index a4496e0..78588d3 100644 (file)
@@ -5,11 +5,11 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
- * are also available at http://curl.haxx.se/docs/copyright.html.
+ * are also available at https://curl.haxx.se/docs/copyright.html.
  *
  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  * copies of the Software, and permit persons to whom the Software is
  *
  * This program is a single-threaded process.
  *
- * This program is intended to be highly portable and as such it must be kept as
- * simple as possible, due to this the only signal handling mechanisms used will
- * be those of ANSI C, and used only in the most basic form which is good enough
- * for the purpose of this program.
+ * This program is intended to be highly portable and as such it must be kept
+ * as simple as possible, due to this the only signal handling mechanisms used
+ * will be those of ANSI C, and used only in the most basic form which is good
+ * enough for the purpose of this program.
  *
  * For the above reason and the specific needs of this program signals SIGHUP,
- * SIGPIPE and SIGALRM will be simply ignored on systems where this can be done.
- * If possible, signals SIGINT and SIGTERM will be handled by this program as an
- * indication to cleanup and finish execution as soon as possible.  This will be
- * achieved with a single signal handler 'exit_signal_handler' for both signals.
+ * SIGPIPE and SIGALRM will be simply ignored on systems where this can be
+ * done.  If possible, signals SIGINT and SIGTERM will be handled by this
+ * program as an indication to cleanup and finish execution as soon as
+ * possible.  This will be achieved with a single signal handler
+ * 'exit_signal_handler' for both signals.
  *
  * The 'exit_signal_handler' upon the first SIGINT or SIGTERM received signal
  * will just set to one the global var 'got_exit_signal' storing in global var
  * 'exit_signal' the signal that triggered this change.
  *
  * Nothing fancy that could introduce problems is used, the program at certain
- * points in its normal flow checks if var 'got_exit_signal' is set and in case
- * this is true it just makes its way out of loops and functions in structured
- * and well behaved manner to achieve proper program cleanup and termination.
+ * points in its normal flow checks if var 'got_exit_signal' is set and in
+ * case this is true it just makes its way out of loops and functions in
+ * structured and well behaved manner to achieve proper program cleanup and
+ * termination.
  *
- * Even with the above mechanism implemented it is worthwile to note that other
- * signals might still be received, or that there might be systems on which it
- * is not possible to trap and ignore some of the above signals.  This implies
- * that for increased portability and reliability the program must be coded as
- * if no signal was being ignored or handled at all.  Enjoy it!
+ * Even with the above mechanism implemented it is worthwile to note that
+ * other signals might still be received, or that there might be systems on
+ * which it is not possible to trap and ignore some of the above signals.
+ * This implies that for increased portability and reliability the program
+ * must be coded as if no signal was being ignored or handled at all.  Enjoy
+ * it!
  */
 
 #ifdef HAVE_SIGNAL_H
@@ -203,36 +206,42 @@ static void install_signal_handlers(void)
 {
 #ifdef SIGHUP
   /* ignore SIGHUP signal */
-  if((old_sighup_handler = signal(SIGHUP, SIG_IGN)) == SIG_ERR)
+  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)
+  old_sigpipe_handler = signal(SIGPIPE, SIG_IGN);
+  if(old_sigpipe_handler == SIG_ERR)
     logmsg("cannot install SIGPIPE handler: %s", strerror(errno));
 #endif
 #ifdef SIGALRM
   /* ignore SIGALRM signal */
-  if((old_sigalrm_handler = signal(SIGALRM, SIG_IGN)) == SIG_ERR)
+  old_sigalrm_handler = signal(SIGALRM, SIG_IGN);
+  if(old_sigalrm_handler == SIG_ERR)
     logmsg("cannot install SIGALRM 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)
+  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)
+  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 */
-  if((old_sigbreak_handler = signal(SIGBREAK, exit_signal_handler)) == SIG_ERR)
+  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);
@@ -469,23 +478,28 @@ static void lograw(unsigned char *buffer, ssize_t len)
   unsigned char *ptr = buffer;
   char *optr = data;
   ssize_t width=0;
+  int left = sizeof(data);
 
   for(i=0; i<len; i++) {
     switch(ptr[i]) {
     case '\n':
-      sprintf(optr, "\\n");
+      snprintf(optr, left, "\\n");
       width += 2;
       optr += 2;
+      left-=2;
       break;
     case '\r':
-      sprintf(optr, "\\r");
+      snprintf(optr, left, "\\r");
       width += 2;
       optr += 2;
+      left-=2;
       break;
     default:
-      sprintf(optr, "%c", (ISGRAPH(ptr[i]) || ptr[i]==0x20) ?ptr[i]:'.');
+      snprintf(optr, left, "%c", (ISGRAPH(ptr[i]) ||
+                                  ptr[i]==0x20) ?ptr[i]:'.');
       width++;
       optr++;
+      left--;
       break;
     }
 
@@ -493,6 +507,7 @@ static void lograw(unsigned char *buffer, ssize_t len)
       logmsg("'%s'", data);
       width = 0;
       optr = data;
+      left = sizeof(data);
     }
   }
   if(width)
@@ -509,8 +524,8 @@ static void lograw(unsigned char *buffer, ssize_t len)
  * other handle types supported by WaitForMultipleObjectsEx() as
  * well as disk files, anonymous and names pipes, and character input.
  *
- * http://msdn.microsoft.com/en-us/library/windows/desktop/ms687028.aspx
- * http://msdn.microsoft.com/en-us/library/windows/desktop/ms741572.aspx
+ * https://msdn.microsoft.com/en-us/library/windows/desktop/ms687028.aspx
+ * https://msdn.microsoft.com/en-us/library/windows/desktop/ms741572.aspx
  */
 struct select_ws_wait_data {
   HANDLE handle; /* actual handle to wait for during select */
@@ -547,8 +562,8 @@ static DWORD WINAPI select_ws_wait_thread(LPVOID lpParameter)
         * Approach: Loop till either the internal event is signalled
         *           or if the end of the file has already been reached.
         */
-      while(WaitForMultipleObjectsEx(2, handles, FALSE, INFINITE, FALSE)
-            == WAIT_OBJECT_0 + 1) {
+      while(WaitForMultipleObjectsEx(1, handles, FALSE, 0, FALSE)
+            == WAIT_TIMEOUT) {
         /* get total size of file */
         length = 0;
         size.QuadPart = 0;
@@ -558,7 +573,8 @@ static DWORD WINAPI select_ws_wait_thread(LPVOID lpParameter)
           size.HighPart = length;
           /* get the current position within the file */
           pos.QuadPart = 0;
-          pos.LowPart = SetFilePointer(handle, 0, &pos.HighPart, FILE_CURRENT);
+          pos.LowPart = SetFilePointer(handle, 0, &pos.HighPart,
+                                       FILE_CURRENT);
           if((pos.LowPart != INVALID_SET_FILE_POINTER) ||
              (GetLastError() == NO_ERROR)) {
             /* compare position with size, abort if not equal */
@@ -611,8 +627,8 @@ static DWORD WINAPI select_ws_wait_thread(LPVOID lpParameter)
         * Approach: Loop till either the internal event is signalled
         *           or there is data in the pipe available for reading.
         */
-      while(WaitForMultipleObjectsEx(2, handles, FALSE, INFINITE, FALSE)
-            == WAIT_OBJECT_0 + 1) {
+      while(WaitForMultipleObjectsEx(1, handles, FALSE, 0, FALSE)
+            == WAIT_TIMEOUT) {
         /* peek into the pipe and retrieve the amount of data available */
         length = 0;
         if(PeekNamedPipe(handle, NULL, 0, NULL, &length, NULL)) {
@@ -695,7 +711,7 @@ static int select_ws(int nfds, fd_set *readfds, fd_set *writefds,
 
   /* check if we got descriptors, sleep in case we got none */
   if(!nfds) {
-    Sleep((timeout->tv_sec * 1000) + (timeout->tv_usec / 1000));
+    Sleep((timeout->tv_sec*1000)+(DWORD)(((double)timeout->tv_usec)/1000.0));
     return 0;
   }
 
@@ -856,6 +872,17 @@ static int select_ws(int nfds, fd_set *readfds, fd_set *writefds,
     }
   }
 
+  for(fds = 0; fds < nfds; fds++) {
+    if(FD_ISSET(fds, readfds))
+      logmsg("select_ws: %d is readable", fds);
+
+    if(FD_ISSET(fds, writefds))
+      logmsg("select_ws: %d is writable", fds);
+
+    if(FD_ISSET(fds, exceptfds))
+      logmsg("select_ws: %d is excepted", fds);
+  }
+
   for(idx = 0; idx < wsa; idx++) {
     WSAEventSelect(data[idx].wsasock, NULL, 0);
     WSACloseEvent(data[idx].wsaevent);
@@ -1026,7 +1053,7 @@ static bool juggle(curl_socket_t *sockfdp,
       return FALSE;
 
     logmsg("Received %c%c%c%c (on stdin)",
-           buffer[0], buffer[1], buffer[2], buffer[3] );
+           buffer[0], buffer[1], buffer[2], buffer[3]);
 
     if(!memcmp("PING", buffer, 4)) {
       /* send reply on stdout, just proving we are alive */
@@ -1037,7 +1064,7 @@ static bool juggle(curl_socket_t *sockfdp,
     else if(!memcmp("PORT", buffer, 4)) {
       /* Question asking us what PORT number we are listening to.
          Replies to PORT with "IPv[num]/[port]" */
-      sprintf((char *)buffer, "%s/%hu\n", ipv_inuse, port);
+      snprintf((char *)buffer, sizeof(buffer), "%s/%hu\n", ipv_inuse, port);
       buffer_len = (ssize_t)strlen((char *)buffer);
       snprintf(data, sizeof(data), "PORT\n%04zx\n", buffer_len);
       if(!write_stdout(data, 10))
@@ -1059,7 +1086,7 @@ static bool juggle(curl_socket_t *sockfdp,
       buffer[5] = '\0';
 
       buffer_len = (ssize_t)strtol((char *)buffer, NULL, 16);
-      if (buffer_len > (ssize_t)sizeof(buffer)) {
+      if(buffer_len > (ssize_t)sizeof(buffer)) {
         logmsg("ERROR: Buffer size (%zu bytes) too small for data size "
                "(%zd bytes)", sizeof(buffer), buffer_len);
         return FALSE;
@@ -1262,7 +1289,7 @@ static curl_socket_t sockdaemon(curl_socket_t sock,
       sclose(sock);
       return CURL_SOCKET_BAD;
     }
-    switch (localaddr.sa.sa_family) {
+    switch(localaddr.sa.sa_family) {
     case AF_INET:
       *listenport = ntohs(localaddr.sa4.sin_port);
       break;
@@ -1457,7 +1484,7 @@ int main(int argc, char *argv[])
       me.sa4.sin_family = AF_INET;
       me.sa4.sin_port = htons(connectport);
       me.sa4.sin_addr.s_addr = INADDR_ANY;
-      if (!addr)
+      if(!addr)
         addr = "127.0.0.1";
       Curl_inet_pton(AF_INET, addr, &me.sa4.sin_addr);
 
@@ -1468,7 +1495,7 @@ int main(int argc, char *argv[])
       memset(&me.sa6, 0, sizeof(me.sa6));
       me.sa6.sin6_family = AF_INET6;
       me.sa6.sin6_port = htons(connectport);
-      if (!addr)
+      if(!addr)
         addr = "::1";
       Curl_inet_pton(AF_INET6, addr, &me.sa6.sin6_addr);