[vlc-commits] tls: remove virtual_socket_t and clean send/recv prototypes up

Rémi Denis-Courmont git at videolan.org
Wed Dec 16 21:31:17 CET 2015


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed Dec 16 18:43:36 2015 +0200| [d3c0569cd25cb6eba33344a0a8fb6780e7753068] | committer: Rémi Denis-Courmont

tls: remove virtual_socket_t and clean send/recv prototypes up

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=d3c0569cd25cb6eba33344a0a8fb6780e7753068
---

 include/vlc_common.h            |    1 -
 include/vlc_network.h           |    8 ------
 include/vlc_tls.h               |    3 ++-
 modules/access/http/transport.c |    4 +--
 modules/misc/gnutls.c           |   55 +++++++++++++--------------------------
 modules/misc/securetransport.c  |   11 +++-----
 src/network/tls.c               |    4 +--
 7 files changed, 28 insertions(+), 58 deletions(-)

diff --git a/include/vlc_common.h b/include/vlc_common.h
index b2e304f..86c2f27 100644
--- a/include/vlc_common.h
+++ b/include/vlc_common.h
@@ -291,7 +291,6 @@ typedef struct filter_t filter_t;
 typedef struct filter_sys_t filter_sys_t;
 
 /* Network */
-typedef struct virtual_socket_t v_socket_t;
 typedef struct vlc_url_t vlc_url_t;
 
 /* Misc */
diff --git a/include/vlc_network.h b/include/vlc_network.h
index 078d3c5..221076b 100644
--- a/include/vlc_network.h
+++ b/include/vlc_network.h
@@ -132,14 +132,6 @@ int net_Subscribe (vlc_object_t *obj, int fd, const struct sockaddr *addr,
 
 VLC_API int net_SetCSCov( int fd, int sendcov, int recvcov );
 
-/* Functions to read from or write to the networking layer */
-struct virtual_socket_t
-{
-    void *p_sys;
-    int (*pf_recv) ( void *, void *, size_t );
-    int (*pf_send) ( void *, const void *, size_t );
-};
-
 VLC_API ssize_t net_Read( vlc_object_t *p_this, int fd, void *p_data, size_t i_data );
 #define net_Read(a,b,c,d) net_Read(VLC_OBJECT(a),b,c,d)
 VLC_API ssize_t net_Write( vlc_object_t *p_this, int fd, const void *p_data, size_t i_data );
diff --git a/include/vlc_tls.h b/include/vlc_tls.h
index 48e5d70..a39aee6 100644
--- a/include/vlc_tls.h
+++ b/include/vlc_tls.h
@@ -43,7 +43,8 @@ struct vlc_tls
     void *sys;
     int fd;
 
-    struct virtual_socket_t sock;
+    ssize_t (*recv)(struct vlc_tls *, void *, size_t);
+    ssize_t (*send)(struct vlc_tls *, const void *, size_t);
 };
 
 /**
diff --git a/modules/access/http/transport.c b/modules/access/http/transport.c
index 2a93a2f..2f70dd4 100644
--- a/modules/access/http/transport.c
+++ b/modules/access/http/transport.c
@@ -54,7 +54,7 @@ ssize_t vlc_https_recv(vlc_tls_t *tls, void *buf, size_t len)
     while (count < len)
     {
         int canc = vlc_savecancel();
-        ssize_t val = tls->sock.pf_recv(tls, (char *)buf + count, len - count);
+        ssize_t val = tls->recv(tls, (char *)buf + count, len - count);
 
         vlc_restorecancel(canc);
 
@@ -115,7 +115,7 @@ ssize_t vlc_https_send(vlc_tls_t *tls, const void *buf, size_t len)
     while (count < len)
     {
         int canc = vlc_savecancel();
-        ssize_t val = tls->sock.pf_send(tls, (char *)buf + count, len - count);
+        ssize_t val = tls->send(tls, (char *)buf + count, len - count);
 
         vlc_restorecancel(canc);
 
diff --git a/modules/misc/gnutls.c b/modules/misc/gnutls.c
index fc73041..5330bf3 100644
--- a/modules/misc/gnutls.c
+++ b/modules/misc/gnutls.c
@@ -163,14 +163,11 @@ static ssize_t vlc_gnutls_writev (gnutls_transport_ptr_t ptr,
 /**
  * Sends data through a TLS session.
  */
