if2ip.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2002, 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. #ifdef HAVE_SYS_SOCKET_H
  32. #include <sys/socket.h>
  33. #endif
  34. #ifdef HAVE_NETINET_IN_H
  35. #include <netinet/in.h>
  36. #endif
  37. #ifdef HAVE_ARPA_INET_H
  38. #include <arpa/inet.h>
  39. #endif
  40. #ifdef HAVE_SYS_TIME_H
  41. /* This must be before net/if.h for AIX 3.2 to enjoy life */
  42. #include <sys/time.h>
  43. #endif
  44. #ifdef HAVE_NET_IF_H
  45. #include <net/if.h>
  46. #endif
  47. #include <sys/ioctl.h>
  48. /* -- if2ip() -- */
  49. #ifdef HAVE_NETDB_H
  50. #include <netdb.h>
  51. #endif
  52. #ifdef HAVE_SYS_SOCKIO_H
  53. #include <sys/sockio.h>
  54. #endif
  55. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  56. #include "inet_ntoa_r.h"
  57. #endif
  58. #ifdef VMS
  59. #define IOCTL_3_ARGS
  60. #include <inet.h>
  61. #endif
  62. /* The last #include file should be: */
  63. #ifdef MALLOCDEBUG
  64. #include "memdebug.h"
  65. #endif
  66. #define SYS_ERROR -1
  67. char *Curl_if2ip(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. memset(&req, 0, sizeof(req));
  80. strcpy(req.ifr_name, interface);
  81. req.ifr_addr.sa_family = AF_INET;
  82. #ifdef IOCTL_3_ARGS
  83. if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req)) {
  84. #else
  85. if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
  86. #endif
  87. sclose(dummy);
  88. return NULL;
  89. }
  90. else {
  91. struct in_addr in;
  92. struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
  93. memcpy(&in, &(s->sin_addr.s_addr), sizeof(in));
  94. #if defined(HAVE_INET_NTOA_R)
  95. ip = inet_ntoa_r(in,buf,buf_size);
  96. #else
  97. ip = strncpy(buf,inet_ntoa(in),buf_size);
  98. ip[buf_size - 1] = 0;
  99. #endif
  100. }
  101. sclose(dummy);
  102. }
  103. return ip;
  104. }
  105. /* -- end of if2ip() -- */
  106. #else
  107. #define if2ip(x) NULL
  108. #endif
  109. /*
  110. * local variables:
  111. * eval: (load-file "../curl-mode.el")
  112. * end:
  113. * vim600: fdm=marker
  114. * vim: et sw=2 ts=2 sts=2 tw=78
  115. */