1
/*****************************************************************************
2
* netutils.c: various network functions
3
*****************************************************************************
4
* Copyright (C) 1999-2001 VideoLAN
5
* $Id: netutils.c,v 1.53 2001/12/07 18:33:08 sam 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@via.ecp.fr>
11
* Christophe Massiot <massiot@via.ecp.fr>
12
* Samuel Hocevar <sam@via.ecp.fr>
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.
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.
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
*****************************************************************************/
29
/*****************************************************************************
31
*****************************************************************************/
34
#include <stdlib.h> /* free(), realloc(), atoi() */
35
#include <errno.h> /* errno() */
36
#include <string.h> /* memset() */
38
#ifdef STRNCASECMP_IN_STRINGS_H
43
# include <unistd.h> /* gethostname() */
44
#elif defined( _MSC_VER ) && defined( _WIN32 )
48
#if !defined( _MSC_VER )
49
#include <sys/time.h> /* gettimeofday */
53
# include <winsock2.h>
54
#elif !defined( SYS_BEOS ) && !defined( SYS_NTO )
55
# include <netdb.h> /* hostent ... */
56
# include <sys/socket.h> /* BSD: struct sockaddr */
57
# include <netinet/in.h> /* BSD: struct in_addr */
58
# ifdef HAVE_ARPA_INET_H
59
# include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
64
#include <sys/ioctl.h> /* ioctl() */
67
#if defined( WIN32 ) /* tools to get the MAC adress from */
68
#include <windows.h> /* the interface under Windows */
73
#include <net/if.h> /* interface (arch-dependent) */
76
#ifdef HAVE_SYS_SOCKIO_H
77
#include <sys/sockio.h>
85
#include "intf_playlist.h"
89
/*****************************************************************************
90
* input_channel_t: channel library data
91
*****************************************************************************
92
* Store global channel library data.
93
* The part of the code concerning the channel changing process is unstable
94
* as it depends on the VideoLAN channel server, which isn't frozen for
96
*****************************************************************************/
97
typedef struct input_channel_s
99
int i_channel; /* current channel number */
100
mtime_t last_change; /* last change date */
103
/*****************************************************************************
105
*****************************************************************************/
106
static int GetMacAddress ( int i_fd, char *psz_mac );
108
static int GetAdapterInfo ( int i_adapter, char *psz_string );
111
/*****************************************************************************
112
* network_BuildAddr : fill a sockaddr_in structure
113
*****************************************************************************/
114
int network_BuildAddr( struct sockaddr_in * p_socket,
115
char * psz_address, int i_port )
117
#if defined( SYS_BEOS )
118
intf_ErrMsg( "error: networking is not yet supported under BeOS" );
123
memset( p_socket, 0, sizeof( struct sockaddr_in ) );
124
p_socket->sin_family = AF_INET; /* family */
125
p_socket->sin_port = htons( i_port );
126
if( psz_address == NULL )
128
p_socket->sin_addr.s_addr = INADDR_ANY;
132
struct hostent * p_hostent;
134
/* Try to convert address directly from in_addr - this will work if
135
* psz_broadcast is dotted decimal. */
136
#ifdef HAVE_ARPA_INET_H
137
if( !inet_aton( psz_address, &p_socket->sin_addr) )
139
if( (p_socket->sin_addr.s_addr = inet_addr( psz_address )) == -1 )
142
/* We have a fqdn, try to find its address */
143
if ( (p_hostent = gethostbyname( psz_address )) == NULL )
145
intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_address );
149
/* Copy the first address of the host in the socket address */
150
memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
151
p_hostent->h_length );
158
/*****************************************************************************
159
* network_ChannelCreate: initialize global channel method data
160
*****************************************************************************
161
* Initialize channel input method global data. This function should be called
162
* once before any input thread is created or any call to other
163
* input_Channel*() function is attempted.
164
*****************************************************************************/
165
int network_ChannelCreate( void )
167
#if defined( SYS_LINUX ) || defined( WIN32 )
169
/* Allocate structure */
170
p_main->p_channel = malloc( sizeof( input_channel_t ) );
171
if( p_main->p_channel == NULL )
173
intf_ErrMsg( "network error: could not create channel bank" );
177
/* Initialize structure */
178
p_main->p_channel->i_channel = 0;
179
p_main->p_channel->last_change = 0;
181
intf_WarnMsg( 2, "network: channels initialized" );
185
intf_ErrMsg( "network error : channels not supported on this platform" );
191
/*****************************************************************************
192
* network_ChannelJoin: join a channel
193
*****************************************************************************
194
* This function will try to join a channel. If the relevant interface is
195
* already on the good channel, nothing will be done. Else, and if possible
196
* (if the interface is not locked), the channel server will be contacted
197
* and a change will be requested. The function will block until the change
198
* is effective. Note that once a channel is no more used, its interface
199
* should be unlocked using input_ChannelLeave().
200
* Non 0 will be returned in case of error.
201
*****************************************************************************/
202
int network_ChannelJoin( int i_channel )
204
#if defined( SYS_LINUX ) || defined( WIN32 )
206
#define VLCS_VERSION 13
207
#define MESSAGE_LENGTH 256
209
char psz_mess[ MESSAGE_LENGTH ];
211
int i_fd, i_dummy, i_port;
213
struct sockaddr_in sa_server;
214
struct sockaddr_in sa_client;
215
struct timeval delay;
218
if( !main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
219
INPUT_NETWORK_CHANNEL_DEFAULT ) )
221
intf_ErrMsg( "network: channels disabled, to enable them, use the"
222
"--channels option" );
226
/* If last change is too recent, wait a while */
227
if( mdate() - p_main->p_channel->last_change < INPUT_CHANNEL_CHANGE_DELAY )
229
intf_WarnMsg( 2, "network: waiting before changing channel" );
230
/* XXX Isn't this completely brain-damaged ??? -- Sam */
231
mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY );
234
/* Initializing the socket */
235
i_fd = socket( AF_INET, SOCK_DGRAM, 0 );
238
intf_ErrMsg( "network error: unable to create vlcs socket (%s)",
244
if( setsockopt( i_fd, SOL_SOCKET, SO_REUSEADDR,
245
(void *) &i_dummy, sizeof( i_dummy ) ) == -1 )
247
intf_ErrMsg( "network error: can't SO_REUSEADDR vlcs socket (%s)",
253
/* Getting information about the channel server */
254
psz_vlcs = main_GetPszVariable( INPUT_CHANNEL_SERVER_VAR,
255
INPUT_CHANNEL_SERVER_DEFAULT );
256
i_port = main_GetIntVariable( INPUT_CHANNEL_PORT_VAR,
257
INPUT_CHANNEL_PORT_DEFAULT );
259
intf_WarnMsg( 5, "network: socket %i, vlcs '%s', port %d",
260
i_fd, psz_vlcs, i_port );
262
memset( &sa_client, 0x00, sizeof(struct sockaddr_in) );
263
memset( &sa_server, 0x00, sizeof(struct sockaddr_in) );
264
sa_client.sin_family = AF_INET;
265
sa_server.sin_family = AF_INET;
266
sa_client.sin_port = htons( 4312 );
267
sa_server.sin_port = htons( i_port );
268
sa_client.sin_addr.s_addr = INADDR_ANY;
269
#ifdef HAVE_ARPA_INET_H
270
inet_aton( psz_vlcs, &sa_server.sin_addr );
272
sa_server.sin_addr.s_addr = inet_addr( psz_vlcs );
275
/* Bind the socket */
276
if( bind( i_fd, (struct sockaddr*)(&sa_client), sizeof(sa_client) ) )
278
intf_ErrMsg( "network: unable to bind vlcs socket (%s)",
284
/* Look for the interface MAC address */
285
if( GetMacAddress( i_fd, psz_mac ) )
287
intf_ErrMsg( "network error: failed getting MAC address" );
292
intf_WarnMsg( 6, "network: MAC address is %s", psz_mac );
294
/* Build the message */
295
sprintf( psz_mess, "%d %u %lu %s \n", i_channel, VLCS_VERSION,
296
(unsigned long)(mdate() / (u64)1000000),
299
/* Send the message */
300
sendto( i_fd, psz_mess, MESSAGE_LENGTH, 0,
301
(struct sockaddr *)(&sa_server), sizeof(struct sockaddr) );
303
intf_WarnMsg( 2, "network: attempting to join channel %d", i_channel );
305
/* We have changed channels ! (or at least, we tried) */
306
p_main->p_channel->last_change = mdate();
307
p_main->p_channel->i_channel = i_channel;
309
/* Wait 5 sec for an answer from the server */
313
FD_SET( i_fd, &fds );
315
switch( select( i_fd + 1, &fds, NULL, NULL, &delay ) )
318
intf_ErrMsg( "network error: no answer from vlcs" );
324
intf_ErrMsg( "network error: error while listening to vlcs" );
330
i_dummy = sizeof( struct sockaddr );
331
recvfrom( i_fd, psz_mess, MESSAGE_LENGTH, 0,
332
(struct sockaddr *)(&sa_client), &i_dummy);
333
psz_mess[ MESSAGE_LENGTH - 1 ] = 0;
335
if( !strncasecmp( psz_mess, "E: ", 3 ) )
337
intf_ErrMsg( "network error: vlcs said '%s'", psz_mess + 3 );
341
else if( !strncasecmp( psz_mess, "I: ", 3 ) )
343
intf_WarnMsg( 2, "network info: vlcs said '%s'", psz_mess + 3 );
345
else /* We got something to play ! FIXME: not very nice */
348
(&p_main->p_playlist->p_item[ p_main->p_playlist->i_index + 1])
349
vlc_mutex_lock( &p_main->p_playlist->change_lock );
352
free( p_item->psz_name );
353
p_item->psz_name = strdup( psz_mess );
354
/* Unlock _afterwards_ */
355
vlc_mutex_unlock( &p_main->p_playlist->change_lock );
359
/* Unlock _before_ */
360
vlc_mutex_unlock( &p_main->p_playlist->change_lock );
361
intf_PlaylistAdd( p_main->p_playlist, 0, psz_mess );
365
/* Close the socket and return nicely */
371
intf_ErrMsg( "network error: channels not supported on this platform" );
377
/* Following functions are local */
379
/*****************************************************************************
380
* GetMacAddress: extract the MAC Address
381
*****************************************************************************/
382
static int GetMacAddress( int i_fd, char *psz_mac )
384
#if defined( SYS_LINUX )
385
struct ifreq interface;
389
* Looking for information about the eth0 interface
391
interface.ifr_addr.sa_family = AF_INET;
392
strcpy( interface.ifr_name,
393
main_GetPszVariable( INPUT_IFACE_VAR, INPUT_IFACE_DEFAULT ) );
395
i_ret = ioctl( i_fd, SIOCGIFHWADDR, &interface );
399
intf_ErrMsg( "network error: ioctl SIOCGIFHWADDR failed" );
403
sprintf( psz_mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
404
interface.ifr_hwaddr.sa_data[0] & 0xff,
405
interface.ifr_hwaddr.sa_data[1] & 0xff,
406
interface.ifr_hwaddr.sa_data[2] & 0xff,
407
interface.ifr_hwaddr.sa_data[3] & 0xff,
408
interface.ifr_hwaddr.sa_data[4] & 0xff,
409
interface.ifr_hwaddr.sa_data[5] & 0xff );
413
#elif defined( WIN32 )
416
/* Get adapter list - support for more than one adapter */
417
LANA_ENUM AdapterList;
420
intf_WarnMsg( 2, "network: looking for MAC address" );
422
memset( &Ncb, 0, sizeof( NCB ) );
423
Ncb.ncb_command = NCBENUM;
424
Ncb.ncb_buffer = (unsigned char *)&AdapterList;
425
Ncb.ncb_length = sizeof( AdapterList );
428
/* Get all of the local ethernet addresses */
429
for ( i = 0; i < AdapterList.length ; ++i )
431
if ( GetAdapterInfo ( AdapterList.lana[ i ], psz_mac ) == 0 )
446
/*****************************************************************************
447
* GetAdapterInfo : gets some informations about the interface using NETBIOS
448
*****************************************************************************/
449
static int GetAdapterInfo( int i_adapter, char *psz_string )
453
ADAPTER_STATUS adapt;
454
NAME_BUFFER psz_name[30];
457
/* Reset the LAN adapter so that we can begin querying it */
459
memset( &Ncb, 0, sizeof ( Ncb ) );
460
Ncb.ncb_command = NCBRESET;
461
Ncb.ncb_lana_num = i_adapter;
463
if( Netbios( &Ncb ) != NRC_GOODRET )
465
intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode );
469
/* Prepare to get the adapter status block */
470
memset( &Ncb, 0, sizeof( Ncb ) ) ; /* Initialization */
471
Ncb.ncb_command = NCBASTAT;
472
Ncb.ncb_lana_num = i_adapter;
474
strcpy( (char *)Ncb.ncb_callname, "*" );
476
memset( &Adapter, 0, sizeof ( Adapter ) );
477
Ncb.ncb_buffer = ( unsigned char * ) &Adapter;
478
Ncb.ncb_length = sizeof ( Adapter );
480
/* Get the adapter's info and, if this works, return it in standard,
481
* colon-delimited form. */
482
if ( Netbios( &Ncb ) == 0 )
484
sprintf ( psz_string, "%02X:%02X:%02X:%02X:%02X:%02X",
485
(int) ( Adapter.adapt.adapter_address[0] ),
486
(int) ( Adapter.adapt.adapter_address[1] ),
487
(int) ( Adapter.adapt.adapter_address[2] ),
488
(int) ( Adapter.adapt.adapter_address[3] ),
489
(int) ( Adapter.adapt.adapter_address[4] ),
490
(int) ( Adapter.adapt.adapter_address[5] ) );
492
intf_WarnMsg( 2, "network: found MAC address %s", psz_string );
498
intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode );