netutils.c 17.9 KB
Newer Older
1
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
2
 * netutils.c: various network functions
3
 *****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
4
 * Copyright (C) 1999, 2000, 2001 VideoLAN
Sam Hocevar's avatar
   
Sam Hocevar committed
5
 * $Id: netutils.c,v 1.37 2001/06/02 01:09:03 sam Exp $
6
 *
7
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
Benoit Steiner's avatar
   
Benoit Steiner committed
8
 *          Benoit Steiner <benny@via.ecp.fr>
Henri Fallon's avatar
   
Henri Fallon committed
9
 *          Henri Fallon <henri@videolan.org>
Xavier Marchesini's avatar
Xavier Marchesini committed
10
 *          Xavier Marchesini <xav@via.ecp.fr>
11
12
13
14
15
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
Sam Hocevar's avatar
   
Sam Hocevar committed
16
 *
17
18
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
21
 *
22
23
24
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
26

27
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
28
 * Preamble
29
 *****************************************************************************/
30
31
#include "defs.h"

32
33
#include <stdlib.h>                             /* free(), realloc(), atoi() */
#include <errno.h>                                                /* errno() */
Sam Hocevar's avatar
   
Sam Hocevar committed
34
#include <string.h>                                              /* memset() */
Sam Hocevar's avatar
   
Sam Hocevar committed
35
36

#ifdef HAVE_UNISTD_H
Sam Hocevar's avatar
   
Sam Hocevar committed
37
#include <unistd.h>                                         /* gethostname() */
Sam Hocevar's avatar
   
Sam Hocevar committed
38
39
40
41
42
#elif defined( _MSC_VER ) && defined( _WIN32 )
#include <io.h>
#endif

#if !defined( _MSC_VER )
Henri Fallon's avatar
   
Henri Fallon committed
43
#include <sys/time.h>                                        /* gettimeofday */
Sam Hocevar's avatar
   
Sam Hocevar committed
44
#endif
45

Sam Hocevar's avatar
   
Sam Hocevar committed
46
#if !defined( WIN32 )
Sam Hocevar's avatar
   
Sam Hocevar committed
47
#include <netdb.h>                                        /* gethostbyname() */
48
49
#include <netinet/in.h>                               /* BSD: struct in_addr */
#include <sys/socket.h>                              /* BSD: struct sockaddr */
Sam Hocevar's avatar
   
Sam Hocevar committed
50
51
#endif

52
#ifdef HAVE_ARPA_INET_H
53
54
55
#include <arpa/inet.h>                           /* inet_ntoa(), inet_aton() */
#endif

Henri Fallon's avatar
   
Henri Fallon committed
56
57
58
59
#ifdef SYS_LINUX
#include <sys/ioctl.h>                                            /* ioctl() */
#endif

Sam Hocevar's avatar
   
Sam Hocevar committed
60
#if defined( WIN32 )                    /* tools to get the MAC adress from  */
Xavier Marchesini's avatar
Xavier Marchesini committed
61
62
63
64
#include <windows.h>                    /* the interface under Windows	     */
#include <stdio.h>
#endif

Sam Hocevar's avatar
   
Sam Hocevar committed
65
#ifdef HAVE_NET_IF_H
66
67
#include <net/if.h>                            /* interface (arch-dependent) */
#endif
Henri Fallon's avatar
   
Henri Fallon committed
68

Benoit Steiner's avatar
   
Benoit Steiner committed
69
#ifdef HAVE_SYS_SOCKIO_H
Benoit Steiner's avatar
Benoit Steiner committed
70
#include <sys/sockio.h>
Benoit Steiner's avatar
   
Benoit Steiner committed
71
#endif
Michel Kaempf's avatar
Michel Kaempf committed
72
73
74
75

#include "config.h"
#include "common.h"
#include "mtime.h"
Henri Fallon's avatar
   
Henri Fallon committed
76
#include "threads.h"
Henri Fallon's avatar
   
Henri Fallon committed
77
#include "main.h"
Michel Kaempf's avatar
Michel Kaempf committed
78
79
80

#include "intf_msg.h"

Sam Hocevar's avatar
   
Sam Hocevar committed
81
#include "netutils.h"
Henri Fallon's avatar
   
Henri Fallon committed
82

