Initial revision
[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_SOCKIO_H
70 #include <sys/sockio.h>
71 #endif
72
73 #define SYS_ERROR -1
74
75 char *if2ip(char *interface)
76 {
77   int dummy;
78   char *ip=NULL;
79   
80   if(!interface)
81     return NULL;
82
83   dummy = socket(AF_INET, SOCK_STREAM, 0);
84   if (SYS_ERROR == dummy) {
85     return NULL;
86   }
87   else {
88     struct ifreq req;
89     memset(&req, 0, sizeof(req));
90     strcpy(req.ifr_name, interface);
91     req.ifr_addr.sa_family = AF_INET;
92     if (SYS_ERROR == ioctl(dummy, SIOCGIFADDR, &req, sizeof(req))) {
93       return NULL;
94     }
95     else {
96       struct in_addr in;
97
98       struct sockaddr_in *s = (struct sockaddr_in *)&req.ifr_dstaddr;
99       memcpy(&in, &(s->sin_addr.s_addr), sizeof(in));
100       ip = (char *)strdup(inet_ntoa(in));
101     }
102     close(dummy);
103   }
104   return ip;
105 }
106
107 /* -- end of if2ip() -- */
108 #else
109 #define if2ip(x) NULL
110 #endif