~ubuntu-branches/debian/wheezy/vlc/wheezy

« back to all changes in this revision

Viewing changes to src/misc/netutils.c

Tags: upstream-0.7.2.final
ImportĀ upstreamĀ versionĀ 0.7.2.final

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 * netutils.c: various network functions
3
 
 *****************************************************************************
4
 
 * Copyright (C) 1999-2001 VideoLAN
5
 
 * $Id: netutils.c,v 1.53 2001/12/07 18:33:08 sam Exp $
6
 
 *
7
 
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8
 
 *          Benoit Steiner <benny@via.ecp.fr>
9
 
 *          Henri Fallon <henri@videolan.org>
10
 
 *          Xavier Marchesini <xav@via.ecp.fr>
11
 
 *          Christophe Massiot <massiot@via.ecp.fr>
12
 
 *          Samuel Hocevar <sam@via.ecp.fr>
13
 
 *
14
 
 * This program is free software; you can redistribute it and/or modify
15
 
 * it under the terms of the GNU General Public License as published by
16
 
 * the Free Software Foundation; either version 2 of the License, or
17
 
 * (at your option) any later version.
18
 
 *
19
 
 * This program is distributed in the hope that it will be useful,
20
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 
 * GNU General Public License for more details.
23
 
 *
24
 
 * You should have received a copy of the GNU General Public License
25
 
 * along with this program; if not, write to the Free Software
26
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
27
 
 *****************************************************************************/
28
 
 
29
 
/*****************************************************************************
30
 
 * Preamble
31
 
 *****************************************************************************/
32
 
#include "defs.h"
33
 
 
34
 
#include <stdlib.h>                             /* free(), realloc(), atoi() */
35
 
#include <errno.h>                                                /* errno() */
36
 
#include <string.h>                                              /* memset() */
37
 
 
38
 
#ifdef STRNCASECMP_IN_STRINGS_H
39
 
#   include <strings.h>
40
 
#endif
41
 
 
42
 
#ifdef HAVE_UNISTD_H
43
 
#   include <unistd.h>                                      /* gethostname() */
44
 
#elif defined( _MSC_VER ) && defined( _WIN32 )
45
 
#   include <io.h>
46
 
#endif
47
 
 
48
 
#if !defined( _MSC_VER )
49
 
#include <sys/time.h>                                        /* gettimeofday */
50
 
#endif
51
 
 
52
 
#ifdef WIN32
53
 
#   include <winsock2.h>
54
 
#elif !defined( SYS_BEOS ) && !defined( SYS_NTO )
55
 
#   include <netdb.h>                                         /* hostent ... */
56
 
#   include <sys/socket.h>                           /* BSD: struct sockaddr */
57
 
#   include <netinet/in.h>                            /* BSD: struct in_addr */
58
 
#   ifdef HAVE_ARPA_INET_H
59
 
#       include <arpa/inet.h>                    /* inet_ntoa(), inet_aton() */
60
 
#   endif
61
 
#endif
62
 
 
63
 
#ifdef SYS_LINUX
64
 
#include <sys/ioctl.h>                                            /* ioctl() */
65
 
#endif
66
 
 
67
 
#if defined( WIN32 )                    /* tools to get the MAC adress from  */
68
 
#include <windows.h>                    /* the interface under Windows       */
69
 
#include <stdio.h>
70
 
#endif
71
 
 
72
 
#ifdef HAVE_NET_IF_H
73
 
#include <net/if.h>                            /* interface (arch-dependent) */
74
 
#endif
75
 
 
76
 
#ifdef HAVE_SYS_SOCKIO_H
77
 
#include <sys/sockio.h>
78
 
#endif
79
 
 
80
 
#include "common.h"
81
 
#include "mtime.h"
82
 
#include "intf_msg.h"
83
 
#include "threads.h"
84
 
 
85
 
#include "intf_playlist.h"
86
 
 
87
 
#include "netutils.h"
88
 
 
89
 
/*****************************************************************************
90
 
 * input_channel_t: channel library data
91
 
 *****************************************************************************
92
 
 * Store global channel library data.
93
 
 * The part of the code concerning the channel changing process is unstable
94
 
 * as it depends on the VideoLAN channel server, which isn't frozen for
95
 
 * the time being.
96
 
 *****************************************************************************/
97
 
typedef struct input_channel_s
98
 
{
99
 
    int         i_channel;                         /* current channel number */
100
 
    mtime_t     last_change;                             /* last change date */
101
 
} input_channel_t;
102
 
 
103
 