83
/*****************************************************************************
Henri Fallon's avatar
   
Henri Fallon committed
84
85
86
87
88
89
90
91
92
 * input_channel_t: channel library data
 *****************************************************************************
 * Store global channel library data.
 * The part of the code concerning the channel changing process is unstable
 * as it depends on the VideoLAN channel server, which isn't frozen for
 * the time being.
 *****************************************************************************/
typedef struct input_channel_s
{
Sam Hocevar's avatar
   
Sam Hocevar committed
93
    int         i_channel;                         /* current channel number */
Henri Fallon's avatar
   
Henri Fallon committed
94
95
96
    mtime_t     last_change;                             /* last change date */
} input_channel_t;

Xavier Marchesini's avatar
Xavier Marchesini committed
97
/*****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
98
99
100
101
102
 * Local prototypes
 *****************************************************************************/
static int GetMacAddress   ( int i_socket, char *psz_mac );
#ifdef WIN32
static int GetAdapterInfo  ( int i_adapter, char *psz_string );
Xavier Marchesini's avatar
Xavier Marchesini committed
103
104
#endif

Henri Fallon's avatar
   
Henri Fallon committed
105
106
/*****************************************************************************
 * network_BuildLocalAddr : fill a sockaddr_in structure for local binding
107
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
108
int network_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port,
Henri Fallon's avatar
   
Henri Fallon committed
109
                            char * psz_broadcast )
Michel Kaempf's avatar
Michel Kaempf committed
110
{
Henri Fallon's avatar
   
Henri Fallon committed
111
112
    char                psz_hostname[INPUT_MAX_SOURCE_LENGTH];
    struct hostent    * p_hostent;
Sam Hocevar's avatar
   
Sam Hocevar committed
113

Henri Fallon's avatar
   
Henri Fallon committed
114
115
    /* Reset struct */
    memset( p_socket, 0, sizeof( struct sockaddr_in ) );
Sam Hocevar's avatar
   
Sam Hocevar committed
116
    p_socket->sin_family = AF_INET;                                /* family */
Henri Fallon's avatar
   
Henri Fallon committed
117
    p_socket->sin_port = htons( i_port );
Henri Fallon's avatar
   
Henri Fallon committed
118
    if( psz_broadcast == NULL )
Henri Fallon's avatar
   
Henri Fallon committed
119
120
121
122
123
124
125
126
    {
        /* Try to get our own IP */
        if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
        {
            intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
                         strerror( errno ) );
            return( -1 );
        }
Michel Kaempf's avatar
Michel Kaempf committed
127

Henri Fallon's avatar
   
Henri Fallon committed
128
129
    }
    else
Sam Hocevar's avatar
   
Sam Hocevar committed
130
131
    {
        /* I didn't manage to make INADDR_ANYT work, even with setsockopt
Henri Fallon's avatar
   
Henri Fallon committed
132
133
134
         * so, as it's kludgy to try and determine the broadcast addr
         * it is passed as an argument in the command line */
        strncpy( psz_hostname, psz_broadcast, INPUT_MAX_SOURCE_LENGTH );
135
    }
Henri Fallon's avatar
   
Henri Fallon committed
136

137
138
    /* Try to convert address directly from in_addr - this will work if
     * psz_in_addr is dotted decimal. */
Henri Fallon's avatar
   
Henri Fallon committed
139
140
#ifdef HAVE_ARPA_INET_H
    if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
141
#else
Henri Fallon's avatar
   
Henri Fallon committed
142
    if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
143
#endif
Michel Kaempf's avatar
Michel Kaempf committed
144
    {
Henri Fallon's avatar
   
Henri Fallon committed
145
146
        /* We have a fqdn, try to find its address */
        if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
147
        {
Henri Fallon's avatar
   
Henri Fallon committed
148
            intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
149
            return( -1 );
Michel Kaempf's avatar
Michel Kaempf committed
150
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
151

Michel Kaempf's avatar
Michel Kaempf committed
152
        /* Copy the first address of the host in the socket address */
Sam Hocevar's avatar
   
Sam Hocevar committed
153
        memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
Henri Fallon's avatar
   
Henri Fallon committed
154
                 p_hostent->h_length );
Michel Kaempf's avatar
Michel Kaempf committed
155
    }
156
    return( 0 );
Michel Kaempf's avatar
Michel Kaempf committed
157
158
}

