]> git.sesse.net Git - vlc/blob - src/misc/netutils.c
* IPv6 network module, courtesy of Alexis Guillard <alexis.guillard@bt.com>,
[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.59 2002/03/04 23:56:38 massiot 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@via.ecp.fr>
11  *          Christophe Massiot <massiot@via.ecp.fr>
12  *          Samuel Hocevar <sam@via.ecp.fr>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
27  *****************************************************************************/
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include <stdlib.h>                             /* free(), realloc(), atoi() */
33 #include <errno.h>                                                /* errno() */
34 #include <string.h>                                              /* memset() */
35
36 #include <videolan/vlc.h>
37
38 #ifdef HAVE_UNISTD_H
39 #   include <unistd.h>                                      /* gethostname() */
40 #elif defined( _MSC_VER ) && defined( _WIN32 )
41 #   include <io.h>
42 #endif
43
44 #if !defined( _MSC_VER )
45 #include <sys/time.h>                                        /* gettimeofday */
46 #endif
47
48 #ifdef WIN32
49 #   include <winsock2.h>
50 #else
51 #   include <netdb.h>                                         /* hostent ... */
52 #   include <sys/socket.h>                           /* BSD: struct sockaddr */
53 #   include <netinet/in.h>                            /* BSD: struct in_addr */
54 #   ifdef HAVE_ARPA_INET_H
55 #       include <arpa/inet.h>                    /* inet_ntoa(), inet_aton() */
56 #   endif
57 #endif
58
59 #ifdef SYS_LINUX
60 #include <sys/ioctl.h>                                            /* ioctl() */
61 #endif
62
63 #if defined( WIN32 )                    /* tools to get the MAC adress from  */
64 #include <windows.h>                    /* the interface under Windows       */
65 #include <stdio.h>
66 #include <nb30.h>
67 #endif
68
69 #ifdef HAVE_NET_IF_H
70 #include <net/if.h>                            /* interface (arch-dependent) */
71 #endif
72
73 #ifdef HAVE_SYS_SOCKIO_H
74 #include <sys/sockio.h>
75 #endif
76
77 #include "netutils.h"
78
79 #include "intf_playlist.h"
80 #include "network.h"
81
82 /*****************************************************************************
83  * input_channel_t: channel library data
84  *****************************************************************************
85  * Store global channel library data.
86  * The part of the code concerning the channel changing process is unstable
87  * as it depends on the VideoLAN channel server, which isn't frozen for
88  * the time being.
89  *****************************************************************************/
90 typedef struct input_channel_s
91 {
92     int         i_channel;                         /* current channel number */
93     mtime_t     last_change;                             /* last change date */
94 } input_channel_t;
95
96 /*****************************************************************************
97  * Local prototypes
98  *****************************************************************************/
99 static int GetMacAddress   ( int i_fd, char *psz_mac );
100 #ifdef WIN32
101 static int GetAdapterInfo  ( int i_adapter, char *psz_string );
102 #endif
103
104 /*****************************************************************************
105  * network_ChannelCreate: initialize global channel method data
106  *****************************************************************************
107  * Initialize channel input method global data. This function should be called
108  * once before any input thread is created or any call to other
109  * input_Channel*() function is attempted.
110  *****************************************************************************/
111 int network_ChannelCreate( void )
112 {
113 #if !defined( SYS_LINUX ) && !defined( WIN32 )
114     intf_ErrMsg( "channel warning: VLAN-based channels are not supported"
115                  " under this architecture" );
116 #endif
117
118     /* Allocate structure */
119     p_main->p_channel = malloc( sizeof( input_channel_t ) );
120     if( p_main->p_channel == NULL )
121     {
122         intf_ErrMsg( "network error: could not create channel bank" );
123         return( -1 );
124     }
125
126     /* Initialize structure */
127     p_main->p_channel->i_channel   = 0;
128     p_main->p_channel->last_change = 0;
129
130     intf_WarnMsg( 2, "network: channels initialized" );
131     return( 0 );
132 }
133
134 /*****************************************************************************
135  * network_ChannelJoin: join a channel
136  *****************************************************************************
137  * This function will try to join a channel. If the relevant interface is
138  * already on the good channel, nothing will be done. Else, and if possible
139  * (if the interface is not locked), the channel server will be contacted
140  * and a change will be requested. The function will block until the change
141  * is effective. Note that once a channel is no more used, its interface
142  * should be unlocked using input_ChannelLeave().
143  * Non 0 will be returned in case of error.
144  *****************************************************************************/
145 int network_ChannelJoin( int i_channel )
146 {
147 #define VLCS_VERSION 13
148 #define MESSAGE_LENGTH 256
149
150     struct module_s *   p_network;
151     char *              psz_network = NULL;
152     network_socket_t    socket_desc;
153     char psz_mess[ MESSAGE_LENGTH ];
154     char psz_mac[ 40 ];
155     int i_fd, i_port;
156     char *psz_vlcs;
157     struct timeval delay;
158     fd_set fds;
159
160     if( !config_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR ) )
161     {
162         intf_ErrMsg( "network: channels disabled, to enable them, use the"
163                      "--channels option" );
164         return -1;
165     }
166
167     /* If last change is too recent, wait a while */
168     if( mdate() - p_main->p_channel->last_change < INPUT_CHANNEL_CHANGE_DELAY )
169     {
170         intf_WarnMsg( 2, "network: waiting before changing channel" );
171         /* XXX Isn't this completely brain-damaged ??? -- Sam */
172         /* Yes it is. I don't think this is still justified with the new
173          * vlanserver --Meuuh */
174         mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY );
175     }
176
177     if( config_GetIntVariable( INPUT_IPV4_VAR ) )
178     {
179         psz_network = "ipv4";
180     }
181     if( config_GetIntVariable( INPUT_IPV6_VAR ) )
182     {
183         psz_network = "ipv6";
184     }
185
186     /* Getting information about the channel server */
187     if( !(psz_vlcs = config_GetPszVariable( INPUT_CHANNEL_SERVER_VAR )) )
188     {
189         intf_ErrMsg( "network: configuration variable %s empty",
190                      INPUT_CHANNEL_SERVER_VAR );
191         return -1;
192     }
193
194     i_port = config_GetIntVariable( INPUT_CHANNEL_PORT_VAR );
195
196     intf_WarnMsg( 5, "channel: connecting to %s:%d",
197                      psz_vlcs, i_port );
198
199     /* Prepare the network_socket_t structure */
200     socket_desc.i_type = NETWORK_UDP;
201     socket_desc.psz_bind_addr = NULL;
202     socket_desc.i_bind_port = 4321;
203     socket_desc.psz_server_addr = psz_vlcs;
204     socket_desc.i_server_port = i_port;
205
206     /* Find an appropriate network module */
207     p_network = module_Need( MODULE_CAPABILITY_NETWORK, psz_network,
208                              &socket_desc );
209     if( p_network == NULL )
210     {
211         return( -1 );
212     }
213     module_Unneed( p_network );
214
215     free( psz_vlcs ); /* Do we really need this ? -- Meuuh */
216     i_fd = socket_desc.i_handle;
217
218     /* Look for the interface MAC address */
219     if( GetMacAddress( i_fd, psz_mac ) )
220     {
221         intf_ErrMsg( "network error: failed getting MAC address" );
222         close( i_fd );
223         return -1;
224     }
225
226     intf_WarnMsg( 6, "network: MAC address is %s", psz_mac );
227
228     /* Build the message */
229     sprintf( psz_mess, "%d %u %lu %s \n", i_channel, VLCS_VERSION,
230                        (unsigned long)(mdate() / (u64)1000000),
231                        psz_mac );
232
233     /* Send the message */
234     send( i_fd, psz_mess, MESSAGE_LENGTH, 0 );
235
236     intf_WarnMsg( 2, "network: attempting to join channel %d", i_channel );
237
238     /* We have changed channels ! (or at least, we tried) */
239     p_main->p_channel->last_change = mdate();
240     p_main->p_channel->i_channel = i_channel;
241
242     /* Wait 5 sec for an answer from the server */
243     delay.tv_sec = 5;
244     delay.tv_usec = 0;
245     FD_ZERO( &fds );
246     FD_SET( i_fd, &fds );
247
248     switch( select( i_fd + 1, &fds, NULL, NULL, &delay ) )
249     {
250         case 0:
251             intf_ErrMsg( "network error: no answer from vlcs" );
252             close( i_fd );
253             return -1;
254             break;
255
256         case -1:
257             intf_ErrMsg( "network error: error while listening to vlcs" );
258             close( i_fd );
259             return -1;
260             break;
261     }
262
263     recv( i_fd, psz_mess, MESSAGE_LENGTH, 0 );
264     psz_mess[ MESSAGE_LENGTH - 1 ] = '\0';
265
266     if( !strncasecmp( psz_mess, "E: ", 3 ) )
267     {
268         intf_ErrMsg( "network error: vlcs said '%s'", psz_mess + 3 );
269         close( i_fd );
270         return -1;
271     }
272     else if( !strncasecmp( psz_mess, "I: ", 3 ) )
273     {
274         intf_WarnMsg( 2, "network info: vlcs said '%s'", psz_mess + 3 );
275     }
276     else /* We got something to play ! FIXME: not very nice */
277     {
278 #   define p_item \
279         (&p_main->p_playlist->p_item[ p_main->p_playlist->i_index + 1])
280         vlc_mutex_lock( &p_main->p_playlist->change_lock );
281         if( p_item )
282         {
283             free( p_item->psz_name );
284             p_item->psz_name = strdup( psz_mess );
285             /* Unlock _afterwards_ */
286             vlc_mutex_unlock( &p_main->p_playlist->change_lock );
287         }
288         else
289         {
290             /* Unlock _before_ */
291             vlc_mutex_unlock( &p_main->p_playlist->change_lock );
292             intf_PlaylistAdd( p_main->p_playlist, 0, psz_mess );
293         }
294     }
295
296     /* Close the socket and return nicely */
297     close( i_fd );
298
299     return 0;
300 }
301
302 /* Following functions are local */
303
304 /*****************************************************************************
305  * GetMacAddress: extract the MAC Address
306  *****************************************************************************/
307 static int GetMacAddress( int i_fd, char *psz_mac )
308 {
309 #if defined( SYS_LINUX )
310     struct ifreq interface;
311     int i_ret;
312     char *psz_interface;
313
314     /*
315      * Looking for information about the eth0 interface
316      */
317     interface.ifr_addr.sa_family = AF_INET;
318     if( !(psz_interface = config_GetPszVariable( INPUT_IFACE_VAR )) )
319     {
320         intf_ErrMsg( "network error: configuration variable %s empty",
321                      INPUT_IFACE_VAR );
322         return -1;
323     }
324     strcpy( interface.ifr_name, psz_interface );
325     free( psz_interface );
326
327     i_ret = ioctl( i_fd, SIOCGIFHWADDR, &interface );
328
329     if( i_ret )
330     {
331         intf_ErrMsg( "network error: ioctl SIOCGIFHWADDR failed" );
332         return( i_ret );
333     }
334
335     sprintf( psz_mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
336                       interface.ifr_hwaddr.sa_data[0] & 0xff,
337                       interface.ifr_hwaddr.sa_data[1] & 0xff,
338                       interface.ifr_hwaddr.sa_data[2] & 0xff,
339                       interface.ifr_hwaddr.sa_data[3] & 0xff,
340                       interface.ifr_hwaddr.sa_data[4] & 0xff,
341                       interface.ifr_hwaddr.sa_data[5] & 0xff );
342
343     return( 0 );
344
345 #elif defined( WIN32 )
346     int i, i_ret = -1;
347
348     /* Get adapter list - support for more than one adapter */
349     LANA_ENUM AdapterList;
350     NCB       Ncb;
351
352     intf_WarnMsg( 2, "network: looking for MAC address" );
353
354     memset( &Ncb, 0, sizeof( NCB ) );
355     Ncb.ncb_command = NCBENUM;
356     Ncb.ncb_buffer = (unsigned char *)&AdapterList;
357     Ncb.ncb_length = sizeof( AdapterList );
358     Netbios( &Ncb );
359
360     /* Get all of the local ethernet addresses */
361     for ( i = 0; i < AdapterList.length ; ++i )
362     {
363         if ( GetAdapterInfo ( AdapterList.lana[ i ], psz_mac ) == 0 )
364         {
365             i_ret = 0;
366         }
367     }
368
369     return( i_ret );
370
371 #else
372     strcpy( psz_mac, "00:00:00:00:00:00" );
373     return( 0 );
374
375 #endif
376 }
377
378 #ifdef WIN32
379 /*****************************************************************************
380  * GetAdapterInfo : gets some informations about the interface using NETBIOS
381  *****************************************************************************/
382 static int GetAdapterInfo( int i_adapter, char *psz_string )
383 {
384     struct ASTAT
385     {
386         ADAPTER_STATUS adapt;
387         NAME_BUFFER    psz_name[30];
388     } Adapter;
389
390     /* Reset the LAN adapter so that we can begin querying it */
391     NCB Ncb;
392     memset( &Ncb, 0, sizeof ( Ncb ) );
393     Ncb.ncb_command  = NCBRESET;
394     Ncb.ncb_lana_num = i_adapter;
395
396     if( Netbios( &Ncb ) != NRC_GOODRET )
397     {
398         intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode );
399         return -1;
400     }
401
402     /* Prepare to get the adapter status block */
403     memset( &Ncb, 0, sizeof( Ncb ) ) ;     /* Initialization */
404     Ncb.ncb_command = NCBASTAT;
405     Ncb.ncb_lana_num = i_adapter;
406
407     strcpy( (char *)Ncb.ncb_callname, "*" );
408
409     memset( &Adapter, 0, sizeof ( Adapter ) );
410     Ncb.ncb_buffer = ( unsigned char * ) &Adapter;
411     Ncb.ncb_length = sizeof ( Adapter );
412
413     /* Get the adapter's info and, if this works, return it in standard,
414      * colon-delimited form. */
415     if ( Netbios( &Ncb ) == 0 )
416     {
417         sprintf ( psz_string, "%02X:%02X:%02X:%02X:%02X:%02X",
418                 (int) ( Adapter.adapt.adapter_address[0] ),
419                 (int) ( Adapter.adapt.adapter_address[1] ),
420                 (int) ( Adapter.adapt.adapter_address[2] ),
421                 (int) ( Adapter.adapt.adapter_address[3] ),
422                 (int) ( Adapter.adapt.adapter_address[4] ),
423                 (int) ( Adapter.adapt.adapter_address[5] ) );
424
425         intf_WarnMsg( 2, "network: found MAC address %s", psz_string );
426
427         return 0;
428     }
429     else
430     {
431         intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode );
432         return -1;
433     }
434 }
435 #endif /* WIN32 */
436