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