]> git.sesse.net Git - vlc/blobdiff - src/misc/netutils.c
Some heavy changes today:
[vlc] / src / misc / netutils.c
index 70decac2c65d4de38b573c78b1638b3344753a35..d8ef718b952a9f3d2fe95a83112ca492f223580e 100644 (file)
@@ -1,18 +1,21 @@
 /*****************************************************************************
  * netutils.c: various network functions
  *****************************************************************************
- * Copyright (C) 1999, 2000, 2001 VideoLAN
- * $Id: netutils.c,v 1.27 2001/04/27 18:07:57 henri Exp $
+ * Copyright (C) 1999-2001 VideoLAN
+ * $Id: netutils.c,v 1.54 2001/12/30 07:09:56 sam Exp $
  *
  * Authors: Vincent Seguin <seguin@via.ecp.fr>
  *          Benoit Steiner <benny@via.ecp.fr>
  *          Henri Fallon <henri@videolan.org>
+ *          Xavier Marchesini <xav@via.ecp.fr>
+ *          Christophe Massiot <massiot@via.ecp.fr>
+ *          Samuel Hocevar <sam@via.ecp.fr>
  *
  * 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.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include "defs.h"
-
-#include <netdb.h>                                        /* gethostbyname() */
 #include <stdlib.h>                             /* free(), realloc(), atoi() */
 #include <errno.h>                                                /* errno() */
-#include <string.h>                                      /* bzero(), bcopy() */
-#include <unistd.h>                                         /* gethostname() */
+#include <string.h>                                              /* memset() */
+
+#include <videolan/vlc.h>
+
+#ifdef STRNCASECMP_IN_STRINGS_H
+#   include <strings.h>
+#endif
+
+#ifdef HAVE_UNISTD_H
+#   include <unistd.h>                                      /* gethostname() */
+#elif defined( _MSC_VER ) && defined( _WIN32 )
+#   include <io.h>
+#endif
+
+#if !defined( _MSC_VER )
 #include <sys/time.h>                                        /* gettimeofday */
+#endif
 
-#include <netinet/in.h>                               /* BSD: struct in_addr */
-#include <sys/socket.h>                              /* BSD: struct sockaddr */
-#ifdef HAVE_ARPA_INET_H
-#include <arpa/inet.h>                           /* inet_ntoa(), inet_aton() */
+#ifdef WIN32
+#   include <winsock2.h>
+#elif !defined( SYS_BEOS ) && !defined( SYS_NTO )
+#   include <netdb.h>                                         /* hostent ... */
+#   include <sys/socket.h>                           /* BSD: struct sockaddr */
+#   include <netinet/in.h>                            /* BSD: struct in_addr */
+#   ifdef HAVE_ARPA_INET_H
+#       include <arpa/inet.h>                    /* inet_ntoa(), inet_aton() */
+#   endif
 #endif
 
 #ifdef SYS_LINUX
 #include <sys/ioctl.h>                                            /* ioctl() */
 #endif
 
+#if defined( WIN32 )                    /* tools to get the MAC adress from  */
+#include <windows.h>                    /* the interface under Windows       */
+#include <stdio.h>
+#endif
+
 #ifdef HAVE_NET_IF_H
 #include <net/if.h>                            /* interface (arch-dependent) */
 #endif
 #include <sys/sockio.h>
 #endif
 
-#include "config.h"
-#include "common.h"
-#include "mtime.h"
-#include "threads.h"
-#include "main.h"
-
-#include "intf_msg.h"
-
 #include "netutils.h"
 
