db28e4aad9a76e534e72d07d832d669a5e094797
[platform/upstream/curl.git] / lib / if2ip.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2004, 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$
22  ***************************************************************************/
23
24 #include "setup.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #include "if2ip.h"
35
36 #if !defined(WIN32) && !defined(__BEOS__) && !defined(__CYGWIN32__) && \
37     !defined(__riscos__) && !defined(__INTERIX) && !defined(NETWARE)
38
39 #ifdef HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
41 #endif
42 #ifdef HAVE_NETINET_IN_H
43 #include <netinet/in.h>
44 #endif
45 #ifdef HAVE_ARPA_INET_H
46 #include <arpa/inet.h>
47 #endif
48
49 #ifdef HAVE_SYS_TIME_H
50 /* This must be before net/if.h for AIX 3.2 to enjoy life */
51 #include <sys/time.h>
52 #endif
53 #ifdef HAVE_NET_IF_H
54 #include <net/if.h>
55 #endif
56 #ifdef HAVE_SYS_IOCTL_H
57 #include <sys/ioctl.h>
58 #endif
59
60 #ifdef HAVE_NETDB_H
61 #include <netdb.h>
62 #endif
63
64 #ifdef HAVE_SYS_SOCKIO_H
65 #include <sys/sockio.h>
66 #endif
67
68 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
69 #include "inet_ntoa_r.h"
70 #endif
71
72 #ifdef VMS
73 #include <inet.h>
74 #endif
75
76 #include "memory.h"
77
78 /* The last #include file should be: */
79 #include "memdebug.h"
80
81 #define SYS_ERROR -1
82
83 char *Curl_if2ip(const char *interface, char *buf, int buf_size)
84 {
85   int dummy;
86   char *ip=NULL;
87
88   if(!interface)
89     return NULL;
90
91   dummy = socket(AF_INET, SOCK_STREAM, 0);
92   if (SYS_ERROR == dummy) {
93     return NULL;
94   }
95   else {
96     struct ifreq req;
97     size_t len = strlen(interface);
98     memset(&req, 0, sizeof(req));
99     if(len >= sizeof(req.ifr_name))
100       return NULL; /* this can't be a fine interface name */
101     memcpy(req.ifr_name, interface, len+1);
102     req.ifr_addr.sa_family = AF_INET;
103 #ifdef IOCTL_3_ARGS
104     if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req)) {
105 #else
106     if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
107 #endif
108       sclose(dummy);
109       return NULL;
110     }
111     else {
112       struct in_addr in;
113
114       struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
115       memcpy(&in, &(s->sin_addr.s_addr), sizeof(in));
116 #if defined(HAVE_INET_NTOA_R)
117       ip = inet_ntoa_r(in,buf,buf_size);
118 #else
119       ip = strncpy(buf,inet_ntoa(in),buf_size);
120       ip[buf_size - 1] = 0;
121 #endif
122     }
123     sclose(dummy);
124   }
125   return ip;
126 }
127
128 /* -- end of if2ip() -- */
129 #else
130 char *Curl_if2ip(const char *interf, char *buf, int buf_size)
131 {
132     (void) interf;
133     (void) buf;
134     (void) buf_size;
135     return NULL;
136 }
137 #endif