diff options
| -rw-r--r-- | opentracker.c | 29 | ||||
| -rw-r--r-- | ot_livesync.c | 42 | ||||
| -rw-r--r-- | ot_livesync.h | 2 | ||||
| -rw-r--r-- | ot_udp.c | 14 | ||||
| -rw-r--r-- | 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 @@ | |||
| 24 | /* Libowfat */ | 24 | /* Libowfat */ |
| 25 | #include "io.h" | 25 | #include "io.h" |
| 26 | #include "iob.h" | 26 | #include "iob.h" |
| 27 | #include "socket.h" | ||
| 28 | 27 | ||
| 29 | /* Opentracker */ | 28 | /* Opentracker */ |
| 30 | #include "ot_accesslist.h" | 29 | #include "ot_accesslist.h" |
| @@ -35,6 +34,7 @@ | |||
| 35 | #include "ot_mutex.h" | 34 | #include "ot_mutex.h" |
| 36 | #include "ot_random.h" | 35 | #include "ot_random.h" |
| 37 | #include "ot_scan.h" | 36 | #include "ot_scan.h" |
| 37 | #include "ot_socket.h" | ||
| 38 | #include "ot_stats.h" | 38 | #include "ot_stats.h" |
| 39 | #include "ot_udp.h" | 39 | #include "ot_udp.h" |
| 40 | #include "trackerlogic.h" | 40 | #include "trackerlogic.h" |
| @@ -254,10 +254,10 @@ static void handle_accept(const int64_t serversocket) { | |||
| 254 | struct http_data *cookie; | 254 | struct http_data *cookie; |
| 255 | int64_t sock; | 255 | int64_t sock; |
| 256 | ot_ip6 ip; | 256 | ot_ip6 ip; |
| 257 | uint16 port; | 257 | uint16_t port; |
| 258 | tai6464 t; | 258 | tai6464 t; |
| 259 | 259 | ||
| 260 | while ((sock = socket_accept6(serversocket, ip, &port, NULL)) != -1) { | 260 | while ((sock = socket_accept(serversocket, ip, &port)) != -1) { |
| 261 | 261 | ||
| 262 | /* Put fd into a non-blocking mode */ | 262 | /* Put fd into a non-blocking mode */ |
| 263 | io_nonblock(sock); | 263 | io_nonblock(sock); |
| @@ -339,7 +339,7 @@ static void *server_mainloop(void *args) { | |||
| 339 | } | 339 | } |
| 340 | 340 | ||
| 341 | static int64_t ot_try_bind(ot_ip6 ip, uint16_t port, PROTO_FLAG proto) { | 341 | static int64_t ot_try_bind(ot_ip6 ip, uint16_t port, PROTO_FLAG proto) { |
| 342 | int64_t sock = proto == FLAG_TCP ? socket_tcp6() : socket_udp6(); | 342 | int64_t sock; |
| 343 | 343 | ||
| 344 | #ifdef _DEBUG | 344 | #ifdef _DEBUG |
| 345 | { | 345 | { |
| @@ -352,11 +352,13 @@ static int64_t ot_try_bind(ot_ip6 ip, uint16_t port, PROTO_FLAG proto) { | |||
| 352 | } | 352 | } |
| 353 | #endif | 353 | #endif |
| 354 | 354 | ||
| 355 | if (socket_bind6_reuse(sock, ip, port, 0) == -1) | 355 | if (proto == FLAG_TCP) { |
| 356 | panic("socket_bind6_reuse"); | 356 | if ((sock = socket_tcp_listen(ip, port)) == -1) |
| 357 | 357 | panic("socket_tcp_listen"); | |
| 358 | if ((proto == FLAG_TCP) && (socket_listen(sock, SOMAXCONN) == -1)) | 358 | } else { |
| 359 | panic("socket_listen"); | 359 | if ((sock = socket_udp_bind(ip, port)) == -1) |
| 360 | panic("socket_udp_bind"); | ||
| 361 | } | ||
| 360 | 362 | ||
| 361 | if (!io_fd(sock)) | 363 | if (!io_fd(sock)) |
| 362 | panic("io_fd"); | 364 | panic("io_fd"); |
| @@ -450,6 +452,7 @@ typedef enum { | |||
| 450 | CONFIG_LISTEN_TCP, | 452 | CONFIG_LISTEN_TCP, |
| 451 | CONFIG_LISTEN_UDP, | 453 | CONFIG_LISTEN_UDP, |
| 452 | CONFIG_LISTEN_UDP_WORKERS, | 454 | CONFIG_LISTEN_UDP_WORKERS, |
| 455 | CONFIG_LISTEN_REUSEPORT, | ||
| 453 | CONFIG_ACCESS_WHITELIST, | 456 | CONFIG_ACCESS_WHITELIST, |
| 454 | CONFIG_ACCESS_BLACKLIST, | 457 | CONFIG_ACCESS_BLACKLIST, |
| 455 | CONFIG_ACCESS_FIFO_ADD, | 458 | CONFIG_ACCESS_FIFO_ADD, |
| @@ -477,6 +480,7 @@ static const config_key config_keys[] = { | |||
| 477 | CONFIG_KEY("listen.tcp", CONFIG_LISTEN_TCP), | 480 | CONFIG_KEY("listen.tcp", CONFIG_LISTEN_TCP), |
| 478 | CONFIG_KEY("listen.udp", CONFIG_LISTEN_UDP), | 481 | CONFIG_KEY("listen.udp", CONFIG_LISTEN_UDP), |
| 479 | CONFIG_KEY("listen.udp.workers", CONFIG_LISTEN_UDP_WORKERS), | 482 | CONFIG_KEY("listen.udp.workers", CONFIG_LISTEN_UDP_WORKERS), |
| 483 | CONFIG_KEY("listen.reuseport", CONFIG_LISTEN_REUSEPORT), | ||
| 480 | #ifdef WANT_ACCESSLIST_WHITE | 484 | #ifdef WANT_ACCESSLIST_WHITE |
| 481 | CONFIG_KEY("access.whitelist", CONFIG_ACCESS_WHITELIST), | 485 | CONFIG_KEY("access.whitelist", CONFIG_ACCESS_WHITELIST), |
| 482 | #elif defined(WANT_ACCESSLIST_BLACK) | 486 | #elif defined(WANT_ACCESSLIST_BLACK) |
| @@ -582,6 +586,13 @@ int parse_configfile(char *config_filename) { | |||
| 582 | if (scan_udec(p, &value, UINT_MAX)) | 586 | if (scan_udec(p, &value, UINT_MAX)) |
| 583 | g_udp_workers = (unsigned int)value; | 587 | g_udp_workers = (unsigned int)value; |
| 584 | break; | 588 | break; |
| 589 | case CONFIG_LISTEN_REUSEPORT: | ||
| 590 | /* Like listen.udp.workers this is read when a socket is bound, so it | ||
| 591 | has to come before the listen lines it should apply to. */ | ||
| 592 | if (!scan_udec(p, &value, 1)) | ||
| 593 | goto parse_error; | ||
| 594 | g_socket_reuseport = (int)value; | ||
| 595 | break; | ||
| 585 | #ifdef WANT_ACCESSLIST_WHITE | 596 | #ifdef WANT_ACCESSLIST_WHITE |
| 586 | case CONFIG_ACCESS_WHITELIST: | 597 | case CONFIG_ACCESS_WHITELIST: |
| 587 | set_config_option(&g_accesslist_filename, p); | 598 | 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 @@ | |||
| 5 | 5 | ||
| 6 | /* System */ | 6 | /* System */ |
| 7 | #include <arpa/inet.h> | 7 | #include <arpa/inet.h> |
| 8 | #include <fcntl.h> | ||
| 9 | #include <pthread.h> | 8 | #include <pthread.h> |
| 10 | #include <stdlib.h> | 9 | #include <stdlib.h> |
| 11 | #include <string.h> | 10 | #include <string.h> |
| 12 | #include <sys/types.h> | 11 | #include <sys/types.h> |
| 13 | #include <unistd.h> | 12 | #include <unistd.h> |
| 14 | 13 | ||
| 15 | /* Libowfat */ | ||
| 16 | #include "socket.h" | ||
| 17 | |||
| 18 | /* Opentracker */ | 14 | /* Opentracker */ |
| 19 | #include "ot_accesslist.h" | 15 | #include "ot_accesslist.h" |
| 20 | #include "ot_ip.h" | 16 | #include "ot_ip.h" |
| 17 | #include "ot_socket.h" | ||
| 21 | #include "ot_livesync.h" | 18 | #include "ot_livesync.h" |
| 22 | #include "ot_mutex.h" | 19 | #include "ot_mutex.h" |
| 23 | #include "ot_stats.h" | 20 | #include "ot_stats.h" |
| @@ -25,7 +22,7 @@ | |||
| 25 | 22 | ||
| 26 | #ifdef WANT_SYNC_LIVE | 23 | #ifdef WANT_SYNC_LIVE |
| 27 | 24 | ||
| 28 | char groupip_1[4] = {224, 0, 23, 5}; | 25 | ot_ip6 groupip_1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 224, 0, 23, 5}; |
| 29 | 26 | ||
| 30 | #define LIVESYNC_INCOMING_BUFFSIZE (256 * 256) | 27 | #define LIVESYNC_INCOMING_BUFFSIZE (256 * 256) |
| 31 | 28 | ||
| @@ -39,11 +36,11 @@ enum { OT_SYNC_PEER4, OT_SYNC_PEER6 }; | |||
| 39 | /* Forward declaration */ | 36 | /* Forward declaration */ |
| 40 | static void *livesync_worker(void *args); | 37 | static void *livesync_worker(void *args); |
| 41 | 38 | ||
| 42 | /* For outgoing packets */ | ||
| 43 | static int64_t g_socket_in = -1; | ||
| 44 | |||
| 45 | /* For incoming packets */ | 39 | /* For incoming packets */ |
| 46 | static int64_t g_socket_out = -1; | 40 | static int g_socket_in = -1; |
| 41 | |||
| 42 | /* For outgoing packets */ | ||
| 43 | static int g_socket_out = -1; | ||
| 47 | 44 | ||
| 48 | static pthread_mutex_t g_outbuf_mutex = PTHREAD_MUTEX_INITIALIZER; | 45 | static pthread_mutex_t g_outbuf_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 49 | typedef struct { | 46 | typedef struct { |
| @@ -89,34 +86,17 @@ void livesync_deinit(void) { | |||
| 89 | } | 86 | } |
| 90 | 87 | ||
| 91 | void livesync_bind_mcast(ot_ip6 ip, uint16_t port) { | 88 | void livesync_bind_mcast(ot_ip6 ip, uint16_t port) { |
| 92 | char tmpip[4] = {0, 0, 0, 0}; | ||
| 93 | char *v4ip; | ||
| 94 | |||
| 95 | if (!ip_is_v4mapped(ip)) | 89 | if (!ip_is_v4mapped(ip)) |
| 96 | exerr("v6 mcast support not yet available."); | 90 | exerr("v6 mcast support not yet available."); |
| 97 | v4ip = ip + 12; | ||
| 98 | 91 | ||
| 99 | if (g_socket_in != -1) | 92 | if (g_socket_in != -1) |
| 100 | exerr("Error: Livesync listen ip specified twice."); | 93 | exerr("Error: Livesync listen ip specified twice."); |
| 101 | 94 | ||
| 102 | if ((g_socket_in = socket_udp4()) < 0) | 95 | if ((g_socket_in = socket_mcast_join(groupip_1, ip, port)) == -1) |
| 103 | exerr("Error: Cant create live sync incoming socket."); | ||
| 104 | |||
| 105 | fcntl(g_socket_in, F_SETFL, fcntl(g_socket_in, F_GETFL, 0) & ~O_NONBLOCK); | ||
| 106 | |||
| 107 | if (socket_bind4_reuse(g_socket_in, tmpip, port) == -1) | ||
| 108 | exerr("Error: Cant bind live sync incoming socket."); | 96 | exerr("Error: Cant bind live sync incoming socket."); |
| 109 | 97 | ||
| 110 | if (socket_mcjoin4(g_socket_in, groupip_1, v4ip)) | 98 | if ((g_socket_out = socket_mcast_bind(ip, port, 0)) == -1) |
| 111 | exerr("Error: Cant make live sync incoming socket join mcast group."); | ||
| 112 | |||
| 113 | if ((g_socket_out = socket_udp4()) < 0) | ||
| 114 | exerr("Error: Cant create live sync outgoing socket."); | ||
| 115 | if (socket_bind4_reuse(g_socket_out, v4ip, port) == -1) | ||
| 116 | exerr("Error: Cant bind live sync outgoing socket."); | 99 | exerr("Error: Cant bind live sync outgoing socket."); |
| 117 | |||
| 118 | socket_mcttl4(g_socket_out, 1); | ||
| 119 | socket_mcloop4(g_socket_out, 0); | ||
| 120 | } | 100 | } |
| 121 | 101 | ||
| 122 | /* Caller MUST hold g_outbuf_mutex. Returns with g_outbuf_mutex unlocked */ | 102 | /* Caller MUST hold g_outbuf_mutex. Returns with g_outbuf_mutex unlocked */ |
| @@ -132,7 +112,7 @@ static void livesync_issue_peersync(sync_buffer *buf) { | |||
| 132 | has modified the protected element */ | 112 | has modified the protected element */ |
| 133 | pthread_mutex_unlock(&g_outbuf_mutex); | 113 | pthread_mutex_unlock(&g_outbuf_mutex); |
| 134 | 114 | ||
| 135 | socket_send4(g_socket_out, mycopy, fill, groupip_1, LIVESYNC_PORT); | 115 | socket_mcast_send(g_socket_out, mycopy, fill, groupip_1, LIVESYNC_PORT); |
| 136 | } | 116 | } |
| 137 | 117 | ||
| 138 | static void livesync_handle_peersync(struct ot_workstruct *ws, size_t peer_size) { | 118 | static void livesync_handle_peersync(struct ot_workstruct *ws, size_t peer_size) { |
| @@ -215,12 +195,10 @@ static void *livesync_worker(void *args) { | |||
| 215 | ws.inbuf = ws.request = malloc(LIVESYNC_INCOMING_BUFFSIZE); | 195 | ws.inbuf = ws.request = malloc(LIVESYNC_INCOMING_BUFFSIZE); |
| 216 | ws.outbuf = ws.reply = 0; | 196 | ws.outbuf = ws.reply = 0; |
| 217 | 197 | ||
| 218 | memcpy(in_ip, ip_v4mapped_prefix, sizeof(ip_v4mapped_prefix)); | ||
| 219 | |||
| 220 | while (1) { | 198 | while (1) { |
| 221 | uint32_t packet_type; | 199 | uint32_t packet_type; |
| 222 | 200 | ||
| 223 | ws.request_size = socket_recv4(g_socket_in, (char *)ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, 12 + (char *)in_ip, &in_port); | 201 | ws.request_size = socket_recv(g_socket_in, ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, in_ip, &in_port); |
| 224 | 202 | ||
| 225 | /* Expect at least tracker id and packet type */ | 203 | /* Expect at least tracker id and packet type */ |
| 226 | if (ws.request_size <= (ssize_t)(sizeof(g_tracker_id) + sizeof(uint32_t))) | 204 | 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); | |||
| 51 | void livesync_deinit(void); | 51 | void livesync_deinit(void); |
| 52 | 52 | ||
| 53 | /* Join multicast group for listening and create sending socket */ | 53 | /* Join multicast group for listening and create sending socket */ |
| 54 | void livesync_bind_mcast(char *ip, uint16_t port); | 54 | void livesync_bind_mcast(ot_ip6 ip, uint16_t port); |
| 55 | 55 | ||
| 56 | /* Inform live sync about whats going on. */ | 56 | /* Inform live sync about whats going on. */ |
| 57 | void livesync_tell(struct ot_workstruct *ws); | 57 | void livesync_tell(struct ot_workstruct *ws); |
| @@ -12,12 +12,12 @@ | |||
| 12 | 12 | ||
| 13 | /* Libowfat */ | 13 | /* Libowfat */ |
| 14 | #include "io.h" | 14 | #include "io.h" |
| 15 | #include "socket.h" | ||
| 16 | 15 | ||
| 17 | /* Opentracker */ | 16 | /* Opentracker */ |
| 18 | #include "ot_ip.h" | 17 | #include "ot_ip.h" |
| 19 | #include "ot_random.h" | 18 | #include "ot_random.h" |
| 20 | #include "ot_rijndael.h" | 19 | #include "ot_rijndael.h" |
| 20 | #include "ot_socket.h" | ||
| 21 | #include "ot_stats.h" | 21 | #include "ot_stats.h" |
| 22 | #include "ot_udp.h" | 22 | #include "ot_udp.h" |
| 23 | #include "trackerlogic.h" | 23 | #include "trackerlogic.h" |
| @@ -75,13 +75,13 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { | |||
| 75 | ot_ip6 remoteip; | 75 | ot_ip6 remoteip; |
| 76 | uint32_t *inpacket = (uint32_t *)ws->inbuf; | 76 | uint32_t *inpacket = (uint32_t *)ws->inbuf; |
| 77 | uint32_t *outpacket = (uint32_t *)ws->outbuf; | 77 | uint32_t *outpacket = (uint32_t *)ws->outbuf; |
| 78 | uint32_t left, event, scopeid; | 78 | uint32_t left, event; |
| 79 | uint32_t connid[2]; | 79 | uint32_t connid[2]; |
| 80 | uint32_t action; | 80 | uint32_t action; |
| 81 | uint16_t port, remoteport; | 81 | uint16_t port, remoteport; |
| 82 | size_t byte_count, scrape_count; | 82 | size_t byte_count, scrape_count; |
| 83 | 83 | ||
| 84 | ssize_t result = socket_recv6(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport, &scopeid); | 84 | ssize_t result = socket_recv(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport); |
| 85 | if (result <= 0) | 85 | if (result <= 0) |
| 86 | return 0; | 86 | return 0; |
| 87 | byte_count = (size_t)result; | 87 | byte_count = (size_t)result; |
| @@ -118,7 +118,7 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { | |||
| 118 | outpacket[0] = htonl(3); | 118 | outpacket[0] = htonl(3); |
| 119 | outpacket[1] = inpacket[3]; | 119 | outpacket[1] = inpacket[3]; |
| 120 | memcpy(&outpacket[2], "Connection ID missmatch.", s); | 120 | memcpy(&outpacket[2], "Connection ID missmatch.", s); |
| 121 | socket_send6(serversocket, ws->outbuf, 8 + s, remoteip, remoteport, 0); | 121 | socket_send(serversocket, ws->outbuf, 8 + s, remoteip, remoteport); |
| 122 | stats_issue_event(EVENT_CONNID_MISSMATCH, FLAG_UDP, 8 + s); | 122 | stats_issue_event(EVENT_CONNID_MISSMATCH, FLAG_UDP, 8 + s); |
| 123 | return 1; | 123 | return 1; |
| 124 | } | 124 | } |
| @@ -135,7 +135,7 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { | |||
| 135 | outpacket[2] = connid[0]; | 135 | outpacket[2] = connid[0]; |
| 136 | outpacket[3] = connid[1]; | 136 | outpacket[3] = connid[1]; |
| 137 | 137 | ||
| 138 | socket_send6(serversocket, ws->outbuf, 16, remoteip, remoteport, 0); | 138 | socket_send(serversocket, ws->outbuf, 16, remoteip, remoteport); |
| 139 | stats_issue_event(EVENT_CONNECT, FLAG_UDP, 16); | 139 | stats_issue_event(EVENT_CONNECT, FLAG_UDP, 16); |
| 140 | break; | 140 | break; |
| 141 | case 1: /* This is an announce action */ | 141 | case 1: /* This is an announce action */ |
| @@ -185,7 +185,7 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { | |||
| 185 | ws->reply_size = 8 + add_peer_to_torrent_and_return_peers(FLAG_UDP, ws, numwant); | 185 | ws->reply_size = 8 + add_peer_to_torrent_and_return_peers(FLAG_UDP, ws, numwant); |
| 186 | } | 186 | } |
| 187 | 187 | ||
| 188 | socket_send6(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport, 0); | 188 | socket_send(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport); |
| 189 | stats_issue_event(EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size); | 189 | stats_issue_event(EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size); |
| 190 | break; | 190 | break; |
| 191 | 191 | ||
| @@ -196,7 +196,7 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { | |||
| 196 | for (scrape_count = 0; (scrape_count * 20 < byte_count - 16) && (scrape_count <= 74); scrape_count++) | 196 | for (scrape_count = 0; (scrape_count * 20 < byte_count - 16) && (scrape_count <= 74); scrape_count++) |
| 197 | return_udp_scrape_for_torrent(*(ot_hash *)(((char *)inpacket) + 16 + 20 * scrape_count), ((char *)outpacket) + 8 + 12 * scrape_count); | 197 | return_udp_scrape_for_torrent(*(ot_hash *)(((char *)inpacket) + 16 + 20 * scrape_count), ((char *)outpacket) + 8 + 12 * scrape_count); |
| 198 | 198 | ||
| 199 | socket_send6(serversocket, ws->outbuf, 8 + 12 * scrape_count, remoteip, remoteport, 0); | 199 | socket_send(serversocket, ws->outbuf, 8 + 12 * scrape_count, remoteip, remoteport); |
| 200 | stats_issue_event(EVENT_SCRAPE, FLAG_UDP, scrape_count); | 200 | stats_issue_event(EVENT_SCRAPE, FLAG_UDP, scrape_count); |
| 201 | break; | 201 | break; |
| 202 | } | 202 | } |
| @@ -7,38 +7,33 @@ | |||
| 7 | #include <arpa/inet.h> | 7 | #include <arpa/inet.h> |
| 8 | #include <ctype.h> | 8 | #include <ctype.h> |
| 9 | #include <errno.h> | 9 | #include <errno.h> |
| 10 | #include <fcntl.h> | ||
| 11 | #include <pthread.h> | 10 | #include <pthread.h> |
| 12 | #include <stdint.h> | 11 | #include <stdint.h> |
| 13 | #include <stdio.h> | 12 | #include <stdio.h> |
| 14 | #include <stdlib.h> | 13 | #include <stdlib.h> |
| 15 | #include <string.h> | 14 | #include <string.h> |
| 16 | #include <sys/socket.h> | ||
| 17 | #include <unistd.h> | 15 | #include <unistd.h> |
| 18 | 16 | ||
| 19 | /* Libowfat */ | 17 | /* Libowfat */ |
| 20 | #include "io.h" | 18 | #include "io.h" |
| 21 | #include "iob.h" | 19 | #include "iob.h" |
| 22 | #include "socket.h" | ||
| 23 | 20 | ||
| 24 | /* Opentracker */ | 21 | /* Opentracker */ |
| 25 | #include "ot_ip.h" | 22 | #include "ot_ip.h" |
| 26 | #include "ot_mutex.h" | 23 | #include "ot_mutex.h" |
| 27 | #include "ot_random.h" | 24 | #include "ot_random.h" |
| 28 | #include "ot_scan.h" | 25 | #include "ot_scan.h" |
| 26 | #include "ot_socket.h" | ||
| 29 | #include "ot_stats.h" | 27 | #include "ot_stats.h" |
| 30 | #include "ot_vector.h" | 28 | #include "ot_vector.h" |
| 31 | #include "trackerlogic.h" | 29 | #include "trackerlogic.h" |
| 32 | 30 | ||
| 33 | #ifndef WANT_SYNC_LIVE | ||
| 34 | #define WANT_SYNC_LIVE | ||
| 35 | #endif | ||
| 36 | #include "ot_livesync.h" | 31 | #include "ot_livesync.h" |
| 37 | 32 | ||
| 38 | ot_ip6 g_serverip; | 33 | ot_ip6 g_serverip; |
| 39 | uint16_t g_serverport = 9009; | 34 | uint16_t g_serverport = 9009; |
| 40 | uint32_t g_tracker_id; | 35 | uint32_t g_tracker_id; |
| 41 | char groupip_1[4] = {224, 0, 23, 5}; | 36 | ot_ip6 groupip_1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 224, 0, 23, 5}; |
| 42 | int g_self_pipe[2]; | 37 | int g_self_pipe[2]; |
| 43 | 38 | ||
| 44 | /* If you have more than 10 peers, don't use this proxy | 39 | /* If you have more than 10 peers, don't use this proxy |
| @@ -63,11 +58,11 @@ enum { OT_SYNC_PEER4, OT_SYNC_PEER6 }; | |||
| 63 | enum { FLAG_SERVERSOCKET = 1 }; | 58 | enum { FLAG_SERVERSOCKET = 1 }; |
| 64 | 59 | ||
| 65 | /* For incoming packets */ | 60 | /* For incoming packets */ |
| 66 | static int64_t g_socket_in = -1; | 61 | static int g_socket_in = -1; |
| 67 | static uint8_t g_inbuffer[LIVESYNC_INCOMING_BUFFSIZE]; | 62 | static uint8_t g_inbuffer[LIVESYNC_INCOMING_BUFFSIZE]; |
| 68 | 63 | ||
| 69 | /* For outgoing packets */ | 64 | /* For outgoing packets */ |
| 70 | static int64_t g_socket_out = -1; | 65 | static int g_socket_out = -1; |
| 71 | static uint8_t g_peerbuffer_start[LIVESYNC_OUTGOING_BUFFSIZE_PEERS]; | 66 | static uint8_t g_peerbuffer_start[LIVESYNC_OUTGOING_BUFFSIZE_PEERS]; |
| 72 | static uint8_t *g_peerbuffer_pos; | 67 | static uint8_t *g_peerbuffer_pos; |
| 73 | static uint8_t *g_peerbuffer_highwater = g_peerbuffer_start + LIVESYNC_OUTGOING_BUFFSIZE_PEERS - LIVESYNC_OUTGOING_WATERMARK_PEERS; | 68 | 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_ | |||
| 89 | } | 84 | } |
| 90 | 85 | ||
| 91 | void livesync_bind_mcast(ot_ip6 ip, uint16_t port) { | 86 | void livesync_bind_mcast(ot_ip6 ip, uint16_t port) { |
| 92 | char tmpip[4] = {0, 0, 0, 0}; | ||
| 93 | char *v4ip; | ||
| 94 | |||
| 95 | if (!ip_is_v4mapped(ip)) | 87 | if (!ip_is_v4mapped(ip)) |
| 96 | exerr("v6 mcast support not yet available."); | 88 | exerr("v6 mcast support not yet available."); |
| 97 | v4ip = ip + 12; | ||
| 98 | 89 | ||
| 99 | if (g_socket_in != -1) | 90 | if (g_socket_in != -1) |
| 100 | exerr("Error: Livesync listen ip specified twice."); | 91 | exerr("Error: Livesync listen ip specified twice."); |
| 101 | 92 | ||
| 102 | if ((g_socket_in = socket_udp4()) < 0) | 93 | if ((g_socket_in = socket_mcast_join(groupip_1, ip, port)) == -1) |
| 103 | exerr("Error: Cant create live sync incoming socket."); | ||
| 104 | |||
| 105 | fcntl(g_socket_in, F_SETFL, fcntl(g_socket_in, F_GETFL, 0) & ~O_NONBLOCK); | ||
| 106 | |||
| 107 | if (socket_bind4_reuse(g_socket_in, tmpip, port) == -1) | ||
| 108 | exerr("Error: Cant bind live sync incoming socket."); | 94 | exerr("Error: Cant bind live sync incoming socket."); |
| 109 | 95 | ||
| 110 | if (socket_mcjoin4(g_socket_in, groupip_1, v4ip)) | 96 | if ((g_socket_out = socket_mcast_bind(ip, port, 1)) == -1) |
| 111 | exerr("Error: Cant make live sync incoming socket join mcast group."); | ||
| 112 | |||
| 113 | if ((g_socket_out = socket_udp4()) < 0) | ||
| 114 | exerr("Error: Cant create live sync outgoing socket."); | ||
| 115 | if (socket_bind4_reuse(g_socket_out, v4ip, port) == -1) | ||
| 116 | exerr("Error: Cant bind live sync outgoing socket."); | 97 | exerr("Error: Cant bind live sync outgoing socket."); |
| 117 | |||
| 118 | socket_mcttl4(g_socket_out, 1); | ||
| 119 | socket_mcloop4(g_socket_out, 1); | ||
| 120 | } | 98 | } |
| 121 | 99 | ||
| 122 | size_t add_peer_to_torrent_proxy(ot_hash hash, ot_peer *peer, size_t peer_size) { | 100 | 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) { | |||
| 283 | size_t i; | 261 | size_t i; |
| 284 | for (i = 0; i < g_connection_count; ++i) | 262 | for (i = 0; i < g_connection_count; ++i) |
| 285 | if (PROXYPEER_NEEDSCONNECT(g_connections[i].state)) { | 263 | if (PROXYPEER_NEEDSCONNECT(g_connections[i].state)) { |
| 286 | int64_t newfd = socket_tcp6(); | 264 | int64_t newfd = socket_tcp_connect(g_serverip, g_serverport, g_connections[i].ip, g_connections[i].port); |
| 287 | fprintf(stderr, "(Re)connecting to peer..."); | 265 | fprintf(stderr, "(Re)connecting to peer..."); |
| 288 | if (newfd < 0) | 266 | if (newfd == -1) |
| 289 | continue; /* No socket for you */ | 267 | continue; /* No socket for you */ |
| 290 | io_fd(newfd); | 268 | io_fd(newfd); |
| 291 | if (socket_bind6_reuse(newfd, g_serverip, g_serverport, 0)) { | ||
| 292 | io_close(newfd); | ||
| 293 | continue; | ||
| 294 | } | ||
| 295 | if (socket_connect6(newfd, g_connections[i].ip, g_connections[i].port, 0) == -1 && errno != EINPROGRESS && errno != EWOULDBLOCK) { | ||
| 296 | close(newfd); | ||
| 297 | continue; | ||
| 298 | } | ||
| 299 | io_wantwrite(newfd); /* So we will be informed when it is connected */ | 269 | io_wantwrite(newfd); /* So we will be informed when it is connected */ |
| 300 | io_setcookie(newfd, g_connections + i); | 270 | io_setcookie(newfd, g_connections + i); |
| 301 | 271 | ||
| @@ -311,9 +281,9 @@ static void handle_reconnects(void) { | |||
| 311 | static void handle_accept(int64_t serversocket) { | 281 | static void handle_accept(int64_t serversocket) { |
| 312 | int64_t newfd; | 282 | int64_t newfd; |
| 313 | ot_ip6 ip; | 283 | ot_ip6 ip; |
| 314 | uint16 port; | 284 | uint16_t port; |
| 315 | 285 | ||
| 316 | while ((newfd = socket_accept6(serversocket, ip, &port, NULL)) != -1) { | 286 | while ((newfd = socket_accept(serversocket, ip, &port)) != -1) { |
| 317 | 287 | ||
| 318 | /* XXX some access control */ | 288 | /* XXX some access control */ |
| 319 | 289 | ||
| @@ -436,7 +406,7 @@ static void handle_write(int64_t peersocket) { | |||
| 436 | break; | 406 | break; |
| 437 | case FLAG_CONNECTING: | 407 | case FLAG_CONNECTING: |
| 438 | /* Ensure that the connection is established and handle connection error */ | 408 | /* Ensure that the connection is established and handle connection error */ |
| 439 | if (peer->state & FLAG_OUTGOING && !socket_connected(peersocket)) { | 409 | if (peer->state & FLAG_OUTGOING && !socket_is_connected(peersocket)) { |
| 440 | fprintf(stderr, "failed\n"); | 410 | fprintf(stderr, "failed\n"); |
| 441 | reset_info_block(peer); | 411 | reset_info_block(peer); |
| 442 | io_close(peersocket); | 412 | io_close(peersocket); |
| @@ -515,13 +485,10 @@ static void panic(const char *routine) { | |||
| 515 | } | 485 | } |
| 516 | 486 | ||
| 517 | static int64_t ot_try_bind(ot_ip6 ip, uint16_t port) { | 487 | static int64_t ot_try_bind(ot_ip6 ip, uint16_t port) { |
| 518 | int64_t sock = socket_tcp6(); | 488 | int64_t sock = socket_tcp_listen(ip, port); |
| 519 | |||
| 520 | if (socket_bind6_reuse(sock, ip, port, 0) == -1) | ||
| 521 | panic("socket_bind6_reuse"); | ||
| 522 | 489 | ||
| 523 | if (socket_listen(sock, SOMAXCONN) == -1) | 490 | if (sock == -1) |
| 524 | panic("socket_listen"); | 491 | panic("socket_tcp_listen"); |
| 525 | 492 | ||
| 526 | if (!io_fd(sock)) | 493 | if (!io_fd(sock)) |
| 527 | panic("io_fd"); | 494 | panic("io_fd"); |
| @@ -776,7 +743,7 @@ static void *streamsync_worker(void *args) { | |||
| 776 | } | 743 | } |
| 777 | 744 | ||
| 778 | static void livesync_issue_peersync() { | 745 | static void livesync_issue_peersync() { |
| 779 | socket_send4(g_socket_out, (char *)g_peerbuffer_start, g_peerbuffer_pos - g_peerbuffer_start, groupip_1, LIVESYNC_PORT); | 746 | socket_mcast_send(g_socket_out, g_peerbuffer_start, (size_t)(g_peerbuffer_pos - g_peerbuffer_start), groupip_1, LIVESYNC_PORT); |
| 780 | g_peerbuffer_pos = g_peerbuffer_start + sizeof(g_tracker_id) + sizeof(uint32_t); | 747 | g_peerbuffer_pos = g_peerbuffer_start + sizeof(g_tracker_id) + sizeof(uint32_t); |
| 781 | g_next_packet_time = time(NULL) + LIVESYNC_MAXDELAY; | 748 | g_next_packet_time = time(NULL) + LIVESYNC_MAXDELAY; |
| 782 | } | 749 | } |
| @@ -871,7 +838,7 @@ static void *livesync_worker(void *args) { | |||
| 871 | ot_ip6 in_ip; | 838 | ot_ip6 in_ip; |
| 872 | uint16_t in_port; | 839 | uint16_t in_port; |
| 873 | uint32_t packet_type; | 840 | uint32_t packet_type; |
| 874 | size_t datalen = socket_recv4(g_socket_in, (char *)g_inbuffer, LIVESYNC_INCOMING_BUFFSIZE, 12 + (char *)in_ip, &in_port); | 841 | ssize_t datalen = socket_recv(g_socket_in, g_inbuffer, LIVESYNC_INCOMING_BUFFSIZE, in_ip, &in_port); |
| 875 | 842 | ||
| 876 | /* Expect at least tracker id and packet type */ | 843 | /* Expect at least tracker id and packet type */ |
| 877 | if (datalen <= (ssize_t)(sizeof(g_tracker_id) + sizeof(uint32_t))) | 844 | if (datalen <= (ssize_t)(sizeof(g_tracker_id) + sizeof(uint32_t))) |
