]> git.sesse.net Git - vlc/blob - src/misc/netutils.c
f352ff80f9335a353119383e80929cb0b959cea6
[vlc] / src / misc / netutils.c
1 /*****************************************************************************
2  * netutils.c: various network functions
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 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
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() */
39 #endif
40
41 #if defined (HAVE_NET_IF_H)
42 #include <net/if.h>                            /* interface (arch-dependent) */
43 #endif
44
45 #ifdef HAVE_SYS_SOCKIO_H
46 #include <sys/sockio.h>
47 #endif
48
49 #include "config.h"
50 #include "common.h"
51 #include "mtime.h"
52 #include "threads.h"
53
54 #include "intf_msg.h"
55
56 #include "netutils.h"
57
58
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 )
65 {
66     char                psz_hostname[INPUT_MAX_SOURCE_LENGTH];
67     struct hostent    * p_hostent;
68     
69     /* Reset struct */
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 );
73     if( !b_broadcast )
74     {
75         /* Try to get our own IP */
76         if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
77         {
78             intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
79                          strerror( errno ) );
80             return( -1 );
81         }
82
83     }
84     else
85     {
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));
89     }
90
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) )
95 #else
96     if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
97 #endif
98     {
99         /* We have a fqdn, try to find its address */
100         if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
101         {
102             intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
103             return( -1 );
104         }
105         
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 );
109     }
110     return( 0 );
111 }
112
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 )
117 {
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 */
124     
125      /* Try to convert address directly from in_addr - this will work if
126       * psz_in_addr is dotted decimal. */
127
128 #ifdef HAVE_ARPA_INET_H
129     if( !inet_aton( psz_server, &p_socket->sin_addr) )
130 #else
131     if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
132 #endif
133     {
134         /* We have a fqdn, try to find its address */
135         if ( (p_hostent = gethostbyname(psz_server)) == NULL )
136         {
137             intf_ErrMsg( "BuildRemoteAddr: unknown host %s", 
138                          psz_server );
139             return( -1 );
140         }
141         
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 );
145     }
146     return( 0 );
147 }
148 #endif