]> git.sesse.net Git - vlc/blobdiff - src/misc/netutils.c
* DirectX plugin by Gildas Bazin <gbazin@netcourrier.com>.
[vlc] / src / misc / netutils.c
index 36d9969c6fb0967b25444879e962aa26f9840617..b1bdcc18fb0d6dbf671a65b0db1e63ef998f26b7 100644 (file)
@@ -1,16 +1,19 @@
 /*****************************************************************************
  * netutils.c: various network functions
  *****************************************************************************
- * Copyright (C) 1999, 2000 VideoLAN
+ * Copyright (C) 1999, 2000, 2001 VideoLAN
+ * $Id: netutils.c,v 1.37 2001/06/02 01:09:03 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>
  *
  * 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
  *****************************************************************************/
 #include "defs.h"
 
-#include <netdb.h>                                        /* gethostbyname() */
 #include <stdlib.h>                             /* free(), realloc(), atoi() */
 #include <errno.h>                                                /* errno() */
-#include <string.h>                                      /* bzero(), bcopy() */
+#include <string.h>                                              /* memset() */
 
+#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
+
+#if !defined( WIN32 )
+#include <netdb.h>                                        /* gethostbyname() */
 #include <netinet/in.h>                               /* BSD: struct in_addr */
 #include <sys/socket.h>                              /* BSD: struct sockaddr */
+#endif
+
 #ifdef HAVE_ARPA_INET_H
 #include <arpa/inet.h>                           /* inet_ntoa(), inet_aton() */
 #endif
 
-#if defined (HAVE_SYS_IOCTL_H)
+#ifdef SYS_LINUX
 #include <sys/ioctl.h>                                            /* ioctl() */
 #endif
 
-#include <unistd.h>                           /* needed for ioctl on Solaris */
-//#include <stropts.h>
+#if defined( WIN32 )                    /* tools to get the MAC adress from  */
+#include <windows.h>                    /* the interface under Windows      */
+#include <stdio.h>
+#endif
 
-#if defined (HAVE_NET_IF_H)
+#ifdef HAVE_NET_IF_H
 #include <net/if.h>                            /* interface (arch-dependent) */
 #endif
+
 #ifdef HAVE_SYS_SOCKIO_H
 #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 "debug.h"
 
 #include "netutils.h"
 
 /*****************************************************************************
- * BuildInetAddr: build an Internet address descriptor
+ * input_channel_t: channel library data
  *****************************************************************************
- * Build an internet socket descriptor from a host name, or an ip, and a port.
- * If the address is NULL, then INADDR_ANY will be used, allowing to receive
- * on any address for a local socket. Usually, in this case, 'port' is also null
- * and the function always succeeds.
+ * 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.
  *****************************************************************************/
-int BuildInetAddr( struct sockaddr_in *p_sa_in, char *psz_in_addr, int i_port )
+typedef struct input_channel_s
 {
-    struct hostent *p_hostent;                            /* host descriptor */
+    int         i_channel;                         /* current channel number */
+    mtime_t     last_change;                             /* last change date */
+} input_channel_t;
+
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+static int GetMacAddress   ( int i_socket, char *psz_mac );
+#ifdef WIN32
+static int GetAdapterInfo  ( int i_adapter, char *psz_string );
+#endif
 
-    memset( p_sa_in, 0, sizeof( struct sockaddr_in ) );
-    p_sa_in->sin_family = AF_INET;                                 /* family */
-    p_sa_in->sin_port = htons( i_port );                             /* port */
+/*****************************************************************************
+ * network_BuildLocalAddr : fill a sockaddr_in structure for local binding
+ *****************************************************************************/
+int network_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port,
+                            char * psz_broadcast )
+{
+    char                psz_hostname[INPUT_MAX_SOURCE_LENGTH];
+    struct hostent    * p_hostent;
+
+    /* Reset struct */
+    memset( p_socket, 0, sizeof( struct sockaddr_in ) );
+    p_socket->sin_family = AF_INET;                                /* family */
+    p_socket->sin_port = htons( i_port );
+    if( psz_broadcast == 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 );
+        }
 
