1 /*****************************************************************************
2 * input_vlan.c: vlan management library
4 *****************************************************************************/
6 /*****************************************************************************
8 *****************************************************************************/
15 #include <arpa/inet.h>
17 #include <netinet/in.h>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
24 #include "vlc_thread.h"
26 #include "input_vlan.h"
30 /*****************************************************************************
31 * input_vlan_t: vlan library data
32 *****************************************************************************
33 * Store global vlan library data.
34 *****************************************************************************/
35 typedef struct input_vlan_s
37 int i_vlan_id; /* current vlan number */
38 mtime_t last_change; /* last change date */
41 /*****************************************************************************
43 *****************************************************************************/
44 static int ZeTrucMucheFunction( int Channel );
46 /*****************************************************************************
47 * input_VlanCreate: initialize global vlan method data
48 *****************************************************************************
49 * Initialize vlan input method global data. This function should be called
50 * once before any input thread is created or any call to other input_Vlan*()
51 * function is attempted.
52 *****************************************************************************/
53 int input_VlanCreate( void )
55 /* Allocate structure */
56 p_main->p_vlan = malloc( sizeof( input_vlan_t ) );
57 if( p_main->p_vlan == NULL )
59 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
63 /* Initialize structure */
64 p_main->p_vlan->i_vlan_id = 0;
65 p_main->p_vlan->last_change = 0;
67 intf_Msg("VLANs initialized\n");
71 /*****************************************************************************
72 * input_VlanDestroy: free global vlan method data
73 *****************************************************************************
74 * Free resources allocated by input_VlanMethodInit. This function should be
75 * called at the end of the program.
76 *****************************************************************************/
77 void input_VlanDestroy( void )
79 /* Return to default vlan */
80 if( p_main->p_vlan->i_vlan_id != 0 )
86 free( p_main->p_vlan );
89 /*****************************************************************************
90 * input_VlanJoin: join a vlan
91 *****************************************************************************
92 * This function will try to join a vlan. If the relevant interface is already
93 * on the good vlan, nothing will be done. Else, and if possible (if the
94 * interface is not locked), the vlan server will be contacted and a change will
95 * be requested. The function will block until the change is effective. Note
96 * that once a vlan is no more used, it's interface should be unlocked using
98 * Non 0 will be returned in case of error.
99 *****************************************************************************/
100 int input_VlanJoin( int i_vlan_id )
102 /* If last change is too recent, wait a while */
103 if( mdate() - p_main->p_vlan->last_change < INPUT_VLAN_CHANGE_DELAY )
105 intf_Msg("Waiting before changing VLAN...\n");
106 mwait( p_main->p_vlan->last_change + INPUT_VLAN_CHANGE_DELAY );
108 p_main->p_vlan->last_change = mdate();
109 p_main->p_vlan->i_vlan_id = i_vlan_id;
111 intf_Msg("Joining VLAN %d (channel %d)\n", i_vlan_id + 2, i_vlan_id );
112 return( ZeTrucMucheFunction( i_vlan_id ) ); // ?? join vlan
115 /*****************************************************************************
116 * input_VlanLeave: leave a vlan
117 *****************************************************************************
118 * This function tells the vlan library that the designed interface is no more
119 * locked and than vlan changes can occur.
120 *****************************************************************************/
121 void input_VlanLeave( int i_vlan_id )
126 /* following functions are local */
128 static int ZeTrucMucheFunction( int Channel)
132 struct ifreq interface;
133 struct sockaddr_in sa_server;
134 struct sockaddr_in sa_client;
138 *Looking for informations about the eth0 interface
141 interface.ifr_addr.sa_family = AF_INET;
142 strcpy( interface.ifr_name, main_GetPszVariable( INPUT_IFACE_VAR, INPUT_IFACE_DEFAULT ) );
144 i_socket = socket( AF_INET, SOCK_DGRAM, 0 );
146 /* Looking for the interface IP address */
147 ioctl( i_socket, SIOCGIFDSTADDR, &interface );
148 ipaddr = inet_ntoa((*(struct sockaddr_in *)(&(interface.ifr_addr))).sin_addr );
150 /* Looking for the interface MAC address */
151 ioctl( i_socket, SIOCGIFHWADDR, &interface );
155 * Getting address, port, ... of the server
159 bzero( &sa_server, sizeof(struct sockaddr_in) );
160 /* sin_family is ALWAYS set to AF_INET (see in man 7 ip)*/
161 sa_server.sin_family = AF_INET;
162 /* Giving port on to connect after having convert it*/
163 sa_server.sin_port = htons ( main_GetIntVariable( INPUT_VLAN_PORT_VAR, INPUT_VLAN_PORT_DEFAULT ));
164 /* Giving address after having convert it into binary data*/
165 inet_aton( main_GetPszVariable( INPUT_VLAN_SERVER_VAR, INPUT_VLAN_SERVER_DEFAULT ), &(sa_server.sin_addr) );
168 * Getting address, port, ... of the client
172 bzero( &sa_client, sizeof(struct sockaddr_in) );
173 /* sin_family is ALWAYS set to AF_INET (see in man 7 ip)*/
174 sa_client.sin_family = AF_INET;
175 /* Giving port on to connect after having convert it*/
176 sa_client.sin_port = htons( 0 );
177 /* Giving address after having convert it into binary data*/
178 inet_aton( ipaddr, &(sa_client.sin_addr) );
180 /* Initialization of the socket */
181 i_socket = socket(AF_INET, SOCK_DGRAM, 17 ); // ?? UDP
182 /* SOCK_DGRAM because here we use DATAGRAM
183 * Sachant qu'il y a un #define AF_INET = PF_INET dans sys/socket.h et que PF_INET est le IP protocol family ...
184 * Protocol is in #define, should be 17 for udp */
186 /* Elaborate the message to send */
187 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 interface.ifr_hwaddr.sa_data[0] & 0xff,
190 interface.ifr_hwaddr.sa_data[1] & 0xff,
191 interface.ifr_hwaddr.sa_data[2] & 0xff,
192 interface.ifr_hwaddr.sa_data[3] & 0xff,
193 interface.ifr_hwaddr.sa_data[4] & 0xff,
194 interface.ifr_hwaddr.sa_data[5] & 0xff,
195 interface.ifr_hwaddr.sa_data[0] & 0xff,
196 interface.ifr_hwaddr.sa_data[1] & 0xff,
197 interface.ifr_hwaddr.sa_data[2] & 0xff,
198 interface.ifr_hwaddr.sa_data[3] & 0xff,
199 interface.ifr_hwaddr.sa_data[4] & 0xff,
200 interface.ifr_hwaddr.sa_data[5] & 0xff
203 /* Send the message */
204 intf_DbgMsg("%s\n", mess);
205 sendto(i_socket,mess,80,0,(struct sockaddr *)&sa_server,sizeof(struct sockaddr));
207 /*Close the socket */