]> git.sesse.net Git - vlc/blob - src/misc/netutils.c
- Ported the 0.2.0's channel changing functions
[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.23 2001/04/11 02:01:24 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 #if defined (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                             boolean_t b_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( !b_broadcast )
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         /* 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));
112     }
113
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) )
118 #else
119     if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
120 #endif
121     {
122         /* We have a fqdn, try to find its address */
123         if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
124         {
125             intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
126             return( -1 );
127         }
128         
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 );
132     }
133     return( 0 );
134 }
135
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 )
140 {
141     struct hostent            * p_hostent;
142
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 */
147     
148      /* Try to convert address directly from in_addr - this will work if
149       * psz_in_addr is dotted decimal. */
150
151 #ifdef HAVE_ARPA_INET_H
152     if( !inet_aton( psz_server, &p_socket->sin_addr) )
153 #else
154     if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
155 #endif
156     {
157         /* We have a fqdn, try to find its address */
158         if ( (p_hostent = gethostbyname(psz_server)) == NULL )
159         {
160             intf_ErrMsg( "BuildRemoteAddr: unknown host %s", 
161                          psz_server );
162             return( -1 );
163         }
164         
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 );
168     }
169     return( 0 );
170 }
171
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 )
180 {
181 /* Even when BSD are supported, BeOS is not likely to be supported, so 
182  * I prefer to put it apart */    
183 #ifdef SYS_BEOS
184     intf_ErrMsg( "error: channel changing is not yet supported under BeOS" );
185     return( 1 );
186 #else
187 /* FIXME : channels handling only work for linux */
188 #ifdef SYS_LINUX
189     /* Allocate structure */
190     p_main->p_channel = malloc( sizeof( input_channel_t ) );
191     if( p_main->p_channel == NULL )
192     {
193         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
194         return( -1 );
195     }
196
197     /* Initialize structure */
198     p_main->p_channel->i_channel_id   = 0;
199     p_main->p_channel->last_change = 0;
200
201     intf_Msg("Channels initialized\n");
202     return( 0 );
203 #else
204     intf_ErrMsg( "error : channel changing only works with linux yest" );
205 #endif /* SYS_LINUX */   
206 #endif /* SYS_BEOS */
207 }
208
209 /*****************************************************************************
210  * network_ChannelJoin: join a channel
211  *****************************************************************************
212  * This function will try to join a channel. If the relevant interface is 
213  * already on the good channel, nothing will be done. Else, and if possible 
214  * (if the interface is not locked), the channel server will be contacted 
215  * and a change will be requested. The function will block until the change 
216  * is effective. Note that once a channel is no more used, it's interface 
217  * should be unlocked using input_ChannelLeave().
218  * Non 0 will be returned in case of error.
219  *****************************************************************************/
220 int network_ChannelJoin( int i_channel_id )
221 {
222     intf_ErrMsg("Changing to channel %d",i_channel_id);
223     return(0);
224 /* Courtesy of Nitrox. He'll update it soon */
225 #if 0
226 /* I still prefer to put BeOS a bit apart */    
227 #ifdef SYS_BEOS
228     intf_ErrMsg( "Channels are not yet supported uunder BeOS" );
229     return( -1 );
230 #else
231 #ifdef SYS_LINUX    
232     int                 socket_cl;
233     int                 fromlen;
234     struct ifreq        interface;
235     struct sockaddr_in  sa_server;
236     struct sockaddr_in  sa_client;
237     unsigned int        version = 12;
238     char                mess[80];
239     char                mess_length = 80;
240     struct timeval     *date_cl;
241     struct timeval      time;
242     long unsigned int   date;
243     int                 nbanswer;
244     char                answer;
245     fd_set              rfds;
246     unsigned int        rc;
247 /* debug */ intf_ErrMsg("ChannelJoin : %d",i_channel_id);
248     /* If last change is too recent, wait a while */
249     if( mdate() - p_main->p_channel->last_change < INPUT_CHANNEL_CHANGE_DELAY )
250     {
251         intf_Msg("Waiting before changing channel...\n");
252         mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY );
253     }
254     p_main->p_channel->last_change = mdate();
255     p_main->p_channel->i_channel_id = i_channel_id;
256
257     intf_Msg("Joining channel %d\n", i_channel_id );
258
259     /*      
260      * Looking for information about the eth0 interface
261      */
262     interface.ifr_addr.sa_family=AF_INET;
263     strcpy(interface.ifr_name,INPUT_IFACE_DEFAULT);
264     
265     
266     /*
267      * Initialysing the socket
268      */
269     socket_cl=socket(AF_INET,SOCK_DGRAM,0);
270
271     
272     /* 
273      * Getting the server's information 
274      */
275     bzero(&sa_server,sizeof(struct sockaddr_in));
276     sa_server.sin_family=AF_INET;
277     sa_server.sin_port=htons(INPUT_CHANNEL_PORT_DEFAULT);
278     inet_aton(INPUT_CHANNEL_SERVER_DEFAULT,&(sa_server.sin_addr));
279     
280     /*
281      * Looking for the interface MAC address
282      */
283     ioctl(socket_cl,SIOCGIFHWADDR,&interface);
284     intf_DbgMsg(
285         "CHANNELSERVER: macaddr == %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
286         interface.ifr_hwaddr.sa_data[0] & 0xff,
287         interface.ifr_hwaddr.sa_data[1] & 0xff,
288         interface.ifr_hwaddr.sa_data[2] & 0xff,
289         interface.ifr_hwaddr.sa_data[3] & 0xff,
290         interface.ifr_hwaddr.sa_data[4] & 0xff,
291         interface.ifr_hwaddr.sa_data[5] & 0xff);
292     
293     /*
294      * Getting date of the client
295      */
296     date_cl=malloc(sizeof(struct timeval));
297     if(date_cl==NULL)
298     {
299         intf_ErrMsg("CHANNELSERVER: unable to allocate memory\n");
300     /*    return VS_R_MEMORY;*/
301         return -1;
302     }
303     
304     if (gettimeofday(date_cl,0)==-1)
305         return -1;
306     date=date_cl->tv_sec;
307     free(date_cl);
308     intf_DbgMsg("CHANNELSERVER: date %lu\n",date);
309
310
311     /* 
312      * Build of the message
313      */
314     sprintf(mess,"%d %u %lu %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x \n",
315           i_channel_id, version, date,
316         interface.ifr_hwaddr.sa_data[0] & 0xff, 
317         interface.ifr_hwaddr.sa_data[1] & 0xff,
318         interface.ifr_hwaddr.sa_data[2] & 0xff,
319         interface.ifr_hwaddr.sa_data[3] & 0xff,
320         interface.ifr_hwaddr.sa_data[4] & 0xff,
321         interface.ifr_hwaddr.sa_data[5] & 0xff);
322  
323     intf_DbgMsg("CHANNELSERVER: The message is %s\n",mess);
324
325
326     /*
327      * Open the socket 2
328      */
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     fromlen=sizeof(struct sockaddr);
334     rc=bind(socket_cl,(struct sockaddr *)(&sa_client),sizeof(struct sockaddr));
335     if (rc)
336     {
337         intf_ErrMsg("CHANNELSERVER: Unable to bind socket:%u\n",rc); 
338     /* TODO put CS_R_BIND in types.h*/
339     /*    return CS_R_SOCKET;*/
340         return -1;
341     }
342
343
344     /*
345      * Send the message
346      */
347     sendto(socket_cl,mess,mess_length,0,(struct sockaddr *)(&sa_server),\
348            sizeof(struct sockaddr));
349    
350      /*
351      * Waiting 5 sec for one answer from the server
352      */
353     time.tv_sec=5;
354     time.tv_usec=0;
355     FD_ZERO(&rfds);
356     FD_SET(socket_cl,&rfds);
357     nbanswer=select(socket_cl+1,&rfds,NULL,NULL,&time);
358     if(nbanswer==0)
359         intf_DbgMsg("CHANNELSERVER: no answer\n");
360     else if(nbanswer==-1)
361         intf_DbgMsg("CHANNELSERVER: Unable to receive the answer\n");
362     else
363     {
364         recvfrom(socket_cl,&answer,sizeof(char),0,\
365                  (struct sockaddr *)(&sa_client),&fromlen);
366         intf_DbgMsg("CHANNELSERVER: the answer : %hhd\n",answer);
367         if(answer==-1)
368             intf_DbgMsg(
369                     "CHANNELSERVER: The server failed to create the thread\n");
370         else if(answer==0)
371             intf_DbgMsg(
372                     "CHANNELSERVER: The server tries to change the channel\n");
373         else
374             intf_DbgMsg("CHANNELSERVER: Unknown answer !\n");
375     }
376     
377     /*
378      * Close the socket
379      */
380     close(socket_cl);
381
382     return 0;
383 #else /* SYS_LINUX */
384     intf_ErrMsg( "Channel only work under linux yet" );
385 #endif /* SYS_LINUX */    
386     
387 }
388 #endif /* SYS_BEOS */
389 #endif /* if 0 */
390 }