]> git.sesse.net Git - vlc/blob - src/misc/netutils.c
* Header cleaning: filled all empty authors fields, added CVS $Id stuff.
[vlc] / src / misc / netutils.c
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 $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Benoit Steiner <benny@via.ecp.fr>
9  *          Henri Fallon <henri@videolan.org>
10  *
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.
15  * 
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "defs.h"
30
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() */
36
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() */
41 #endif
42
43 #if defined (HAVE_NET_IF_H)
44 #include <net/if.h>                            /* interface (arch-dependent) */
45 #endif
46
47 #ifdef HAVE_SYS_SOCKIO_H
48 #include <sys/sockio.h>
49 #endif
50
51 #include "config.h"
52 #include "common.h"
53 #include "mtime.h"
54 #include "threads.h"
55
56 #include "intf_msg.h"
57
58 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
59
60 #include "netutils.h"
61
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 )
67 {
68     char                psz_hostname[INPUT_MAX_SOURCE_LENGTH];
69     struct hostent    * p_hostent;
70     
71     /* Reset struct */
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 );
75     if( !b_broadcast )
76     {
77         /* Try to get our own IP */
78         if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
79         {
80             intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
81                          strerror( errno ) );
82             return( -1 );
83         }
84
85     }
86     else
87     {
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));
91     }
92
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) )
97 #else
98     if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
99 #endif
100     {
101         /* We have a fqdn, try to find its address */
102         if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
103         {
104             intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
105             return( -1 );
106         }
107         
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 );
111     }
112     return( 0 );
113 }
114
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 )
119 {
120     struct hostent            * p_hostent;
121
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 */
126     
127      /* Try to convert address directly from in_addr - this will work if
128       * psz_in_addr is dotted decimal. */
129
130 #ifdef HAVE_ARPA_INET_H
131     if( !inet_aton( psz_server, &p_socket->sin_addr) )
132 #else
133     if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
134 #endif
135     {
136         /* We have a fqdn, try to find its address */
137         if ( (p_hostent = gethostbyname(psz_server)) == NULL )
138         {
139             intf_ErrMsg( "BuildRemoteAddr: unknown host %s", 
140                          psz_server );
141             return( -1 );
142         }
143         
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 );
147     }
148     return( 0 );
149 }
150 #endif
151