/*****************************************************************************
104
 
 * Local prototypes
105
 
 *****************************************************************************/
106
 
static int GetMacAddress   ( int i_fd, char *psz_mac );
107
 
#ifdef WIN32
108
 
static int GetAdapterInfo  ( int i_adapter, char *psz_string );
109
 
#endif
110
 
 
111
 
/*****************************************************************************
112
 
 * network_BuildAddr : fill a sockaddr_in structure
113
 
 *****************************************************************************/
114
 
int network_BuildAddr( struct sockaddr_in * p_socket,
115
 
                       char * psz_address, int i_port )
116
 
{
117
 
#if defined( SYS_BEOS )
118
 
    intf_ErrMsg( "error: networking is not yet supported under BeOS" );
119
 
    return( 1 );
120
 
 
121
 
#else
122
 
    /* Reset struct */
123
 
    memset( p_socket, 0, sizeof( struct sockaddr_in ) );
124
 
    p_socket->sin_family = AF_INET;                                /* family */
125
 
    p_socket->sin_port = htons( i_port );
126
 
    if( psz_address == NULL )
127
 
    {
128
 
        p_socket->sin_addr.s_addr = INADDR_ANY;
129
 
    }
130
 
    else
131
 
    {
132
 
        struct hostent    * p_hostent;
133
 
 
134
 
        /* Try to convert address directly from in_addr - this will work if
135
 
         * psz_broadcast is dotted decimal. */
136
 
#ifdef HAVE_ARPA_INET_H
137
 
        if( !inet_aton( psz_address, &p_socket->sin_addr) )
138
 
#else
139
 
        if( (p_socket->sin_addr.s_addr = inet_addr( psz_address )) == -1 )
140
 
#endif
141
 
        {
142
 
            /* We have a fqdn, try to find its address */
143
 
            if ( (p_hostent = gethostbyname( psz_address )) == NULL )
144
 
            {
145
 
                intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_address );
146
 
                return( -1 );
147
 
            }
148
 
 
149
 
            /* Copy the first address of the host in the socket address */
150
 
            memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
151
 
                     p_hostent->h_length );
152
 
        }
153
 
    }
154
 
    return( 0 );
155
 
#endif
156
 
}
157
 
 
158
 
/*****************************************************************************
159
 
 * network_ChannelCreate: initialize global channel method data
160
 
 *****************************************************************************
161
 
 * Initialize channel input method global data. This function should be called
162
 
 * once before any input thread is created or any call to other
163
 
 * input_Channel*() function is attempted.
164
 
 *****************************************************************************/
165
 
int network_ChannelCreate( void )
166
 
{
167
 
#if defined( SYS_LINUX ) || defined( WIN32 )
168
 
 
169
 
    /* Allocate structure */
170
 
    p_main->p_channel = malloc( sizeof( input_channel_t ) );
171
 
    if( p_main->p_channel == NULL )
172
 
    {
173
 
        intf_ErrMsg( "network error: could not create channel bank" );
174
 
        return( -1 );
175
 
    }
176
 
 
177
 
    /* Initialize structure */
178
 
    p_main->p_channel->i_channel   = 0;
179
 
    p_main->p_channel->last_change = 0;
180
 
 
181
 
    intf_WarnMsg( 2, "network: channels initialized" );
182
 
    return( 0 );
183
 
 
184
 
#else
185
 
    intf_ErrMsg( "network error : channels not supported on this platform" );
186
 
    return( 1 );
187
 
 
188
 
#endif
189
 
}
190
 
 
191
 
/*****************************************************************************
192
 
 * network_ChannelJoin: join a channel
193
 
 *****************************************************************************
194
 
 * This function will try to join a channel. If the relevant interface is
195
 
 * already on the good channel, nothing will be done. Else, and if possible
196
 
 * (if the interface is not locked), the channel server will be contacted
197
 
 * and a change will be requested. The function will block until the change
198
 
 * is effective. Note that once a channel is no more used, its interface
199
 
 * should be unlocked using input_ChannelLeave().
200
 
 * Non 0 will be returned in case of error.
201
 
 *****************************************************************************/
202
 
int network_ChannelJoin( int i_channel )
203
 
