]> git.sesse.net Git - vlc/blob - src/input/input_vlan.c
. normalement on devrait se prendre 1 seul mail par commit gr�ce aux
[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 GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "defs.h"
28
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdio.h>                                              /* sprintf() */
31 #include <unistd.h>                                               /* close() */
32 #include <string.h>                                   /* strerror(), bzero() */
33 #include <stdlib.h>                                                /* free() */
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 static int ZeTrucMucheFunction( int Channel );
73
74 /*****************************************************************************
75  * input_VlanCreate: initialize global vlan method data
76  *****************************************************************************
77  * Initialize vlan input method global data. This function should be called
78  * once before any input thread is created or any call to other input_Vlan*()
79  * function is attempted.
80  *****************************************************************************/
81 int input_VlanCreate( void )
82 {
83 #ifdef SYS_BEOS
84     intf_ErrMsg( "error: vlans are not supported under beos\n" );
85     return( 1 );
86 #else
87     /* Allocate structure */
88     p_main->p_vlan = malloc( sizeof( input_vlan_t ) );
89     if( p_main->p_vlan == NULL )
90     {
91         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
92         return( 1 );
93     }
94
95     /* Initialize structure */
96     p_main->p_vlan->i_vlan_id   = 0;
97     p_main->p_vlan->last_change = 0;
98
99     intf_Msg("VLANs initialized\n");
100     return( 0 );
101 #endif /* SYS_BEOS */
102 }
103
104 /*****************************************************************************
105  * input_VlanDestroy: free global vlan method data
106  *****************************************************************************
107  * Free resources allocated by input_VlanMethodInit. This function should be
108  * called at the end of the program.
109  *****************************************************************************/
110 void input_VlanDestroy( void )
111 {
112     /* Return to default vlan */
113     if( p_main->p_vlan->i_vlan_id != 0 )
114     {
115         input_VlanJoin( 0 );
116     }
117
118     /* Free structure */
119     free( p_main->p_vlan );
120 }
121
122 /*****************************************************************************
123  * input_VlanJoin: join a vlan
124  *****************************************************************************
125  * This function will try to join a vlan. If the relevant interface is already
126  * on the good vlan, nothing will be done. Else, and if possible (if the
127  * interface is not locked), the vlan server will be contacted and a change will
128  * be requested. The function will block until the change is effective. Note
129  * that once a vlan is no more used, it's interface should be unlocked using
130  * input_VlanLeave().
131  * Non 0 will be returned in case of error.
132  *****************************************************************************/
133 int input_VlanJoin( int i_vlan_id )
134 {
135 #ifdef SYS_BEOS
136     return( -1 );
137 #else
138     /* If last change is too recent, wait a while */
139     if( mdate() - p_main->p_vlan->last_change < INPUT_VLAN_CHANGE_DELAY )
140     {
141         intf_Msg("Waiting before changing VLAN...\n");
142         mwait( p_main->p_vlan->last_change + INPUT_VLAN_CHANGE_DELAY );
143     }
144     p_main->p_vlan->last_change = mdate();
145     p_main->p_vlan->i_vlan_id = i_vlan_id;
146
147     intf_Msg("Joining VLAN %d (channel %d)\n", i_vlan_id + 2, i_vlan_id );
148     return( ZeTrucMucheFunction( i_vlan_id ) ); /* FIXME: join vlan ?? */
149 #endif /* SYS_BEOS */
150 }
151
152 /*****************************************************************************
153  * input_VlanLeave: leave a vlan
154  *****************************************************************************
155  * This function tells the vlan library that the designed interface is no more
156  * locked and than vlan changes can occur.
157  *****************************************************************************/
158 void input_VlanLeave( int i_vlan_id )
159 {
160     /* XXX?? */
161 }
162
163 /* following functions are local */
164
165 static int ZeTrucMucheFunction( int Channel)
166 {
167 #ifdef SYS_LINUX
168     int                         i_socket;
169     char   *                    ipaddr;
170     struct ifreq                interface;
171     struct sockaddr_in          sa_server;
172     struct sockaddr_in          sa_client;
173     char                        mess[80];
174
175     /*
176      *Looking for informations about the eth0 interface
177      */
178
179     interface.ifr_addr.sa_family = AF_INET;
180     strcpy( interface.ifr_name, main_GetPszVariable( INPUT_IFACE_VAR, INPUT_IFACE_DEFAULT ) );
181
182     i_socket = socket( AF_INET, SOCK_DGRAM, 0 );
183
184     /* Looking for the interface IP address */
185     ioctl( i_socket, SIOCGIFDSTADDR, &interface );
186     ipaddr = inet_ntoa((*(struct sockaddr_in *)(&(interface.ifr_addr))).sin_addr );
187
188     /* Looking for the interface MAC address */
189     ioctl( i_socket, SIOCGIFHWADDR, &interface );
190     close( i_socket );
191
192     /*
193      * Getting address, port, ... of the server
194      */
195
196     /* Initialize */
197     bzero( &sa_server, sizeof(struct sockaddr_in) );
198     /* sin_family is ALWAYS set to AF_INET (see in man 7 ip)*/
199     sa_server.sin_family = AF_INET;
200     /* Giving port on to connect after having convert it*/
201     sa_server.sin_port = htons ( main_GetIntVariable( INPUT_VLAN_PORT_VAR, INPUT_VLAN_PORT_DEFAULT ));
202     /* Giving address after having convert it into binary data*/
203     inet_aton( main_GetPszVariable( INPUT_VLAN_SERVER_VAR, INPUT_VLAN_SERVER_DEFAULT ), &(sa_server.sin_addr) );
204
205     /*
206      * Getting address, port, ... of the client
207      */
208
209     /* Initialize */
210     bzero( &sa_client, sizeof(struct sockaddr_in) );
211     /* sin_family is ALWAYS set to AF_INET (see in man 7 ip)*/
212     sa_client.sin_family = AF_INET;
213     /* Giving port on to connect after having convert it*/
214     sa_client.sin_port = htons( 0 );
215     /* Giving address after having convert it into binary data*/
216     inet_aton( ipaddr, &(sa_client.sin_addr) );
217
218     /* Initialization of the socket */
219     i_socket = socket(AF_INET, SOCK_DGRAM, 17 ); /* XXX?? UDP */
220      /*  SOCK_DGRAM because here we use DATAGRAM
221       * Sachant qu'il y a un #define AF_INET = PF_INET dans sys/socket.h et que PF_INET est le IP protocol family ...
222       * Protocol is in #define, should be 17 for udp */
223
224     /* Elaborate the message to send */
225         sprintf( mess , "%d %s %2.2x%2.2x%2.2x%2.2x%2.2x%2.2x %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x \n",
226             Channel, ipaddr,
227             interface.ifr_hwaddr.sa_data[0] & 0xff,
228             interface.ifr_hwaddr.sa_data[1] & 0xff,
229             interface.ifr_hwaddr.sa_data[2] & 0xff,
230             interface.ifr_hwaddr.sa_data[3] & 0xff,
231             interface.ifr_hwaddr.sa_data[4] & 0xff,
232             interface.ifr_hwaddr.sa_data[5] & 0xff,
233                     interface.ifr_hwaddr.sa_data[0] & 0xff,
234             interface.ifr_hwaddr.sa_data[1] & 0xff,
235             interface.ifr_hwaddr.sa_data[2] & 0xff,
236             interface.ifr_hwaddr.sa_data[3] & 0xff,
237             interface.ifr_hwaddr.sa_data[4] & 0xff,
238             interface.ifr_hwaddr.sa_data[5] & 0xff
239         );
240
241     /* Send the message */
242     intf_DbgMsg("%s\n", mess);
243         sendto(i_socket,mess,80,0,(struct sockaddr *)&sa_server,sizeof(struct sockaddr));
244
245     /*Close the socket */
246     close( i_socket );
247 #endif
248
249     return 0;
250 }