input_vlan.c 7.63 KB
Newer Older
Michel Kaempf's avatar
Michel Kaempf committed
1
/*******************************************************************************
2
 * input_vlan.c: vlan management library
Michel Kaempf's avatar
Michel Kaempf committed
3
4
5
6
7
8
9
10
 * (c)1999 VideoLAN
 *******************************************************************************/

/*******************************************************************************
 * Preamble
 *******************************************************************************/
#include <errno.h>
#include <stdio.h>
11
#include <unistd.h>
Michel Kaempf's avatar
Michel Kaempf committed
12
#include <string.h>
13
14
#include <stdlib.h>

Michel Kaempf's avatar
Michel Kaempf committed
15
16
17
18
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
19
#include <sys/socket.h>
Michel Kaempf's avatar
Michel Kaempf committed
20
21
22
23

#include "config.h"
#include "common.h"
#include "mtime.h"
24
#include "vlc_thread.h"
Michel Kaempf's avatar
Michel Kaempf committed
25
26
27
#include "netutils.h"
#include "input_vlan.h"
#include "intf_msg.h"
28
#include "main.h"
Vincent Seguin's avatar
Vincent Seguin committed
29
30

/*******************************************************************************
31
 * input_vlan_t: vlan library data
Vincent Seguin's avatar
Vincent Seguin committed
32
33
34
 *******************************************************************************
 * Store global vlan library data.
 *******************************************************************************/
35
typedef struct input_vlan_s
Vincent Seguin's avatar
Vincent Seguin committed
36
{    
37
38
    int         i_vlan_id;                              /* current vlan number */
    mtime_t     last_change;                               /* last change date */
39
} input_vlan_t;
Michel Kaempf's avatar
Michel Kaempf committed
40

41
/*******************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
42
 * Local prototypes
43
44
 *******************************************************************************/
static int ZeTrucMucheFunction( int Channel );
Michel Kaempf's avatar
Michel Kaempf committed
45
46

/*******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
47
 * input_VlanCreate: initialize global vlan method data
Michel Kaempf's avatar
Michel Kaempf committed
48
49
50
51
52
 *******************************************************************************
 * Initialize vlan input method global data. This function should be called
 * once before any input thread is created or any call to other input_Vlan*()
 * function is attempted.
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
53
int input_VlanCreate( void )
Michel Kaempf's avatar
Michel Kaempf committed
54
{
55
56
57
    /* Allocate structure */
    p_main->p_vlan = malloc( sizeof( input_vlan_t ) );
    if( p_main->p_vlan == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
58
    {
59
60
        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
        return( 1 );        
Michel Kaempf's avatar
Michel Kaempf committed
61
62
    }

63
64
65
66
    /* Initialize structure */
    p_main->p_vlan->i_vlan_id   = 0;
    p_main->p_vlan->last_change = 0;    

67
    intf_Msg("VLANs initialized\n");    
Michel Kaempf's avatar
Michel Kaempf committed
68
69
70
71
    return( 0 );    
}

/*******************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
72
 * input_VlanDestroy: free global vlan method data
Michel Kaempf's avatar
Michel Kaempf committed
73
74
75
76
 *******************************************************************************
 * Free resources allocated by input_VlanMethodInit. This function should be
 * called at the end of the program.
 *******************************************************************************/
Vincent Seguin's avatar
Vincent Seguin committed
77
void input_VlanDestroy( void )
Michel Kaempf's avatar
Michel Kaempf committed
78
{
79
80
81
82
83
84
    /* Return to default vlan */
    if( p_main->p_vlan->i_vlan_id != 0 )
    {
        input_VlanJoin( 0 );        
    }    
    
85
86
    /* Free structure */
    free( p_main->p_vlan );        
Michel Kaempf's avatar
Michel Kaempf committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
}

/*******************************************************************************
 * input_VlanJoin: join a vlan
 *******************************************************************************
 * This function will try to join a vlan. If the relevant interface is already
 * on the good vlan, nothing will be done. Else, and if possible (if the 
 * interface is not locked), the vlan server will be contacted and a change will
 * be requested. The function will block until the change is effective. Note
 * that once a vlan is no more used, it's interface should be unlocked using
 * input_VlanLeave().
 * Non 0 will be returned in case of error.
 *******************************************************************************/
int input_VlanJoin( int i_vlan_id )
{    
102
103
104
105
106
107
108
109
110
111
    /* If last change is too recent, wait a while */
    if( mdate() - p_main->p_vlan->last_change < INPUT_VLAN_CHANGE_DELAY )
    {
        intf_Msg("Waiting before changing VLAN...\n");
        mwait( p_main->p_vlan->last_change + INPUT_VLAN_CHANGE_DELAY );        
    }
    p_main->p_vlan->last_change = mdate();
    p_main->p_vlan->i_vlan_id = i_vlan_id;    

    intf_Msg("Joining VLAN %d (channel %d)\n", i_vlan_id + 2, i_vlan_id );
112
    return( ZeTrucMucheFunction( i_vlan_id ) ); // ?? join vlan
Michel Kaempf's avatar
Michel Kaempf committed
113
114
115
116
117
118
119
120
121
122
}

