Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / lib / if2ip.c
blob0cb263e845aa9160ef23fc8d9ba449b937e99282
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: if2ip.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
24 #include "setup.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
34 #include "if2ip.h"
37 * This test can probably be simplified to #if defined(SIOCGIFADDR) and
38 * moved after the following includes.
40 #if !defined(WIN32) && !defined(__BEOS__) && !defined(__CYGWIN__) && \
41 !defined(__riscos__) && !defined(__INTERIX) && !defined(NETWARE) && \
42 !defined(__AMIGA__) && !defined(__minix) && !defined(__SYMBIAN32__) && \
43 !defined(__WATCOMC__)
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
47 #endif
48 #ifdef HAVE_NETINET_IN_H
49 #include <netinet/in.h>
50 #endif
51 #ifdef HAVE_ARPA_INET_H
52 #include <arpa/inet.h>
53 #endif
55 #ifdef HAVE_SYS_TIME_H
56 /* This must be before net/if.h for AIX 3.2 to enjoy life */
57 #include <sys/time.h>
58 #endif
59 #ifdef HAVE_NET_IF_H
60 #include <net/if.h>
61 #endif
62 #ifdef HAVE_SYS_IOCTL_H
63 #include <sys/ioctl.h>
64 #endif
66 #ifdef HAVE_NETDB_H
67 #include <netdb.h>
68 #endif
70 #ifdef HAVE_SYS_SOCKIO_H
71 #include <sys/sockio.h>
72 #endif
74 #ifdef VMS
75 #include <inet.h>
76 #endif
78 #include "inet_ntop.h"
79 #include "memory.h"
81 /* The last #include file should be: */
82 #include "memdebug.h"
84 #define SYS_ERROR -1
86 char *Curl_if2ip(const char *interface, char *buf, int buf_size)
88 int dummy;
89 char *ip=NULL;
91 if(!interface)
92 return NULL;
94 dummy = socket(AF_INET, SOCK_STREAM, 0);
95 if(SYS_ERROR == dummy) {
96 return NULL;
98 else {
99 struct ifreq req;
100 size_t len = strlen(interface);
101 memset(&req, 0, sizeof(req));
102 if(len >= sizeof(req.ifr_name)) {
103 sclose(dummy);
104 return NULL; /* this can't be a fine interface name */
106 memcpy(req.ifr_name, interface, len+1);
107 req.ifr_addr.sa_family = AF_INET;
108 #ifdef IOCTL_3_ARGS
109 if(SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req)) {
110 #else
111 if(SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
112 #endif
113 sclose(dummy);
114 return NULL;
116 else {
117 struct in_addr in;
119 struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
120 memcpy(&in, &s->sin_addr, sizeof(in));
121 ip = (char *) Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
123 sclose(dummy);
125 return ip;
128 /* -- end of if2ip() -- */
129 #else
130 char *Curl_if2ip(const char *interf, char *buf, int buf_size)
132 (void) interf;
133 (void) buf;
134 (void) buf_size;
135 return NULL;
137 #endif