1 /*****************************************************************************
2 * netutils.c: various network functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 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() */
35 #include <netinet/in.h> /* BSD: struct in_addr */
36 #include <sys/socket.h> /* BSD: struct sockaddr */
37 #ifdef HAVE_ARPA_INET_H
38 #include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
41 #if defined (HAVE_NET_IF_H)
42 #include <net/if.h> /* interface (arch-dependent) */
45 #ifdef HAVE_SYS_SOCKIO_H
46 #include <sys/sockio.h>
59 #ifndef SYS_BEOS /* I need help for the BeOS portage */
60 /*****************************************************************************
61 * input_BuildLocalAddr : fill a sockaddr_in structure for local binding
62 *****************************************************************************/
63 int input_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port,
64 boolean_t b_broadcast )
66 char psz_hostname[INPUT_MAX_SOURCE_LENGTH];
67 struct hostent * p_hostent;
70 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
71 p_socket->sin_family = AF_INET; /* family */
72 p_socket->sin_port = htons( i_port );
75 /* Try to get our own IP */
76 if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
78 intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
86 /* Instead of trying to find the broadcast address using non-portable
87 * ioctl, let's bind INADDR_ANY */
88 strncpy(psz_hostname,"0.0.0.0",sizeof(psz_hostname));
91 /* Try to convert address directly from in_addr - this will work if
92 * psz_in_addr is dotted decimal. */
93 #ifdef HAVE_ARPA_INET_H
94 if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
96 if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
99 /* We have a fqdn, try to find its address */
100 if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
102 intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
106 /* Copy the first address of the host in the socket address */
107 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
108 p_hostent->h_length );
113 /*****************************************************************************
114 * input_BuildRemoteAddr : fill a sockaddr_in structure for remote host
115 *****************************************************************************/
116 int input_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
118 struct hostent * p_hostent;
119 printf("BuildRemoteAddr : psz_server = %s\n",psz_server );
120 /* Reset structure */
121 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
122 p_socket->sin_family = AF_INET; /* family */
123 p_socket->sin_port = htons( 0 ); /* This is for remote end */
125 /* Try to convert address directly from in_addr - this will work if
126 * psz_in_addr is dotted decimal. */
128 #ifdef HAVE_ARPA_INET_H
129 if( !inet_aton( psz_server, &p_socket->sin_addr) )
131 if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
134 /* We have a fqdn, try to find its address */
135 if ( (p_hostent = gethostbyname(psz_server)) == NULL )
137 intf_ErrMsg( "BuildRemoteAddr: unknown host %s",
142 /* Copy the first address of the host in the socket address */
143 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
144 p_hostent->h_length );