ot_udp.c 4.3 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 22
static char static_inbuf[8192];
static char static_outbuf[8192];

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

static void udp_make_connectionid( uint32_t * connid, const char * remoteip ) {
  /* Touch unused variable */
E
erdgeist 已提交
27
  (void)remoteip;
28 29

  /* Use a static secret for now */
30
  memmove( connid, g_static_connid, 8 );
31 32 33 34
}

static int udp_test_connectionid( const uint32_t * const connid, const char * remoteip ) {
  /* Touch unused variable */
E
erdgeist 已提交
35
  (void)remoteip;
36 37 38 39 40

  /* Test against our static secret */
  return !memcmp( connid, g_static_connid, 8 );
}

E
erdgeist 已提交
41 42 43 44 45
/* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */
void handle_udp4( int64 serversocket ) {
  ot_peer     peer;
  ot_hash    *hash = NULL;
  char        remoteip[4];
46 47 48 49
  uint32_t   *inpacket = (uint32_t*)static_inbuf;
  uint32_t   *outpacket = (uint32_t*)static_outbuf;
  uint32_t    numwant, left, event;
  uint16_t    port, remoteport;
E
erdgeist 已提交
50 51 52 53
  size_t      r, r_out;

  r = socket_recv4( serversocket, static_inbuf, sizeof( static_inbuf ), remoteip, &remoteport);

E
erdgeist 已提交
54 55
  stats_issue_event( EVENT_ACCEPT, FLAG_UDP, ntohl(*(uint32_t*)remoteip) );
  stats_issue_event( EVENT_READ, FLAG_UDP, r );
E
erdgeist 已提交
56 57 58 59 60

  /* Minimum udp tracker packet size, also catches error */
  if( r < 16 )
    return;

61
/*  fprintf( stderr, "UDP Connection id: %16llX\n", *(uint64_t*)inpacket ); */
E
erdgeist 已提交
62

E
erdgeist 已提交
63 64
  switch( ntohl( inpacket[2] ) ) {
    case 0: /* This is a connect action */
E
erdgeist 已提交
65 66 67 68
      /* look for udp bittorrent magic id */
      if( (ntohl(inpacket[0]) != 0x00000417) || (ntohl(inpacket[1]) != 0x27101980) )
        return;

69 70 71
      outpacket[0] = 0;
      outpacket[1] = inpacket[3];
      udp_make_connectionid( outpacket + 2, remoteip );
E
erdgeist 已提交
72

E
erdgeist 已提交
73
      socket_send4( serversocket, static_outbuf, 16, remoteip, remoteport );
E
erdgeist 已提交
74
      stats_issue_event( EVENT_CONNECT, FLAG_UDP, 16 );
E
erdgeist 已提交
75 76 77 78 79 80
      break;
    case 1: /* This is an announce action */
      /* Minimum udp announce packet size */
      if( r < 98 )
        return;

81
      if( !udp_test_connectionid( inpacket, remoteip ))
82
        fprintf( stderr, "UDP connect Connection id missmatch.\n" );
83

E
erdgeist 已提交
84 85 86
      /* We do only want to know, if it is zero */
      left  = inpacket[64/4] | inpacket[68/4];

87 88 89
      numwant = ntohl( inpacket[92/4] );
      if (numwant > 200) numwant = 200;

E
erdgeist 已提交
90
      event = ntohl( inpacket[80/4] );
91
      port  = *(uint16_t*)( static_inbuf + 96 );
E
erdgeist 已提交
92 93 94 95
      hash  = (ot_hash*)( static_inbuf + 16 );

      OT_SETIP( &peer, remoteip );
      OT_SETPORT( &peer, &port );
96
      OT_PEERFLAG( &peer ) = 0;
E
erdgeist 已提交
97 98

      switch( event ) {
99 100
        case 1: OT_PEERFLAG( &peer ) |= PEER_FLAG_COMPLETED; break;
        case 3: OT_PEERFLAG( &peer ) |= PEER_FLAG_STOPPED; break;
E
erdgeist 已提交
101 102 103 104
        default: break;
      }

      if( !left )
105
        OT_PEERFLAG( &peer )         |= PEER_FLAG_SEEDING;
E
erdgeist 已提交
106 107 108 109

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

110
      if( OT_PEERFLAG( &peer ) & PEER_FLAG_STOPPED ) /* Peer is gone. */
E
erdgeist 已提交
111
        r = remove_peer_from_torrent( hash, &peer, static_outbuf, FLAG_UDP );
112 113
      else
        r = 8 + add_peer_to_torrent_and_return_peers( hash, &peer, FLAG_UDP, numwant, static_outbuf + 8 );
E
erdgeist 已提交
114 115

      socket_send4( serversocket, static_outbuf, r, remoteip, remoteport );
E
erdgeist 已提交
116
      stats_issue_event( EVENT_ANNOUNCE, FLAG_UDP, r );
E
erdgeist 已提交
117 118 119
      break;

    case 2: /* This is a scrape action */
120
      if( !udp_test_connectionid( inpacket, remoteip ))
121
        fprintf( stderr, "UDP scrape Connection id missmatch.\n" );
122

E
erdgeist 已提交
123 124 125 126 127 128 129
      outpacket[0] = htonl( 2 );    /* scrape action */
      outpacket[1] = inpacket[12/4];

      for( r_out = 0; ( r_out * 20 < r - 16) && ( r_out <= 74 ); r_out++ )
        return_udp_scrape_for_torrent( (ot_hash*)( static_inbuf + 16 + 20 * r_out ), static_outbuf + 8 + 12 * r_out );

      socket_send4( serversocket, static_outbuf, 8 + 12 * r_out, remoteip, remoteport );
E
erdgeist 已提交
130
      stats_issue_event( EVENT_SCRAPE, FLAG_UDP, r );
E
erdgeist 已提交
131 132 133
      break;
  }
}
E
erdgeist 已提交
134

135 136 137 138
void udp_init( ) {

}

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