[xiph-commits] r15611 - in icecast/trunk: icecast/src net

karl at svn.xiph.org karl at svn.xiph.org
Wed Jan 7 18:47:45 PST 2009


Author: karl
Date: 2009-01-07 18:47:44 -0800 (Wed, 07 Jan 2009)
New Revision: 15611

Modified:
   icecast/trunk/icecast/src/connection.c
   icecast/trunk/icecast/src/slave.c
   icecast/trunk/net/sock.c
   icecast/trunk/net/sock.h
Log:
avoid SOCK_NONBLOCK name clash with recent glibc. Add support for optional
bind parameter to connect_wto call. Some small compiler cleanups.


Modified: icecast/trunk/icecast/src/connection.c
===================================================================
--- icecast/trunk/icecast/src/connection.c	2009-01-08 02:18:11 UTC (rev 15610)
+++ icecast/trunk/icecast/src/connection.c	2009-01-08 02:47:44 UTC (rev 15611)
@@ -723,6 +723,13 @@
             /* setup client for reading incoming http */
             client->refbuf->data [PER_CLIENT_REFBUF_SIZE-1] = '\000';
 
+            if (sock_set_blocking (client->con->sock, 0) || sock_set_nodelay (client->con->sock))
+            {
+                WARN0 ("failed to set tcp options on client connection, dropping");
+                client_destroy (client);
+                continue;
+            }
+
             node = calloc (1, sizeof (client_queue_t));
             if (node == NULL)
             {
@@ -745,9 +752,6 @@
             }
             config_release_config();
 
-            sock_set_blocking (client->con->sock, SOCK_NONBLOCK);
-            sock_set_nodelay (client->con->sock);
-
             _add_request_queue (node);
             stats_event_inc (NULL, "connections");
             duration = 5;
@@ -1394,7 +1398,7 @@
                 sock_close (sock);
                 break;
             }
-            sock_set_blocking (sock, SOCK_NONBLOCK);
+            sock_set_blocking (sock, 0);
             successful = 1;
             global.serversock [count] = sock;
             count++;

Modified: icecast/trunk/icecast/src/slave.c
===================================================================
--- icecast/trunk/icecast/src/slave.c	2009-01-08 02:18:11 UTC (rev 15610)
+++ icecast/trunk/icecast/src/slave.c	2009-01-08 02:47:44 UTC (rev 15611)
@@ -277,7 +277,7 @@
                 break;
             }
             global_unlock ();
-            sock_set_blocking (streamsock, SOCK_NONBLOCK);
+            sock_set_blocking (streamsock, 0);
             client_set_queue (client, NULL);
             free (server);
             free (mount);

Modified: icecast/trunk/net/sock.c
===================================================================
--- icecast/trunk/net/sock.c	2009-01-08 02:18:11 UTC (rev 15610)
+++ icecast/trunk/net/sock.c	2009-01-08 02:47:44 UTC (rev 15611)
@@ -131,7 +131,7 @@
 #endif
 }
 
-static void sock_set_error(int val)
+void sock_set_error(int val)
 {
 #ifdef _WIN32
      WSASetLastError (val);
@@ -242,12 +242,12 @@
 #endif /* _WIN32 */
 
 /* sock_set_blocking
-**
-** set the sock blocking or nonblocking
-** SOCK_BLOCK for blocking
-** SOCK_NONBLOCK for nonblocking
-*/
-int sock_set_blocking(sock_t sock, const int block)
+ *
+ * set the sock blocking or nonblocking
+ * 1 for blocking
+ * 0 for nonblocking
+ */
+int sock_set_blocking(sock_t sock, int block)
 {
 #ifdef _WIN32
 #ifdef __MINGW32__
@@ -263,7 +263,7 @@
 #ifdef _WIN32
     return ioctlsocket(sock, FIONBIO, &varblock);
 #else
-    return fcntl(sock, F_SETFL, (block == SOCK_BLOCK) ? 0 : O_NONBLOCK);
+    return fcntl(sock, F_SETFL, (block) ? 0 : O_NONBLOCK);
 #endif
 }
 
@@ -570,6 +570,11 @@
 }
 #endif
 
+sock_t sock_connect_wto (const char *hostname, int port, int timeout)
+{
+    return sock_connect_wto_bind(hostname, port, NULL, timeout);
+}
+
 #ifdef HAVE_GETADDRINFO
 
 sock_t sock_connect_non_blocking (const char *hostname, unsigned port)
@@ -593,7 +598,7 @@
         if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol)) 
                 > -1)
         {
-            sock_set_blocking (sock, SOCK_NONBLOCK);
+            sock_set_blocking (sock, 0);
             if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 && 
                     !sock_connect_pending(sock_error()))
             {
@@ -614,10 +619,10 @@
  * timeout is 0 or less then we will wait until the OS gives up on the connect
  * The socket is returned
  */
-sock_t sock_connect_wto(const char *hostname, int port, int timeout)
+sock_t sock_connect_wto_bind (const char *hostname, int port, const char *bnd, int timeout)
 {
     sock_t sock = SOCK_ERROR;
-    struct addrinfo *ai, *head, hints;
+    struct addrinfo *ai, *head, *b_head=NULL, hints;
     char service[8];
 
     memset (&hints, 0, sizeof (hints));
@@ -634,8 +639,24 @@
         if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol)) >= 0)
         {
             if (timeout > 0)
-                sock_set_blocking (sock, SOCK_NONBLOCK);
+                sock_set_blocking (sock, 0);
 
+            if (bnd)
+            {
+                struct addrinfo b_hints;
+                memset (&b_hints, 0, sizeof(b_hints));
+                b_hints.ai_family = ai->ai_family;
+                b_hints.ai_socktype = ai->ai_socktype;
+                b_hints.ai_protocol = ai->ai_protocol;
+                if (getaddrinfo (bnd, NULL, &b_hints, &b_head) ||
+                        bind (sock, b_head->ai_addr, b_head->ai_addrlen) < 0)
+                {
+                    sock_close (sock);
+                    sock = SOCK_ERROR;
+                    break;
+                }
+            }
+
             if (connect (sock, ai->ai_addr, ai->ai_addrlen) == 0)
                 break;
 
@@ -650,7 +671,7 @@
                     if (connected == 1) /* connected */
                     {
                         if (timeout >= 0)
-                            sock_set_blocking(sock, SOCK_BLOCK);
+                            sock_set_blocking(sock, 1);
                         break;
                     }
                 }
