X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fmisc%2Fnetutils.c;h=1719a8ac1003fd4801fdfae7427d39b5d1866baa;hb=1171979d73a6ddd4da292e772aa8d29d0e581996;hp=c3db66c65f6d0ce3c7a7f35226590bcf9c033cab;hpb=8cc8d9579d12da1f3a00ea9f870803d67c5c091c;p=vlc diff --git a/src/misc/netutils.c b/src/misc/netutils.c index c3db66c65f..1719a8ac10 100644 --- a/src/misc/netutils.c +++ b/src/misc/netutils.c @@ -2,14 +2,15 @@ * netutils.c: various network functions ***************************************************************************** * Copyright (C) 1999-2001 VideoLAN - * $Id: netutils.c,v 1.56 2002/02/20 23:23:53 sam Exp $ + * $Id: netutils.c,v 1.80 2003/03/03 14:21:08 gbazin Exp $ * * Authors: Vincent Seguin * Benoit Steiner * Henri Fallon - * Xavier Marchesini + * Xavier Marchesini * Christophe Massiot * Samuel Hocevar + * Jon Lech Johansen * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,18 +31,17 @@ * Preamble *****************************************************************************/ #include /* free(), realloc(), atoi() */ -#include /* errno() */ #include /* memset() */ -#include +#include -#ifdef STRNCASECMP_IN_STRINGS_H -# include +#ifdef HAVE_ERRNO_H +# include /* errno() */ #endif #ifdef HAVE_UNISTD_H # include /* gethostname() */ -#elif defined( _MSC_VER ) && defined( _WIN32 ) +#elif defined( WIN32 ) && !defined( UNDER_CE ) # include #endif @@ -49,9 +49,11 @@ #include /* gettimeofday */ #endif -#ifdef WIN32 +#if defined( UNDER_CE ) + /* No real network support (FIXME: use winsock1?) */ +#elif defined( WIN32 ) # include -#elif !defined( SYS_BEOS ) +#else # include /* hostent ... */ # include /* BSD: struct sockaddr */ # include /* BSD: struct in_addr */ @@ -64,10 +66,17 @@ #include /* ioctl() */ #endif -#if defined( WIN32 ) /* tools to get the MAC adress from */ -#include /* the interface under Windows */ -#include -#include +#ifdef SYS_DARWIN +#include +#include +#include +#endif + +#if defined( WIN32 ) && !defined( UNDER_CE ) + /* tools to get the MAC adress from the interface under Windows */ +# include +# include +# include #endif #ifdef HAVE_NET_IF_H @@ -79,8 +88,9 @@ #endif #include "netutils.h" +#include "vlc_playlist.h" -#include "intf_playlist.h" +#include "network.h" /***************************************************************************** * input_channel_t: channel library data @@ -90,67 +100,23 @@ * as it depends on the VideoLAN channel server, which isn't frozen for * the time being. *****************************************************************************/ -typedef struct input_channel_s +struct input_channel_t { int i_channel; /* current channel number */ mtime_t last_change; /* last change date */ -} input_channel_t; +}; /***************************************************************************** * Local prototypes *****************************************************************************/ -static int GetMacAddress ( int i_fd, char *psz_mac ); -#ifdef WIN32 +static int GetMacAddress ( vlc_object_t *, int i_fd, char *psz_mac ); +#ifdef SYS_DARWIN +static int GetNetIntfCtrl ( const char *psz_interface, + io_object_t *ctrl_service ); +#elif defined( WIN32 ) && !defined( UNDER_CE ) static int GetAdapterInfo ( int i_adapter, char *psz_string ); #endif -/***************************************************************************** - * network_BuildAddr : fill a sockaddr_in structure - *****************************************************************************/ -int network_BuildAddr( struct sockaddr_in * p_socket, - char * psz_address, int i_port ) -{ -#if defined( SYS_BEOS ) - intf_ErrMsg( "error: networking is not yet supported under BeOS" ); - return( 1 ); - -#else - /* Reset struct */ - memset( p_socket, 0, sizeof( struct sockaddr_in ) ); - p_socket->sin_family = AF_INET; /* family */ - p_socket->sin_port = htons( i_port ); - if( psz_address == NULL ) - { - p_socket->sin_addr.s_addr = INADDR_ANY; - } - else - { - struct hostent * p_hostent; - - /* Try to convert address directly from in_addr - this will work if - * psz_broadcast is dotted decimal. */ -#ifdef HAVE_ARPA_INET_H - if( !inet_aton( psz_address, &p_socket->sin_addr) ) -#else - if( (p_socket->sin_addr.s_addr = inet_addr( psz_address )) == -1 ) -#endif - { - /* We have a fqdn, try to find its address */ - if ( (p_hostent = gethostbyname( psz_address )) == NULL ) - { - intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_address ); - return( -1 ); - } - - /* Copy the first address of the host in the socket address */ - memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0], - p_hostent->h_length ); - } - } - return( 0 ); -#endif -} - /***************************************************************************** * network_ChannelCreate: initialize global channel method data ***************************************************************************** @@ -158,30 +124,27 @@ int network_BuildAddr( struct sockaddr_in * p_socket, * once before any input thread is created or any call to other * input_Channel*() function is attempted. *****************************************************************************/ -int network_ChannelCreate( void ) +int __network_ChannelCreate( vlc_object_t *p_this ) { -#if defined( SYS_LINUX ) || defined( WIN32 ) +#if !defined( SYS_LINUX ) && !( defined( WIN32 ) && !defined( UNDER_CE ) ) + msg_Warn( p_this, "VLAN-based channels are not supported " + "on this architecture" ); +#endif /* Allocate structure */ - p_main->p_channel = malloc( sizeof( input_channel_t ) ); - if( p_main->p_channel == NULL ) + p_this->p_vlc->p_channel = malloc( sizeof( input_channel_t ) ); + if( p_this->p_vlc->p_channel == NULL ) { - intf_ErrMsg( "network error: could not create channel bank" ); + msg_Err( p_this, "out of memory" ); return( -1 ); } /* Initialize structure */ - p_main->p_channel->i_channel = 0; - p_main->p_channel->last_change = 0; + p_this->p_vlc->p_channel->i_channel = 0; + p_this->p_vlc->p_channel->last_change = 0; - intf_WarnMsg( 2, "network: channels initialized" ); + msg_Dbg( p_this, "channels initialized" ); return( 0 ); - -#else - intf_ErrMsg( "network error : channels not supported on this platform" ); - return( 1 ); - -#endif } /***************************************************************************** @@ -195,97 +158,88 @@ int network_ChannelCreate( void ) * should be unlocked using input_ChannelLeave(). * Non 0 will be returned in case of error. *****************************************************************************/ -int network_ChannelJoin( int i_channel ) +int __network_ChannelJoin( vlc_object_t *p_this, int i_channel ) { -#if defined( SYS_LINUX ) || defined( WIN32 ) - #define VLCS_VERSION 13 #define MESSAGE_LENGTH 256 +#if defined( SYS_LINUX ) || ( defined( WIN32 ) && !defined( UNDER_CE ) ) + module_t * p_network; + char * psz_network = NULL; + network_socket_t socket_desc; char psz_mess[ MESSAGE_LENGTH ]; char psz_mac[ 40 ]; - int i_fd, i_dummy, i_port; + int i_fd, i_port; char *psz_vlcs; - struct sockaddr_in sa_server; - struct sockaddr_in sa_client; struct timeval delay; fd_set fds; - if( !main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR, - INPUT_NETWORK_CHANNEL_DEFAULT ) ) + if( p_this->p_vlc->p_channel == NULL ) { - intf_ErrMsg( "network: channels disabled, to enable them, use the" - "--channels option" ); - return -1; + msg_Warn( p_this, "channels not initialized" ); + return 0; } - /* If last change is too recent, wait a while */ - if( mdate() - p_main->p_channel->last_change < INPUT_CHANNEL_CHANGE_DELAY ) + if( p_this->p_vlc->p_channel->i_channel == i_channel ) { - intf_WarnMsg( 2, "network: waiting before changing channel" ); - /* XXX Isn't this completely brain-damaged ??? -- Sam */ - mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY ); + return 0; } - /* Initializing the socket */ - i_fd = socket( AF_INET, SOCK_DGRAM, 0 ); - if( i_fd < 0 ) + if( !config_GetInt( p_this, "network-channel" ) ) { - intf_ErrMsg( "network error: unable to create vlcs socket (%s)", - strerror( errno ) ); + msg_Err( p_this, "channels disabled, to enable them, use the" + " --channels option" ); return -1; } - i_dummy = 1; - if( setsockopt( i_fd, SOL_SOCKET, SO_REUSEADDR, - (void *) &i_dummy, sizeof( i_dummy ) ) == -1 ) + if( config_GetInt( p_this, "ipv4" ) ) { - intf_ErrMsg( "network error: can't SO_REUSEADDR vlcs socket (%s)", - strerror(errno)); - close( i_fd ); - return -1; + psz_network = "ipv4"; + } + if( config_GetInt( p_this, "ipv6" ) ) + { + psz_network = "ipv6"; } /* Getting information about the channel server */ - psz_vlcs = main_GetPszVariable( INPUT_CHANNEL_SERVER_VAR, - INPUT_CHANNEL_SERVER_DEFAULT ); - i_port = main_GetIntVariable( INPUT_CHANNEL_PORT_VAR, - INPUT_CHANNEL_PORT_DEFAULT ); - - intf_WarnMsg( 5, "network: socket %i, vlcs '%s', port %d", - i_fd, psz_vlcs, i_port ); - - memset( &sa_client, 0x00, sizeof(struct sockaddr_in) ); - memset( &sa_server, 0x00, sizeof(struct sockaddr_in) ); - sa_client.sin_family = AF_INET; - sa_server.sin_family = AF_INET; - sa_client.sin_port = htons( 4312 ); - sa_server.sin_port = htons( i_port ); - sa_client.sin_addr.s_addr = INADDR_ANY; -#ifdef HAVE_ARPA_INET_H - inet_aton( psz_vlcs, &sa_server.sin_addr ); -#else - sa_server.sin_addr.s_addr = inet_addr( psz_vlcs ); -#endif - - /* Bind the socket */ - if( bind( i_fd, (struct sockaddr*)(&sa_client), sizeof(sa_client) ) ) + if( !(psz_vlcs = config_GetPsz( p_this, "channel-server" )) ) { - intf_ErrMsg( "network: unable to bind vlcs socket (%s)", - strerror( errno ) ); - close( i_fd ); + msg_Err( p_this, "configuration variable channel-server empty" ); return -1; } + i_port = config_GetInt( p_this, "channel-port" ); + + msg_Dbg( p_this, "connecting to %s:%d", psz_vlcs, i_port ); + + /* Prepare the network_socket_t structure */ + socket_desc.i_type = NETWORK_UDP; + socket_desc.psz_bind_addr = ""; + socket_desc.i_bind_port = 4321; + socket_desc.psz_server_addr = psz_vlcs; + socket_desc.i_server_port = i_port; + + /* Find an appropriate network module */ + p_this->p_private = (void*) &socket_desc; + p_network = module_Need( p_this, "network", psz_network/*, &socket_desc*/ ); + if( p_network == NULL ) + { + return( -1 ); + } + module_Unneed( p_this, p_network ); + + free( psz_vlcs ); /* Do we really need this ? -- Meuuh */ + i_fd = socket_desc.i_handle; + /* Look for the interface MAC address */ - if( GetMacAddress( i_fd, psz_mac ) ) + if( GetMacAddress( p_this, i_fd, psz_mac ) ) { - intf_ErrMsg( "network error: failed getting MAC address" ); + msg_Err( p_this, "failed getting MAC address" ); close( i_fd ); return -1; } - intf_WarnMsg( 6, "network: MAC address is %s", psz_mac ); + msg_Dbg( p_this, "MAC address is %s", psz_mac ); /* Build the message */ sprintf( psz_mess, "%d %u %lu %s \n", i_channel, VLCS_VERSION, @@ -293,14 +247,13 @@ int network_ChannelJoin( int i_channel ) psz_mac ); /* Send the message */ - sendto( i_fd, psz_mess, MESSAGE_LENGTH, 0, - (struct sockaddr *)(&sa_server), sizeof(struct sockaddr) ); + send( i_fd, psz_mess, MESSAGE_LENGTH, 0 ); - intf_WarnMsg( 2, "network: attempting to join channel %d", i_channel ); + msg_Dbg( p_this, "attempting to join channel %d", i_channel ); /* We have changed channels ! (or at least, we tried) */ - p_main->p_channel->last_change = mdate(); - p_main->p_channel->i_channel = i_channel; + p_this->p_vlc->p_channel->last_change = mdate(); + p_this->p_vlc->p_channel->i_channel = i_channel; /* Wait 5 sec for an answer from the server */ delay.tv_sec = 5; @@ -311,63 +264,52 @@ int network_ChannelJoin( int i_channel ) switch( select( i_fd + 1, &fds, NULL, NULL, &delay ) ) { case 0: - intf_ErrMsg( "network error: no answer from vlcs" ); + msg_Err( p_this, "no answer from vlcs" ); close( i_fd ); return -1; - break; case -1: - intf_ErrMsg( "network error: error while listening to vlcs" ); + msg_Err( p_this, "error while listening to vlcs" ); close( i_fd ); return -1; - break; } - i_dummy = sizeof( struct sockaddr ); - recvfrom( i_fd, psz_mess, MESSAGE_LENGTH, 0, - (struct sockaddr *)(&sa_client), &i_dummy); - psz_mess[ MESSAGE_LENGTH - 1 ] = 0; + recv( i_fd, psz_mess, MESSAGE_LENGTH, 0 ); + psz_mess[ MESSAGE_LENGTH - 1 ] = '\0'; - if( !strncasecmp( psz_mess, "E: ", 3 ) ) + if( !strncasecmp( psz_mess, "E:", 2 ) ) { - intf_ErrMsg( "network error: vlcs said '%s'", psz_mess + 3 ); + msg_Err( p_this, "vlcs said '%s'", psz_mess + 2 ); close( i_fd ); return -1; } - else if( !strncasecmp( psz_mess, "I: ", 3 ) ) + else if( !strncasecmp( psz_mess, "I:", 2 ) ) { - intf_WarnMsg( 2, "network info: vlcs said '%s'", psz_mess + 3 ); + msg_Dbg( p_this, "vlcs said '%s'", psz_mess + 2 ); } - else /* We got something to play ! FIXME: not very nice */ + else { -# define p_item \ - (&p_main->p_playlist->p_item[ p_main->p_playlist->i_index + 1]) - vlc_mutex_lock( &p_main->p_playlist->change_lock ); - if( p_item ) + /* We got something to play ! */ + playlist_t *p_playlist; + p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST, + FIND_ANYWHERE ); + if( p_playlist != NULL ) { - free( p_item->psz_name ); - p_item->psz_name = strdup( psz_mess ); - /* Unlock _afterwards_ */ - vlc_mutex_unlock( &p_main->p_playlist->change_lock ); - } - else - { - /* Unlock _before_ */ - vlc_mutex_unlock( &p_main->p_playlist->change_lock ); - intf_PlaylistAdd( p_main->p_playlist, 0, psz_mess ); + playlist_Add( p_playlist, psz_mess, + PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END ); + vlc_object_release( p_playlist ); } } /* Close the socket and return nicely */ +# if defined( WIN32 ) + closesocket( i_fd ); +# else close( i_fd ); - - return 0; - -#else - intf_ErrMsg( "network error: channels not supported on this platform" ); - return -1; +# endif #endif + return 0; } /* Following functions are local */ @@ -375,24 +317,30 @@ int network_ChannelJoin( int i_channel ) /***************************************************************************** * GetMacAddress: extract the MAC Address *****************************************************************************/ -static int GetMacAddress( int i_fd, char *psz_mac ) +static int GetMacAddress( vlc_object_t *p_this, int i_fd, char *psz_mac ) { #if defined( SYS_LINUX ) struct ifreq interface; int i_ret; + char *psz_interface; /* * Looking for information about the eth0 interface */ interface.ifr_addr.sa_family = AF_INET; - strcpy( interface.ifr_name, - main_GetPszVariable( INPUT_IFACE_VAR, INPUT_IFACE_DEFAULT ) ); + if( !(psz_interface = config_GetPsz( p_this, "iface" )) ) + { + msg_Err( p_this, "configuration variable iface empty" ); + return -1; + } + strcpy( interface.ifr_name, psz_interface ); + free( psz_interface ); i_ret = ioctl( i_fd, SIOCGIFHWADDR, &interface ); if( i_ret ) { - intf_ErrMsg( "network error: ioctl SIOCGIFHWADDR failed" ); + msg_Err( p_this, "ioctl SIOCGIFHWADDR failed" ); return( i_ret ); } @@ -406,14 +354,55 @@ static int GetMacAddress( int i_fd, char *psz_mac ) return( 0 ); -#elif defined( WIN32 ) +#elif defined( SYS_DARWIN ) + char *psz_interface; + io_object_t ctrl_service; + CFTypeRef cfd_mac_address; + UInt8 ui_mac_address[kIOEthernetAddressSize]; + + if( !(psz_interface = config_GetPsz( p_this, "iface" )) ) + { + msg_Err( p_this, "configuration variable iface empty" ); + return( -1 ); + } + + if( GetNetIntfCtrl( psz_interface, &ctrl_service ) ) + { + msg_Err( p_this, "GetNetIntfCtrl failed" ); + return( -1 ); + } + + cfd_mac_address = IORegistryEntryCreateCFProperty( ctrl_service, + CFSTR(kIOMACAddress), + kCFAllocatorDefault, + 0 ); + IOObjectRelease( ctrl_service ); + if( cfd_mac_address == NULL ) + { + msg_Err( p_this, "IORegistryEntryCreateCFProperty failed" ); + return( -1 ); + } + + CFDataGetBytes( cfd_mac_address, + CFRangeMake(0, kIOEthernetAddressSize), + ui_mac_address ); + CFRelease( cfd_mac_address ); + + sprintf( psz_mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x", + ui_mac_address[0], ui_mac_address[1], + ui_mac_address[2], ui_mac_address[3], + ui_mac_address[4], ui_mac_address[5] ); + + return( 0 ); + +#elif defined( WIN32 ) && !defined( UNDER_CE ) int i, i_ret = -1; /* Get adapter list - support for more than one adapter */ LANA_ENUM AdapterList; NCB Ncb; - intf_WarnMsg( 2, "network: looking for MAC address" ); + msg_Dbg( p_this, "looking for MAC address" ); memset( &Ncb, 0, sizeof( NCB ) ); Ncb.ncb_command = NCBENUM; @@ -433,12 +422,57 @@ static int GetMacAddress( int i_fd, char *psz_mac ) return( i_ret ); #else - return( -1); + strcpy( psz_mac, "00:00:00:00:00:00" ); + return( 0 ); #endif } -#ifdef WIN32 +#ifdef SYS_DARWIN +/***************************************************************************** + * GetNetIntfCtrl : get parent controller for network interface + *****************************************************************************/ +static int GetNetIntfCtrl( const char *psz_interface, + io_object_t *ctrl_service ) +{ + mach_port_t port; + kern_return_t ret; + io_object_t intf_service; + io_iterator_t intf_iterator; + + /* get port for IOKit communication */ + if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS ) + { + return( -1 ); + } + + /* look up the IOService object for the interface */ + ret = IOServiceGetMatchingServices( port, IOBSDNameMatching( port, 0, + psz_interface ), &intf_iterator ); + if( ret != KERN_SUCCESS ) + { + return( -1 ); + } + + intf_service = IOIteratorNext( intf_iterator ); + if( intf_service == NULL ) + { + return( -1 ); + } + + ret = IORegistryEntryGetParentEntry( intf_service, + kIOServicePlane, + ctrl_service ); + IOObjectRelease( intf_service ); + if( ret != KERN_SUCCESS ) + { + return( -1 ); + } + + return( 0 ); +} + +#elif defined( WIN32 ) && !defined( UNDER_CE ) /***************************************************************************** * GetAdapterInfo : gets some informations about the interface using NETBIOS *****************************************************************************/ @@ -458,7 +492,7 @@ static int GetAdapterInfo( int i_adapter, char *psz_string ) if( Netbios( &Ncb ) != NRC_GOODRET ) { - intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode ); +//X intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode ); return -1; } @@ -485,13 +519,13 @@ static int GetAdapterInfo( int i_adapter, char *psz_string ) (int) ( Adapter.adapt.adapter_address[4] ), (int) ( Adapter.adapt.adapter_address[5] ) ); - intf_WarnMsg( 2, "network: found MAC address %s", psz_string ); +//X intf_WarnMsg( 2, "network: found MAC address %s", psz_string ); return 0; } else { - intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode ); +//X intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode ); return -1; } }