summaryrefslogtreecommitdiff
path: root/ot_udp.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_udp.c')
-rw-r--r--ot_udp.c26
1 files changed, 14 insertions, 12 deletions
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 @@
13/* Libowfat */ 13/* Libowfat */
14#include "socket.h" 14#include "socket.h"
15#include "io.h" 15#include "io.h"
16#include "ip6.h"
16 17
17/* Opentracker */ 18/* Opentracker */
18#include "trackerlogic.h" 19#include "trackerlogic.h"
@@ -73,7 +74,7 @@ int handle_udp6( int64 serversocket, struct ot_workstruct *ws ) {
73 ot_ip6 remoteip; 74 ot_ip6 remoteip;
74 uint32_t *inpacket = (uint32_t*)ws->inbuf; 75 uint32_t *inpacket = (uint32_t*)ws->inbuf;
75 uint32_t *outpacket = (uint32_t*)ws->outbuf; 76 uint32_t *outpacket = (uint32_t*)ws->outbuf;
76 uint32_t numwant, left, event, scopeid; 77 uint32_t left, event, scopeid;
77 uint32_t connid[2]; 78 uint32_t connid[2];
78 uint32_t action; 79 uint32_t action;
79 uint16_t port, remoteport; 80 uint16_t port, remoteport;
@@ -141,34 +142,35 @@ int handle_udp6( int64 serversocket, struct ot_workstruct *ws ) {
141 /* We do only want to know, if it is zero */ 142 /* We do only want to know, if it is zero */
142 left = inpacket[64/4] | inpacket[68/4]; 143 left = inpacket[64/4] | inpacket[68/4];
143 144
144 /* Limit amount of peers to OT_MAX_PEERS_UDP */
145 numwant = ntohl( inpacket[92/4] );
146 if (numwant > OT_MAX_PEERS_UDP) numwant = OT_MAX_PEERS_UDP;
147
148 event = ntohl( inpacket[80/4] ); 145 event = ntohl( inpacket[80/4] );
149 port = *(uint16_t*)( ((char*)inpacket) + 96 ); 146 port = *(uint16_t*)( ((char*)inpacket) + 96 );
150 ws->hash = (ot_hash*)( ((char*)inpacket) + 16 ); 147 ws->hash = (ot_hash*)( ((char*)inpacket) + 16 );
151 148
152 OT_SETIP( &ws->peer, remoteip ); 149 OT_SETIP( ws->peer, remoteip );
153 OT_SETPORT( &ws->peer, &port ); 150 OT_SETPORT( ws->peer, &port );
154 OT_PEERFLAG( &ws->peer ) = 0; 151 OT_PEERFLAG( ws->peer ) = 0;
155 152
156 switch( event ) { 153 switch( event ) {
157 case 1: OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_COMPLETED; break; 154 case 1: OT_PEERFLAG( ws->peer ) |= PEER_FLAG_COMPLETED; break;
158 case 3: OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_STOPPED; break; 155 case 3: OT_PEERFLAG( ws->peer ) |= PEER_FLAG_STOPPED; break;
159 default: break; 156 default: break;
160 } 157 }
161 158
162 if( !left ) 159 if( !left )
163 OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_SEEDING; 160 OT_PEERFLAG( ws->peer ) |= PEER_FLAG_SEEDING;
164 161
165 outpacket[0] = htonl( 1 ); /* announce action */ 162 outpacket[0] = htonl( 1 ); /* announce action */
166 outpacket[1] = inpacket[12/4]; 163 outpacket[1] = inpacket[12/4];
167 164
168 if( OT_PEERFLAG( &ws->peer ) & PEER_FLAG_STOPPED ) { /* Peer is gone. */ 165 if( OT_PEERFLAG( ws->peer ) & PEER_FLAG_STOPPED ) { /* Peer is gone. */
169 ws->reply = ws->outbuf; 166 ws->reply = ws->outbuf;
170 ws->reply_size = remove_peer_from_torrent( FLAG_UDP, ws ); 167 ws->reply_size = remove_peer_from_torrent( FLAG_UDP, ws );
171 } else { 168 } else {
169 /* Limit amount of peers to OT_MAX_PEERS_UDP */
170 uint32_t numwant = ntohl( inpacket[92/4] );
171 size_t max_peers = ip6_isv4mapped(remoteip) ? OT_MAX_PEERS_UDP4 : OT_MAX_PEERS_UDP6;
172 if (numwant > max_peers) numwant = max_peers;
173
172 ws->reply = ws->outbuf + 8; 174 ws->reply = ws->outbuf + 8;
173 ws->reply_size = 8 + add_peer_to_torrent_and_return_peers( FLAG_UDP, ws, numwant ); 175 ws->reply_size = 8 + add_peer_to_torrent_and_return_peers( FLAG_UDP, ws, numwant );
174 } 176 }