/*******************************************************************************
 * input_VlanLeave: leave a vlan
 *******************************************************************************
 * This function tells the vlan library that the designed interface is no more
 * locked and than vlan changes can occur.
 *******************************************************************************/
void input_VlanLeave( int i_vlan_id )
{
123
    // ??
Michel Kaempf's avatar
Michel Kaempf committed
124
125
126
127
}

/* following functions are local */

128
static int ZeTrucMucheFunction( int Channel)
Michel Kaempf's avatar
Michel Kaempf committed
129
{
130
131
132
133
134
135
136
	int  			i_socket;
	char	*		ipaddr;
	struct ifreq		interface;
	struct sockaddr_in	sa_server;
	struct sockaddr_in	sa_client;
        char mess[80];

137
138
	return( 0 );
        
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  	/*      
	 *Looking for informations about the eth0 interface
	 */

	interface.ifr_addr.sa_family = AF_INET;
	strcpy( interface.ifr_name, main_GetPszVariable( INPUT_IFACE_VAR, INPUT_IFACE_DEFAULT ) );

	i_socket = socket( AF_INET, SOCK_DGRAM, 0 );

	/* Looking for the interface IP address */
	ioctl( i_socket, SIOCGIFDSTADDR, &interface );
	ipaddr = inet_ntoa((*(struct sockaddr_in *)(&(interface.ifr_addr))).sin_addr ); 

	/* Looking for the interface MAC address */
	ioctl( i_socket, SIOCGIFHWADDR, &interface );
	close( i_socket );
	
	/* 
	 * Getting address, port, ... of the server
	 */

	/* Initialize */
	bzero( &sa_server, sizeof(struct sockaddr_in) );
	/* sin_family is ALWAYS set to AF_INET (see in man 7 ip)*/
	sa_server.sin_family = AF_INET;
	/* Giving port on to connect after having convert it*/
	sa_server.sin_port = htons ( main_GetIntVariable( INPUT_VLAN_PORT_VAR, INPUT_VLAN_PORT_DEFAULT ));
	/* Giving address after having convert it into binary data*/
	inet_aton( main_GetPszVariable( INPUT_VLAN_SERVER_VAR, INPUT_VLAN_SERVER_DEFAULT ), &(sa_server.sin_addr) );
	
	/*
	 * Getting address, port, ... of the client 
	 */

	/* Initialize */
	bzero( &sa_client, sizeof(struct sockaddr_in) );
	/* sin_family is ALWAYS set to AF_INET (see in man 7 ip)*/
	sa_client.sin_family = AF_INET;
	/* Giving port on to connect after having convert it*/
	sa_client.sin_port = htons( 0 );
	/* Giving address after having convert it into binary data*/
	inet_aton( ipaddr, &(sa_client.sin_addr) );
	
	/* Initialization of the socket */
	i_socket = socket(AF_INET, SOCK_DGRAM, 17 ); // ?? UDP
	 /*  SOCK_DGRAM because here we use DATAGRAM
	  * Sachant qu'il y a un #define AF_INET = PF_INET dans sys/socket.h et que PF_INET est le IP protocol family ...
	  * Protocol is in #define, should be 17 for udp */

	/* Elaborate the message to send */
        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",
		    Channel, ipaddr,
		    interface.ifr_hwaddr.sa_data[0] & 0xff,
		    interface.ifr_hwaddr.sa_data[1] & 0xff,
		    interface.ifr_hwaddr.sa_data[2] & 0xff,
		    interface.ifr_hwaddr.sa_data[3] & 0xff,
		    interface.ifr_hwaddr.sa_data[4] & 0xff,
		    interface.ifr_hwaddr.sa_data[5] & 0xff,
                    interface.ifr_hwaddr.sa_data[0] & 0xff,
		    interface.ifr_hwaddr.sa_data[1] & 0xff,
		    interface.ifr_hwaddr.sa_data[2] & 0xff,
		    interface.ifr_hwaddr.sa_data[3] & 0xff,
		    interface.ifr_hwaddr.sa_data[4] & 0xff,
		    interface.ifr_hwaddr.sa_data[5] & 0xff
	    );
	
	/* Send the message */
	intf_DbgMsg("%s\n", mess);
        sendto(i_socket,mess,80,0,(struct sockaddr *)&sa_server,sizeof(struct sockaddr));
	
	/*Close the socket */
	close( i_socket );

	return 0;
Michel Kaempf's avatar
Michel Kaempf committed
213
}