{
204
 
#if defined( SYS_LINUX ) || defined( WIN32 )
205
 
 
206
 
#define VLCS_VERSION 13
207
 
#define MESSAGE_LENGTH 256
208
 
 
209
 
    char psz_mess[ MESSAGE_LENGTH ];
210
 
    char psz_mac[ 40 ];
211
 
    int i_fd, i_dummy, i_port;
212
 
    char *psz_vlcs;
213
 
    struct sockaddr_in sa_server;
214
 
    struct sockaddr_in sa_client;
215
 
    struct timeval delay;
216
 
    fd_set fds;
217
 
 
218
 
    if( !main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
219
 
                              INPUT_NETWORK_CHANNEL_DEFAULT  ) )
220
 
    {
221
 
        intf_ErrMsg( "network: channels disabled, to enable them, use the"
222
 
                     "--channels option" );
223
 
        return -1;
224
 
    }
225
 
 
226
 
    /* If last change is too recent, wait a while */
227
 
    if( mdate() - p_main->p_channel->last_change < INPUT_CHANNEL_CHANGE_DELAY )
228
 
    {
229
 
        intf_WarnMsg( 2, "network: waiting before changing channel" );
230
 
        /* XXX Isn't this completely brain-damaged ??? -- Sam */
231
 
        mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY );
232
 
    }
233
 
 
234
 
    /* Initializing the socket */
235
 
    i_fd = socket( AF_INET, SOCK_DGRAM, 0 );
236
 
    if( i_fd < 0 )
237
 
    {
238
 
        intf_ErrMsg( "network error: unable to create vlcs socket (%s)",
239
 
                     strerror( errno ) );
240
 
        return -1;
241
 
    }
242
 
 
243
 
    i_dummy = 1;
244
 
    if( setsockopt( i_fd, SOL_SOCKET, SO_REUSEADDR,
245
 
                    (void *) &i_dummy, sizeof( i_dummy ) ) == -1 )
246
 
    {
247
 
        intf_ErrMsg( "network error: can't SO_REUSEADDR vlcs socket (%s)",
248
 
                     strerror(errno));
249
 
        close( i_fd );
250
 
        return -1;
251
 
    }
252
 
 
253
 
    /* Getting information about the channel server */
254
 
    psz_vlcs = main_GetPszVariable( INPUT_CHANNEL_SERVER_VAR,
255
 
                                    INPUT_CHANNEL_SERVER_DEFAULT );
256
 
    i_port = main_GetIntVariable( INPUT_CHANNEL_PORT_VAR,
257
 
                                  INPUT_CHANNEL_PORT_DEFAULT );
258
 
 
259
 
    intf_WarnMsg( 5, "network: socket %i, vlcs '%s', port %d",
260
 
                     i_fd, psz_vlcs, i_port );
261
 
 
262
 
    memset( &sa_client, 0x00, sizeof(struct sockaddr_in) );
263
 
    memset( &sa_server, 0x00, sizeof(struct sockaddr_in) );
264
 
    sa_client.sin_family      = AF_INET;
265
 
    sa_server.sin_family      = AF_INET;
266
 
    sa_client.sin_port        = htons( 4312 );
267
 
    sa_server.sin_port        = htons( i_port );
268
 
    sa_client.sin_addr.s_addr = INADDR_ANY;
269
 
#ifdef HAVE_ARPA_INET_H
270
 
    inet_aton( psz_vlcs, &sa_server.sin_addr );
271
 
#else
272
 
    sa_server.sin_addr.s_addr = inet_addr( psz_vlcs );
273
 
#endif
274
 
 
275
 
    /* Bind the socket */
276
 
    if( bind( i_fd, (struct sockaddr*)(&sa_client), sizeof(sa_client) ) )
277
 
    {
278
 
        intf_ErrMsg( "network: unable to bind vlcs socket (%s)",
279
 
                     strerror( errno ) );
280
 
        close( i_fd );
281
 
        return -1;
282
 
    }
283
 
 
284
 
    /* Look for the interface MAC address */
285
 
    if( GetMacAddress( i_fd, psz_mac ) )
286
 
    {
287
 
        intf_ErrMsg( "network error: failed getting MAC address" );
288
 
        close( i_fd );
289
 
        return -1;
290
 
    }
291
 
 
292
 
    intf_WarnMsg( 6, "network: MAC address is %s", psz_mac );
293
 
 
294
 
    /* Build the message */
295
 
    sprintf( psz_mess, "%d %u %lu %s \n", i_channel, VLCS_VERSION,
296
 
                       (unsigned long)(mdate() / (u64)1000000),
297
 
                       psz_mac );
298
 
 
299
 
    /* Send the message */
