mirror of
				git://erdgeist.org/opentracker
				synced 2025-11-04 11:53:22 +08:00 
			
		
		
		
	Make code endianess save
This commit is contained in:
		
							parent
							
								
									9e93e6b6e1
								
							
						
					
					
						commit
						82b4df6751
					
				@ -114,6 +114,7 @@ void httpresponse(struct http_data* h,int64 s)
 | 
				
			|||||||
    ot_torrent *torrent;
 | 
					    ot_torrent *torrent;
 | 
				
			||||||
    ot_hash    *hash = NULL;
 | 
					    ot_hash    *hash = NULL;
 | 
				
			||||||
    int         numwant, tmp, scanon;
 | 
					    int         numwant, tmp, scanon;
 | 
				
			||||||
 | 
					    unsigned short port = htons(6881);
 | 
				
			||||||
    size_t      reply_size = 0;
 | 
					    size_t      reply_size = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    array_cat0(&h->r);
 | 
					    array_cat0(&h->r);
 | 
				
			||||||
@ -187,8 +188,9 @@ e400:
 | 
				
			|||||||
      if( byte_diff(data,8,"announce"))
 | 
					      if( byte_diff(data,8,"announce"))
 | 
				
			||||||
        goto e404;
 | 
					        goto e404;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      peer.ip = h->ip;
 | 
					      OT_SETIP( &peer, &h->ip);
 | 
				
			||||||
      peer.port_flags = 6881 << 16;
 | 
					      OT_SETPORT( &peer, &port );
 | 
				
			||||||
 | 
					      OT_FLAG( &peer ) = 0;
 | 
				
			||||||
      numwant = 50;
 | 
					      numwant = 50;
 | 
				
			||||||
      scanon = 1;
 | 
					      scanon = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -202,12 +204,12 @@ e400:
 | 
				
			|||||||
        case 4:
 | 
					        case 4:
 | 
				
			||||||
          if(!byte_diff(data,4,"port")) {
 | 
					          if(!byte_diff(data,4,"port")) {
 | 
				
			||||||
            size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
 | 
					            size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
 | 
				
			||||||
            if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) || (tmp > 65536) ) goto e404;
 | 
					            if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) || ( tmp > 0xffff ) ) goto e404;
 | 
				
			||||||
            peer.port_flags = ( tmp << 16 ) | ( peer.port_flags & 0xffff );
 | 
					            port = htons( tmp ); OT_SETPORT ( &peer, &port );
 | 
				
			||||||
          } else if(!byte_diff(data,4,"left")) {
 | 
					          } else if(!byte_diff(data,4,"left")) {
 | 
				
			||||||
            size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
 | 
					            size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
 | 
				
			||||||
            if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) ) goto e404;
 | 
					            if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) ) goto e404;
 | 
				
			||||||
            if( !tmp ) peer.port_flags |= PEER_FLAG_SEEDING;
 | 
					            if( !tmp ) OT_FLAG( &peer ) |= PEER_FLAG_SEEDING;
 | 
				
			||||||
          } else
 | 
					          } else
 | 
				
			||||||
            scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
 | 
					            scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
@ -218,10 +220,10 @@ e400:
 | 
				
			|||||||
          case -1:
 | 
					          case -1:
 | 
				
			||||||
            goto e404;
 | 
					            goto e404;
 | 
				
			||||||
          case 7:
 | 
					          case 7:
 | 
				
			||||||
            if(!byte_diff(data,7,"stopped")) peer.port_flags |= PEER_FLAG_STOPPED;
 | 
					            if(!byte_diff(data,7,"stopped")) OT_FLAG( &peer ) |= PEER_FLAG_STOPPED;
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
          case 9:
 | 
					          case 9:
 | 
				
			||||||
            if(!byte_diff(data,9,"complete")) peer.port_flags |= PEER_FLAG_COMPLETED;
 | 
					            if(!byte_diff(data,9,"complete")) OT_FLAG( &peer ) |= PEER_FLAG_COMPLETED;
 | 
				
			||||||
          default: // Fall through intended
 | 
					          default: // Fall through intended
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
@ -263,7 +265,7 @@ e400:
 | 
				
			|||||||
      /* Scanned whole query string */
 | 
					      /* Scanned whole query string */
 | 
				
			||||||
      if( !hash ) goto e404;
 | 
					      if( !hash ) goto e404;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if( peer.port_flags & PEER_FLAG_STOPPED ) {
 | 
					      if( OT_FLAG( &peer ) & PEER_FLAG_STOPPED ) {
 | 
				
			||||||
        remove_peer_from_torrent( hash, &peer );
 | 
					        remove_peer_from_torrent( hash, &peer );
 | 
				
			||||||
        reply = strdup( "d15:warning message4:Okaye" ); reply_size = 26;
 | 
					        reply = strdup( "d15:warning message4:Okaye" ); reply_size = 26;
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
 | 
				
			|||||||
@ -18,9 +18,7 @@
 | 
				
			|||||||
// Helper functions for binary_find
 | 
					// Helper functions for binary_find
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); }
 | 
					int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); }
 | 
				
			||||||
