[vlc-commits] tls: introduce vlc_tls_SocketPair()

Rémi Denis-Courmont git at videolan.org
Sat Feb 25 22:41:58 CET 2017


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sat Feb 25 23:28:42 2017 +0200| [e70e33564351d2500f19cc5aa46f4ab2811d3880] | committer: Rémi Denis-Courmont

tls: introduce vlc_tls_SocketPair()

This creates a pair of mutually connected stream vlc_tls_t.

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

 include/vlc_tls.h  |  5 +++++
 src/libvlccore.sym |  1 +
 src/network/tls.c  | 26 ++++++++++++++++++++++++++
 3 files changed, 32 insertions(+)

diff --git a/include/vlc_tls.h b/include/vlc_tls.h
index 38a41f7..3a00b40 100644
--- a/include/vlc_tls.h
+++ b/include/vlc_tls.h
@@ -232,6 +232,11 @@ VLC_API void vlc_tls_Delete (vlc_tls_creds_t *);
  */
 VLC_API vlc_tls_t *vlc_tls_SocketOpen(int fd);
 
+/**
+ * Creates a connected pair of transport-layer sockets.
+ */
+VLC_API int vlc_tls_SocketPair(int family, int protocol, vlc_tls_t *[2]);
+
 struct addrinfo;
 
 /**
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index b4bdd98..343638d 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -445,6 +445,7 @@ vlc_tls_GetLine
 vlc_tls_SocketOpen
 vlc_tls_SocketOpenTCP
 vlc_tls_SocketOpenTLS
+vlc_tls_SocketPair
 ToCharset
 update_Check
 update_Delete
diff --git a/src/network/tls.c b/src/network/tls.c
index d7450d2..9cde48e 100644
--- a/src/network/tls.c
+++ b/src/network/tls.c
@@ -404,6 +404,32 @@ vlc_tls_t *vlc_tls_SocketOpen(int fd)
     return vlc_tls_SocketAlloc(fd, NULL, 0);
 }
 
+int vlc_tls_SocketPair(int family, int protocol, vlc_tls_t *pair[2])
+{
+    int fds[2];
+
+    if (vlc_socketpair(family, SOCK_STREAM, protocol, fds, true))
+        return -1;
+
+    for (size_t i = 0; i < 2; i++)
+    {
+        setsockopt(fds[i], SOL_SOCKET, SO_REUSEADDR,
+                   &(int){ 1 }, sizeof (int));
+
+        pair[i] = vlc_tls_SocketAlloc(fds[i], NULL, 0);
+        if (unlikely(pair[i] == NULL))
+        {
+            net_Close(fds[i]);
+            if (i)
+                vlc_tls_SessionDelete(pair[0]);
+            else
+                net_Close(fds[1]);
+            return -1;
+        }
+    }
+    return 0;
+}
+
 /**
  * Allocates an unconnected transport layer socket.
  */



More information about the vlc-commits mailing list