From 2afc4893bf802700a1decfff57673cefc861c7e7 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Wed, 3 Apr 2024 22:25:30 +0200 Subject: Prepare opentracker for dual stack capabilities --- ot_udp.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'ot_udp.c') diff --git a/ot_udp.c b/ot_udp.c index 8309660..c32a7e2 100644 --- a/ot_udp.c +++ b/ot_udp.c @@ -13,6 +13,7 @@ /* Libowfat */ #include "socket.h" #include "io.h" +#include "ip6.h" /* Opentracker */ #include "trackerlogic.h" @@ -73,7 +74,7 @@ int handle_udp6( int64 serversocket, struct ot_workstruct *ws ) { ot_ip6 remoteip; uint32_t *inpacket = (uint32_t*)ws->inbuf; uint32_t *outpacket = (uint32_t*)ws->outbuf; - uint32_t numwant, left, event, scopeid; + uint32_t left, event, scopeid; uint32_t connid[2]; uint32_t action; uint16_t port, remoteport; @@ -141,34 +142,35 @@ int handle_udp6( int64 serversocket, struct ot_workstruct *ws ) { /* We do only want to know, if it is zero */ left = inpacket[64/4] | inpacket[68/4]; - /* Limit amount of peers to OT_MAX_PEERS_UDP */ - numwant = ntohl( inpacket[92/4] ); - if (numwant > OT_MAX_PEERS_UDP) numwant = OT_MAX_PEERS_UDP; - event = ntohl( inpacket[80/4] ); port = *(uint16_t*)( ((char*)inpacket) + 96 ); ws->hash = (ot_hash*)( ((char*)inpacket) + 16 ); - OT_SETIP( &ws->peer, remoteip ); - OT_SETPORT( &ws->peer, &port ); - OT_PEERFLAG( &ws->peer ) = 0; + OT_SETIP( ws->peer, remoteip ); + OT_SETPORT( ws->peer, &port ); + OT_PEERFLAG( ws->peer ) = 0; switch( event ) { - case 1: OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_COMPLETED; break; - case 3: OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_STOPPED; break; + case 1: OT_PEERFLAG( ws->peer ) |= PEER_FLAG_COMPLETED; break; + case 3: OT_PEERFLAG( ws->peer ) |= PEER_FLAG_STOPPED; break; default: break; } if( !left ) - OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_SEEDING; + OT_PEERFLAG( ws->peer ) |= PEER_FLAG_SEEDING; outpacket[0] = htonl( 1 ); /* announce action */ outpacket[1] = inpacket[12/4]; - if( OT_PEERFLAG( &ws->peer ) & PEER_FLAG_STOPPED ) { /* Peer is gone. */ + 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 { + /* Limit amount of peers to OT_MAX_PEERS_UDP */ + uint32_t numwant = ntohl( inpacket[92/4] ); + size_t max_peers = ip6_isv4mapped(remoteip) ? OT_MAX_PEERS_UDP4 : OT_MAX_PEERS_UDP6; + if (numwant > max_peers) numwant = max_peers; + ws->reply = ws->outbuf + 8; ws->reply_size = 8 + add_peer_to_torrent_and_return_peers( FLAG_UDP, ws, numwant ); } -- cgit v1.2.3