1 /*****************************************************************************
2 * netutils.c: various network functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001 VideoLAN
5 * $Id: netutils.c,v 1.38 2001/10/03 02:20:39 tcastley 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 #elif defined( SYS_LINUX ) || defined( WIN32 )
223 /* FIXME : channels handling only work for linux */
224 /* Allocate structure */
225 p_main->p_channel = malloc( sizeof( input_channel_t ) );
226 if( p_main->p_channel == NULL )
228 intf_ErrMsg( "network error: could not create channel bank" );
232 /* Initialize structure */
233 p_main->p_channel->i_channel = 0;
234 p_main->p_channel->last_change = 0;
236 intf_Msg( "network: channels initialized" );
240 intf_ErrMsg( "network error : channels not supported" );
246 /*****************************************************************************
247 * network_ChannelJoin: join a channel
248 *****************************************************************************
249 * This function will try to join a channel. If the relevant interface is
250 * already on the good channel, nothing will be done. Else, and if possible
251 * (if the interface is not locked), the channel server will be contacted
252 * and a change will be requested. The function will block until the change
253 * is effective. Note that once a channel is no more used, it's interface
254 * should be unlocked using input_ChannelLeave().
255 * Non 0 will be returned in case of error.
256 *****************************************************************************/
257 int network_ChannelJoin( int i_channel )
259 /* I still prefer to put BeOS a bit apart */
260 #if defined( SYS_BEOS )
261 intf_ErrMsg( "network error: channels are not yet supported under BeOS" );
264 #elif defined( SYS_LINUX ) || defined( WIN32 )
267 struct sockaddr_in sa_server;
268 struct sockaddr_in sa_client;
269 unsigned int i_version = 12;
272 char i_mess_length = 80;
273 unsigned long int i_date;
274 struct timeval answer_delay;
279 char * psz_channel_server;
281 if( !main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
282 INPUT_NETWORK_CHANNEL_DEFAULT ) )
284 intf_ErrMsg( "network: channels disabled, to enable them, use the"
285 "--channels option" );
290 intf_DbgMsg( "network: ChannelJoin : %d", i_channel );
291 /* If last change is too recent, wait a while */
292 if( mdate() - p_main->p_channel->last_change < INPUT_CHANNEL_CHANGE_DELAY )
294 intf_WarnMsg( 2, "network: waiting before changing channel" );
295 mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY );
298 p_main->p_channel->last_change = mdate();
299 p_main->p_channel->i_channel = i_channel;
301 intf_WarnMsg( 2, "network: joining channel %d", i_channel );
304 * Initializing the socket
306 i_socket = socket( AF_INET, SOCK_DGRAM, 0 );
309 * Getting the server's information
311 intf_WarnMsg( 6, "Channel server: %s port: %d",
312 main_GetPszVariable( INPUT_CHANNEL_SERVER_VAR,
313 INPUT_CHANNEL_SERVER_DEFAULT ),
314 main_GetIntVariable( INPUT_CHANNEL_PORT_VAR,
315 INPUT_CHANNEL_PORT_DEFAULT ) );
317 memset( &sa_server, 0x00, sizeof(struct sockaddr_in) );
318 sa_server.sin_family = AF_INET;
319 sa_server.sin_port = htons( main_GetIntVariable( INPUT_CHANNEL_PORT_VAR,
320 INPUT_CHANNEL_PORT_DEFAULT ) );
322 psz_channel_server = strdup( main_GetPszVariable( INPUT_CHANNEL_SERVER_VAR,
323 INPUT_CHANNEL_SERVER_DEFAULT ) );
324 #ifdef HAVE_ARPA_INET_H
325 inet_aton( psz_channel_server, &sa_server.sin_addr );
327 sa_server.sin_addr.s_addr = inet_addr( psz_channel_server );
329 free( psz_channel_server );
332 * Looking for the interface MAC address
334 if( GetMacAddress( i_socket, psz_mac ) )
336 intf_ErrMsg( "network error: failed getting MAC address" );
341 * Getting date of the client in seconds
343 i_date = mdate() / 1000000;
344 intf_DbgMsg( "vlcs: date %lu", i_date );
347 * Build of the message
349 sprintf( psz_mess, "%d %u %lu %s \n",
350 i_channel, i_version, i_date, psz_mac );
352 intf_DbgMsg( "vlcs: The message is %s", psz_mess );
357 memset( &sa_client, 0x00, sizeof(struct sockaddr_in) );
358 sa_client.sin_family = AF_INET;
359 sa_client.sin_port = htons(4312);
360 sa_client.sin_addr.s_addr = INADDR_ANY;
361 i_fromlen = sizeof( struct sockaddr );
362 i_rc = bind( i_socket, (struct sockaddr *)(&sa_client),\
363 sizeof(struct sockaddr) );
366 intf_ErrMsg( "vlcs: Unable to bind socket:%u ", i_rc );
367 /* TODO put CS_R_BIND in types.h*/
368 /* return CS_R_SOCKET;*/
375 sendto( i_socket, psz_mess, i_mess_length, 0, \
376 (struct sockaddr *)(&sa_server), \
377 sizeof(struct sockaddr) );
380 * Waiting 5 sec for one answer from the server
382 answer_delay.tv_sec = 5;
383 answer_delay.tv_usec = 0;
385 FD_SET( i_socket, &fds );
386 i_nbanswer = select( i_socket + 1, &fds, NULL, NULL, &answer_delay );
391 intf_DbgMsg( "vlcs: no answer" );
395 intf_DbgMsg( "vlcs: unable to receive the answer ");
399 recvfrom( i_socket, &i_answer, sizeof(char), 0,\
400 (struct sockaddr *)(&sa_client), &i_fromlen);
402 intf_DbgMsg( "vlcs: the answer : %i", i_answer );
407 intf_DbgMsg( "vlcs: the server failed to create the thread" );
410 intf_DbgMsg( "vlcs: the server tries to change the channel" );
413 intf_DbgMsg( "vlcs: unknown answer !" );
427 intf_ErrMsg( "network error: channels not supported" );
433 /* Following functions are local */
435 /*****************************************************************************
436 * GetMacAddress: extract the MAC Address
437 *****************************************************************************/
438 static int GetMacAddress( int i_socket, char *psz_mac )
440 #if defined( SYS_LINUX )
441 struct ifreq interface;
445 * Looking for information about the eth0 interface
447 interface.ifr_addr.sa_family = AF_INET;
448 strcpy( interface.ifr_name, INPUT_IFACE_DEFAULT );
450 i_ret = ioctl( i_socket, SIOCGIFHWADDR, &interface );
454 intf_ErrMsg( "network error: ioctl SIOCGIFHWADDR failed" );
458 sprintf( psz_mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
459 interface.ifr_hwaddr.sa_data[0] & 0xff,
460 interface.ifr_hwaddr.sa_data[1] & 0xff,
461 interface.ifr_hwaddr.sa_data[2] & 0xff,
462 interface.ifr_hwaddr.sa_data[3] & 0xff,
463 interface.ifr_hwaddr.sa_data[4] & 0xff,
464 interface.ifr_hwaddr.sa_data[5] & 0xff );
468 #elif defined( WIN32 )
471 /* Get adapter list - support for more than one adapter */
472 LANA_ENUM AdapterList;
475 intf_WarnMsg( 2, "network: looking for MAC address" );
477 memset( &Ncb, 0, sizeof( NCB ) );
478 Ncb.ncb_command = NCBENUM;
479 Ncb.ncb_buffer = (unsigned char *)&AdapterList;
480 Ncb.ncb_length = sizeof( AdapterList );
483 /* Get all of the local ethernet addresses */
484 for ( i = 0; i < AdapterList.length ; ++i )
486 if ( GetAdapterInfo ( AdapterList.lana[ i ], psz_mac ) == 0 )
501 /*****************************************************************************
502 * GetAdapterInfo : gets some informations about the interface using NETBIOS
503 *****************************************************************************/
504 static int GetAdapterInfo( int i_adapter, char *psz_string )
508 ADAPTER_STATUS adapt;
509 NAME_BUFFER psz_name[30];
512 /* Reset the LAN adapter so that we can begin querying it */
514 memset( &Ncb, 0, sizeof ( Ncb ) );
515 Ncb.ncb_command = NCBRESET;
516 Ncb.ncb_lana_num = i_adapter;
518 if( Netbios( &Ncb ) != NRC_GOODRET )
520 intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode );
524 /* Prepare to get the adapter status block */
525 memset( &Ncb, 0, sizeof( Ncb ) ) ; /* Initialization */
526 Ncb.ncb_command = NCBASTAT;
527 Ncb.ncb_lana_num = i_adapter;
529 strcpy( (char *)Ncb.ncb_callname, "*" );
531 memset( &Adapter, 0, sizeof ( Adapter ) );
532 Ncb.ncb_buffer = ( unsigned char * ) &Adapter;
533 Ncb.ncb_length = sizeof ( Adapter );
535 /* Get the adapter's info and, if this works, return it in standard,
536 * colon-delimited form. */
537 if ( Netbios( &Ncb ) == 0 )
539 sprintf ( psz_string, "%02X:%02X:%02X:%02X:%02X:%02X",
540 (int) ( Adapter.adapt.adapter_address[0] ),
541 (int) ( Adapter.adapt.adapter_address[1] ),
542 (int) ( Adapter.adapt.adapter_address[2] ),
543 (int) ( Adapter.adapt.adapter_address[3] ),
544 (int) ( Adapter.adapt.adapter_address[4] ),
545 (int) ( Adapter.adapt.adapter_address[5] ) );
547 intf_WarnMsg( 2, "network: found MAC address %s", psz_string );
553 intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode );