summaryrefslogtreecommitdiff
path: root/src/socket/socket_linux.c
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2026-09-07 13:04:10 -0400
committerAndrew Opalach <andrew@akon.city> 2026-09-07 13:04:10 -0400
commitb034b81ad74f61fc2f188c68df40f89b7900d0aa (patch)
tree20a80e4ca6d587f7fbb26f1cda13c358bc2f0473 /src/socket/socket_linux.c
parenta27216c145b1b13ad863e49e5400778ad608640f (diff)
downloadlibnaunet-b034b81ad74f61fc2f188c68df40f89b7900d0aa.tar.gz
libnaunet-b034b81ad74f61fc2f188c68df40f89b7900d0aa.tar.bz2
libnaunet-b034b81ad74f61fc2f188c68df40f89b7900d0aa.zip
Add cork() and eagain() to socket
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'src/socket/socket_linux.c')
-rw-r--r--src/socket/socket_linux.c51
1 files changed, 13 insertions, 38 deletions
diff --git a/src/socket/socket_linux.c b/src/socket/socket_linux.c
index 2634fc2..cfdcab3 100644
--- a/src/socket/socket_linux.c
+++ b/src/socket/socket_linux.c
@@ -1,9 +1,9 @@
#define AL_LOG_SECTION "socket"
#include <al/log.h>
-#include <arpa/inet.h>
-#include <netdb.h>
#include <netinet/tcp.h>
+#include <arpa/inet.h>
#include <sys/fcntl.h>
+#include <netdb.h>
#include "../util/error.h"
@@ -62,10 +62,10 @@ void nn_socket_set_blocking(struct nn_socket *sock, bool blocking)
nn_fd_set_blocking(sock->fd, blocking);
}
-void nn_socket_set_nodelay(struct nn_socket *sock, s32 nodelay)
+void nn_socket_set_cork(struct nn_socket *sock, s32 cork)
{
al_assert(sock->type == NNWT_SOCKET_TCP);
- setsockopt(sock->fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay));
+ setsockopt(sock->fd, SOL_TCP, TCP_CORK, &cork, sizeof(cork));
}
void nn_socket_set_reuse_addr(struct nn_socket *sock, s32 reuse_addr)
@@ -74,36 +74,6 @@ void nn_socket_set_reuse_addr(struct nn_socket *sock, s32 reuse_addr)
setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr));
}
-u32 nn_socket_get_send_buf(struct nn_socket *sock)
-{
- u32 send_queue_size;
- socklen_t optlen = sizeof(send_queue_size);
- getsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF, &send_queue_size, &optlen);
- return send_queue_size / 2;
-}
-
-void nn_socket_set_send_buf(struct nn_socket *sock, u32 sndbuf)
-{
- u32 send_queue_size = sndbuf;
- setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF, &send_queue_size, sizeof(send_queue_size));
- al_assert(nn_socket_get_send_buf(sock) == send_queue_size);
-}
-
-u32 nn_socket_get_recv_buf(struct nn_socket *sock)
-{
- u32 receive_queue_size;
- socklen_t optlen = sizeof(receive_queue_size);
- getsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF, &receive_queue_size, &optlen);
- return receive_queue_size / 2;
-}
-
-void nn_socket_set_recv_buf(struct nn_socket *sock, u32 rcvbuf)
-{
- u32 receive_queue_size = rcvbuf;
- setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF, &receive_queue_size, sizeof(receive_queue_size));
- al_assert(nn_socket_get_recv_buf(sock) == receive_queue_size);
-}
-
static bool parse_address(struct nn_socket *sock, str *addr, u16 port)
{
struct addrinfo hints = { 0 };
@@ -212,18 +182,18 @@ bool nn_socket_accept(struct nn_socket *sock, struct nn_socket *cl, s32 flags)
switch (cl->type) {
case NNWT_SOCKET_TCP: {
- u16 port = 0;
+ //u16 port = 0;
char addr_str[INET6_ADDRSTRLEN] = { 0 };
if (addr.ss_family == AF_INET) {
struct sockaddr_in *saddr = (struct sockaddr_in *)&addr;
- port = ntohs(saddr->sin_port);
+ //port = ntohs(saddr->sin_port);
inet_ntop(AF_INET, &saddr->sin_addr, addr_str, sizeof(addr_str));
} else if (addr.ss_family == AF_INET6) {
struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)&addr;
- port = ntohs(saddr->sin6_port);
+ //port = ntohs(saddr->sin6_port);
inet_ntop(AF_INET6, &saddr->sin6_addr, addr_str, sizeof(addr_str));
}
- log_debug("Connection from %s:%hu.", addr_str, port);
+ log_debug("Connection from %s.", addr_str);
break;
}
case NNWT_SOCKET_UNIX:
@@ -281,6 +251,11 @@ ssize_t nn_socket_recvfrom(struct nn_socket *sock, void *buf, size_t size)
return recvfrom(sock->fd, buf, size, 0, sock->addrinfo->ai_addr, &sock->addrinfo->ai_addrlen);
}
+bool nn_socket_eagain(ssize_t ret)
+{
+ return (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK));
+}
+
// This would normally be a log_warn() but we rely on expected errors for control flow.
bool nn_socket_check_error(ssize_t ret)
{