diff options
Diffstat (limited to 'proxy.c')
| -rw-r--r-- | proxy.c | 21 |
1 files changed, 12 insertions, 9 deletions
| @@ -19,6 +19,7 @@ | |||
| 19 | 19 | ||
| 20 | /* Opentracker */ | 20 | /* Opentracker */ |
| 21 | #include "ot_batch.h" | 21 | #include "ot_batch.h" |
| 22 | #include "ot_fdstate.h" | ||
| 22 | #include "ot_ip.h" | 23 | #include "ot_ip.h" |
| 23 | #include "ot_mutex.h" | 24 | #include "ot_mutex.h" |
| 24 | #include "ot_random.h" | 25 | #include "ot_random.h" |
| @@ -148,7 +149,7 @@ size_t add_peer_to_torrent_proxy(ot_hash hash, ot_peer *peer, size_t peer_size) | |||
| 148 | size_t remove_peer_from_torrent_proxy(ot_hash hash, ot_peer *peer, size_t peer_size) { | 149 | size_t remove_peer_from_torrent_proxy(ot_hash hash, ot_peer *peer, size_t peer_size) { |
| 149 | int exactmatch; | 150 | int exactmatch; |
| 150 | ot_vector *torrents_list = mutex_bucket_lock_by_hash(hash); | 151 | ot_vector *torrents_list = mutex_bucket_lock_by_hash(hash); |
| 151 | ot_torrent *torrent = binary_search(hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); | 152 | ot_torrent *torrent = vector_binary_search(hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); |
| 152 | 153 | ||
| 153 | if (exactmatch) { | 154 | if (exactmatch) { |
| 154 | ot_peerlist *peer_list = peer_list = peer_size == OT_PEER_SIZE6 ? torrent->peer_list6 : torrent->peer_list4; | 155 | ot_peerlist *peer_list = peer_list = peer_size == OT_PEER_SIZE6 ? torrent->peer_list6 : torrent->peer_list4; |
| @@ -267,7 +268,7 @@ static void handle_reconnects(void) { | |||
| 267 | continue; /* No socket for you */ | 268 | continue; /* No socket for you */ |
| 268 | io_fd(newfd); | 269 | io_fd(newfd); |
| 269 | io_wantwrite(newfd); /* So we will be informed when it is connected */ | 270 | io_wantwrite(newfd); /* So we will be informed when it is connected */ |
| 270 | io_setcookie(newfd, g_connections + i); | 271 | fdstate_open(newfd, g_connections + i, 0); |
| 271 | 272 | ||
| 272 | /* Prepare connection info block */ | 273 | /* Prepare connection info block */ |
| 273 | reset_info_block(g_connections + i); | 274 | reset_info_block(g_connections + i); |
| @@ -310,7 +311,7 @@ static void handle_accept(int64_t serversocket) { | |||
| 310 | g_connections[i].port = port; | 311 | g_connections[i].port = port; |
| 311 | g_connections[i].fd = newfd; | 312 | g_connections[i].fd = newfd; |
| 312 | 313 | ||
| 313 | io_setcookie(newfd, g_connections + i); | 314 | fdstate_open(newfd, g_connections + i, 0); |
| 314 | 315 | ||
| 315 | /* We expect the connecting side to begin with its tracker_id */ | 316 | /* We expect the connecting side to begin with its tracker_id */ |
| 316 | io_wantread(newfd); | 317 | io_wantread(newfd); |
| @@ -325,7 +326,7 @@ static void handle_read(int64_t peersocket) { | |||
| 325 | int i; | 326 | int i; |
| 326 | int64_t datalen; | 327 | int64_t datalen; |
| 327 | uint32_t tracker_id; | 328 | uint32_t tracker_id; |
| 328 | proxy_peer *peer = io_getcookie(peersocket); | 329 | proxy_peer *peer = fdstate_cookie(peersocket); |
| 329 | 330 | ||
| 330 | if (!peer) { | 331 | if (!peer) { |
| 331 | /* Can't happen ;) */ | 332 | /* Can't happen ;) */ |
| @@ -391,7 +392,7 @@ static void handle_read(int64_t peersocket) { | |||
| 391 | 392 | ||
| 392 | /* Can write new sync data to the stream */ | 393 | /* Can write new sync data to the stream */ |
| 393 | static void handle_write(int64_t peersocket) { | 394 | static void handle_write(int64_t peersocket) { |
| 394 | proxy_peer *peer = io_getcookie(peersocket); | 395 | proxy_peer *peer = fdstate_cookie(peersocket); |
| 395 | 396 | ||
| 396 | if (!peer) { | 397 | if (!peer) { |
| 397 | /* Can't happen ;) */ | 398 | /* Can't happen ;) */ |
| @@ -464,7 +465,7 @@ static void server_mainloop() { | |||
| 464 | 465 | ||
| 465 | /* Loop over readable sockets */ | 466 | /* Loop over readable sockets */ |
| 466 | while ((sock = io_canread()) != -1) { | 467 | while ((sock = io_canread()) != -1) { |
| 467 | const void *cookie = io_getcookie(sock); | 468 | const void *cookie = fdstate_cookie(sock); |
| 468 | if ((uintptr_t)cookie == FLAG_SERVERSOCKET) | 469 | if ((uintptr_t)cookie == FLAG_SERVERSOCKET) |
| 469 | handle_accept(sock); | 470 | handle_accept(sock); |
| 470 | else | 471 | else |
| @@ -493,7 +494,7 @@ static int64_t ot_try_bind(ot_ip6 ip, uint16_t port) { | |||
| 493 | if (!io_fd(sock)) | 494 | if (!io_fd(sock)) |
| 494 | panic("io_fd"); | 495 | panic("io_fd"); |
| 495 | 496 | ||
| 496 | io_setcookie(sock, (void *)FLAG_SERVERSOCKET); | 497 | fdstate_open(sock, (void *)FLAG_SERVERSOCKET, 0); |
| 497 | io_wantread(sock); | 498 | io_wantread(sock); |
| 498 | return sock; | 499 | return sock; |
| 499 | } | 500 | } |
| @@ -506,7 +507,7 @@ static int scan_ip6_port(const char *src, ot_ip6 ip, uint16_t *port) { | |||
| 506 | ++s; | 507 | ++s; |
| 507 | if (*s == '[') | 508 | if (*s == '[') |
| 508 | ++s, ++bracket; /* for v6 style notation */ | 509 | ++s, ++bracket; /* for v6 style notation */ |
| 509 | if (!(off = ip_scan(s, ip))) | 510 | if (!(off = ip_parse(s, ip))) |
| 510 | return 0; | 511 | return 0; |
| 511 | s += off; | 512 | s += off; |
| 512 | if (*s == 0 || isspace(*s)) | 513 | if (*s == 0 || isspace(*s)) |
| @@ -521,7 +522,7 @@ static int scan_ip6_port(const char *src, ot_ip6 ip, uint16_t *port) { | |||
| 521 | if (*(s++) != ':') | 522 | if (*(s++) != ':') |
| 522 | return 0; | 523 | return 0; |
| 523 | } | 524 | } |
| 524 | if (!(off = scan_udec(s, &value, 65535))) | 525 | if (!(off = scan_uint(s, &value, 65535))) |
| 525 | return 0; | 526 | return 0; |
| 526 | *port = (uint16_t)value; | 527 | *port = (uint16_t)value; |
| 527 | return off + s - src; | 528 | return off + s - src; |
| @@ -535,6 +536,8 @@ int main(int argc, char **argv) { | |||
| 535 | int scanon = 1, lbound = 0, sbound = 0; | 536 | int scanon = 1, lbound = 0, sbound = 0; |
| 536 | 537 | ||
| 537 | random_init(); | 538 | random_init(); |
| 539 | if (fdstate_init() == -1) | ||
| 540 | panic("fdstate_init"); | ||
| 538 | g_tracker_id = random_uint32(); | 541 | g_tracker_id = random_uint32(); |
| 539 | 542 | ||
| 540 | while (scanon) { | 543 | while (scanon) { |
