[vlc-commits] src: add vlc_socketpair() helper

Rémi Denis-Courmont git at videolan.org
Sun Jan 10 19:49:57 CET 2016


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Jan 10 20:44:46 2016 +0200| [9d02c991abe7b0104f06fce1abd2a9c66e30fce6] | committer: Rémi Denis-Courmont

src: add vlc_socketpair() helper

(works like vlc_socket())

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

 include/vlc_network.h  |    1 +
 src/libvlccore.sym     |    1 +
 src/os2/filesystem.c   |   26 +++++++++++++++++++-------
 src/posix/filesystem.c |   43 ++++++++++++++++++++++++++++++++++++-------
 src/win32/filesystem.c |    7 +++++++
 5 files changed, 64 insertions(+), 14 deletions(-)

diff --git a/include/vlc_network.h b/include/vlc_network.h
index 221076b..6205790 100644
--- a/include/vlc_network.h
+++ b/include/vlc_network.h
@@ -76,6 +76,7 @@ struct msghdr
 #endif
 
 VLC_API int vlc_socket (int, int, int, bool nonblock) VLC_USED;
+VLC_API int vlc_socketpair (int, int, int, int [2], bool nonblock);
 
 struct sockaddr;
 VLC_API int vlc_accept( int, struct sockaddr *, socklen_t *, bool ) VLC_USED;
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index 5edf5d7..4881791 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -471,6 +471,7 @@ vlc_pipe
 vlc_write
 vlc_writev
 vlc_socket
+vlc_socketpair
 vlc_accept
 utf8_vfprintf
 var_AddCallback
diff --git a/src/os2/filesystem.c b/src/os2/filesystem.c
index 3d12c99..8ee29d3 100644
--- a/src/os2/filesystem.c
+++ b/src/os2/filesystem.c
@@ -302,18 +302,30 @@ ssize_t vlc_writev(int fd, const struct iovec *iov, int count)
     return val;
 }
 
+static void vlc_socket_setup(int fd, bool nonblock)
+{
+    fcntl(fd, F_SETFD, FD_CLOEXEC);
+
+    if (nonblock)
+        fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
+}
+
 int vlc_socket (int pf, int type, int proto, bool nonblock)
 {
-    int fd;
+    int fd = socket(pf, type, proto);
+    if (fd != -1)
+        vlc_socket_setup(fd, nonblock);
+    return fd;
+}
 
-    fd = socket (pf, type, proto);
-    if (fd == -1)
+int vlc_socketpair(int pf, int type, int proto, int fds[2], bool nonblock)
+{
+    if (socketpair(pf, type, proto, fds))
         return -1;
 
-    fcntl (fd, F_SETFD, FD_CLOEXEC);
-    if (nonblock)
-        fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
-    return fd;
+    vlc_socket_setup(fds[0], nonblock);
+    vlc_socket_setup(fds[1], nonblock);
+    return 0;
 }
 
 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
diff --git a/src/posix/filesystem.c b/src/posix/filesystem.c
index 0bced24..bf93e6b 100644
--- a/src/posix/filesystem.c
+++ b/src/posix/filesystem.c
@@ -271,6 +271,18 @@ ssize_t vlc_writev(int fd, const struct iovec *iov, int count)
 
 #include <vlc_network.h>
 
+static void vlc_socket_setup(int fd, bool nonblock)
+{
+    fcntl(fd, F_SETFD, FD_CLOEXEC);
+
+    if (nonblock)
+        fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
+
+#ifdef SO_NOSIGPIPE
+    setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){ 1 }, sizeof (int));
+#endif
+}
+
 /**
  * Creates a socket file descriptor. The new file descriptor has the
  * close-on-exec flag set.
@@ -288,6 +300,7 @@ int vlc_socket (int pf, int type, int proto, bool nonblock)
     type |= SOCK_CLOEXEC;
     if (nonblock)
         type |= SOCK_NONBLOCK;
+
     fd = socket (pf, type, proto);
     if (fd != -1 || errno != EINVAL)
         return fd;
@@ -296,16 +309,32 @@ int vlc_socket (int pf, int type, int proto, bool nonblock)
 #endif
 
     fd = socket (pf, type, proto);
-    if (fd == -1)
-        return -1;
+    if (fd != -1)
+        vlc_socket_setup(fd, nonblock);
+    return fd;
+}
 
-    fcntl (fd, F_SETFD, FD_CLOEXEC);
+int vlc_socketpair(int pf, int type, int proto, int fds[2], bool nonblock)
+{
+#ifdef SOCK_CLOEXEC
+    type |= SOCK_CLOEXEC;
     if (nonblock)
-        fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
-#ifdef SO_NOSIGPIPE
-    setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){ 1 }, sizeof (int));
+        type |= SOCK_NONBLOCK;
+
+    if (socketpair(pf, type, proto, fds) == 0)
+        return 0;
+    if (errno != EINVAL)
+        return -1;
+
+    type &= ~(SOCK_CLOEXEC|SOCK_NONBLOCK);
 #endif
-    return fd;
+
+    if (socketpair(pf, type, proto, fds))
+        return -1;
+
+    vlc_socket_setup(fds[0], nonblock);
+    vlc_socket_setup(fds[1], nonblock);
+    return 0;
 }
 
 /**
diff --git a/src/win32/filesystem.c b/src/win32/filesystem.c
index 62c4439..9036dbe 100644
--- a/src/win32/filesystem.c
+++ b/src/win32/filesystem.c
@@ -327,6 +327,13 @@ int vlc_socket (int pf, int type, int proto, bool nonblock)
     return fd;
 }
 
+int vlc_socketpair(int pf, int type, int proto, int fds[2], bool nonblock)
+{
+    (void) pf; (void) type; (void) proto; (void) fds; (void) nonblock;
+    errno = ENOSYS;
+    return -1;
+}
+
 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
 {
     int fd = accept (lfd, addr, alen);



More information about the vlc-commits mailing list