1 /*****************************************************************************
2 * input_vlan.c: vlan management library
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
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() */
35 #if defined(SYS_BSD) || defined(SYS_BEOS)
36 #include <netinet/in.h> /* struct in_addr */
37 #include <sys/socket.h> /* struct sockaddr */
40 #if defined(SYS_LINUX) || defined(SYS_BSD) || defined(SYS_GNU)
41 #include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
45 #include <sys/ioctl.h> /* ioctl() */
46 #include <net/if.h> /* interface (arch-dependent) */
54 #include "input_vlan.h"
58 /*****************************************************************************
59 * input_vlan_t: vlan library data
60 *****************************************************************************
61 * Store global vlan library data.
62 *****************************************************************************/
63 typedef struct input_vlan_s
65 int i_vlan_id; /* current vlan number */
66 mtime_t last_change; /* last change date */
69 /*****************************************************************************
71 *****************************************************************************/
72 static int ZeTrucMucheFunction( int Channel );
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 )
84 intf_ErrMsg( "error: vlans are not supported under beos\n" );
87 /* Allocate structure */
88 p_main->p_vlan = malloc( sizeof( input_vlan_t ) );
89 if( p_main->p_vlan == NULL )
91 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
95 /* Initialize structure */
96 p_main->p_vlan->i_vlan_id = 0;
97 p_main->p_vlan->last_change = 0;
99 intf_Msg("VLANs initialized\n");
101 #endif /* SYS_BEOS */
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 )
112 /* Return to default vlan */
113 if( p_main->p_vlan->i_vlan_id != 0 )
119 free( p_main->p_vlan );
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
131 * Non 0 will be returned in case of error.
132 *****************************************************************************/
133 int input_VlanJoin( int i_vlan_id )
138 /* If last change is too recent, wait a while */
139 if( mdate() - p_main->p_vlan->last_change < INPUT_VLAN_CHANGE_DELAY )
141 intf_Msg("Waiting before changing VLAN...\n");
142 mwait( p_main->p_vlan->last_change + INPUT_VLAN_CHANGE_DELAY );
144 p_main->p_vlan->last_change = mdate();
145 p_main->p_vlan->i_vlan_id = i_vlan_id;
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 */
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 )
163 /* following functions are local */
165 static int ZeTrucMucheFunction( int Channel)
170 struct ifreq interface;
171 struct sockaddr_in sa_server;
172 struct sockaddr_in sa_client;
176 *Looking for informations about the eth0 interface
179 interface.ifr_addr.sa_family = AF_INET;
180 strcpy( interface.ifr_name, main_GetPszVariable( INPUT_IFACE_VAR, INPUT_IFACE_DEFAULT ) );
182 i_socket = socket( AF_INET, SOCK_DGRAM, 0 );
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 );
188 /* Looking for the interface MAC address */
189 ioctl( i_socket, SIOCGIFHWADDR, &interface );
193 * Getting address, port, ... of the server
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) );
206 * Getting address, port, ... of the client
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) );
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 */
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",
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
241 /* Send the message */
242 intf_DbgMsg("%s\n", mess);
243 sendto(i_socket,mess,80,0,(struct sockaddr *)&sa_server,sizeof(struct sockaddr));
245 /*Close the socket */