]> git.sesse.net Git - vlc/blob - src/input/input_vlan.c
Bon, puisque �a semble commiter sous BeOS, je commite.
[vlc] / src / input / input_vlan.c
1 /*****************************************************************************
2  * input_vlan.c: vlan management library
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include "defs.h"
27
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdio.h>                                              /* sprintf() */
30 #include <unistd.h>                                               /* close() */
31 #include <string.h>                                   /* strerror(), bzero() */
32 #include <stdlib.h>                                                /* free() */
33 #include <sys/time.h>                             /* timeval */
34
35 #if defined(SYS_BSD) || defined(SYS_BEOS)
36 #include <netinet/in.h>                                    /* struct in_addr */
37 #include <sys/socket.h>                                   /* struct sockaddr */
38 #endif
39
40 #if defined(SYS_LINUX) || defined(SYS_BSD) || defined(SYS_GNU)
41 #include <arpa/inet.h>                           /* inet_ntoa(), inet_aton() */
42 #endif
43
44 #ifdef SYS_LINUX
45 #include <sys/ioctl.h>                                            /* ioctl() */
46 #include <net/if.h>                            /* interface (arch-dependent) */
47 #endif
48
49 #include "config.h"
50 #include "common.h"
51 #include "threads.h"
52 #include "mtime.h"
53 #include "plugins.h"
54 #include "netutils.h"
55 #include "input_vlan.h"
56 #include "intf_msg.h"
57
58 #include "main.h"
59
60 /*****************************************************************************
61  * input_vlan_t: vlan library data
62  *****************************************************************************
63  * Store global vlan library data.
64  *****************************************************************************/
65 typedef struct input_vlan_s
66 {
67     int         i_vlan_id;                            /* current vlan number */
68     mtime_t     last_change;                             /* last change date */
69 } input_vlan_t;
70
71 /*****************************************************************************
72  * Local prototypes
73  *****************************************************************************/
74
75 /*****************************************************************************
76  * input_VlanCreate: initialize global vlan method data
77  *****************************************************************************
78  * Initialize vlan input method global data. This function should be called
79  * once before any input thread is created or any call to other input_Vlan*()
80  * function is attempted.
81  *****************************************************************************/
82 int input_VlanCreate( void )
83 {
84 #ifdef SYS_BEOS
85     intf_ErrMsg( "error: vlans are not supported under beos\n" );
86     return( 1 );
87 #else
88     /* Allocate structure */
89     p_main->p_vlan = malloc( sizeof( input_vlan_t ) );
90     if( p_main->p_vlan == NULL )
91     {
92         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
93         return( 1 );
94     }
95
96     /* Initialize structure */
97     p_main->p_vlan->i_vlan_id   = 0;
98     p_main->p_vlan->last_change = 0;
99
100     intf_Msg("VLANs initialized\n");
101     return( 0 );
102 #endif /* SYS_BEOS */
103 }
104
105 /*****************************************************************************
106  * input_VlanDestroy: free global vlan method data
107  *****************************************************************************
108  * Free resources allocated by input_VlanMethodInit. This function should be
109  * called at the end of the program.
110  *****************************************************************************/
111 void input_VlanDestroy( void )
112 {
113     /* Return to default vlan */
114     if( p_main->p_vlan->i_vlan_id != 0 )
115     {
116         input_VlanJoin( 0 );
117     }
118
119     /* Free structure */
120     free( p_main->p_vlan );
121 }
122
123 /*****************************************************************************
124  * input_VlanLeave: leave a vlan
125  *****************************************************************************
126  * This function tells the vlan library that the designed interface is no more
127  * locked and than vlan changes can occur.
128  *****************************************************************************/
129 void input_VlanLeave( int i_vlan_id )
130 {
131     /* XXX?? */
132 }
133
134 /*****************************************************************************
135  * input_VlanJoin: join a vlan
136  *****************************************************************************
137  * This function will try to join a vlan. If the relevant interface is already
138  * on the good vlan, nothing will be done. Else, and if possible (if the
139  * interface is not locked), the vlan server will be contacted and a change will
140  * be requested. The function will block until the change is effective. Note
141  * that once a vlan is no more used, it's interface should be unlocked using
142  * input_VlanLeave().
143  * Non 0 will be returned in case of error.
144  *****************************************************************************/
145 int input_VlanJoin( int i_vlan_id )
146 {
147 #ifdef SYS_BEOS
148     return( -1 );
149 #else
150
151 #define SERVER "138.195.130.90"
152 #define INTERFACE "eth0"
153 /* default server port */
154 #define VLANSERVER_PORT 6010
155     
156     int                 socket_cl;
157     int                 fromlen;
158     struct ifreq        interface;
159     struct sockaddr_in  sa_server;
160     struct sockaddr_in  sa_client;
161     unsigned int        version = 12;
162     char                mess[80];
163     struct timeval     *date_cl;
164     struct timeval      time;
165     long unsigned int   date;
166     int                 nbanswer;
167     char                answer;
168     fd_set              rfds;
169
170     /* If last change is too recent, wait a while */
171     if( mdate() - p_main->p_vlan->last_change < INPUT_VLAN_CHANGE_DELAY )
172     {
173         intf_Msg("Waiting before changing VLAN...\n");
174         mwait( p_main->p_vlan->last_change + INPUT_VLAN_CHANGE_DELAY );
175     }
176     p_main->p_vlan->last_change = mdate();
177     p_main->p_vlan->i_vlan_id = i_vlan_id;
178
179     intf_Msg("Joining VLAN %d (channel %d)\n", i_vlan_id + 2, i_vlan_id );
180
181     /*      
182      *Looking for informations about the eth0 interface
183      */
184     interface.ifr_addr.sa_family = AF_INET;
185     strcpy( interface.ifr_name, INTERFACE );
186     
187     
188     /*
189      * Initialysing the socket
190      */
191     socket_cl = socket( AF_INET, SOCK_DGRAM, 0 );
192     intf_DbgMsg( "socket %d\n", socket_cl );
193
194     
195     /* 
196      * Getting the server's information 
197      */
198     bzero (&sa_server, sizeof (struct sockaddr_in));
199     sa_server.sin_family = AF_INET;
200     sa_server.sin_port = htons (VLANSERVER_PORT);
201     inet_aton (SERVER, &(sa_server.sin_addr));
202     
203     /*
204      * Looking for the interface MAC address
205      */
206     ioctl( socket_cl, SIOCGIFHWADDR, &interface );
207     intf_DbgMsg( "macaddr == %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
208         interface.ifr_hwaddr.sa_data[0] & 0xff,
209         interface.ifr_hwaddr.sa_data[1] & 0xff,
210         interface.ifr_hwaddr.sa_data[2] & 0xff,
211         interface.ifr_hwaddr.sa_data[3] & 0xff,
212         interface.ifr_hwaddr.sa_data[4] & 0xff,
213         interface.ifr_hwaddr.sa_data[5] & 0xff );
214     
215     /*
216      * Getting date of the client
217      */
218     date_cl = malloc (sizeof (struct timeval));
219     if (gettimeofday (date_cl, 0) == -1)
220     {
221         return -1;
222     }
223     date = date_cl->tv_sec;
224     intf_DbgMsg ("date %lu\n", date);
225
226
227     /* 
228      * Build of the message
229      */
230     sprintf (mess, "%d %u %lu %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x \n",
231         i_vlan_id, version, date,
232     interface.ifr_hwaddr.sa_data[0] & 0xff, 
233     interface.ifr_hwaddr.sa_data[1] & 0xff,
234     interface.ifr_hwaddr.sa_data[2] & 0xff,
235     interface.ifr_hwaddr.sa_data[3] & 0xff,
236     interface.ifr_hwaddr.sa_data[4] & 0xff,
237     interface.ifr_hwaddr.sa_data[5] & 0xff);
238     intf_DbgMsg ("The message is %s\n", mess);
239
240
241     /*
242      * Open the socket 2
243      */
244     bzero (&sa_client, sizeof (struct sockaddr_in));
245     sa_client.sin_family = AF_INET;
246     sa_client.sin_port = htons( 4312 );
247     sa_client.sin_addr.s_addr = INADDR_ANY;
248     intf_DbgMsg ("socket %d\n", socket_cl = socket( AF_INET, SOCK_DGRAM, 0 ));
249     fromlen = sizeof (struct sockaddr);
250     intf_DbgMsg( "bind %i\n", bind( socket_cl, (struct sockaddr *)(&sa_client), sizeof( struct sockaddr )));
251
252
253     /*
254      * Send the message
255      */
256     sendto (socket_cl, mess, 80, 0, (struct sockaddr *)(&sa_server), sizeof (struct sockaddr ));
257     {
258       unsigned z;
259       printf("BBP\n");
260       z=0;
261       do {z++;} while (mess[z]!=':');
262       do {z++;} while (mess[z]!='e');
263       printf("meuuh %d %d\n",(unsigned)mess[z+3],(unsigned)mess[z+4]);
264     }
265     printf("BBP2\n");
266
267     
268      /*
269      * Waiting 5 sec for one answer from the server
270      */
271     time.tv_sec = 5;
272     time.tv_usec = 0;
273     FD_ZERO( &rfds );
274     FD_SET( socket_cl, &rfds );
275     nbanswer = select( socket_cl + 1, &rfds, NULL, NULL, &time);
276     if( nbanswer == 0 )
277     {
278         intf_DbgMsg( "no answer\n" );
279     }
280     else if( nbanswer == -1 )
281     {
282         intf_DbgMsg( "I couldn't recieve the answer\n" );
283     }
284     else
285     {
286        recvfrom (socket_cl, &answer, sizeof( char ), 0, (struct sockaddr *)(&sa_client), &fromlen);
287         intf_DbgMsg( "the answer : %hhd\n", answer );
288         if( answer == -1 )
289         {
290             intf_DbgMsg( "The server doesn't succed to create the thread\n" );
291         }
292         else if( answer == 0 )
293         {
294             intf_DbgMsg( "The server try to change the channel\n" );
295         }
296         else
297         {
298             intf_DbgMsg( "I don't know what is this answer !\n" );
299         }
300     }
301     
302
303     /*
304      * Close the socket
305      */
306     close( socket_cl);
307
308     return 0;
309 #endif
310 }