-static int gnutls_Send (void *opaque, const void *buf, size_t length)
+static ssize_t gnutls_Send (vlc_tls_t *tls, const void *buf, size_t length)
 {
-    assert (opaque != NULL);
-
-    vlc_tls_t *tls = opaque;
     gnutls_session_t session = tls->sys;
+    ssize_t val = gnutls_record_send (session, buf, length);
 
-    int val = gnutls_record_send (session, buf, length);
     return (val < 0) ? gnutls_Error (tls, val) : val;
 }
 
@@ -178,14 +175,11 @@ static int gnutls_Send (void *opaque, const void *buf, size_t length)
 /**
  * Receives data through a TLS session.
  */
-static int gnutls_Recv (void *opaque, void *buf, size_t length)
+static ssize_t gnutls_Recv (vlc_tls_t *tls, void *buf, size_t length)
 {
-    assert (opaque != NULL);
-
-    vlc_tls_t *tls = opaque;
     gnutls_session_t session = tls->sys;
+    ssize_t val = gnutls_record_recv (session, buf, length);
 
-    int val = gnutls_record_recv (session, buf, length);
     return (val < 0) ? gnutls_Error (tls, val) : val;
 }
 
@@ -255,9 +249,8 @@ static int gnutls_SessionOpen (vlc_tls_t *tls, int type,
     gnutls_transport_set_vec_push_function (session, vlc_gnutls_writev);
 #endif
     tls->sys = session;
-    tls->sock.p_sys = NULL;
-    tls->sock.pf_send = gnutls_Send;
-    tls->sock.pf_recv = gnutls_Recv;
+    tls->send = gnutls_Send;
+    tls->recv = gnutls_Recv;
     return VLC_SUCCESS;
 
 error:
@@ -332,9 +325,7 @@ static void gnutls_SessionClose (vlc_tls_t *tls)
 {
     gnutls_session_t session = tls->sys;
 
-    if (tls->sock.p_sys != NULL)
-        gnutls_bye (session, GNUTLS_SHUT_WR);
-
+    gnutls_bye (session, GNUTLS_SHUT_RDWR);
     gnutls_deinit (session);
 }
 
@@ -375,17 +366,11 @@ static int gnutls_ClientHandshake (vlc_tls_t *tls, const char *host,
     {
         msg_Err (tls, "Certificate verification error: %s",
                  gnutls_strerror (val));
-failure:
-        gnutls_bye (session, GNUTLS_SHUT_RDWR);
         return -1;
     }
 
-    if (status == 0)
-    {   /* Good certificate */
-success:
-        tls->sock.p_sys = tls;
+    if (status == 0) /* Good certificate */
         return 0;
-    }
 
     /* Bad certificate */
     gnutls_datum_t desc;
@@ -402,7 +387,7 @@ success:
     status &= ~GNUTLS_CERT_UNEXPECTED_OWNER; /* mismatched hostname */
 
     if (status != 0 || host == NULL)
-        goto failure; /* Really bad certificate */
+        return -1; /* Really bad certificate */
 
     /* Look up mismatching certificate in store */
     const gnutls_datum_t *datum;
@@ -412,7 +397,7 @@ success:
     if (datum == NULL || count == 0)
     {
         msg_Err (tls, "Peer certificate not available");
-        goto failure;
+        return -1;
     }
 
     msg_Dbg (tls, "%u certificate(s) in the list", count);
@@ -423,7 +408,7 @@ success:
     {
         case 0:
             msg_Dbg (tls, "certificate key match for %s", host);
-            goto success;
+            return 0;
         case GNUTLS_E_NO_CERTIFICATE_FOUND:
             msg_Dbg (tls, "no known certificates for %s", host);
             msg = N_("However the security certificate presented by the "
@@ -439,7 +424,7 @@ success:
         default:
             msg_Err (tls, "certificate key match error for %s: %s", host,
                      gnutls_strerror (val));
-            goto failure;
+            return -1;
     }
 
     if (dialog_Question (tls, _("Insecure site"),
@@ -449,17 +434,17 @@ success:
           "If in doubt, abort now.\n"),
                          _("Abort"), _("View certificate"), NULL,
                          vlc_gettext (msg), host) != 2)
-        goto failure;
+        return -1;
 
     gnutls_x509_crt_t cert;
 
     if (gnutls_x509_crt_init (&cert))
-        goto failure;
+        return -1;
     if (gnutls_x509_crt_import (cert, datum, GNUTLS_X509_FMT_DER)
      || gnutls_x509_crt_print (cert, GNUTLS_CRT_PRINT_ONELINE, &desc))
     {
         gnutls_x509_crt_deinit (cert);
-        goto failure;
+        return -1;
     }
     gnutls_x509_crt_deinit (cert);
 
@@ -482,9 +467,9 @@ success:
             if (val)
                 msg_Err (tls, "cannot store X.509 certificate: %s",
                          gnutls_strerror (val));
-            goto success;
+            return 0;
     }
-    goto failure;
+    return -1;
 }
 
 /**
@@ -558,12 +543,8 @@ static int gnutls_ServerSessionOpen (vlc_tls_creds_t *crd, vlc_tls_t *tls,
 static int gnutls_ServerHandshake (vlc_tls_t *tls, const char *host,
                                    const char *service, char **restrict alp)
 {
-    int val = gnutls_ContinueHandshake (tls, alp);
-    if (val == 0)
-        tls->sock.p_sys = tls;
-
     (void) host; (void) service;
-    return val;
+    return gnutls_ContinueHandshake (tls, alp);
 }
 
 /**
diff --git a/modules/misc/securetransport.c b/modules/misc/securetransport.c
index f9ba1bc..bc9bff2 100644
--- a/modules/misc/securetransport.c
+++ b/modules/misc/securetransport.c
@@ -422,9 +422,8 @@ static int st_Handshake (vlc_tls_t *session, const char *host,
 /**
  * Sends data through a TLS session.
  */
