[xiph-commits] r9690 - icecast/branches/kh/icecast/src

karl at svn.xiph.org karl at svn.xiph.org
Mon Aug 1 19:45:39 PDT 2005


Author: karl
Date: 2005-08-01 19:45:35 -0700 (Mon, 01 Aug 2005)
New Revision: 9690

Modified:
   icecast/branches/kh/icecast/src/client.c
   icecast/branches/kh/icecast/src/connection.c
   icecast/branches/kh/icecast/src/connection.h
   icecast/branches/kh/icecast/src/slave.c
Log:
re-arrange SSL handling slightly, less #ifdef mess in client.c


Modified: icecast/branches/kh/icecast/src/client.c
===================================================================
--- icecast/branches/kh/icecast/src/client.c	2005-08-02 00:10:01 UTC (rev 9689)
+++ icecast/branches/kh/icecast/src/client.c	2005-08-02 02:45:35 UTC (rev 9690)
@@ -148,36 +148,11 @@
         client->refbuf->len -= len;
         return len;
     }
-#ifdef HAVE_OPENSSL
-    if (client->con->ssl)
-        bytes = SSL_read (client->con->ssl, buf, len);
-    else
-#endif
-        bytes = sock_read_bytes (client->con->sock, buf, len);
+    bytes = client->con->read (client->con, buf, len);
 
-    if (bytes > 0)
-        return bytes;
-
-    if (bytes < 0)
-    {
-#ifdef HAVE_OPENSSL
-        if (client->con->ssl)
-        {
-            switch (SSL_get_error (client->con->ssl, bytes))
-            {
-                case SSL_ERROR_WANT_READ:
-                case SSL_ERROR_WANT_WRITE:
-                    return -1;
-            }
-            bytes = -1;
-        }
-        else
-#endif
-            if (sock_recoverable (sock_error()))
-                return -1;
+    if (client->con->error)
         WARN0 ("reading from connection has failed");
-    }
-    client->con->error = 1;
+
     return bytes;
 }
 
@@ -264,38 +239,14 @@
 
     client->pending_io = 0;
 #else
-    int ret;
-#ifdef HAVE_OPENSSL
-    if (client->con->ssl)
-        ret = SSL_write (client->con->ssl, buf, len);
-    else
-#endif
-        ret = sock_write_bytes (client->con->sock, buf, len);
-#endif
+    int ret = client->con->send (client->con, buf, len);
 
     if (ret < 0)
-    {
-#ifdef HAVE_OPENSSL
-        if (client->con->ssl)
-        {
-            switch (SSL_get_error (client->con->ssl, ret))
-            {
-                case SSL_ERROR_WANT_READ:
-                case SSL_ERROR_WANT_WRITE:
-                    return -1;
-            }
-            ret = -1;
-        }
-        else
-#endif
-            if (sock_recoverable (sock_error()))
-                return -1;
         DEBUG0 ("Client connection died");
-        client->con->error = 1;
-    }
-    if (ret > 0)
+    else
         client->con->sent_bytes += ret;
     return ret;
+#endif
 }
 
 void client_set_queue (client_t *client, refbuf_t *refbuf)

Modified: icecast/branches/kh/icecast/src/connection.c
===================================================================
--- icecast/branches/kh/icecast/src/connection.c	2005-08-02 00:10:01 UTC (rev 9689)
+++ icecast/branches/kh/icecast/src/connection.c	2005-08-02 02:45:35 UTC (rev 9690)
@@ -201,8 +201,63 @@
     return id;
 }
 
