Albert Chin-A-Young's fixes
[platform/upstream/curl.git] / lib / if2ip.c
1 /*****************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  *  The contents of this file are subject to the Mozilla Public License
9  *  Version 1.0 (the "License"); you may not use this file except in
10  *  compliance with the License. You may obtain a copy of the License at
11  *  http://www.mozilla.org/MPL/
12  *
13  *  Software distributed under the License is distributed on an "AS IS"
14  *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15  *  License for the specific language governing rights and limitations
16  *  under the License.
17  *
18  *  The Original Code is Curl.
19  *
20  *  The Initial Developer of the Original Code is Daniel Stenberg.
21  *
22  *  Portions created by the Initial Developer are Copyright (C) 1998.
23  *  All Rights Reserved.
24  *
25  * ------------------------------------------------------------
26  * Main author:
27  * - Daniel Stenberg <daniel@haxx.se>
28  *
29  *      http://curl.haxx.se
30  *
31  * $Source$
32  * $Revision$
33  * $Date$
34  * $Author$
35  * $State$
36  * $Locker$
37  *
38  * ------------------------------------------------------------
39  ****************************************************************************/
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include "setup.h"
46
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50
51 #if ! defined(WIN32) && ! defined(__BEOS__)
52
53 #ifdef NEED_REENTRANT
54 #define _REENTRANT
55 #endif
56
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
59 #endif
60 #ifdef HAVE_NETINET_IN_H
61 #include <netinet/in.h>
62 #endif
63 #ifdef HAVE_ARPA_INET_H
64 #include <arpa/inet.h>
65 #endif
66
67 #ifdef HAVE_SYS_TIME_H
68 /* This must be before net/if.h for AIX 3.2 to enjoy life */
69 #include <sys/time.h>
70 #endif
71 #ifdef HAVE_NET_IF_H
72 #include <net/if.h>
73 #endif
74 #include <sys/ioctl.h>
75
76 /* -- if2ip() -- */
77 #ifdef HAVE_NETDB_H
78 #include <netdb.h>
79 #endif
80
81 #ifdef HAVE_SYS_SOCKIO_H
82 #include <sys/sockio.h>
83 #endif
84
85 #ifndef HAVE_INET_NTOA_R_DECL
86 #include "inet_ntoa_r.h"
87 #endif
88
89 #define SYS_ERROR -1
90
91 char *if2ip(char *interface, char *buf, int buf_size)
92 {
93   int dummy;
94   char *ip=NULL;
95   
96   if(!interface)
97     return NULL;
98
99   dummy = socket(AF_INET, SOCK_STREAM, 0);
100   if (SYS_ERROR == dummy) {
101     return NULL;
102   }
103   else {
104     struct ifreq req;
105     memset(&req, 0, sizeof(req));
106     strcpy(req.ifr_name, interface);
107     req.ifr_addr.sa_family = AF_INET;
108     if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
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     close(dummy);
124   }
125   return ip;
126 }
127
128 /* -- end of if2ip() -- */
129 #else
130 #define if2ip(x) NULL
131 #endif