1 /*****************************************************************************
2 * netutils.c: various network functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001 VideoLAN
5 * $Id: netutils.c,v 1.39 2001/10/10 14:25:15 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 )
112 char psz_hostname[INPUT_MAX_SOURCE_LENGTH];
113 struct hostent * p_hostent;
115 #if defined( SYS_BEOS )
116 intf_ErrMsg( "error: channel changing is not yet supported under BeOS" );
121 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
122 p_socket->sin_family = AF_INET; /* family */
123 p_socket->sin_port = htons( i_port );
124 if( psz_broadcast == NULL )
126 /* Try to get our own IP */
127 if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
129 intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
137 /* I didn't manage to make INADDR_ANYT work, even with setsockopt
138 * so, as it's kludgy to try and determine the broadcast addr
139 * it is passed as an argument in the command line */
140 strncpy( psz_hostname, psz_broadcast, INPUT_MAX_SOURCE_LENGTH );
143 /* Try to convert address directly from in_addr - this will work if
144 * psz_in_addr is dotted decimal. */
145 #ifdef HAVE_ARPA_INET_H
146 if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
148 if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
151 /* We have a fqdn, try to find its address */
152 if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
154 intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
158 /* Copy the first address of the host in the socket address */
159 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
160 p_hostent->h_length );
165 /*****************************************************************************
166 * network_BuildRemoteAddr : fill a sockaddr_in structure for remote host
167 *****************************************************************************/
168 int network_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
171 struct hostent * p_hostent;
173 #if defined( SYS_BEOS )
174 intf_ErrMsg( "error: channel changing is not yet supported under BeOS" );
178 /* Reset structure */
179 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
180 p_socket->sin_family = AF_INET; /* family */
181 p_socket->sin_port = htons( 0 ); /* This is for remote end */
183 /* Try to convert address directly from in_addr - this will work if
184 * psz_in_addr is dotted decimal. */
186 #ifdef HAVE_ARPA_INET_H
187 if( !inet_aton( psz_server, &p_socket->sin_addr) )
189 if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
192 /* We have a fqdn, try to find its address */
193 if ( (p_hostent = gethostbyname(psz_server)) == NULL )
195 intf_ErrMsg( "BuildRemoteAddr: unknown host %s",
200 /* Copy the first address of the host in the socket address */
201 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
202 p_hostent->h_length );
207 /*****************************************************************************
208 * network_ChannelCreate: initialize global channel method data
209 *****************************************************************************
210 * Initialize channel input method global data. This function should be called
211 * once before any input thread is created or any call to other
212 * input_Channel*() function is attempted.
213 *****************************************************************************/
214 int network_ChannelCreate( void )
216 /* Even when BSD are supported, BeOS is not likely to be supported, so
217 * I prefer to put it apart */
218 #if defined( SYS_BEOS )
219 intf_ErrMsg( "error: channel changing is not yet supported under BeOS" );
222 /* FIXME : channel handling only work for linux */
223 #elif defined( SYS_LINUX ) || defined( WIN32 )
225 /* Allocate structure */
226 p_main->p_channel = malloc( sizeof( input_channel_t ) );
227 if( p_main->p_channel == NULL )
229 intf_ErrMsg( "network error: could not create channel bank" );
233 /* Initialize structure */
234 p_main->p_channel->i_channel = 0;
235 p_main->p_channel->last_change = 0;
237 intf_Msg( "network: channels initialized" );
241 intf_ErrMsg( "network error : channels not supported" );
247 /*****************************************************************************
248 * network_ChannelJoin: join a channel
249 *****************************************************************************
250 * This function will try to join a channel. If the relevant interface is
251 * already on the good channel, nothing will be done. Else, and if possible
252 * (if the interface is not locked), the channel server will be contacted
253 * and a change will be requested. The function will block until the change
254 * is effective. Note that once a channel is no more used, it's interface
255 * should be unlocked using input_ChannelLeave().
256 * Non 0 will be returned in case of error.
257 *****************************************************************************/
258 int network_ChannelJoin( int i_channel )
260 /* I still prefer to put BeOS a bit apart */
261 #if defined( SYS_BEOS )
262 intf_ErrMsg( "network error: channels are not yet supported under BeOS" );
265 #elif defined( SYS_LINUX ) || defined( WIN32 )
268 struct sockaddr_in sa_server;
269 struct sockaddr_in sa_client;
270 unsigned int i_version = 12;
273 char i_mess_length = 80;
274 unsigned long int i_date;
275 struct timeval answer_delay;
280 char * psz_channel_server;
282 if( !main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
283 INPUT_NETWORK_CHANNEL_DEFAULT ) )
285 intf_ErrMsg( "network: channels disabled, to enable them, use the"
286 "--channels option" );
291 intf_DbgMsg( "network: ChannelJoin : %d", i_channel );
292 /* If last change is too recent, wait a while */
293 if( mdate() - p_main->p_channel->last_change < INPUT_CHANNEL_CHANGE_DELAY )
295 intf_WarnMsg( 2, "network: waiting before changing channel" );
296 mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY );
299 p_main->p_channel->last_change = mdate();
300 p_main->p_channel->i_channel = i_channel;
302 intf_WarnMsg( 2, "network: joining channel %d", i_channel );
305 * Initializing the socket
307 i_socket = socket( AF_INET, SOCK_DGRAM, 0 );
310 * Getting the server's information
312 intf_WarnMsg( 6, "Channel server: %s port: %d",
313 main_GetPszVariable( INPUT_CHANNEL_SERVER_VAR,
314 INPUT_CHANNEL_SERVER_DEFAULT ),
315 main_GetIntVariable( INPUT_CHANNEL_PORT_VAR,
316 INPUT_CHANNEL_PORT_DEFAULT ) );
318 memset( &sa_server, 0x00, sizeof(struct sockaddr_in) );
319 sa_server.sin_family = AF_INET;
320 sa_server.sin_port = htons( main_GetIntVariable( INPUT_CHANNEL_PORT_VAR,
321 INPUT_CHANNEL_PORT_DEFAULT ) );
323 psz_channel_server = strdup( main_GetPszVariable( INPUT_CHANNEL_SERVER_VAR,
324 INPUT_CHANNEL_SERVER_DEFAULT ) );
325 #ifdef HAVE_ARPA_INET_H
326 inet_aton( psz_channel_server, &sa_server.sin_addr );
328 sa_server.sin_addr.s_addr = inet_addr( psz_channel_server );
330 free( psz_channel_server );
333 * Looking for the interface MAC address
335 if( GetMacAddress( i_socket, psz_mac ) )
337 intf_ErrMsg( "network error: failed getting MAC address" );
342 * Getting date of the client in seconds
344 i_date = mdate() / 1000000;
345 intf_DbgMsg( "vlcs: date %lu", i_date );
348 * Build of the message
350 sprintf( psz_mess, "%d %u %lu %s \n",
351 i_channel, i_version, i_date, psz_mac );
353 intf_DbgMsg( "vlcs: The message is %s", psz_mess );
358 memset( &sa_client, 0x00, sizeof(struct sockaddr_in) );
359 sa_client.sin_family = AF_INET;
360 sa_client.sin_port = htons(4312);
361 sa_client.sin_addr.s_addr = INADDR_ANY;
362 i_fromlen = sizeof( struct sockaddr );
363 i_rc = bind( i_socket, (struct sockaddr *)(&sa_client),\
364 sizeof(struct sockaddr) );
367 intf_ErrMsg( "vlcs: Unable to bind socket:%u ", i_rc );
368 /* TODO put CS_R_BIND in types.h*/
369 /* return CS_R_SOCKET;*/
376 sendto( i_socket, psz_mess, i_mess_length, 0, \
377 (struct sockaddr *)(&sa_server), \
378 sizeof(struct sockaddr) );
381 * Waiting 5 sec for one answer from the server
383 answer_delay.tv_sec = 5;
384 answer_delay.tv_usec = 0;
386 FD_SET( i_socket, &fds );
387 i_nbanswer = select( i_socket + 1, &fds, NULL, NULL, &answer_delay );
392 intf_DbgMsg( "vlcs: no answer" );
396 intf_DbgMsg( "vlcs: unable to receive the answer ");
400 recvfrom( i_socket, &i_answer, sizeof(char), 0,\
401 (struct sockaddr *)(&sa_client), &i_fromlen);
403 intf_DbgMsg( "vlcs: the answer : %i", i_answer );
408 intf_DbgMsg( "vlcs: the server failed to create the thread" );
411 intf_DbgMsg( "vlcs: the server tries to change the channel" );
414 intf_DbgMsg( "vlcs: unknown answer !" );
428 intf_ErrMsg( "network error: channels not supported" );
434 /* Following functions are local */
436 /*****************************************************************************
437 * GetMacAddress: extract the MAC Address
438 *****************************************************************************/
439 static int GetMacAddress( int i_socket, char *psz_mac )
441 #if defined( SYS_LINUX )
442 struct ifreq interface;
446 * Looking for information about the eth0 interface
448 interface.ifr_addr.sa_family = AF_INET;
449 strcpy( interface.ifr_name, INPUT_IFACE_DEFAULT );
451 i_ret = ioctl( i_socket, SIOCGIFHWADDR, &interface );
455 intf_ErrMsg( "network error: ioctl SIOCGIFHWADDR failed" );
459 sprintf( psz_mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
460 interface.ifr_hwaddr.sa_data[0] & 0xff,
461 interface.ifr_hwaddr.sa_data[1] & 0xff,
462 interface.ifr_hwaddr.sa_data[2] & 0xff,
463 interface.ifr_hwaddr.sa_data[3] & 0xff,
464 interface.ifr_hwaddr.sa_data[4] & 0xff,
465 interface.ifr_hwaddr.sa_data[5] & 0xff );
469 #elif defined( WIN32 )
472 /* Get adapter list - support for more than one adapter */
473 LANA_ENUM AdapterList;
476 intf_WarnMsg( 2, "network: looking for MAC address" );
478 memset( &Ncb, 0, sizeof( NCB ) );
479 Ncb.ncb_command = NCBENUM;
480 Ncb.ncb_buffer = (unsigned char *)&AdapterList;
481 Ncb.ncb_length = sizeof( AdapterList );
484 /* Get all of the local ethernet addresses */
485 for ( i = 0; i < AdapterList.length ; ++i )
487 if ( GetAdapterInfo ( AdapterList.lana[ i ], psz_mac ) == 0 )
502 /*****************************************************************************
503 * GetAdapterInfo : gets some informations about the interface using NETBIOS
504 *****************************************************************************/
505 static int GetAdapterInfo( int i_adapter, char *psz_string )
509 ADAPTER_STATUS adapt;
510 NAME_BUFFER psz_name[30];
513 /* Reset the LAN adapter so that we can begin querying it */
515 memset( &Ncb, 0, sizeof ( Ncb ) );
516 Ncb.ncb_command = NCBRESET;
517 Ncb.ncb_lana_num = i_adapter;
519 if( Netbios( &Ncb ) != NRC_GOODRET )
521 intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode );
525 /* Prepare to get the adapter status block */
526 memset( &Ncb, 0, sizeof( Ncb ) ) ; /* Initialization */
527 Ncb.ncb_command = NCBASTAT;
528 Ncb.ncb_lana_num = i_adapter;
530 strcpy( (char *)Ncb.ncb_callname, "*" );
532 memset( &Adapter, 0, sizeof ( Adapter ) );
533 Ncb.ncb_buffer = ( unsigned char * ) &Adapter;
534 Ncb.ncb_length = sizeof ( Adapter );
536 /* Get the adapter's info and, if this works, return it in standard,
537 * colon-delimited form. */
538 if ( Netbios( &Ncb ) == 0 )
540 sprintf ( psz_string, "%02X:%02X:%02X:%02X:%02X:%02X",
541 (int) ( Adapter.adapt.adapter_address[0] ),
542 (int) ( Adapter.adapt.adapter_address[1] ),
543 (int) ( Adapter.adapt.adapter_address[2] ),
544 (int) ( Adapter.adapt.adapter_address[3] ),
545 (int) ( Adapter.adapt.adapter_address[4] ),
546 (int) ( Adapter.adapt.adapter_address[5] ) );
548 intf_WarnMsg( 2, "network: found MAC address %s", psz_string );
554 intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode );