gethostbyaddr.c 2.6 KB

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