1 /*****************************************************************************
2 * netutils.c: various network functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: netutils.c,v 1.80 2003/03/03 14:21:08 gbazin Exp $
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>
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.
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.
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 *****************************************************************************/
30 /*****************************************************************************
32 *****************************************************************************/
33 #include <stdlib.h> /* free(), realloc(), atoi() */
34 #include <string.h> /* memset() */
39 # include <errno.h> /* errno() */
43 # include <unistd.h> /* gethostname() */
44 #elif defined( WIN32 ) && !defined( UNDER_CE )
48 #if !defined( _MSC_VER )
49 #include <sys/time.h> /* gettimeofday */
52 #if defined( UNDER_CE )
53 /* No real network support (FIXME: use winsock1?) */
54 #elif defined( WIN32 )
55 # include <winsock2.h>
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() */
66 #include <sys/ioctl.h> /* ioctl() */
70 #include <IOKit/IOKitLib.h>
71 #include <IOKit/network/IOEthernetInterface.h>
72 #include <IOKit/network/IOEthernetController.h>
75 #if defined( WIN32 ) && !defined( UNDER_CE )
76 /* tools to get the MAC adress from the interface under Windows */
83 #include <net/if.h> /* interface (arch-dependent) */
86 #ifdef HAVE_SYS_SOCKIO_H
87 #include <sys/sockio.h>
91 #include "vlc_playlist.h"
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
102 *****************************************************************************/
103 struct input_channel_t
105 int i_channel; /* current channel number */
106 mtime_t last_change; /* last change date */
109 /*****************************************************************************
111 *****************************************************************************/
112 static int GetMacAddress ( vlc_object_t *, int i_fd, char *psz_mac );
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 );
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 )
129 #if !defined( SYS_LINUX ) && !( defined( WIN32 ) && !defined( UNDER_CE ) )
130 msg_Warn( p_this, "VLAN-based channels are not supported "
131 "on this architecture" );
134 /* Allocate structure */
135 p_this->p_vlc->p_channel = malloc( sizeof( input_channel_t ) );
136 if( p_this->p_vlc->p_channel == NULL )
138 msg_Err( p_this, "out of memory" );
142 /* Initialize structure */
143 p_this->p_vlc->p_channel->i_channel = 0;
144 p_this->p_vlc->p_channel->last_change = 0;
146 msg_Dbg( p_this, "channels initialized" );
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 )
163 #define VLCS_VERSION 13
164 #define MESSAGE_LENGTH 256
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 ];
174 struct timeval delay;
177 if( p_this->p_vlc->p_channel == NULL )
179 msg_Warn( p_this, "channels not initialized" );
183 if( p_this->p_vlc->p_channel->i_channel == i_channel )
188 if( !config_GetInt( p_this, "network-channel" ) )
190 msg_Err( p_this, "channels disabled, to enable them, use the"
191 " --channels option" );
195 if( config_GetInt( p_this, "ipv4" ) )
197 psz_network = "ipv4";
199 if( config_GetInt( p_this, "ipv6" ) )
201 psz_network = "ipv6";
204 /* Getting information about the channel server */
205 if( !(psz_vlcs = config_GetPsz( p_this, "channel-server" )) )
207 msg_Err( p_this, "configuration variable channel-server empty" );
211 i_port = config_GetInt( p_this, "channel-port" );
213 msg_Dbg( p_this, "connecting to %s:%d", psz_vlcs, i_port );
215 /* Prepare the network_socket_t structure */
216 socket_desc.i_type = NETWORK_UDP;
217 socket_desc.psz_bind_addr = "";
218 socket_desc.i_bind_port = 4321;
219 socket_desc.psz_server_addr = psz_vlcs;
220 socket_desc.i_server_port = i_port;
222 /* Find an appropriate network module */
223 p_this->p_private = (void*) &socket_desc;
224 p_network = module_Need( p_this, "network", psz_network/*, &socket_desc*/ );
225 if( p_network == NULL )
229 module_Unneed( p_this, p_network );
231 free( psz_vlcs ); /* Do we really need this ? -- Meuuh */
232 i_fd = socket_desc.i_handle;
234 /* Look for the interface MAC address */
235 if( GetMacAddress( p_this, i_fd, psz_mac ) )
237 msg_Err( p_this, "failed getting MAC address" );
242 msg_Dbg( p_this, "MAC address is %s", psz_mac );
244 /* Build the message */
245 sprintf( psz_mess, "%d %u %lu %s \n", i_channel, VLCS_VERSION,
246 (unsigned long)(mdate() / (u64)1000000),
249 /* Send the message */
250 send( i_fd, psz_mess, MESSAGE_LENGTH, 0 );
252 msg_Dbg( p_this, "attempting to join channel %d", i_channel );
254 /* We have changed channels ! (or at least, we tried) */
255 p_this->p_vlc->p_channel->last_change = mdate();
256 p_this->p_vlc->p_channel->i_channel = i_channel;
258 /* Wait 5 sec for an answer from the server */
262 FD_SET( i_fd, &fds );
264 switch( select( i_fd + 1, &fds, NULL, NULL, &delay ) )
267 msg_Err( p_this, "no answer from vlcs" );
272 msg_Err( p_this, "error while listening to vlcs" );
277 recv( i_fd, psz_mess, MESSAGE_LENGTH, 0 );
278 psz_mess[ MESSAGE_LENGTH - 1 ] = '\0';
280 if( !strncasecmp( psz_mess, "E:", 2 ) )
282 msg_Err( p_this, "vlcs said '%s'", psz_mess + 2 );
286 else if( !strncasecmp( psz_mess, "I:", 2 ) )
288 msg_Dbg( p_this, "vlcs said '%s'", psz_mess + 2 );
292 /* We got something to play ! */
293 playlist_t *p_playlist;
294 p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
296 if( p_playlist != NULL )
298 playlist_Add( p_playlist, psz_mess,
299 PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
300 vlc_object_release( p_playlist );
304 /* Close the socket and return nicely */
305 # if defined( WIN32 )
315 /* Following functions are local */
317 /*****************************************************************************
318 * GetMacAddress: extract the MAC Address
319 *****************************************************************************/
320 static int GetMacAddress( vlc_object_t *p_this, int i_fd, char *psz_mac )
322 #if defined( SYS_LINUX )
323 struct ifreq interface;
328 * Looking for information about the eth0 interface
330 interface.ifr_addr.sa_family = AF_INET;
331 if( !(psz_interface = config_GetPsz( p_this, "iface" )) )
333 msg_Err( p_this, "configuration variable iface empty" );
336 strcpy( interface.ifr_name, psz_interface );
337 free( psz_interface );
339 i_ret = ioctl( i_fd, SIOCGIFHWADDR, &interface );
343 msg_Err( p_this, "ioctl SIOCGIFHWADDR failed" );
347 sprintf( psz_mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
348 interface.ifr_hwaddr.sa_data[0] & 0xff,
349 interface.ifr_hwaddr.sa_data[1] & 0xff,
350 interface.ifr_hwaddr.sa_data[2] & 0xff,
351 interface.ifr_hwaddr.sa_data[3] & 0xff,
352 interface.ifr_hwaddr.sa_data[4] & 0xff,
353 interface.ifr_hwaddr.sa_data[5] & 0xff );
357 #elif defined( SYS_DARWIN )
359 io_object_t ctrl_service;
360 CFTypeRef cfd_mac_address;
361 UInt8 ui_mac_address[kIOEthernetAddressSize];
363 if( !(psz_interface = config_GetPsz( p_this, "iface" )) )
365 msg_Err( p_this, "configuration variable iface empty" );
369 if( GetNetIntfCtrl( psz_interface, &ctrl_service ) )
371 msg_Err( p_this, "GetNetIntfCtrl failed" );
375 cfd_mac_address = IORegistryEntryCreateCFProperty( ctrl_service,
376 CFSTR(kIOMACAddress),
379 IOObjectRelease( ctrl_service );
380 if( cfd_mac_address == NULL )
382 msg_Err( p_this, "IORegistryEntryCreateCFProperty failed" );
386 CFDataGetBytes( cfd_mac_address,
387 CFRangeMake(0, kIOEthernetAddressSize),
389 CFRelease( cfd_mac_address );
391 sprintf( psz_mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
392 ui_mac_address[0], ui_mac_address[1],
393 ui_mac_address[2], ui_mac_address[3],
394 ui_mac_address[4], ui_mac_address[5] );
398 #elif defined( WIN32 ) && !defined( UNDER_CE )
401 /* Get adapter list - support for more than one adapter */
402 LANA_ENUM AdapterList;
405 msg_Dbg( p_this, "looking for MAC address" );
407 memset( &Ncb, 0, sizeof( NCB ) );
408 Ncb.ncb_command = NCBENUM;
409 Ncb.ncb_buffer = (unsigned char *)&AdapterList;
410 Ncb.ncb_length = sizeof( AdapterList );
413 /* Get all of the local ethernet addresses */
414 for ( i = 0; i < AdapterList.length ; ++i )
416 if ( GetAdapterInfo ( AdapterList.lana[ i ], psz_mac ) == 0 )
425 strcpy( psz_mac, "00:00:00:00:00:00" );
432 /*****************************************************************************
433 * GetNetIntfCtrl : get parent controller for network interface
434 *****************************************************************************/
435 static int GetNetIntfCtrl( const char *psz_interface,
436 io_object_t *ctrl_service )
440 io_object_t intf_service;
441 io_iterator_t intf_iterator;
443 /* get port for IOKit communication */
444 if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS )
449 /* look up the IOService object for the interface */
450 ret = IOServiceGetMatchingServices( port, IOBSDNameMatching( port, 0,
451 psz_interface ), &intf_iterator );
452 if( ret != KERN_SUCCESS )
457 intf_service = IOIteratorNext( intf_iterator );
458 if( intf_service == NULL )
463 ret = IORegistryEntryGetParentEntry( intf_service,
466 IOObjectRelease( intf_service );
467 if( ret != KERN_SUCCESS )
475 #elif defined( WIN32 ) && !defined( UNDER_CE )
476 /*****************************************************************************
477 * GetAdapterInfo : gets some informations about the interface using NETBIOS
478 *****************************************************************************/
479 static int GetAdapterInfo( int i_adapter, char *psz_string )
483 ADAPTER_STATUS adapt;
484 NAME_BUFFER psz_name[30];
487 /* Reset the LAN adapter so that we can begin querying it */
489 memset( &Ncb, 0, sizeof ( Ncb ) );
490 Ncb.ncb_command = NCBRESET;
491 Ncb.ncb_lana_num = i_adapter;
493 if( Netbios( &Ncb ) != NRC_GOODRET )
495 //X intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode );
499 /* Prepare to get the adapter status block */
500 memset( &Ncb, 0, sizeof( Ncb ) ) ; /* Initialization */
501 Ncb.ncb_command = NCBASTAT;
502 Ncb.ncb_lana_num = i_adapter;
504 strcpy( (char *)Ncb.ncb_callname, "*" );
506 memset( &Adapter, 0, sizeof ( Adapter ) );
507 Ncb.ncb_buffer = ( unsigned char * ) &Adapter;
508 Ncb.ncb_length = sizeof ( Adapter );
510 /* Get the adapter's info and, if this works, return it in standard,
511 * colon-delimited form. */
512 if ( Netbios( &Ncb ) == 0 )
514 sprintf ( psz_string, "%02X:%02X:%02X:%02X:%02X:%02X",
515 (int) ( Adapter.adapt.adapter_address[0] ),
516 (int) ( Adapter.adapt.adapter_address[1] ),
517 (int) ( Adapter.adapt.adapter_address[2] ),
518 (int) ( Adapter.adapt.adapter_address[3] ),
519 (int) ( Adapter.adapt.adapter_address[4] ),
520 (int) ( Adapter.adapt.adapter_address[5] ) );
522 //X intf_WarnMsg( 2, "network: found MAC address %s", psz_string );
528 //X intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode );