]> git.sesse.net Git - vlc/blob - src/misc/netutils.c
03601ddea0c9bc6c366081439b39ef1a6815ef52
[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.73 2002/07/31 20:56:53 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  *
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 <vlc/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 #include "vlc_playlist.h"
79
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 struct input_channel_t
91 {
92     int         i_channel;                         /* current channel number */
93     mtime_t     last_change;                             /* last change date */
94 };
95
96 /*****************************************************************************
97  * Local prototypes
98  *****************************************************************************/
99 static int GetMacAddress   ( vlc_object_t *, 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( vlc_object_t *p_this )
112 {
113 #if !defined( SYS_LINUX ) && !defined( WIN32 )
114     msg_Err( p_this, "VLAN-based channels are not supported "
115                      "on this architecture" );
116 #endif
117
118     /* Allocate structure */
119     p_this->p_vlc->p_channel = malloc( sizeof( input_channel_t ) );
120     if( p_this->p_vlc->p_channel == NULL )
121     {
122         msg_Err( p_this, "out of memory" );
123         return( -1 );
124     }
125
126     /* Initialize structure */
127     p_this->p_vlc->p_channel->i_channel   = 0;
128     p_this->p_vlc->p_channel->last_change = 0;
129
130     msg_Dbg( p_this, "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( vlc_object_t *p_this, int i_channel )
146 {
147 #define VLCS_VERSION 13
148 #define MESSAGE_LENGTH 256
149
150     module_t *       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( p_this->p_vlc->p_channel->i_channel == i_channel )
161     {
162         return 0;
163     }
164
165     if( !config_GetInt( p_this, "network-channel" ) )
166     {
167         msg_Err( p_this, "channels disabled, to enable them, use the"
168                          " --channels option" );
169         return -1;
170     }
171
172     if( config_GetInt( p_this, "ipv4" ) )
173     {
174         psz_network = "ipv4";
175     }
176     if( config_GetInt( p_this, "ipv6" ) )
177     {
178         psz_network = "ipv6";
179     }
180
181     /* Getting information about the channel server */
182     if( !(psz_vlcs = config_GetPsz( p_this, "channel-server" )) )
183     {
184         msg_Err( p_this, "configuration variable channel-server empty" );
185         return -1;
186     }
187
188     i_port = config_GetInt( p_this, "channel-port" );
189
190     msg_Dbg( p_this, "connecting to %s:%d", psz_vlcs, i_port );
191
192     /* Prepare the network_socket_t structure */
193     socket_desc.i_type = NETWORK_UDP;
194     socket_desc.psz_bind_addr = "";
195     socket_desc.i_bind_port = 4321;
196     socket_desc.psz_server_addr = psz_vlcs;
197     socket_desc.i_server_port = i_port;
198
199     /* Find an appropriate network module */
200     p_network = module_Need( p_this, "network", psz_network/*, &socket_desc*/ );
201     if( p_network == NULL )
202     {
203         return( -1 );
204     }
205     module_Unneed( p_this, p_network );
206
207     free( psz_vlcs ); /* Do we really need this ? -- Meuuh */
208     i_fd = socket_desc.i_handle;
209
210     /* Look for the interface MAC address */
211     if( GetMacAddress( p_this, i_fd, psz_mac ) )
212     {
213         msg_Err( p_this, "failed getting MAC address" );
214         close( i_fd );
215         return -1;
216     }
217
218     msg_Dbg( p_this, "MAC address is %s", psz_mac );
219
220     /* Build the message */
221     sprintf( psz_mess, "%d %u %lu %s \n", i_channel, VLCS_VERSION,
222                        (unsigned long)(mdate() / (u64)1000000),
223                        psz_mac );
224
225     /* Send the message */
226     send( i_fd, psz_mess, MESSAGE_LENGTH, 0 );
227
228     msg_Dbg( p_this, "attempting to join channel %d", i_channel );
229
230     /* We have changed channels ! (or at least, we tried) */
231     p_this->p_vlc->p_channel->last_change = mdate();
232     p_this->p_vlc->p_channel->i_channel = i_channel;
233
234     /* Wait 5 sec for an answer from the server */
235     delay.tv_sec = 5;
236     delay.tv_usec = 0;
237     FD_ZERO( &fds );
238     FD_SET( i_fd, &fds );
239
240     switch( select( i_fd + 1, &fds, NULL, NULL, &delay ) )
241     {
242         case 0:
243             msg_Err( p_this, "no answer from vlcs" );
244             close( i_fd );
245             return -1;
246
247         case -1:
248             msg_Err( p_this, "error while listening to vlcs" );
249             close( i_fd );
250             return -1;
251     }
252
253     recv( i_fd, psz_mess, MESSAGE_LENGTH, 0 );
254     psz_mess[ MESSAGE_LENGTH - 1 ] = '\0';
255
256     if( !strncasecmp( psz_mess, "E:", 2 ) )
257     {
258         msg_Err( p_this, "vlcs said '%s'", psz_mess + 2 );
259         close( i_fd );
260         return -1;
261     }
262     else if( !strncasecmp( psz_mess, "I:", 2 ) )
263     {
264         msg_Dbg( p_this, "vlcs said '%s'", psz_mess + 2 );
265     }
266     else
267     {
268         /* We got something to play ! */
269         playlist_t *p_playlist;
270         p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
271                                               FIND_ANYWHERE );
272         if( p_playlist != NULL )
273         {
274             playlist_Add( p_playlist, psz_mess,
275                           PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
276             vlc_object_release( p_playlist );
277         }
278     }
279
280     /* Close the socket and return nicely */
281 #ifndef WIN32
282     close( i_fd );
283 #else
284     closesocket( i_fd );
285 #endif
286
287     return 0;
288 }
289
290 /* Following functions are local */
291
292 /*****************************************************************************
293  * GetMacAddress: extract the MAC Address
294  *****************************************************************************/
295 static int GetMacAddress( vlc_object_t *p_this, int i_fd, char *psz_mac )
296 {
297 #if defined( SYS_LINUX )
298     struct ifreq interface;
299     int i_ret;
300     char *psz_interface;
301
302     /*
303      * Looking for information about the eth0 interface
304      */
305     interface.ifr_addr.sa_family = AF_INET;
306     if( !(psz_interface = config_GetPsz( p_this, "iface" )) )
307     {
308         msg_Err( p_this, "configuration variable iface empty" );
309         return -1;
310     }
311     strcpy( interface.ifr_name, psz_interface );
312     free( psz_interface );
313
314     i_ret = ioctl( i_fd, SIOCGIFHWADDR, &interface );
315
316     if( i_ret )
317     {
318         msg_Err( p_this, "ioctl SIOCGIFHWADDR failed" );
319         return( i_ret );
320     }
321
322     sprintf( psz_mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
323                       interface.ifr_hwaddr.sa_data[0] & 0xff,
324                       interface.ifr_hwaddr.sa_data[1] & 0xff,
325                       interface.ifr_hwaddr.sa_data[2] & 0xff,
326                       interface.ifr_hwaddr.sa_data[3] & 0xff,
327                       interface.ifr_hwaddr.sa_data[4] & 0xff,
328                       interface.ifr_hwaddr.sa_data[5] & 0xff );
329
330     return( 0 );
331
332 #elif defined( WIN32 )
333     int i, i_ret = -1;
334
335     /* Get adapter list - support for more than one adapter */
336     LANA_ENUM AdapterList;
337     NCB       Ncb;
338
339     msg_Dbg( p_this, "looking for MAC address" );
340
341     memset( &Ncb, 0, sizeof( NCB ) );
342     Ncb.ncb_command = NCBENUM;
343     Ncb.ncb_buffer = (unsigned char *)&AdapterList;
344     Ncb.ncb_length = sizeof( AdapterList );
345     Netbios( &Ncb );
346
347     /* Get all of the local ethernet addresses */
348     for ( i = 0; i < AdapterList.length ; ++i )
349     {
350         if ( GetAdapterInfo ( AdapterList.lana[ i ], psz_mac ) == 0 )
351         {
352             i_ret = 0;
353         }
354     }
355
356     return( i_ret );
357
358 #else
359     strcpy( psz_mac, "00:00:00:00:00:00" );
360     return( 0 );
361
362 #endif
363 }
364
365 #ifdef WIN32
366 /*****************************************************************************
367  * GetAdapterInfo : gets some informations about the interface using NETBIOS
368  *****************************************************************************/
369 static int GetAdapterInfo( int i_adapter, char *psz_string )
370 {
371     struct ASTAT
372     {
373         ADAPTER_STATUS adapt;
374         NAME_BUFFER    psz_name[30];
375     } Adapter;
376
377     /* Reset the LAN adapter so that we can begin querying it */
378     NCB Ncb;
379     memset( &Ncb, 0, sizeof ( Ncb ) );
380     Ncb.ncb_command  = NCBRESET;
381     Ncb.ncb_lana_num = i_adapter;
382
383     if( Netbios( &Ncb ) != NRC_GOODRET )
384     {
385 //X        intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode );
386         return -1;
387     }
388
389     /* Prepare to get the adapter status block */
390     memset( &Ncb, 0, sizeof( Ncb ) ) ;     /* Initialization */
391     Ncb.ncb_command = NCBASTAT;
392     Ncb.ncb_lana_num = i_adapter;
393
394     strcpy( (char *)Ncb.ncb_callname, "*" );
395
396     memset( &Adapter, 0, sizeof ( Adapter ) );
397     Ncb.ncb_buffer = ( unsigned char * ) &Adapter;
398     Ncb.ncb_length = sizeof ( Adapter );
399
400     /* Get the adapter's info and, if this works, return it in standard,
401      * colon-delimited form. */
402     if ( Netbios( &Ncb ) == 0 )
403     {
404         sprintf ( psz_string, "%02X:%02X:%02X:%02X:%02X:%02X",
405                 (int) ( Adapter.adapt.adapter_address[0] ),
406                 (int) ( Adapter.adapt.adapter_address[1] ),
407                 (int) ( Adapter.adapt.adapter_address[2] ),
408                 (int) ( Adapter.adapt.adapter_address[3] ),
409                 (int) ( Adapter.adapt.adapter_address[4] ),
410                 (int) ( Adapter.adapt.adapter_address[5] ) );
411
412 //X        intf_WarnMsg( 2, "network: found MAC address %s", psz_string );
413
414         return 0;
415     }
416     else
417     {
418 //X        intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode );
419         return -1;
420     }
421 }
422 #endif /* WIN32 */
423