diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-04-30 03:32:36 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-04-30 03:32:36 +0200 |
commit | 0d0a857c696b653a65c709bbfb11bdaff67369b8 (patch) | |
tree | e1c26da2053f8b4801b5120517b16838b7a66c32 /src | |
parent | 4f082c7df3998cdc2cdf9845a62d886d05257cb8 (diff) | |
download | fastd-0d0a857c696b653a65c709bbfb11bdaff67369b8.tar fastd-0d0a857c696b653a65c709bbfb11bdaff67369b8.zip |
Use SOCK_NONBLOCK where available
Diffstat (limited to 'src')
-rw-r--r-- | src/async.c | 7 | ||||
-rw-r--r-- | src/compat.h | 7 | ||||
-rw-r--r-- | src/fastd.h | 4 | ||||
-rw-r--r-- | src/socket.c | 8 |
4 files changed, 19 insertions, 7 deletions
diff --git a/src/async.c b/src/async.c index 28e0b03..21a6930 100644 --- a/src/async.c +++ b/src/async.c @@ -38,10 +38,13 @@ void fastd_async_init(void) { int fds[2]; /* use socketpair with SOCK_DGRAM instead of pipe2 with O_DIRECT to keep this portable */ - if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) + if (socketpair(AF_UNIX, SOCK_DGRAM|SOCK_NONBLOCK, 0, fds)) exit_errno("socketpair"); - fastd_setfl(fds[1], O_NONBLOCK); +#ifdef NO_HAVE_SOCK_NONBLOCK + fastd_setnonblock(fds[0]); + fastd_setnonblock(fds[1]); +#endif ctx.async_rfd = fds[0]; ctx.async_wfd = fds[1]; diff --git a/src/compat.h b/src/compat.h index ea3f109..e3d9e49 100644 --- a/src/compat.h +++ b/src/compat.h @@ -60,6 +60,13 @@ struct ethhdr { #define IP_FREEBIND 15 #endif + +#ifndef SOCK_NONBLOCK +#define NO_HAVE_SOCK_NONBLOCK +#define SOCK_NONBLOCK 0 +#endif + + #ifndef HAVE_GET_CURRENT_DIR_NAME #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) diff --git a/src/fastd.h b/src/fastd.h index b5dbe0c..3bb4689 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -331,12 +331,12 @@ static inline int fastd_rand(int min, int max) { } -static inline void fastd_setfl(const int fd, int set) { +static inline void fastd_setnonblock(int fd) { int flags = fcntl(fd, F_GETFL); if (flags < 0) exit_errno("Getting file status flags failed: fcntl"); - if (fcntl(fd, F_SETFL, flags|set) < 0) + if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0) exit_errno("Setting file status flags failed: fcntl"); } diff --git a/src/socket.c b/src/socket.c index d49888c..340967c 100644 --- a/src/socket.c +++ b/src/socket.c @@ -33,7 +33,7 @@ static int bind_socket(const fastd_bind_address_t *addr, bool warn) { int af = AF_UNSPEC; if (addr->addr.sa.sa_family != AF_INET) { - fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP); + fd = socket(PF_INET6, SOCK_DGRAM|SOCK_NONBLOCK, IPPROTO_UDP); if (fd >= 0) { af = AF_INET6; @@ -46,7 +46,7 @@ static int bind_socket(const fastd_bind_address_t *addr, bool warn) { } } if (fd < 0 && addr->addr.sa.sa_family != AF_INET6) { - fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + fd = socket(PF_INET, SOCK_DGRAM|SOCK_NONBLOCK, IPPROTO_UDP); if (fd < 0) exit_errno("unable to create socket"); else @@ -56,7 +56,9 @@ static int bind_socket(const fastd_bind_address_t *addr, bool warn) { if (fd < 0) goto error; - fastd_setfl(fd, O_NONBLOCK); +#ifdef NO_HAVE_SOCK_NONBLOCK + fastd_setnonblock(fd); +#endif int one = 1; |