-connection_t *create_connection(sock_t sock, sock_t serversock, char *ip)
+
+#ifdef HAVE_OPENSSL
+static int connection_read_ssl (connection_t *con, char *buf, unsigned len)
 {
+    int bytes = SSL_read (con->ssl, buf, len);
+
+    if (bytes < 0)
+    {
+        switch (SSL_get_error (con->ssl, bytes))
+        {
+            case SSL_ERROR_WANT_READ:
+            case SSL_ERROR_WANT_WRITE:
+                return -1;
+        }
+        con->error = 1;
+    }
+    return bytes;
+}
+
+
+static int connection_send_ssl (connection_t *con, const char *buf, unsigned len)
+{
+    int bytes = SSL_write (con->ssl, buf, len);
+
+    if (bytes < 0)
+    {
+        switch (SSL_get_error (con->ssl, bytes))
+        {
+            case SSL_ERROR_WANT_READ:
+            case SSL_ERROR_WANT_WRITE:
+                return -1;
+        }
+        con->error = 1;
+    }
+    return bytes;
+}
+#endif
+
+static int connection_read (connection_t *con, char *buf, unsigned len)
+{
+    int bytes = sock_read_bytes (con->sock, buf, len);
+    if (bytes == -1 && !sock_recoverable (sock_error()))
+        con->error = 1;
+    return bytes;
+}
+
+static int connection_send (connection_t *con, const char *buf, unsigned len)
+{
+    int bytes = sock_write_bytes (con->sock, buf, len);
+    if (bytes == -1 && !sock_recoverable (sock_error()))
+        con->error = 1;
+    return bytes;
+}
+
+
+connection_t *connection_create (sock_t sock, sock_t serversock, char *ip)
+{
     connection_t *con;
     con = (connection_t *)calloc(1, sizeof(connection_t));
     if (con)
@@ -212,11 +267,25 @@
         con->con_time = global.time;
         con->id = _next_connection_id();
         con->ip = ip;
+        con->read = connection_read;
+        con->send = connection_send;
     }
 
     return con;
 }
 
+/* prepare connection for interacting with SSL
+ */
+void connection_uses_ssl (connection_t *con)
+{
+    con->read = connection_read_ssl;
+    con->send = connection_send_ssl;
+    con->ssl = SSL_new (ssl_ctx);
+    SSL_set_accept_state (con->ssl);
+    SSL_set_fd (con->ssl, con->sock);
+}
+
+
 static int wait_for_serversock(int timeout)
 {
 #ifdef HAVE_POLL
@@ -317,7 +386,7 @@
     sock = sock_accept(serversock, ip, MAX_ADDR_LEN);
     if (sock >= 0)
     {
-        con = create_connection(sock, serversock, ip);
+        con = connection_create (sock, serversock, ip);
         if (con == NULL)
             free (ip);
 
@@ -510,26 +579,12 @@
                 {
                     if (config->listeners[i].shoutcast_compat)
                         node->shoutcast = 1;
-#ifdef HAVE_OPENSSL
                     if (config->listeners[i].ssl && ssl_ok)
-                    {
-                        client->con->ssl = SSL_new (ssl_ctx);
-                        SSL_set_accept_state (client->con->ssl);
-                        SSL_set_fd (client->con->ssl, client->con->sock);
-                    }
-#endif
+                        connection_uses_ssl (client->con);
                 }
             }
             config_release_config();
 
-#ifdef HAVE_OPENSSL
-            if (node->shoutcast && client->con->ssl)
-            {
-                client_destroy (client);
-                free (node);
-                continue;
-            }
-#endif
             sock_set_blocking (client->con->sock, SOCK_NONBLOCK);
             sock_set_nodelay (client->con->sock);
 

Modified: icecast/branches/kh/icecast/src/connection.h
===================================================================
--- icecast/branches/kh/icecast/src/connection.h	2005-08-02 00:10:01 UTC (rev 9689)
+++ icecast/branches/kh/icecast/src/connection.h	2005-08-02 02:45:35 UTC (rev 9690)
@@ -42,6 +42,9 @@
     /* SSL handler */
     SSL *ssl;
 #endif
+    int (*send)(struct connection_tag *handle, const char *buf, unsigned int len);
+    int (*read)(struct connection_tag *handle, char *buf, unsigned int len);
+
     char *ip;
     char *host;
 
@@ -51,7 +54,7 @@
 void connection_shutdown(void);
 void connection_accept_loop(void);
 void connection_close(connection_t *con);
-connection_t *create_connection(sock_t sock, sock_t serversock, char *ip);
+connection_t *connection_create (sock_t sock, sock_t serversock, char *ip);
 int connection_complete_source (struct source_tag *source, connection_t *con,
         http_parser_t *parser);
 

Modified: icecast/branches/kh/icecast/src/slave.c
===================================================================
--- icecast/branches/kh/icecast/src/slave.c	2005-08-02 00:10:01 UTC (rev 9689)
+++ icecast/branches/kh/icecast/src/slave.c	2005-08-02 02:45:35 UTC (rev 9690)
@@ -233,7 +233,7 @@
                     relay->server, relay->port, relay->mount);
             break;
         }
-        con = create_connection (streamsock, -1, NULL);
+        con = connection_create (streamsock, -1, NULL);
 
         if (relay->username && relay->password)
         {



More information about the commits mailing list