From 8d00b1e039938c13d47c116827f74211a2d89838 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Sat, 15 Dec 2007 17:11:43 +0000 Subject: Prepare udp connection id generation and checking --- ot_udp.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/ot_udp.c b/ot_udp.c index 56b5f44..f34d08f 100644 --- a/ot_udp.c +++ b/ot_udp.c @@ -4,6 +4,7 @@ /* System */ #include #include +#include /* Libowfat */ #include "socket.h" @@ -17,6 +18,24 @@ static char static_inbuf[8192]; static char static_outbuf[8192]; +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 */ + remoteip = remoteip; + + /* Use a static secret for now */ + memcpy( connid, g_static_connid, 8 ); +} + +static int udp_test_connectionid( const uint32_t * const connid, const char * remoteip ) { + /* Touch unused variable */ + remoteip = remoteip; + + /* Test against our static secret */ + return !memcmp( connid, g_static_connid, 8 ); +} + /* UDP implementation according to http://xbtt.sourceforge.net/udp_tracker_protocol.html */ void handle_udp4( int64 serversocket ) { ot_peer peer; @@ -44,8 +63,9 @@ void handle_udp4( int64 serversocket ) { switch( ntohl( inpacket[2] ) ) { case 0: /* This is a connect action */ - outpacket[0] = 0; outpacket[1] = inpacket[3]; - outpacket[2] = inpacket[0]; outpacket[3] = inpacket[1]; + outpacket[0] = 0; + outpacket[1] = inpacket[3]; + udp_make_connectionid( outpacket + 2, remoteip ); socket_send4( serversocket, static_outbuf, 16, remoteip, remoteport ); stats_issue_event( EVENT_CONNECT, 0, 16 ); break; @@ -54,6 +74,9 @@ void handle_udp4( int64 serversocket ) { if( r < 98 ) return; + if( !udp_test_connectionid( inpacket, remoteip )) + fprintf( stderr, "UDP Connection id missmatch\n" ); + numwant = 200; /* We do only want to know, if it is zero */ left = inpacket[64/4] | inpacket[68/4]; @@ -93,6 +116,9 @@ void handle_udp4( int64 serversocket ) { break; case 2: /* This is a scrape action */ + if( !udp_test_connectionid( inpacket, remoteip )) + fprintf( stderr, "UDP Connection id missmatch\n" ); + outpacket[0] = htonl( 2 ); /* scrape action */ outpacket[1] = inpacket[12/4]; -- cgit v1.2.3