]> git.sesse.net Git - vlc/blob - src/input/input_vlan.c
. le code des VLAN devrait refonctionner
[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 "netutils.h"
54 #include "input_vlan.h"
55 #include "intf_msg.h"
56 #include "main.h"
57
58 /*****************************************************************************
59  * input_vlan_t: vlan library data
60  *****************************************************************************
61  * Store global vlan library data.
62  *****************************************************************************/
63 typedef struct input_vlan_s
64 {
65     int         i_vlan_id;                            /* current vlan number */
66     mtime_t     last_change;                             /* last change date */
67 } input_vlan_t;
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72
73 /*****************************************************************************
74  * input_VlanCreate: initialize global vlan method data
75  *****************************************************************************
76  * Initialize vlan input method global data. This function should be called
77  * once before any input thread is created or any call to other input_Vlan*()
78  * function is attempted.
79  *****************************************************************************/
80 int input_VlanCreate( void )
81 {
82 #ifdef SYS_BEOS
83     intf_ErrMsg( "error: vlans are not supported under beos\n" );
84     return( 1 );
85 #else
86     /* Allocate structure */
87     p_main->p_vlan = malloc( sizeof( input_vlan_t ) );
88     if( p_main->p_vlan == NULL )
89     {
90         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
91         return( 1 );
92     }
93
94     /* Initialize structure */
95     p_main->p_vlan->i_vlan_id   = 0;
96     p_main->p_vlan->last_change = 0;
97
98     intf_Msg("VLANs initialized\n");
99     return( 0 );
100 #endif /* SYS_BEOS */
101 }
102
103 /*****************************************************************************
104  * input_VlanDestroy: free global vlan method data
105  *****************************************************************************
106  * Free resources allocated by input_VlanMethodInit. This function should be
107  * called at the end of the program.
108  *****************************************************************************/
109 void input_VlanDestroy( void )
110 {
111     /* Return to default vlan */
112     if( p_main->p_vlan->i_vlan_id != 0 )
113     {
114         input_VlanJoin( 0 );
115     }
116
117     /* Free structure */
118     free( p_main->p_vlan );
119 }
120
121 /*****************************************************************************
122  * input_VlanLeave: leave a vlan
123  *****************************************************************************
124  * This function tells the vlan library that the designed interface is no more
125  * locked and than vlan changes can occur.
126  *****************************************************************************/
127 void input_VlanLeave( int i_vlan_id )
128 {
129     /* XXX?? */
130 }
131
132 /*****************************************************************************
133  * input_VlanJoin: join a vlan
134  *****************************************************************************
135  * This function will try to join a vlan. If the relevant interface is already
136  * on the good vlan, nothing will be done. Else, and if possible (if the
137  * interface is not locked), the vlan server will be contacted and a change will
138  * be requested. The function will block until the change is effective. Note
139  * that once a vlan is no more used, it's interface should be unlocked using
140  * input_VlanLeave().
141  * Non 0 will be returned in case of error.
142  *****************************************************************************/
143 int input_VlanJoin( int i_vlan_id )
144 {
145 #ifdef SYS_BEOS
146     return( -1 );
147 #else
148
149 #define SERVER "138.195.130.90"
150 #define INTERFACE "eth0"
151 /* default server port */
152 #define VLANSERVER_PORT 6010
153     
154     int                 socket_cl;
155     int                 fromlen;
156     struct ifreq        interface;
157     struct sockaddr_in  sa_server;
158     struct sockaddr_in  sa_client;
159     unsigned int        version = 12;
160     char                mess[80];
161     struct timeval     *date_cl;
162     struct timeval      time;
163     long unsigned int   date;
164     int                 nbanswer;
165     char                answer;
166     fd_set              rfds;
167
168     /* If last change is too recent, wait a while */
169     if( mdate() - p_main->p_vlan->last_change < INPUT_VLAN_CHANGE_DELAY )
170     {
171         intf_Msg("Waiting before changing VLAN...\n");
172         mwait( p_main->p_vlan->last_change + INPUT_VLAN_CHANGE_DELAY );
173     }
174     p_main->p_vlan->last_change = mdate();
175     p_main->p_vlan->i_vlan_id = i_vlan_id;
176
177     intf_Msg("Joining VLAN %d (channel %d)\n", i_vlan_id + 2, i_vlan_id );
178
179     /*      
180      *Looking for informations about the eth0 interface
181      */
182     interface.ifr_addr.sa_family = AF_INET;
183     strcpy( interface.ifr_name, INTERFACE );
184     
185     
186     /*
187      * Initialysing the socket
188      */
189     socket_cl = socket( AF_INET, SOCK_DGRAM, 0 );
190     intf_DbgMsg( "socket %d\n", socket_cl );
191
192     
193     /* 
194      * Getting the server's information 
195      */
196     bzero (&sa_server, sizeof (struct sockaddr_in));
197     sa_server.sin_family = AF_INET;
198     sa_server.sin_port = htons (VLANSERVER_PORT);
199     inet_aton (SERVER, &(sa_server.sin_addr));
200     
201     /*
202      * Looking for the interface MAC address
203      */
204     ioctl( socket_cl, SIOCGIFHWADDR, &interface );
205     intf_DbgMsg( "macaddr == %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
206         interface.ifr_hwaddr.sa_data[0] & 0xff,
207         interface.ifr_hwaddr.sa_data[1] & 0xff,
208         interface.ifr_hwaddr.sa_data[2] & 0xff,
209         interface.ifr_hwaddr.sa_data[3] & 0xff,
210         interface.ifr_hwaddr.sa_data[4] & 0xff,
211         interface.ifr_hwaddr.sa_data[5] & 0xff );
212     
213     /*
214      * Getting date of the client
215      */
216     date_cl = malloc (sizeof (struct timeval));
217     if (gettimeofday (date_cl, 0) == -1)
218     {
219         return -1;
220     }
221     date = date_cl->tv_sec;
222     intf_DbgMsg ("date %lu\n", date);
223
224
225     /* 
226      * Build of the message
227      */
228     sprintf (mess, "%d %u %lu %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x \n",
229         i_vlan_id, version, date,
230     interface.ifr_hwaddr.sa_data[0] & 0xff, 
231     interface.ifr_hwaddr.sa_data[1] & 0xff,
232     interface.ifr_hwaddr.sa_data[2] & 0xff,
233     interface.ifr_hwaddr.sa_data[3] & 0xff,
234     interface.ifr_hwaddr.sa_data[4] & 0xff,
235     interface.ifr_hwaddr.sa_data[5] & 0xff);
236     intf_DbgMsg ("The message is %s\n", mess);
237
238
239     /*
240      * Open the socket 2
241      */
242     bzero (&sa_client, sizeof (struct sockaddr_in));
243     sa_client.sin_family = AF_INET;
244     sa_client.sin_port = htons( 4312 );
245     sa_client.sin_addr.s_addr = INADDR_ANY;
246     intf_DbgMsg ("socket %d\n", socket_cl = socket( AF_INET, SOCK_DGRAM, 0 ));
247     fromlen = sizeof (struct sockaddr);
248     intf_DbgMsg( "bind %i\n", bind( socket_cl, (struct sockaddr *)(&sa_client), sizeof( struct sockaddr )));
249
250
251     /*
252      * Send the message
253      */
254     sendto (socket_cl, mess, 80, 0, (struct sockaddr *)(&sa_server), sizeof (struct sockaddr ));
255     {
256       unsigned z;
257       printf("BBP\n");
258       z=0;
259       do {z++;} while (mess[z]!=':');
260       do {z++;} while (mess[z]!='e');
261       printf("meuuh %d %d\n",(unsigned)mess[z+3],(unsigned)mess[z+4]);
262     }
263     printf("BBP2\n");
264
265     
266      /*
267      * Waiting 5 sec for one answer from the server
268      */
269     time.tv_sec = 5;
270     time.tv_usec = 0;
271     FD_ZERO( &rfds );
272     FD_SET( socket_cl, &rfds );
273     nbanswer = select( socket_cl + 1, &rfds, NULL, NULL, &time);
274     if( nbanswer == 0 )
275     {
276         intf_DbgMsg( "no answer\n" );
277     }
278     else if( nbanswer == -1 )
279     {
280         intf_DbgMsg( "I couldn't recieve the answer\n" );
281     }
282     else
283     {
284        recvfrom (socket_cl, &answer, sizeof( char ), 0, (struct sockaddr *)(&sa_client), &fromlen);
285         intf_DbgMsg( "the answer : %hhd\n", answer );
286         if( answer == -1 )
287         {
288             intf_DbgMsg( "The server doesn't succed to create the thread\n" );
289         }
290         else if( answer == 0 )
291         {
292             intf_DbgMsg( "The server try to change the channel\n" );
293         }
294         else
295         {
296             intf_DbgMsg( "I don't know what is this answer !\n" );
297         }
298     }
299     
300
301     /*
302      * Close the socket
303      */
304     close( socket_cl);
305
306     return 0;
307 #endif
308 }