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