[vlc-commits] tls: replace fd with get_fd callback

Rémi Denis-Courmont git at videolan.org
Wed Jan 13 21:49:49 CET 2016


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Tue Jan 12 00:01:32 2016 +0200| [79f332e2d8739dfac9684dc4093e4c07d4ea875f] | committer: Rémi Denis-Courmont

tls: replace fd with get_fd callback

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

 include/vlc_tls.h                   |   11 ++++++++---
 modules/access/http/chunked_test.c  |    7 +++++++
 modules/access/http/h2conn.c        |    2 +-
 modules/access/http/h2output.c      |    2 +-
 modules/access/http/h2output_test.c |    7 +++++++
 modules/access/http/tunnel.c        |    2 +-
 modules/misc/gnutls.c               |    8 ++++++++
 modules/misc/securetransport.c      |    7 +++++++
 src/network/tls.c                   |   22 +++++++++++++++-------
 test/modules/misc/tls.c             |    2 +-
 10 files changed, 56 insertions(+), 14 deletions(-)

diff --git a/include/vlc_tls.h b/include/vlc_tls.h
index 1b65894..0db689f 100644
--- a/include/vlc_tls.h
+++ b/include/vlc_tls.h
@@ -40,12 +40,12 @@ struct vlc_tls
 {
     vlc_object_t *obj;
     void *sys;
-    int fd;
 
+    int (*get_fd)(struct vlc_tls *);
     ssize_t (*readv)(struct vlc_tls *, struct iovec *, unsigned);
     ssize_t (*writev)(struct vlc_tls *, const struct iovec *, unsigned);
     int (*shutdown)(struct vlc_tls *, bool duplex);
-    void (*close)(vlc_tls_t *);
+    void (*close)(struct vlc_tls *);
 };
 
 /**
@@ -89,6 +89,11 @@ VLC_API vlc_tls_t *vlc_tls_SessionCreate (vlc_tls_creds_t *, int fd,
  */
 VLC_API void vlc_tls_SessionDelete (vlc_tls_t *);
 
+static inline int vlc_tls_GetFD(vlc_tls_t *tls)
+{
+    return tls->get_fd(tls);
+}
+
 /**
  * Receives data through a TLS session.
  */
