int parse_packet( const char str[] ) { const char *beg; int port = 0; /* Find port (required) */ beg = parse_packet_param( str, "Port: "); if( beg == NULL ) { return 0; } /* Read port */ if( sscanf( beg, "%d\r\n", &port ) != 1 && port > 0 && port < 65536 ) { return 0; } /* Find infohash (optional) */ beg = parse_packet_param( str, "Infohash: " ); if( beg != NULL ) { /* Read infohash for own request */ if( (strstr(beg, "\r\n") - beg) == SHA1_HEX_LENGTH && str_isHex( beg, SHA1_HEX_LENGTH ) && memcmp( beg, LPD_DEFAULT_INFOHASH, SHA1_HEX_LENGTH ) != 0 ) { memcpy( g_infohash, beg, SHA1_HEX_LENGTH ); } } return port; }
int parse_packet( const char *str ) { const char *beg; int port = 0; /* Parse port */ beg = parse_packet_param( str, "Port: "); if( beg == NULL ) { return 0; } if( sscanf( beg, "%d\r\n", &port ) != 1 && port > 0 && port < 65536 ) { return 0; } /* Check for existence of server field */ beg = parse_packet_param( str, "Server: "); if( beg == NULL ) { return 0; } /* Check for existence of version field */ beg = parse_packet_param( str, "Version: "); if( beg == NULL ) { return 0; } return port; }