Commit a4dc58c8 authored by Henri Fallon's avatar Henri Fallon

- Moved 2 local network functions
- Removed old code
- Broadcast should work. I'm not detecting broadcast address, i'm
binding 0.0.0.0
parent 335d7364
......@@ -6,6 +6,7 @@
* Copyright (C) 1999, 2000 VideoLAN
*
* Authors: Vincent Seguin <seguin@via.ecp.fr>
* Henri Fallon <henri@videolan.org>
*
* 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
......@@ -22,53 +23,8 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
/*****************************************************************************
* Required headers:
* <netinet/in.h>
* <sys/socket.h>
*****************************************************************************/
/*****************************************************************************
* if_descr_t: describes a network interface.
*****************************************************************************
* Note that if the interface is a point to point one, the broadcast address is
* set to the destination address of that interface
*****************************************************************************/
typedef struct
{
/* Interface device name (e.g. "eth0") */
char* psz_ifname;
/* Interface physical address */
struct sockaddr sa_phys_addr;
/* Interface network address */
struct sockaddr_in sa_net_addr;
/* Interface broadcast address */
struct sockaddr_in sa_bcast_addr;
/* Interface flags: see if.h for their description) */
u16 i_flags;
} if_descr_t;
/*****************************************************************************
* net_descr_t: describes all the interfaces of the computer
*****************************************************************************
* Nothing special to say :)
*****************************************************************************/
typedef struct
{
/* Number of networks interfaces described below */
int i_if_number;
/* Table of if_descr_t describing each interface */
if_descr_t* a_if;
} net_descr_t;
/*****************************************************************************
* Prototypes
*****************************************************************************/
int ReadIfConf (int i_sockfd, if_descr_t* p_ifdescr, char* psz_name);
int ReadNetConf (int i_sockfd, net_descr_t* p_net_descr);
int BuildInetAddr ( struct sockaddr_in *p_sa_in, char *psz_in_addr, int i_port );
int ServerPort ( char *psz_addr );
int input_BuildLocalAddr( struct sockaddr_in *, int, boolean_t );
int input_BuildRemoteAddr(struct sockaddr_in *, char * );
......@@ -4,7 +4,7 @@
* decoders.
*****************************************************************************
* Copyright (C) 1998, 1999, 2000 VideoLAN
* $Id: input.c,v 1.91 2001/03/07 10:31:10 stef Exp $
* $Id: input.c,v 1.92 2001/03/11 19:00:18 henri Exp $
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
*
......@@ -69,6 +69,9 @@
#include "main.h"
/* #include <netutils.h> */
/*****************************************************************************
* Local prototypes
*****************************************************************************/
......@@ -508,76 +511,19 @@ void input_FileClose( input_thread_t * p_input )
return;
}
#ifndef SYS_BEOS
/*****************************************************************************
* input_BuildLocalAddr : fill a sockaddr_in structure for local binding
* input_NetworkOpen : open a network socket
*****************************************************************************/
int input_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port,
boolean_t b_broadcast )
void input_NetworkOpen( input_thread_t * p_input )
{
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( !b_broadcast )
{
/* 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
{
/* Using broadcast address. There are many ways of doing it, one of
* the simpliest being a #define ...
* FIXME : this is ugly */
strncpy( psz_hostname, INPUT_BCAST_ADDR,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 );
}
/*****************************************************************************
* input_BuildRemoteAddr : fill a sockaddr_in structure for remote host
*****************************************************************************/
int input_BuildRemoteAddr( input_thread_t * p_input,
struct sockaddr_in * p_socket )
{
struct hostent * p_hostent;
int i_option_value, i_port;
struct sockaddr_in s_socket;
boolean_t b_broadcast;
/* 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 */
/* FIXME : we don't handle channels for the moment */
/* Get the remote server */
if( p_input->p_source == NULL )
......@@ -586,42 +532,6 @@ int input_BuildRemoteAddr( input_thread_t * p_input,
INPUT_SERVER_DEFAULT );
}
/* 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( p_input->p_source, &p_socket->sin_addr) )
#else
if( (p_socket->sin_addr.s_addr = inet_addr( p_input->p_source )) == -1 )
#endif
{
/* We have a fqdn, try to find its address */
if ( (p_hostent = gethostbyname(p_input->p_source)) == NULL )
{
intf_ErrMsg( "BuildRemoteAddr: unknown host %s",
p_input->p_source );
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 );
}
/*****************************************************************************
* input_NetworkOpen : open a network socket
*****************************************************************************/
void input_NetworkOpen( input_thread_t * p_input )
{
int i_option_value, i_port;
struct sockaddr_in s_socket;
boolean_t b_broadcast;
/* FIXME : we don't handle channels for the moment */
/* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
* protocol */
p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
......@@ -644,7 +554,6 @@ void input_NetworkOpen( input_thread_t * p_input )
return;
}
#ifndef SYS_BEOS
/* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
* packet loss caused by scheduling problems */
i_option_value = 524288;
......@@ -657,7 +566,6 @@ void input_NetworkOpen( input_thread_t * p_input )
p_input->b_error = 1;
return;
}
#endif /* SYS_BEOS */
/* Get details about what we are supposed to do */
b_broadcast = (boolean_t)main_GetIntVariable( INPUT_BROADCAST_VAR, 0 );
......@@ -685,7 +593,7 @@ void input_NetworkOpen( input_thread_t * p_input )
}
/* Build socket for remote connection */
if ( input_BuildRemoteAddr( p_input, &s_socket )
if ( input_BuildRemoteAddr( &s_socket, p_input->p_source )
== -1 )
{
close( p_input->i_handle );
......
......@@ -5,6 +5,7 @@
*
* Authors: Vincent Seguin <seguin@via.ecp.fr>
* Benoit Steiner <benny@via.ecp.fr>
* Henri Fallon <henri@videolan.org>
*
* 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
......@@ -37,16 +38,10 @@
#include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
#endif
#if defined (HAVE_SYS_IOCTL_H)
#include <sys/ioctl.h> /* ioctl() */
#endif
#include <unistd.h> /* needed for ioctl on Solaris */
//#include <stropts.h>
#if defined (HAVE_NET_IF_H)
#include <net/if.h> /* interface (arch-dependent) */
#endif
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif
......@@ -54,282 +49,100 @@
#include "config.h"
#include "common.h"
#include "mtime.h"
#include "threads.h"
#include "intf_msg.h"
#include "debug.h"
#include "netutils.h"
/*****************************************************************************
* BuildInetAddr: build an Internet address descriptor
*****************************************************************************
* 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.
*****************************************************************************/
int BuildInetAddr( struct sockaddr_in *p_sa_in, char *psz_in_addr, int i_port )
{
struct hostent *p_hostent; /* host descriptor */
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 */
/* Use INADDR_ANY if psz_in_addr is NULL */
if( psz_in_addr == NULL )
{
p_sa_in->sin_addr.s_addr = htonl(INADDR_ANY);
}
/* 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_SOLARIS2_6
else if( !inet_aton( psz_in_addr, &p_sa_in->sin_addr) )
#else
else if( (p_sa_in->sin_addr.s_addr = inet_addr( psz_in_addr )) == -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)
{
intf_ErrMsg( "net error: unknown host %s", psz_in_addr );
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 );
}
return( 0 );
}
#ifndef SYS_BEOS /* I need help for the BeOS portage */
/*****************************************************************************
* 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.
* input_BuildLocalAddr : fill a sockaddr_in structure for local binding
*****************************************************************************/
int ServerPort( char *psz_addr )
int input_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port,
boolean_t b_broadcast )
{
char *psz_index;
char psz_hostname[INPUT_MAX_SOURCE_LENGTH];
struct hostent * p_hostent;
/* Scan string for ':' */
for( psz_index = psz_addr; *psz_index && (*psz_index != ':'); psz_index++ )
/* 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( !b_broadcast )
{
;
}
/* If a port number has been found, convert it and return it */
if( *psz_index == ':' )
/* Try to get our own IP */
if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
{
*psz_index = '\0';
return( atoi( psz_index + 1 ) );
intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
strerror( errno ) );
return( -1 );
}
return( - 1 );
}
/*****************************************************************************
* ReadIfConf: Read the configuration of an interface
*****************************************************************************
* i_sockfd must reference a socket open as follow: AF_INET, DOCK_DGRAM, 0
*****************************************************************************/
#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 i_rc = 0;
#if defined (HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H)
struct ifreq ifr_config;
ASSERT(p_ifdescr);
ASSERT(psz_name);
/* Which interface are we interested in ? */
strcpy(ifr_config.ifr_name, psz_name);
/* Read the flags for that interface */
i_rc = ioctl(i_sockfd, SIOCGIFFLAGS, (byte_t *)&ifr_config);
if( !i_rc )
{
p_ifdescr->i_flags = ifr_config.ifr_flags;
intf_DbgMsg("%s flags: 0x%x", psz_name, p_ifdescr->i_flags);
}
else
{
intf_ErrMsg( "net error: cannot read flags for interface %s (%s)",
psz_name, strerror(errno) );
return -1;
/* Instead of trying to find the broadcast address using non-portable
* ioctl, let's bind INADDR_ANY */
strncpy(psz_hostname,"0.0.0.0",sizeof(psz_hostname));
}
/* 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);
/* 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
i_rc = ioctl(i_sockfd, SIOCGIFHWADDR, (byte_t *)&ifr_config);
if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
#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;
}
/* 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 )
{
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));
}
else
/* We have a fqdn, try to find its address */
if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
{
intf_ErrMsg( "net error: cannot read network address for %s (%s)",
psz_name, strerror(errno) );
return -1;
intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
return( -1 );
}
/* Read broadcast address of the interface and store it in our description */
if(p_ifdescr->i_flags & IFF_POINTOPOINT)
{
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;
/* 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 );
}
#endif
return i_rc;
return( 0 );
}
/*****************************************************************************
* 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
* input_BuildRemoteAddr : fill a sockaddr_in structure for remote host
*****************************************************************************/
int ReadNetConf(int i_sockfd, net_descr_t* p_net_descr)
int input_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
{
int i_rc = 0;
#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;
int i_if_number;
int i_remaining;
ASSERT(p_net_descr);
/* 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;
/* 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;
struct hostent * p_hostent;
printf("BuildRemoteAddr : psz_server = %s\n",psz_server );
/* 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 */
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) );
/* No see what we detected */
if( !i_rc )
{
/* Init the given net_descr_t struct */
p_net_descr->i_if_number = 0;
p_net_descr->a_if = NULL;
/* Try to convert address directly from in_addr - this will work if
* psz_in_addr is dotted decimal. */
/* 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++ )
#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
{
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 )
/* We have a fqdn, try to find its address */
if ( (p_hostent = gethostbyname(psz_server)) == NULL )
{
intf_ErrMsg( "net error: cannot access interface %s (%s)",
p_ifr_current_if->ifr_name, strerror(errno) );
i_rc = -1;
break;
intf_ErrMsg( "BuildRemoteAddr: unknown host %s",
psz_server );
return( -1 );
}
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);
}
}
/* 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 );
}
/* Don't need the a_ifr_ifconf anymore */
free( a_ifr_ifconf );
#endif
return i_rc;
return( 0 );
}
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment