]> git.sesse.net Git - vlc/blob - src/misc/netutils.c
* Bug fixes and enhancements in the Gtk+/Gnome interfaces.
[vlc] / src / misc / netutils.c
1 /*****************************************************************************
2  * netutils.c: various network functions
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  *
6  * Authors: Vincent Seguin <seguin@via.ecp.fr>
7  *          Benoit Steiner <benny@via.ecp.fr>
8  *          Henri Fallon <henri@videolan.org>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29
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() */
35
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() */
40 #endif
41
42 #if defined (HAVE_NET_IF_H)
43 #include <net/if.h>                            /* interface (arch-dependent) */
44 #endif
45
46 #ifdef HAVE_SYS_SOCKIO_H
47 #include <sys/sockio.h>
48 #endif
49
50 #include "config.h"
51 #include "common.h"
52 #include "mtime.h"
53 #include "threads.h"
54
55 #include "intf_msg.h"
56
57 #ifndef SYS_BEOS /* I need help for the BeOS portage */
58
59 #include "netutils.h"
60
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 )
66 {
67     char                psz_hostname[INPUT_MAX_SOURCE_LENGTH];
68     struct hostent    * p_hostent;
69     
70     /* Reset struct */
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 );
74     if( !b_broadcast )
75     {
76         /* Try to get our own IP */
77         if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
78         {
79             intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
80                          strerror( errno ) );
81             return( -1 );
82         }
83
84     }
85     else
86     {
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));
90     }
91
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) )
96 #else
97     if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
98 #endif
99     {
100         /* We have a fqdn, try to find its address */
101         if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
102         {
103             intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
104             return( -1 );
105         }
106         
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 );
110     }
111     return( 0 );
112 }
113
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 )
118 {
119     struct hostent            * p_hostent;
120
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 */
125     
126      /* Try to convert address directly from in_addr - this will work if
127       * psz_in_addr is dotted decimal. */
128
129 #ifdef HAVE_ARPA_INET_H
130     if( !inet_aton( psz_server, &p_socket->sin_addr) )
131 #else
132     if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
133 #endif
134     {
135         /* We have a fqdn, try to find its address */
136         if ( (p_hostent = gethostbyname(psz_server)) == NULL )
137         {
138             intf_ErrMsg( "BuildRemoteAddr: unknown host %s", 
139                          psz_server );
140             return( -1 );
141         }
142         
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 );
146     }
147     return( 0 );
148 }
149 #endif
150