netutils.c 5.6 KB
Newer Older
1
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
2
 * netutils.c: various network functions
3
 *****************************************************************************
Sam Hocevar's avatar
   
Sam Hocevar committed
4
 * Copyright (C) 1999, 2000, 2001 VideoLAN
Sam Hocevar's avatar
   
Sam Hocevar committed
5
 * $Id: netutils.c,v 1.22 2001/03/21 13:42:34 sam Exp $
6
 *
7
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
Benoit Steiner's avatar
   
Benoit Steiner committed
8
 *          Benoit Steiner <benny@via.ecp.fr>
Henri Fallon's avatar
   
Henri Fallon committed
9
 *          Henri Fallon <henri@videolan.org>
10
11
12
13
14
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
15
 * 
16
17
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
20
 *
21
22
23
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
25

26
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
27
 * Preamble
28
 *****************************************************************************/
29
30
#include "defs.h"

31
32
33
34
#include <netdb.h>                                        /* gethostbyname() */
#include <stdlib.h>                             /* free(), realloc(), atoi() */
#include <errno.h>                                                /* errno() */
#include <string.h>                                      /* bzero(), bcopy() */
Sam Hocevar's avatar
   
Sam Hocevar committed
35
#include <unistd.h>                                         /* gethostname() */
36

37
38
#include <netinet/in.h>                               /* BSD: struct in_addr */
#include <sys/socket.h>                              /* BSD: struct sockaddr */
39
#ifdef HAVE_ARPA_INET_H
40
41
42
#include <arpa/inet.h>                           /* inet_ntoa(), inet_aton() */
#endif

Benoit Steiner's avatar
Benoit Steiner committed
43
#if defined (HAVE_NET_IF_H)
44
45
#include <net/if.h>                            /* interface (arch-dependent) */
#endif
Henri Fallon's avatar
   
Henri Fallon committed
46

Benoit Steiner's avatar
   
Benoit Steiner committed
47
#ifdef HAVE_SYS_SOCKIO_H
Benoit Steiner's avatar
Benoit Steiner committed
48
#include <sys/sockio.h>
Benoit Steiner's avatar
   
Benoit Steiner committed
49
#endif
Michel Kaempf's avatar
Michel Kaempf committed
50
51
52
53

#include "config.h"
#include "common.h"
#include "mtime.h"
Henri Fallon's avatar
   
Henri Fallon committed
54
#include "threads.h"
Michel Kaempf's avatar
Michel Kaempf committed
55
56
57

#include "intf_msg.h"

Sam Hocevar's avatar
   
Sam Hocevar committed
58
#if !defined( SYS_BEOS ) && !defined( SYS_NTO )
Michel Kaempf's avatar
Michel Kaempf committed
59

Sam Hocevar's avatar
   
Sam Hocevar committed
60
#include "netutils.h"
Henri Fallon's avatar
   
Henri Fallon committed
61

62
/*****************************************************************************
Henri Fallon's avatar
   
Henri Fallon committed
63
 * input_BuildLocalAddr : fill a sockaddr_in structure for local binding
64
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
65
66
int network_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port, 
                            boolean_t b_broadcast )
Michel Kaempf's avatar
Michel Kaempf committed
67
{
Henri Fallon's avatar
   
Henri Fallon committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    char                psz_hostname[INPUT_MAX_SOURCE_LENGTH];
    struct hostent    * p_hostent;
    
    /* Reset struct */
    memset( p_socket, 0, sizeof( struct sockaddr_in ) );
    p_socket->sin_family = AF_INET;                                 /* family */
    p_socket->sin_port = htons( i_port );
    if( !b_broadcast )
    {
        /* Try to get our own IP */
        if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
        {
            intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
                         strerror( errno ) );
            return( -1 );
        }
Michel Kaempf's avatar
Michel Kaempf committed
84

Henri Fallon's avatar
   
Henri Fallon committed
85
86
    }
    else
