resolver_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. *
  3. * Copyright (C) 2001-2003 FhG Fokus
  4. *
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * Kamailio is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * Kamailio is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include <netdb.h>
  27. #include <unistd.h>
  28. #include <sys/types.h>
  29. #include <fcntl.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. static char *id="$Id$";
  34. static char *version="resolver_test 0.1";
  35. static char* help_msg="\
  36. Usage: resolver -n address [-c count] [-v]\n\
  37. Options:\n\
  38. -n address address to be resolved\n\
  39. -c count how many times to resolve it\n\
  40. -v increase verbosity level\n\
  41. -V version number\n\
  42. -h this help message\n\
  43. ";
  44. int main (int argc, char** argv)
  45. {
  46. char c;
  47. struct hostent* he;
  48. int ok;
  49. int errors;
  50. int r;
  51. char *tmp;
  52. int count;
  53. int verbose;
  54. char *address;
  55. /* init */
  56. count=0;
  57. verbose=0;
  58. address=0;
  59. ok=errors=0;
  60. opterr=0;
  61. while ((c=getopt(argc,argv, "n:c:vhV"))!=-1){
  62. switch(c){
  63. case 'n':
  64. address=optarg;
  65. break;
  66. case 'v':
  67. verbose++;
  68. break;
  69. case 'c':
  70. count=strtol(optarg, &tmp, 10);
  71. if ((tmp==0)||(*tmp)){
  72. fprintf(stderr, "bad count: -c %s\n", optarg);
  73. goto error;
  74. }
  75. break;
  76. case 'V':
  77. printf("version: %s\n", version);
  78. printf("%s\n",id);
  79. exit(0);
  80. break;
  81. case 'h':
  82. printf("version: %s\n", version);
  83. printf("%s", help_msg);
  84. exit(0);
  85. break;
  86. case '?':
  87. if (isprint(optopt))
  88. fprintf(stderr, "Unknown option `-%c´\n", optopt);
  89. else
  90. fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
  91. goto error;
  92. case ':':
  93. fprintf(stderr, "Option `-%c´ requires an argument.\n",
  94. optopt);
  95. goto error;
  96. break;
  97. default:
  98. abort();
  99. }
  100. }
  101. /* check if all the required params are present */
  102. if (address==0){
  103. fprintf(stderr, "Missing -a address\n");
  104. exit(-1);
  105. }
  106. if(count==0){
  107. fprintf(stderr, "Missing count (-c number)\n");
  108. exit(-1);
  109. }else if(count<0){
  110. fprintf(stderr, "Invalid count (-c %d)\n", count);
  111. exit(-1);
  112. }
  113. /* flood loop */
  114. for (r=0; r<count; r++){
  115. if ((verbose>1)&&(r%1000)) putchar('.');
  116. /* resolve destination loop */
  117. he=gethostbyname(address);
  118. if (he==0){
  119. errors++;
  120. if (verbose>1)
  121. putchar('?');
  122. }else ok++;
  123. }
  124. printf("\n%d requests, %d succeeded, %d errors\n", count, ok, errors);
  125. exit(0);
  126. error:
  127. exit(-1);
  128. }