]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/curl/lib/if2ip.c
hello world
[icculus/iodoom3.git] / neo / curl / lib / if2ip.c
1 /***************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2004, 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: if2ip.c,v 1.32 2004/03/17 12:46:46 bagder Exp $
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 #if !defined(WIN32) && !defined(__BEOS__) && !defined(__CYGWIN32__) && \
35     !defined(__riscos__) && !defined(__INTERIX) && !defined(NETWARE)
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 #ifdef HAVE_SYS_IOCTL_H
55 #include <sys/ioctl.h>
56 #endif
57
58 /* -- if2ip() -- */
59 #ifdef HAVE_NETDB_H
60 #include <netdb.h>
61 #endif
62
63 #ifdef HAVE_SYS_SOCKIO_H
64 #include <sys/sockio.h>
65 #endif
66
67 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL) 
68 #include "inet_ntoa_r.h"
69 #endif
70
71 #ifdef  VMS
72 #include <inet.h>
73 #endif
74
75 #include "if2ip.h"
76
77 /* The last #include file should be: */
78 #ifdef CURLDEBUG
79 #include "memdebug.h"
80 #endif
81
82 #define SYS_ERROR -1
83
84 char *Curl_if2ip(char *interface, char *buf, int buf_size)
85 {
86   int dummy;
87   char *ip=NULL;
88   
89   if(!interface)
90     return NULL;
91
92   dummy = socket(AF_INET, SOCK_STREAM, 0);
93   if (SYS_ERROR == dummy) {
94     return NULL;
95   }
96   else {
97     struct ifreq req;
98     memset(&req, 0, sizeof(req));
99     strcpy(req.ifr_name, interface);
100     req.ifr_addr.sa_family = AF_INET;
101 #ifdef  IOCTL_3_ARGS
102     if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req)) {
103 #else
104     if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
105 #endif
106       sclose(dummy);
107       return NULL;
108     }
109     else {
110       struct in_addr in;
111
112       struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
113       memcpy(&in, &(s->sin_addr.s_addr), sizeof(in));
114 #if defined(HAVE_INET_NTOA_R)
115       ip = inet_ntoa_r(in,buf,buf_size);
116 #else
117       ip = strncpy(buf,inet_ntoa(in),buf_size);
118       ip[buf_size - 1] = 0;
119 #endif
120     }
121     sclose(dummy);
122   }
123   return ip;
124 }
125
126 /* -- end of if2ip() -- */
127 #else
128 char *Curl_if2ip(char *interface, char *buf, int buf_size)
129 {
130     return NULL;
131 }
132 #endif