1 /*****************************************************************************
2 * netutils.c: various network functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001 VideoLAN
5 * $Id: netutils.c,v 1.30 2001/05/18 09:49:16 xav 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> /* bzero(), bcopy() */
35 #include <unistd.h> /* gethostname() */
36 #include <sys/time.h> /* gettimeofday */
39 #include <netdb.h> /* gethostbyname() */
40 #include <netinet/in.h> /* BSD: struct in_addr */
41 #include <sys/socket.h> /* BSD: struct sockaddr */
44 #ifdef HAVE_ARPA_INET_H
45 #include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
49 #include <sys/ioctl.h> /* ioctl() */
52 #ifdef WIN32 /* tools to get the MAC adress from */
53 #include <windows.h> /* the interface under Windows */
61 #include <net/if.h> /* interface (arch-dependent) */
64 #ifdef HAVE_SYS_SOCKIO_H
65 #include <sys/sockio.h>
79 /*****************************************************************************
80 * input_channel_t: channel library data
81 *****************************************************************************
82 * Store global channel library data.
83 * The part of the code concerning the channel changing process is unstable
84 * as it depends on the VideoLAN channel server, which isn't frozen for
86 *****************************************************************************/
87 typedef struct input_channel_s
89 int i_channel_id; /* current channel number */
90 mtime_t last_change; /* last change date */
96 /*****************************************************************************
97 * GetAdapterInfo : gets some informations about the interface using NETBIOS *
98 *****************************************************************************/
101 using namespace std ;
103 bool GetAdapterInfo ( int nAdapterNum, string & sMAC )
105 /* Reset the LAN adapter so that we can begin querying it */
108 memset ( &Ncb, 0, sizeof ( Ncb ) ) ;
109 Ncb.ncb_command = NCBRESET ;
110 Ncb.ncb_lana_num = nAdapterNum ;
112 if ( Netbios ( &Ncb ) != NRC_GOODRET )
115 ostrstream outs ( acTemp, sizeof ( acTemp ) ) ;
117 /* FIXME This should use vlc's standard handling error functions */
119 outs << "error " << Ncb.ncb_retcode << " on reset" << ends ;
124 /* Prepare to get the adapter status block */
126 memset ( &Ncb, 0, sizeof ( Ncb ) ) ; /* Initialization */
127 Ncb.ncb_command = NCBASTAT ;
128 Ncb.ncb_lana_num = nAdapterNum ;
130 strcpy ( ( char * ) Ncb.ncb_callname, "*" ) ;
133 ADAPTER_STATUS adapt ;
134 NAME_BUFFER NameBuff[30] ;
137 memset ( &Adapter, 0, sizeof ( Adapter ) ) ;
138 Ncb.ncb_buffer = ( unsigned char * ) &Adapter ;
139 Ncb.ncb_length = sizeof ( Adapter ) ;
141 /* Get the adapter's info and, if this works, return it in standard,
142 colon-delimited form. */
144 if ( Netbios( &Ncb ) == 0 )
147 sprintf ( acMAC, "%02X:%02X:%02X:%02X:%02X:%02X",
148 int ( Adapter.adapt.adapter_address[0] ),
149 int ( Adapter.adapt.adapter_address[1] ),
150 int ( Adapter.adapt.adapter_address[2] ),
151 int ( Adapter.adapt.adapter_address[3] ),
152 int ( Adapter.adapt.adapter_address[4] ),
153 int ( Adapter.adapt.adapter_address[5] ) );
160 ostrstream outs ( acTemp, sizeof ( acTemp ) ) ;
161 /* FIXME Same thing as up there */
163 outs << "error " << Ncb.ncb_retcode << " on ASTAT" << ends;
170 /*****************************************************************************
171 * GetMacAddress : Extracts the MAC Address from the informations collected in
173 ****************************************************************************/
175 string GetMacAddress()
177 /* Get adapter list - support for more than one adapter */
179 LANA_ENUM AdapterList ;
182 memset ( &Ncb, 0, sizeof ( NCB ) ) ;
183 Ncb.ncb_command = NCBENUM ;
184 Ncb.ncb_buffer = ( unsigned char * ) &AdapterList ;
185 Ncb.ncb_length = sizeof ( AdapterList ) ;
188 /* Get all of the local ethernet addresses */
192 for ( int i = 0; i < AdapterList.length ; ++i )
194 if ( GetAdapterInfo ( AdapterList.lana [ i ] , sMAC ) )
196 cout << "Adapter " << int ( AdapterList. lana [ i ] ) <<
197 "'s MAC is " << sMAC << endl ;
202 /* FIXME those bloody error messages */
204 cerr << "Failed to get MAC address! Do you" << endl;
205 cerr << "have the NetBIOS protocol installed?" << endl;
216 /*****************************************************************************
217 * network_BuildLocalAddr : fill a sockaddr_in structure for local binding
218 *****************************************************************************/
219 int network_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port,
220 char * psz_broadcast )
222 char psz_hostname[INPUT_MAX_SOURCE_LENGTH];
223 struct hostent * p_hostent;
226 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
227 p_socket->sin_family = AF_INET; /* family */
228 p_socket->sin_port = htons( i_port );
229 if( psz_broadcast == NULL )
231 /* Try to get our own IP */
232 if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
234 intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
242 /* I didn't manage to make INADDR_ANYT work, even with setsockopt
243 * so, as it's kludgy to try and determine the broadcast addr
244 * it is passed as an argument in the command line */
245 strncpy( psz_hostname, psz_broadcast, INPUT_MAX_SOURCE_LENGTH );
248 /* Try to convert address directly from in_addr - this will work if
249 * psz_in_addr is dotted decimal. */
250 #ifdef HAVE_ARPA_INET_H
251 if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
253 if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
256 /* We have a fqdn, try to find its address */
257 if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
259 intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
263 /* Copy the first address of the host in the socket address */
264 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
265 p_hostent->h_length );
270 /*****************************************************************************
271 * network_BuildRemoteAddr : fill a sockaddr_in structure for remote host
272 *****************************************************************************/
273 int network_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
275 struct hostent * p_hostent;
277 /* Reset structure */
278 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
279 p_socket->sin_family = AF_INET; /* family */
280 p_socket->sin_port = htons( 0 ); /* This is for remote end */
282 /* Try to convert address directly from in_addr - this will work if
283 * psz_in_addr is dotted decimal. */
285 #ifdef HAVE_ARPA_INET_H
286 if( !inet_aton( psz_server, &p_socket->sin_addr) )
288 if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
291 /* We have a fqdn, try to find its address */
292 if ( (p_hostent = gethostbyname(psz_server)) == NULL )
294 intf_ErrMsg( "BuildRemoteAddr: unknown host %s",
299 /* Copy the first address of the host in the socket address */
300 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
301 p_hostent->h_length );
306 /*****************************************************************************
307 * network_ChannelCreate: initialize global channel method data
308 *****************************************************************************
309 * Initialize channel input method global data. This function should be called
310 * once before any input thread is created or any call to other
311 * input_Channel*() function is attempted.
312 *****************************************************************************/
313 int network_ChannelCreate( void )
315 /* Even when BSD are supported, BeOS is not likely to be supported, so
316 * I prefer to put it apart */
317 #if defined( SYS_BEOS )
318 intf_ErrMsg( "error: channel changing is not yet supported under BeOS" );
321 #elif defined( SYS_LINUX )
322 /* FIXME : channels handling only work for linux */
323 /* Allocate structure */
324 p_main->p_channel = malloc( sizeof( input_channel_t ) );
325 if( p_main->p_channel == NULL )
327 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
331 /* Initialize structure */
332 p_main->p_channel->i_channel_id = 0;
333 p_main->p_channel->last_change = 0;
335 intf_Msg("Channels initialized\n");
339 intf_ErrMsg( "error : channel changing only works with linux yest" );
345 /*****************************************************************************
346 * network_ChannelJoin: join a channel
347 *****************************************************************************
348 * This function will try to join a channel. If the relevant interface is
349 * already on the good channel, nothing will be done. Else, and if possible
350 * (if the interface is not locked), the channel server will be contacted
351 * and a change will be requested. The function will block until the change
352 * is effective. Note that once a channel is no more used, it's interface
353 * should be unlocked using input_ChannelLeave().
354 * Non 0 will be returned in case of error.
355 *****************************************************************************/
356 int network_ChannelJoin( int i_channel_id )
358 /* I still prefer to put BeOS a bit apart */
359 #if defined( SYS_BEOS )
360 intf_ErrMsg( "Channels are not yet supported under BeOS" );
363 #elif defined( SYS_LINUX )
366 struct ifreq s_interface;
367 struct sockaddr_in sa_server;
368 struct sockaddr_in sa_client;
369 unsigned int i_version = 12;
371 char i_mess_length = 80;
372 struct timeval * p_date_cl;
373 struct timeval s_time;
374 long unsigned int i_date;
380 if( ! p_main->b_channels )
382 intf_ErrMsg( "Channels disabled. To enable them, use the --channels"
387 intf_DbgMsg( "ChannelJoin : %d", i_channel_id );
388 /* If last change is too recent, wait a while */
389 if( mdate() - p_main->p_channel->last_change < INPUT_CHANNEL_CHANGE_DELAY )
391 intf_Msg( "Waiting before changing channel...\n" );
392 mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY );
394 p_main->p_channel->last_change = mdate();
395 p_main->p_channel->i_channel_id = i_channel_id;
397 intf_Msg( "Joining channel %d\n", i_channel_id );
400 * Looking for information about the eth0 interface
402 s_interface.ifr_addr.sa_family = AF_INET;
403 strcpy( s_interface.ifr_name, INPUT_IFACE_DEFAULT );
407 * Initialysing the socket
409 i_socket_cl=socket( AF_INET, SOCK_DGRAM, 0 );
413 * Getting the server's information
415 bzero( &sa_server, sizeof(struct sockaddr_in) );
416 sa_server.sin_family = AF_INET;
417 sa_server.sin_port = htons( INPUT_CHANNEL_PORT_DEFAULT );
418 inet_aton( INPUT_CHANNEL_SERVER_DEFAULT, &(sa_server.sin_addr) );
421 * Looking for the interface MAC address
423 ioctl( i_socket_cl, SIOCGIFHWADDR, &s_interface );
425 "CHANNELSERVER: macaddr == %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
426 s_interface.ifr_hwaddr.sa_data[0] & 0xff,
427 s_interface.ifr_hwaddr.sa_data[1] & 0xff,
428 s_interface.ifr_hwaddr.sa_data[2] & 0xff,
429 s_interface.ifr_hwaddr.sa_data[3] & 0xff,
430 s_interface.ifr_hwaddr.sa_data[4] & 0xff,
431 s_interface.ifr_hwaddr.sa_data[5] & 0xff );
434 * Getting date of the client
436 p_date_cl=malloc( sizeof(struct timeval) );
437 if( p_date_cl == NULL )
439 intf_ErrMsg( "CHANNELSERVER: unable to allocate memory\n" );
440 /* return VS_R_MEMORY;*/
444 if ( gettimeofday( p_date_cl, 0 ) == -1 )
448 i_date = p_date_cl->tv_sec;
450 intf_DbgMsg( "CHANNELSERVER: date %lu\n", i_date );
454 * Build of the message
456 sprintf( psz_mess, "%d %u %lu %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x \n",
457 i_channel_id, i_version, i_date,
458 s_interface.ifr_hwaddr.sa_data[0] & 0xff,
459 s_interface.ifr_hwaddr.sa_data[1] & 0xff,
460 s_interface.ifr_hwaddr.sa_data[2] & 0xff,
461 s_interface.ifr_hwaddr.sa_data[3] & 0xff,
462 s_interface.ifr_hwaddr.sa_data[4] & 0xff,
463 s_interface.ifr_hwaddr.sa_data[5] & 0xff );
465 intf_DbgMsg( "CHANNELSERVER: The message is %s\n", psz_mess );
470 bzero( &sa_client, sizeof(struct sockaddr_in) );
471 sa_client.sin_family = AF_INET;
472 sa_client.sin_port = htons(4312);
473 sa_client.sin_addr.s_addr = INADDR_ANY;
474 i_fromlen = sizeof( struct sockaddr );
475 i_rc = bind( i_socket_cl, (struct sockaddr *)(&sa_client),\
476 sizeof(struct sockaddr) );
479 intf_ErrMsg( "CHANNELSERVER: Unable to bind socket:%u\n", i_rc );
480 /* TODO put CS_R_BIND in types.h*/
481 /* return CS_R_SOCKET;*/
489 sendto( i_socket_cl, psz_mess, i_mess_length, 0, \
490 (struct sockaddr *)(&sa_server), \
491 sizeof(struct sockaddr) );
494 * Waiting 5 sec for one answer from the server
499 FD_SET( i_socket_cl, &s_rfds );
500 i_nbanswer = select( i_socket_cl+1, &s_rfds, NULL, NULL, &s_time );
501 if( i_nbanswer == 0 )
503 intf_DbgMsg( "CHANNELSERVER: no answer\n" );
505 else if( i_nbanswer == -1 )
507 intf_DbgMsg( "CHANNELSERVER: Unable to receive the answer\n ");
511 recvfrom( i_socket_cl, &i_answer, sizeof(char), 0,\
512 (struct sockaddr *)(&sa_client), &i_fromlen);
513 intf_DbgMsg( "CHANNELSERVER: the answer : %hhd\n", i_answer );
517 "CHANNELSERVER: The server failed to create the thread\n" );
519 else if( i_answer == 0 )
522 "CHANNELSERVER: The server tries to change the channel\n" );
526 intf_DbgMsg( "CHANNELSERVER: Unknown answer !\n" );
533 close( i_socket_cl );
538 intf_ErrMsg( "Channels only work under linux yet" );