[vlc-commits] tls: add separate callback for shutdown

Rémi Denis-Courmont git at videolan.org
Sun Dec 20 23:03:17 CET 2015


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Dec 20 23:48:05 2015 +0200| [3b6bb8ae5453658097b8c99164fdcdbf380d7213] | committer: Rémi Denis-Courmont

tls: add separate callback for shutdown

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

 include/vlc_tls.h     |   20 ++++++++++++++++++++
 modules/misc/gnutls.c |   10 +++++++++-
 src/network/tls.c     |    6 ++++++
 3 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/include/vlc_tls.h b/include/vlc_tls.h
index 67b27cc..59647e8 100644
--- a/include/vlc_tls.h
+++ b/include/vlc_tls.h
@@ -44,6 +44,7 @@ struct vlc_tls
 
     ssize_t (*recv)(struct vlc_tls *, void *, size_t);
     ssize_t (*send)(struct vlc_tls *, const void *, size_t);
+    int (*shutdown)(struct vlc_tls *, bool duplex);
     void (*close)(vlc_tls_t *);
 };
 
@@ -88,6 +89,25 @@ VLC_API int vlc_tls_Read(vlc_tls_t *, void *buf, size_t len, bool waitall);
 VLC_API char *vlc_tls_GetLine(vlc_tls_t *);
 VLC_API int vlc_tls_Write(vlc_tls_t *, const void *buf, size_t len);
 
+/**
+ * Terminates a TLS session.
+ *
+ * This sends the TLS session close notification to the other end, securely
+ * indicating that no further data will be sent. Data can still be received
+ * until a close notification is received from the other end.
+ *
+ * @param duplex whether to stop receiving data as well
+ * @retval 0 the session was terminated securely and cleanly
+ *           (the underlying socket can be reused for other purposes)
+ * @return -1 the session was terminated locally, but either a notification
+ *            could not be sent or received (the underlying socket cannot be
+ *            reused and must be closed)
+ */
+static inline int vlc_tls_Shutdown(vlc_tls_t *tls, bool duplex)
+{
+    return tls->shutdown(tls, duplex);
+}
+
 # define tls_Recv(a,b,c) vlc_tls_Read(a,b,c,false)
 # define tls_Send(a,b,c) vlc_tls_Write(a,b,c)
 
diff --git a/modules/misc/gnutls.c b/modules/misc/gnutls.c
index 723372d..1262e5a 100644
--- a/modules/misc/gnutls.c
+++ b/modules/misc/gnutls.c
@@ -182,6 +182,14 @@ static ssize_t gnutls_Recv (vlc_tls_t *tls, void *buf, size_t length)
     return (val < 0) ? gnutls_Error (tls, val) : val;
 }
 
+static int gnutls_Shutdown(vlc_tls_t *tls, bool duplex)
+{
+    gnutls_session_t session = tls->sys;
+    int val = gnutls_bye(session, duplex ? GNUTLS_SHUT_RDWR : GNUTLS_SHUT_WR);
+
+    return (val < 0) ? gnutls_Error(tls, val) : 0;
+}
+
 /**
  * Terminates a TLS session.
  *
@@ -192,7 +200,6 @@ static void gnutls_Close (vlc_tls_t *tls)
 {
     gnutls_session_t session = tls->sys;
 
-    gnutls_bye (session, GNUTLS_SHUT_RDWR);
     gnutls_deinit (session);
 }
 
@@ -264,6 +271,7 @@ static int gnutls_SessionOpen(vlc_tls_creds_t *creds, vlc_tls_t *tls, int type,
     tls->sys = session;
     tls->send = gnutls_Send;
     tls->recv = gnutls_Recv;
+    tls->shutdown = gnutls_Shutdown;
     tls->close = gnutls_Close;
     return VLC_SUCCESS;
 
diff --git a/src/network/tls.c b/src/network/tls.c
index c119d33..9ddc8c9 100644
--- a/src/network/tls.c
+++ b/src/network/tls.c
@@ -322,6 +322,11 @@ static ssize_t vlc_tls_DummySend(vlc_tls_t *tls, const void *buf, size_t len)
     return send(tls->fd, buf, len, MSG_NOSIGNAL);
 }
 
+static int vlc_tls_DummyShutdown(vlc_tls_t *tls, bool duplex)
+{
+    return shutdown(tls->fd, duplex ? SHUT_RDWR : SHUT_WR);
+}
+
 static void vlc_tls_DummyClose(vlc_tls_t *tls)
 {
     (void) tls;
@@ -337,6 +342,7 @@ vlc_tls_t *vlc_tls_DummyCreate(vlc_object_t *obj, int fd)
     session->fd = fd;
     session->recv = vlc_tls_DummyReceive;
     session->send = vlc_tls_DummySend;
+    session->shutdown = vlc_tls_DummyShutdown;
     session->close = vlc_tls_DummyClose;
     return session;
 }



More information about the vlc-commits mailing list