1 /*****************************************************************************
2 * netutils.c: various network functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001 VideoLAN
5 * $Id: netutils.c,v 1.25 2001/04/17 20:43:41 marcari Exp $
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Benoit Steiner <benny@via.ecp.fr>
9 * Henri Fallon <henri@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <netdb.h> /* gethostbyname() */
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 */
38 #include <netinet/in.h> /* BSD: struct in_addr */
39 #include <sys/socket.h> /* BSD: struct sockaddr */
40 #ifdef HAVE_ARPA_INET_H
41 #include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
45 #include <sys/ioctl.h> /* ioctl() */
48 #if defined (HAVE_NET_IF_H)
49 #include <net/if.h> /* interface (arch-dependent) */
52 #ifdef HAVE_SYS_SOCKIO_H
53 #include <sys/sockio.h>
68 /*****************************************************************************
69 * input_channel_t: channel library data
70 *****************************************************************************
71 * Store global channel library data.
72 * The part of the code concerning the channel changing process is unstable
73 * as it depends on the VideoLAN channel server, which isn't frozen for
75 *****************************************************************************/
76 typedef struct input_channel_s
78 int i_channel_id; /* current channel number */
79 mtime_t last_change; /* last change date */
83 /*****************************************************************************
84 * network_BuildLocalAddr : fill a sockaddr_in structure for local binding
85 *****************************************************************************/
86 int network_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port,
87 boolean_t b_broadcast )
89 char psz_hostname[INPUT_MAX_SOURCE_LENGTH];
90 struct hostent * p_hostent;
93 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
94 p_socket->sin_family = AF_INET; /* family */
95 p_socket->sin_port = htons( i_port );
98 /* Try to get our own IP */
99 if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
101 intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
109 /* Instead of trying to find the broadcast address using non-portable
110 * ioctl, let's bind INADDR_ANY */
111 strncpy(psz_hostname,"0.0.0.0",sizeof(psz_hostname));
114 /* Try to convert address directly from in_addr - this will work if
115 * psz_in_addr is dotted decimal. */
116 #ifdef HAVE_ARPA_INET_H
117 if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
119 if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
122 /* We have a fqdn, try to find its address */
123 if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
125 intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
129 /* Copy the first address of the host in the socket address */
130 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
131 p_hostent->h_length );
136 /*****************************************************************************
137 * network_BuildRemoteAddr : fill a sockaddr_in structure for remote host
138 *****************************************************************************/
139 int network_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
141 struct hostent * p_hostent;
143 /* Reset structure */
144 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
145 p_socket->sin_family = AF_INET; /* family */
146 p_socket->sin_port = htons( 0 ); /* This is for remote end */
148 /* Try to convert address directly from in_addr - this will work if
149 * psz_in_addr is dotted decimal. */
151 #ifdef HAVE_ARPA_INET_H
152 if( !inet_aton( psz_server, &p_socket->sin_addr) )
154 if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
157 /* We have a fqdn, try to find its address */
158 if ( (p_hostent = gethostbyname(psz_server)) == NULL )
160 intf_ErrMsg( "BuildRemoteAddr: unknown host %s",
165 /* Copy the first address of the host in the socket address */
166 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
167 p_hostent->h_length );
172 /*****************************************************************************
173 * network_ChannelCreate: initialize global channel method data
174 *****************************************************************************
175 * Initialize channel input method global data. This function should be called
176 * once before any input thread is created or any call to other
177 * input_Channel*() function is attempted.
178 *****************************************************************************/
179 int network_ChannelCreate( void )
181 /* Even when BSD are supported, BeOS is not likely to be supported, so
182 * I prefer to put it apart */
184 intf_ErrMsg( "error: channel changing is not yet supported under BeOS" );
187 /* FIXME : channels handling only work for linux */
189 /* Allocate structure */
190 p_main->p_channel = malloc( sizeof( input_channel_t ) );
191 if( p_main->p_channel == NULL )
193 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
197 /* Initialize structure */
198 p_main->p_channel->i_channel_id = 0;
199 p_main->p_channel->last_change = 0;
201 intf_Msg("Channels initialized\n");
204 intf_ErrMsg( "error : channel changing only works with linux yest" );
206 #endif /* SYS_LINUX */
207 #endif /* SYS_BEOS */
210 /*****************************************************************************
211 * network_ChannelJoin: join a channel
212 *****************************************************************************
213 * This function will try to join a channel. If the relevant interface is
214 * already on the good channel, nothing will be done. Else, and if possible
215 * (if the interface is not locked), the channel server will be contacted
216 * and a change will be requested. The function will block until the change
217 * is effective. Note that once a channel is no more used, it's interface
218 * should be unlocked using input_ChannelLeave().
219 * Non 0 will be returned in case of error.
220 *****************************************************************************/
221 int network_ChannelJoin( int i_channel_id )
223 /* I still prefer to put BeOS a bit apart */
225 intf_ErrMsg( "Channels are not yet supported under BeOS" );
231 struct ifreq s_interface;
232 struct sockaddr_in sa_server;
233 struct sockaddr_in sa_client;
234 unsigned int i_version = 12;
236 char i_mess_length = 80;
237 struct timeval * p_date_cl;
238 struct timeval s_time;
239 long unsigned int i_date;
246 intf_DbgMsg( "ChannelJoin : %d", i_channel_id );
247 /* If last change is too recent, wait a while */
248 if( mdate() - p_main->p_channel->last_change < INPUT_CHANNEL_CHANGE_DELAY )
250 intf_Msg( "Waiting before changing channel...\n" );
251 mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY );
253 p_main->p_channel->last_change = mdate();
254 p_main->p_channel->i_channel_id = i_channel_id;
256 intf_Msg( "Joining channel %d\n", i_channel_id );
259 * Looking for information about the eth0 interface
261 s_interface.ifr_addr.sa_family = AF_INET;
262 strcpy( s_interface.ifr_name, INPUT_IFACE_DEFAULT );
266 * Initialysing the socket
268 i_socket_cl=socket( AF_INET, SOCK_DGRAM, 0 );
272 * Getting the server's information
274 bzero( &sa_server, sizeof(struct sockaddr_in) );
275 sa_server.sin_family = AF_INET;
276 sa_server.sin_port = htons( INPUT_CHANNEL_PORT_DEFAULT );
277 inet_aton( INPUT_CHANNEL_SERVER_DEFAULT, &(sa_server.sin_addr) );
280 * Looking for the interface MAC address
282 ioctl( i_socket_cl, SIOCGIFHWADDR, &s_interface );
284 "CHANNELSERVER: macaddr == %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
285 s_interface.ifr_hwaddr.sa_data[0] & 0xff,
286 s_interface.ifr_hwaddr.sa_data[1] & 0xff,
287 s_interface.ifr_hwaddr.sa_data[2] & 0xff,
288 s_interface.ifr_hwaddr.sa_data[3] & 0xff,
289 s_interface.ifr_hwaddr.sa_data[4] & 0xff,
290 s_interface.ifr_hwaddr.sa_data[5] & 0xff );
293 * Getting date of the client
295 p_date_cl=malloc( sizeof(struct timeval) );
296 if( p_date_cl == NULL )
298 intf_ErrMsg( "CHANNELSERVER: unable to allocate memory\n" );
299 /* return VS_R_MEMORY;*/
303 if ( gettimeofday( p_date_cl, 0 ) == -1 )
307 i_date = p_date_cl->tv_sec;
309 intf_DbgMsg( "CHANNELSERVER: date %lu\n", i_date );
313 * Build of the message
315 sprintf( psz_mess, "%d %u %lu %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x \n",
316 i_channel_id, i_version, i_date,
317 s_interface.ifr_hwaddr.sa_data[0] & 0xff,
318 s_interface.ifr_hwaddr.sa_data[1] & 0xff,
319 s_interface.ifr_hwaddr.sa_data[2] & 0xff,
320 s_interface.ifr_hwaddr.sa_data[3] & 0xff,
321 s_interface.ifr_hwaddr.sa_data[4] & 0xff,
322 s_interface.ifr_hwaddr.sa_data[5] & 0xff );
324 intf_DbgMsg( "CHANNELSERVER: The message is %s\n", psz_mess );
329 bzero( &sa_client, sizeof(struct sockaddr_in) );
330 sa_client.sin_family = AF_INET;
331 sa_client.sin_port = htons(4312);
332 sa_client.sin_addr.s_addr = INADDR_ANY;
333 i_fromlen = sizeof( struct sockaddr );
334 i_rc = bind( i_socket_cl, (struct sockaddr *)(&sa_client),\
335 sizeof(struct sockaddr) );
338 intf_ErrMsg( "CHANNELSERVER: Unable to bind socket:%u\n", i_rc );
339 /* TODO put CS_R_BIND in types.h*/
340 /* return CS_R_SOCKET;*/
348 sendto( i_socket_cl, psz_mess, i_mess_length, 0, \
349 (struct sockaddr *)(&sa_server), \
350 sizeof(struct sockaddr) );
353 * Waiting 5 sec for one answer from the server
358 FD_SET( i_socket_cl, &s_rfds );
359 i_nbanswer = select( i_socket_cl+1, &s_rfds, NULL, NULL, &s_time );
360 if( i_nbanswer == 0 )
362 intf_DbgMsg( "CHANNELSERVER: no answer\n" );
364 else if( i_nbanswer == -1 )
366 intf_DbgMsg( "CHANNELSERVER: Unable to receive the answer\n ");
370 recvfrom( i_socket_cl, &i_answer, sizeof(char), 0,\
371 (struct sockaddr *)(&sa_client), &i_fromlen);
372 intf_DbgMsg( "CHANNELSERVER: the answer : %hhd\n", i_answer );
376 "CHANNELSERVER: The server failed to create the thread\n" );
378 else if( i_answer == 0 )
381 "CHANNELSERVER: The server tries to change the channel\n" );
385 intf_DbgMsg( "CHANNELSERVER: Unknown answer !\n" );
392 close( i_socket_cl );
395 #else /* SYS_LINUX */
396 intf_ErrMsg( "Channel only work under linux yet" );
397 #endif /* SYS_LINUX */
399 #endif /* SYS_BEOS */