-
+#include "intf_playlist.h"
 
 /*****************************************************************************
  * input_channel_t: channel library data
  *****************************************************************************/
 typedef struct input_channel_s
 {
-    int         i_channel_id;                      /* current channel number */
+    int         i_channel;                         /* current channel number */
     mtime_t     last_change;                             /* last change date */
 } input_channel_t;
 
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+static int GetMacAddress   ( int i_fd, char *psz_mac );
+#ifdef WIN32
+static int GetAdapterInfo  ( int i_adapter, char *psz_string );
+#endif
 
 /*****************************************************************************
- * network_BuildLocalAddr : fill a sockaddr_in structure for local binding
+ * network_BuildAddr : fill a sockaddr_in structure
  *****************************************************************************/
-int network_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port, 
-                            char * psz_broadcast )
+int network_BuildAddr( struct sockaddr_in * p_socket,
+                       char * psz_address, int i_port )
 {
-    char                psz_hostname[INPUT_MAX_SOURCE_LENGTH];
-    struct hostent    * p_hostent;
-    
+#if defined( SYS_BEOS )
+    intf_ErrMsg( "error: networking is not yet supported under BeOS" );
+    return( 1 );
+
+#else
     /* Reset struct */
     memset( p_socket, 0, sizeof( struct sockaddr_in ) );
-    p_socket->sin_family = AF_INET;                                 /* family */
+    p_socket->sin_family = AF_INET;                                /* family */
     p_socket->sin_port = htons( i_port );
-    if( psz_broadcast == NULL )
+    if( psz_address == NULL )
     {
-        /* 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 );
-        }
-
+        p_socket->sin_addr.s_addr = INADDR_ANY;
     }
     else
-    {   
-        /* I didn't manage to make INADDR_ANYT work, even with setsockopt 
-         * 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 );
-    }
-
-    /* Try to convert address directly from in_addr - this will work if
-     * psz_in_addr is dotted decimal. */
-#ifdef HAVE_ARPA_INET_H
-    if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
-#else
-    if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
-#endif
     {
-        /* We have a fqdn, try to find its address */
-        if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
-        {
-            intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
-            return( -1 );
-        }
-        
-        /* Copy the first address of the host in the socket address */
-        memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0], 
-                 p_hostent->h_length );
-    }
-    return( 0 );
-}
-
-/*****************************************************************************
- * network_BuildRemoteAddr : fill a sockaddr_in structure for remote host
- *****************************************************************************/
-int network_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
-{
-    struct hostent            * p_hostent;
-
-    /* Reset structure */
-    memset( p_socket, 0, sizeof( struct sockaddr_in ) );
-    p_socket->sin_family = AF_INET;                                 /* family */
-    p_socket->sin_port = htons( 0 );                /* This is for remote end */
-    
-     /* Try to convert address directly from in_addr - this will work if
-      * psz_in_addr is dotted decimal. */
+        struct hostent    * p_hostent;
 
+        /* Try to convert address directly from in_addr - this will work if
+         * psz_broadcast is dotted decimal. */
 #ifdef HAVE_ARPA_INET_H
-    if( !inet_aton( psz_server, &p_socket->sin_addr) )
+        if( !inet_aton( psz_address, &p_socket->sin_addr) )
 #else
-    if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
+        if( (p_socket->sin_addr.s_addr = inet_addr( psz_address )) == -1 )
 #endif
-    {
-        /* We have a fqdn, try to find its address */
-        if ( (p_hostent = gethostbyname(psz_server)) == NULL )
         {
-            intf_ErrMsg( "BuildRemoteAddr: unknown host %s", 
-                         psz_server );
-            return( -1 );
+            /* We have a fqdn, try to find its address */
+            if ( (p_hostent = gethostbyname( psz_address )) == NULL )
+            {
+                intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_address );
+                return( -1 );
+            }
+
+            /* Copy the first address of the host in the socket address */
+            memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
+                     p_hostent->h_length );
         }
-        
-        /* Copy the first address of the host in the socket address */
-        memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0], 
-                 p_hostent->h_length );
     }
     return( 0 );
+#endif
 }
 
 /*****************************************************************************
  * network_ChannelCreate: initialize global channel method data
  *****************************************************************************
  * Initialize channel input method global data. This function should be called
- * once before any input thread is created or any call to other 
+ * once before any input thread is created or any call to other
  * input_Channel*() function is attempted.
  *****************************************************************************/
 int network_ChannelCreate( void )
 {
-/* Even when BSD are supported, BeOS is not likely to be supported, so 
- * I prefer to put it apart */    
-#if defined( SYS_BEOS )
-    intf_ErrMsg( "error: channel changing is not yet supported under BeOS" );
-    return( 1 );
+#if defined( SYS_LINUX ) || defined( WIN32 )
 
-#elif defined( SYS_LINUX )
-/* FIXME : channels handling only work for linux */
     /* Allocate structure */
     p_main->p_channel = malloc( sizeof( input_channel_t ) );
     if( p_main->p_channel == NULL )
     {
-        intf_ErrMsg("error: %s\n", strerror(ENOMEM));
+        intf_ErrMsg( "network error: could not create channel bank" );
         return( -1 );
     }
 
     /* Initialize structure */
-    p_main->p_channel->i_channel_id   = 0;
+    p_main->p_channel->i_channel   = 0;
     p_main->p_channel->last_change = 0;
 
-    intf_Msg("Channels initialized\n");
+    intf_WarnMsg( 2, "network: channels initialized" );
     return( 0 );
 
 #else
-    intf_ErrMsg( "error : channel changing only works with linux yest" );
+    intf_ErrMsg( "network error : channels not supported on this platform" );
     return( 1 );
 
 #endif
@@ -212,199 +186,313 @@ int network_ChannelCreate( void )
 /*****************************************************************************
  * network_ChannelJoin: join a channel
  *****************************************************************************
- * 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 
+ * 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, its interface
  * should be unlocked using input_ChannelLeave().
  * Non 0 will be returned in case of error.
  *****************************************************************************/
-int network_ChannelJoin( int i_channel_id )
+int network_ChannelJoin( int i_channel )
 {
-/* I still prefer to put BeOS a bit apart */   
-#if defined( SYS_BEOS )
-    intf_ErrMsg( "Channels are not yet supported under BeOS" );
-    return( -1 );
-
-#elif defined( SYS_LINUX )
-    int                 i_socket_cl;
-    int                 i_fromlen;
-    struct ifreq        s_interface;
-    struct sockaddr_in  sa_server;
-    struct sockaddr_in  sa_client;
-    unsigned int        i_version = 12;
-    char                psz_mess[80];
-    char                i_mess_length = 80;
-    struct timeval *    p_date_cl;
-    struct timeval      s_time;
-    long unsigned int   i_date;
-    int                 i_nbanswer;
-    char                i_answer;
-    fd_set              s_rfds;
-    unsigned int       i_rc;
-    if( ! p_main->b_channels )
+#if defined( SYS_LINUX ) || defined( WIN32 )
+
+#define VLCS_VERSION 13
+#define MESSAGE_LENGTH 256
+
+    char psz_mess[ MESSAGE_LENGTH ];
+    char psz_mac[ 40 ];
+    int i_fd, i_dummy, i_port;
+    char *psz_vlcs;
+    struct sockaddr_in sa_server;
+    struct sockaddr_in sa_client;
+    struct timeval delay;
+    fd_set fds;
+
+    if( !main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
+                              INPUT_NETWORK_CHANNEL_DEFAULT  ) )
     {
-        intf_ErrMsg( "Channels disabled. To enable them, use the --channels"
-                     " option" );
-        return( -1 );
+        intf_ErrMsg( "network: channels disabled, to enable them, use the"
+                     "--channels option" );
+        return -1;
     }
-    /* debug */ 
-    intf_DbgMsg( "ChannelJoin : %d", i_channel_id );
+
     /* If last change is too recent, wait a while */
     if( mdate() - p_main->p_channel->last_change < INPUT_CHANNEL_CHANGE_DELAY )
     {
-        intf_Msg( "Waiting before changing channel...\n" );
+        intf_WarnMsg( 2, "network: waiting before changing channel" );
+        /* XXX Isn't this completely brain-damaged ??? -- Sam */
         mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY );
     }
-    p_main->p_channel->last_change = mdate();
-    p_main->p_channel->i_channel_id = i_channel_id;
 
-    intf_Msg( "Joining channel %d\n", i_channel_id );
+    /* Initializing the socket */
+    i_fd = socket( AF_INET, SOCK_DGRAM, 0 );
+    if( i_fd < 0 )
+    {
+        intf_ErrMsg( "network error: unable to create vlcs socket (%s)",
+                     strerror( errno ) );
+        return -1;
+    }
 
-    /*      
-     * Looking for information about the eth0 interface
-     */
-    s_interface.ifr_addr.sa_family = AF_INET;
-    strcpy( s_interface.ifr_name, INPUT_IFACE_DEFAULT );
-    
-    
-    /*
-     * Initialysing the socket
-     */
-    i_socket_cl=socket( AF_INET, SOCK_DGRAM, 0 );
+    i_dummy = 1;
+    if( setsockopt( i_fd, SOL_SOCKET, SO_REUSEADDR,
+                    (void *) &i_dummy, sizeof( i_dummy ) ) == -1 )
+    {
+        intf_ErrMsg( "network error: can't SO_REUSEADDR vlcs socket (%s)",
+                     strerror(errno));
+        close( i_fd );
+        return -1;
+    }
 
-    
-    /* 
-     * Getting the server's information 
-     */
-    bzero( &sa_server, sizeof(struct sockaddr_in) );
-    sa_server.sin_family = AF_INET;
-    sa_server.sin_port   = htons( INPUT_CHANNEL_PORT_DEFAULT );
-    inet_aton( INPUT_CHANNEL_SERVER_DEFAULT, &(sa_server.sin_addr) );
+    /* Getting information about the channel server */
+    psz_vlcs = main_GetPszVariable( INPUT_CHANNEL_SERVER_VAR,
+                                    INPUT_CHANNEL_SERVER_DEFAULT );
+    i_port = main_GetIntVariable( INPUT_CHANNEL_PORT_VAR,
+                                  INPUT_CHANNEL_PORT_DEFAULT );
+
+    intf_WarnMsg( 5, "network: socket %i, vlcs '%s', port %d",
+                     i_fd, psz_vlcs, i_port );
+
+    memset( &sa_client, 0x00, sizeof(struct sockaddr_in) );
+    memset( &sa_server, 0x00, sizeof(struct sockaddr_in) );
+    sa_client.sin_family      = AF_INET;
+    sa_server.sin_family      = AF_INET;
+    sa_client.sin_port        = htons( 4312 );
+    sa_server.sin_port        = htons( i_port );
+    sa_client.sin_addr.s_addr = INADDR_ANY;
+#ifdef HAVE_ARPA_INET_H
+    inet_aton( psz_vlcs, &sa_server.sin_addr );
+#else
+    sa_server.sin_addr.s_addr = inet_addr( psz_vlcs );
+#endif
 
-    /*
-     * Looking for the interface MAC address
-     */
-    ioctl( i_socket_cl, SIOCGIFHWADDR, &s_interface );
-    intf_DbgMsg(
-        "CHANNELSERVER: macaddr == %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
-        s_interface.ifr_hwaddr.sa_data[0] & 0xff,
-        s_interface.ifr_hwaddr.sa_data[1] & 0xff,
-        s_interface.ifr_hwaddr.sa_data[2] & 0xff,
-        s_interface.ifr_hwaddr.sa_data[3] & 0xff,
-        s_interface.ifr_hwaddr.sa_data[4] & 0xff,
-        s_interface.ifr_hwaddr.sa_data[5] & 0xff );
-    
-    /*
-     * Getting date of the client
-     */
-    p_date_cl=malloc( sizeof(struct timeval) );
-    if( p_date_cl == NULL )
+    /* Bind the socket */
+    if( bind( i_fd, (struct sockaddr*)(&sa_client), sizeof(sa_client) ) )
     {
-        intf_ErrMsg( "CHANNELSERVER: unable to allocate memory\n" );
-    /*    return VS_R_MEMORY;*/
-        return( -1);
+        intf_ErrMsg( "network: unable to bind vlcs socket (%s)",
+                     strerror( errno ) );
+        close( i_fd );
+        return -1;
     }
