netutils.c 5.52 KB
Newer Older
1
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
2
 * netutils.c: various network functions
3
 *****************************************************************************
4
5
 * Copyright (C) 1999, 2000 VideoLAN
 *
6
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
Benoit Steiner's avatar
   
Benoit Steiner committed
7
 *          Benoit Steiner <benny@via.ecp.fr>
Henri Fallon's avatar
   
Henri Fallon committed
8
 *          Henri Fallon <henri@videolan.org>
9
10
11
12
13
 *
 * 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.
14
 * 
15
16
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
19
 *
20
21
22
 * 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.
23
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
24

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

30
31
32
33
#include <netdb.h>                                        /* gethostbyname() */
#include <stdlib.h>                             /* free(), realloc(), atoi() */
#include <errno.h>                                                /* errno() */
#include <string.h>                                      /* bzero(), bcopy() */
34

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

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

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

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

#include "intf_msg.h"

#include "netutils.h"

Henri Fallon's avatar
   
Henri Fallon committed
58
59

#ifndef SYS_BEOS /* I need help for the BeOS portage */
60
/*****************************************************************************
Henri Fallon's avatar
   
Henri Fallon committed
61
 * input_BuildLocalAddr : fill a sockaddr_in structure for local binding
62
 *****************************************************************************/
Henri Fallon's avatar
   
Henri Fallon committed
63
64
int input_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port, 
                          boolean_t b_broadcast )
Michel Kaempf's avatar
Michel Kaempf committed
65
{
Henri Fallon's avatar
   
Henri Fallon committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    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
82

Henri Fallon's avatar
   
Henri Fallon committed
83
84
    }
    else
85
    {
Henri Fallon's avatar
   
Henri Fallon committed
86
87
88
        /* 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));
89
    }
Henri Fallon's avatar
   
Henri Fallon committed
90

91
92
    /* 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
93
94
#ifdef HAVE_ARPA_INET_H
    if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
95
#else
Henri Fallon's avatar
   
Henri Fallon committed
96
    if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
97
#endif
Michel Kaempf's avatar
Michel Kaempf committed
98
    {
Henri Fallon's avatar
   
Henri Fallon committed
99
100
        /* We have a fqdn, try to find its address */
        if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
101
        {
Henri Fallon's avatar
   
Henri Fallon committed
102
            intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
103
            return( -1 );
Michel Kaempf's avatar
Michel Kaempf committed
104
        }
Henri Fallon's avatar
   
Henri Fallon committed
105
        
Michel Kaempf's avatar
Michel Kaempf committed
106
        /* Copy the first address of the host in the socket address */
Henri Fallon's avatar
   
Henri Fallon committed
107
108
        memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0], 
                 p_hostent->h_length );
Michel Kaempf's avatar
Michel Kaempf committed
109
    }
110
    return( 0 );
Michel Kaempf's avatar
Michel Kaempf committed
111
112
}

113
/*****************************************************************************
Henri Fallon's avatar
   
Henri Fallon committed
114
 * input_BuildRemoteAddr : fill a sockaddr_in structure for remote host
115
 *****************************************************************************/
Henri Fallon's avatar
   
Henri Fallon committed
116
int input_BuildRemoteAddr( struct sockaddr_in * p_socket, char * psz_server )
Michel Kaempf's avatar
Michel Kaempf committed
117
{
Henri Fallon's avatar
   
Henri Fallon committed
118
119
120
121
122
123
124
125
126
    struct hostent            * p_hostent;
printf("BuildRemoteAddr : psz_server = %s\n",psz_server );    
    /* 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
127

Henri Fallon's avatar
   
Henri Fallon committed
128
129
#ifdef HAVE_ARPA_INET_H
    if( !inet_aton( psz_server, &p_socket->sin_addr) )
Benoit Steiner's avatar
Benoit Steiner committed
130
#else
Henri Fallon's avatar
   
Henri Fallon committed
131
    if( (p_socket->sin_addr.s_addr = inet_addr( psz_server )) == -1 )
132
#endif
Michel Kaempf's avatar
Michel Kaempf committed
133
    {
Henri Fallon's avatar
   
Henri Fallon committed
134
135
        /* We have a fqdn, try to find its address */
        if ( (p_hostent = gethostbyname(psz_server)) == NULL )
Michel Kaempf's avatar
Michel Kaempf committed
136
        {
Henri Fallon's avatar
   
Henri Fallon committed
137
138
139
            intf_ErrMsg( "BuildRemoteAddr: unknown host %s", 
                         psz_server );
            return( -1 );
Michel Kaempf's avatar
Michel Kaempf committed
140
        }
Henri Fallon's avatar
   
Henri Fallon committed
141
142
143
144
        
        /* 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
145
    }
Henri Fallon's avatar
   
Henri Fallon committed
146
    return( 0 );
Michel Kaempf's avatar
Michel Kaempf committed
147
}
Christophe Massiot's avatar
Christophe Massiot committed
148
#endif