300
 
    sendto( i_fd, psz_mess, MESSAGE_LENGTH, 0,
301
 
            (struct sockaddr *)(&sa_server), sizeof(struct sockaddr) );
302
 
 
303
 
    intf_WarnMsg( 2, "network: attempting to join channel %d", i_channel );
304
 
 
305
 
    /* We have changed channels ! (or at least, we tried) */
306
 
    p_main->p_channel->last_change = mdate();
307
 
    p_main->p_channel->i_channel = i_channel;
308
 
 
309
 
    /* Wait 5 sec for an answer from the server */
310
 
    delay.tv_sec = 5;
311
 
    delay.tv_usec = 0;
312
 
    FD_ZERO( &fds );
313
 
    FD_SET( i_fd, &fds );
314
 
 
315
 
    switch( select( i_fd + 1, &fds, NULL, NULL, &delay ) )
316
 
    {
317
 
        case 0:
318
 
            intf_ErrMsg( "network error: no answer from vlcs" );
319
 
            close( i_fd );
320
 
            return -1;
321
 
            break;
322
 
 
323
 
        case -1:
324
 
            intf_ErrMsg( "network error: error while listening to vlcs" );
325
 
            close( i_fd );
326
 
            return -1;
327
 
            break;
328
 
    }
329
 
 
330
 
    i_dummy = sizeof( struct sockaddr );
331
 
    recvfrom( i_fd, psz_mess, MESSAGE_LENGTH, 0,
332
 
              (struct sockaddr *)(&sa_client), &i_dummy);
333
 
    psz_mess[ MESSAGE_LENGTH - 1 ] = 0;
334
 
 
335
 
    if( !strncasecmp( psz_mess, "E: ", 3 ) )
336
 
    {
337
 
        intf_ErrMsg( "network error: vlcs said '%s'", psz_mess + 3 );
338
 
        close( i_fd );
339
 
        return -1;
340
 
    }
341
 
    else if( !strncasecmp( psz_mess, "I: ", 3 ) )
342
 
    {
343
 
        intf_WarnMsg( 2, "network info: vlcs said '%s'", psz_mess + 3 );
344
 
    }
345
 
    else /* We got something to play ! FIXME: not very nice */
346
 
    {
347
 
#   define p_item \
348
 
        (&p_main->p_playlist->p_item[ p_main->p_playlist->i_index + 1])
349
 
        vlc_mutex_lock( &p_main->p_playlist->change_lock );
350
 
        if( p_item )
351
 
        {
352
 
            free( p_item->psz_name );
353
 
            p_item->psz_name = strdup( psz_mess );
354
 
            /* Unlock _afterwards_ */
355
 
            vlc_mutex_unlock( &p_main->p_playlist->change_lock );
356
 
        }
357
 
        else
358
 
        {
359
 
            /* Unlock _before_ */
360
 
            vlc_mutex_unlock( &p_main->p_playlist->change_lock );
361
 
            intf_PlaylistAdd( p_main->p_playlist, 0, psz_mess );
362
 
        }
363
 
    }
364
 
 
365
 
    /* Close the socket and return nicely */
366
 
    close( i_fd );
367
 
 
368
 
    return 0;
369
 
 
370
 
#else
371
 
    intf_ErrMsg( "network error: channels not supported on this platform" );
372
 
    return -1; 
373
 
 
374
 
#endif
375
 
}
376
 
 
377
 
/* Following functions are local */
378
 
 
379
 
/*****************************************************************************
380
 
 * GetMacAddress: extract the MAC Address
381
 
 *****************************************************************************/
382
 
static int GetMacAddress( int i_fd, char *psz_mac )
383
 