159
/*****************************************************************************
Henri Fallon's avatar
   
Henri Fallon committed
160
 * network_BuildRemoteAddr : fill a sockaddr_in structure for remote host
161
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
162
int network_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
Michel Kaempf's avatar
Michel Kaempf committed
163
{
Henri Fallon's avatar
   
Henri Fallon committed
164
    struct hostent            * p_hostent;
Sam Hocevar's avatar
   
Sam Hocevar committed
165

Henri Fallon's avatar
   
Henri Fallon committed
166
167
    /* Reset structure */
    memset( p_socket, 0, sizeof( struct sockaddr_in ) );
Sam Hocevar's avatar
   
Sam Hocevar committed
168
169
    p_socket->sin_family = AF_INET;                                /* family */
    p_socket->sin_port = htons( 0 );               /* This is for remote end */
Sam Hocevar's avatar
   
Sam Hocevar committed
170

Henri Fallon's avatar
   
Henri Fallon committed
171
172
     /* Try to convert address directly from in_addr - this will work if
      * psz_in_addr is dotted decimal. */
Michel Kaempf's avatar
Michel Kaempf committed
173

Henri Fallon's avatar
   
Henri Fallon committed
174
175
#ifdef HAVE_ARPA_INET_H
    if( !inet_aton( psz_server, &p_socket->sin_addr) )
Benoit Steiner's avatar
Benoit Steiner committed
176
#else
Henri Fallon's avatar
   
Henri Fallon committed
177
    if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
178
#endif
Michel Kaempf's avatar
Michel Kaempf committed
179
    {
Henri Fallon's avatar
   
Henri Fallon committed
180
181
        /* We have a fqdn, try to find its address */
        if ( (p_hostent = gethostbyname(psz_server)) == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
182
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
183
            intf_ErrMsg( "BuildRemoteAddr: unknown host %s",
Henri Fallon's avatar
   
Henri Fallon committed
184
185
                         psz_server );
            return( -1 );
Michel Kaempf's avatar
Michel Kaempf committed
186
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
187

Henri Fallon's avatar
   
Henri Fallon committed
188
        /* Copy the first address of the host in the socket address */
Sam Hocevar's avatar
   
Sam Hocevar committed
189
        memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
Henri Fallon's avatar
   
Henri Fallon committed
190
                 p_hostent->h_length );
Michel Kaempf's avatar
Michel Kaempf committed
191
    }
Henri Fallon's avatar
   
Henri Fallon committed
192
    return( 0 );
Michel Kaempf's avatar
Michel Kaempf committed
193
}
Sam Hocevar's avatar
   
Sam Hocevar committed
194

Henri Fallon's avatar
   
Henri Fallon committed
195
196
197
198
/*****************************************************************************
 * network_ChannelCreate: initialize global channel method data
 *****************************************************************************
 * Initialize channel input method global data. This function should be called
Sam Hocevar's avatar
   
Sam Hocevar committed
199
 * once before any input thread is created or any call to other
Henri Fallon's avatar
   
Henri Fallon committed
200
201
202
203
 * input_Channel*() function is attempted.
 *****************************************************************************/
int network_ChannelCreate( void )
{
Sam Hocevar's avatar
   
Sam Hocevar committed
204
205
/* Even when BSD are supported, BeOS is not likely to be supported, so
 * I prefer to put it apart */
Sam Hocevar's avatar
   
Sam Hocevar committed
206
#if defined( SYS_BEOS )
Henri Fallon's avatar
   
Henri Fallon committed
207
208
    intf_ErrMsg( "error: channel changing is not yet supported under BeOS" );
    return( 1 );
Sam Hocevar's avatar
   
Sam Hocevar committed
209

Sam Hocevar's avatar
   
Sam Hocevar committed
210
#elif defined( SYS_LINUX ) || defined( WIN32 )
Henri Fallon's avatar
   
Henri Fallon committed
211
212
213
214
215
/* FIXME : channels handling only work for linux */
    /* Allocate structure */
    p_main->p_channel = malloc( sizeof( input_channel_t ) );
    if( p_main->p_channel == NULL )
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
216
        intf_ErrMsg( "network error: could not create channel bank" );
Henri Fallon's avatar
   
Henri Fallon committed
217
218
219
220
        return( -1 );
    }

    /* Initialize structure */