@@ -129,7 +134,7 @@ static inline int vlc_tls_Shutdown(vlc_tls_t *tls, bool duplex)
  */
 static inline void vlc_tls_Close(vlc_tls_t *session)
 {
-    int fd = session->fd;
+    int fd = vlc_tls_GetFD(session);
 
     vlc_tls_SessionDelete(session);
     shutdown(fd, SHUT_RDWR);
diff --git a/modules/access/http/chunked_test.c b/modules/access/http/chunked_test.c
index 90c4edd..b22a765 100644
--- a/modules/access/http/chunked_test.c
+++ b/modules/access/http/chunked_test.c
@@ -39,6 +39,12 @@ static const char *stream_content;
 static size_t stream_length;
 static bool stream_bad;
 
+static int fd_callback(struct vlc_tls *tls)
+{
+    (void) tls;
+    return -1;
+}
+
 static ssize_t recv_callback(struct vlc_tls *tls, struct iovec *iov,
                              unsigned count)
 {
@@ -72,6 +78,7 @@ static void close_callback(struct vlc_tls *tls)
 
 static struct vlc_tls chunked_tls =
 {
+    .get_fd = fd_callback,
     .readv = recv_callback,
     .close = close_callback,
 };
diff --git a/modules/access/http/h2conn.c b/modules/access/http/h2conn.c
index 60ec980..0035162 100644
--- a/modules/access/http/h2conn.c
+++ b/modules/access/http/h2conn.c
@@ -528,7 +528,7 @@ static ssize_t vlc_https_recv(vlc_tls_t *tls, void *buf, size_t len)
     struct iovec iov;
     size_t count = 0;
 
-    ufd.fd = tls->fd;
+    ufd.fd = vlc_tls_GetFD(tls);
     ufd.events = POLLIN;
     iov.iov_base = buf;
     iov.iov_len = len;
diff --git a/modules/access/http/h2output.c b/modules/access/http/h2output.c
index dd57804..05207b5 100644
--- a/modules/access/http/h2output.c
+++ b/modules/access/http/h2output.c
@@ -193,7 +193,7 @@ static ssize_t vlc_https_send(vlc_tls_t *tls, const void *buf, size_t len)
     struct iovec iov;
     size_t count = 0;
 
-    ufd.fd = tls->fd;
+    ufd.fd = vlc_tls_GetFD(tls);
     ufd.events = POLLOUT;
     iov.iov_base = (void *)buf;
     iov.iov_len = len;
diff --git a/modules/access/http/h2output_test.c b/modules/access/http/h2output_test.c
index 34d29b4..fedbb7a 100644
--- a/modules/access/http/h2output_test.c
+++ b/modules/access/http/h2output_test.c
@@ -40,6 +40,12 @@ static bool send_failure = false;
 static bool expect_hello = true;
 static vlc_sem_t rx;
 
+static int fd_callback(vlc_tls_t *tls)
+{
+    (void) tls;
+    return -1;
+}
+
 static ssize_t send_callback(vlc_tls_t *tls, const struct iovec *iov,
                              unsigned count)
 {
@@ -74,6 +80,7 @@ static ssize_t send_callback(vlc_tls_t *tls, const struct iovec *iov,
 
 static vlc_tls_t fake_tls =
 {
+    .get_fd = fd_callback,
     .writev = send_callback,
 };
 
diff --git a/modules/access/http/tunnel.c b/modules/access/http/tunnel.c
index 4b931e1..57ab3d2 100644
--- a/modules/access/http/tunnel.c
+++ b/modules/access/http/tunnel.c
@@ -162,7 +162,7 @@ vlc_tls_t *vlc_https_connect_proxy(vlc_tls_creds_t *creds,
 # error ENOSYS
     /* TODO: create a vlc_tls_t * from a struct vlc_http_msg *. */
 #else
-    int fd = session->fd;
+    int fd = vlc_tls_GetFD(session);
 
     session->shutdown = vlc_http_tls_shutdown_ignore;
     session->close = vlc_http_tls_close_ignore;
diff --git a/modules/misc/gnutls.c b/modules/misc/gnutls.c
index 8512516..34c1be9 100644
--- a/modules/misc/gnutls.c
+++ b/modules/misc/gnutls.c
@@ -157,6 +157,13 @@ static ssize_t vlc_gnutls_writev (gnutls_transport_ptr_t ptr,
     return sendmsg (fd, &msg, MSG_NOSIGNAL);
 }
 
+static int gnutls_GetFD(vlc_tls_t *tls)
+{
+    gnutls_session_t session = tls->sys;
+
+    return gnutls_transport_get_int(session);
+}
+
 static ssize_t gnutls_Recv(vlc_tls_t *tls, struct iovec *iov, unsigned count)
 {
     gnutls_session_t session = tls->sys;
@@ -293,6 +300,7 @@ static int gnutls_SessionOpen(vlc_tls_creds_t *creds, vlc_tls_t *tls, int type,
     gnutls_transport_set_int (session, fd);
     gnutls_transport_set_vec_push_function (session, vlc_gnutls_writev);
     tls->sys = session;
+    tls->get_fd = gnutls_GetFD;
     tls->readv = gnutls_Recv;
     tls->writev = gnutls_Send;
     tls->shutdown = gnutls_Shutdown;
diff --git a/modules/misc/securetransport.c b/modules/misc/securetransport.c
index afb1d62..c7f265e 100644
--- a/modules/misc/securetransport.c
+++ b/modules/misc/securetransport.c
@@ -424,6 +424,13 @@ static int st_Handshake (vlc_tls_creds_t *crd, vlc_tls_t *session,
     }
 }
 
+static int st_GetFD (vlc_tls_t *session)
+{
+    vlc_tls_sys_t *sys = session->sys;
+
+    return sys->i_fd;
+}
+
 /**
  * Sends data through a TLS session.
  */
diff --git a/src/network/tls.c b/src/network/tls.c
index 0b88966..066176d 100644
--- a/src/network/tls.c
+++ b/src/network/tls.c
@@ -136,7 +136,6 @@ vlc_tls_t *vlc_tls_SessionCreate (vlc_tls_creds_t *crd, int fd,
         return NULL;
 
     session->obj = crd->p_parent;
-    session->fd = fd;
 
     int val = crd->open (crd, session, fd, host, alpn);
     if (val != VLC_SUCCESS)
@@ -222,7 +221,7 @@ ssize_t vlc_tls_Read(vlc_tls_t *session, void *buf, size_t len, bool waitall)
     struct pollfd ufd;
     struct iovec iov;
 
-    ufd.fd = session->fd;
+    ufd.fd = vlc_tls_GetFD(session);
     ufd.events = POLLIN;
     iov.iov_base = buf;
     iov.iov_len = len;
@@ -258,7 +257,7 @@ ssize_t vlc_tls_Write(vlc_tls_t *session, const void *buf, size_t len)
     struct pollfd ufd;
     struct iovec iov;
 
-    ufd.fd = session->fd;
+    ufd.fd = vlc_tls_GetFD(session);
     ufd.events = POLLOUT;
     iov.iov_base = (void *)buf;
     iov.iov_len = len;
@@ -318,31 +317,39 @@ error:
     return NULL;
 }
 
+static int vlc_tls_DummyGetFD(vlc_tls_t *tls)
+{
+    return (intptr_t)tls->sys;
+}
+
 static ssize_t vlc_tls_DummyReceive(vlc_tls_t *tls, struct iovec *iov,
                                     unsigned count)
 {
+    int fd = (intptr_t)tls->sys;
     struct msghdr msg =
     {
         .msg_iov = iov,
         .msg_iovlen = count,
     };
-    return recvmsg(tls->fd, &msg, 0);
+    return recvmsg(fd, &msg, 0);
 }
 
 static ssize_t vlc_tls_DummySend(vlc_tls_t *tls, const struct iovec *iov,
                                  unsigned count)
 {
+    int fd = (intptr_t)tls->sys;
     const struct msghdr msg =
     {
         .msg_iov = (struct iovec *)iov,
         .msg_iovlen = count,
     };
-    return sendmsg(tls->fd, &msg, MSG_NOSIGNAL);
+    return sendmsg(fd, &msg, MSG_NOSIGNAL);
 }
 
 static int vlc_tls_DummyShutdown(vlc_tls_t *tls, bool duplex)
 {
-    return shutdown(tls->fd, duplex ? SHUT_RDWR : SHUT_WR);
+    int fd = (intptr_t)tls->sys;
+    return shutdown(fd, duplex ? SHUT_RDWR : SHUT_WR);
 }
 
 static void vlc_tls_DummyClose(vlc_tls_t *tls)
@@ -357,7 +364,8 @@ vlc_tls_t *vlc_tls_DummyCreate(vlc_object_t *obj, int fd)
         return NULL;
 
     session->obj = obj;
-    session->fd = fd;
+    session->sys = (void *)(intptr_t)fd;
+    session->get_fd = vlc_tls_DummyGetFD;
     session->readv = vlc_tls_DummyReceive;
     session->writev = vlc_tls_DummySend;
     session->shutdown = vlc_tls_DummyShutdown;
diff --git a/test/modules/misc/tls.c b/test/modules/misc/tls.c
index bb54e25..f4fec82 100644
--- a/test/modules/misc/tls.c
+++ b/test/modules/misc/tls.c
@@ -73,7 +73,7 @@ static void *tls_echo(void *data)
     ssize_t val;
     char buf[256];
 
-    ufd.fd = tls->fd;
+    ufd.fd = vlc_tls_GetFD(tls);
 
     while ((val = vlc_tls_SessionHandshake(server_creds, tls)) > 0)
     {



More information about the vlc-commits mailing list