gethostbyaddr.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <netdb.h>
  24. static char* id="$Id$";
  25. static char* version="gethostbyaddr 0.1";
  26. static char* help_msg="\
  27. Usage: gethostbyaddr [-hV] -n host\n\
  28. Options:\n\
  29. -n host host name\n\
  30. -V version number\n\
  31. -h this help message\n\
  32. ";
  33. int main(int argc, char** argv)
  34. {
  35. char c;
  36. char* name;
  37. struct hostent* he;
  38. unsigned char** h;
  39. name=0;
  40. opterr=0;
  41. while ((c=getopt(argc, argv, "n:hV"))!=-1){
  42. switch(c){
  43. case 'n':
  44. name=optarg;
  45. break;
  46. case 'V':
  47. printf("version: %s\n", version);
  48. printf("%s\n", id);
  49. exit(0);
  50. break;
  51. case 'h':
  52. printf("version: %s\n", version);
  53. printf("%s", help_msg);
  54. exit(0);
  55. break;
  56. case '?':
  57. if (isprint(optopt))
  58. fprintf(stderr, "Unknown option `-%c´\n", optopt);
  59. else
  60. fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
  61. goto error;
  62. case ':':
  63. fprintf(stderr, "Option `-%c´ requires an argument.\n",
  64. optopt);
  65. goto error;
  66. break;
  67. default:
  68. abort();
  69. }
  70. }
  71. if (name==0){
  72. fprintf(stderr, "Missing domain name (-n name)\n");
  73. goto error;
  74. }
  75. he=gethostbyname(name);
  76. if (he==0){
  77. printf("bad address <%s>\n", name);
  78. goto error;
  79. }
  80. he=gethostbyaddr(he->h_addr_list[0], he->h_length, he->h_addrtype);
  81. if (he==0) printf("no answer\n");
  82. else{
  83. printf("h_name=%s\n", he->h_name);
  84. for(h=he->h_aliases;*h;h++)
  85. printf(" alias=%s\n", *h);
  86. for(h=he->h_addr_list;*h;h++)
  87. printf(" ip=%d.%d.%d.%d\n", (*h)[0],(*h)[1],(*h)[2],(*h)[3] );
  88. }
  89. printf("done\n");
  90. exit(0);
  91. error:
  92. exit(-1);
  93. }