]> git.sesse.net Git - vlc/blobdiff - src/misc/netutils.c
* ALL: new module API. Makes a few things a lot simpler, and we gain
[vlc] / src / misc / netutils.c
index 6398dfcf056aa2bf5e7f47e084521abee72eec0a..03601ddea0c9bc6c366081439b39ef1a6815ef52 100644 (file)
@@ -1,13 +1,15 @@
 /*****************************************************************************
  * netutils.c: various network functions
  *****************************************************************************
- * Copyright (C) 1999, 2000, 2001 VideoLAN
- * $Id: netutils.c,v 1.38 2001/10/03 02:20:39 tcastley Exp $
+ * Copyright (C) 1999-2001 VideoLAN
+ * $Id: netutils.c,v 1.73 2002/07/31 20:56:53 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>
+ *          Xavier Marchesini <xav@alarue.net>
+ *          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
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include "defs.h"
-
 #include <stdlib.h>                             /* free(), realloc(), atoi() */
 #include <errno.h>                                                /* errno() */
 #include <string.h>                                              /* memset() */
 
+#include <vlc/vlc.h>
+
 #ifdef HAVE_UNISTD_H
-#include <unistd.h>                                         /* gethostname() */
+#   include <unistd.h>                                      /* gethostname() */
 #elif defined( _MSC_VER ) && defined( _WIN32 )
-#include <io.h>
+#   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() */
+#ifdef WIN32
+#   include <winsock2.h>
+#else
+#   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
@@ -58,8 +61,9 @@
 #endif
 
 #if defined( WIN32 )                    /* tools to get the MAC adress from  */
-#include <windows.h>                    /* the interface under Windows      */
+#include <windows.h>                    /* the interface under Windows       */
 #include <stdio.h>
+#include <nb30.h>
 #endif
 
 #ifdef HAVE_NET_IF_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 "netutils.h"
+#include "vlc_playlist.h"
+
+#include "network.h"
 
 /*****************************************************************************
  * input_channel_t: channel library data
  * as it depends on the VideoLAN channel server, which isn't frozen for
  * the time being.
  *****************************************************************************/
-typedef struct input_channel_s
+struct input_channel_t
 {
     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 );
