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 --- proxy.c | 65 ++++++++++++++++------------------------------------------------- 1 file changed, 16 insertions(+), 49 deletions(-) (limited to 'proxy.c') 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