-/*******************************************************************************
+/*****************************************************************************
* netutils.c: various network functions
- * (c)1999 VideoLAN
- *******************************************************************************
- * ??
- *******************************************************************************
- * Required headers:
- * <netinet/in.h>
- *******************************************************************************/
-
-/*******************************************************************************
+ *****************************************************************************
+ * Copyright (C) 1999, 2000 VideoLAN
+ *
+ * Authors:
+ *
+ * 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 GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *****************************************************************************/
+
+/*****************************************************************************
* Preamble
- *******************************************************************************/
-#include <netdb.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <net/if.h>
+ *****************************************************************************/
+#include "defs.h"
+
+#include <netdb.h> /* gethostbyname() */
+#include <stdlib.h> /* free(), realloc(), atoi() */
+#include <errno.h> /* errno() */
+#include <string.h> /* bzero(), bcopy() */
+
+#ifdef SYS_BSD
+#include <netinet/in.h> /* struct in_addr */
+#include <sys/socket.h> /* struct sockaddr */
+#endif
+
+#if defined(SYS_LINUX) || defined(SYS_BSD) || defined(SYS_GNU)
+#include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
+#endif
+
+
+#ifdef SYS_LINUX
+#include <sys/ioctl.h> /* ioctl() */
+#include <net/if.h> /* interface (arch-dependent) */
+#endif
#include "config.h"
#include "common.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 */
+ struct hostent *p_hostent; /* host descriptor */
- bzero( p_sa_in, sizeof( struct sockaddr_in ) );
- p_sa_in->sin_family = AF_INET; /* family */
- p_sa_in->sin_port = htons( i_port ); /* port */
+ 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. */
+ }
+ /* Try to convert address directly from in_addr - this will work if
+ * psz_in_addr is dotted decimal. */
+#ifdef SYS_BEOS
+ else if( (p_sa_in->sin_addr.s_addr = inet_addr( psz_in_addr )) == -1 )
+#else
else if( !inet_aton( psz_in_addr, &p_sa_in->sin_addr) )
+#endif
{
/* The convertion failed: the address is an host name, which needs
* to be resolved */
- intf_DbgMsg("debug: resolving internet address %s...\n", psz_in_addr);
+ intf_DbgMsg("debug: resolving internet address %s...\n", psz_in_addr);
if ( (p_hostent = gethostbyname(psz_in_addr)) == NULL)
{
intf_ErrMsg("error: unknown host %s\n", psz_in_addr);
- return( -1 );
+ return( -1 );
}
/* Copy the first address of the host in the socket address */
- bcopy( p_hostent->h_addr_list[0], &p_sa_in->sin_addr, p_hostent->h_length);
+ memmove( &p_sa_in->sin_addr, p_hostent->h_addr_list[0], p_hostent->h_length );
}
- return( 0 );
+ 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.
- *******************************************************************************/
+ *****************************************************************************/
int ServerPort( char *psz_addr )
{
char *psz_index;
for( psz_index = psz_addr; *psz_index && (*psz_index != ':'); psz_index++ )
{
;
- }
-
+ }
+
/* If a port number has been found, convert it and return it */
if( *psz_index == ':' )
{
return( atoi( psz_index + 1 ) );
}
- 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
- *******************************************************************************/
+ *****************************************************************************/
int ReadIfConf(int i_sockfd, if_descr_t* p_ifdescr, char* psz_name)
-{
- struct ifreq ifr_config;
+{
int i_rc = 0;
-
+#ifdef SYS_LINUX
+ struct ifreq ifr_config;
+
ASSERT(p_ifdescr);
ASSERT(psz_name);
-
+
/* Which interface are we interested in ? */
strcpy(ifr_config.ifr_name, psz_name);
return -1;
}
- /* Read physical address of the interface and store it in our description */
+ /* Read physical address of the interface and store it in our description */
i_rc = ioctl(i_sockfd, SIOCGIFHWADDR, (byte_t *)&ifr_config);
if( !i_rc )
{
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 )
psz_name, strerror(errno));
return -1;
}
-
- /* Read broadcast address of the interface and store it in our description */
+
+ /* 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\n", psz_name);
psz_name, strerror(errno));
return -1;
}
-
+#endif /* SYS_LINUX */
+
return i_rc;
}
-/*******************************************************************************
+/*****************************************************************************
* 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
- *******************************************************************************/
+ *****************************************************************************/
int ReadNetConf(int i_sockfd, net_descr_t* p_net_descr)
{
+#ifdef SYS_LINUX
struct ifreq* a_ifr_ifconf = NULL;
struct ifreq* p_ifr_current_if;
struct ifconf ifc_netconf;
-
+
int i_if_number;
int i_remaining;
+#endif /* SYS_LINUX */
int i_rc = 0;
+#ifdef SYS_LINUX
ASSERT(p_net_descr);
/* Start by assuming we have few than 3 interfaces (i_if_number will
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));
- /* Read the info ??? */
+ /* 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);
}
}
}
-
+
/* Don't need the a_ifr_ifconf anymore */
free( a_ifr_ifconf );
+#endif /* SYS_LINUX */
+
return i_rc;
}