1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifdef HAVE_NETINET_IN_H
26 # include <netinet/in.h>
28 #ifdef HAVE_ARPA_INET_H
29 # include <arpa/inet.h>
34 #ifdef HAVE_SYS_IOCTL_H
35 # include <sys/ioctl.h>
40 #ifdef HAVE_SYS_SOCKIO_H
41 # include <sys/sockio.h>
53 #include "inet_ntop.h"
57 #define _MPRINTF_REPLACE /* use our functions only */
58 #include <curl/mprintf.h>
60 #include "curl_memory.h"
61 /* The last #include file should be: */
64 /* ------------------------------------------------------------------ */
66 #if defined(HAVE_GETIFADDRS)
68 bool Curl_if_is_interface_name(const char *interf)
72 struct ifaddrs *iface, *head;
74 if(getifaddrs(&head) >= 0) {
75 for(iface=head; iface != NULL; iface=iface->ifa_next) {
76 if(curl_strequal(iface->ifa_name, interf)) {
86 char *Curl_if2ip(int af, const char *interf, char *buf, int buf_size)
88 struct ifaddrs *iface, *head;
91 if(getifaddrs(&head) >= 0) {
92 for(iface=head; iface != NULL; iface=iface->ifa_next) {
93 if((iface->ifa_addr != NULL) &&
94 (iface->ifa_addr->sa_family == af) &&
95 curl_strequal(iface->ifa_name, interf)) {
100 unsigned int scopeid = 0;
101 addr = &((struct sockaddr_in6 *)iface->ifa_addr)->sin6_addr;
102 #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
103 /* Include the scope of this interface as part of the address */
104 scopeid = ((struct sockaddr_in6 *)iface->ifa_addr)->sin6_scope_id;
107 snprintf(scope, sizeof(scope), "%%%u", scopeid);
111 addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
112 ip = (char *) Curl_inet_ntop(af, addr, buf, buf_size);
113 strlcat(buf, scope, buf_size);
122 #elif defined(HAVE_IOCTL_SIOCGIFADDR)
124 bool Curl_if_is_interface_name(const char *interf)
126 /* This is here just to support the old interfaces */
129 char *ip = Curl_if2ip(AF_INET, interf, buf, sizeof(buf));
131 return (ip != NULL) ? TRUE : FALSE;
134 char *Curl_if2ip(int af, const char *interf, char *buf, int buf_size)
138 struct sockaddr_in *s;
143 if(!interf || (af != AF_INET))
146 len = strlen(interf);
147 if(len >= sizeof(req.ifr_name))
150 dummy = socket(AF_INET, SOCK_STREAM, 0);
151 if(CURL_SOCKET_BAD == dummy)
154 memset(&req, 0, sizeof(req));
155 memcpy(req.ifr_name, interf, len+1);
156 req.ifr_addr.sa_family = AF_INET;
158 if(ioctl(dummy, SIOCGIFADDR, &req) < 0) {
163 s = (struct sockaddr_in *)&req.ifr_addr;
164 memcpy(&in, &s->sin_addr, sizeof(in));
165 ip = (char *) Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
173 bool Curl_if_is_interface_name(const char *interf)
180 char *Curl_if2ip(int af, const char *interf, char *buf, int buf_size)