-    
-    if ( gettimeofday( p_date_cl, 0 ) == -1 )
+
+    /* Look for the interface MAC address */
+    if( GetMacAddress( i_fd, psz_mac ) )
     {
-        return( -1);
+        intf_ErrMsg( "network error: failed getting MAC address" );
+        close( i_fd );
+        return -1;
     }
-    i_date = p_date_cl->tv_sec;
-    free( p_date_cl );
-    intf_DbgMsg( "CHANNELSERVER: date %lu\n", i_date );
 
+    intf_WarnMsg( 6, "network: MAC address is %s", psz_mac );
 
-    /* 
-     * Build of the message
-     */
-    sprintf( psz_mess, "%d %u %lu %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x \n",
-          i_channel_id, i_version, i_date,
-        s_interface.ifr_hwaddr.sa_data[0] & 0xff, 
-        s_interface.ifr_hwaddr.sa_data[1] & 0xff,
-        s_interface.ifr_hwaddr.sa_data[2] & 0xff,
-        s_interface.ifr_hwaddr.sa_data[3] & 0xff,
-        s_interface.ifr_hwaddr.sa_data[4] & 0xff,
-        s_interface.ifr_hwaddr.sa_data[5] & 0xff );
-    intf_DbgMsg( "CHANNELSERVER: The message is %s\n", psz_mess );
+    /* Build the message */
+    sprintf( psz_mess, "%d %u %lu %s \n", i_channel, VLCS_VERSION,
+                       (unsigned long)(mdate() / (u64)1000000),
+                       psz_mac );
 
-    /*
-     * Open the socket 2
-     */
-    bzero( &sa_client, sizeof(struct sockaddr_in) );
-    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 );
-    i_rc = bind( i_socket_cl, (struct sockaddr *)(&sa_client),\
-                 sizeof(struct sockaddr) );
-    if ( i_rc )
+    /* Send the message */
+    sendto( i_fd, psz_mess, MESSAGE_LENGTH, 0,
+            (struct sockaddr *)(&sa_server), sizeof(struct sockaddr) );
+
+    intf_WarnMsg( 2, "network: attempting to join channel %d", i_channel );
+
+    /* We have changed channels ! (or at least, we tried) */
+    p_main->p_channel->last_change = mdate();
+    p_main->p_channel->i_channel = i_channel;
+
+    /* Wait 5 sec for an answer from the server */
+    delay.tv_sec = 5;
+    delay.tv_usec = 0;
+    FD_ZERO( &fds );
+    FD_SET( i_fd, &fds );
+
+    switch( select( i_fd + 1, &fds, NULL, NULL, &delay ) )
     {
-        intf_ErrMsg( "CHANNELSERVER: Unable to bind socket:%u\n", i_rc ); 
-    /* TODO put CS_R_BIND in types.h*/
-    /*    return CS_R_SOCKET;*/
-        return -1;
+        case 0:
+            intf_ErrMsg( "network error: no answer from vlcs" );
+            close( i_fd );
+            return -1;
+            break;
+
+        case -1:
+            intf_ErrMsg( "network error: error while listening to vlcs" );
+            close( i_fd );
+            return -1;
+            break;
     }
 
