ot_udp.c 4.8 KB
Newer Older
E
erdgeist 已提交
1
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
E
erdgeist 已提交
2
   It is considered beerware. Prost. Skol. Cheers or whatever.
E
erdgeist 已提交
3

E
erdgeist 已提交
4
   $id$ */
E
erdgeist 已提交
5 6

/* System */
7 8
#include <stdlib.h>
#include <pthread.h>
E
erdgeist 已提交
9
#include <string.h>
10
#include <arpa/inet.h>
11
#include <stdio.h>
E
erdgeist 已提交
12 13 14 15 16 17 18 19 20 21

/* Libowfat */
#include "socket.h"
#include "io.h"

/* Opentracker */
#include "trackerlogic.h"
#include "ot_udp.h"
#include "ot_stats.h"

22 23
static const uint8_t g_static_connid[8] = { 0x23, 0x42, 0x05, 0x17, 0xde, 0x41, 0x50, 0xff };

E
V6  
erdgeist 已提交
24
static void udp_make_connectionid( uint32_t * connid, const ot_ip6 remoteip ) {
25
  /* Touch unused variable */
E
erdgeist 已提交
26
  (void)remoteip;
27 28

  /* Use a static secret for now */
E
V6  
erdgeist 已提交
29
  memcpy( connid, g_static_connid, 8 );
30 31
}

E
erdgeist 已提交
32
/* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */
33
int handle_udp6( int64 serversocket, struct ot_workstruct *ws ) {
E
V6  
erdgeist 已提交
34
  ot_ip6      remoteip;
35 36
  uint32_t   *inpacket = (uint32_t*)ws->inbuf;
  uint32_t   *outpacket = (uint32_t*)ws->outbuf;
E
V6  
erdgeist 已提交
37
  uint32_t    numwant, left, event, scopeid;
38
  uint16_t    port, remoteport;
39
  size_t      byte_count, scrape_count;
E
erdgeist 已提交
40

41
  byte_count = socket_recv6( serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport, &scopeid );
42
  if( !byte_count ) return 0;
E
erdgeist 已提交
43

E
V6  
erdgeist 已提交
44
  stats_issue_event( EVENT_ACCEPT, FLAG_UDP, (uintptr_t)remoteip );
45
  stats_issue_event( EVENT_READ, FLAG_UDP, byte_count );
E
erdgeist 已提交
46

47 48 49
  /* Initialise hash pointer */
  ws->hash = NULL;
  ws->peer_id = NULL;
50

E
erdgeist 已提交
51
  /* Minimum udp tracker packet size, also catches error */
52
  if( byte_count < 16 )
53
    return 1;
E
erdgeist 已提交
54 55 56

  switch( ntohl( inpacket[2] ) ) {
    case 0: /* This is a connect action */
E
erdgeist 已提交
57 58
      /* look for udp bittorrent magic id */
      if( (ntohl(inpacket[0]) != 0x00000417) || (ntohl(inpacket[1]) != 0x27101980) )
59
        return 1;
E
erdgeist 已提交
60

61 62 63
      outpacket[0] = 0;
      outpacket[1] = inpacket[3];
      udp_make_connectionid( outpacket + 2, remoteip );
E
erdgeist 已提交
64

E
V6  
erdgeist 已提交
65
      socket_send6( serversocket, ws->outbuf, 16, remoteip, remoteport, 0 );
E
erdgeist 已提交
66
      stats_issue_event( EVENT_CONNECT, FLAG_UDP, 16 );
E
erdgeist 已提交
67 68 69
      break;
    case 1: /* This is an announce action */
      /* Minimum udp announce packet size */
70
      if( byte_count < 98 )
71
        return 1;
E
erdgeist 已提交
72 73 74 75

      /* We do only want to know, if it is zero */
      left  = inpacket[64/4] | inpacket[68/4];

76 77 78
      numwant = ntohl( inpacket[92/4] );
      if (numwant > 200) numwant = 200;

79 80 81
      event    = ntohl( inpacket[80/4] );
      port     = *(uint16_t*)( ((char*)inpacket) + 96 );
      ws->hash = (ot_hash*)( ((char*)inpacket) + 16 );
E
erdgeist 已提交
82

83 84 85
      OT_SETIP( &ws->peer, remoteip );
      OT_SETPORT( &ws->peer, &port );
      OT_PEERFLAG( &ws->peer ) = 0;
E
erdgeist 已提交
86 87

      switch( event ) {
88 89
        case 1: OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_COMPLETED; break;
        case 3: OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_STOPPED; break;
E
erdgeist 已提交
90 91 92 93
        default: break;
      }

      if( !left )
94
        OT_PEERFLAG( &ws->peer )         |= PEER_FLAG_SEEDING;
E
erdgeist 已提交
95 96 97 98

      outpacket[0] = htonl( 1 );    /* announce action */
      outpacket[1] = inpacket[12/4];

99 100 101 102 103 104 105
      if( OT_PEERFLAG( &ws->peer ) & PEER_FLAG_STOPPED ) { /* Peer is gone. */
        ws->reply      = ws->outbuf;
        ws->reply_size = remove_peer_from_torrent( FLAG_UDP, ws );
      } else {
        ws->reply      = ws->outbuf + 8;
        ws->reply_size = 8 + add_peer_to_torrent_and_return_peers( FLAG_UDP, ws, numwant );
      }
E
erdgeist 已提交
106

107 108
      socket_send6( serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport, 0 );
      stats_issue_event( EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size );
E
erdgeist 已提交
109 110 111 112 113 114
      break;

    case 2: /* This is a scrape action */
      outpacket[0] = htonl( 2 );    /* scrape action */
      outpacket[1] = inpacket[12/4];

115 116
      for( scrape_count = 0; ( scrape_count * 20 < byte_count - 16) && ( scrape_count <= 74 ); scrape_count++ )
        return_udp_scrape_for_torrent( *(ot_hash*)( ((char*)inpacket) + 16 + 20 * scrape_count ), ((char*)outpacket) + 8 + 12 * scrape_count );
E
erdgeist 已提交
117

118 119
      socket_send6( serversocket, ws->outbuf, 8 + 12 * scrape_count, remoteip, remoteport, 0 );
      stats_issue_event( EVENT_SCRAPE, FLAG_UDP, scrape_count );
E
erdgeist 已提交
120 121
      break;
  }
122
  return 1;
E
erdgeist 已提交
123
}
E
erdgeist 已提交
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
static void* udp_worker( void * args ) {
  int64 sock = (int64)args;
  struct ot_workstruct ws;
  memset( &ws, 0, sizeof(ws) );

  ws.inbuf=malloc(G_INBUF_SIZE);
  ws.outbuf=malloc(G_OUTBUF_SIZE);
#ifdef    _DEBUG_HTTPERROR
  ws.debugbuf=malloc(G_DEBUGBUF_SIZE);
#endif

  while( g_opentracker_running )
    handle_udp6( sock, &ws );

  free( ws.inbuf );
  free( ws.outbuf );
#ifdef    _DEBUG_HTTPERROR
  free( ws.debugbuf );
#endif
  return NULL;
}
146

147 148 149 150 151 152 153
void udp_init( int64 sock, unsigned int worker_count ) {
  pthread_t thread_id;
#ifdef _DEBUG
  fprintf( stderr, " installing %d workers on udp socket %ld", worker_count, (unsigned long)sock );
#endif
  while( worker_count-- )
    pthread_create( &thread_id, NULL, udp_worker, (void *)sock );
154 155
}

E
erdgeist 已提交
156
const char *g_version_udp_c = "$Source$: $Revision$\n";