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
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <stdio.h> /* sprintf() */
30 #include <unistd.h> /* close() */
31 #include <string.h> /* strerror(), bzero() */
32 #include <stdlib.h> /* free() */
33 #include <sys/time.h> /* timeval */
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) */
55 #include "input_vlan.h"
60 /*****************************************************************************
61 * input_vlan_t: vlan library data
62 *****************************************************************************
63 * Store global vlan library data.
64 *****************************************************************************/
65 typedef struct input_vlan_s
67 int i_vlan_id; /* current vlan number */
68 mtime_t last_change; /* last change date */
71 /*****************************************************************************
73 *****************************************************************************/
75 /*****************************************************************************
76 * input_VlanCreate: initialize global vlan method data
77 *****************************************************************************
78 * Initialize vlan input method global data. This function should be called
79 * once before any input thread is created or any call to other input_Vlan*()
80 * function is attempted.
81 *****************************************************************************/
82 int input_VlanCreate( void )
85 intf_ErrMsg( "error: vlans are not supported under beos\n" );
88 /* Allocate structure */
89 p_main->p_vlan = malloc( sizeof( input_vlan_t ) );
90 if( p_main->p_vlan == NULL )
92 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
96 /* Initialize structure */
97 p_main->p_vlan->i_vlan_id = 0;
98 p_main->p_vlan->last_change = 0;
100 intf_Msg("VLANs initialized\n");
102 #endif /* SYS_BEOS */
105 /*****************************************************************************
106 * input_VlanDestroy: free global vlan method data
107 *****************************************************************************
108 * Free resources allocated by input_VlanMethodInit. This function should be
109 * called at the end of the program.
110 *****************************************************************************/
111 void input_VlanDestroy( void )
113 /* Return to default vlan */
114 if( p_main->p_vlan->i_vlan_id != 0 )
120 free( p_main->p_vlan );
123 /*****************************************************************************
124 * input_VlanLeave: leave a vlan
125 *****************************************************************************
126 * This function tells the vlan library that the designed interface is no more
127 * locked and than vlan changes can occur.
128 *****************************************************************************/
129 void input_VlanLeave( int i_vlan_id )
134 /*****************************************************************************
135 * input_VlanJoin: join a vlan
136 *****************************************************************************
137 * This function will try to join a vlan. If the relevant interface is already
138 * on the good vlan, nothing will be done. Else, and if possible (if the
139 * interface is not locked), the vlan server will be contacted and a change will
140 * be requested. The function will block until the change is effective. Note
141 * that once a vlan is no more used, it's interface should be unlocked using
143 * Non 0 will be returned in case of error.
144 *****************************************************************************/
145 int input_VlanJoin( int i_vlan_id )
151 #define SERVER "138.195.130.90"
152 #define INTERFACE "eth0"
153 /* default server port */
154 #define VLANSERVER_PORT 6010
158 struct ifreq interface;
159 struct sockaddr_in sa_server;
160 struct sockaddr_in sa_client;
161 unsigned int version = 12;
163 struct timeval *date_cl;
165 long unsigned int date;
170 /* If last change is too recent, wait a while */
171 if( mdate() - p_main->p_vlan->last_change < INPUT_VLAN_CHANGE_DELAY )
173 intf_Msg("Waiting before changing VLAN...\n");
174 mwait( p_main->p_vlan->last_change + INPUT_VLAN_CHANGE_DELAY );
176 p_main->p_vlan->last_change = mdate();
177 p_main->p_vlan->i_vlan_id = i_vlan_id;
179 intf_Msg("Joining VLAN %d (channel %d)\n", i_vlan_id + 2, i_vlan_id );
182 *Looking for informations about the eth0 interface
184 interface.ifr_addr.sa_family = AF_INET;
185 strcpy( interface.ifr_name, INTERFACE );
189 * Initialysing the socket
191 socket_cl = socket( AF_INET, SOCK_DGRAM, 0 );
192 intf_DbgMsg( "socket %d\n", socket_cl );
196 * Getting the server's information
198 bzero (&sa_server, sizeof (struct sockaddr_in));
199 sa_server.sin_family = AF_INET;
200 sa_server.sin_port = htons (VLANSERVER_PORT);
201 inet_aton (SERVER, &(sa_server.sin_addr));
204 * Looking for the interface MAC address
206 ioctl( socket_cl, SIOCGIFHWADDR, &interface );
207 intf_DbgMsg( "macaddr == %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
208 interface.ifr_hwaddr.sa_data[0] & 0xff,
209 interface.ifr_hwaddr.sa_data[1] & 0xff,
210 interface.ifr_hwaddr.sa_data[2] & 0xff,
211 interface.ifr_hwaddr.sa_data[3] & 0xff,
212 interface.ifr_hwaddr.sa_data[4] & 0xff,
213 interface.ifr_hwaddr.sa_data[5] & 0xff );
216 * Getting date of the client
218 date_cl = malloc (sizeof (struct timeval));
219 if (gettimeofday (date_cl, 0) == -1)
223 date = date_cl->tv_sec;
224 intf_DbgMsg ("date %lu\n", date);
228 * Build of the message
230 sprintf (mess, "%d %u %lu %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x \n",
231 i_vlan_id, version, date,
232 interface.ifr_hwaddr.sa_data[0] & 0xff,
233 interface.ifr_hwaddr.sa_data[1] & 0xff,
234 interface.ifr_hwaddr.sa_data[2] & 0xff,
235 interface.ifr_hwaddr.sa_data[3] & 0xff,
236 interface.ifr_hwaddr.sa_data[4] & 0xff,
237 interface.ifr_hwaddr.sa_data[5] & 0xff);
238 intf_DbgMsg ("The message is %s\n", mess);
244 bzero (&sa_client, sizeof (struct sockaddr_in));
245 sa_client.sin_family = AF_INET;
246 sa_client.sin_port = htons( 4312 );
247 sa_client.sin_addr.s_addr = INADDR_ANY;
248 intf_DbgMsg ("socket %d\n", socket_cl = socket( AF_INET, SOCK_DGRAM, 0 ));
249 fromlen = sizeof (struct sockaddr);
250 intf_DbgMsg( "bind %i\n", bind( socket_cl, (struct sockaddr *)(&sa_client), sizeof( struct sockaddr )));
256 sendto (socket_cl, mess, 80, 0, (struct sockaddr *)(&sa_server), sizeof (struct sockaddr ));
261 do {z++;} while (mess[z]!=':');
262 do {z++;} while (mess[z]!='e');
263 printf("meuuh %d %d\n",(unsigned)mess[z+3],(unsigned)mess[z+4]);
269 * Waiting 5 sec for one answer from the server
274 FD_SET( socket_cl, &rfds );
275 nbanswer = select( socket_cl + 1, &rfds, NULL, NULL, &time);
278 intf_DbgMsg( "no answer\n" );
280 else if( nbanswer == -1 )
282 intf_DbgMsg( "I couldn't recieve the answer\n" );
286 recvfrom (socket_cl, &answer, sizeof( char ), 0, (struct sockaddr *)(&sa_client), &fromlen);
287 intf_DbgMsg( "the answer : %hhd\n", answer );
290 intf_DbgMsg( "The server doesn't succed to create the thread\n" );
292 else if( answer == 0 )
294 intf_DbgMsg( "The server try to change the channel\n" );
298 intf_DbgMsg( "I don't know what is this answer !\n" );