bulkquery.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include <arpa/nameser.h>
  2. #include <lwres/lwres.h>
  3. #include <strings.h>
  4. #include <stdio.h>
  5. #include <pthread.h>
  6. #define BUFLEN 1024
  7. #define MAXSTR 80
  8. #define MAXTHREADS 50
  9. #define MAXRBLS 40
  10. #define DEFTTL 600
  11. extern int errno;
  12. extern int h_errno;
  13. struct ipnode;
  14. typedef struct ipnode *iplist;
  15. struct ipnode {
  16. char *IP;
  17. iplist next;
  18. };
  19. iplist IPs;
  20. pthread_mutex_t *mutexp;
  21. pthread_mutex_t *mutexoutput;
  22. char *dnsrbls[MAXRBLS];
  23. int numrbls, numthreads, numqueries, defttl;
  24. void do_queries () {
  25. iplist tIP;
  26. lwres_context_t *ctx = NULL;
  27. lwres_grbnresponse_t *response = NULL;
  28. int n, i;
  29. pthread_mutex_lock(mutexp);
  30. tIP = IPs;
  31. if (IPs != NULL) {
  32. IPs = tIP->next;
  33. }
  34. pthread_mutex_unlock(mutexp);
  35. while (tIP != NULL) {
  36. //fprintf (stderr, "making query %s\n", tIP->IP); fflush(stderr);
  37. if (lwres_context_create(&ctx, NULL, NULL, NULL, 0) != 0) {
  38. fprintf (stderr, "Couldn't create context\n");
  39. return;
  40. } else {
  41. lwres_conf_parse(ctx, lwres_resolv_conf);
  42. //pthread_mutex_lock(mutexoutput);
  43. n = lwres_getrdatabyname(ctx, tIP->IP, ns_c_in, ns_t_a, 0, &response);
  44. //pthread_mutex_unlock(mutexoutput);
  45. if (n == LWRES_R_SUCCESS) {
  46. printf ("%s,%d.%d.%d.%d,%d\n", tIP->IP,
  47. response->rdatas[0][0], response->rdatas[0][1],
  48. response->rdatas[0][2], response->rdatas[0][3],
  49. response->ttl);
  50. //fprintf (stderr, "freeing response\n"); fflush(stderr);
  51. lwres_grbnresponse_free(ctx, &response);
  52. } else {
  53. //fprintf (stderr, "Nothing found\n");
  54. printf ("%s, %s, %d\n", tIP->IP, tIP->IP, defttl);
  55. }
  56. //fprintf (stderr, "freeing context\n"); fflush(stderr);
  57. lwres_context_destroy(&ctx);
  58. //fprintf (stderr, "done freeing\n"); fflush(stderr);
  59. }
  60. pthread_mutex_lock(mutexp);
  61. tIP = IPs;
  62. if (IPs != NULL) {
  63. IPs = tIP->next;
  64. }
  65. pthread_mutex_unlock(mutexp);
  66. }
  67. }
  68. void GetRBLs() {
  69. char instr[MAXSTR];
  70. numrbls = 0;
  71. while ((fgets(instr, MAXSTR, stdin) != NULL) && (numrbls < MAXRBLS)) {
  72. instr[strlen(instr)-1] = 0; // strip off newline
  73. if (strncmp(instr, "----------", 10) == 0) {
  74. return;
  75. }
  76. dnsrbls[numrbls] = (char *) malloc(strlen(instr)+1);
  77. if (dnsrbls[numrbls] == NULL) {
  78. fprintf (stderr, "Couldn't allocate memory for %d DNS RBLs\n", numrbls);
  79. exit (10);
  80. } else {
  81. strcpy (dnsrbls[numrbls], instr);
  82. numrbls++;
  83. }
  84. }
  85. }
  86. main () {
  87. pthread_t threads[MAXTHREADS];
  88. char instr[MAXSTR];
  89. iplist tIP;
  90. int loop1;
  91. if (fgets(instr, MAXSTR, stdin) != NULL) {
  92. defttl = atoi(instr);
  93. }
  94. if (defttl < 0)
  95. defttl = DEFTTL;
  96. GetRBLs();
  97. // for (loop1=0; loop1<numrbls; loop1++)
  98. // fprintf (stderr, "%s\n", dnsrbls[loop1]);
  99. // fprintf (stderr, "----------\n");
  100. numqueries = 0;
  101. IPs = NULL;
  102. while (fgets(instr, MAXSTR, stdin) != NULL) {
  103. instr[strlen(instr)-1] = 0;
  104. for (loop1 = 0; loop1 < numrbls; loop1++) {
  105. tIP = (iplist)malloc(sizeof(struct ipnode));
  106. tIP->IP = (char *)malloc(strlen(instr)+strlen(dnsrbls[loop1])+2);
  107. strcpy (tIP->IP, instr);
  108. strcat (tIP->IP, dnsrbls[loop1]);
  109. tIP->next = IPs;
  110. IPs = tIP;
  111. numqueries++;
  112. }
  113. }
  114. // fprintf (stderr, "%d queries to make\n", numqueries);
  115. // tIP = IPs;
  116. // while (tIP != NULL) {
  117. // fprintf (stderr, "%s\n", tIP->IP);
  118. // tIP = tIP->next;
  119. // }
  120. // fprintf (stderr, "done\n");
  121. // exit (0);
  122. mutexp=(pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
  123. pthread_mutex_init(mutexp, NULL);
  124. mutexoutput=(pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
  125. pthread_mutex_init(mutexoutput, NULL);
  126. numthreads = 0; // number of threads created successfully
  127. for (loop1 = 0; ((loop1<MAXTHREADS) && (loop1<numqueries)); loop1++) {
  128. if (pthread_create(&threads[loop1], NULL,
  129. (void *) do_queries, NULL) != 0) {
  130. fprintf (stderr, "Couldn't make more than %d threads\n", numthreads);
  131. break;
  132. } else {
  133. numthreads++;
  134. }
  135. }
  136. for (loop1 = 0; loop1 < numthreads ; loop1++) {
  137. pthread_join(threads[loop1], NULL);
  138. }
  139. //do_queries();
  140. exit (0);
  141. }