-static int st_Send (void *opaque, const void *buf, size_t length)
+static ssize_t st_Send (vlc_tls_t *session, const void *buf, size_t length)
 {
-    vlc_tls_t *session = opaque;
     vlc_tls_sys_t *sys = session->sys;
     OSStatus ret = noErr;
 
@@ -476,9 +475,8 @@ static int st_Send (void *opaque, const void *buf, size_t length)
 /**
  * Receives data through a TLS session.
  */
-static int st_Recv (void *opaque, void *buf, size_t length)
+static ssize_t st_Recv (vlc_tls_t *session, void *buf, size_t length)
 {
-    vlc_tls_t *session = opaque;
     vlc_tls_sys_t *sys = session->sys;
 
     size_t actualSize;
@@ -542,9 +540,8 @@ static int st_SessionOpenCommon (vlc_tls_creds_t *crd, vlc_tls_t *session,
     sys->p_context = NULL;
 
     session->sys = sys;
-    session->sock.p_sys = session;
-    session->sock.pf_send = st_Send;
-    session->sock.pf_recv = st_Recv;
+    session->send = st_Send;
+    session->recv = st_Recv;
     crd->handshake = st_Handshake;
 
     SSLContextRef p_context = NULL;
diff --git a/src/network/tls.c b/src/network/tls.c
index a153ddf..7b7bfa5 100644
--- a/src/network/tls.c
+++ b/src/network/tls.c
@@ -235,7 +235,7 @@ int vlc_tls_Read(vlc_tls_t *session, void *buf, size_t len, bool waitall)
             return -1;
         }
 
-        ssize_t val = session->sock.pf_recv(session, buf, len);
+        ssize_t val = session->recv(session, buf, len);
         if (val > 0)
         {
             if (!waitall)
@@ -268,7 +268,7 @@ int vlc_tls_Write(vlc_tls_t *session, const void *buf, size_t len)
             return -1;
         }
 
-        ssize_t val = session->sock.pf_send(session, buf, len);
+        ssize_t val = session->send(session, buf, len);
         if (val > 0)
         {
             buf = ((const char *)buf) + val;



More information about the vlc-commits mailing list