if2ip.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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. #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. /* The last #include file should be: */
  66. #ifdef CURLDEBUG
  67. #include "memdebug.h"
  68. #endif
  69. #define SYS_ERROR -1
  70. char *Curl_if2ip(char *interface, char *buf, int buf_size)
  71. {
  72. int dummy;
  73. char *ip=NULL;
  74. if(!interface)
  75. return NULL;
  76. dummy = socket(AF_INET, SOCK_STREAM, 0);
  77. if (SYS_ERROR == dummy) {
  78. return NULL;
  79. }
  80. else {
  81. struct ifreq req;
  82. memset(&req, 0, sizeof(req));
  83. strcpy(req.ifr_name, interface);
  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. #if defined(HAVE_INET_NTOA_R)
  98. ip = inet_ntoa_r(in,buf,buf_size);
  99. #else
  100. ip = strncpy(buf,inet_ntoa(in),buf_size);
  101. ip[buf_size - 1] = 0;
  102. #endif
  103. }
  104. sclose(dummy);
  105. }
  106. return ip;
  107. }
  108. /* -- end of if2ip() -- */
  109. #else
  110. char *Curl_if2ip(char *interface, char *buf, int buf_size)
  111. {
  112. return NULL;
  113. }
  114. #endif