]> git.sesse.net Git - vlc/blob - src/misc/netutils.c
* ALL: got rid of p_object->p_this which is now useless.
[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.68 2002/06/01 18:04:49 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
79 #include "network.h"
80
81 /*****************************************************************************
82  * input_channel_t: channel library data
83  *****************************************************************************
84  * Store global channel library data.
85  * The part of the code concerning the channel changing process is unstable
86  * as it depends on the VideoLAN channel server, which isn't frozen for
87  * the time being.
88  *****************************************************************************/
89 struct input_channel_s
90 {
91     int         i_channel;                         /* current channel number */
92     mtime_t     last_change;                             /* last change date */
93 };
94
95 /*****************************************************************************
96  * Local prototypes
97  *****************************************************************************/
98 static int GetMacAddress   ( vlc_object_t *, int i_fd, char *psz_mac );
99 #ifdef WIN32
100 static int GetAdapterInfo  ( int i_adapter, char *psz_string );
101 #endif
102
103 /*****************************************************************************
104  * network_ChannelCreate: initialize global channel method data
105  *****************************************************************************
106  * Initialize channel input method global data. This function should be called
107  * once before any input thread is created or any call to other
108  * input_Channel*() function is attempted.
109  *****************************************************************************/
110 int __network_ChannelCreate( vlc_object_t *p_this )
111 {
112 #if !defined( SYS_LINUX ) && !defined( WIN32 )
113     msg_Err( p_this, "VLAN-based channels are not supported "
114                      "on this architecture" );
115 #endif
116
117     /* Allocate structure */
118     p_this->p_vlc->p_channel = malloc( sizeof( input_channel_t ) );
119     if( p_this->p_vlc->p_channel == NULL )
120     {
121         msg_Err( p_this, "out of memory" );
122         return( -1 );
123     }
124
125     /* Initialize structure */
126     p_this->p_vlc->p_channel->i_channel   = 0;
127     p_this->p_vlc->p_channel->last_change = 0;
128
129     msg_Dbg( p_this, "channels initialized" );
130     return( 0 );
131 }
132
133 /*****************************************************************************
134  * network_ChannelJoin: join a channel
135  *****************************************************************************
136  * This function will try to join a channel. If the relevant interface is
137  * already on the good channel, nothing will be done. Else, and if possible
138  * (if the interface is not locked), the channel server will be contacted
139  * and a change will be requested. The function will block until the change
140  * is effective. Note that once a channel is no more used, its interface
141  * should be unlocked using input_ChannelLeave().
142  * Non 0 will be returned in case of error.
143  *****************************************************************************/
144 int __network_ChannelJoin( vlc_object_t *p_this, int i_channel )
145 {
146 #define VLCS_VERSION 13
147 #define MESSAGE_LENGTH 256
148
149     module_t *       p_network;
150     char *           psz_network = NULL;
151     network_socket_t socket_desc;
152     char psz_mess[ MESSAGE_LENGTH ];
153     char psz_mac[ 40 ];
154     int i_fd, i_port;
155     char *psz_vlcs;
156     struct timeval delay;
157     fd_set fds;
158
159     if( !config_GetInt( p_this, "network-channel" ) )
160     {
161         msg_Err( p_this, "channels disabled, to enable them, use the"
162                          " --channels option" );
163         return -1;
164     }
165
166     /* If last change is too recent, wait a while */
167 //    if( mdate() - p_this->p_vlc->p_channel->last_change
168 //            < INPUT_CHANNEL_CHANGE_DELAY )
169 //    {
170 //        msg_Warn( p_this, "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_this->p_vlc->p_channel->last_change
175 //                   + INPUT_CHANNEL_CHANGE_DELAY );
176 //    }
177
178     if( config_GetInt( p_this, "ipv4" ) )
179     {
180         psz_network = "ipv4";
181     }
182     if( config_GetInt( p_this, "ipv6" ) )
183     {
184         psz_network = "ipv6";
185     }
186
187     /* Getting information about the channel server */
188     if( !(psz_vlcs = config_GetPsz( p_this, "channel-server" )) )
189     {
190         msg_Err( p_this, "configuration variable channel-server empty" );
191         return -1;
192     }
193
194     i_port = config_GetInt( p_this, "channel-port" );
195
196     msg_Dbg( p_this, "connecting to %s:%d", psz_vlcs, i_port );
197
198     /* Prepare the network_socket_t structure */
199     socket_desc.i_type = NETWORK_UDP;
200     socket_desc.psz_bind_addr = "";
201     socket_desc.i_bind_port = 4321;
202     socket_desc.psz_server_addr = psz_vlcs;
203     socket_desc.i_server_port = i_port;
204
205     /* Find an appropriate network module */
206     p_network = module_Need( p_this, MODULE_CAPABILITY_NETWORK, psz_network,
207                              &socket_desc );
208     if( p_network == NULL )
209     {
210         return( -1 );
211     }
212     module_Unneed( p_network );
213
214     free( psz_vlcs ); /* Do we really need this ? -- Meuuh */
215     i_fd = socket_desc.i_handle;
216
217     /* Look for the interface MAC address */
218     if( GetMacAddress( p_this, i_fd, psz_mac ) )
219     {
220         msg_Err( p_this, "failed getting MAC address" );
221         close( i_fd );
222         return -1;
223     }
224
225     msg_Dbg( p_this, "MAC address is %s", psz_mac );
226
227     /* Build the message */
228     sprintf( psz_mess, "%d %u %lu %s \n", i_channel, VLCS_VERSION,
229                        (unsigned long)(mdate() / (u64)1000000),
230                        psz_mac );
231
232     /* Send the message */
233     send( i_fd, psz_mess, MESSAGE_LENGTH, 0 );
234
235     msg_Dbg( p_this, "attempting to join channel %d", i_channel );
236
237     /* We have changed channels ! (or at least, we tried) */
238     p_this->p_vlc->p_channel->last_change = mdate();
239     p_this->p_vlc->p_channel->i_channel = i_channel;
240
241     /* Wait 5 sec for an answer from the server */
242     delay.tv_sec = 5;
243     delay.tv_usec = 0;
244     FD_ZERO( &fds );
245     FD_SET( i_fd, &fds );
246
247     switch( select( i_fd + 1, &fds, NULL, NULL, &delay ) )
248     {
249         case 0:
250             msg_Err( p_this, "no answer from vlcs" );
251             close( i_fd );
252             return -1;
253
254         case -1:
255             msg_Err( p_this, "error while listening to vlcs" );
256             close( i_fd );
257             return -1;
258     }
259
260     recv( i_fd, psz_mess, MESSAGE_LENGTH, 0 );
261     psz_mess[ MESSAGE_LENGTH - 1 ] = '\0';
262
263     if( !strncasecmp( psz_mess, "E:", 2 ) )
264     {
265         msg_Err( p_this, "vlcs said '%s'", psz_mess + 2 );
266         close( i_fd );
267         return -1;
268     }
269     else if( !strncasecmp( psz_mess, "I:", 2 ) )
270     {
271         msg_Dbg( p_this, "vlcs said '%s'", psz_mess + 2 );
272     }
273     else /* We got something to play ! FIXME: not very nice */
274     {
275 #if 0
276 #   define p_item (&p_this->p_vlc->p_playlist->p_item \
277                        [ p_this->p_vlc->p_playlist->i_index + 1])
278         vlc_mutex_lock( &p_this->p_vlc->p_playlist->change_lock );
279         if( p_item )
280         {
281             free( p_item->psz_name );
282             p_item->psz_name = strdup( psz_mess );
283             /* Unlock _afterwards_ */
284             vlc_mutex_unlock( &p_this->p_vlc->p_playlist->change_lock );
285         }
286         else
287         {
288             /* Unlock _before_ */
289             vlc_mutex_unlock( &p_this->p_vlc->p_playlist->change_lock );
290             intf_PlaylistAdd( p_this->p_vlc->p_playlist, 0, psz_mess );
291         }
292 #endif
293     }
294
295     /* Close the socket and return nicely */
296 #ifndef WIN32
297     close( i_fd );
298 #else
299     closesocket( i_fd );
300 #endif
301
302     return 0;
303 }
304
305 /* Following functions are local */
306
307 /*****************************************************************************
308  * GetMacAddress: extract the MAC Address
309  *****************************************************************************/
310 static int GetMacAddress( vlc_object_t *p_this, int i_fd, char *psz_mac )
311 {
312 #if defined( SYS_LINUX )
313     struct ifreq interface;
314     int i_ret;
315     char *psz_interface;
316
317     /*
318      * Looking for information about the eth0 interface
319      */
320     interface.ifr_addr.sa_family = AF_INET;
321     if( !(psz_interface = config_GetPsz( p_this, "iface" )) )
322     {
323         msg_Err( p_this, "configuration variable iface empty" );
324         return -1;
325     }
326     strcpy( interface.ifr_name, psz_interface );
327     free( psz_interface );
328
329     i_ret = ioctl( i_fd, SIOCGIFHWADDR, &interface );
330
331     if( i_ret )
332     {
333         msg_Err( p_this, "ioctl SIOCGIFHWADDR failed" );
334         return( i_ret );
335     }
336
337     sprintf( psz_mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
338                       interface.ifr_hwaddr.sa_data[0] & 0xff,
339                       interface.ifr_hwaddr.sa_data[1] & 0xff,
340                       interface.ifr_hwaddr.sa_data[2] & 0xff,
341                       interface.ifr_hwaddr.sa_data[3] & 0xff,
342                       interface.ifr_hwaddr.sa_data[4] & 0xff,
343                       interface.ifr_hwaddr.sa_data[5] & 0xff );
344
345     return( 0 );
346
347 #elif defined( WIN32 )
348     int i, i_ret = -1;
349
350     /* Get adapter list - support for more than one adapter */
351     LANA_ENUM AdapterList;
352     NCB       Ncb;
353
354     msg_Dbg( p_this, "looking for MAC address" );
355
356     memset( &Ncb, 0, sizeof( NCB ) );
357     Ncb.ncb_command = NCBENUM;
358     Ncb.ncb_buffer = (unsigned char *)&AdapterList;
359     Ncb.ncb_length = sizeof( AdapterList );
360     Netbios( &Ncb );
361
362     /* Get all of the local ethernet addresses */
363     for ( i = 0; i < AdapterList.length ; ++i )
364     {
365         if ( GetAdapterInfo ( AdapterList.lana[ i ], psz_mac ) == 0 )
366         {
367             i_ret = 0;
368         }
369     }
370
371     return( i_ret );
372
373 #else
374     strcpy( psz_mac, "00:00:00:00:00:00" );
375     return( 0 );
376
377 #endif
378 }
379
380 #ifdef WIN32
381 /*****************************************************************************
382  * GetAdapterInfo : gets some informations about the interface using NETBIOS
383  *****************************************************************************/
384 static int GetAdapterInfo( int i_adapter, char *psz_string )
385 {
386     struct ASTAT
387     {
388         ADAPTER_STATUS adapt;
389         NAME_BUFFER    psz_name[30];
390     } Adapter;
391
392     /* Reset the LAN adapter so that we can begin querying it */
393     NCB Ncb;
394     memset( &Ncb, 0, sizeof ( Ncb ) );
395     Ncb.ncb_command  = NCBRESET;
396     Ncb.ncb_lana_num = i_adapter;
397
398     if( Netbios( &Ncb ) != NRC_GOODRET )
399     {
400 //X        intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode );
401         return -1;
402     }
403
404     /* Prepare to get the adapter status block */
405     memset( &Ncb, 0, sizeof( Ncb ) ) ;     /* Initialization */
406     Ncb.ncb_command = NCBASTAT;
407     Ncb.ncb_lana_num = i_adapter;
408
409     strcpy( (char *)Ncb.ncb_callname, "*" );
410
411     memset( &Adapter, 0, sizeof ( Adapter ) );
412     Ncb.ncb_buffer = ( unsigned char * ) &Adapter;
413     Ncb.ncb_length = sizeof ( Adapter );
414
415     /* Get the adapter's info and, if this works, return it in standard,
416      * colon-delimited form. */
417     if ( Netbios( &Ncb ) == 0 )
418     {
419         sprintf ( psz_string, "%02X:%02X:%02X:%02X:%02X:%02X",
420                 (int) ( Adapter.adapt.adapter_address[0] ),
421                 (int) ( Adapter.adapt.adapter_address[1] ),
422                 (int) ( Adapter.adapt.adapter_address[2] ),
423                 (int) ( Adapter.adapt.adapter_address[3] ),
424                 (int) ( Adapter.adapt.adapter_address[4] ),
425                 (int) ( Adapter.adapt.adapter_address[5] ) );
426
427 //X        intf_WarnMsg( 2, "network: found MAC address %s", psz_string );
428
429         return 0;
430     }
431     else
432     {
433 //X        intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode );
434         return -1;
435     }
436 }
437 #endif /* WIN32 */
438