-    /* Use INADDR_ANY if psz_in_addr is NULL */
-    if( psz_in_addr == NULL )
+    }
+    else
     {
-        p_sa_in->sin_addr.s_addr = htonl(INADDR_ANY);
+        /* 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. */
-#if defined HAVE_ARPA_INET_H && !defined SYS_SOLARIS
-    else if( !inet_aton( psz_in_addr, &p_sa_in->sin_addr) )
+#ifdef HAVE_ARPA_INET_H
+    if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
 #else
-    else if( (p_sa_in->sin_addr.s_addr = inet_addr( psz_in_addr )) == -1 )
+    if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
 #endif
     {
-        /* The convertion failed: the address is an host name, which needs
-         * to be resolved */
-        intf_DbgMsg("debug: resolving internet address %s...", psz_in_addr);
-        if ( (p_hostent = gethostbyname(psz_in_addr)) == NULL)
+        /* We have a fqdn, try to find its address */
+        if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
         {
-            intf_ErrMsg( "net error: unknown host %s", psz_in_addr );
+            intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
             return( -1 );
         }
 
         /* Copy the first address of the host in the socket address */
-        memmove( &p_sa_in->sin_addr, p_hostent->h_addr_list[0], p_hostent->h_length );
+        memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
+                 p_hostent->h_length );
     }
     return( 0 );
 }
 
-
 /*****************************************************************************
- * ServerPort: extract port from a "server:port" adress
- *****************************************************************************
- * Returns the port number in a "server:port" address and replace the ":" by
- * a NUL character, or returns -1.
+ * network_BuildRemoteAddr : fill a sockaddr_in structure for remote host
  *****************************************************************************/
-int ServerPort( char *psz_addr )
+int network_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
 {
-    char *psz_index;
+    struct hostent            * p_hostent;
 
-    /* Scan string for ':' */
-    for( psz_index = psz_addr; *psz_index && (*psz_index != ':'); psz_index++ )
-    {
-        ;
-    }
+    /* 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 */
 
-    /* If a port number has been found, convert it and return it */
-    if( *psz_index == ':' )
+     /* 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_server, &p_socket->sin_addr) )
+#else
+    if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
+#endif
     {
-        *psz_index = '\0';
-        return( atoi( psz_index + 1 ) );
-    }
+        /* 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 );
+        }
 
-    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 );
 }
 
-
 /*****************************************************************************
- * ReadIfConf: Read the configuration of an interface
+ * network_ChannelCreate: initialize global channel method data
  *****************************************************************************
- * i_sockfd must reference a socket open as follow: AF_INET, DOCK_DGRAM, 0
+ * Initialize channel input method global data. This function should be called
+ * once before any input thread is created or any call to other
+ * input_Channel*() function is attempted.
  *****************************************************************************/
-#if 0
-/* pbm :  SIOCGIFHWADDR, doesn't exist on BSD -> find a portable way to
-   do this --Meuuh */
-int ReadIfConf(int i_sockfd, if_descr_t* p_ifdescr, char* psz_name)
+int network_ChannelCreate( void )
 {
-    int i_rc = 0;
-#if defined (HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H)
-    struct ifreq ifr_config;
+/* 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 );
+
+#elif defined( SYS_LINUX ) || defined( WIN32 )
+/* 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( "network error: could not create channel bank" );
+        return( -1 );
+    }
+
+    /* Initialize structure */
+    p_main->p_channel->i_channel   = 0;
+    p_main->p_channel->last_change = 0;
+
+    intf_Msg( "network: channels initialized" );
+    return( 0 );
 
-    ASSERT(p_ifdescr);
-    ASSERT(psz_name);
+#else
+    intf_ErrMsg( "network error : channels not supported" );
+    return( 1 );
 
-    /* Which interface are we interested in ? */
-    strcpy(ifr_config.ifr_name, psz_name);
+#endif
+}
 
-    /* Read the flags for that interface */
-    i_rc = ioctl(i_sockfd, SIOCGIFFLAGS, (byte_t *)&ifr_config);
-    if( !i_rc )
+/*****************************************************************************
+ * 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
+ * should be unlocked using input_ChannelLeave().
+ * Non 0 will be returned in case of error.
+ *****************************************************************************/
+int network_ChannelJoin( int i_channel )
+{
+/* I still prefer to put BeOS a bit apart */
+#if defined( SYS_BEOS )
+    intf_ErrMsg( "network error: channels are not yet supported under BeOS" );
+    return( -1 );
+
+#elif defined( SYS_LINUX ) || defined( WIN32 )
+    int                 i_socket;
+    int                 i_fromlen;
+    struct sockaddr_in  sa_server;
+    struct sockaddr_in  sa_client;
+    unsigned int        i_version = 12;
+    char                psz_mess[ 80 ];
+    char                psz_mac[ 40 ];
+    char                i_mess_length = 80;
+    unsigned long int   i_date;
+    struct timeval      answer_delay;
+    int                 i_nbanswer;
+    char                i_answer;
+    fd_set              fds;
+    unsigned int        i_rc;
+    char *              psz_channel_server;
+
+    if( !main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
+                              INPUT_NETWORK_CHANNEL_DEFAULT  ) )
     {
-        p_ifdescr->i_flags = ifr_config.ifr_flags;
-        intf_DbgMsg("%s flags: 0x%x", psz_name, p_ifdescr->i_flags);
+        intf_ErrMsg( "network: channels disabled, to enable them, use the"
+                     "--channels option" );
+        return( -1 );
     }
-    else
+
+    /* debug */
+    intf_DbgMsg( "network: ChannelJoin : %d", i_channel );
+    /* If last change is too recent, wait a while */
+    if( mdate() - p_main->p_channel->last_change < INPUT_CHANNEL_CHANGE_DELAY )
     {
-        intf_ErrMsg( "net error: cannot read flags for interface %s (%s)",
-                     psz_name, strerror(errno) );
-        return -1;
+        intf_WarnMsg( 2, "network: waiting before changing channel" );
+        mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY );
     }
 
