5
5
* | (__| |_| | _ <| |___
6
6
* \___|\___/|_| \_\_____|
8
* Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
8
* Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
10
10
* This software is licensed as described in the file COPYING, which
11
11
* you should have received as part of this distribution. The terms
18
18
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
* KIND, either express or implied.
21
* $Id: if2ip.c,v 1.53 2008-04-22 22:53:54 danf Exp $
21
* $Id: if2ip.c,v 1.63 2008-12-30 08:05:38 gknauf Exp $
22
22
***************************************************************************/
30
26
#ifdef HAVE_UNISTD_H
37
* This test can probably be simplified to #if defined(SIOCGIFADDR) and
38
* moved after the following includes.
40
#if !defined(WIN32) && !defined(__BEOS__) && !defined(__CYGWIN__) && \
41
!defined(__riscos__) && !defined(__INTERIX) && !defined(NETWARE) && \
42
!defined(__AMIGA__) && !defined(__minix) && !defined(__SYMBIAN32__)
44
29
#ifdef HAVE_SYS_SOCKET_H
45
#include <sys/socket.h>
30
# include <sys/socket.h>
47
32
#ifdef HAVE_NETINET_IN_H
48
#include <netinet/in.h>
33
# include <netinet/in.h>
50
35
#ifdef HAVE_ARPA_INET_H
51
#include <arpa/inet.h>
54
#ifdef HAVE_SYS_TIME_H
55
/* This must be before net/if.h for AIX 3.2 to enjoy life */
36
# include <arpa/inet.h>
58
38
#ifdef HAVE_NET_IF_H
61
41
#ifdef HAVE_SYS_IOCTL_H
62
#include <sys/ioctl.h>
42
# include <sys/ioctl.h>
65
44
#ifdef HAVE_NETDB_H
69
47
#ifdef HAVE_SYS_SOCKIO_H
70
#include <sys/sockio.h>
48
# include <sys/sockio.h>
77
60
#include "inet_ntop.h"
64
#define _MPRINTF_REPLACE /* use our functions only */
65
#include <curl/mprintf.h>
78
67
#include "memory.h"
80
68
/* The last #include file should be: */
81
69
#include "memdebug.h"
71
/* ------------------------------------------------------------------ */
73
#if defined(HAVE_GETIFADDRS)
75
char *Curl_if2ip(int af, const char *interface, char *buf, int buf_size)
77
struct ifaddrs *iface, *head;
80
if (getifaddrs(&head) >= 0) {
81
for (iface=head; iface != NULL; iface=iface->ifa_next) {
82
if ((iface->ifa_addr != NULL) &&
83
(iface->ifa_addr->sa_family == af) &&
84
curl_strequal(iface->ifa_name, interface)) {
89
unsigned int scopeid = 0;
90
addr = &((struct sockaddr_in6 *)iface->ifa_addr)->sin6_addr;
91
#ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
92
/* Include the scope of this interface as part of the address */
93
scopeid = ((struct sockaddr_in6 *)iface->ifa_addr)->sin6_scope_id;
96
snprintf(scope, sizeof(scope), "%%%u", scopeid);
100
addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
101
ip = (char *) Curl_inet_ntop(af, addr, buf, buf_size);
102
strlcat(buf, scope, buf_size);
111
#elif defined(HAVE_IOCTL_SIOCGIFADDR)
83
113
#define SYS_ERROR -1
85
char *Curl_if2ip(const char *interface, char *buf, int buf_size)
115
char *Curl_if2ip(int af, const char *interface, char *buf, int buf_size)
120
if(!interface || (af != AF_INET))
93
123
dummy = socket(AF_INET, SOCK_STREAM, 0);
105
135
memcpy(req.ifr_name, interface, len+1);
106
136
req.ifr_addr.sa_family = AF_INET;
108
137
if(SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req)) {
110
if(SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
116
142
struct in_addr in;
118
struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
144
struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_addr;
119
145
memcpy(&in, &s->sin_addr, sizeof(in));
120
146
ip = (char *) Curl_inet_ntop(s->sin_family, &in, buf, buf_size);