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