-   /* Read physical address of the interface and store it in our description */
-#ifdef SYS_SOLARIS
-    i_rc = ioctl(i_sockfd, SIOCGENADDR, (byte_t *)&ifr_config);
+    p_main->p_channel->last_change = mdate();
+    p_main->p_channel->i_channel   = i_channel;
+
+    intf_WarnMsg( 2, "network: joining channel %d", i_channel );
+
+    /*
+     * Initializing the socket
+     */
+    i_socket = socket( AF_INET, SOCK_DGRAM, 0 );
+
+    /*
+     * Getting the server's information
+     */
+    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 ) );
+
+    memset( &sa_server, 0x00, sizeof(struct sockaddr_in) );
+    sa_server.sin_family = AF_INET;
+    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 ) );
+#ifdef HAVE_ARPA_INET_H
+    inet_aton( psz_channel_server, &sa_server.sin_addr );
 #else
-    i_rc = ioctl(i_sockfd, SIOCGIFHWADDR, (byte_t *)&ifr_config);
+    sa_server.sin_addr.s_addr = inet_addr( psz_channel_server );
 #endif
-    if( !i_rc )
-    {
-        memcpy(&p_ifdescr->sa_phys_addr, &ifr_config.ifr_addr, sizeof(struct sockaddr));
-        intf_DbgMsg("%s MAC address: %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x", psz_name,
-                    p_ifdescr->sa_phys_addr.sa_data[0]&0xff,
-                    p_ifdescr->sa_phys_addr.sa_data[1]&0xff,
-                    p_ifdescr->sa_phys_addr.sa_data[2]&0xff,
-                    p_ifdescr->sa_phys_addr.sa_data[3]&0xff,
-                    p_ifdescr->sa_phys_addr.sa_data[4]&0xff,
-                    p_ifdescr->sa_phys_addr.sa_data[5]&0xff);
-    }
-    else
-    {
-        intf_ErrMsg( "net error: cannot read hardware address for %s (%s)",
-                     psz_name, strerror(errno) );
-        return -1;
-    }
+    free( psz_channel_server );
 
-    /* Read IP address of the interface and store it in our description */
-    i_rc = ioctl(i_sockfd, SIOCGIFADDR, (byte_t *)&ifr_config);
-    if( !i_rc )
+    /*
+     * Looking for the interface MAC address
+     */
+    if( GetMacAddress( i_socket, psz_mac ) )
     {
-        memcpy(&p_ifdescr->sa_net_addr, &ifr_config.ifr_addr, sizeof(struct sockaddr));
-        intf_DbgMsg("%s IP address: %s", psz_name,
-                    inet_ntoa(p_ifdescr->sa_net_addr.sin_addr));
+        intf_ErrMsg( "network error: failed getting MAC address" );
+        return( -1 );
     }
