dict_test.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <stdio.h>
  2. #include "uthash.h"
  3. #include <netinet/ip.h>
  4. struct groupinfo {
  5. uint32_t id; /* use this field as key (name has to be "id" */
  6. /* new_marker_value = markervalue_start + (ip - nw_addr) / group_size */
  7. uint32_t nw_addr;
  8. uint32_t group_size;
  9. uint32_t markervalue_start;
  10. UT_hash_handle hh; /* makes this structure hashable */
  11. };
  12. struct groupinfo *group_hashtable = NULL;
  13. void add_group(struct groupinfo *g) {
  14. struct groupinfo *g_find;
  15. HASH_FIND_INT(group_hashtable, &g->id, g_find); /* id already in the hash? */
  16. if (g_find == NULL) {
  17. HASH_ADD_INT( group_hashtable, id, g );
  18. } else {
  19. printf("id already in hashtable, not adding: %d", g->id);
  20. }
  21. }
  22. /*
  23. INFO: It is ok to have concurrent readers on hashtable since uthash 1.5
  24. */
  25. struct groupinfo *find_group(int ip_prefix) {
  26. struct groupinfo *s;
  27. HASH_FIND_INT( group_hashtable, &ip_prefix, s );
  28. return s;
  29. }
  30. void delete_group(struct groupinfo *g) {
  31. HASH_DEL( group_hashtable, g);
  32. }
  33. void read_groups(char *filename, uint32_t markervalue_bits) {
  34. char line[1000];
  35. /* file format: ip\tmarkervalue\tsubnets */
  36. FILE *ip_file = fopen(filename, "r");
  37. while (fgets(line, 1000, ip_file) != NULL) {
  38. if (!ip_file) {
  39. perror("Error while opening the group file.\n");
  40. exit(EXIT_FAILURE);
  41. }
  42. /* split by tabs */
  43. char* token = strtok(line, "\t");
  44. //printf("%s\n", token);
  45. struct in_addr addr;
  46. if (!inet_aton(token, &addr)) {
  47. printf("could not parse ip: %s\n", token);
  48. }
  49. uint32_t ip_int_ho = (uint32_t)ntohl(addr.s_addr);
  50. // remove trailing checksum bits
  51. // 0xAAAAAABB -> 0x00AAAAAA
  52. //ip_int_ho >>= (32 - markervalue_bits);
  53. //printf("ip: %u\n", ip_int_ho);
  54. token = strtok(NULL, "\t");
  55. //printf("%s\n", token);
  56. uint32_t markervalue = atoi(token);
  57. //printf("markervlue: %d\n", markervalue);
  58. token = strtok(NULL, "\t");
  59. //printf("%s\n", token);
  60. uint32_t subnets = atoi(token);
  61. //printf("subnets: %d\n", subnets);
  62. //printf("------\n");
  63. struct groupinfo *newentry;
  64. newentry = malloc(sizeof(struct groupinfo));
  65. newentry->id = ip_int_ho;
  66. add_group(newentry);
  67. }
  68. fclose(ip_file);
  69. unsigned int groups_total;
  70. groups_total = HASH_COUNT(group_hashtable);
  71. printf("there are %u groups\n", groups_total);
  72. }
  73. int main() {
  74. read_groups("./groupfile_test3.csv", 24);
  75. }
  76. int main_() {
  77. int i = 0;
  78. for (; i < 500000; ++i) {
  79. struct groupinfo *newentry;
  80. newentry = malloc(sizeof(struct groupinfo));
  81. newentry->id = i;
  82. add_group(newentry);
  83. }
  84. unsigned int groups_total;
  85. groups_total = HASH_COUNT(group_hashtable);
  86. printf("there are %u groups\n", groups_total);
  87. struct groupinfo *g = find_group(1234);
  88. printf("value: %d\n", g->id);
  89. sleep(99999);
  90. }