if2ip.c 3.1 KB

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