Commit 9d02c991 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

src: add vlc_socketpair() helper

(works like vlc_socket())
parent 11c606b1
......@@ -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;
......
......@@ -471,6 +471,7 @@ vlc_pipe
vlc_write
vlc_writev
vlc_socket
vlc_socketpair
vlc_accept
utf8_vfprintf
var_AddCallback
......
......@@ -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)
......
......@@ -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;
}
/**
......
......@@ -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);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment