From fe9b92e566f837665cc06c82374e4e42f9295c99 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 9 Sep 2020 01:12:38 +0900 Subject: [PATCH] util: introduce fd_set_{snd,rcv}buf() (cherry picked from commit d9d9b2a0ae2befb645ef3aa420831423bcb9f58f) --- src/basic/socket-util.c | 12 ++++++------ src/basic/socket-util.h | 10 ++++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index da207377e07..4b3a4f206cd 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -617,7 +617,7 @@ bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b return false; } -int fd_inc_sndbuf(int fd, size_t n) { +int fd_set_sndbuf(int fd, size_t n, bool increase) { int r, value; socklen_t l = sizeof(value); @@ -625,7 +625,7 @@ int fd_inc_sndbuf(int fd, size_t n) { return -ERANGE; r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l); - if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2) + if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2) return 0; /* First, try to set the buffer size with SO_SNDBUF. */ @@ -636,7 +636,7 @@ int fd_inc_sndbuf(int fd, size_t n) { /* SO_SNDBUF above may set to the kernel limit, instead of the requested size. * So, we need to check the actual buffer size here. */ r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l); - if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2) + if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2) return 1; /* If we have the privileges we will ignore the kernel limit. */ @@ -647,7 +647,7 @@ int fd_inc_sndbuf(int fd, size_t n) { return 1; } -int fd_inc_rcvbuf(int fd, size_t n) { +int fd_set_rcvbuf(int fd, size_t n, bool increase) { int r, value; socklen_t l = sizeof(value); @@ -655,7 +655,7 @@ int fd_inc_rcvbuf(int fd, size_t n) { return -ERANGE; r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l); - if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2) + if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2) return 0; /* First, try to set the buffer size with SO_RCVBUF. */ @@ -666,7 +666,7 @@ int fd_inc_rcvbuf(int fd, size_t n) { /* SO_RCVBUF above may set to the kernel limit, instead of the requested size. * So, we need to check the actual buffer size here. */ r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l); - if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2) + if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2) return 1; /* If we have the privileges we will ignore the kernel limit. */ diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h index 9e02e398875..1177ac1d80e 100644 --- a/src/basic/socket-util.h +++ b/src/basic/socket-util.h @@ -118,8 +118,14 @@ int netlink_family_from_string(const char *s) _pure_; bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b); -int fd_inc_sndbuf(int fd, size_t n); -int fd_inc_rcvbuf(int fd, size_t n); +int fd_set_sndbuf(int fd, size_t n, bool increase); +static inline int fd_inc_sndbuf(int fd, size_t n) { + return fd_set_sndbuf(fd, n, true); +} +int fd_set_rcvbuf(int fd, size_t n, bool increase); +static inline int fd_inc_rcvbuf(int fd, size_t n) { + return fd_set_rcvbuf(fd, n, true); +} int ip_tos_to_string_alloc(int i, char **s); int ip_tos_from_string(const char *s);