Sam Hocevar's avatar
   
Sam Hocevar committed
221
    p_main->p_channel->i_channel   = 0;
Henri Fallon's avatar
   
Henri Fallon committed
222
223
    p_main->p_channel->last_change = 0;

Sam Hocevar's avatar
   
Sam Hocevar committed
224
    intf_Msg( "network: channels initialized" );
Henri Fallon's avatar
   
Henri Fallon committed
225
    return( 0 );
Sam Hocevar's avatar
   
Sam Hocevar committed
226

Henri Fallon's avatar
   
Henri Fallon committed
227
#else
Sam Hocevar's avatar
   
Sam Hocevar committed
228
    intf_ErrMsg( "network error : channels not supported" );
Sam Hocevar's avatar
   
Sam Hocevar committed
229
    return( 1 );
Sam Hocevar's avatar
   
Sam Hocevar committed
230
231

#endif
Henri Fallon's avatar
   
Henri Fallon committed
232
233
234
235
236
}

/*****************************************************************************
 * network_ChannelJoin: join a channel
 *****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
237
238
239
240
241
 * This function will try to join a channel. If the relevant interface is
 * already on the good channel, nothing will be done. Else, and if possible
 * (if the interface is not locked), the channel server will be contacted
 * and a change will be requested. The function will block until the change
 * is effective. Note that once a channel is no more used, it's interface
Henri Fallon's avatar
   
Henri Fallon committed
242
243
244
 * should be unlocked using input_ChannelLeave().
 * Non 0 will be returned in case of error.
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
245
int network_ChannelJoin( int i_channel )
Henri Fallon's avatar
   
Henri Fallon committed
246
{
Sam Hocevar's avatar
   
Sam Hocevar committed
247
/* I still prefer to put BeOS a bit apart */
Sam Hocevar's avatar
   
Sam Hocevar committed
248
#if defined( SYS_BEOS )
Sam Hocevar's avatar
   
Sam Hocevar committed
249
    intf_ErrMsg( "network error: channels are not yet supported under BeOS" );
Henri Fallon's avatar
   
Henri Fallon committed
250
    return( -1 );
Sam Hocevar's avatar
   
Sam Hocevar committed
251

Sam Hocevar's avatar
   
Sam Hocevar committed
252
#elif defined( SYS_LINUX ) || defined( WIN32 )
Sam Hocevar's avatar
   
Sam Hocevar committed
253
    int                 i_socket;
Marc Ariberti's avatar
Marc Ariberti committed
254
    int                 i_fromlen;
Henri Fallon's avatar
   
Henri Fallon committed
255
256
    struct sockaddr_in  sa_server;
    struct sockaddr_in  sa_client;
Marc Ariberti's avatar
Marc Ariberti committed
257
    unsigned int        i_version = 12;
Sam Hocevar's avatar
   
Sam Hocevar committed
258
259
    char                psz_mess[ 80 ];
    char                psz_mac[ 40 ];
Marc Ariberti's avatar
Marc Ariberti committed
260
    char                i_mess_length = 80;
Sam Hocevar's avatar
   
Sam Hocevar committed
261
262
    unsigned long int   i_date;
    struct timeval      answer_delay;
Marc Ariberti's avatar
Marc Ariberti committed
263
264
    int                 i_nbanswer;
    char                i_answer;
Sam Hocevar's avatar
   
Sam Hocevar committed
265
    fd_set              fds;
Sam Hocevar's avatar
   
Sam Hocevar committed
266
    unsigned int        i_rc;
267
    char *              psz_channel_server;
Sam Hocevar's avatar
   
Sam Hocevar committed
268

269
270
    if( !main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
                              INPUT_NETWORK_CHANNEL_DEFAULT  ) )
Henri Fallon's avatar
   
Henri Fallon committed
271
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
272
273
        intf_ErrMsg( "network: channels disabled, to enable them, use the"
                     "--channels option" );
Henri Fallon's avatar
   
Henri Fallon committed
274
275
        return( -1 );
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
276
277
278

    /* debug */
    intf_DbgMsg( "network: ChannelJoin : %d", i_channel );
Henri Fallon's avatar
   
