removed _REENTRANT define
[platform/upstream/curl.git] / lib / if2ip.c
1 /*****************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2000, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * In order to be useful for every potential user, curl and libcurl are
11  * dual-licensed under the MPL and the MIT/X-derivate licenses.
12  *
13  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
14  * copies of the Software, and permit persons to whom the Software is
15  * furnished to do so, under the terms of the MPL or the MIT/X-derivate
16  * licenses. You may pick one of these licenses.
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
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #if ! defined(WIN32) && ! defined(__BEOS__) && !defined(__CYGWIN32__)
36
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h>
39 #endif
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
42 #endif
43 #ifdef HAVE_ARPA_INET_H
44 #include <arpa/inet.h>
45 #endif
46
47 #ifdef HAVE_SYS_TIME_H
48 /* This must be before net/if.h for AIX 3.2 to enjoy life */
49 #include <sys/time.h>
50 #endif
51 #ifdef HAVE_NET_IF_H
52 #include <net/if.h>
53 #endif
54 #include <sys/ioctl.h>
55
56 /* -- if2ip() -- */
57 #ifdef HAVE_NETDB_H
58 #include <netdb.h>
59 #endif
60
61 #ifdef HAVE_SYS_SOCKIO_H
62 #include <sys/sockio.h>
63 #endif
64
65 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL) 
66 #include "inet_ntoa_r.h"
67 #endif
68
69 /* The last #include file should be: */
70 #ifdef MALLOCDEBUG
71 #include "memdebug.h"
72 #endif
73
74 #define SYS_ERROR -1
75
76 char *Curl_if2ip(char *interface, char *buf, int buf_size)
77 {
78   int dummy;
79   char *ip=NULL;
80   
81   if(!interface)
82     return NULL;
83
84   dummy = socket(AF_INET, SOCK_STREAM, 0);
85   if (SYS_ERROR == dummy) {
86     return NULL;
87   }
88   else {
89     struct ifreq req;
90     memset(&req, 0, sizeof(req));
91     strcpy(req.ifr_name, interface);
92     req.ifr_addr.sa_family = AF_INET;
93     if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
94       sclose(dummy);
95       return NULL;
96     }
97     else {
98       struct in_addr in;
99
100       struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
101       memcpy(&in, &(s->sin_addr.s_addr), sizeof(in));
102 #if defined(HAVE_INET_NTOA_R)
103       ip = inet_ntoa_r(in,buf,buf_size);
104 #else
105       ip = strncpy(buf,inet_ntoa(in),buf_size);
106       ip[buf_size - 1] = 0;
107 #endif
108     }
109     sclose(dummy);
110   }
111   return ip;
112 }
113
114 /* -- end of if2ip() -- */
115 #else
116 #define if2ip(x) NULL
117 #endif