buggy code (2)
[webserver.git] / server.c
index 10517ca72b32ece41ace52565c2146e2ac8cc851..1535ef39e5157480fdd73f5cf5beaa3fa1524d20 100644 (file)
--- a/server.c
+++ b/server.c
@@ -5,6 +5,7 @@
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #ifdef _WIN32 /* Windows */
 #include <winsock2.h>
 #include <ws2tcpip.h>
 #include <netinet/tcp.h>
 //#include <sys/types.h>
 //#include <sys/socket.h>
+#include <sys/select.h>
 #endif
 
 #include "debug.h"
 
 #include "server.h"
 
-/* types */
+/* compat */
 
 #ifdef _WIN32 /* Windows */
-typedef SOCKET socket_t;
 #define PF_INET AF_INET
 #define ERRNO (WSAGetLastError ())
 #else /* Posix */
-typedef int socket_t;
 #define closesocket close
 #define ERRNO errno
 #define SOCKET_ERROR -1
 #endif
 
+/* types */
+
 /* constants */
 
 #define BACKLOG 5
@@ -45,9 +47,9 @@ typedef int socket_t;
 
 /* gobal variables */
 
-/* open listening socket */
+/* init network context */
 
-socket_t open_listening_socket (int port)
+void init_network_context (void)
 {
 #ifdef _WIN32 /* Windows */
     WSADATA WSAData;
@@ -55,7 +57,21 @@ socket_t open_listening_socket (int port)
     assert (INVALID_SOCKET == (socket_t)-1);
     assert (SOCKET_ERROR == -1);
 #endif
+}
+
+/* terminate network context */
+
+void terminate_network_context (void)
+{
+#ifdef _WIN32 /* Windows */
+    WSACleanup ();
+#endif
+}
 
+/* open listening socket */
+
+socket_t open_listening_socket (int port)
+{
     VERBOSE (DEBUG, fprintf (stdout, "Opening socket\n"));
     socket_t sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
     if (sock == INVALID_SOCKET) {
@@ -106,73 +122,116 @@ socket_t open_listening_socket (int port)
     return sock;
 }
 
-/* close listening socket */
-void close_listening_socket (socket_t sock)
-{
-    int rc = closesocket (sock);
-    if (rc == SOCKET_ERROR) {
-        VERBOSE (ERROR, fprintf (stderr, "error: %d\n", ERRNO));
-    }
-#ifdef _WIN32 /* Windows */
-    WSACleanup ();
-#endif
-}
-
 /* accept incomming connection */
 
-int accept_incoming_connection (socket_t sock)
+socket_t accept_incoming_connection (socket_t sock)
 {
-    int connection = accept (sock, NULL, 0);
+    socket_t connection = accept (sock, NULL, 0);
     if (connection < 0) {
-        return connection;
+        return INVALID_SOCKET;
     }
 
 #ifndef _WIN32 /* POSIX */
     fcntl (connection, F_SETFL, O_NONBLOCK);
 #endif
     int val = 1;
-    rc = setsockopt (connection, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof (val));
+    int rc = setsockopt (connection, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof (val));
     if (rc < 0) {
         VERBOSE (ERROR, fprintf (stderr, "error: %s\n", "setsockopt/TCP_NODELAY"));
         closesocket (connection);
         if (rc == SOCKET_ERROR) {
             VERBOSE (ERROR, fprintf (stderr, "error: close %d\n", ERRNO));
         }
-        return -1;
+        return INVALID_SOCKET;
     }
 
     return connection;
 }
 
-/* receive data */
-int receive_data (socket_t sock, unsigned char data, int maxlen)
+/* close listening socket */
+
+void close_socket (socket_t sock)
 {
+    int rc = closesocket (sock);
+    if (rc == SOCKET_ERROR) {
+        VERBOSE (ERROR, fprintf (stderr, "error: %d\n", ERRNO));
+    }
+}
 
-    /* timeout management */
-    fd_set rfds;
-    struct timeval tv = { 0, TIMEOUT };
-    FD_ZERO (&rfds);
-    FD_SET (sock, &rfds);
+/* receive data from socket */
 
-    int retval = select (sock + 1, &rfds, NULL, NULL, &tv);
-    if (retval != 1)
-        return retval; /* 0 or SOCKET_ERROR */
+int receive_data (socket_t sock, unsigned char **pdata)
+{
+    unsigned char buffer[BUFFER_SIZE] = {0};
+    unsigned char *data = NULL;
+    int len = 0;
 
-    /* read from socket */
-    int len = read (sock, data, maxlen);
+    while (1) {
 
-    if (rc = 0) { /* sock closed */
-        return 0;
-    } else if ((rc < 0) && (ERRNO != EAGAIN)) { /* error */
-        return -1;
-    } else if (rc > 0) {
-        return len;
-    }
+        /* timeout management */
+        fd_set rfds;
+        struct timeval tv = { 0, TIMEOUT };
+        FD_ZERO (&rfds);
+        FD_SET (sock, &rfds);
+
+        int retval = select (sock + 1, &rfds, NULL, NULL, &tv);
+        if (retval != 1) { /* 0 or SOCKET_ERROR */
+            break;
+        }
 
-    /* do we retry ? */
+        /* read from socket */
+        int rc = read (sock, buffer, BUFFER_SIZE);
+
+        if (rc == 0) { /* sock closed */
+            if (data) {
+                free (data);
+                data = NULL;
+                len  = 0;
+            }
+            break;
+
+        } else if ((rc < 0) && (ERRNO != EAGAIN)) { /* error */
+            if (data) {
+                free (data);
+                data = NULL;
+            }
+            len  = -1;
+            break;
+
+        } else if (rc > 0) {
+            data = realloc (data, len + rc);
+            memcpy (data + len, buffer, rc);
+            len += rc;
+        }
+    }
 
-    return -2;
+    if (pdata != NULL) {
+        *pdata = data;
+    }
+    return len;
 }
 
+/* send data onto socket */
+
+int send_data (socket_t sock, unsigned char *data, int len)
+{
+    int index = 0;
+
+    while (index < len) {
+        int rc = write (sock, data + index, len - index);
+
+        if (rc == 0) { /* sock closed */
+            index = 0;
+            break;
+        } else if ((rc < 0) && (ERRNO != EAGAIN)) { /* error */
+            index = -1;
+            break;
+        } else if (rc > 0) {
+            index += rc;
+        }
+    }
+
+    return index;
+}
 
 /* vim: set ts=4 sw=4 et: */