Henri Fallon committed
279
280
281
    /* If last change is too recent, wait a while */
    if( mdate() - p_main->p_channel->last_change < INPUT_CHANNEL_CHANGE_DELAY )
    {
282
        intf_WarnMsg( 2, "network: waiting before changing channel" );
Henri Fallon's avatar
   
Henri Fallon committed
283
284
        mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY );
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
285

Henri Fallon's avatar
   
Henri Fallon committed
286
    p_main->p_channel->last_change = mdate();
Sam Hocevar's avatar
   
Sam Hocevar committed
287
    p_main->p_channel->i_channel   = i_channel;
Henri Fallon's avatar
   
Henri Fallon committed
288

289
    intf_WarnMsg( 2, "network: joining channel %d", i_channel );
Henri Fallon's avatar
   
Henri Fallon committed
290
291

    /*
Sam Hocevar's avatar
   
Sam Hocevar committed
292
     * Initializing the socket
Henri Fallon's avatar
   
Henri Fallon committed
293
     */
Sam Hocevar's avatar
   
Sam Hocevar committed
294
    i_socket = socket( AF_INET, SOCK_DGRAM, 0 );
Henri Fallon's avatar
   
Henri Fallon committed
295

Sam Hocevar's avatar
   
Sam Hocevar committed
296
297
    /*
     * Getting the server's information
Henri Fallon's avatar
   
Henri Fallon committed
298
     */
299
300
301
302
303
304
    intf_WarnMsg( 6, "Channel server: %s port: %d",
            main_GetPszVariable( INPUT_CHANNEL_SERVER_VAR,
                                 INPUT_CHANNEL_SERVER_DEFAULT ),
            main_GetIntVariable( INPUT_CHANNEL_PORT_VAR,
                                 INPUT_CHANNEL_PORT_DEFAULT ) );

Sam Hocevar's avatar
   
Sam Hocevar committed
305
    memset( &sa_server, 0x00, sizeof(struct sockaddr_in) );
Marc Ariberti's avatar
Marc Ariberti committed
306
    sa_server.sin_family = AF_INET;
307
308
309
310
311
    sa_server.sin_port   = htons( main_GetIntVariable( INPUT_CHANNEL_PORT_VAR,
                                  INPUT_CHANNEL_PORT_DEFAULT ) );

    psz_channel_server = strdup( main_GetPszVariable( INPUT_CHANNEL_SERVER_VAR,
                                 INPUT_CHANNEL_SERVER_DEFAULT ) );
Sam Hocevar's avatar
   
Sam Hocevar committed
312
#ifdef HAVE_ARPA_INET_H
313
    inet_aton( psz_channel_server, &sa_server.sin_addr );
Sam Hocevar's avatar
   
Sam Hocevar committed
314
#else
315
    sa_server.sin_addr.s_addr = inet_addr( psz_channel_server );
Sam Hocevar's avatar
   
Sam Hocevar committed
316
#endif
317
    free( psz_channel_server );
Marc Ariberti's avatar
Marc Ariberti committed
318

Henri Fallon's avatar
   
Henri Fallon committed
319
320
321
    /*
     * Looking for the interface MAC address
     */
Sam Hocevar's avatar
   
Sam Hocevar committed
322
    if( GetMacAddress( i_socket, psz_mac ) )
Henri Fallon's avatar
   
Henri Fallon committed
323
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
324
325
        intf_ErrMsg( "network error: failed getting MAC address" );
        return( -1 );
Marc Ariberti's avatar
Marc Ariberti committed
326
    }
Henri Fallon's avatar
   
Henri Fallon committed
327

Sam Hocevar's avatar
   
Sam Hocevar committed
328
329
330
331
332
    /*
     * Getting date of the client in seconds
     */
    i_date = mdate() / 1000000;
    intf_DbgMsg( "vlcs: date %lu", i_date );
Henri Fallon's avatar
   
Henri Fallon committed
333

Sam Hocevar's avatar
   
Sam Hocevar committed
334
    /*
Henri Fallon's avatar
   
Henri Fallon committed
335
336
     * Build of the message
     */
Sam Hocevar's avatar
   
Sam Hocevar committed
337
338
339
340
    sprintf( psz_mess, "%d %u %lu %s \n",
             i_channel, i_version, i_date, psz_mac );

    intf_DbgMsg( "vlcs: The message is %s", psz_mess );
Henri Fallon's avatar
   
