resolver_test.c 3.3 KB

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