int compare_ip_port( const void *peer1, const void *peer2 ) {
 | 
					int compare_ip_port( const void *peer1, const void *peer2 ) { return memcmp( peer1, peer2, 6 ); }
 | 
				
			||||||
if( ((ot_peer*)peer1)->ip != ((ot_peer*)peer2)->ip ) return ((ot_peer*)peer1)->ip - ((ot_peer*)peer2)->ip;
 | 
					 | 
				
			||||||
return ((ot_peer*)peer1)->port_flags - ((ot_peer*)peer2)->port_flags; }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void *binary_search( const void *key, const void *base,
 | 
					static void *binary_search( const void *key, const void *base,
 | 
				
			||||||
  unsigned long member_count, const unsigned long member_size,
 | 
					  unsigned long member_count, const unsigned long member_size,
 | 
				
			||||||
@ -82,7 +80,7 @@ static int vector_remove_peer( ot_vector *vector, ot_peer *peer ) {
 | 
				
			|||||||
  match = BINARY_FIND( peer, vector->data, vector->size, sizeof( ot_peer ), compare_ip_port, &exactmatch );
 | 
					  match = BINARY_FIND( peer, vector->data, vector->size, sizeof( ot_peer ), compare_ip_port, &exactmatch );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if( !exactmatch ) return 0;
 | 
					  if( !exactmatch ) return 0;
 | 
				
			||||||
  exactmatch = match->port_flags & PEER_FLAG_SEEDING ? 2 : 1;
 | 
					  exactmatch = OT_FLAG( match ) & PEER_FLAG_SEEDING ? 2 : 1;
 | 
				
			||||||
  MEMMOVE( match, match + 1, ((ot_peer*)vector->data) + vector->size - match - 1 );
 | 
					  MEMMOVE( match, match + 1, ((ot_peer*)vector->data) + vector->size - match - 1 );
 | 
				
			||||||
  vector->size--;
 | 
					  vector->size--;
 | 
				
			||||||
  return exactmatch;
 | 
					  return exactmatch;
 | 
				
			||||||
@ -164,7 +162,7 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ) {
 | 
				
			|||||||
  if( !exactmatch ) {
 | 
					  if( !exactmatch ) {
 | 
				
			||||||
    int i;
 | 
					    int i;
 | 
				
			||||||
    MEMMOVE( peer_dest, peer, sizeof( ot_peer ) );
 | 
					    MEMMOVE( peer_dest, peer, sizeof( ot_peer ) );
 | 
				
			||||||
    if( peer->port_flags & PEER_FLAG_SEEDING )
 | 
					    if( OT_FLAG(peer) & PEER_FLAG_SEEDING )
 | 
				
			||||||
      torrent->peer_list->seed_count[0]++;
 | 
					      torrent->peer_list->seed_count[0]++;
 | 
				
			||||||
    for( i=1; i<OT_POOLS_COUNT; ++i ) {
 | 
					    for( i=1; i<OT_POOLS_COUNT; ++i ) {
 | 
				
			||||||
      switch( vector_remove_peer( &torrent->peer_list->peers[i], peer ) ) {
 | 
					      switch( vector_remove_peer( &torrent->peer_list->peers[i], peer ) ) {
 | 
				
			||||||
@ -174,12 +172,12 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ) {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    if( (peer_dest->port_flags & PEER_FLAG_SEEDING ) && !(peer->port_flags & PEER_FLAG_SEEDING ) )
 | 
					    if( (OT_FLAG(peer_dest) & PEER_FLAG_SEEDING ) && !(OT_FLAG(peer) & PEER_FLAG_SEEDING ) )
 | 
				
			||||||
      torrent->peer_list->seed_count[0]--;
 | 
					      torrent->peer_list->seed_count[0]--;
 | 
				
			||||||
    if( !(peer_dest->port_flags & PEER_FLAG_SEEDING ) && (peer->port_flags & PEER_FLAG_SEEDING ) )
 | 
					    if( !(OT_FLAG(peer_dest) & PEER_FLAG_SEEDING ) && (OT_FLAG(peer) & PEER_FLAG_SEEDING ) )
 | 
				
			||||||
      torrent->peer_list->seed_count[0]++;
 | 
					      torrent->peer_list->seed_count[0]++;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  if( peer->port_flags & PEER_FLAG_COMPLETED )
 | 
					  if( OT_FLAG(peer) & PEER_FLAG_COMPLETED )
 | 
				
			||||||
    torrent->peer_list->downloaded++;
 | 
					    torrent->peer_list->downloaded++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return torrent;
 | 
					  return torrent;
 | 
				
			||||||
 | 
				
			|||||||
@ -39,13 +39,16 @@ typedef struct {
 | 
				
			|||||||
} ot_vector;
 | 
					} ot_vector;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct {
 | 
					typedef struct {
 | 
				
			||||||
  ot_ip    ip;
 | 
					  ot_byte data[8];
 | 
				
			||||||
  ot_dword port_flags;
 | 
					 | 
				
			||||||
} ot_peer;
 | 
					} ot_peer;
 | 
				
			||||||
static const ot_byte PEER_FLAG_SEEDING   = 0x80;
 | 
					static const ot_byte PEER_FLAG_SEEDING   = 0x80;
 | 
				
			||||||
static const ot_byte PEER_FLAG_COMPLETED = 0x40;
 | 
					static const ot_byte PEER_FLAG_COMPLETED = 0x40;
 | 
				
			||||||
static const ot_byte PEER_FLAG_STOPPED   = 0x20;
 | 
					static const ot_byte PEER_FLAG_STOPPED   = 0x20;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define OT_SETIP( peer, ip ) MEMMOVE((peer),(ip),4);
 | 
				
			||||||
 | 
					#define OT_SETPORT( peer, port ) MEMMOVE((peer),(port),2);
 | 
				
			||||||
 | 
					#define OT_FLAG(peer) (((ot_byte*)(peer))[6])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct {
 | 
					typedef struct {
 | 
				
			||||||
  ot_time        base;
 | 
					  ot_time        base;
 | 
				
			||||||
  unsigned long  seed_count[ OT_POOLS_COUNT ];
 | 
					  unsigned long  seed_count[ OT_POOLS_COUNT ];
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user