-    else
+
+    /*
+     * Getting date of the client in seconds
+     */
+    i_date = mdate() / 1000000;
+    intf_DbgMsg( "vlcs: date %lu", i_date );
+
+    /*
+     * Build of the message
+     */
+    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 );
+
+    /*
+     * Open the socket 2
+     */
+    memset( &sa_client, 0x00, 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, (struct sockaddr *)(&sa_client),\
+                 sizeof(struct sockaddr) );
+    if ( i_rc )
     {
-        intf_ErrMsg( "net error: cannot read network address for %s (%s)",
-                     psz_name, strerror(errno) );
+        intf_ErrMsg( "vlcs: Unable to bind socket:%u ", i_rc );
+    /* TODO put CS_R_BIND in types.h*/
+    /*    return CS_R_SOCKET;*/
         return -1;
     }
 
-  /* Read broadcast address of the interface and store it in our description */
-    if(p_ifdescr->i_flags & IFF_POINTOPOINT)
+    /*
+     * Send the message
+     */
+    sendto( i_socket, psz_mess, i_mess_length, 0, \
+            (struct sockaddr *)(&sa_server),   \
+            sizeof(struct sockaddr) );
+
+     /*
+     * Waiting 5 sec for one answer from the server
+     */
+    answer_delay.tv_sec  = 5;
+    answer_delay.tv_usec = 0;
+    FD_ZERO( &fds );
+    FD_SET( i_socket, &fds );
+    i_nbanswer = select( i_socket + 1, &fds, NULL, NULL, &answer_delay );
+
+    switch( i_nbanswer )
     {
-        intf_DbgMsg("%s doen't not support broadcast", psz_name);
-        i_rc = ioctl(i_sockfd, SIOCGIFDSTADDR, (byte_t *)&ifr_config);
-    }
-    else
-    {
-        intf_DbgMsg("%s supports broadcast", psz_name);
-        i_rc = ioctl(i_sockfd, SIOCGIFBRDADDR, (byte_t *)&ifr_config);
-    }
-    if( !i_rc )
-    {
-        memcpy(&p_ifdescr->sa_bcast_addr, &ifr_config.ifr_addr, sizeof(struct sockaddr));
-        intf_DbgMsg("%s broadcast address: %s", psz_name,
-                    inet_ntoa(p_ifdescr->sa_bcast_addr.sin_addr));
-    }
-    else
-    {
-        intf_ErrMsg( "net error: cannot read broadcast address for %s (%s)",
-                     psz_name, strerror(errno));
-        return -1;
+    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,\
+                  (struct sockaddr *)(&sa_client), &i_fromlen);
+
+        intf_DbgMsg( "vlcs: the answer : %i", i_answer );
+
+        switch( i_answer )
+        {
+            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;
+        }
+        break;
     }
-#endif
 
-    return i_rc;
+    /*
+     * Close the socket
+     */
+    close( i_socket );
+
+    return( 0 );
+
+#else
+    intf_ErrMsg( "network error: channels not supported" );
+    return( -1 );
+
+#endif
 }
 
+/* Following functions are local */
 
 /*****************************************************************************
- * ReadNetConf: Retrieve the network configuration of the host
- *****************************************************************************
- * Only IP interfaces are listed, and only if they are up
- * i_sockfd must reference a socket open as follow: AF_INET, DOCK_DGRAM, 0
+ * GetMacAddress: extract the MAC Address
  *****************************************************************************/