Henri Fallon committed
341
342
343
344

    /*
     * Open the socket 2
     */
Sam Hocevar's avatar
   
Sam Hocevar committed
345
    memset( &sa_client, 0x00, sizeof(struct sockaddr_in) );
Marc Ariberti's avatar
Marc Ariberti committed
346
347
348
349
    sa_client.sin_family = AF_INET;
    sa_client.sin_port   = htons(4312);
    sa_client.sin_addr.s_addr = INADDR_ANY;
    i_fromlen = sizeof( struct sockaddr );
Sam Hocevar's avatar
   
Sam Hocevar committed
350
    i_rc = bind( i_socket, (struct sockaddr *)(&sa_client),\
Marc Ariberti's avatar
Marc Ariberti committed
351
352
                 sizeof(struct sockaddr) );
    if ( i_rc )
Henri Fallon's avatar
   
Henri Fallon committed
353
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
354
        intf_ErrMsg( "vlcs: Unable to bind socket:%u ", i_rc );
Henri Fallon's avatar
   
Henri Fallon committed
355
356
357
358
359
360
361
362
    /* TODO put CS_R_BIND in types.h*/
    /*    return CS_R_SOCKET;*/
        return -1;
    }

    /*
     * Send the message
     */
Sam Hocevar's avatar
   
Sam Hocevar committed
363
    sendto( i_socket, psz_mess, i_mess_length, 0, \
Marc Ariberti's avatar
Marc Ariberti committed
364
365
            (struct sockaddr *)(&sa_server),   \
            sizeof(struct sockaddr) );
Sam Hocevar's avatar
   
Sam Hocevar committed
366

Henri Fallon's avatar
   
Henri Fallon committed
367
368
369
     /*
     * Waiting 5 sec for one answer from the server
     */
Sam Hocevar's avatar
   
Sam Hocevar committed
370
371
    answer_delay.tv_sec  = 5;
    answer_delay.tv_usec = 0;
Sam Hocevar's avatar
   
Sam Hocevar committed
372
373
374
    FD_ZERO( &fds );
    FD_SET( i_socket, &fds );
    i_nbanswer = select( i_socket + 1, &fds, NULL, NULL, &answer_delay );
Sam Hocevar's avatar
   
Sam Hocevar committed
375
376

    switch( i_nbanswer )
Henri Fallon's avatar
   
Henri Fallon committed
377
    {
Sam Hocevar's avatar
   
Sam Hocevar committed
378
379
380
381
382
383
384
385
386
387
    case 0:
        intf_DbgMsg( "vlcs: no answer" );
        break;

    case -1:
        intf_DbgMsg( "vlcs: unable to receive the answer ");
        break;

    default:
        recvfrom( i_socket, &i_answer, sizeof(char), 0,\
Marc Ariberti's avatar
Marc Ariberti committed
388
                  (struct sockaddr *)(&sa_client), &i_fromlen);
Sam Hocevar's avatar
   
Sam Hocevar committed
389
390
391
392

        intf_DbgMsg( "vlcs: the answer : %i", i_answer );

        switch( i_answer )
Marc Ariberti's avatar
Marc Ariberti committed
393
        {
Sam Hocevar's avatar
   
Sam Hocevar committed
394
395
396
397
398
399
400
401
402
            case -1:
                intf_DbgMsg( "vlcs: the server failed to create the thread" );
                break;
            case 0:
                intf_DbgMsg( "vlcs: the server tries to change the channel" );
                break;
            default:
                intf_DbgMsg( "vlcs: unknown answer !" );
                break;
Marc Ariberti's avatar
Marc Ariberti committed
403
        }
Sam Hocevar's avatar
   
Sam Hocevar committed
404
        break;
Henri Fallon's avatar
   
Henri Fallon committed
405
    }
Sam Hocevar's avatar
   
Sam Hocevar committed
406

Henri Fallon's avatar
   
Henri Fallon committed
407
408
409
    /*
     * Close the socket
     */
Sam Hocevar's avatar
   
Sam Hocevar committed
410
    close( i_socket );
Henri Fallon's avatar
   
Henri Fallon committed
411

Marc Ariberti's avatar
Marc Ariberti committed
412
413
    return( 0 );

Sam Hocevar's avatar
   
Sam Hocevar committed
414
#else
Sam Hocevar's avatar
   
