]> git.sesse.net Git - vlc/blob - src/misc/netutils.c
Encore un commit venu tout droit des abysses de l'enfer, d�sol� pour
[vlc] / src / misc / netutils.c
1 /*****************************************************************************
2  * netutils.c: various network functions
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <netdb.h>                                        /* gethostbyname() */
28 #include <stdlib.h>                             /* free(), realloc(), atoi() */
29 #include <errno.h>                                                /* errno() */
30 #include <string.h>                                      /* bzero(), bcopy() */
31
32 #ifdef SYS_BSD
33 #include <netinet/in.h>                                    /* struct in_addr */
34 #include <sys/socket.h>                                   /* struct sockaddr */
35 #endif
36
37 #include <arpa/inet.h>                                   /* htons(), htonl() */
38
39 #ifdef SYS_LINUX
40 #include <sys/ioctl.h>                                            /* ioctl() */
41 #include <net/if.h>                            /* interface (arch-dependent) */
42 #endif
43
44 #include "config.h"
45 #include "common.h"
46 #include "mtime.h"
47
48 #include "intf_msg.h"
49 #include "debug.h"
50
51 #include "netutils.h"
52
53 /*****************************************************************************
54  * BuildInetAddr: build an Internet address descriptor
55  *****************************************************************************
56  * Build an internet socket descriptor from a host name, or an ip, and a port.
57  * If the address is NULL, then INADDR_ANY will be used, allowing to receive
58  * on any address for a local socket. Usually, in this case, 'port' is also null
59  * and the function always succeeds.
60  *****************************************************************************/
61 int BuildInetAddr( struct sockaddr_in *p_sa_in, char *psz_in_addr, int i_port )
62 {
63     struct hostent *p_hostent;                            /* host descriptor */
64
65     bzero( p_sa_in, sizeof( struct sockaddr_in ) );
66     p_sa_in->sin_family = AF_INET;                                 /* family */
67     p_sa_in->sin_port = htons( i_port );                             /* port */
68
69     /* Use INADDR_ANY if psz_in_addr is NULL */
70     if( psz_in_addr == NULL )
71     {
72         p_sa_in->sin_addr.s_addr = htonl(INADDR_ANY);
73     }
74     /* Try to convert address directly from in_addr - this will work if
75      * psz_in_addr is dotted decimal. */
76     else if( !inet_aton( psz_in_addr, &p_sa_in->sin_addr) )
77     {
78         /* The convertion failed: the address is an host name, which needs
79          * to be resolved */
80         intf_DbgMsg("debug: resolving internet address %s...\n", psz_in_addr);
81         if ( (p_hostent = gethostbyname(psz_in_addr)) == NULL)
82         {
83             intf_ErrMsg("error: unknown host %s\n", psz_in_addr);
84             return( -1 );
85         }
86
87         /* Copy the first address of the host in the socket address */
88         bcopy( p_hostent->h_addr_list[0], &p_sa_in->sin_addr, p_hostent->h_length);
89     }
90     return( 0 );
91 }
92
93
94 /*****************************************************************************
95  * ServerPort: extract port from a "server:port" adress
96  *****************************************************************************
97  * Returns the port number in a "server:port" address and replace the ":" by
98  * a NUL character, or returns -1.
99  *****************************************************************************/
100 int ServerPort( char *psz_addr )
101 {
102     char *psz_index;
103
104     /* Scan string for ':' */
105     for( psz_index = psz_addr; *psz_index && (*psz_index != ':'); psz_index++ )
106     {
107         ;
108     }
109
110     /* If a port number has been found, convert it and return it */
111     if( *psz_index == ':' )
112     {
113         *psz_index = '\0';
114         return( atoi( psz_index + 1 ) );
115     }
116
117     return( - 1 );
118 }
119
120
121 /*****************************************************************************
122  * ReadIfConf: Read the configuration of an interface
123  *****************************************************************************
124  * i_sockfd must reference a socket open as follow: AF_INET, DOCK_DGRAM, 0
125  *****************************************************************************/
126 int ReadIfConf(int i_sockfd, if_descr_t* p_ifdescr, char* psz_name)
127 {
128     int i_rc = 0;
129 #ifdef SYS_LINUX
130     struct ifreq ifr_config;
131
132     ASSERT(p_ifdescr);
133     ASSERT(psz_name);
134
135     /* Which interface are we interested in ? */
136     strcpy(ifr_config.ifr_name, psz_name);
137
138     /* Read the flags for that interface */
139     i_rc = ioctl(i_sockfd, SIOCGIFFLAGS, (byte_t *)&ifr_config);
140     if( !i_rc )
141     {
142         p_ifdescr->i_flags = ifr_config.ifr_flags;
143         intf_DbgMsg("%s flags: 0x%x\n", psz_name, p_ifdescr->i_flags);
144     }
145     else
146     {
147         intf_ErrMsg("Cannot read flags for interface %s: %s\n", psz_name,
148                     strerror(errno));
149         return -1;
150     }
151
152    /* Read physical address of the interface and store it in our description */
153     i_rc = ioctl(i_sockfd, SIOCGIFHWADDR, (byte_t *)&ifr_config);
154     if( !i_rc )
155     {
156         memcpy(&p_ifdescr->sa_phys_addr, &ifr_config.ifr_addr, sizeof(struct sockaddr));
157         intf_DbgMsg("%s MAC address: %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n", psz_name,
158                     p_ifdescr->sa_phys_addr.sa_data[0]&0xff,
159                     p_ifdescr->sa_phys_addr.sa_data[1]&0xff,
160                     p_ifdescr->sa_phys_addr.sa_data[2]&0xff,
161                     p_ifdescr->sa_phys_addr.sa_data[3]&0xff,
162                     p_ifdescr->sa_phys_addr.sa_data[4]&0xff,
163                     p_ifdescr->sa_phys_addr.sa_data[5]&0xff);
164     }
165     else
166     {
167         intf_ErrMsg("Cannot read hardware address for interface %s: %s\n",
168                     psz_name, strerror(errno));
169         return -1;
170     }
171
172     /* Read IP address of the interface and store it in our description */
173     i_rc = ioctl(i_sockfd, SIOCGIFADDR, (byte_t *)&ifr_config);
174     if( !i_rc )
175     {
176         memcpy(&p_ifdescr->sa_net_addr, &ifr_config.ifr_addr, sizeof(struct sockaddr));
177         intf_DbgMsg("%s IP address: %s\n", psz_name,
178                     inet_ntoa(p_ifdescr->sa_net_addr.sin_addr));
179     }
180     else
181     {
182         intf_ErrMsg("Cannot read network address for interface %s: %s\n",
183                     psz_name, strerror(errno));
184         return -1;
185     }
186
187   /* Read broadcast address of the interface and store it in our description */
188     if(p_ifdescr->i_flags & IFF_POINTOPOINT)
189     {
190         intf_DbgMsg("%s doen't not support broadcast\n", psz_name);
191         i_rc = ioctl(i_sockfd, SIOCGIFDSTADDR, (byte_t *)&ifr_config);
192     }
193     else
194     {
195         intf_DbgMsg("%s supports broadcast\n", psz_name);
196         i_rc = ioctl(i_sockfd, SIOCGIFBRDADDR, (byte_t *)&ifr_config);
197     }
198     if( !i_rc )
199     {
200         memcpy(&p_ifdescr->sa_bcast_addr, &ifr_config.ifr_addr, sizeof(struct sockaddr));
201         intf_DbgMsg("%s broadcast address: %s\n", psz_name,
202                     inet_ntoa(p_ifdescr->sa_bcast_addr.sin_addr));
203     }
204     else
205     {
206         intf_ErrMsg("Cannot read broadcast address for interface %s: %s\n",
207                     psz_name, strerror(errno));
208         return -1;
209     }
210 #endif /* SYS_LINUX */
211
212     return i_rc;
213 }
214
215
216
217 /*****************************************************************************
218  * ReadNetConf: Retrieve the network configuration of the host
219  *****************************************************************************
220  * Only IP interfaces are listed, and only if they are up
221  * i_sockfd must reference a socket open as follow: AF_INET, DOCK_DGRAM, 0
222  *****************************************************************************/
223 int ReadNetConf(int i_sockfd, net_descr_t* p_net_descr)
224 {
225 #ifdef SYS_LINUX
226     struct ifreq* a_ifr_ifconf = NULL;
227     struct ifreq* p_ifr_current_if;
228     struct ifconf ifc_netconf;
229
230     int i_if_number;
231     int i_remaining;
232 #endif /* SYS_LINUX */
233     int i_rc = 0;
234
235 #ifdef SYS_LINUX
236     ASSERT(p_net_descr);
237
238     /* Start by assuming we have few than 3 interfaces (i_if_number will
239        be incremented by 1 when entering the loop) */
240     i_if_number = 2;
241
242     /* Retrieve network configuration for that host */
243     do
244     {
245         i_if_number++;
246         a_ifr_ifconf = realloc(a_ifr_ifconf, i_if_number*sizeof(struct ifreq));
247         ifc_netconf.ifc_len = i_if_number*sizeof(struct ifreq);
248         ifc_netconf.ifc_req = a_ifr_ifconf;
249
250         i_rc = ioctl(i_sockfd, SIOCGIFCONF, (byte_t*)&ifc_netconf);
251         if( i_rc )
252         {
253             intf_ErrMsg("Cannot read network configuration: %s\n",
254                         strerror(errno));
255             break;
256         }
257     }
258     /* If we detected ifc_len interfaces, this may mean that others have
259        been missed because the a_ifr_ifconf was to little, so increase
260        it's size and retry */
261     while( ifc_netconf.ifc_len >= i_if_number*sizeof(struct ifreq) );
262
263     /* No see what we detected */
264     if( !i_rc )
265     {
266         /* Init the given net_descr_t struct */
267         p_net_descr->i_if_number = 0;
268         p_net_descr->a_if = NULL;
269
270         /* Iterate through the entries of the a_ifr_ifconf table */
271         p_ifr_current_if = ifc_netconf.ifc_req;
272         for( i_remaining = ifc_netconf.ifc_len / sizeof (struct ifreq);
273              i_remaining-- > 0; p_ifr_current_if++ )
274         {
275             intf_DbgMsg("Found interface %s\n", p_ifr_current_if->ifr_name);
276
277             /* Don't use an interface devoted to an address family other than IP */
278             if(p_ifr_current_if->ifr_addr.sa_family != AF_INET)
279                 continue;
280
281             /* Read the status of this interface */
282             if( ioctl(i_sockfd, SIOCGIFFLAGS, (byte_t *)p_ifr_current_if) < 0 )
283             {
284                 intf_ErrMsg("Cannot access interface %s: %s\n",
285                             p_ifr_current_if->ifr_name, strerror(errno));
286                 i_rc = -1;
287                 break;
288             }
289             else
290             {
291                 /* Skip this interface if it is not up or if this is a loopback one */
292                 if( !p_ifr_current_if->ifr_flags & IFF_UP ||
293                     p_ifr_current_if->ifr_flags & IFF_LOOPBACK )
294                   continue;
295
296                 /* Add an entry to the net_descr struct to store the description of
297                    that interface */
298                 p_net_descr->i_if_number++;
299                 p_net_descr->a_if = realloc(p_net_descr->a_if,
300                                             p_net_descr->i_if_number*sizeof(if_descr_t));
301                 /* FIXME: Read the info ?? */
302                 i_rc = ReadIfConf(i_sockfd, &p_net_descr->a_if[p_net_descr->i_if_number-1],
303                                   p_ifr_current_if->ifr_name);
304             }
305         }
306     }
307
308     /* Don't need the a_ifr_ifconf anymore */
309     free( a_ifr_ifconf );
310 #endif /* SYS_LINUX */
311
312     return i_rc;
313 }
314
315