+    i_dummy = sizeof( struct sockaddr );
+    recvfrom( i_fd, psz_mess, MESSAGE_LENGTH, 0,
+              (struct sockaddr *)(&sa_client), &i_dummy);
+    psz_mess[ MESSAGE_LENGTH - 1 ] = 0;
 
-    /*
-     * Send the message
-     */
-    sendto( i_socket_cl, psz_mess, i_mess_length, 0, \
-            (struct sockaddr *)(&sa_server),   \
-            sizeof(struct sockaddr) );
-   
-     /*
-     * Waiting 5 sec for one answer from the server
-     */
-    s_time.tv_sec  = 5;
-    s_time.tv_usec = 0;
-    FD_ZERO( &s_rfds );
-    FD_SET( i_socket_cl, &s_rfds );
-    i_nbanswer = select( i_socket_cl+1, &s_rfds, NULL, NULL, &s_time );
-    if( i_nbanswer == 0 )
+    if( !strncasecmp( psz_mess, "E: ", 3 ) )
     {
-        intf_DbgMsg( "CHANNELSERVER: no answer\n" );
+        intf_ErrMsg( "network error: vlcs said '%s'", psz_mess + 3 );
+        close( i_fd );
+        return -1;
     }
-    else if( i_nbanswer == -1 )
+    else if( !strncasecmp( psz_mess, "I: ", 3 ) )
     {
-        intf_DbgMsg( "CHANNELSERVER: Unable to receive the answer\n ");
+        intf_WarnMsg( 2, "network info: vlcs said '%s'", psz_mess + 3 );
     }
-    else
+    else /* We got something to play ! FIXME: not very nice */
     {
-        recvfrom( i_socket_cl, &i_answer, sizeof(char), 0,\
-                  (struct sockaddr *)(&sa_client), &i_fromlen);
-        intf_DbgMsg( "CHANNELSERVER: the answer : %hhd\n", i_answer );
-        if( i_answer == -1 )
-        {
-            intf_DbgMsg(
-                  "CHANNELSERVER: The server failed to create the thread\n" );
-        }
-        else if( i_answer == 0 )
+#   define p_item \
+        (&p_main->p_playlist->p_item[ p_main->p_playlist->i_index + 1])
+        vlc_mutex_lock( &p_main->p_playlist->change_lock );
+        if( p_item )
         {
-            intf_DbgMsg(
-                  "CHANNELSERVER: The server tries to change the channel\n" );
+            free( p_item->psz_name );
+            p_item->psz_name = strdup( psz_mess );
+            /* Unlock _afterwards_ */
+            vlc_mutex_unlock( &p_main->p_playlist->change_lock );
         }
         else
         {
-            intf_DbgMsg( "CHANNELSERVER: Unknown answer !\n" );
+            /* Unlock _before_ */
+            vlc_mutex_unlock( &p_main->p_playlist->change_lock );
+            intf_PlaylistAdd( p_main->p_playlist, 0, psz_mess );
         }
     }
-    
+
+    /* Close the socket and return nicely */
+    close( i_fd );
+
+    return 0;
+
+#else
+    intf_ErrMsg( "network error: channels not supported on this platform" );
+    return -1; 
+
+#endif
+}
+
+/* Following functions are local */
+
+/*****************************************************************************
+ * GetMacAddress: extract the MAC Address
+ *****************************************************************************/
+static int GetMacAddress( int i_fd, char *psz_mac )
+{
+#if defined( SYS_LINUX )
+    struct ifreq interface;
+    int i_ret;
+
     /*
-     * Close the socket
+     * Looking for information about the eth0 interface
      */
-    close( i_socket_cl );
+    interface.ifr_addr.sa_family = AF_INET;
+    strcpy( interface.ifr_name, 
+            main_GetPszVariable( INPUT_IFACE_VAR, INPUT_IFACE_DEFAULT ) );
+
+    i_ret = ioctl( i_fd, 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 );
+
 #else
-    intf_ErrMsg( "Channels only work under linux yet" );
-    return( -1 );
+    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 */
+