+static int GetMacAddress   ( vlc_object_t *, 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
- *****************************************************************************/
-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;
-
-#if defined( SYS_BEOS )
-    intf_ErrMsg( "error: channel changing is not yet supported under BeOS" );
-    return( 1 );
-#endif
-
-    /* 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 );
-        }
-
-    }
-    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;
-
-#if defined( SYS_BEOS )
-    intf_ErrMsg( "error: channel changing is not yet supported under BeOS" );
-    return( 1 );
-#endif
-
-    /* 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. */
-
-#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
-    {
-        /* 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 );
-        }
-
-        /* 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_ChannelCreate: initialize global channel method data
  *****************************************************************************
@@ -211,36 +108,27 @@ int network_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
  * once before any input thread is created or any call to other
  * input_Channel*() function is attempted.
  *****************************************************************************/
-int network_ChannelCreate( void )
+int __network_ChannelCreate( vlc_object_t *p_this )
 {
-/* 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 */
+#if !defined( SYS_LINUX ) && !defined( WIN32 )
+    msg_Err( p_this, "VLAN-based channels are not supported "
+                     "on this architecture" );
+#endif
+
     /* Allocate structure */
-    p_main->p_channel = malloc( sizeof( input_channel_t ) );
-    if( p_main->p_channel == NULL )
+    p_this->p_vlc->p_channel = malloc( sizeof( input_channel_t ) );
+    if( p_this->p_vlc->p_channel == NULL )
     {
-        intf_ErrMsg( "network error: could not create channel bank" );
+        msg_Err( p_this, "out of memory" );
         return( -1 );
     }
 
     /* Initialize structure */
-    p_main->p_channel->i_channel   = 0;
-    p_main->p_channel->last_change = 0;
+    p_this->p_vlc->p_channel->i_channel   = 0;
+    p_this->p_vlc->p_channel->last_change = 0;
 
-    intf_Msg( "network: channels initialized" );
+    msg_Dbg( p_this, "channels initialized" );
     return( 0 );
-
-#else
-    intf_ErrMsg( "network error : channels not supported" );
-    return( 1 );
-
-#endif
 }
 
 /*****************************************************************************
@@ -250,184 +138,153 @@ int network_ChannelCreate( void )
  * 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
+ * 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 )
+int __network_ChannelJoin( vlc_object_t *p_this, 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  ) )
+#define VLCS_VERSION 13
+#define MESSAGE_LENGTH 256
+
+    module_t *       p_network;
+    char *           psz_network = NULL;
+    network_socket_t socket_desc;
+    char psz_mess[ MESSAGE_LENGTH ];
+    char psz_mac[ 40 ];
+    int i_fd, i_port;
+    char *psz_vlcs;
+    struct timeval delay;
+    fd_set fds;
+
+    if( p_this->p_vlc->p_channel->i_channel == i_channel )
     {
-        intf_ErrMsg( "network: channels disabled, to enable them, use the"
-                     "--channels option" );
-        return( -1 );
+        return 0;
     }
 
-    /* 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 )
+    if( !config_GetInt( p_this, "network-channel" ) )
     {
-        intf_WarnMsg( 2, "network: waiting before changing channel" );
-        mwait( p_main->p_channel->last_change + INPUT_CHANNEL_CHANGE_DELAY );
+        msg_Err( p_this, "channels disabled, to enable them, use the"
+                         " --channels option" );
+        return -1;
     }
 
-    p_main->p_channel->last_change = mdate();
-    p_main->p_channel->i_channel   = i_channel;
+    if( config_GetInt( p_this, "ipv4" ) )
+    {
+        psz_network = "ipv4";
+    }
+    if( config_GetInt( p_this, "ipv6" ) )
+    {
+        psz_network = "ipv6";
+    }
 
-    intf_WarnMsg( 2, "network: joining channel %d", i_channel );
+    /* Getting information about the channel server */
+    if( !(psz_vlcs = config_GetPsz( p_this, "channel-server" )) )
+    {
+        msg_Err( p_this, "configuration variable channel-server empty" );
+        return -1;
+    }
 
-    /*
-     * Initializing the socket
-     */
-    i_socket = socket( AF_INET, SOCK_DGRAM, 0 );
+    i_port = config_GetInt( p_this, "channel-port" );
 
-    /*
-     * 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
-    sa_server.sin_addr.s_addr = inet_addr( psz_channel_server );
-#endif
-    free( psz_channel_server );
+    msg_Dbg( p_this, "connecting to %s:%d", psz_vlcs, i_port );
 
-    /*
-     * Looking for the interface MAC address
-     */
-    if( GetMacAddress( i_socket, psz_mac ) )
+    /* Prepare the network_socket_t structure */
+    socket_desc.i_type = NETWORK_UDP;
+    socket_desc.psz_bind_addr = "";
+    socket_desc.i_bind_port = 4321;
+    socket_desc.psz_server_addr = psz_vlcs;
+    socket_desc.i_server_port = i_port;
+
+    /* Find an appropriate network module */
+    p_network = module_Need( p_this, "network", psz_network/*, &socket_desc*/ );
+    if( p_network == NULL )
     {
-        intf_ErrMsg( "network error: failed getting MAC address" );
         return( -1 );
     }
+    module_Unneed( p_this, p_network );
 
-    /*
-     * 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 );
+    free( psz_vlcs ); /* Do we really need this ? -- Meuuh */
+    i_fd = socket_desc.i_handle;
 
-    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 )
+    /* Look for the interface MAC address */
+    if( GetMacAddress( p_this, i_fd, psz_mac ) )
     {
-        intf_ErrMsg( "vlcs: Unable to bind socket:%u ", i_rc );
-    /* TODO put CS_R_BIND in types.h*/
-    /*    return CS_R_SOCKET;*/
+        msg_Err( p_this, "failed getting MAC address" );
+        close( i_fd );
         return -1;
     }
 
-    /*
-     * Send the message
-     */
-    sendto( i_socket, psz_mess, i_mess_length, 0, \
-            (struct sockaddr *)(&sa_server),   \
-            sizeof(struct sockaddr) );
+    msg_Dbg( p_this, "MAC address is %s", psz_mac );
 
-     /*
-     * 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 );
+    /* Build the message */
+    sprintf( psz_mess, "%d %u %lu %s \n", i_channel, VLCS_VERSION,
+                       (unsigned long)(mdate() / (u64)1000000),
+                       psz_mac );
 
-    switch( i_nbanswer )
-    {
-    case 0:
-        intf_DbgMsg( "vlcs: no answer" );
-        break;
+    /* Send the message */
+    send( i_fd, psz_mess, MESSAGE_LENGTH, 0 );
 
-    case -1:
-        intf_DbgMsg( "vlcs: unable to receive the answer ");
-        break;
+    msg_Dbg( p_this, "attempting to join channel %d", i_channel );
 
-    default:
-        recvfrom( i_socket, &i_answer, sizeof(char), 0,\
-                  (struct sockaddr *)(&sa_client), &i_fromlen);
+    /* We have changed channels ! (or at least, we tried) */
+    p_this->p_vlc->p_channel->last_change = mdate();
+    p_this->p_vlc->p_channel->i_channel = i_channel;
 
-        intf_DbgMsg( "vlcs: the answer : %i", i_answer );
+    /* 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( 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;
+    switch( select( i_fd + 1, &fds, NULL, NULL, &delay ) )
+    {
+        case 0:
+            msg_Err( p_this, "no answer from vlcs" );
+            close( i_fd );
+            return -1;
+
+        case -1:
+            msg_Err( p_this, "error while listening to vlcs" );
+            close( i_fd );
+            return -1;
     }
 
-    /*
-     * Close the socket
-     */
-    close( i_socket );
+    recv( i_fd, psz_mess, MESSAGE_LENGTH, 0 );
+    psz_mess[ MESSAGE_LENGTH - 1 ] = '\0';
 
-    return( 0 );
+    if( !strncasecmp( psz_mess, "E:", 2 ) )
+    {
+        msg_Err( p_this, "vlcs said '%s'", psz_mess + 2 );
+        close( i_fd );
+        return -1;
+    }
+    else if( !strncasecmp( psz_mess, "I:", 2 ) )
+    {
+        msg_Dbg( p_this, "vlcs said '%s'", psz_mess + 2 );
+    }
+    else
+    {
+        /* We got something to play ! */
+        playlist_t *p_playlist;
+        p_playlist = vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
+                                              FIND_ANYWHERE );
+        if( p_playlist != NULL )
+        {
+            playlist_Add( p_playlist, psz_mess,
+                          PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
+            vlc_object_release( p_playlist );
+        }
+    }
 
+    /* Close the socket and return nicely */
+#ifndef WIN32
+    close( i_fd );
 #else
-    intf_ErrMsg( "network error: channels not supported" );
-    return( -1 );
-
+    closesocket( i_fd );
 #endif
+
+    return 0;
 }
 
 /* Following functions are local */
@@ -435,23 +292,30 @@ int network_ChannelJoin( int i_channel )
 /*****************************************************************************
  * GetMacAddress: extract the MAC Address
  *****************************************************************************/
-static int GetMacAddress( int i_socket, char *psz_mac )
+static int GetMacAddress( vlc_object_t *p_this, int i_fd, char *psz_mac )
 {
 #if defined( SYS_LINUX )
     struct ifreq interface;
     int i_ret;
+    char *psz_interface;
 
     /*
      * Looking for information about the eth0 interface
      */
     interface.ifr_addr.sa_family = AF_INET;
-    strcpy( interface.ifr_name, INPUT_IFACE_DEFAULT );
+    if( !(psz_interface = config_GetPsz( p_this, "iface" )) )
+    {
+        msg_Err( p_this, "configuration variable iface empty" );
+        return -1;
+    }
+    strcpy( interface.ifr_name, psz_interface );
+    free( psz_interface );
 
-    i_ret = ioctl( i_socket, SIOCGIFHWADDR, &interface );
+    i_ret = ioctl( i_fd, SIOCGIFHWADDR, &interface );
 
     if( i_ret )
     {
-        intf_ErrMsg( "network error: ioctl SIOCGIFHWADDR failed" );
+        msg_Err( p_this, "ioctl SIOCGIFHWADDR failed" );
         return( i_ret );
     }
 
@@ -472,7 +336,7 @@ static int GetMacAddress( int i_socket, char *psz_mac )
     LANA_ENUM AdapterList;
     NCB       Ncb;
 
-    intf_WarnMsg( 2, "network: looking for MAC address" );
+    msg_Dbg( p_this, "looking for MAC address" );
 
     memset( &Ncb, 0, sizeof( NCB ) );
     Ncb.ncb_command = NCBENUM;
@@ -485,14 +349,15 @@ static int GetMacAddress( int i_socket, char *psz_mac )
     {
         if ( GetAdapterInfo ( AdapterList.lana[ i ], psz_mac ) == 0 )
         {
-           i_ret = 0;
+            i_ret = 0;
         }
     }
 
     return( i_ret );
 
 #else
-    return( -1);
+    strcpy( psz_mac, "00:00:00:00:00:00" );
+    return( 0 );
 
 #endif
 }
@@ -517,7 +382,7 @@ static int GetAdapterInfo( int i_adapter, char *psz_string )
 
     if( Netbios( &Ncb ) != NRC_GOODRET )
     {
-        intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode );
+//X        intf_ErrMsg( "network error: reset returned %i", Ncb.ncb_retcode );
         return -1;
     }
 
@@ -544,13 +409,13 @@ static int GetAdapterInfo( int i_adapter, char *psz_string )
                 (int) ( Adapter.adapt.adapter_address[4] ),
                 (int) ( Adapter.adapt.adapter_address[5] ) );
 
-       intf_WarnMsg( 2, "network: found MAC address %s", psz_string );
+//X        intf_WarnMsg( 2, "network: found MAC address %s", psz_string );
 
         return 0;
     }
     else
     {
-        intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode );
+//X        intf_ErrMsg( "network error: ASTAT returned %i", Ncb.ncb_retcode );
         return -1;
     }
 }