summaryrefslogtreecommitdiff
path: root/proxy.c
diff options
context:
space:
mode:
Diffstat (limited to 'proxy.c')
-rw-r--r--proxy.c35
1 files changed, 19 insertions, 16 deletions
diff --git a/proxy.c b/proxy.c
index 219d926..a5c6bb7 100644
--- a/proxy.c
+++ b/proxy.c
@@ -16,9 +16,10 @@
16 16
17/* Libowfat */ 17/* Libowfat */
18#include "io.h" 18#include "io.h"
19#include "iob.h"
20 19
21/* Opentracker */ 20/* Opentracker */
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)
148size_t remove_peer_from_torrent_proxy(ot_hash hash, ot_peer *peer, size_t peer_size) { 149size_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;
@@ -228,7 +229,7 @@ typedef struct {
228 size_t indata_length; /* Length of unprocessed data */ 229 size_t indata_length; /* Length of unprocessed data */
229 uint32_t tracker_id; /* How the other end greeted */ 230 uint32_t tracker_id; /* How the other end greeted */
230 int64_t fd; /* A file handle, if connected, <= 0 is disconnected (0 initially, -1 else) */ 231 int64_t fd; /* A file handle, if connected, <= 0 is disconnected (0 initially, -1 else) */
231 io_batch outdata; /* The iobatch containing our sync data */ 232 ot_batch outdata; /* The batch containing our sync data */
232 233
233 size_t packet_tcount; /* Number of unprocessed torrents in packet we currently receive */ 234 size_t packet_tcount; /* Number of unprocessed torrents in packet we currently receive */
234 uint8_t packet_tprefix; /* Prefix byte for all torrents in current packet */ 235 uint8_t packet_tprefix; /* Prefix byte for all torrents in current packet */
@@ -243,7 +244,7 @@ void reset_info_block(proxy_peer *peer) {
243 peer->tracker_id = 0; 244 peer->tracker_id = 0;
244 peer->fd = -1; 245 peer->fd = -1;
245 peer->packet_tcount = 0; 246 peer->packet_tcount = 0;
246 iob_reset(&peer->outdata); 247 batch_reset(&peer->outdata);
247 PROXYPEER_SETDISCONNECTED(peer->state); 248 PROXYPEER_SETDISCONNECTED(peer->state);
248} 249}
249 250
@@ -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 */
393static void handle_write(int64_t peersocket) { 394static 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 ;) */
@@ -424,11 +425,11 @@ static void handle_write(int64_t peersocket) {
424 } 425 }
425 break; 426 break;
426 case FLAG_CONNECTED: 427 case FLAG_CONNECTED:
427 switch (iob_send(peersocket, &peer->outdata)) { 428 switch (batch_send(peersocket, &peer->outdata)) {
428 case 0: /* all data sent */ 429 case BATCH_DONE:
429 io_dontwantwrite(peersocket); 430 io_dontwantwrite(peersocket);
430 break; 431 break;
431 case -3: /* an error occured */ 432 case BATCH_FAILED:
432 io_close(peersocket); 433 io_close(peersocket);
433 reset_info_block(peer); 434 reset_info_block(peer);
434 break; 435 break;
@@ -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) {
@@ -728,7 +731,7 @@ static void *streamsync_worker(void *args) {
728 void *tmp = malloc(mem); 731 void *tmp = malloc(mem);
729 if (tmp) { 732 if (tmp) {
730 memcpy(tmp, ptr, mem); 733 memcpy(tmp, ptr, mem);
731 iob_addbuf_free(&g_connections[i].outdata, tmp, mem); 734 batch_add(&g_connections[i].outdata, tmp, mem);
732 io_wantwrite(g_connections[i].fd); 735 io_wantwrite(g_connections[i].fd);
733 } 736 }
734 } 737 }