Sam Hocevar committed
415
    intf_ErrMsg( "network error: channels not supported" );
Sam Hocevar's avatar
   
Sam Hocevar committed
416
417
418
    return( -1 );

#endif
Henri Fallon's avatar
   
Henri Fallon committed
419
}
Sam Hocevar's avatar
   
Sam Hocevar committed
420

Sam Hocevar's avatar
   
Sam Hocevar committed
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* Following functions are local */

/*****************************************************************************
 * GetMacAddress: extract the MAC Address
 *****************************************************************************/
static int GetMacAddress( int i_socket, char *psz_mac )
{
#if defined( SYS_LINUX )
    struct ifreq interface;
    int i_ret;

    /*
     * Looking for information about the eth0 interface
     */
    interface.ifr_addr.sa_family = AF_INET;
    strcpy( interface.ifr_name, INPUT_IFACE_DEFAULT );

    i_ret = ioctl( i_socket, SIOCGIFHWADDR, &interface );

    if( i_ret )
    {
        intf_ErrMsg( "network error: ioctl SIOCGIFHWADDR failed" );
        return( i_ret );
    }

    sprintf( psz_mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
                      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 );

    return( 0 );

#elif defined( WIN32 )
    int i, i_ret = -1;

    /* Get adapter list - support for more than one adapter */
    LANA_ENUM AdapterList;
    NCB       Ncb;

    intf_WarnMsg( 2, "network: looking for MAC address" );

    memset( &Ncb, 0, sizeof( NCB ) );
    Ncb.ncb_command = NCBENUM;
    Ncb.ncb_buffer = (unsigned char *)&AdapterList;
    Ncb.ncb_length = sizeof( AdapterList );
    Netbios( &Ncb );

    /* Get all of the local ethernet addresses */
    for ( i = 0; i < AdapterList.length ; ++i )
    {
        if ( GetAdapterInfo ( AdapterList.lana[ i ], psz_mac ) == 0 )
        {
	    i_ret = 0;
        }
    }

    return( i_ret );

Sam Hocevar's avatar
   
Sam Hocevar committed
482
#else
Sam Hocevar's avatar
   
Sam Hocevar committed
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
    return( -1);

#endif
}

#ifdef WIN32
/*****************************************************************************
 * GetAdapterInfo : gets some informations about the interface using NETBIOS
 *****************************************************************************/
static int GetAdapterInfo( int i_adapter, char *psz_string )
{
    struct ASTAT
    {
        ADAPTER_STATUS adapt;
        NAME_BUFFER    psz_name[30];
    } Adapter;

    /* Reset the LAN adapter so that we can begin querying it */
    NCB Ncb;
    memset( &Ncb, 0, sizeof ( Ncb ) );
    Ncb.ncb_command  = NCBRESET;
    Ncb.ncb_lana_num = i_adapter;

    if( Netbios( &Ncb ) != NRC_GOODRET )
    {
        intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode );
        return -1;
    }

    /* Prepare to get the adapter status block */
    memset( &Ncb, 0, sizeof( Ncb ) ) ;     /* Initialization */
    Ncb.ncb_command = NCBASTAT;
    Ncb.ncb_lana_num = i_adapter;

    strcpy( (char *)Ncb.ncb_callname, "*" );

    memset( &Adapter, 0, sizeof ( Adapter ) );
    Ncb.ncb_buffer = ( unsigned char * ) &Adapter;
    Ncb.ncb_length = sizeof ( Adapter );

    /* Get the adapter's info and, if this works, return it in standard,
     * colon-delimited form. */
    if ( Netbios( &Ncb ) == 0 )
    {
        sprintf ( psz_string, "%02X:%02X:%02X:%02X:%02X:%02X",
                (int) ( Adapter.adapt.adapter_address[0] ),
                (int) ( Adapter.adapt.adapter_address[1] ),
                (int) ( Adapter.adapt.adapter_address[2] ),
                (int) ( Adapter.adapt.adapter_address[3] ),
                (int) ( Adapter.adapt.adapter_address[4] ),
                (int) ( Adapter.adapt.adapter_address[5] ) );

	intf_WarnMsg( 2, "network: found MAC address %s", psz_string );

        return 0;
    }
    else
    {
        intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode );
        return -1;
    }
}
#endif /* WIN32 */