Fixed ftp support with uClibc due to differing inet_ntoa_r() behaviour.
[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 #ifdef VMS
69 #include <inet.h>
70 #endif
71
72 #include "memory.h"
73
74 /* The last #include file should be: */
75 #include "memdebug.h"
76
77 #define SYS_ERROR -1
78
79 char *Curl_if2ip(const char *interface, char *buf, int buf_size)
80 {
81   int dummy;
82   char *ip=NULL;
83
84   if(!interface)
85     return NULL;
86
87   dummy = socket(AF_INET, SOCK_STREAM, 0);
88   if (SYS_ERROR == dummy) {
89     return NULL;
90   }
91   else {
92     struct ifreq req;
93     size_t len = strlen(interface);
94     memset(&req, 0, sizeof(req));
95     if(len >= sizeof(req.ifr_name))
96       return NULL; /* this can't be a fine interface name */
97     memcpy(req.ifr_name, interface, len+1);
98     req.ifr_addr.sa_family = AF_INET;
99 #ifdef IOCTL_3_ARGS
100     if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req)) {
101 #else
102     if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
103 #endif
104       sclose(dummy);
105       return NULL;
106     }
107     else {
108       struct in_addr in;
109
110       struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
111       memcpy(&in, &(s->sin_addr.s_addr), sizeof(in));
112       ip = Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
113     }
114     sclose(dummy);
115   }
116   return ip;
117 }
118
119 /* -- end of if2ip() -- */
120 #else
121 char *Curl_if2ip(const char *interf, char *buf, int buf_size)
122 {
123     (void) interf;
124     (void) buf;
125     (void) buf_size;
126     return NULL;
127 }
128 #endif