87
    {
Henri Fallon's avatar
   
Henri Fallon committed
88
89
90
        /* Instead of trying to find the broadcast address using non-portable
         * ioctl, let's bind INADDR_ANY */
        strncpy(psz_hostname,"0.0.0.0",sizeof(psz_hostname));
91
    }
Henri Fallon's avatar
   
Henri Fallon committed
92

93
94
    /* Try to convert address directly from in_addr - this will work if
     * psz_in_addr is dotted decimal. */
Henri Fallon's avatar
   
Henri Fallon committed
95
96
#ifdef HAVE_ARPA_INET_H
    if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
97
#else
Henri Fallon's avatar
   
Henri Fallon committed
98
    if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
99
#endif
Michel Kaempf's avatar
Michel Kaempf committed
100
    {
Henri Fallon's avatar
   
Henri Fallon committed
101
102
        /* We have a fqdn, try to find its address */
        if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
103
        {
Henri Fallon's avatar
   
Henri Fallon committed
104
            intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
105
            return( -1 );
Michel Kaempf's avatar
Michel Kaempf committed
106
        }
Henri Fallon's avatar
   
Henri Fallon committed
107
        
Michel Kaempf's avatar
Michel Kaempf committed
108
        /* Copy the first address of the host in the socket address */
Henri Fallon's avatar
   
Henri Fallon committed
109
110
        memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0], 
                 p_hostent->h_length );
Michel Kaempf's avatar
Michel Kaempf committed
111
    }
112
    return( 0 );
Michel Kaempf's avatar
Michel Kaempf committed
113
114
}

115
/*****************************************************************************
Henri Fallon's avatar
   
Henri Fallon committed
116
 * input_BuildRemoteAddr : fill a sockaddr_in structure for remote host
117
 *****************************************************************************/
Sam Hocevar's avatar
   
Sam Hocevar committed
118
int network_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
Michel Kaempf's avatar
Michel Kaempf committed
119
{
Henri Fallon's avatar
   
Henri Fallon committed
120
    struct hostent            * p_hostent;
Sam Hocevar's avatar
   
Sam Hocevar committed
121

Henri Fallon's avatar
   
Henri Fallon committed
122
123
124
125
126
127
128
    /* Reset structure */
    memset( p_socket, 0, sizeof( struct sockaddr_in ) );
    p_socket->sin_family = AF_INET;                                 /* family */
    p_socket->sin_port = htons( 0 );                /* This is for remote end */
    
     /* Try to convert address directly from in_addr - this will work if
      * psz_in_addr is dotted decimal. */
Michel Kaempf's avatar
Michel Kaempf committed
129

Henri Fallon's avatar
   
Henri Fallon committed
130
131
#ifdef HAVE_ARPA_INET_H
    if( !inet_aton( psz_server, &p_socket->sin_addr) )
Benoit Steiner's avatar
Benoit Steiner committed
132
#else
Henri Fallon's avatar
   
Henri Fallon committed
133
    if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
134
#endif
Michel Kaempf's avatar
Michel Kaempf committed
135
    {
Henri Fallon's avatar
   
Henri Fallon committed
136
137
        /* We have a fqdn, try to find its address */
        if ( (p_hostent = gethostbyname(psz_server)) == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
138
        {
Henri Fallon's avatar
   
Henri Fallon committed
139
140
141
            intf_ErrMsg( "BuildRemoteAddr: unknown host %s", 
                         psz_server );
            return( -1 );
Michel Kaempf's avatar
Michel Kaempf committed
142
        }
Henri Fallon's avatar
   
Henri Fallon committed
143
144
145
146
        
        /* Copy the first address of the host in the socket address */
        memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0], 
                 p_hostent->h_length );
Michel Kaempf's avatar
Michel Kaempf committed
147
    }
Henri Fallon's avatar
   
Henri Fallon committed
148
    return( 0 );
Michel Kaempf's avatar
Michel Kaempf committed
149
}
Christophe Massiot's avatar
Christophe Massiot committed
150
#endif
Sam Hocevar's avatar
   
Sam Hocevar committed
151