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