From e70e33564351d2500f19cc5aa46f4ab2811d3880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= Date: Sat, 25 Feb 2017 23:28:42 +0200 Subject: [PATCH] tls: introduce vlc_tls_SocketPair() This creates a pair of mutually connected stream vlc_tls_t. --- 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 38a41f779d..3a00b40d79 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 b4bdd98939..343638d045 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 d7450d299a..9cde48e686 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. */ -- GitLab