if2ip.c 3.3 KB

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