if2ip.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #ifdef HAVE_UNISTD_H
  28. #include <unistd.h>
  29. #endif
  30. #include "if2ip.h"
  31. /*
  32. * This test can probably be simplified to #if defined(SIOCGIFADDR) and
  33. * moved after the following includes.
  34. */
  35. #if !defined(WIN32) && !defined(__BEOS__) && !defined(__CYGWIN__) && \
  36. !defined(__riscos__) && !defined(__INTERIX) && !defined(NETWARE) && \
  37. !defined(_AMIGASF) && !defined(__minix)
  38. #ifdef HAVE_SYS_SOCKET_H
  39. #include <sys/socket.h>
  40. #endif
  41. #ifdef HAVE_NETINET_IN_H
  42. #include <netinet/in.h>
  43. #endif
  44. #ifdef HAVE_ARPA_INET_H
  45. #include <arpa/inet.h>
  46. #endif
  47. #ifdef HAVE_SYS_TIME_H
  48. /* This must be before net/if.h for AIX 3.2 to enjoy life */
  49. #include <sys/time.h>
  50. #endif
  51. #ifdef HAVE_NET_IF_H
  52. #include <net/if.h>
  53. #endif
  54. #ifdef HAVE_SYS_IOCTL_H
  55. #include <sys/ioctl.h>
  56. #endif
  57. #ifdef HAVE_NETDB_H
  58. #include <netdb.h>
  59. #endif
  60. #ifdef HAVE_SYS_SOCKIO_H
  61. #include <sys/sockio.h>
  62. #endif
  63. #ifdef VMS
  64. #include <inet.h>
  65. #endif
  66. #include "inet_ntop.h"
  67. #include "memory.h"
  68. /* The last #include file should be: */
  69. #include "memdebug.h"
  70. #define SYS_ERROR -1
  71. char *Curl_if2ip(const char *interface, char *buf, int buf_size)
  72. {
  73. int dummy;
  74. char *ip=NULL;
  75. if(!interface)
  76. return NULL;
  77. dummy = socket(AF_INET, SOCK_STREAM, 0);
  78. if (SYS_ERROR == dummy) {
  79. return NULL;
  80. }
  81. else {
  82. struct ifreq req;
  83. size_t len = strlen(interface);
  84. memset(&req, 0, sizeof(req));
  85. if(len >= sizeof(req.ifr_name))
  86. return NULL; /* this can't be a fine interface name */
  87. memcpy(req.ifr_name, interface, len+1);
  88. req.ifr_addr.sa_family = AF_INET;
  89. #ifdef IOCTL_3_ARGS
  90. if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req)) {
  91. #else
  92. if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
  93. #endif
  94. sclose(dummy);
  95. return NULL;
  96. }
  97. else {
  98. struct in_addr in;
  99. struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
  100. memcpy(&in, &s->sin_addr, sizeof(in));
  101. ip = (char *) Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
  102. }
  103. sclose(dummy);
  104. }
  105. return ip;
  106. }
  107. /* -- end of if2ip() -- */
  108. #else
  109. char *Curl_if2ip(const char *interf, char *buf, int buf_size)
  110. {
  111. (void) interf;
  112. (void) buf;
  113. (void) buf_size;
  114. return NULL;
  115. }
  116. #endif