summaryrefslogtreecommitdiffstats
path: root/stream/tcp.c
blob: e34efbd41274029b1062ed53864094623b44cfcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 *  Copyright (C) 2001 Bertrand BAUDET, 2006 Benjamin Zores
 *   Network helpers for TCP connections
 *   (originally borrowed from network.c,
 *      by Bertrand BAUDET <bertrand_baudet@yahoo.com>).
 *
 *   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.
 *
 *   This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *   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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <ctype.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>

#ifndef HAVE_WINSOCK2
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define closesocket close
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

#include "mp_msg.h"
#include "help_mp.h"
#include "tcp.h"

/* IPv6 options */
int network_prefer_ipv4 = 0;

/* Converts an address family constant to a string */
static const char *
af2String (int af)
{
  switch (af)
  {
  case AF_INET:
    return "AF_INET";
#ifdef HAVE_AF_INET6
  case AF_INET6:
    return "AF_INET6";
#endif
  default:
    return "Unknown address family!";
  }
}

static int
connect2Server_with_af (char *host, int port, int af ,int verb)
{
  int socket_server_fd;
  int err;
  socklen_t err_len;
  int ret, count = 0;
  fd_set set;
  struct timeval tv;

  union {
    struct sockaddr_in four;
#ifdef HAVE_AF_INET6
    struct sockaddr_in6 six;
#endif
  } server_address;

  size_t server_address_size;
  void *our_s_addr; /* Pointer to sin_addr or sin6_addr */
  struct hostent *hp = NULL;
  char buf[255];
	
#ifdef HAVE_WINSOCK2
  u_long val;
#endif
	
  socket_server_fd = socket (af, SOCK_STREAM, 0);
  if (socket_server_fd == -1)
    return TCP_ERROR_FATAL;

  switch (af)
  {
  case AF_INET:
    our_s_addr = (void *) &server_address.four.sin_addr;
    break;
#ifdef HAVE_AF_INET6
  case AF_INET6:
    our_s_addr = (void *) &server_address.six.sin6_addr;
    break;
#endif
  default:
    mp_msg (MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
    return TCP_ERROR_FATAL;
  }
	
  memset (&server_address, 0, sizeof (server_address));
	
#ifndef HAVE_WINSOCK2
#ifdef USE_ATON
  if (inet_aton (host, our_s_addr) !=1)
#else
  if (inet_pton (af, host, our_s_addr) !=1)
#endif /* USE_ATON */
#else
  if (inet_addr (host) == INADDR_NONE)
#endif /* HAVE_WINSOCK2 */
  {
    if (verb)
      mp_msg (MSGT_NETWORK, MSGL_STATUS,
              MSGTR_MPDEMUX_NW_ResolvingHostForAF, host, af2String (af));
		
#ifdef HAVE_GETHOSTBYNAME2
    hp = (struct hostent *) gethostbyname2 (host, af);
#else
    hp = (struct hostent *) gethostbyname (host);
#endif /* HAVE_GETHOSTBYNAME2 */

    if (!hp)
    {
      if (verb)
        mp_msg (MSGT_NETWORK, MSGL_ERR,
                MSGTR_MPDEMUX_NW_CantResolv, af2String (af), host);
      return TCP_ERROR_FATAL;
    }
		
    memcpy (our_s_addr, (void *) hp->h_addr_list[0], hp->h_length);
  }
#ifdef HAVE_WINSOCK2
  else
  {
    unsigned long addr = inet_addr (host);
    memcpy (our_s_addr, (void *) &addr, sizeof (addr));
  }
#endif /* HAVE_WINSOCK2 */
	
  switch (af)
  {
  case AF_INET:
    server_address.four.sin_family = af;
    server_address.four.sin_port = htons (port);			
    server_address_size = sizeof (server_address.four);
    break;
#ifdef HAVE_AF_INET6		
  case AF_INET6:
    server_address.six.sin6_family = af;
    server_address.six.sin6_port = htons (port);
    server_address_size = sizeof (server_address.six);
    break;
#endif /* HAVE_AF_INET6 */
  default:
    mp_msg (MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
    return TCP_ERROR_FATAL;
  }

#if defined(USE_ATON) || defined(HAVE_WINSOCK2)
  strncpy (buf, inet_ntoa (*((struct in_addr *) our_s_addr)), 255);
#else
  inet_ntop (af, our_s_addr, buf, 255);
#endif /* USE_ATON || HAVE_WINSOCK2 */
  
  if (verb)
    mp_msg (MSGT_NETWORK, MSGL_STATUS,
            MSGTR_MPDEMUX_NW_ConnectingToServer, host, buf, port);

  /* Turn the socket as non blocking so we can timeout on the connection */
#ifndef HAVE_WINSOCK2
  fcntl (socket_server_fd, F_SETFL,
         fcntl (socket_server_fd, F_GETFL) | O_NONBLOCK);
#else
  val = 1;
  ioctlsocket (socket_server_fd, FIONBIO, &val);
#endif /* HAVE_WINSOCK2 */
  
  if (connect (socket_server_fd, (struct sockaddr *) &server_address,
               server_address_size) == -1)
  {
#ifndef HAVE_WINSOCK2
    if (errno != EINPROGRESS)
#else
    if ((WSAGetLastError () != WSAEINPROGRESS)
        && (WSAGetLastError () != WSAEWOULDBLOCK))
#endif /* HAVE_WINSOCK2 */
    {
      if (verb)
        mp_msg (MSGT_NETWORK, MSGL_ERR,
                MSGTR_MPDEMUX_NW_CantConnect2Server, af2String (af));

      closesocket (socket_server_fd);
      return TCP_ERROR_PORT;
    }
  }

  tv.tv_sec = 0;
  tv.tv_usec = 500000;

  FD_ZERO (&set);
  FD_SET (socket_server_fd, &set);

  /* When the connection will be made, we will have a writable fd */
  while ((ret = select (socket_server_fd + 1, NULL, &set, NULL, &tv)) == 0)
  {
    if (ret < 0)
      mp_msg (MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_SelectFailed);
    else if (ret > 0)
      break;
    else if (count > 30 || mp_input_check_interrupt (500))
    {
      if (count > 30)
        mp_msg (MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_ConnTimeout);
      else
        mp_msg (MSGT_NETWORK, MSGL_V, "Connection interuppted by user\n");
      return TCP_ERROR_TIMEOUT;
    }

    count++;
    FD_ZERO (&set);
    FD_SET (socket_server_fd, &set);
    tv.tv_sec = 0;
    tv.tv_usec = 500000;
  }

  /* Turn back the socket as blocking */
#ifndef HAVE_WINSOCK2
  fcntl (socket_server_fd, F_SETFL,
         fcntl (socket_server_fd, F_GETFL) & ~O_NONBLOCK);
#else
  val = 0;
  ioctlsocket (socket_server_fd, FIONBIO, &val);
#endif /* HAVE_WINSOCK2 */
  
  /* Check if there were any error */
  err_len = sizeof (int);
  ret = getsockopt (socket_server_fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
  if (ret < 0)
  {
    mp_msg (MSGT_NETWORK, MSGL_ERR,
            MSGTR_MPDEMUX_NW_GetSockOptFailed, strerror (errno));
    return TCP_ERROR_FATAL;
  }

  if (err > 0)
  {
    mp_msg (MSGT_NETWORK, MSGL_ERR,
            MSGTR_MPDEMUX_NW_ConnectError, strerror (err));
    return TCP_ERROR_PORT;
  }
	
  return socket_server_fd;
}

int
connect2Server (char *host, int port, int verb)
{
#ifdef HAVE_AF_INET6
  int r;
  int s = TCP_ERROR_FATAL;

  r = connect2Server_with_af (host, port,
                              network_prefer_ipv4 ? AF_INET:AF_INET6, verb);
  if (r > TCP_ERROR_PORT)
    return r;

  s = connect2Server_with_af (host, port,
                              network_prefer_ipv4 ? AF_INET6:AF_INET, verb);
  if (s == TCP_ERROR_FATAL)
    return r;

  return s;
#else	
  return connect2Server_with_af (host, port, AF_INET,verb);
#endif /* HAVE_AF_INET6 */
}