An open and free bittorrent tracker https://erdgeist.org/gitweb/opentracker
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.8 KiB

13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
** struct ot_workstruct gets ritcher (and will become even ritcher soon). This is where we encapsulate all per-request data from peer to hash to peer_id, so that it is available everywhere without passing hundreds of pointers down the stack. Most functions that do work down the stack now accept an ot_workstruct and some flags. So it can end up in the stats/event-handler where it will be the default parameter in the future. ** peer_id is now being copied by default and moved to ot_workstruct So it is available in stats and subsequent functions. ** sync scrape madness is gone SYNC_SCRAPE was intended to sync tracker state that would normally be lost on restarts i.e. downloaded counts per torrent. The way was to push it in the tracker cloud after finding all neighbouring trackers. This is madness. It never was tested and can be done per tracker by fetching stats/mode=statedump from time to time and starting opentracker with the -l option later. ** livesync thread has its own ot_workstruct now So it can behave like ot_udp and ot_http against trackerlogic.c and get rid of the first half of the embarrassing global variables. The sending half will be fixed soon [tm]. ** stats can log completed events The author recognizes the needs of original content distributors to keep track of the amount of times a work has been downloaded. While not feasible and used on openbittorrent and other open and anonymous tracker installations, a tracker user can now choose to send those events to syslog.
11 years ago
12 years ago
** struct ot_workstruct gets ritcher (and will become even ritcher soon). This is where we encapsulate all per-request data from peer to hash to peer_id, so that it is available everywhere without passing hundreds of pointers down the stack. Most functions that do work down the stack now accept an ot_workstruct and some flags. So it can end up in the stats/event-handler where it will be the default parameter in the future. ** peer_id is now being copied by default and moved to ot_workstruct So it is available in stats and subsequent functions. ** sync scrape madness is gone SYNC_SCRAPE was intended to sync tracker state that would normally be lost on restarts i.e. downloaded counts per torrent. The way was to push it in the tracker cloud after finding all neighbouring trackers. This is madness. It never was tested and can be done per tracker by fetching stats/mode=statedump from time to time and starting opentracker with the -l option later. ** livesync thread has its own ot_workstruct now So it can behave like ot_udp and ot_http against trackerlogic.c and get rid of the first half of the embarrassing global variables. The sending half will be fixed soon [tm]. ** stats can log completed events The author recognizes the needs of original content distributors to keep track of the amount of times a work has been downloaded. While not feasible and used on openbittorrent and other open and anonymous tracker installations, a tracker user can now choose to send those events to syslog.
11 years ago
** struct ot_workstruct gets ritcher (and will become even ritcher soon). This is where we encapsulate all per-request data from peer to hash to peer_id, so that it is available everywhere without passing hundreds of pointers down the stack. Most functions that do work down the stack now accept an ot_workstruct and some flags. So it can end up in the stats/event-handler where it will be the default parameter in the future. ** peer_id is now being copied by default and moved to ot_workstruct So it is available in stats and subsequent functions. ** sync scrape madness is gone SYNC_SCRAPE was intended to sync tracker state that would normally be lost on restarts i.e. downloaded counts per torrent. The way was to push it in the tracker cloud after finding all neighbouring trackers. This is madness. It never was tested and can be done per tracker by fetching stats/mode=statedump from time to time and starting opentracker with the -l option later. ** livesync thread has its own ot_workstruct now So it can behave like ot_udp and ot_http against trackerlogic.c and get rid of the first half of the embarrassing global variables. The sending half will be fixed soon [tm]. ** stats can log completed events The author recognizes the needs of original content distributors to keep track of the amount of times a work has been downloaded. While not feasible and used on openbittorrent and other open and anonymous tracker installations, a tracker user can now choose to send those events to syslog.
11 years ago
** struct ot_workstruct gets ritcher (and will become even ritcher soon). This is where we encapsulate all per-request data from peer to hash to peer_id, so that it is available everywhere without passing hundreds of pointers down the stack. Most functions that do work down the stack now accept an ot_workstruct and some flags. So it can end up in the stats/event-handler where it will be the default parameter in the future. ** peer_id is now being copied by default and moved to ot_workstruct So it is available in stats and subsequent functions. ** sync scrape madness is gone SYNC_SCRAPE was intended to sync tracker state that would normally be lost on restarts i.e. downloaded counts per torrent. The way was to push it in the tracker cloud after finding all neighbouring trackers. This is madness. It never was tested and can be done per tracker by fetching stats/mode=statedump from time to time and starting opentracker with the -l option later. ** livesync thread has its own ot_workstruct now So it can behave like ot_udp and ot_http against trackerlogic.c and get rid of the first half of the embarrassing global variables. The sending half will be fixed soon [tm]. ** stats can log completed events The author recognizes the needs of original content distributors to keep track of the amount of times a work has been downloaded. While not feasible and used on openbittorrent and other open and anonymous tracker installations, a tracker user can now choose to send those events to syslog.
11 years ago
** struct ot_workstruct gets ritcher (and will become even ritcher soon). This is where we encapsulate all per-request data from peer to hash to peer_id, so that it is available everywhere without passing hundreds of pointers down the stack. Most functions that do work down the stack now accept an ot_workstruct and some flags. So it can end up in the stats/event-handler where it will be the default parameter in the future. ** peer_id is now being copied by default and moved to ot_workstruct So it is available in stats and subsequent functions. ** sync scrape madness is gone SYNC_SCRAPE was intended to sync tracker state that would normally be lost on restarts i.e. downloaded counts per torrent. The way was to push it in the tracker cloud after finding all neighbouring trackers. This is madness. It never was tested and can be done per tracker by fetching stats/mode=statedump from time to time and starting opentracker with the -l option later. ** livesync thread has its own ot_workstruct now So it can behave like ot_udp and ot_http against trackerlogic.c and get rid of the first half of the embarrassing global variables. The sending half will be fixed soon [tm]. ** stats can log completed events The author recognizes the needs of original content distributors to keep track of the amount of times a work has been downloaded. While not feasible and used on openbittorrent and other open and anonymous tracker installations, a tracker user can now choose to send those events to syslog.
11 years ago
** struct ot_workstruct gets ritcher (and will become even ritcher soon). This is where we encapsulate all per-request data from peer to hash to peer_id, so that it is available everywhere without passing hundreds of pointers down the stack. Most functions that do work down the stack now accept an ot_workstruct and some flags. So it can end up in the stats/event-handler where it will be the default parameter in the future. ** peer_id is now being copied by default and moved to ot_workstruct So it is available in stats and subsequent functions. ** sync scrape madness is gone SYNC_SCRAPE was intended to sync tracker state that would normally be lost on restarts i.e. downloaded counts per torrent. The way was to push it in the tracker cloud after finding all neighbouring trackers. This is madness. It never was tested and can be done per tracker by fetching stats/mode=statedump from time to time and starting opentracker with the -l option later. ** livesync thread has its own ot_workstruct now So it can behave like ot_udp and ot_http against trackerlogic.c and get rid of the first half of the embarrassing global variables. The sending half will be fixed soon [tm]. ** stats can log completed events The author recognizes the needs of original content distributors to keep track of the amount of times a work has been downloaded. While not feasible and used on openbittorrent and other open and anonymous tracker installations, a tracker user can now choose to send those events to syslog.
11 years ago
** struct ot_workstruct gets ritcher (and will become even ritcher soon). This is where we encapsulate all per-request data from peer to hash to peer_id, so that it is available everywhere without passing hundreds of pointers down the stack. Most functions that do work down the stack now accept an ot_workstruct and some flags. So it can end up in the stats/event-handler where it will be the default parameter in the future. ** peer_id is now being copied by default and moved to ot_workstruct So it is available in stats and subsequent functions. ** sync scrape madness is gone SYNC_SCRAPE was intended to sync tracker state that would normally be lost on restarts i.e. downloaded counts per torrent. The way was to push it in the tracker cloud after finding all neighbouring trackers. This is madness. It never was tested and can be done per tracker by fetching stats/mode=statedump from time to time and starting opentracker with the -l option later. ** livesync thread has its own ot_workstruct now So it can behave like ot_udp and ot_http against trackerlogic.c and get rid of the first half of the embarrassing global variables. The sending half will be fixed soon [tm]. ** stats can log completed events The author recognizes the needs of original content distributors to keep track of the amount of times a work has been downloaded. While not feasible and used on openbittorrent and other open and anonymous tracker installations, a tracker user can now choose to send those events to syslog.
11 years ago
  1. /* This software was written by Dirk Engling <erdgeist@erdgeist.org>
  2. It is considered beerware. Prost. Skol. Cheers or whatever.
  3. $id$ */
  4. /* System */
  5. #include <stdlib.h>
  6. #include <pthread.h>
  7. #include <string.h>
  8. #include <arpa/inet.h>
  9. #include <stdio.h>
  10. /* Libowfat */
  11. #include "socket.h"
  12. #include "io.h"
  13. /* Opentracker */
  14. #include "trackerlogic.h"
  15. #include "ot_udp.h"
  16. #include "ot_stats.h"
  17. static const uint8_t g_static_connid[8] = { 0x23, 0x42, 0x05, 0x17, 0xde, 0x41, 0x50, 0xff };
  18. static void udp_make_connectionid( uint32_t * connid, const ot_ip6 remoteip ) {
  19. /* Touch unused variable */
  20. (void)remoteip;
  21. /* Use a static secret for now */
  22. memcpy( connid, g_static_connid, 8 );
  23. }
  24. /* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */
  25. int handle_udp6( int64 serversocket, struct ot_workstruct *ws ) {
  26. ot_ip6 remoteip;
  27. uint32_t *inpacket = (uint32_t*)ws->inbuf;
  28. uint32_t *outpacket = (uint32_t*)ws->outbuf;
  29. uint32_t numwant, left, event, scopeid;
  30. uint16_t port, remoteport;
  31. size_t byte_count, scrape_count;
  32. byte_count = socket_recv6( serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport, &scopeid );
  33. if( !byte_count ) return 0;
  34. stats_issue_event( EVENT_ACCEPT, FLAG_UDP, (uintptr_t)remoteip );
  35. stats_issue_event( EVENT_READ, FLAG_UDP, byte_count );
  36. /* Initialise hash pointer */
  37. ws->hash = NULL;
  38. ws->peer_id = NULL;
  39. /* Minimum udp tracker packet size, also catches error */
  40. if( byte_count < 16 )
  41. return 1;
  42. switch( ntohl( inpacket[2] ) ) {
  43. case 0: /* This is a connect action */
  44. /* look for udp bittorrent magic id */
  45. if( (ntohl(inpacket[0]) != 0x00000417) || (ntohl(inpacket[1]) != 0x27101980) )
  46. return 1;
  47. outpacket[0] = 0;
  48. outpacket[1] = inpacket[3];
  49. udp_make_connectionid( outpacket + 2, remoteip );
  50. socket_send6( serversocket, ws->outbuf, 16, remoteip, remoteport, 0 );
  51. stats_issue_event( EVENT_CONNECT, FLAG_UDP, 16 );
  52. break;
  53. case 1: /* This is an announce action */
  54. /* Minimum udp announce packet size */
  55. if( byte_count < 98 )
  56. return 1;
  57. /* We do only want to know, if it is zero */
  58. left = inpacket[64/4] | inpacket[68/4];
  59. numwant = ntohl( inpacket[92/4] );
  60. if (numwant > 200) numwant = 200;
  61. event = ntohl( inpacket[80/4] );
  62. port = *(uint16_t*)( ((char*)inpacket) + 96 );
  63. ws->hash = (ot_hash*)( ((char*)inpacket) + 16 );
  64. OT_SETIP( &ws->peer, remoteip );
  65. OT_SETPORT( &ws->peer, &port );
  66. OT_PEERFLAG( &ws->peer ) = 0;
  67. switch( event ) {
  68. case 1: OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_COMPLETED; break;
  69. case 3: OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_STOPPED; break;
  70. default: break;
  71. }
  72. if( !left )
  73. OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_SEEDING;
  74. outpacket[0] = htonl( 1 ); /* announce action */
  75. outpacket[1] = inpacket[12/4];
  76. if( OT_PEERFLAG( &ws->peer ) & PEER_FLAG_STOPPED ) { /* Peer is gone. */
  77. ws->reply = ws->outbuf;
  78. ws->reply_size = remove_peer_from_torrent( FLAG_UDP, ws );
  79. } else {
  80. ws->reply = ws->outbuf + 8;
  81. ws->reply_size = 8 + add_peer_to_torrent_and_return_peers( FLAG_UDP, ws, numwant );
  82. }
  83. socket_send6( serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport, 0 );
  84. stats_issue_event( EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size );
  85. break;
  86. case 2: /* This is a scrape action */
  87. outpacket[0] = htonl( 2 ); /* scrape action */
  88. outpacket[1] = inpacket[12/4];
  89. for( scrape_count = 0; ( scrape_count * 20 < byte_count - 16) && ( scrape_count <= 74 ); scrape_count++ )
  90. return_udp_scrape_for_torrent( *(ot_hash*)( ((char*)inpacket) + 16 + 20 * scrape_count ), ((char*)outpacket) + 8 + 12 * scrape_count );
  91. socket_send6( serversocket, ws->outbuf, 8 + 12 * scrape_count, remoteip, remoteport, 0 );
  92. stats_issue_event( EVENT_SCRAPE, FLAG_UDP, scrape_count );
  93. break;
  94. }
  95. return 1;
  96. }
  97. static void* udp_worker( void * args ) {
  98. int64 sock = (int64)args;
  99. struct ot_workstruct ws;
  100. memset( &ws, 0, sizeof(ws) );
  101. ws.inbuf=malloc(G_INBUF_SIZE);
  102. ws.outbuf=malloc(G_OUTBUF_SIZE);
  103. #ifdef _DEBUG_HTTPERROR
  104. ws.debugbuf=malloc(G_DEBUGBUF_SIZE);
  105. #endif
  106. while( g_opentracker_running )
  107. handle_udp6( sock, &ws );
  108. free( ws.inbuf );
  109. free( ws.outbuf );
  110. #ifdef _DEBUG_HTTPERROR
  111. free( ws.debugbuf );
  112. #endif
  113. return NULL;
  114. }
  115. void udp_init( int64 sock, unsigned int worker_count ) {
  116. pthread_t thread_id;
  117. #ifdef _DEBUG
  118. fprintf( stderr, " installing %d workers on udp socket %ld", worker_count, (unsigned long)sock );
  119. #endif
  120. while( worker_count-- )
  121. pthread_create( &thread_id, NULL, udp_worker, (void *)sock );
  122. }
  123. const char *g_version_udp_c = "$Source$: $Revision$\n";