Skip to content

Commit 1ee9b42

Browse files
committed
cleanup
1 parent 6c17a1c commit 1ee9b42

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

src/kad.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,16 +384,16 @@ int kad_announce_once( const uint8_t id[], int port ) {
384384
/*
385385
* Add a new value to the announcement list or refresh an announcement.
386386
*/
387-
int kad_announce( const char query_raw[], int port, time_t lifetime ) {
388-
char query[QUERY_MAX_SIZE];
387+
int kad_announce( const char query[], int port, time_t lifetime ) {
388+
char hostname[QUERY_MAX_SIZE];
389389

390390
// Remove .p2p suffix and convert to lowercase
391-
if( query_sanitize( query, sizeof(query), query_raw ) != 0 ) {
391+
if( query_sanitize( hostname, sizeof(hostname), query ) != 0 ) {
392392
return -1;
393393
}
394394

395395
// Store query to call kad_announce_once() later/multiple times
396-
return announces_add( query, port, lifetime ) ? 0 : -2;
396+
return announces_add( hostname, port, lifetime ) ? 0 : -2;
397397
}
398398

399399
// Lookup known nodes that are nearest to the given id
@@ -403,18 +403,18 @@ int kad_lookup( const char query[], IP addr_array[], size_t addr_num ) {
403403

404404
// Trim spaces, remove .p2p suffix and convert to lowercase
405405
if( query_sanitize( hostname, sizeof(hostname), query ) != 0 ) {
406-
log_debug( "query_sanitize error" );
406+
log_debug( "KAD: query_sanitize error" );
407407
return -1;
408408
}
409409

410-
log_debug( "Lookup identifier: %s", hostname );
410+
log_debug( "KAD: Lookup identifier: %s", hostname );
411411

412412
// Find existing or create new search
413413
search = searches_start( hostname );
414414

415415
if( search == NULL ) {
416416
// Failed to create a new search
417-
log_debug("searches_start error");
417+
log_debug( "KAD: searches_start error" );
418418
return -1;
419419
}
420420

@@ -569,7 +569,7 @@ void kad_debug_storage( int fd ) {
569569

570570
s = storage;
571571
for( j = 0; s; ++j ) {
572-
dprintf( fd, " id: %s\n", str_id(s->id ));
572+
dprintf( fd, " id: %s\n", str_id( s->id ));
573573
for( i = 0; i < s->numpeers; ++i ) {
574574
p = &s->peers[i];
575575
to_addr( &addr, &p->ip, p->len, htons( p->port ) );

src/searches.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static const char *str_state( int state ) {
4747
case AUTH_WAITING: return "WAITING";
4848
default:
4949
log_err( "Invalid state: %d", state );
50-
exit(1);
50+
exit( 1 );
5151
}
5252
}
5353

@@ -127,6 +127,7 @@ static struct search_t *find_next_search( auth_callback *callback ) {
127127
return NULL;
128128
}
129129

130+
// Find query/IP-address to authenticate; callback is used as a marker.
130131
struct result_t *searches_get_auth_target( char query[], IP *addr, auth_callback *callback ) {
131132
struct search_t *search;
132133
struct result_t *result;
@@ -150,6 +151,7 @@ struct result_t *searches_get_auth_target( char query[], IP *addr, auth_callback
150151
return result;
151152
}
152153

154+
// Set the authentication state of a result
153155
void searches_set_auth_state( const char query[], const IP *addr, const int state ) {
154156
struct search_t *search;
155157
struct result_t *result;

src/utils.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
int hex_get_id( uint8_t id[], size_t len, const char query[] ) {
2121
size_t query_len = strlen( query );
2222
if( str_isHex( query, query_len ) ) {
23-
memset( id, 0, len ); // Fill up id with random numbers?
23+
memset( id, 0, len );
2424
bytes_from_hex( id, query, MIN( 2 * len, query_len ) );
2525
return 1;
2626
}
@@ -43,14 +43,9 @@ int is_suffix( const char str[], const char suffix[] ) {
4343
}
4444
}
4545

46-
void* memdup( const void* src, size_t size ) {
47-
void* out = malloc(size);
48-
return memcpy( out, src, size );
49-
}
50-
5146
/*
52-
* Sanitize query.
53-
* Convert to lowercase and removing TLDs if it matches --query-tld.
47+
* Sanitize a query string.
48+
* Convert to lowercase and remove the TLD if it matches --query-tld.
5449
*
5550
* example.com.p2p => example.com
5651
* example.com => example.com
@@ -176,10 +171,10 @@ int id_equal( const uint8_t id1[], const uint8_t id2[] ) {
176171
}
177172

178173
// Check if string consist of hexdecimal characters
179-
int str_isHex( const char str[], size_t size ) {
174+
int str_isHex( const char str[], size_t len ) {
180175
size_t i = 0;
181176

182-
for( i = 0; i < size; i++ ) {
177+
for( i = 0; i < len; i++ ) {
183178
const char c = str[i];
184179
if( (c >= '0' && c <= '9')
185180
|| (c >= 'A' && c <= 'F')
@@ -190,8 +185,8 @@ int str_isHex( const char str[], size_t size ) {
190185
}
191186
}
192187

193-
// Return 1 if size is even
194-
return !(size & 1);
188+
// Return 1 if len is even
189+
return !(len & 1);
195190
}
196191

197192
// Matches [0-9a-zA-Z._-]*
@@ -261,8 +256,7 @@ const char *str_addr( const IP *addr ) {
261256
return addrbuf;
262257
}
263258

264-
int addr_is_localhost( const IP *addr )
265-
{
259+
int addr_is_localhost( const IP *addr ) {
266260
// 127.0.0.1
267261
const uint32_t inaddr_loopback = htonl( INADDR_LOOPBACK );
268262

@@ -276,8 +270,7 @@ int addr_is_localhost( const IP *addr )
276270
}
277271
}
278272

279-
int addr_is_multicast( const IP *addr )
280-
{
273+
int addr_is_multicast( const IP *addr ) {
281274
switch( addr->ss_family ) {
282275
case AF_INET:
283276
return IN_MULTICAST(ntohl(((IP4*) addr)->sin_addr.s_addr));

src/utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
// Number of elements in an array
99
#define N_ELEMS(x) (sizeof(x) / sizeof(x[0]))
1010

11+
// Typical min/max methods
1112
#define MAX(x, y) ((x) >= (y) ? (x) : (y))
1213
#define MIN(x, y) ((x) <= (y) ? (x) : (y))
1314

15+
// Make a symbol into a string literal
1416
#define STR_HELPER(x) #x
1517
#define STR(x) STR_HELPER(x)
1618

@@ -24,7 +26,6 @@
2426
int hex_get_id( uint8_t id[], size_t len, const char query[] );
2527

2628
int is_suffix( const char str[], const char suffix[] );
27-
void* memdup(const void* src, size_t size);
2829

2930
int query_sanitize( char buf[], size_t buflen, const char query[] );
3031

0 commit comments

Comments
 (0)