ot_udp.c 4.2 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
V6  
erdgeist 已提交
30
static int udp_test_connectionid( const uint32_t * const connid, const ot_ip6 remoteip ) {
31
  /* Touch unused variable */
E
erdgeist 已提交
32
  (void)remoteip;
33 34 35 36 37

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

E
erdgeist 已提交
38
/* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */
E
V6  
erdgeist 已提交
39
void handle_udp6( int64 serversocket, struct ot_workstruct *ws ) {
E
erdgeist 已提交
40 41
  ot_peer     peer;
  ot_hash    *hash = NULL;
E
V6  
erdgeist 已提交
42
  ot_ip6      remoteip;
43 44
  uint32_t   *inpacket = (uint32_t*)ws->inbuf;
  uint32_t   *outpacket = (uint32_t*)ws->outbuf;
E
V6  
erdgeist 已提交
45
  uint32_t    numwant, left, event, scopeid;
46
  uint16_t    port, remoteport;
E
erdgeist 已提交
47 48
  size_t      r, r_out;

E
V6  
erdgeist 已提交
49
  r = socket_recv6( serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport, &scopeid );
E
erdgeist 已提交
50

E
V6  
erdgeist 已提交
51
  stats_issue_event( EVENT_ACCEPT, FLAG_UDP, (uintptr_t)remoteip );
E
erdgeist 已提交
52
  stats_issue_event( EVENT_READ, FLAG_UDP, r );
E
erdgeist 已提交
53 54 55 56 57 58 59

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

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

64 65 66
      outpacket[0] = 0;
      outpacket[1] = inpacket[3];
      udp_make_connectionid( outpacket + 2, remoteip );
E
erdgeist 已提交
67

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

76
      if( !udp_test_connectionid( inpacket, remoteip ))
77
        fprintf( stderr, "UDP connect Connection id missmatch.\n" );
78

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

82 83 84
      numwant = ntohl( inpacket[92/4] );
      if (numwant > 200) numwant = 200;

E
erdgeist 已提交
85
      event = ntohl( inpacket[80/4] );
86 87
      port  = *(uint16_t*)( ((char*)inpacket) + 96 );
      hash  = (ot_hash*)( ((char*)inpacket) + 16 );
E
erdgeist 已提交
88 89 90

      OT_SETIP( &peer, remoteip );
      OT_SETPORT( &peer, &port );
91
      OT_PEERFLAG( &peer ) = 0;
E
erdgeist 已提交
92 93

      switch( event ) {
94 95
        case 1: OT_PEERFLAG( &peer ) |= PEER_FLAG_COMPLETED; break;
        case 3: OT_PEERFLAG( &peer ) |= PEER_FLAG_STOPPED; break;
E
erdgeist 已提交
96 97 98 99
        default: break;
      }

      if( !left )
100
        OT_PEERFLAG( &peer )         |= PEER_FLAG_SEEDING;
E
erdgeist 已提交
101 102 103 104

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

105
      if( OT_PEERFLAG( &peer ) & PEER_FLAG_STOPPED ) /* Peer is gone. */
E
V6  
erdgeist 已提交
106
        r = remove_peer_from_torrent( *hash, &peer, ws->outbuf, FLAG_UDP );
107
      else
E
V6  
erdgeist 已提交
108
        r = 8 + add_peer_to_torrent_and_return_peers( *hash, &peer, FLAG_UDP, numwant, ((char*)outpacket) + 8 );
E
erdgeist 已提交
109

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

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

E
erdgeist 已提交
118 119 120 121
      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++ )
E
V6  
erdgeist 已提交
122
        return_udp_scrape_for_torrent( *(ot_hash*)( ((char*)inpacket) + 16 + 20 * r_out ), ((char*)outpacket) + 8 + 12 * r_out );
E
erdgeist 已提交
123

E
V6  
erdgeist 已提交
124
      socket_send6( serversocket, ws->outbuf, 8 + 12 * r_out, remoteip, remoteport, 0 );
E
erdgeist 已提交
125
      stats_issue_event( EVENT_SCRAPE, FLAG_UDP, r );
E
erdgeist 已提交
126 127 128
      break;
  }
}
E
erdgeist 已提交
129

130 131 132 133
void udp_init( ) {

}

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