@@ -662,8 +683,9 @@
         }
         ai = ai->ai_next;
     }
-    if (head)
-        freeaddrinfo (head);
+    if (b_head)
+        freeaddrinfo (b_head);
+    freeaddrinfo (head);
 
     return sock;
 }
@@ -747,7 +769,7 @@
     memcpy(&server.sin_addr, &sin.sin_addr, sizeof(struct sockaddr_in));
 
     server.sin_family = AF_INET;
-    server.sin_port = htons(port);
+    server.sin_port = htons((short)port);
 
     return connect(sock, (struct sockaddr *)&server, sizeof(server));
 }
@@ -760,13 +782,13 @@
     if (sock == SOCK_ERROR)
         return SOCK_ERROR;
 
-    sock_set_blocking (sock, SOCK_NONBLOCK);
+    sock_set_blocking (sock, 0);
     sock_try_connection (sock, hostname, port);
     
     return sock;
 }
 
-sock_t sock_connect_wto(const char *hostname, int port, int timeout)
+sock_t sock_connect_wto_bind (const char *hostname, int port, const char *bnd, int timeout)
 {
     sock_t sock;
 
@@ -774,9 +796,24 @@
     if (sock == SOCK_ERROR)
         return SOCK_ERROR;
 
+    if (bnd)
+    {
+        struct sockaddr_in sa;
+
+        memset(&sa, 0, sizeof(sa));
+        sa.sin_family = AF_INET;
+
+        if (inet_aton (bnd, &sa.sin_addr) == 0 ||
+            bind (sock, (struct sockaddr *)&sa, sizeof(sa)) < 0)
+        {
+            sock_close (sock);
+            return SOCK_ERROR;
+        }
+    }
+
     if (timeout)
     {
-        sock_set_blocking (sock, SOCK_NONBLOCK);
+        sock_set_blocking (sock, 0);
         if (sock_try_connection (sock, hostname, port) < 0)
         {
             int ret = sock_connected (sock, timeout);
@@ -786,7 +823,7 @@
                 return SOCK_ERROR;
             }
         }
-        sock_set_blocking(sock, SOCK_BLOCK);
+        sock_set_blocking(sock, 1);
     }
     else
     {
@@ -828,12 +865,12 @@
             return SOCK_ERROR;
         } else {
             sa.sin_family = AF_INET;
-            sa.sin_port = htons(port);
+            sa.sin_port = htons((short)port);
         }
     } else {
         sa.sin_addr.s_addr = INADDR_ANY;
         sa.sin_family = AF_INET;
-        sa.sin_port = htons(port);
+        sa.sin_port = htons((short)port);
     }
 
     /* get a socket */

Modified: icecast/trunk/net/sock.h
===================================================================
--- icecast/trunk/net/sock.h	2009-01-08 02:18:11 UTC (rev 15610)
+++ icecast/trunk/net/sock.h	2009-01-08 02:47:44 UTC (rev 15611)
@@ -65,9 +65,6 @@
 #define SOCK_ERROR (sock_t)-1
 #define SOCK_TIMEOUT -2
 
-#define SOCK_BLOCK 0
-#define SOCK_NONBLOCK 1
-
 /* sock connect macro */
 #define sock_connect(h, p) sock_connect_wto(h, p, 0)
 
@@ -76,6 +73,7 @@
 # define sock_shutdown _mangle(sock_shutdown)
 # define sock_get_localip _mangle(sock_get_localip)
 # define sock_error _mangle(sock_error)
+# define sock_set_error _mangle(sock_set_error)
 # define sock_recoverable _mangle(sock_recoverable)
 # define sock_stalled _mangle(sock_stalled)
 # define sock_valid_socket _mangle(sock_valid_socket)
@@ -85,6 +83,7 @@
 # define sock_set_keepalive _mangle(sock_set_keepalive)
 # define sock_close _mangle(sock_close)
 # define sock_connect_wto _mangle(sock_connect_wto)
+# define sock_connect_wto_bind _mangle(sock_connect_wto_bind)
 # define sock_connect_non_blocking _mangle(sock_connect_non_blocking)
 # define sock_connected _mangle(sock_connected)
 # define sock_write_bytes _mangle(sock_write_bytes)
@@ -112,10 +111,12 @@
 int sock_set_nolinger(sock_t sock);
 int sock_set_keepalive(sock_t sock);
 int sock_set_nodelay(sock_t sock);
+void sock_set_error(int val);
 int sock_close(sock_t  sock);
 
 /* Connection related socket functions */
 sock_t sock_connect_wto(const char *hostname, int port, int timeout);
+sock_t sock_connect_wto_bind(const char *hostname, int port, const char *bnd, int timeout);
 sock_t sock_connect_non_blocking(const char *host, unsigned port);
 int sock_connected(sock_t sock, int timeout);
 



More information about the commits mailing list