if2ip.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2007, Daniel Stenberg, <[email protected]>, 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(__AMIGA__) && !defined(__minix) && !defined(__SYMBIAN32__) && \
  38. !defined(__WATCOMC__)
  39. #ifdef HAVE_SYS_SOCKET_H
  40. #include <sys/socket.h>
  41. #endif
  42. #ifdef HAVE_NETINET_IN_H
  43. #include <netinet/in.h>
  44. #endif
  45. #ifdef HAVE_ARPA_INET_H
  46. #include <arpa/inet.h>
  47. #endif
  48. #ifdef HAVE_SYS_TIME_H
  49. /* This must be before net/if.h for AIX 3.2 to enjoy life */
  50. #include <sys/time.h>
  51. #endif
  52. #ifdef HAVE_NET_IF_H
  53. #include <net/if.h>
  54. #endif
  55. #ifdef HAVE_SYS_IOCTL_H
  56. #include <sys/ioctl.h>
  57. #endif
  58. #ifdef HAVE_NETDB_H
  59. #include <netdb.h>
  60. #endif
  61. #ifdef HAVE_SYS_SOCKIO_H
  62. #include <sys/sockio.h>
  63. #endif
  64. #ifdef VMS
  65. #include <inet.h>
  66. #endif
  67. #include "inet_ntop.h"
  68. #include "memory.h"
  69. /* The last #include file should be: */
  70. #include "memdebug.h"
  71. #define SYS_ERROR -1
  72. char *Curl_if2ip(const char *interface, char *buf, int buf_size)
  73. {
  74. int dummy;
  75. char *ip=NULL;
  76. if(!interface)
  77. return NULL;
  78. dummy = socket(AF_INET, SOCK_STREAM, 0);
  79. if(SYS_ERROR == dummy) {
  80. return NULL;
  81. }
  82. else {
  83. struct ifreq req;
  84. size_t len = strlen(interface);
  85. memset(&req, 0, sizeof(req));
  86. if(len >= sizeof(req.ifr_name)) {
  87. sclose(dummy);
  88. return NULL; /* this can't be a fine interface name */
  89. }
  90. memcpy(req.ifr_name, interface, len+1);
  91. req.ifr_addr.sa_family = AF_INET;
  92. #ifdef IOCTL_3_ARGS
  93. if(SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req)) {
  94. #else
  95. if(SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
  96. #endif
  97. sclose(dummy);
  98. return NULL;
  99. }
  100. else {
  101. struct in_addr in;
  102. struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
  103. memcpy(&in, &s->sin_addr, sizeof(in));
  104. ip = (char *) Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
  105. }
  106. sclose(dummy);
  107. }
  108. return ip;
  109. }
  110. /* -- end of if2ip() -- */
  111. #else
  112. char *Curl_if2ip(const char *interf, char *buf, int buf_size)
  113. {
  114. (void) interf;
  115. (void) buf;
  116. (void) buf_size;
  117. return NULL;
  118. }
  119. #endif