1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2008, 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: if2ip.c,v 1.63 2008-12-30 08:05:38 gknauf Exp $
22 ***************************************************************************/
23
24 #include "setup.h"
25
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #ifdef HAVE_SYS_SOCKET_H
30 # include <sys/socket.h>
31 #endif
32 #ifdef HAVE_NETINET_IN_H
33 # include <netinet/in.h>
34 #endif
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h>
37 #endif
38 #ifdef HAVE_NET_IF_H
39 # include <net/if.h>
40 #endif
41 #ifdef HAVE_SYS_IOCTL_H
42 # include <sys/ioctl.h>
43 #endif
44 #ifdef HAVE_NETDB_H
45 # include <netdb.h>
46 #endif
47 #ifdef HAVE_SYS_SOCKIO_H
48 # include <sys/sockio.h>
49 #endif
50 #ifdef HAVE_IFADDRS_H
51 # include <ifaddrs.h>
52 #endif
53 #ifdef HAVE_STROPTS_H
54 # include <stropts.h>
55 #endif
56 #ifdef VMS
57 # include <inet.h>
58 #endif
59
60 #include "inet_ntop.h"
61 #include "strequal.h"
62 #include "if2ip.h"
63
64 #define _MPRINTF_REPLACE /* use our functions only */
65 #include <curl/mprintf.h>
66
67 #include "memory.h"
68 /* The last #include file should be: */
69 #include "memdebug.h"
70
71 /* ------------------------------------------------------------------ */
72
73 #if defined(HAVE_GETIFADDRS)
74
Curl_if2ip(int af,const char * interface,char * buf,int buf_size)75 char *Curl_if2ip(int af, const char *interface, char *buf, int buf_size)
76 {
77 struct ifaddrs *iface, *head;
78 char *ip=NULL;
79
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)) {
85 void *addr;
86 char scope[12]="";
87 #ifdef ENABLE_IPV6
88 if (af == AF_INET6) {
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;
94 #endif
95 if (scopeid)
96 snprintf(scope, sizeof(scope), "%%%u", scopeid);
97 }
98 else
99 #endif
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);
103 break;
104 }
105 }
106 freeifaddrs(head);
107 }
108 return ip;
109 }
110
111 #elif defined(HAVE_IOCTL_SIOCGIFADDR)
112
113 #define SYS_ERROR -1
114
Curl_if2ip(int af,const char * interface,char * buf,int buf_size)115 char *Curl_if2ip(int af, const char *interface, char *buf, int buf_size)
116 {
117 int dummy;
118 char *ip=NULL;
119
120 if(!interface || (af != AF_INET))
121 return NULL;
122
123 dummy = socket(AF_INET, SOCK_STREAM, 0);
124 if(SYS_ERROR == dummy) {
125 return NULL;
126 }
127 else {
128 struct ifreq req;
129 size_t len = strlen(interface);
130 memset(&req, 0, sizeof(req));
131 if(len >= sizeof(req.ifr_name)) {
132 sclose(dummy);
133 return NULL; /* this can't be a fine interface name */
134 }
135 memcpy(req.ifr_name, interface, len+1);
136 req.ifr_addr.sa_family = AF_INET;
137 if(SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req)) {
138 sclose(dummy);
139 return NULL;
140 }
141 else {
142 struct in_addr in;
143
144 struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_addr;
145 memcpy(&in, &s->sin_addr, sizeof(in));
146 ip = (char *) Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
147 }
148 sclose(dummy);
149 }
150 return ip;
151 }
152
153 #else
154
Curl_if2ip(int af,const char * interf,char * buf,int buf_size)155 char *Curl_if2ip(int af, const char *interf, char *buf, int buf_size)
156 {
157 (void) af;
158 (void) interf;
159 (void) buf;
160 (void) buf_size;
161 return NULL;
162 }
163
164 #endif
165