1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2004, 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.32 2004/03/17 12:46:46 bagder Exp $
22 ***************************************************************************/
34 #if !defined(WIN32) && !defined(__BEOS__) && !defined(__CYGWIN32__) && \
35 !defined(__riscos__) && !defined(__INTERIX) && !defined(NETWARE)
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h>
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
43 #ifdef HAVE_ARPA_INET_H
44 #include <arpa/inet.h>
47 #ifdef HAVE_SYS_TIME_H
48 /* This must be before net/if.h for AIX 3.2 to enjoy life */
54 #ifdef HAVE_SYS_IOCTL_H
55 #include <sys/ioctl.h>
63 #ifdef HAVE_SYS_SOCKIO_H
64 #include <sys/sockio.h>
67 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
68 #include "inet_ntoa_r.h"
77 /* The last #include file should be: */
84 char *Curl_if2ip(char *interface, char *buf, int buf_size)
92 dummy = socket(AF_INET, SOCK_STREAM, 0);
93 if (SYS_ERROR == dummy) {
98 memset(&req, 0, sizeof(req));
99 strcpy(req.ifr_name, interface);
100 req.ifr_addr.sa_family = AF_INET;
102 if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req)) {
104 if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
112 struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
113 memcpy(&in, &(s->sin_addr.s_addr), sizeof(in));
114 #if defined(HAVE_INET_NTOA_R)
115 ip = inet_ntoa_r(in,buf,buf_size);
117 ip = strncpy(buf,inet_ntoa(in),buf_size);
118 ip[buf_size - 1] = 0;
126 /* -- end of if2ip() -- */
128 char *Curl_if2ip(char *interface, char *buf, int buf_size)