diff options
Diffstat (limited to 'ot_accesslist.c')
| -rw-r--r-- | ot_accesslist.c | 52 |
1 files changed, 27 insertions, 25 deletions
diff --git a/ot_accesslist.c b/ot_accesslist.c index d7e88ee..4bf1701 100644 --- a/ot_accesslist.c +++ b/ot_accesslist.c | |||
| @@ -21,8 +21,6 @@ | |||
| 21 | #include <errno.h> | 21 | #include <errno.h> |
| 22 | #endif | 22 | #endif |
| 23 | 23 | ||
| 24 | /* Libowfat */ | ||
| 25 | |||
| 26 | /* Opentracker */ | 24 | /* Opentracker */ |
| 27 | #include "ot_accesslist.h" | 25 | #include "ot_accesslist.h" |
| 28 | #include "ot_hex.h" | 26 | #include "ot_hex.h" |
| @@ -110,7 +108,7 @@ size_t accesslist_parse_hashes(ot_hash *dest, size_t capacity, const char *buf, | |||
| 110 | size_t count = 0; | 108 | size_t count = 0; |
| 111 | 109 | ||
| 112 | while (count < capacity && end - offs >= 40) { | 110 | while (count < capacity && end - offs >= 40) { |
| 113 | if (hex_tohash(dest[count], offs)) { | 111 | if (hex_to_hash(dest[count], offs)) { |
| 114 | offs += 40; | 112 | offs += 40; |
| 115 | 113 | ||
| 116 | if (offs == end || !hex_isdigit((unsigned char)*offs)) | 114 | if (offs == end || !hex_isdigit((unsigned char)*offs)) |
| @@ -162,6 +160,9 @@ static void accesslist_readfile(void) { | |||
| 162 | const char *map; | 160 | const char *map; |
| 163 | size_t maplen, capacity, found; | 161 | size_t maplen, capacity, found; |
| 164 | 162 | ||
| 163 | if (!g_accesslist_filename) | ||
| 164 | return; | ||
| 165 | |||
| 165 | if (accesslist_map(g_accesslist_filename, &map, &maplen) < 0) { | 166 | if (accesslist_map(g_accesslist_filename, &map, &maplen) < 0) { |
| 166 | char *wd = getcwd(NULL, 0); | 167 | char *wd = getcwd(NULL, 0); |
| 167 | fprintf(stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).\nPWD: %s\n", g_accesslist_filename, wd); | 168 | fprintf(stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).\nPWD: %s\n", g_accesslist_filename, wd); |
| @@ -187,7 +188,8 @@ static void accesslist_readfile(void) { | |||
| 187 | if (map) | 188 | if (map) |
| 188 | munmap((void *)map, maplen); | 189 | munmap((void *)map, maplen); |
| 189 | 190 | ||
| 190 | qsort(accesslist_new->list, found, sizeof(ot_hash), vector_compare_hash); | 191 | if (found) |
| 192 | qsort(accesslist_new->list, found, sizeof(ot_hash), vector_compare_hash); | ||
| 191 | accesslist_new->size = found; | 193 | accesslist_new->size = found; |
| 192 | 194 | ||
| 193 | /* Now exchange the accesslist vector in the least race condition prone way */ | 195 | /* Now exchange the accesslist vector in the least race condition prone way */ |
| @@ -225,18 +227,18 @@ int accesslist_hashisvalid(const ot_hash hash) { | |||
| 225 | #endif | 227 | #endif |
| 226 | void *exactmatch = NULL; | 228 | void *exactmatch = NULL; |
| 227 | 229 | ||
| 228 | if (accesslist) | 230 | if (accesslist && accesslist->size) |
| 229 | exactmatch = bsearch(hash, accesslist->list, accesslist->size, OT_HASH_COMPARE_SIZE, vector_compare_hash); | 231 | exactmatch = bsearch(hash, accesslist->list, accesslist->size, OT_HASH_COMPARE_SIZE, vector_compare_hash); |
| 230 | 232 | ||
| 231 | #ifdef WANT_DYNAMIC_ACCESSLIST | 233 | #ifdef WANT_DYNAMIC_ACCESSLIST |
| 232 | /* If we had no match on the main list, scan the list of dynamically added hashes */ | 234 | /* If we had no match on the main list, scan the list of dynamically added hashes */ |
| 233 | accesslist_add = g_accesslist_add; | 235 | accesslist_add = g_accesslist_add; |
| 234 | if ((exactmatch == NULL) && accesslist_add) | 236 | if ((exactmatch == NULL) && accesslist_add && accesslist_add->size) |
| 235 | exactmatch = bsearch(hash, accesslist_add->list, accesslist_add->size, OT_HASH_COMPARE_SIZE, vector_compare_hash); | 237 | exactmatch = bsearch(hash, accesslist_add->list, accesslist_add->size, OT_HASH_COMPARE_SIZE, vector_compare_hash); |
| 236 | 238 | ||
| 237 | /* If we found a matching hash on the main list, scan the list of dynamically deleted hashes */ | 239 | /* If we found a matching hash on the main list, scan the list of dynamically deleted hashes */ |
| 238 | accesslist_delete = g_accesslist_delete; | 240 | accesslist_delete = g_accesslist_delete; |
| 239 | if ((exactmatch != NULL) && accesslist_delete && bsearch(hash, accesslist_delete->list, accesslist_delete->size, OT_HASH_COMPARE_SIZE, vector_compare_hash)) | 241 | if ((exactmatch != NULL) && accesslist_delete && accesslist_delete->size && bsearch(hash, accesslist_delete->list, accesslist_delete->size, OT_HASH_COMPARE_SIZE, vector_compare_hash)) |
| 240 | exactmatch = NULL; | 242 | exactmatch = NULL; |
| 241 | #endif | 243 | #endif |
| 242 | 244 | ||
| @@ -308,7 +310,7 @@ static void *accesslist_adddel_worker(const char *fifoname, ot_accesslist *_ | |||
| 308 | if (linelen < 41) | 310 | if (linelen < 41) |
| 309 | continue; | 311 | continue; |
| 310 | 312 | ||
| 311 | if (!hex_tohash(info_hash, line)) | 313 | if (!hex_to_hash(info_hash, line)) |
| 312 | continue; | 314 | continue; |
| 313 | 315 | ||
| 314 | /* From now on we modify g_accesslist_add and g_accesslist_delete, so prevent the | 316 | /* From now on we modify g_accesslist_add and g_accesslist_delete, so prevent the |
| @@ -339,7 +341,7 @@ static void *accesslist_adddel_worker(const char *fifoname, ot_accesslist *_ | |||
| 339 | } | 341 | } |
| 340 | } else { | 342 | } else { |
| 341 | int exactmatch = 0; | 343 | int exactmatch = 0; |
| 342 | ot_hash *insert_point = binary_search(info_hash, (*adding_to)->list, (*adding_to)->size, sizeof(ot_hash), OT_HASH_COMPARE_SIZE, &exactmatch); | 344 | ot_hash *insert_point = vector_binary_search(info_hash, (*adding_to)->list, (*adding_to)->size, sizeof(ot_hash), OT_HASH_COMPARE_SIZE, &exactmatch); |
| 343 | 345 | ||
| 344 | /* Only if the info hash is not in the adding_to list, create a new head with that entry */ | 346 | /* Only if the info hash is not in the adding_to list, create a new head with that entry */ |
| 345 | if (!exactmatch) { | 347 | if (!exactmatch) { |
| @@ -386,8 +388,9 @@ void accesslist_init(void) { | |||
| 386 | } | 388 | } |
| 387 | 389 | ||
| 388 | void accesslist_deinit(void) { | 390 | void accesslist_deinit(void) { |
| 389 | /* Wake up sleeping worker */ | 391 | /* Wake the worker and wait for it to notice that the tracker is stopping. */ |
| 390 | pthread_kill(thread_id, SIGHUP); | 392 | pthread_kill(thread_id, SIGHUP); |
| 393 | pthread_join(thread_id, NULL); | ||
| 391 | 394 | ||
| 392 | pthread_mutex_lock(&g_accesslist_mutex); | 395 | pthread_mutex_lock(&g_accesslist_mutex); |
| 393 | 396 | ||
| @@ -399,7 +402,6 @@ void accesslist_deinit(void) { | |||
| 399 | #endif | 402 | #endif |
| 400 | 403 | ||
| 401 | pthread_mutex_unlock(&g_accesslist_mutex); | 404 | pthread_mutex_unlock(&g_accesslist_mutex); |
| 402 | pthread_cancel(thread_id); | ||
| 403 | pthread_mutex_destroy(&g_accesslist_mutex); | 405 | pthread_mutex_destroy(&g_accesslist_mutex); |
| 404 | } | 406 | } |
| 405 | 407 | ||
| @@ -416,7 +418,7 @@ void accesslist_cleanup(void) { | |||
| 416 | } | 418 | } |
| 417 | #endif | 419 | #endif |
| 418 | 420 | ||
| 419 | int address_in_net(const ot_ip6 address, const ot_net *net) { | 421 | int accesslist_address_in_net(const ot_ip6 address, const ot_net *net) { |
| 420 | int bits = net->bits, checkbits = (0x7f00 >> (bits & 7)); | 422 | int bits = net->bits, checkbits = (0x7f00 >> (bits & 7)); |
| 421 | int result = memcmp(address, &net->address, bits >> 3); | 423 | int result = memcmp(address, &net->address, bits >> 3); |
| 422 | if (!result && (bits & 7)) | 424 | if (!result && (bits & 7)) |
| @@ -424,7 +426,7 @@ int address_in_net(const ot_ip6 address, const ot_net *net) { | |||
| 424 | return result == 0; | 426 | return result == 0; |
| 425 | } | 427 | } |
| 426 | 428 | ||
| 427 | void *set_value_for_net(const ot_net *net, ot_vector *vector, const void *value, const size_t member_size) { | 429 | void *accesslist_set_value_for_net(const ot_net *net, ot_vector *vector, const void *value, const size_t member_size) { |
| 428 | size_t i; | 430 | size_t i; |
| 429 | int exactmatch; | 431 | int exactmatch; |
| 430 | 432 | ||
| @@ -435,7 +437,7 @@ void *set_value_for_net(const ot_net *net, ot_vector *vector, const void *value, | |||
| 435 | /* Check each net in vector for overlap */ | 437 | /* Check each net in vector for overlap */ |
| 436 | uint8_t *member = ((uint8_t *)vector->data); | 438 | uint8_t *member = ((uint8_t *)vector->data); |
| 437 | for (i = 0; i < vector->size; ++i) { | 439 | for (i = 0; i < vector->size; ++i) { |
| 438 | if (address_in_net(*(ot_ip6 *)member, net) || address_in_net(net->address, (ot_net *)member)) | 440 | if (accesslist_address_in_net(*(ot_ip6 *)member, net) || accesslist_address_in_net(net->address, (ot_net *)member)) |
| 439 | return 0; | 441 | return 0; |
| 440 | member += member_size; | 442 | member += member_size; |
| 441 | } | 443 | } |
| @@ -451,16 +453,16 @@ void *set_value_for_net(const ot_net *net, ot_vector *vector, const void *value, | |||
| 451 | 453 | ||
| 452 | /* Takes a vector filled with { ot_net net, uint8_t[x] value }; | 454 | /* Takes a vector filled with { ot_net net, uint8_t[x] value }; |
| 453 | Returns value associated with the net, or NULL if not found */ | 455 | Returns value associated with the net, or NULL if not found */ |
| 454 | void *get_value_for_net(const ot_ip6 address, const ot_vector *vector, const size_t member_size) { | 456 | void *accesslist_get_value_for_net(const ot_ip6 address, const ot_vector *vector, const size_t member_size) { |
| 455 | int exactmatch; | 457 | int exactmatch; |
| 456 | /* This binary search will return a pointer to the first non-containing network... */ | 458 | /* This binary search will return a pointer to the first non-containing network... */ |
| 457 | ot_net *net = binary_search(address, vector->data, vector->size, member_size, sizeof(ot_ip6), &exactmatch); | 459 | ot_net *net = vector_binary_search(address, vector->data, vector->size, member_size, sizeof(ot_ip6), &exactmatch); |
| 458 | if (!net) | 460 | if (!net) |
| 459 | return NULL; | 461 | return NULL; |
| 460 | /* ... so we'll need to move back one step unless we've exactly hit the first address in network */ | 462 | /* ... so we'll need to move back one step unless we've exactly hit the first address in network */ |
| 461 | if (!exactmatch && ((void *)net > vector->data)) | 463 | if (!exactmatch && ((void *)net > vector->data)) |
| 462 | --net; | 464 | --net; |
| 463 | if (!address_in_net(address, net)) | 465 | if (!accesslist_address_in_net(address, net)) |
| 464 | return NULL; | 466 | return NULL; |
| 465 | return (void *)net; | 467 | return (void *)net; |
| 466 | } | 468 | } |
| @@ -472,7 +474,7 @@ static pthread_mutex_t g_lognets_list_mutex = PTHREAD_MUTEX_INITIALIZER; | |||
| 472 | 474 | ||
| 473 | void loglist_add_network(const ot_net *net) { | 475 | void loglist_add_network(const ot_net *net) { |
| 474 | pthread_mutex_lock(&g_lognets_list_mutex); | 476 | pthread_mutex_lock(&g_lognets_list_mutex); |
| 475 | set_value_for_net(net, &g_lognets_list, NULL, sizeof(ot_net)); | 477 | accesslist_set_value_for_net(net, &g_lognets_list, NULL, sizeof(ot_net)); |
| 476 | pthread_mutex_unlock(&g_lognets_list_mutex); | 478 | pthread_mutex_unlock(&g_lognets_list_mutex); |
| 477 | } | 479 | } |
| 478 | 480 | ||
| @@ -487,7 +489,7 @@ void loglist_reset(void) { | |||
| 487 | int loglist_check_address(const ot_ip6 address) { | 489 | int loglist_check_address(const ot_ip6 address) { |
| 488 | int result; | 490 | int result; |
| 489 | pthread_mutex_lock(&g_lognets_list_mutex); | 491 | pthread_mutex_lock(&g_lognets_list_mutex); |
| 490 | result = (NULL != get_value_for_net(address, &g_lognets_list, sizeof(ot_net))); | 492 | result = (NULL != accesslist_get_value_for_net(address, &g_lognets_list, sizeof(ot_net))); |
| 491 | pthread_mutex_unlock(&g_lognets_list_mutex); | 493 | pthread_mutex_unlock(&g_lognets_list_mutex); |
| 492 | return result; | 494 | return result; |
| 493 | } | 495 | } |
| @@ -508,17 +510,17 @@ int proxylist_add_network(const ot_net *proxy, const ot_net *net) { | |||
| 508 | pthread_mutex_lock(&g_proxies_list_mutex); | 510 | pthread_mutex_lock(&g_proxies_list_mutex); |
| 509 | 511 | ||
| 510 | /* If we have a direct hit, use and extend the vector there */ | 512 | /* If we have a direct hit, use and extend the vector there */ |
| 511 | map = binary_search(proxy, g_proxies_list.data, g_proxies_list.size, sizeof(ot_proxymap), sizeof(ot_net), &exactmatch); | 513 | map = vector_binary_search(proxy, g_proxies_list.data, g_proxies_list.size, sizeof(ot_proxymap), sizeof(ot_net), &exactmatch); |
| 512 | 514 | ||
| 513 | if (!map || !exactmatch) { | 515 | if (!map || !exactmatch) { |
| 514 | /* else see, if we've got overlapping networks | 516 | /* else see, if we've got overlapping networks |
| 515 | and get a new empty vector if not */ | 517 | and get a new empty vector if not */ |
| 516 | ot_vector empty; | 518 | ot_vector empty; |
| 517 | memset(&empty, 0, sizeof(ot_vector)); | 519 | memset(&empty, 0, sizeof(ot_vector)); |
| 518 | map = set_value_for_net(proxy, &g_proxies_list, &empty, sizeof(ot_proxymap)); | 520 | map = accesslist_set_value_for_net(proxy, &g_proxies_list, &empty, sizeof(ot_proxymap)); |
| 519 | } | 521 | } |
| 520 | 522 | ||
| 521 | if (map && set_value_for_net(net, &map->networks, NULL, sizeof(ot_net))) | 523 | if (map && accesslist_set_value_for_net(net, &map->networks, NULL, sizeof(ot_net))) |
| 522 | result = 1; | 524 | result = 1; |
| 523 | 525 | ||
| 524 | pthread_mutex_unlock(&g_proxies_list_mutex); | 526 | pthread_mutex_unlock(&g_proxies_list_mutex); |
| @@ -531,8 +533,8 @@ int proxylist_check_proxy(const ot_ip6 proxy, const ot_ip6 address) { | |||
| 531 | 533 | ||
| 532 | pthread_mutex_lock(&g_proxies_list_mutex); | 534 | pthread_mutex_lock(&g_proxies_list_mutex); |
| 533 | 535 | ||
| 534 | if ((map = get_value_for_net(proxy, &g_proxies_list, sizeof(ot_proxymap)))) | 536 | if ((map = accesslist_get_value_for_net(proxy, &g_proxies_list, sizeof(ot_proxymap)))) |
| 535 | if (!address || get_value_for_net(address, &map->networks, sizeof(ot_net))) | 537 | if (!address || accesslist_get_value_for_net(address, &map->networks, sizeof(ot_net))) |
| 536 | result = 1; | 538 | result = 1; |
| 537 | 539 | ||
| 538 | pthread_mutex_unlock(&g_proxies_list_mutex); | 540 | pthread_mutex_unlock(&g_proxies_list_mutex); |
| @@ -587,7 +589,7 @@ int accesslist_bless_net(ot_net *net, ot_permissions permissions) { | |||
| 587 | int accesslist_is_blessed(ot_ip6 ip, ot_permissions permissions) { | 589 | int accesslist_is_blessed(ot_ip6 ip, ot_permissions permissions) { |
| 588 | size_t i; | 590 | size_t i; |
| 589 | for (i = 0; i < g_admin_nets_count; ++i) | 591 | for (i = 0; i < g_admin_nets_count; ++i) |
| 590 | if (address_in_net(ip, g_admin_nets + i) && (g_admin_nets_permissions[i] & permissions)) | 592 | if (accesslist_address_in_net(ip, g_admin_nets + i) && (g_admin_nets_permissions[i] & permissions)) |
| 591 | return 1; | 593 | return 1; |
| 592 | return 0; | 594 | return 0; |
| 593 | } | 595 | } |