-int ReadNetConf(int i_sockfd, net_descr_t* p_net_descr)
+static int GetMacAddress( int i_socket, char *psz_mac )
 {
-    int i_rc = 0;
+#if defined( SYS_LINUX )
+    struct ifreq interface;
+    int i_ret;
 
-#if defined (HAVE_SYS_IOCTL_H) && defined (HAVE_NET_IF_H)
-    struct ifreq* a_ifr_ifconf = NULL;
-    struct ifreq* p_ifr_current_if;
-    struct ifconf ifc_netconf;
+    /*
+     * Looking for information about the eth0 interface
+     */
+    interface.ifr_addr.sa_family = AF_INET;
+    strcpy( interface.ifr_name, INPUT_IFACE_DEFAULT );
 
-    int i_if_number;
-    int i_remaining;
+    i_ret = ioctl( i_socket, SIOCGIFHWADDR, &interface );
 
-    ASSERT(p_net_descr);
+    if( i_ret )
+    {
+        intf_ErrMsg( "network error: ioctl SIOCGIFHWADDR failed" );
+        return( i_ret );
+    }
 
-    /* Start by assuming we have few than 3 interfaces (i_if_number will
-       be incremented by 1 when entering the loop) */
-    i_if_number = 2;
+    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 );
 
-    /* Retrieve network configuration for that host */
-    do
-    {
-        i_if_number++;
-        a_ifr_ifconf = realloc(a_ifr_ifconf, i_if_number*sizeof(struct ifreq));
-        ifc_netconf.ifc_len = i_if_number*sizeof(struct ifreq);
-        ifc_netconf.ifc_req = a_ifr_ifconf;
+    return( 0 );
 
-        i_rc = ioctl(i_sockfd, SIOCGIFCONF, (byte_t*)&ifc_netconf);
-        if( i_rc )
-        {
-            intf_ErrMsg( "net error: cannot read network configuration (%s)",
-                         strerror(errno));
-            break;
-        }
-    }
-    /* If we detected ifc_len interfaces, this may mean that others have
-       been missed because the a_ifr_ifconf was to little, so increase
-       it's size and retry */
-    while( ifc_netconf.ifc_len >= i_if_number*sizeof(struct ifreq) );
+#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 );
 
-    /* No see what we detected */
-    if( !i_rc )
+    /* Get all of the local ethernet addresses */
+    for ( i = 0; i < AdapterList.length ; ++i )
     {
-        /* Init the given net_descr_t struct */
-        p_net_descr->i_if_number = 0;
-        p_net_descr->a_if = NULL;
-
-        /* Iterate through the entries of the a_ifr_ifconf table */
-        p_ifr_current_if = ifc_netconf.ifc_req;
-        for( i_remaining = ifc_netconf.ifc_len / sizeof (struct ifreq);
-             i_remaining-- > 0; p_ifr_current_if++ )
+        if ( GetAdapterInfo ( AdapterList.lana[ i ], psz_mac ) == 0 )
         {
-            intf_DbgMsg("Found interface %s", p_ifr_current_if->ifr_name);
-
-            /* Don't use an interface devoted to an address family other than IP */
-            if(p_ifr_current_if->ifr_addr.sa_family != AF_INET)
-                continue;
-
-            /* Read the status of this interface */
-            if( ioctl(i_sockfd, SIOCGIFFLAGS, (byte_t *)p_ifr_current_if) < 0 )
-            {
-                intf_ErrMsg( "net error: cannot access interface %s (%s)",
-                             p_ifr_current_if->ifr_name, strerror(errno) );
-                i_rc = -1;
-                break;
-            }
-            else
-            {
-                /* Skip this interface if it is not up or if this is a loopback one */
-                if( !p_ifr_current_if->ifr_flags & IFF_UP ||
-                    p_ifr_current_if->ifr_flags & IFF_LOOPBACK )
-                  continue;
-
-                /* Add an entry to the net_descr struct to store the description of
-                   that interface */
-                p_net_descr->i_if_number++;
-                p_net_descr->a_if = realloc(p_net_descr->a_if,
-                                            p_net_descr->i_if_number*sizeof(if_descr_t));
-                /* FIXME: Read the info ?? */
-                i_rc = ReadIfConf(i_sockfd, &p_net_descr->a_if[p_net_descr->i_if_number-1],
-                                  p_ifr_current_if->ifr_name);
-            }
+           i_ret = 0;
         }
     }
 
-    /* Don't need the a_ifr_ifconf anymore */
-    free( a_ifr_ifconf );
-#endif
+    return( i_ret );
+
+#else
+    return( -1);
 
-    return i_rc;
+#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 */
 
-#endif