/misc/dnstest.cpp

https://github.com/privacore/open-source-search-engine · C++ · 138 lines · 75 code · 19 blank · 44 comment · 19 complexity · f861fab1da358682e6995c4e2987a4ec MD5 · raw file

  1. // Matt Wells, copyright Jan 2002
  2. // program to test Rdb
  3. #include "gb-include.h"
  4. #include "Rdb.h"
  5. #include "Conf.h"
  6. #include <pthread.h>
  7. #include "Dns.h"
  8. #include "Url.h"
  9. static void timeWrapper ( int fd , void *state ) ;
  10. static void dnsWrapper ( void *state , int32_t ip ) ;
  11. static int32_t s_max = 10;
  12. int main ( int argc , char *argv[] ) {
  13. // init our table for doing zobrist hashing
  14. if ( ! hashinit() ) {
  15. log("main::hashinit failed" ); return 1; }
  16. if ( argc < 2 ) {
  17. //fprintf(stderr,"dnstest <# threads> [conf filename]\n");
  18. fprintf(stderr,"dnstest <# threads> < FILEOFURLS\n");
  19. return -1;
  20. }
  21. g_mem.m_maxMem = 1000000000LL; // 1G
  22. // default conf filename
  23. // char *confFilename = "./gb.conf";
  24. char *confFilename = "./";
  25. // set threads
  26. s_max = atoi(argv[1]);
  27. // use the command line parm as the full conf filename
  28. //if ( argc == 3 ) confFilename = argv[2];
  29. // use default
  30. //if ( argc < 3 ) confFilename = "/gigablast/gigablast.conf";
  31. // start up log file
  32. /*
  33. if ( ! g_log.init( "/tmp/log" ) ) {
  34. fprintf (stderr,"main::Log init failed\n" ); return 1; }
  35. */
  36. // make a new conf
  37. // makeNewConf ( 0 , confFilename );
  38. // read in the conf file
  39. if ( ! g_conf.init ( confFilename ) ) {
  40. fprintf (stderr,"main::Conf init failed\n" ); return 1; }
  41. // debug the udp traffic
  42. //g_conf.m_logDebugUdp = true;
  43. g_conf.m_logDebugDns = true;
  44. // init the memory class after conf since it gets maxMem from Conf
  45. // if ( ! g_mem.init ( 1024*1024*30 ) ) {
  46. // fprintf (stderr,"main::Mem init failed\n" ); return 1; }
  47. // init the loop
  48. if ( ! g_loop.init() ) {
  49. log("main::Loop init failed" ); return 1; }
  50. //if( ! g_threads.init()){
  51. // log("main:Init failed."); return 1;}
  52. // start up hostdb
  53. if ( ! g_hostdb.init("./hosts.conf" , 0 ) ) {
  54. log("main::Hostdb init failed" ); return 1; }
  55. // . then dns client
  56. // . server should listen to a socket and register with g_loop
  57. if ( ! g_dns.init(8855) ) {
  58. log("main::Dns client init failed" ); return 1; }
  59. // every .1 seconds launch a dns request
  60. if (!g_loop.registerSleepCallback(100,NULL,timeWrapper))
  61. return false;
  62. if ( ! g_loop.runLoop() ) {
  63. log("main::runLoop failed" ); return 1; }
  64. return 0;
  65. }
  66. class StateT {
  67. public:
  68. int32_t m_ip;
  69. char m_buf[1024];
  70. int64_t m_time;
  71. };
  72. static int32_t s_count = 0;
  73. void timeWrapper ( int fd , void *state ) {
  74. top:
  75. // bail if too many launched
  76. if ( s_count >= s_max ) return;
  77. // new state
  78. StateT *st = (StateT *)mmalloc ( sizeof(StateT) , "dnstest" );
  79. // get url from stdin into buf
  80. char *p = st->m_buf;
  81. if ( ! fgets ( p , 1023 , stdin ) ) exit ( 0 );
  82. // trim tail
  83. while ( p[0] && !isalnum ( p [ gbstrlen(p) - 1] ) ) p [gbstrlen(p)-1]='\0';
  84. // time it
  85. st->m_time = gettimeofdayInMilliseconds();
  86. // then look it up
  87. Url url;
  88. url.set ( p , gbstrlen(p) );
  89. int32_t status = g_dns.getIp( url.getHost() ,
  90. url.getHostLen() , &st->m_ip , st , dnsWrapper );
  91. logf(LOG_INFO, "dnstest: Looking up %s", url.getHost());
  92. if(g_errno) {
  93. logf(LOG_INFO,"dns: %s.",mstrerror(g_errno));
  94. }
  95. // return on error
  96. if ( status == -1 ) { log("ipWrapper: error"); return; }
  97. // handle return if did not block
  98. if ( status != 0 ) {
  99. s_count++;
  100. //log(LOG_INFO,"dns: status is not 0, calling dnsWrapper");
  101. dnsWrapper ( st , st->m_ip );
  102. }
  103. // otherwise count it
  104. if ( status == 0 ) {
  105. s_count++;
  106. //log(LOG_INFO,"dns: status is 0, increment s_count to %i",
  107. // s_count);
  108. }
  109. // loop to do more
  110. goto top;
  111. }
  112. void dnsWrapper ( void *state , int32_t ip ) {
  113. StateT *st = (StateT *)state;
  114. int64_t time = gettimeofdayInMilliseconds() - st->m_time ;
  115. fprintf ( stderr,"Response: %"INT64"ms %s %s (%s)\n", time,
  116. st->m_buf , iptoa(ip) , mstrerror(g_errno));
  117. mfree ( st , sizeof(StateT), "dnstest" );
  118. s_count--;
  119. }