{
384
 
#if defined( SYS_LINUX )
385
 
    struct ifreq interface;
386
 
    int i_ret;
387
 
 
388
 
    /*
389
 
     * Looking for information about the eth0 interface
390
 
     */
391
 
    interface.ifr_addr.sa_family = AF_INET;
392
 
    strcpy( interface.ifr_name, 
393
 
            main_GetPszVariable( INPUT_IFACE_VAR, INPUT_IFACE_DEFAULT ) );
394
 
 
395
 
    i_ret = ioctl( i_fd, SIOCGIFHWADDR, &interface );
396
 
 
397
 
    if( i_ret )
398
 
    {
399
 
        intf_ErrMsg( "network error: ioctl SIOCGIFHWADDR failed" );
400
 
        return( i_ret );
401
 
    }
402
 
 
403
 
    sprintf( psz_mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
404
 
                      interface.ifr_hwaddr.sa_data[0] & 0xff,
405
 
                      interface.ifr_hwaddr.sa_data[1] & 0xff,
406
 
                      interface.ifr_hwaddr.sa_data[2] & 0xff,
407
 
                      interface.ifr_hwaddr.sa_data[3] & 0xff,
408
 
                      interface.ifr_hwaddr.sa_data[4] & 0xff,
409
 
                      interface.ifr_hwaddr.sa_data[5] & 0xff );
410
 
 
411
 
    return( 0 );
412
 
 
413
 
#elif defined( WIN32 )
414
 
    int i, i_ret = -1;
415
 
 
416
 
    /* Get adapter list - support for more than one adapter */
417
 
    LANA_ENUM AdapterList;
418
 
    NCB       Ncb;
419
 
 
420
 
    intf_WarnMsg( 2, "network: looking for MAC address" );
421
 
 
422
 
    memset( &Ncb, 0, sizeof( NCB ) );
423
 
    Ncb.ncb_command = NCBENUM;
424
 
    Ncb.ncb_buffer = (unsigned char *)&AdapterList;
425
 
    Ncb.ncb_length = sizeof( AdapterList );
426
 
    Netbios( &Ncb );
427
 
 
428
 
    /* Get all of the local ethernet addresses */
429
 
    for ( i = 0; i < AdapterList.length ; ++i )
430
 
    {
431
 
        if ( GetAdapterInfo ( AdapterList.lana[ i ], psz_mac ) == 0 )
432
 
        {
433
 
            i_ret = 0;
434
 
        }
435
 
    }
436
 
 
437
 
    return( i_ret );
438
 
 
439
 
#else
440
 
    return( -1);
441
 
 
442
 
#endif
443
 
}
444
 
 
445
 
#ifdef WIN32
446
 
/*****************************************************************************
447
 
 * GetAdapterInfo : gets some informations about the interface using NETBIOS
448
 
 *****************************************************************************/
449
 
static int GetAdapterInfo( int i_adapter, char *psz_string )
450
 
{
451
 
    struct ASTAT
452
 
    {
453
 
        ADAPTER_STATUS adapt;
454
 
        NAME_BUFFER    psz_name[30];
455
 
    } Adapter;
456
 
 
457
 
    /* Reset the LAN adapter so that we can begin querying it */
458
 
    NCB Ncb;
459
 
    memset( &Ncb, 0, sizeof ( Ncb ) );
460
 
    Ncb.ncb_command  = NCBRESET;
461
 
    Ncb.ncb_lana_num = i_adapter;
462
 
 
463
 
    if( Netbios( &Ncb ) != NRC_GOODRET )
464
 
    {
465
 
        intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode );
466
 
        return -1;
467
 
    }
468
 
 
469
 
    /* Prepare to get the adapter status block */
470
 
    memset( &Ncb, 0, sizeof( Ncb ) ) ;     /* Initialization */
471
 
    Ncb.ncb_command = NCBASTAT;
472
 
    Ncb.ncb_lana_num = i_adapter;
473
 
 
474
 
    strcpy( (char *)Ncb.ncb_callname, "*" );
475
 
 
476
 
    memset( &Adapter, 0, sizeof ( Adapter ) );
477
 
    Ncb.ncb_buffer = ( unsigned char * ) &Adapter;
478
 
    Ncb.ncb_length = sizeof ( Adapter );
479
 
 
480
 
    /* Get the adapter's info and, if this works, return it in standard,
481
 
     * colon-delimited form. */
482
 
    if ( Netbios( &Ncb ) == 0 )
483
 
    {
484
 
        sprintf ( psz_string, "%02X:%02X:%02X:%02X:%02X:%02X",
485
 
                (int) ( Adapter.adapt.adapter_address[0] ),
486
 
                (int) ( Adapter.adapt.adapter_address[1] ),
487
 
                (int) ( Adapter.adapt.adapter_address[2] ),
488
 
                (int) ( Adapter.adapt.adapter_address[3] ),
489
 
                (int) ( Adapter.adapt.adapter_address[4] ),
490
 
                (int) ( Adapter.adapt.adapter_address[5] ) );
491
 
 
492
 
        intf_WarnMsg( 2, "network: found MAC address %s", psz_string );
493
 
 
494
 
        return 0;
495
 
    }
496
 
    else
497
 
    {
498
 
        intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode );
499
 
        return -1;
500
 
    }
501
 
}
502
 
#endif /* WIN32 */
503