1 /*****************************************************************************
2 * netutils.c: various network functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001 VideoLAN
6 * Authors: Vincent Seguin <seguin@via.ecp.fr>
7 * Benoit Steiner <benny@via.ecp.fr>
8 * Henri Fallon <henri@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <netdb.h> /* gethostbyname() */
31 #include <stdlib.h> /* free(), realloc(), atoi() */
32 #include <errno.h> /* errno() */
33 #include <string.h> /* bzero(), bcopy() */
34 #include <unistd.h> /* gethostname() */
36 #include <netinet/in.h> /* BSD: struct in_addr */
37 #include <sys/socket.h> /* BSD: struct sockaddr */
38 #ifdef HAVE_ARPA_INET_H
39 #include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
42 #if defined (HAVE_NET_IF_H)
43 #include <net/if.h> /* interface (arch-dependent) */
46 #ifdef HAVE_SYS_SOCKIO_H
47 #include <sys/sockio.h>
57 #ifndef SYS_BEOS /* I need help for the BeOS portage */
61 /*****************************************************************************
62 * input_BuildLocalAddr : fill a sockaddr_in structure for local binding
63 *****************************************************************************/
64 int network_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port,
65 boolean_t b_broadcast )
67 char psz_hostname[INPUT_MAX_SOURCE_LENGTH];
68 struct hostent * p_hostent;
71 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
72 p_socket->sin_family = AF_INET; /* family */
73 p_socket->sin_port = htons( i_port );
76 /* Try to get our own IP */
77 if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
79 intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
87 /* Instead of trying to find the broadcast address using non-portable
88 * ioctl, let's bind INADDR_ANY */
89 strncpy(psz_hostname,"0.0.0.0",sizeof(psz_hostname));
92 /* Try to convert address directly from in_addr - this will work if
93 * psz_in_addr is dotted decimal. */
94 #ifdef HAVE_ARPA_INET_H
95 if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
97 if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
100 /* We have a fqdn, try to find its address */
101 if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
103 intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
107 /* Copy the first address of the host in the socket address */
108 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
109 p_hostent->h_length );
114 /*****************************************************************************
115 * input_BuildRemoteAddr : fill a sockaddr_in structure for remote host
116 *****************************************************************************/
117 int network_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
119 struct hostent * p_hostent;
121 /* Reset structure */
122 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
123 p_socket->sin_family = AF_INET; /* family */
124 p_socket->sin_port = htons( 0 ); /* This is for remote end */
126 /* Try to convert address directly from in_addr - this will work if
127 * psz_in_addr is dotted decimal. */
129 #ifdef HAVE_ARPA_INET_H
130 if( !inet_aton( psz_server, &p_socket->sin_addr) )
132 if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
135 /* We have a fqdn, try to find its address */
136 if ( (p_hostent = gethostbyname(psz_server)) == NULL )
138 intf_ErrMsg( "BuildRemoteAddr: unknown host %s",
143 /* Copy the first address of the host in the socket address */
144 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
145 p_hostent->h_length );