David LeBlanc'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.Stenberg@haxx.nu>
28  *
29  *      http://curl.haxx.nu
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 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #ifdef HAVE_ARPA_INET_H
56 #include <arpa/inet.h>
57 #endif
58 #include <netinet/in.h>
59 #ifdef HAVE_NET_IF_H
60 #include <net/if.h>
61 #endif
62 #include <sys/ioctl.h>
63
64 /* -- if2ip() -- */
65 #ifdef HAVE_NETDB_H
66 #include <netdb.h>
67 #endif
68
69 #ifdef HAVE_SYS_TIME_H
70 #include <sys/time.h>
71 #endif
72
73 #ifdef HAVE_SYS_SOCKIO_H
74 #include <sys/sockio.h>
75 #endif
76
77 #ifdef HAVE_INET_NTOA_R
78 #include "inet_ntoa_r.h"
79 #endif
80
81 #define SYS_ERROR -1
82
83 char *if2ip(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     memset(&req, 0, sizeof(req));
98     strcpy(req.ifr_name, interface);
99     req.ifr_addr.sa_family = AF_INET;
100     if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
101       return NULL;
102     }
103     else {
104       struct in_addr in;
105
106       struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
107       memcpy(&in, &(s->sin_addr.s_addr), sizeof(in));
108 #if defined(HAVE_INET_NTOA_R)
109       ip = inet_ntoa_r(in,buf,buf_size);
110 #else
111       ip = strncpy(buf,inet_ntoa(in),buf_size);
112       ip[buf_size - 1] = 0;
113 #endif
114     }
115     close(dummy);
116   }
117   return ip;
118 }
119
120 /* -- end of if2ip() -- */
121 #else
122 #define if2ip(x) NULL
123 #endif