]> git.sesse.net Git - vlc/blob - src/misc/netutils.c
Repaired broadcast support : binding INADDR_ANY doesn't seem to work.
[vlc] / src / misc / netutils.c
1 /*****************************************************************************
2  * netutils.c: various network functions
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  * $Id: netutils.c,v 1.27 2001/04/27 18:07:57 henri Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Benoit Steiner <benny@via.ecp.fr>
9  *          Henri Fallon <henri@videolan.org>
10  *
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.
15  * 
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "defs.h"
30
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 */
37
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() */
42 #endif
43
44 #ifdef SYS_LINUX
45 #include <sys/ioctl.h>                                            /* ioctl() */
46 #endif
47
48 #ifdef HAVE_NET_IF_H
49 #include <net/if.h>                            /* interface (arch-dependent) */
50 #endif
51
52 #ifdef HAVE_SYS_SOCKIO_H
53 #include <sys/sockio.h>
54 #endif
55
56 #include "config.h"
57 #include "common.h"
58 #include "mtime.h"
59 #include "threads.h"
60 #include "main.h"
61
62 #include "intf_msg.h"
63
64 #include "netutils.h"
65
66
67
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
74  * the time being.
75  *****************************************************************************/
76 typedef struct input_channel_s
77 {
78     int         i_channel_id;                      /* current channel number */
79     mtime_t     last_change;                             /* last change date */
80 } input_channel_t;
81
82
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                             char * psz_broadcast )
88 {
89     char                psz_hostname[INPUT_MAX_SOURCE_LENGTH];
90     struct hostent    * p_hostent;
91     
92     /* Reset struct */
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 );
96     if( psz_broadcast == NULL )
97     {
98         /* Try to get our own IP */
99         if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
100         {
101             intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
102                          strerror( errno ) );
103             return( -1 );
104         }
105
106     }
107     else
108     {   
109         /* I didn't manage to make INADDR_ANYT work, even with setsockopt 
110          * so, as it's kludgy to try and determine the broadcast addr
111          * it is passed as an argument in the command line */
112         strncpy( psz_hostname, psz_broadcast, INPUT_MAX_SOURCE_LENGTH );
113     }
114
115     /* Try to convert address directly from in_addr - this will work if
116      * psz_in_addr is dotted decimal. */
117 #ifdef HAVE_ARPA_INET_H
118     if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
119 #else
120     if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
121 #endif
122     {
123         /* We have a fqdn, try to find its address */
124         if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
125         {
126             intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
127             return( -1 );
128         }
129         
130         /* Copy the first address of the host in the socket address */
131         memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0], 
132                  p_hostent->h_length );
133     }
134     return( 0 );
135 }
136
137 /*****************************************************************************
138  * network_BuildRemoteAddr : fill a sockaddr_in structure for remote host
139  *****************************************************************************/
140 int network_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
141 {
142     struct hostent            * p_hostent;
143
144     /* Reset structure */
145     memset( p_socket, 0, sizeof( struct sockaddr_in ) );
146     p_socket->sin_family = AF_INET;                                 /* family */
147     p_socket->sin_port = htons( 0 );                /* This is for remote end */
148     
149      /* Try to convert address directly from in_addr - this will work if
150       * psz_in_addr is dotted decimal. */
151
152 #ifdef HAVE_ARPA_INET_H
153     if( !inet_aton( psz_server, &p_socket->sin_addr) )
154 #else
155     if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
156 #endif
157     {
158         /* We have a fqdn, try to find its address */
159         if ( (p_hostent = gethostbyname(psz_server)) == NULL )
160         {
161             intf_ErrMsg( "BuildRemoteAddr: unknown host %s", 
162                          psz_server );
163             return( -1 );
164         }
165         
166         /* Copy the first address of the host in the socket address */
167         memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0], 
168                  p_hostent->h_length );
169     }
170     return( 0 );
171 }
172
173 /*****************************************************************************
174  * network_ChannelCreate: initialize global channel method data
175  *****************************************************************************
176  * Initialize channel input method global data. This function should be called
177  * once before any input thread is created or any call to other 
178  * input_Channel*() function is attempted.
179  *****************************************************************************/
180 int network_ChannelCreate( void )
181 {
182 /* Even when BSD are supported, BeOS is not likely to be supported, so 
183  * I prefer to put it apart */    
184 #if defined( SYS_BEOS )
185     intf_ErrMsg( "error: channel changing is not yet supported under BeOS" );
186     return( 1 );
187
188 #elif defined( SYS_LINUX )
189 /* FIXME : channels handling only work for linux */
190     /* Allocate structure */
191     p_main->p_channel = malloc( sizeof( input_channel_t ) );
192     if( p_main->p_channel == NULL )
193     {
194         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
195         return( -1 );
196     }
197
198     /* Initialize structure */
199     p_main->p_channel->i_channel_id   = 0;
200     p_main->p_channel->last_change = 0;
201
202     intf_Msg("Channels initialized\n");
203     return( 0 );
204
205 #else
206     intf_ErrMsg( "error : channel changing only works with linux yest" );
207     return( 1 );
208
209 #endif
210 }
211
212 /*****************************************************************************
213  * network_ChannelJoin: join a channel
214  *****************************************************************************
215  * This function will try to join a channel. If the relevant interface is 
216  * already on the good channel, nothing will be done. Else, and if possible 
217  * (if the interface is not locked), the channel server will be contacted 
218  * and a change will be requested. The function will block until the change 
219  * is effective. Note that once a channel is no more used, it's interface 
220  * should be unlocked using input_ChannelLeave().
221  * Non 0 will be returned in case of error.
222  *****************************************************************************/
223 int network_ChannelJoin( int i_channel_id )
224 {
225 /* I still prefer to put BeOS a bit apart */   
226 #if defined( SYS_BEOS )
227     intf_ErrMsg( "Channels are not yet supported under BeOS" );
228     return( -1 );
229
230 #elif defined( SYS_LINUX )
231     int                 i_socket_cl;
232     int                 i_fromlen;
233     struct ifreq        s_interface;
234     struct sockaddr_in  sa_server;
235     struct sockaddr_in  sa_client;
236     unsigned int        i_version = 12;
237     char                psz_mess[80];
238     char                i_mess_length = 80;
239     struct timeval *    p_date_cl;
240     struct timeval      s_time;
241     long unsigned int   i_date;
242     int                 i_nbanswer;
243     char                i_answer;
244     fd_set              s_rfds;
245     unsigned int        i_rc;
246  
247     if( ! p_main->b_channels )
248     {
249         intf_ErrMsg( "Channels disabled. To enable them, use the --channels"
250                      " option" );
251         return( -1 );
252     }
253     /* debug */ 
254     intf_DbgMsg( "ChannelJoin : %d", i_channel_id );
255     /* If last change is too recent, wait a while */
256     if( mdate() - p_main->p_channel->last_change < INPUT_CHANNEL_CHANGE_DELAY )
257     {
258         intf_Msg( "Waiting before changing channel...\n" );
259         mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY );
260     }
261     p_main->p_channel->last_change = mdate();
262     p_main->p_channel->i_channel_id = i_channel_id;
263
264     intf_Msg( "Joining channel %d\n", i_channel_id );
265
266     /*      
267      * Looking for information about the eth0 interface
268      */
269     s_interface.ifr_addr.sa_family = AF_INET;
270     strcpy( s_interface.ifr_name, INPUT_IFACE_DEFAULT );
271     
272     
273     /*
274      * Initialysing the socket
275      */
276     i_socket_cl=socket( AF_INET, SOCK_DGRAM, 0 );
277
278     
279     /* 
280      * Getting the server's information 
281      */
282     bzero( &sa_server, sizeof(struct sockaddr_in) );
283     sa_server.sin_family = AF_INET;
284     sa_server.sin_port   = htons( INPUT_CHANNEL_PORT_DEFAULT );
285     inet_aton( INPUT_CHANNEL_SERVER_DEFAULT, &(sa_server.sin_addr) );
286
287     /*
288      * Looking for the interface MAC address
289      */
290     ioctl( i_socket_cl, SIOCGIFHWADDR, &s_interface );
291     intf_DbgMsg(
292         "CHANNELSERVER: macaddr == %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
293         s_interface.ifr_hwaddr.sa_data[0] & 0xff,
294         s_interface.ifr_hwaddr.sa_data[1] & 0xff,
295         s_interface.ifr_hwaddr.sa_data[2] & 0xff,
296         s_interface.ifr_hwaddr.sa_data[3] & 0xff,
297         s_interface.ifr_hwaddr.sa_data[4] & 0xff,
298         s_interface.ifr_hwaddr.sa_data[5] & 0xff );
299     
300     /*
301      * Getting date of the client
302      */
303     p_date_cl=malloc( sizeof(struct timeval) );
304     if( p_date_cl == NULL )
305     {
306         intf_ErrMsg( "CHANNELSERVER: unable to allocate memory\n" );
307     /*    return VS_R_MEMORY;*/
308         return( -1);
309     }
310     
311     if ( gettimeofday( p_date_cl, 0 ) == -1 )
312     {
313         return( -1);
314     }
315     i_date = p_date_cl->tv_sec;
316     free( p_date_cl );
317     intf_DbgMsg( "CHANNELSERVER: date %lu\n", i_date );
318
319
320     /* 
321      * Build of the message
322      */
323     sprintf( psz_mess, "%d %u %lu %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x \n",
324           i_channel_id, i_version, i_date,
325         s_interface.ifr_hwaddr.sa_data[0] & 0xff, 
326         s_interface.ifr_hwaddr.sa_data[1] & 0xff,
327         s_interface.ifr_hwaddr.sa_data[2] & 0xff,
328         s_interface.ifr_hwaddr.sa_data[3] & 0xff,
329         s_interface.ifr_hwaddr.sa_data[4] & 0xff,
330         s_interface.ifr_hwaddr.sa_data[5] & 0xff );
331  
332     intf_DbgMsg( "CHANNELSERVER: The message is %s\n", psz_mess );
333
334     /*
335      * Open the socket 2
336      */
337     bzero( &sa_client, sizeof(struct sockaddr_in) );
338     sa_client.sin_family = AF_INET;
339     sa_client.sin_port   = htons(4312);
340     sa_client.sin_addr.s_addr = INADDR_ANY;
341     i_fromlen = sizeof( struct sockaddr );
342     i_rc = bind( i_socket_cl, (struct sockaddr *)(&sa_client),\
343                  sizeof(struct sockaddr) );
344     if ( i_rc )
345     {
346         intf_ErrMsg( "CHANNELSERVER: Unable to bind socket:%u\n", i_rc ); 
347     /* TODO put CS_R_BIND in types.h*/
348     /*    return CS_R_SOCKET;*/
349         return -1;
350     }
351
352
353     /*
354      * Send the message
355      */
356     sendto( i_socket_cl, psz_mess, i_mess_length, 0, \
357             (struct sockaddr *)(&sa_server),   \
358             sizeof(struct sockaddr) );
359    
360      /*
361      * Waiting 5 sec for one answer from the server
362      */
363     s_time.tv_sec  = 5;
364     s_time.tv_usec = 0;
365     FD_ZERO( &s_rfds );
366     FD_SET( i_socket_cl, &s_rfds );
367     i_nbanswer = select( i_socket_cl+1, &s_rfds, NULL, NULL, &s_time );
368     if( i_nbanswer == 0 )
369     {
370         intf_DbgMsg( "CHANNELSERVER: no answer\n" );
371     }
372     else if( i_nbanswer == -1 )
373     {
374         intf_DbgMsg( "CHANNELSERVER: Unable to receive the answer\n ");
375     }
376     else
377     {
378         recvfrom( i_socket_cl, &i_answer, sizeof(char), 0,\
379                   (struct sockaddr *)(&sa_client), &i_fromlen);
380         intf_DbgMsg( "CHANNELSERVER: the answer : %hhd\n", i_answer );
381         if( i_answer == -1 )
382         {
383             intf_DbgMsg(
384                   "CHANNELSERVER: The server failed to create the thread\n" );
385         }
386         else if( i_answer == 0 )
387         {
388             intf_DbgMsg(
389                   "CHANNELSERVER: The server tries to change the channel\n" );
390         }
391         else
392         {
393             intf_DbgMsg( "CHANNELSERVER: Unknown answer !\n" );
394         }
395     }
396     
397     /*
398      * Close the socket
399      */
400     close( i_socket_cl );
401
402     return( 0 );
403
404 #else
405     intf_ErrMsg( "Channels only work under linux yet" );
406     return( -1 );
407
408 #endif
409 }
410