From 4afeaeb4b6ab6c56e582436b17f367e216f16de1 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sun, 25 Oct 2026 02:43:09 +0200 Subject: Use the new ot_socket abstraction for socket creation, read and write --- opentracker.c | 29 +++++++++++++++++--------- ot_livesync.c | 42 +++++++++----------------------------- ot_livesync.h | 2 +- ot_udp.c | 14 ++++++------- proxy.c | 65 +++++++++++++++-------------------------------------------- 5 files changed, 54 insertions(+), 98 deletions(-) diff --git a/opentracker.c b/opentracker.c index af336aa..5b197d1 100644 --- a/opentracker.c +++ b/opentracker.c @@ -24,7 +24,6 @@ /* Libowfat */ #include "io.h" #include "iob.h" -#include "socket.h" /* Opentracker */ #include "ot_accesslist.h" @@ -35,6 +34,7 @@ #include "ot_mutex.h" #include "ot_random.h" #include "ot_scan.h" +#include "ot_socket.h" #include "ot_stats.h" #include "ot_udp.h" #include "trackerlogic.h" @@ -254,10 +254,10 @@ static void handle_accept(const int64_t serversocket) { struct http_data *cookie; int64_t sock; ot_ip6 ip; - uint16 port; + uint16_t port; tai6464 t; - while ((sock = socket_accept6(serversocket, ip, &port, NULL)) != -1) { + while ((sock = socket_accept(serversocket, ip, &port)) != -1) { /* Put fd into a non-blocking mode */ io_nonblock(sock); @@ -339,7 +339,7 @@ static void *server_mainloop(void *args) { } static int64_t ot_try_bind(ot_ip6 ip, uint16_t port, PROTO_FLAG proto) { - int64_t sock = proto == FLAG_TCP ? socket_tcp6() : socket_udp6(); + int64_t sock; #ifdef _DEBUG { @@ -352,11 +352,13 @@ static int64_t ot_try_bind(ot_ip6 ip, uint16_t port, PROTO_FLAG proto) { } #endif - if (socket_bind6_reuse(sock, ip, port, 0) == -1) - panic("socket_bind6_reuse"); - - if ((proto == FLAG_TCP) && (socket_listen(sock, SOMAXCONN) == -1)) - panic("socket_listen"); + if (proto == FLAG_TCP) { + if ((sock = socket_tcp_listen(ip, port)) == -1) + panic("socket_tcp_listen"); + } else { + if ((sock = socket_udp_bind(ip, port)) == -1) + panic("socket_udp_bind"); + } if (!io_fd(sock)) panic("io_fd"); @@ -450,6 +452,7 @@ typedef enum { CONFIG_LISTEN_TCP, CONFIG_LISTEN_UDP, CONFIG_LISTEN_UDP_WORKERS, + CONFIG_LISTEN_REUSEPORT, CONFIG_ACCESS_WHITELIST, CONFIG_ACCESS_BLACKLIST, CONFIG_ACCESS_FIFO_ADD, @@ -477,6 +480,7 @@ static const config_key config_keys[] = { CONFIG_KEY("listen.tcp", CONFIG_LISTEN_TCP), CONFIG_KEY("listen.udp", CONFIG_LISTEN_UDP), CONFIG_KEY("listen.udp.workers", CONFIG_LISTEN_UDP_WORKERS), + CONFIG_KEY("listen.reuseport", CONFIG_LISTEN_REUSEPORT), #ifdef WANT_ACCESSLIST_WHITE CONFIG_KEY("access.whitelist", CONFIG_ACCESS_WHITELIST), #elif defined(WANT_ACCESSLIST_BLACK) @@ -582,6 +586,13 @@ int parse_configfile(char *config_filename) { if (scan_udec(p, &value, UINT_MAX)) g_udp_workers = (unsigned int)value; break; + case CONFIG_LISTEN_REUSEPORT: + /* Like listen.udp.workers this is read when a socket is bound, so it + has to come before the listen lines it should apply to. */ + if (!scan_udec(p, &value, 1)) + goto parse_error; + g_socket_reuseport = (int)value; + break; #ifdef WANT_ACCESSLIST_WHITE case CONFIG_ACCESS_WHITELIST: set_config_option(&g_accesslist_filename, p); diff --git a/ot_livesync.c b/ot_livesync.c index 8b2dee6..5a63f3c 100644 --- a/ot_livesync.c +++ b/ot_livesync.c @@ -5,19 +5,16 @@ /* System */ #include -#include #include #include #include #include #include -/* Libowfat */ -#include "socket.h" - /* Opentracker */ #include "ot_accesslist.h" #include "ot_ip.h" +#include "ot_socket.h" #include "ot_livesync.h" #include "ot_mutex.h" #include "ot_stats.h" @@ -25,7 +22,7 @@ #ifdef WANT_SYNC_LIVE -char groupip_1[4] = {224, 0, 23, 5}; +ot_ip6 groupip_1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 224, 0, 23, 5}; #define LIVESYNC_INCOMING_BUFFSIZE (256 * 256) @@ -39,11 +36,11 @@ enum { OT_SYNC_PEER4, OT_SYNC_PEER6 }; /* Forward declaration */ static void *livesync_worker(void *args); -/* For outgoing packets */ -static int64_t g_socket_in = -1; - /* For incoming packets */ -static int64_t g_socket_out = -1; +static int g_socket_in = -1; + +/* For outgoing packets */ +static int g_socket_out = -1; static pthread_mutex_t g_outbuf_mutex = PTHREAD_MUTEX_INITIALIZER; typedef struct { @@ -89,34 +86,17 @@ void livesync_deinit(void) { } void livesync_bind_mcast(ot_ip6 ip, uint16_t port) { - char tmpip[4] = {0, 0, 0, 0}; - char *v4ip; - if (!ip_is_v4mapped(ip)) exerr("v6 mcast support not yet available."); - v4ip = ip + 12; if (g_socket_in != -1) exerr("Error: Livesync listen ip specified twice."); - if ((g_socket_in = socket_udp4()) < 0) - exerr("Error: Cant create live sync incoming socket."); - - fcntl(g_socket_in, F_SETFL, fcntl(g_socket_in, F_GETFL, 0) & ~O_NONBLOCK); - - if (socket_bind4_reuse(g_socket_in, tmpip, port) == -1) + if ((g_socket_in = socket_mcast_join(groupip_1, ip, port)) == -1) exerr("Error: Cant bind live sync incoming socket."); - if (socket_mcjoin4(g_socket_in, groupip_1, v4ip)) - exerr("Error: Cant make live sync incoming socket join mcast group."); - - if ((g_socket_out = socket_udp4()) < 0) - exerr("Error: Cant create live sync outgoing socket."); - if (socket_bind4_reuse(g_socket_out, v4ip, port) == -1) + if ((g_socket_out = socket_mcast_bind(ip, port, 0)) == -1) exerr("Error: Cant bind live sync outgoing socket."); - - socket_mcttl4(g_socket_out, 1); - socket_mcloop4(g_socket_out, 0); } /* Caller MUST hold g_outbuf_mutex. Returns with g_outbuf_mutex unlocked */ @@ -132,7 +112,7 @@ static void livesync_issue_peersync(sync_buffer *buf) { has modified the protected element */ pthread_mutex_unlock(&g_outbuf_mutex); - socket_send4(g_socket_out, mycopy, fill, groupip_1, LIVESYNC_PORT); + socket_mcast_send(g_socket_out, mycopy, fill, groupip_1, LIVESYNC_PORT); } static void livesync_handle_peersync(struct ot_workstruct *ws, size_t peer_size) { @@ -215,12 +195,10 @@ static void *livesync_worker(void *args) { ws.inbuf = ws.request = malloc(LIVESYNC_INCOMING_BUFFSIZE); ws.outbuf = ws.reply = 0; - memcpy(in_ip, ip_v4mapped_prefix, sizeof(ip_v4mapped_prefix)); - while (1) { uint32_t packet_type; - ws.request_size = socket_recv4(g_socket_in, (char *)ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, 12 + (char *)in_ip, &in_port); + ws.request_size = socket_recv(g_socket_in, ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, in_ip, &in_port); /* Expect at least tracker id and packet type */ if (ws.request_size <= (ssize_t)(sizeof(g_tracker_id) + sizeof(uint32_t))) diff --git a/ot_livesync.h b/ot_livesync.h index 6eca479..5b9fa0e 100644 --- a/ot_livesync.h +++ b/ot_livesync.h @@ -51,7 +51,7 @@ void livesync_init(void); void livesync_deinit(void); /* Join multicast group for listening and create sending socket */ -void livesync_bind_mcast(char *ip, uint16_t port); +void livesync_bind_mcast(ot_ip6 ip, uint16_t port); /* Inform live sync about whats going on. */ void livesync_tell(struct ot_workstruct *ws); diff --git a/ot_udp.c b/ot_udp.c index 02467e1..e80f4e7 100644 --- a/ot_udp.c +++ b/ot_udp.c @@ -12,12 +12,12 @@ /* Libowfat */ #include "io.h" -#include "socket.h" /* Opentracker */ #include "ot_ip.h" #include "ot_random.h" #include "ot_rijndael.h" +#include "ot_socket.h" #include "ot_stats.h" #include "ot_udp.h" #include "trackerlogic.h" @@ -75,13 +75,13 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { ot_ip6 remoteip; uint32_t *inpacket = (uint32_t *)ws->inbuf; uint32_t *outpacket = (uint32_t *)ws->outbuf; - uint32_t left, event, scopeid; + uint32_t left, event; uint32_t connid[2]; uint32_t action; uint16_t port, remoteport; size_t byte_count, scrape_count; - ssize_t result = socket_recv6(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport, &scopeid); + ssize_t result = socket_recv(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport); if (result <= 0) return 0; byte_count = (size_t)result; @@ -118,7 +118,7 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { outpacket[0] = htonl(3); outpacket[1] = inpacket[3]; memcpy(&outpacket[2], "Connection ID missmatch.", s); - socket_send6(serversocket, ws->outbuf, 8 + s, remoteip, remoteport, 0); + socket_send(serversocket, ws->outbuf, 8 + s, remoteip, remoteport); stats_issue_event(EVENT_CONNID_MISSMATCH, FLAG_UDP, 8 + s); return 1; } @@ -135,7 +135,7 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { outpacket[2] = connid[0]; outpacket[3] = connid[1]; - socket_send6(serversocket, ws->outbuf, 16, remoteip, remoteport, 0); + socket_send(serversocket, ws->outbuf, 16, remoteip, remoteport); stats_issue_event(EVENT_CONNECT, FLAG_UDP, 16); break; case 1: /* This is an announce action */ @@ -185,7 +185,7 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { ws->reply_size = 8 + add_peer_to_torrent_and_return_peers(FLAG_UDP, ws, numwant); } - socket_send6(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport, 0); + socket_send(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport); stats_issue_event(EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size); break; @@ -196,7 +196,7 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { for (scrape_count = 0; (scrape_count * 20 < byte_count - 16) && (scrape_count <= 74); scrape_count++) return_udp_scrape_for_torrent(*(ot_hash *)(((char *)inpacket) + 16 + 20 * scrape_count), ((char *)outpacket) + 8 + 12 * scrape_count); - socket_send6(serversocket, ws->outbuf, 8 + 12 * scrape_count, remoteip, remoteport, 0); + socket_send(serversocket, ws->outbuf, 8 + 12 * scrape_count, remoteip, remoteport); stats_issue_event(EVENT_SCRAPE, FLAG_UDP, scrape_count); break; } diff --git a/proxy.c b/proxy.c index c2dc417..16b8286 100644 --- a/proxy.c +++ b/proxy.c @@ -7,38 +7,33 @@ #include #include #include -#include #include #include #include #include #include -#include #include /* Libowfat */ #include "io.h" #include "iob.h" -#include "socket.h" /* Opentracker */ #include "ot_ip.h" #include "ot_mutex.h" #include "ot_random.h" #include "ot_scan.h" +#include "ot_socket.h" #include "ot_stats.h" #include "ot_vector.h" #include "trackerlogic.h" -#ifndef WANT_SYNC_LIVE -#define WANT_SYNC_LIVE -#endif #include "ot_livesync.h" ot_ip6 g_serverip; uint16_t g_serverport = 9009; uint32_t g_tracker_id; -char groupip_1[4] = {224, 0, 23, 5}; +ot_ip6 groupip_1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 224, 0, 23, 5}; int g_self_pipe[2]; /* If you have more than 10 peers, don't use this proxy @@ -63,11 +58,11 @@ enum { OT_SYNC_PEER4, OT_SYNC_PEER6 }; enum { FLAG_SERVERSOCKET = 1 }; /* For incoming packets */ -static int64_t g_socket_in = -1; +static int g_socket_in = -1; static uint8_t g_inbuffer[LIVESYNC_INCOMING_BUFFSIZE]; /* For outgoing packets */ -static int64_t g_socket_out = -1; +static int g_socket_out = -1; static uint8_t g_peerbuffer_start[LIVESYNC_OUTGOING_BUFFSIZE_PEERS]; static uint8_t *g_peerbuffer_pos; static uint8_t *g_peerbuffer_highwater = g_peerbuffer_start + LIVESYNC_OUTGOING_BUFFSIZE_PEERS - LIVESYNC_OUTGOING_WATERMARK_PEERS; @@ -89,34 +84,17 @@ void stats_issue_event(ot_status_event event, PROTO_FLAG proto, uintptr_t event_ } void livesync_bind_mcast(ot_ip6 ip, uint16_t port) { - char tmpip[4] = {0, 0, 0, 0}; - char *v4ip; - if (!ip_is_v4mapped(ip)) exerr("v6 mcast support not yet available."); - v4ip = ip + 12; if (g_socket_in != -1) exerr("Error: Livesync listen ip specified twice."); - if ((g_socket_in = socket_udp4()) < 0) - exerr("Error: Cant create live sync incoming socket."); - - fcntl(g_socket_in, F_SETFL, fcntl(g_socket_in, F_GETFL, 0) & ~O_NONBLOCK); - - if (socket_bind4_reuse(g_socket_in, tmpip, port) == -1) + if ((g_socket_in = socket_mcast_join(groupip_1, ip, port)) == -1) exerr("Error: Cant bind live sync incoming socket."); - if (socket_mcjoin4(g_socket_in, groupip_1, v4ip)) - exerr("Error: Cant make live sync incoming socket join mcast group."); - - if ((g_socket_out = socket_udp4()) < 0) - exerr("Error: Cant create live sync outgoing socket."); - if (socket_bind4_reuse(g_socket_out, v4ip, port) == -1) + if ((g_socket_out = socket_mcast_bind(ip, port, 1)) == -1) exerr("Error: Cant bind live sync outgoing socket."); - - socket_mcttl4(g_socket_out, 1); - socket_mcloop4(g_socket_out, 1); } size_t add_peer_to_torrent_proxy(ot_hash hash, ot_peer *peer, size_t peer_size) { @@ -283,19 +261,11 @@ static void handle_reconnects(void) { size_t i; for (i = 0; i < g_connection_count; ++i) if (PROXYPEER_NEEDSCONNECT(g_connections[i].state)) { - int64_t newfd = socket_tcp6(); + int64_t newfd = socket_tcp_connect(g_serverip, g_serverport, g_connections[i].ip, g_connections[i].port); fprintf(stderr, "(Re)connecting to peer..."); - if (newfd < 0) + if (newfd == -1) continue; /* No socket for you */ io_fd(newfd); - if (socket_bind6_reuse(newfd, g_serverip, g_serverport, 0)) { - io_close(newfd); - continue; - } - if (socket_connect6(newfd, g_connections[i].ip, g_connections[i].port, 0) == -1 && errno != EINPROGRESS && errno != EWOULDBLOCK) { - close(newfd); - continue; - } io_wantwrite(newfd); /* So we will be informed when it is connected */ io_setcookie(newfd, g_connections + i); @@ -311,9 +281,9 @@ static void handle_reconnects(void) { static void handle_accept(int64_t serversocket) { int64_t newfd; ot_ip6 ip; - uint16 port; + uint16_t port; - while ((newfd = socket_accept6(serversocket, ip, &port, NULL)) != -1) { + while ((newfd = socket_accept(serversocket, ip, &port)) != -1) { /* XXX some access control */ @@ -436,7 +406,7 @@ static void handle_write(int64_t peersocket) { break; case FLAG_CONNECTING: /* Ensure that the connection is established and handle connection error */ - if (peer->state & FLAG_OUTGOING && !socket_connected(peersocket)) { + if (peer->state & FLAG_OUTGOING && !socket_is_connected(peersocket)) { fprintf(stderr, "failed\n"); reset_info_block(peer); io_close(peersocket); @@ -515,13 +485,10 @@ static void panic(const char *routine) { } static int64_t ot_try_bind(ot_ip6 ip, uint16_t port) { - int64_t sock = socket_tcp6(); - - if (socket_bind6_reuse(sock, ip, port, 0) == -1) - panic("socket_bind6_reuse"); + int64_t sock = socket_tcp_listen(ip, port); - if (socket_listen(sock, SOMAXCONN) == -1) - panic("socket_listen"); + if (sock == -1) + panic("socket_tcp_listen"); if (!io_fd(sock)) panic("io_fd"); @@ -776,7 +743,7 @@ static void *streamsync_worker(void *args) { } static void livesync_issue_peersync() { - socket_send4(g_socket_out, (char *)g_peerbuffer_start, g_peerbuffer_pos - g_peerbuffer_start, groupip_1, LIVESYNC_PORT); + socket_mcast_send(g_socket_out, g_peerbuffer_start, (size_t)(g_peerbuffer_pos - g_peerbuffer_start), groupip_1, LIVESYNC_PORT); g_peerbuffer_pos = g_peerbuffer_start + sizeof(g_tracker_id) + sizeof(uint32_t); g_next_packet_time = time(NULL) + LIVESYNC_MAXDELAY; } @@ -871,7 +838,7 @@ static void *livesync_worker(void *args) { ot_ip6 in_ip; uint16_t in_port; uint32_t packet_type; - size_t datalen = socket_recv4(g_socket_in, (char *)g_inbuffer, LIVESYNC_INCOMING_BUFFSIZE, 12 + (char *)in_ip, &in_port); + ssize_t datalen = socket_recv(g_socket_in, g_inbuffer, LIVESYNC_INCOMING_BUFFSIZE, in_ip, &in_port); /* Expect at least tracker id and packet type */ if (datalen <= (ssize_t)(sizeof(g_tracker_id) + sizeof(uint32_t))) -- cgit v1.3