net.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* $Id: net.c,v 1.6 2002/08/29 19:38:52 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2002 Willem Dijkstra
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials provided
  15. * with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <netdb.h>
  32. #include <netinet/in.h>
  33. #include <sys/socket.h>
  34. #include <arpa/inet.h>
  35. #include <sys/types.h>
  36. #include <limits.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. /*
  40. * lookup( hostname ) - hostname resolver
  41. *
  42. * Lookup returns 1 if hostname could be resolved. Resolved data is
  43. * stored in the globals lookup_hostname, lookup_address and lookup_ip below.
  44. */
  45. char lookup_hostname[_POSIX2_LINE_MAX];
  46. char lookup_address[_POSIX2_LINE_MAX];
  47. u_int32_t lookup_ip;
  48. int
  49. lookup(char *name)
  50. {
  51. struct in_addr addr;
  52. struct hostent *host;
  53. extern int h_errno;
  54. char *chostname, *sptr;
  55. int i, bestl, newl;
  56. strcpy(lookup_hostname, "unresolved.");
  57. strcpy(lookup_address, "unresolved.");
  58. strncat(lookup_hostname, name, (_POSIX2_LINE_MAX - 1 - sizeof("unresolved")));
  59. strncat(lookup_address, name, (_POSIX2_LINE_MAX - 1 - sizeof("unresolved")));
  60. if (4 == sscanf(name, "%4d.%4d.%4d.%4d", &i, &i, &i, &i)) {
  61. addr.s_addr = inet_addr(name);
  62. if (addr.s_addr == 0xffffffff)
  63. return 0;
  64. host = gethostbyaddr((char *)&addr.s_addr, 4, AF_INET);
  65. } else {
  66. host = gethostbyname(name);
  67. }
  68. if (host == NULL) {
  69. return 0;
  70. } else {
  71. i = 0;
  72. newl = 0;
  73. bestl = 0;
  74. sptr = (char *)(host->h_name ? host->h_name : host->h_aliases[0]);
  75. chostname = NULL;
  76. while (sptr) {
  77. newl = strlen(sptr);
  78. if (newl > bestl) {
  79. bestl = newl;
  80. chostname = sptr;
  81. }
  82. sptr = host->h_aliases[i++];
  83. }
  84. if (chostname)
  85. snprintf(lookup_hostname, (_POSIX2_LINE_MAX - 1), "%s", chostname);
  86. if (*host->h_addr_list) {
  87. lookup_ip = ntohl(*(u_int32_t *) *(char **) host->h_addr_list);
  88. snprintf(lookup_address, (_POSIX2_LINE_MAX - 1),"%u.%u.%u.%u",
  89. (lookup_ip >> 24), (lookup_ip >> 16) & 0xff,
  90. (lookup_ip >> 8) & 0xff, lookup_ip & 0xff);
  91. }
  92. }
  93. return 1;
  94. }