Skip to content

Commit

Permalink
util: introduce fd_set_{snd,rcv}buf()
Browse files Browse the repository at this point in the history
(cherry picked from commit d9d9b2a)
  • Loading branch information
yuwata authored and keszybz committed Sep 11, 2020
1 parent 4dcae66 commit fe9b92e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/basic/socket-util.c
Expand Up @@ -617,15 +617,15 @@ 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);

if (n > INT_MAX)
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. */
Expand All @@ -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. */
Expand All @@ -647,15 +647,15 @@ 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);

if (n > INT_MAX)
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. */
Expand All @@ -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. */
Expand Down
10 changes: 8 additions & 2 deletions src/basic/socket-util.h
Expand Up @@ -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);
Expand Down

0 comments on commit fe9b92e

Please sign in to comment.