Updated the copyright year since changes have been this year.
[platform/upstream/curl.git] / lib / if2ip.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2005, 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 "inet_ntop.h"
73 #include "memory.h"
74
75 /* The last #include file should be: */
76 #include "memdebug.h"
77
78 #define SYS_ERROR -1
79
80 char *Curl_if2ip(const char *interface, char *buf, int buf_size)
81 {
82   int dummy;
83   char *ip=NULL;
84
85   if(!interface)
86     return NULL;
87
88   dummy = socket(AF_INET, SOCK_STREAM, 0);
89   if (SYS_ERROR == dummy) {
90     return NULL;
91   }
92   else {
93     struct ifreq req;
94     size_t len = strlen(interface);
95     memset(&req, 0, sizeof(req));
96     if(len >= sizeof(req.ifr_name))
97       return NULL; /* this can't be a fine interface name */
98     memcpy(req.ifr_name, interface, len+1);
99     req.ifr_addr.sa_family = AF_INET;
100 #ifdef IOCTL_3_ARGS
101     if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req)) {
102 #else
103     if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
104 #endif
105       sclose(dummy);
106       return NULL;
107     }
108     else {
109       struct in_addr in;
110
111       struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
112       memcpy(&in, &(s->sin_addr.s_addr), sizeof(in));
113       ip = Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
114     }
115     sclose(dummy);
116   }
117   return ip;
118 }
119
120 /* -- end of if2ip() -- */
121 #else
122 char *Curl_if2ip(const char *interf, char *buf, int buf_size)
123 {
124     (void) interf;
125     (void) buf;
126     (void) buf_size;
127     return NULL;
128 }
129 #endif