]> git.sesse.net Git - vlc/blob - src/input/input_vlan.c
Encore un commit venu tout droit des abysses de l'enfer, d�sol� pour
[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 <errno.h>                                                 /* ENOMEM */
28 #include <stdio.h>                                              /* sprintf() */
29 #include <unistd.h>                                               /* close() */
30 #include <string.h>                                   /* strerror(), bzero() */
31 #include <stdlib.h>                                                /* free() */
32
33 #ifdef SYS_BSD
34 #include <netinet/in.h>                                    /* struct in_addr */
35 #include <sys/socket.h>                                   /* struct sockaddr */
36 #endif
37
38 #include <arpa/inet.h>                           /* inet_ntoa(), inet_aton() */
39
40 #ifdef SYS_LINUX
41 #include <sys/ioctl.h>                                            /* ioctl() */
42 #include <net/if.h>                            /* interface (arch-dependent) */
43 #endif
44
45 #include "config.h"
46 #include "common.h"
47 #include "mtime.h"
48 #include "threads.h"
49 #include "netutils.h"
50 #include "input_vlan.h"
51 #include "intf_msg.h"
52 #include "main.h"
53
54 /*****************************************************************************
55  * input_vlan_t: vlan library data
56  *****************************************************************************
57  * Store global vlan library data.
58  *****************************************************************************/
59 typedef struct input_vlan_s
60 {
61     int         i_vlan_id;                            /* current vlan number */
62     mtime_t     last_change;                             /* last change date */
63 } input_vlan_t;
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static int ZeTrucMucheFunction( int Channel );
69
70 /*****************************************************************************
71  * input_VlanCreate: initialize global vlan method data
72  *****************************************************************************
73  * Initialize vlan input method global data. This function should be called
74  * once before any input thread is created or any call to other input_Vlan*()
75  * function is attempted.
76  *****************************************************************************/
77 int input_VlanCreate( void )
78 {
79     /* Allocate structure */
80     p_main->p_vlan = malloc( sizeof( input_vlan_t ) );
81     if( p_main->p_vlan == NULL )
82     {
83         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
84         return( 1 );
85     }
86
87     /* Initialize structure */
88     p_main->p_vlan->i_vlan_id   = 0;
89     p_main->p_vlan->last_change = 0;
90
91     intf_Msg("VLANs initialized\n");
92     return( 0 );
93 }
94
95 /*****************************************************************************
96  * input_VlanDestroy: free global vlan method data
97  *****************************************************************************
98  * Free resources allocated by input_VlanMethodInit. This function should be
99  * called at the end of the program.
100  *****************************************************************************/
101 void input_VlanDestroy( void )
102 {
103     /* Return to default vlan */
104     if( p_main->p_vlan->i_vlan_id != 0 )
105     {
106         input_VlanJoin( 0 );
107     }
108
109     /* Free structure */
110     free( p_main->p_vlan );
111 }
112
113 /*****************************************************************************
114  * input_VlanJoin: join a vlan
115  *****************************************************************************
116  * This function will try to join a vlan. If the relevant interface is already
117  * on the good vlan, nothing will be done. Else, and if possible (if the
118  * interface is not locked), the vlan server will be contacted and a change will
119  * be requested. The function will block until the change is effective. Note
120  * that once a vlan is no more used, it's interface should be unlocked using
121  * input_VlanLeave().
122  * Non 0 will be returned in case of error.
123  *****************************************************************************/
124 int input_VlanJoin( int i_vlan_id )
125 {
126     /* If last change is too recent, wait a while */
127     if( mdate() - p_main->p_vlan->last_change < INPUT_VLAN_CHANGE_DELAY )
128     {
129         intf_Msg("Waiting before changing VLAN...\n");
130         mwait( p_main->p_vlan->last_change + INPUT_VLAN_CHANGE_DELAY );
131     }
132     p_main->p_vlan->last_change = mdate();
133     p_main->p_vlan->i_vlan_id = i_vlan_id;
134
135     intf_Msg("Joining VLAN %d (channel %d)\n", i_vlan_id + 2, i_vlan_id );
136     return( ZeTrucMucheFunction( i_vlan_id ) ); /* FIXME: join vlan ?? */
137 }
138
139 /*****************************************************************************
140  * input_VlanLeave: leave a vlan
141  *****************************************************************************
142  * This function tells the vlan library that the designed interface is no more
143  * locked and than vlan changes can occur.
144  *****************************************************************************/
145 void input_VlanLeave( int i_vlan_id )
146 {
147     /* XXX?? */
148 }
149
150 /* following functions are local */
151
152 static int ZeTrucMucheFunction( int Channel)
153 {
154 #ifdef SYS_LINUX
155     int                         i_socket;
156     char   *                    ipaddr;
157     struct ifreq                interface;
158     struct sockaddr_in          sa_server;
159     struct sockaddr_in          sa_client;
160     char                        mess[80];
161
162     /*
163      *Looking for informations about the eth0 interface
164      */
165
166     interface.ifr_addr.sa_family = AF_INET;
167     strcpy( interface.ifr_name, main_GetPszVariable( INPUT_IFACE_VAR, INPUT_IFACE_DEFAULT ) );
168
169     i_socket = socket( AF_INET, SOCK_DGRAM, 0 );
170
171     /* Looking for the interface IP address */
172     ioctl( i_socket, SIOCGIFDSTADDR, &interface );
173     ipaddr = inet_ntoa((*(struct sockaddr_in *)(&(interface.ifr_addr))).sin_addr );
174
175     /* Looking for the interface MAC address */
176     ioctl( i_socket, SIOCGIFHWADDR, &interface );
177     close( i_socket );
178
179     /*
180      * Getting address, port, ... of the server
181      */
182
183     /* Initialize */
184     bzero( &sa_server, sizeof(struct sockaddr_in) );
185     /* sin_family is ALWAYS set to AF_INET (see in man 7 ip)*/
186     sa_server.sin_family = AF_INET;
187     /* Giving port on to connect after having convert it*/
188     sa_server.sin_port = htons ( main_GetIntVariable( INPUT_VLAN_PORT_VAR, INPUT_VLAN_PORT_DEFAULT ));
189     /* Giving address after having convert it into binary data*/
190     inet_aton( main_GetPszVariable( INPUT_VLAN_SERVER_VAR, INPUT_VLAN_SERVER_DEFAULT ), &(sa_server.sin_addr) );
191
192     /*
193      * Getting address, port, ... of the client
194      */
195
196     /* Initialize */
197     bzero( &sa_client, sizeof(struct sockaddr_in) );
198     /* sin_family is ALWAYS set to AF_INET (see in man 7 ip)*/
199     sa_client.sin_family = AF_INET;
200     /* Giving port on to connect after having convert it*/
201     sa_client.sin_port = htons( 0 );
202     /* Giving address after having convert it into binary data*/
203     inet_aton( ipaddr, &(sa_client.sin_addr) );
204
205     /* Initialization of the socket */
206     i_socket = socket(AF_INET, SOCK_DGRAM, 17 ); /* XXX?? UDP */
207      /*  SOCK_DGRAM because here we use DATAGRAM
208       * Sachant qu'il y a un #define AF_INET = PF_INET dans sys/socket.h et que PF_INET est le IP protocol family ...
209       * Protocol is in #define, should be 17 for udp */
210
211     /* Elaborate the message to send */
212         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",
213             Channel, ipaddr,
214             interface.ifr_hwaddr.sa_data[0] & 0xff,
215             interface.ifr_hwaddr.sa_data[1] & 0xff,
216             interface.ifr_hwaddr.sa_data[2] & 0xff,
217             interface.ifr_hwaddr.sa_data[3] & 0xff,
218             interface.ifr_hwaddr.sa_data[4] & 0xff,
219             interface.ifr_hwaddr.sa_data[5] & 0xff,
220                     interface.ifr_hwaddr.sa_data[0] & 0xff,
221             interface.ifr_hwaddr.sa_data[1] & 0xff,
222             interface.ifr_hwaddr.sa_data[2] & 0xff,
223             interface.ifr_hwaddr.sa_data[3] & 0xff,
224             interface.ifr_hwaddr.sa_data[4] & 0xff,
225             interface.ifr_hwaddr.sa_data[5] & 0xff
226         );
227
228     /* Send the message */
229     intf_DbgMsg("%s\n", mess);
230         sendto(i_socket,mess,80,0,(struct sockaddr *)&sa_server,sizeof(struct sockaddr));
231
232     /*Close the socket */
233     close( i_socket );
234 #endif
235
236     return 0;
237 }