1 /*****************************************************************************
2 * netutils.c: various network functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001 VideoLAN
5 * $Id: netutils.c,v 1.36 2001/05/31 01:37: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>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <stdlib.h> /* free(), realloc(), atoi() */
33 #include <errno.h> /* errno() */
34 #include <string.h> /* memset() */
37 #include <unistd.h> /* gethostname() */
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
42 #if !defined( _MSC_VER )
43 #include <sys/time.h> /* gettimeofday */
47 #include <netdb.h> /* gethostbyname() */
48 #include <netinet/in.h> /* BSD: struct in_addr */
49 #include <sys/socket.h> /* BSD: struct sockaddr */
52 #ifdef HAVE_ARPA_INET_H
53 #include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
57 #include <sys/ioctl.h> /* ioctl() */
60 #if defined( WIN32 ) /* tools to get the MAC adress from */
61 #include <windows.h> /* the interface under Windows */
66 #include <net/if.h> /* interface (arch-dependent) */
69 #ifdef HAVE_SYS_SOCKIO_H
70 #include <sys/sockio.h>
83 /*****************************************************************************
84 * input_channel_t: channel library data
85 *****************************************************************************
86 * Store global channel library data.
87 * The part of the code concerning the channel changing process is unstable
88 * as it depends on the VideoLAN channel server, which isn't frozen for
90 *****************************************************************************/
91 typedef struct input_channel_s
93 int i_channel; /* current channel number */
94 mtime_t last_change; /* last change date */
97 /*****************************************************************************
99 *****************************************************************************/
100 static int GetMacAddress ( int i_socket, char *psz_mac );
102 static int GetAdapterInfo ( int i_adapter, char *psz_string );
105 /*****************************************************************************
106 * network_BuildLocalAddr : fill a sockaddr_in structure for local binding
107 *****************************************************************************/
108 int network_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port,
109 char * psz_broadcast )
111 char psz_hostname[INPUT_MAX_SOURCE_LENGTH];
112 struct hostent * p_hostent;
115 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
116 p_socket->sin_family = AF_INET; /* family */
117 p_socket->sin_port = htons( i_port );
118 if( psz_broadcast == NULL )
120 /* Try to get our own IP */
121 if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
123 intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
131 /* I didn't manage to make INADDR_ANYT work, even with setsockopt
132 * so, as it's kludgy to try and determine the broadcast addr
133 * it is passed as an argument in the command line */
134 strncpy( psz_hostname, psz_broadcast, INPUT_MAX_SOURCE_LENGTH );
137 /* Try to convert address directly from in_addr - this will work if
138 * psz_in_addr is dotted decimal. */
139 #ifdef HAVE_ARPA_INET_H
140 if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
142 if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
145 /* We have a fqdn, try to find its address */
146 if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
148 intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
152 /* Copy the first address of the host in the socket address */
153 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
154 p_hostent->h_length );
159 /*****************************************************************************
160 * network_BuildRemoteAddr : fill a sockaddr_in structure for remote host
161 *****************************************************************************/
162 int network_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
164 struct hostent * p_hostent;
166 /* Reset structure */
167 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
168 p_socket->sin_family = AF_INET; /* family */
169 p_socket->sin_port = htons( 0 ); /* This is for remote end */
171 /* Try to convert address directly from in_addr - this will work if
172 * psz_in_addr is dotted decimal. */
174 #ifdef HAVE_ARPA_INET_H
175 if( !inet_aton( psz_server, &p_socket->sin_addr) )
177 if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
180 /* We have a fqdn, try to find its address */
181 if ( (p_hostent = gethostbyname(psz_server)) == NULL )
183 intf_ErrMsg( "BuildRemoteAddr: unknown host %s",
188 /* Copy the first address of the host in the socket address */
189 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
190 p_hostent->h_length );
195 /*****************************************************************************
196 * network_ChannelCreate: initialize global channel method data
197 *****************************************************************************
198 * Initialize channel input method global data. This function should be called
199 * once before any input thread is created or any call to other
200 * input_Channel*() function is attempted.
201 *****************************************************************************/
202 int network_ChannelCreate( void )
204 /* Even when BSD are supported, BeOS is not likely to be supported, so
205 * I prefer to put it apart */
206 #if defined( SYS_BEOS )
207 intf_ErrMsg( "error: channel changing is not yet supported under BeOS" );
210 #elif defined( SYS_LINUX ) || defined( WIN32 )
211 /* FIXME : channels handling only work for linux */
212 /* Allocate structure */
213 p_main->p_channel = malloc( sizeof( input_channel_t ) );
214 if( p_main->p_channel == NULL )
216 intf_ErrMsg( "network error: could not create channel bank" );
220 /* Initialize structure */
221 p_main->p_channel->i_channel = 0;
222 p_main->p_channel->last_change = 0;
224 intf_Msg( "network: channels initialized" );
228 intf_ErrMsg( "network error : channels not supported" );
234 /*****************************************************************************
235 * network_ChannelJoin: join a channel
236 *****************************************************************************
237 * This function will try to join a channel. If the relevant interface is
238 * already on the good channel, nothing will be done. Else, and if possible
239 * (if the interface is not locked), the channel server will be contacted
240 * and a change will be requested. The function will block until the change
241 * is effective. Note that once a channel is no more used, it's interface
242 * should be unlocked using input_ChannelLeave().
243 * Non 0 will be returned in case of error.
244 *****************************************************************************/
245 int network_ChannelJoin( int i_channel )
247 /* I still prefer to put BeOS a bit apart */
248 #if defined( SYS_BEOS )
249 intf_ErrMsg( "network error: channels are not yet supported under BeOS" );
252 #elif defined( SYS_LINUX ) || defined( WIN32 )
255 struct sockaddr_in sa_server;
256 struct sockaddr_in sa_client;
257 unsigned int i_version = 12;
260 char i_mess_length = 80;
261 unsigned long int i_date;
262 struct timeval answer_delay;
267 char * psz_channel_server;
269 if( !main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
270 INPUT_NETWORK_CHANNEL_DEFAULT ) )
272 intf_ErrMsg( "network: channels disabled, to enable them, use the"
273 "--channels option" );
278 intf_DbgMsg( "network: ChannelJoin : %d", i_channel );
279 /* If last change is too recent, wait a while */
280 if( mdate() - p_main->p_channel->last_change < INPUT_CHANNEL_CHANGE_DELAY )
282 intf_WarnMsg( 2, "network: waiting before changing channel" );
283 mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY );
286 p_main->p_channel->last_change = mdate();
287 p_main->p_channel->i_channel = i_channel;
289 intf_WarnMsg( 2, "network: joining channel %d", i_channel );
292 * Initializing the socket
294 i_socket = socket( AF_INET, SOCK_DGRAM, 0 );
297 * Getting the server's information
299 intf_WarnMsg( 6, "Channel server: %s port: %d",
300 main_GetPszVariable( INPUT_CHANNEL_SERVER_VAR,
301 INPUT_CHANNEL_SERVER_DEFAULT ),
302 main_GetIntVariable( INPUT_CHANNEL_PORT_VAR,
303 INPUT_CHANNEL_PORT_DEFAULT ) );
305 memset( &sa_server, 0x00, sizeof(struct sockaddr_in) );
306 sa_server.sin_family = AF_INET;
307 sa_server.sin_port = htons( main_GetIntVariable( INPUT_CHANNEL_PORT_VAR,
308 INPUT_CHANNEL_PORT_DEFAULT ) );
310 psz_channel_server = strdup( main_GetPszVariable( INPUT_CHANNEL_SERVER_VAR,
311 INPUT_CHANNEL_SERVER_DEFAULT ) );
312 #ifdef HAVE_ARPA_INET_H
313 inet_aton( psz_channel_server, &sa_server.sin_addr );
315 sa_server.sin_addr.s_addr = inet_addr( psz_channel_server );
317 free( psz_channel_server );
320 * Looking for the interface MAC address
322 if( GetMacAddress( i_socket, psz_mac ) )
324 intf_ErrMsg( "network error: failed getting MAC address" );
329 * Getting date of the client in seconds
331 i_date = mdate() / 1000000;
332 intf_DbgMsg( "vlcs: date %lu", i_date );
335 * Build of the message
337 sprintf( psz_mess, "%d %u %lu %s \n",
338 i_channel, i_version, i_date, psz_mac );
340 intf_DbgMsg( "vlcs: The message is %s", psz_mess );
345 memset( &sa_client, 0x00, sizeof(struct sockaddr_in) );
346 sa_client.sin_family = AF_INET;
347 sa_client.sin_port = htons(4312);
348 sa_client.sin_addr.s_addr = INADDR_ANY;
349 i_fromlen = sizeof( struct sockaddr );
350 i_rc = bind( i_socket, (struct sockaddr *)(&sa_client),\
351 sizeof(struct sockaddr) );
354 intf_ErrMsg( "vlcs: Unable to bind socket:%u ", i_rc );
355 /* TODO put CS_R_BIND in types.h*/
356 /* return CS_R_SOCKET;*/
363 sendto( i_socket, psz_mess, i_mess_length, 0, \
364 (struct sockaddr *)(&sa_server), \
365 sizeof(struct sockaddr) );
368 * Waiting 5 sec for one answer from the server
370 answer_delay.tv_sec = 5;
371 answer_delay.tv_usec = 0;
373 FD_SET( i_socket, &fd );
374 i_nbanswer = select( i_socket + 1, &fd, NULL, NULL, &answer_delay );
379 intf_DbgMsg( "vlcs: no answer" );
383 intf_DbgMsg( "vlcs: unable to receive the answer ");
387 recvfrom( i_socket, &i_answer, sizeof(char), 0,\
388 (struct sockaddr *)(&sa_client), &i_fromlen);
390 intf_DbgMsg( "vlcs: the answer : %i", i_answer );
395 intf_DbgMsg( "vlcs: the server failed to create the thread" );
398 intf_DbgMsg( "vlcs: the server tries to change the channel" );
401 intf_DbgMsg( "vlcs: unknown answer !" );
415 intf_ErrMsg( "network error: channels not supported" );
421 /* Following functions are local */
423 /*****************************************************************************
424 * GetMacAddress: extract the MAC Address
425 *****************************************************************************/
426 static int GetMacAddress( int i_socket, char *psz_mac )
428 #if defined( SYS_LINUX )
429 struct ifreq interface;
433 * Looking for information about the eth0 interface
435 interface.ifr_addr.sa_family = AF_INET;
436 strcpy( interface.ifr_name, INPUT_IFACE_DEFAULT );
438 i_ret = ioctl( i_socket, SIOCGIFHWADDR, &interface );
442 intf_ErrMsg( "network error: ioctl SIOCGIFHWADDR failed" );
446 sprintf( psz_mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
447 interface.ifr_hwaddr.sa_data[0] & 0xff,
448 interface.ifr_hwaddr.sa_data[1] & 0xff,
449 interface.ifr_hwaddr.sa_data[2] & 0xff,
450 interface.ifr_hwaddr.sa_data[3] & 0xff,
451 interface.ifr_hwaddr.sa_data[4] & 0xff,
452 interface.ifr_hwaddr.sa_data[5] & 0xff );
456 #elif defined( WIN32 )
459 /* Get adapter list - support for more than one adapter */
460 LANA_ENUM AdapterList;
463 intf_WarnMsg( 2, "network: looking for MAC address" );
465 memset( &Ncb, 0, sizeof( NCB ) );
466 Ncb.ncb_command = NCBENUM;
467 Ncb.ncb_buffer = (unsigned char *)&AdapterList;
468 Ncb.ncb_length = sizeof( AdapterList );
471 /* Get all of the local ethernet addresses */
472 for ( i = 0; i < AdapterList.length ; ++i )
474 if ( GetAdapterInfo ( AdapterList.lana[ i ], psz_mac ) == 0 )
489 /*****************************************************************************
490 * GetAdapterInfo : gets some informations about the interface using NETBIOS
491 *****************************************************************************/
492 static int GetAdapterInfo( int i_adapter, char *psz_string )
496 ADAPTER_STATUS adapt;
497 NAME_BUFFER psz_name[30];
500 /* Reset the LAN adapter so that we can begin querying it */
502 memset( &Ncb, 0, sizeof ( Ncb ) );
503 Ncb.ncb_command = NCBRESET;
504 Ncb.ncb_lana_num = i_adapter;
506 if( Netbios( &Ncb ) != NRC_GOODRET )
508 intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode );
512 /* Prepare to get the adapter status block */
513 memset( &Ncb, 0, sizeof( Ncb ) ) ; /* Initialization */
514 Ncb.ncb_command = NCBASTAT;
515 Ncb.ncb_lana_num = i_adapter;
517 strcpy( (char *)Ncb.ncb_callname, "*" );
519 memset( &Adapter, 0, sizeof ( Adapter ) );
520 Ncb.ncb_buffer = ( unsigned char * ) &Adapter;
521 Ncb.ncb_length = sizeof ( Adapter );
523 /* Get the adapter's info and, if this works, return it in standard,
524 * colon-delimited form. */
525 if ( Netbios( &Ncb ) == 0 )
527 sprintf ( psz_string, "%02X:%02X:%02X:%02X:%02X:%02X",
528 (int) ( Adapter.adapt.adapter_address[0] ),
529 (int) ( Adapter.adapt.adapter_address[1] ),
530 (int) ( Adapter.adapt.adapter_address[2] ),
531 (int) ( Adapter.adapt.adapter_address[3] ),
532 (int) ( Adapter.adapt.adapter_address[4] ),
533 (int) ( Adapter.adapt.adapter_address[5] ) );
535 intf_WarnMsg( 2, "network: found MAC address %s", psz_string );
541 intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode );