ot_udp.c 4.0 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 7

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

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

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

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

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

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

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

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

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

45 46 47
  /* Initialise hash pointer */
  ws->hash = NULL;
  ws->peer_id = NULL;
48

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

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

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

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

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

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

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

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

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

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

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

97 98 99 100 101 102 103
      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 已提交
104

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

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

113 114
      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 已提交
115

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

123 124 125 126
void udp_init( ) {

}

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