1 /*****************************************************************************
2 * netutils.c: various network functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001 VideoLAN
5 * $Id: netutils.c,v 1.22 2001/03/21 13:42:34 sam Exp $
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Benoit Steiner <benny@via.ecp.fr>
9 * Henri Fallon <henri@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <netdb.h> /* gethostbyname() */
32 #include <stdlib.h> /* free(), realloc(), atoi() */
33 #include <errno.h> /* errno() */
34 #include <string.h> /* bzero(), bcopy() */
35 #include <unistd.h> /* gethostname() */
37 #include <netinet/in.h> /* BSD: struct in_addr */
38 #include <sys/socket.h> /* BSD: struct sockaddr */
39 #ifdef HAVE_ARPA_INET_H
40 #include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
43 #if defined (HAVE_NET_IF_H)
44 #include <net/if.h> /* interface (arch-dependent) */
47 #ifdef HAVE_SYS_SOCKIO_H
48 #include <sys/sockio.h>
58 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
62 /*****************************************************************************
63 * input_BuildLocalAddr : fill a sockaddr_in structure for local binding
64 *****************************************************************************/
65 int network_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port,
66 boolean_t b_broadcast )
68 char psz_hostname[INPUT_MAX_SOURCE_LENGTH];
69 struct hostent * p_hostent;
72 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
73 p_socket->sin_family = AF_INET; /* family */
74 p_socket->sin_port = htons( i_port );
77 /* Try to get our own IP */
78 if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
80 intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
88 /* Instead of trying to find the broadcast address using non-portable
89 * ioctl, let's bind INADDR_ANY */
90 strncpy(psz_hostname,"0.0.0.0",sizeof(psz_hostname));
93 /* Try to convert address directly from in_addr - this will work if
94 * psz_in_addr is dotted decimal. */
95 #ifdef HAVE_ARPA_INET_H
96 if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
98 if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
101 /* We have a fqdn, try to find its address */
102 if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
104 intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
108 /* Copy the first address of the host in the socket address */
109 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
110 p_hostent->h_length );
115 /*****************************************************************************
116 * input_BuildRemoteAddr : fill a sockaddr_in structure for remote host
117 *****************************************************************************/
118 int network_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
120 struct hostent * p_hostent;
122 /* Reset structure */
123 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
124 p_socket->sin_family = AF_INET; /* family */
125 p_socket->sin_port = htons( 0 ); /* This is for remote end */
127 /* Try to convert address directly from in_addr - this will work if
128 * psz_in_addr is dotted decimal. */
130 #ifdef HAVE_ARPA_INET_H
131 if( !inet_aton( psz_server, &p_socket->sin_addr) )
133 if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
136 /* We have a fqdn, try to find its address */
137 if ( (p_hostent = gethostbyname(psz_server)) == NULL )
139 intf_ErrMsg( "BuildRemoteAddr: unknown host %s",
144 /* Copy the first address of the host in the socket address */
145 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
146 p_hostent->h_length );