1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, 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 * $Id: if2ip.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
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__) && \
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
48 #ifdef HAVE_NETINET_IN_H
49 #include <netinet/in.h>
51 #ifdef HAVE_ARPA_INET_H
52 #include <arpa/inet.h>
55 #ifdef HAVE_SYS_TIME_H
56 /* This must be before net/if.h for AIX 3.2 to enjoy life */
62 #ifdef HAVE_SYS_IOCTL_H
63 #include <sys/ioctl.h>
70 #ifdef HAVE_SYS_SOCKIO_H
71 #include <sys/sockio.h>
78 #include "inet_ntop.h"
81 /* The last #include file should be: */
86 char *Curl_if2ip(const char *interface
, char *buf
, int buf_size
)
94 dummy
= socket(AF_INET
, SOCK_STREAM
, 0);
95 if(SYS_ERROR
== dummy
) {
100 size_t len
= strlen(interface
);
101 memset(&req
, 0, sizeof(req
));
102 if(len
>= sizeof(req
.ifr_name
)) {
104 return NULL
; /* this can't be a fine interface name */
106 memcpy(req
.ifr_name
, interface
, len
+1);
107 req
.ifr_addr
.sa_family
= AF_INET
;
109 if(SYS_ERROR
== ioctl(dummy
, SIOCGIFADDR
, &req
)) {
111 if(SYS_ERROR
== ioctl(dummy
, SIOCGIFADDR
, &req
, sizeof(req
))) {
119 struct sockaddr_in
*s
= (struct sockaddr_in
*)&req
.ifr_dstaddr
;
120 memcpy(&in
, &s
->sin_addr
, sizeof(in
));
121 ip
= (char *) Curl_inet_ntop(s
->sin_family
, &in
, buf
, buf_size
);
128 /* -- end of if2ip() -- */
130 char *Curl_if2ip(const char *interf
, char *buf
, int buf_size
)