]> git.sesse.net Git - vlc/blob - src/misc/netutils.c
* ./bootstrap: bootstrap now requires the --config flag. With no arguments
[vlc] / src / misc / netutils.c
1 /*****************************************************************************
2  * netutils.c: various network functions
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: netutils.c,v 1.77 2002/11/13 20:51:05 sam Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Benoit Steiner <benny@via.ecp.fr>
9  *          Henri Fallon <henri@videolan.org>
10  *          Xavier Marchesini <xav@alarue.net>
11  *          Christophe Massiot <massiot@via.ecp.fr>
12  *          Samuel Hocevar <sam@via.ecp.fr>
13  *          Jon Lech Johansen <jon-vl@nanocrew.net>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
28  *****************************************************************************/
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include <stdlib.h>                             /* free(), realloc(), atoi() */
34 #include <string.h>                                              /* memset() */
35
36 #include <vlc/vlc.h>
37
38 #ifdef HAVE_ERRNO_H
39 #   include <errno.h>                                             /* errno() */
40 #endif
41
42 #ifdef HAVE_UNISTD_H
43 #   include <unistd.h>                                      /* gethostname() */
44 #elif defined( _MSC_VER ) && defined( _WIN32 ) && !defined( UNDER_CE )
45 #   include <io.h>
46 #endif
47
48 #if !defined( _MSC_VER )
49 #include <sys/time.h>                                        /* gettimeofday */
50 #endif
51
52 #if defined( UNDER_CE )
53     /* No real network support (FIXME: use winsock1?) */
54 #elif defined( WIN32 )
55 #   include <winsock2.h>
56 #else
57 #   include <netdb.h>                                         /* hostent ... */
58 #   include <sys/socket.h>                           /* BSD: struct sockaddr */
59 #   include <netinet/in.h>                            /* BSD: struct in_addr */
60 #   ifdef HAVE_ARPA_INET_H
61 #       include <arpa/inet.h>                    /* inet_ntoa(), inet_aton() */
62 #   endif
63 #endif
64
65 #ifdef SYS_LINUX
66 #include <sys/ioctl.h>                                            /* ioctl() */
67 #endif
68
69 #ifdef SYS_DARWIN
70 #include <IOKit/IOKitLib.h>
71 #include <IOKit/network/IOEthernetInterface.h>
72 #include <IOKit/network/IOEthernetController.h>
73 #endif
74
75 #if defined( WIN32 ) && !defined( UNDER_CE )
76     /* tools to get the MAC adress from the interface under Windows */
77 #   include <windows.h>
78 #   include <stdio.h>
79 #   include <nb30.h>
80 #endif
81
82 #ifdef HAVE_NET_IF_H
83 #include <net/if.h>                            /* interface (arch-dependent) */
84 #endif
85
86 #ifdef HAVE_SYS_SOCKIO_H
87 #include <sys/sockio.h>
88 #endif
89
90 #include "netutils.h"
91 #include "vlc_playlist.h"
92
93 #include "network.h"
94
95 /*****************************************************************************
96  * input_channel_t: channel library data
97  *****************************************************************************
98  * Store global channel library data.
99  * The part of the code concerning the channel changing process is unstable
100  * as it depends on the VideoLAN channel server, which isn't frozen for
101  * the time being.
102  *****************************************************************************/
103 struct input_channel_t
104 {
105     int         i_channel;                         /* current channel number */
106     mtime_t     last_change;                             /* last change date */
107 };
108
109 /*****************************************************************************
110  * Local prototypes
111  *****************************************************************************/
112 static int GetMacAddress   ( vlc_object_t *, int i_fd, char *psz_mac );
113 #ifdef SYS_DARWIN
114 static int GetNetIntfCtrl  ( const char *psz_interface, 
115                              io_object_t *ctrl_service );
116 #elif defined( WIN32 ) && !defined( UNDER_CE )
117 static int GetAdapterInfo  ( int i_adapter, char *psz_string );
118 #endif
119
120 /*****************************************************************************
121  * network_ChannelCreate: initialize global channel method data
122  *****************************************************************************
123  * Initialize channel input method global data. This function should be called
124  * once before any input thread is created or any call to other
125  * input_Channel*() function is attempted.
126  *****************************************************************************/
127 int __network_ChannelCreate( vlc_object_t *p_this )
128 {
129 #if !defined( SYS_LINUX ) && !( defined( WIN32 ) && !defined( UNDER_CE ) )
130     msg_Err( p_this, "VLAN-based channels are not supported "
131                      "on this architecture" );
132 #endif
133
134     /* Allocate structure */
135     p_this->p_vlc->p_channel = malloc( sizeof( input_channel_t ) );
136     if( p_this->p_vlc->p_channel == NULL )
137     {
138         msg_Err( p_this, "out of memory" );
139         return( -1 );
140     }
141
142     /* Initialize structure */
143     p_this->p_vlc->p_channel->i_channel   = 0;
144     p_this->p_vlc->p_channel->last_change = 0;
145
146     msg_Dbg( p_this, "channels initialized" );
147     return( 0 );
148 }
149
150 /*****************************************************************************
151  * network_ChannelJoin: join a channel
152  *****************************************************************************
153  * This function will try to join a channel. If the relevant interface is
154  * already on the good channel, nothing will be done. Else, and if possible
155  * (if the interface is not locked), the channel server will be contacted
156  * and a change will be requested. The function will block until the change
157  * is effective. Note that once a channel is no more used, its interface
158  * should be unlocked using input_ChannelLeave().
159  * Non 0 will be returned in case of error.
160  *****************************************************************************/
161 int __network_ChannelJoin( vlc_object_t *p_this, int i_channel )
162 {
163 #define VLCS_VERSION 13
164 #define MESSAGE_LENGTH 256
165
166 #if defined( SYS_LINUX ) || ( defined( WIN32 ) && !defined( UNDER_CE ) )
167     module_t *       p_network;
168     char *           psz_network = NULL;
169     network_socket_t socket_desc;
170     char psz_mess[ MESSAGE_LENGTH ];
171     char psz_mac[ 40 ];
172     int i_fd, i_port;
173     char *psz_vlcs;
174     struct timeval delay;
175     fd_set fds;
176
177     if( p_this->p_vlc->p_channel->i_channel == i_channel )
178     {
179         return 0;
180     }
181
182     if( !config_GetInt( p_this, "network-channel" ) )
183     {
184         msg_Err( p_this, "channels disabled, to enable them, use the"
185                          " --channels option" );
186         return -1;
187     }
188
189     if( config_GetInt( p_this, "ipv4" ) )
190     {
191         psz_network = "ipv4";
192     }
193     if( config_GetInt( p_this, "ipv6" ) )
194     {
195         psz_network = "ipv6";
196     }
197
198     /* Getting information about the channel server */
199     if( !(psz_vlcs = config_GetPsz( p_this, "channel-server" )) )
200     {
201         msg_Err( p_this, "configuration variable channel-server empty" );
202         return -1;
203     }
204
205     i_port = config_GetInt( p_this, "channel-port" );
206
207     msg_Dbg( p_this, "connecting to %s:%d", psz_vlcs, i_port );
208
209     /* Prepare the network_socket_t structure */
210     socket_desc.i_type = NETWORK_UDP;
211     socket_desc.psz_bind_addr = "";
212     socket_desc.i_bind_port = 4321;
213     socket_desc.psz_server_addr = psz_vlcs;
214     socket_desc.i_server_port = i_port;
215
216     /* Find an appropriate network module */
217     p_this->p_private = (void*) &socket_desc;
218     p_network = module_Need( p_this, "network", psz_network/*, &socket_desc*/ );
219     if( p_network == NULL )
220     {
221         return( -1 );
222     }
223     module_Unneed( p_this, p_network );
224
225     free( psz_vlcs ); /* Do we really need this ? -- Meuuh */
226     i_fd = socket_desc.i_handle;
227
228     /* Look for the interface MAC address */
229     if( GetMacAddress( p_this, i_fd, psz_mac ) )
230     {
231         msg_Err( p_this, "failed getting MAC address" );
232         close( i_fd );
233         return -1;
234     }
235
236     msg_Dbg( p_this, "MAC address is %s", psz_mac );
237
238     /* Build the message */
239     sprintf( psz_mess, "%d %u %lu %s \n", i_channel, VLCS_VERSION,
240                        (unsigned long)(mdate() / (u64)1000000),
241                        psz_mac );
242
243     /* Send the message */
244     send( i_fd, psz_mess, MESSAGE_LENGTH, 0 );
245
246     msg_Dbg( p_this, "attempting to join channel %d", i_channel );
247
248     /* We have changed channels ! (or at least, we tried) */
249     p_this->p_vlc->p_channel->last_change = mdate();
250     p_this->p_vlc->p_channel->i_channel = i_channel;
251
252     /* Wait 5 sec for an answer from the server */
253     delay.tv_sec = 5;
254     delay.tv_usec = 0;
255     FD_ZERO( &fds );
256     FD_SET( i_fd, &fds );
257
258     switch( select( i_fd + 1, &fds, NULL, NULL, &delay ) )
259     {
260         case 0:
261             msg_Err( p_this, "no answer from vlcs" );
262             close( i_fd );
263             return -1;
264
265         case -1:
266             msg_Err( p_this, "error while listening to vlcs" );
267             close( i_fd );
268             return -1;
269     }
270
271     recv( i_fd, psz_mess, MESSAGE_LENGTH, 0 );
272     psz_mess[ MESSAGE_LENGTH - 1 ] = '\0';
273
274     if( !strncasecmp( psz_mess, "E:", 2 ) )
275     {
276         msg_Err( p_this, "vlcs said '%s'", psz_mess + 2 );
277         close( i_fd );
278         return -1;
279     }
280     else if( !strncasecmp( psz_mess, "I:", 2 ) )
281     {
282         msg_Dbg( p_this, "vlcs said '%s'", psz_mess + 2 );
283     }
284     else
285     {
286         /* We got something to play ! */
287         playlist_t *p_playlist;
288         p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
289                                               FIND_ANYWHERE );
290         if( p_playlist != NULL )
291         {
292             playlist_Add( p_playlist, psz_mess,
293                           PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
294             vlc_object_release( p_playlist );
295         }
296     }
297
298     /* Close the socket and return nicely */
299 #   if defined( WIN32 )
300     closesocket( i_fd );
301 #   else
302     close( i_fd );
303 #   endif
304
305 #endif
306     return 0;
307 }
308
309 /* Following functions are local */
310
311 /*****************************************************************************
312  * GetMacAddress: extract the MAC Address
313  *****************************************************************************/
314 static int GetMacAddress( vlc_object_t *p_this, int i_fd, char *psz_mac )
315 {
316 #if defined( SYS_LINUX )
317     struct ifreq interface;
318     int i_ret;
319     char *psz_interface;
320
321     /*
322      * Looking for information about the eth0 interface
323      */
324     interface.ifr_addr.sa_family = AF_INET;
325     if( !(psz_interface = config_GetPsz( p_this, "iface" )) )
326     {
327         msg_Err( p_this, "configuration variable iface empty" );
328         return -1;
329     }
330     strcpy( interface.ifr_name, psz_interface );
331     free( psz_interface );
332
333     i_ret = ioctl( i_fd, SIOCGIFHWADDR, &interface );
334
335     if( i_ret )
336     {
337         msg_Err( p_this, "ioctl SIOCGIFHWADDR failed" );
338         return( i_ret );
339     }
340
341     sprintf( psz_mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
342                       interface.ifr_hwaddr.sa_data[0] & 0xff,
343                       interface.ifr_hwaddr.sa_data[1] & 0xff,
344                       interface.ifr_hwaddr.sa_data[2] & 0xff,
345                       interface.ifr_hwaddr.sa_data[3] & 0xff,
346                       interface.ifr_hwaddr.sa_data[4] & 0xff,
347                       interface.ifr_hwaddr.sa_data[5] & 0xff );
348
349     return( 0 );
350
351 #elif defined( SYS_DARWIN )
352     char *psz_interface;
353     io_object_t ctrl_service;
354     CFTypeRef cfd_mac_address;
355     UInt8 ui_mac_address[kIOEthernetAddressSize];
356
357     if( !(psz_interface = config_GetPsz( p_this, "iface" )) )
358     {
359         msg_Err( p_this, "configuration variable iface empty" );
360         return( -1 );
361     }
362
363     if( GetNetIntfCtrl( psz_interface, &ctrl_service ) )
364     {
365         msg_Err( p_this, "GetNetIntfCtrl failed" );
366         return( -1 );
367     }
368
369     cfd_mac_address = IORegistryEntryCreateCFProperty( ctrl_service,
370                                                        CFSTR(kIOMACAddress),
371                                                        kCFAllocatorDefault,
372                                                        0 ); 
373     IOObjectRelease( ctrl_service );
374     if( cfd_mac_address == NULL )
375     {
376         msg_Err( p_this, "IORegistryEntryCreateCFProperty failed" );
377         return( -1 );
378     }
379
380     CFDataGetBytes( cfd_mac_address, 
381                     CFRangeMake(0, kIOEthernetAddressSize), 
382                     ui_mac_address );
383     CFRelease( cfd_mac_address );
384
385     sprintf( psz_mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
386                       ui_mac_address[0], ui_mac_address[1],
387                       ui_mac_address[2], ui_mac_address[3],
388                       ui_mac_address[4], ui_mac_address[5] ); 
389
390     return( 0 );
391
392 #elif defined( WIN32 ) && !defined( UNDER_CE )
393     int i, i_ret = -1;
394
395     /* Get adapter list - support for more than one adapter */
396     LANA_ENUM AdapterList;
397     NCB       Ncb;
398
399     msg_Dbg( p_this, "looking for MAC address" );
400
401     memset( &Ncb, 0, sizeof( NCB ) );
402     Ncb.ncb_command = NCBENUM;
403     Ncb.ncb_buffer = (unsigned char *)&AdapterList;
404     Ncb.ncb_length = sizeof( AdapterList );
405     Netbios( &Ncb );
406
407     /* Get all of the local ethernet addresses */
408     for ( i = 0; i < AdapterList.length ; ++i )
409     {
410         if ( GetAdapterInfo ( AdapterList.lana[ i ], psz_mac ) == 0 )
411         {
412             i_ret = 0;
413         }
414     }
415
416     return( i_ret );
417
418 #else
419     strcpy( psz_mac, "00:00:00:00:00:00" );
420     return( 0 );
421
422 #endif
423 }
424
425 #ifdef SYS_DARWIN
426 /*****************************************************************************
427  * GetNetIntfCtrl : get parent controller for network interface 
428  *****************************************************************************/
429 static int GetNetIntfCtrl( const char *psz_interface,
430                            io_object_t *ctrl_service )
431 {
432     mach_port_t port;
433     kern_return_t ret;
434     io_object_t intf_service;
435     io_iterator_t intf_iterator;
436
437     /* get port for IOKit communication */
438     if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS )
439     {
440         return( -1 );
441     }
442
443     /* look up the IOService object for the interface */
444     ret = IOServiceGetMatchingServices( port, IOBSDNameMatching( port, 0, 
445                                         psz_interface ), &intf_iterator ); 
446     if( ret != KERN_SUCCESS )
447     {
448         return( -1 );
449     }
450
451     intf_service = IOIteratorNext( intf_iterator );
452     if( intf_service == NULL )
453     {
454         return( -1 );
455     }
456
457     ret = IORegistryEntryGetParentEntry( intf_service,
458                                          kIOServicePlane,
459                                          ctrl_service );
460     IOObjectRelease( intf_service );
461     if( ret != KERN_SUCCESS )
462     {
463         return( -1 );
464     }
465
466     return( 0 );
467 }
468
469 #elif defined( WIN32 ) && !defined( UNDER_CE )
470 /*****************************************************************************
471  * GetAdapterInfo : gets some informations about the interface using NETBIOS
472  *****************************************************************************/
473 static int GetAdapterInfo( int i_adapter, char *psz_string )
474 {
475     struct ASTAT
476     {
477         ADAPTER_STATUS adapt;
478         NAME_BUFFER    psz_name[30];
479     } Adapter;
480
481     /* Reset the LAN adapter so that we can begin querying it */
482     NCB Ncb;
483     memset( &Ncb, 0, sizeof ( Ncb ) );
484     Ncb.ncb_command  = NCBRESET;
485     Ncb.ncb_lana_num = i_adapter;
486
487     if( Netbios( &Ncb ) != NRC_GOODRET )
488     {
489 //X        intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode );
490         return -1;
491     }
492
493     /* Prepare to get the adapter status block */
494     memset( &Ncb, 0, sizeof( Ncb ) ) ;     /* Initialization */
495     Ncb.ncb_command = NCBASTAT;
496     Ncb.ncb_lana_num = i_adapter;
497
498     strcpy( (char *)Ncb.ncb_callname, "*" );
499
500     memset( &Adapter, 0, sizeof ( Adapter ) );
501     Ncb.ncb_buffer = ( unsigned char * ) &Adapter;
502     Ncb.ncb_length = sizeof ( Adapter );
503
504     /* Get the adapter's info and, if this works, return it in standard,
505      * colon-delimited form. */
506     if ( Netbios( &Ncb ) == 0 )
507     {
508         sprintf ( psz_string, "%02X:%02X:%02X:%02X:%02X:%02X",
509                 (int) ( Adapter.adapt.adapter_address[0] ),
510                 (int) ( Adapter.adapt.adapter_address[1] ),
511                 (int) ( Adapter.adapt.adapter_address[2] ),
512                 (int) ( Adapter.adapt.adapter_address[3] ),
513                 (int) ( Adapter.adapt.adapter_address[4] ),
514                 (int) ( Adapter.adapt.adapter_address[5] ) );
515
516 //X        intf_WarnMsg( 2, "network: found MAC address %s", psz_string );
517
518         return 0;
519     }
520     else
521     {
522 //X        intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode );
523         return -1;
524     }
525 }
526 #endif /* WIN32 */
527