Skip to content

Commit

Permalink
Merge pull request #88 from ssahani/dev
Browse files Browse the repository at this point in the history
Misc socket enhancements
  • Loading branch information
ssahani authored May 13, 2024
2 parents 3fd3aeb + a08754e commit 2bab930
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/netlog/netlog-network.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ int manager_network_connect_socket(Manager *m) {
}

int manager_open_network_socket(Manager *m) {
const int one = 1;
int r;

assert(m);
Expand All @@ -405,26 +404,30 @@ int manager_open_network_socket(Manager *m) {
default:
return -EPROTONOSUPPORT;
}

if (m->socket < 0)
return log_error_errno(errno, "Failed to allocate socket: %m");;

if (m->protocol == SYSLOG_TRANSMISSION_PROTOCOL_UDP) {
r = setsockopt(m->socket, IPPROTO_IP, IP_MULTICAST_LOOP, &one, sizeof(one));
if (r < 0) {
r = -errno;
log_error_errno(errno, "Failed to set socket IP_MULTICAST_LOOP: %m");
goto fail;
}
}
return log_error_errno(errno, "Failed to create socket: %m");;

if (m->protocol == SYSLOG_TRANSMISSION_PROTOCOL_TCP) {
r = setsockopt(m->socket, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
if (r < 0) {
r = -errno;
log_error_errno(errno, "Failed to set socket TCP_NODELAY: %m");
goto fail;
switch (m->protocol) {
case SYSLOG_TRANSMISSION_PROTOCOL_UDP: {
r = setsockopt_int(m->socket, IPPROTO_IP, IP_MULTICAST_LOOP, true);
if (r < 0) {
r = -errno;
log_error_errno(errno, "UDP: Failed to set IP_MULTICAST_LOOP: %m");
goto fail;
}}
break;
case SYSLOG_TRANSMISSION_PROTOCOL_TCP: {
r = setsockopt_int(m->socket, IPPROTO_TCP, TCP_FASTOPEN, 5); /* Everybody appears to pick qlen=5, let's do the same here. */
if (r < 0)
log_debug_errno(r, "Failed to enable TCP_FASTOPEN on TCP listening socket, ignoring: %m");

r = setsockopt_int(m->socket, IPPROTO_TCP, TCP_NODELAY, true);
if (r < 0)
log_debug_errno(r, "Failed to enable TCP_NODELAY mode, ignoring: %m");
}
break;
default:
break;
}

r = fd_nonblock(m->socket, true);
Expand Down
20 changes: 20 additions & 0 deletions src/share/socket-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,23 @@ int fd_inc_rcvbuf(int fd, size_t n);
})

int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, bool include_port, char **ret);

static inline int setsockopt_int(int fd, int level, int optname, int value) {
if (setsockopt(fd, level, optname, &value, sizeof(value)) < 0)
return -errno;

return 0;
}

static inline int getsockopt_int(int fd, int level, int optname, int *ret) {
int v;
socklen_t sl = sizeof(v);

if (getsockopt(fd, level, optname, &v, &sl) < 0)
return negative_errno();
if (sl != sizeof(v))
return -EIO;

*ret = v;
return 0;
}

0 comments on commit 2bab930

Please sign in to comment.