annotate stream/tcp.c @ 27464:be85aae103f2

Move duplicated '#define closesocket close' into network.h along with network-related #include #ifdeffery.
author diego
date Fri, 29 Aug 2008 22:55:39 +0000
parents 5a30f5bc23a0
children c0b233cd30ca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
1 /*
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
2 * Network layer for MPlayer
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
3 * by Bertrand BAUDET <bertrand_baudet@yahoo.com>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
4 * (C) 2001, MPlayer team.
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
5 */
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
6
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
7 #include <stdlib.h>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
8 #include <string.h>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
9 #include <unistd.h>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
10
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
11 #include <errno.h>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
12 #include <ctype.h>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
13
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
14 #include <fcntl.h>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
15 #include <sys/time.h>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
16 #include <sys/types.h>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
17
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
18 #include "config.h"
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
19
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
20 #include "mp_msg.h"
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
21 #include "help_mp.h"
27464
be85aae103f2 Move duplicated '#define closesocket close' into network.h along with
diego
parents: 27461
diff changeset
22 #include "network.h"
26751
ba4a01eb4475 Add missing stream.h #include, fixes the warning:
diego
parents: 26326
diff changeset
23 #include "stream.h"
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
24 #include "tcp.h"
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
25
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
26 /* IPv6 options */
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
27 int network_prefer_ipv4 = 0;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
28
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
29 // Converts an address family constant to a string
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
30
24841
44540930bf94 Make functions static if they aren't referenced externally.
zuxy
parents: 24559
diff changeset
31 static const char *af2String(int af) {
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
32 switch (af) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
33 case AF_INET: return "AF_INET";
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
34
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
35 #ifdef HAVE_AF_INET6
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
36 case AF_INET6: return "AF_INET6";
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
37 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
38 default: return "Unknown address family!";
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
39 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
40 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
41
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
42
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
43
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
44 // Connect to a server using a TCP connection, with specified address family
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
45 // return -2 for fatal error, like unable to resolve name, connection timeout...
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
46 // return -1 is unable to connect to a particular port
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
47
24841
44540930bf94 Make functions static if they aren't referenced externally.
zuxy
parents: 24559
diff changeset
48 static int
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
49 connect2Server_with_af(char *host, int port, int af,int verb) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
50 int socket_server_fd;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
51 int err;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
52 socklen_t err_len;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
53 int ret,count = 0;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
54 fd_set set;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
55 struct timeval tv;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
56 union {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
57 struct sockaddr_in four;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
58 #ifdef HAVE_AF_INET6
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
59 struct sockaddr_in6 six;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
60 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
61 } server_address;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
62 size_t server_address_size;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
63 void *our_s_addr; // Pointer to sin_addr or sin6_addr
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
64 struct hostent *hp=NULL;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
65 char buf[255];
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
66
27461
5a30f5bc23a0 Rename HAVE_WINSOCK preprocessor condition to HAVE_WINSOCK_H.
diego
parents: 27359
diff changeset
67 #ifdef HAVE_WINSOCK2_H
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
68 u_long val;
22379
6b245d6e56a9 winsocks expects an int in milliseconds instead of struct timeval to set
ivo
parents: 22142
diff changeset
69 int to;
6b245d6e56a9 winsocks expects an int in milliseconds instead of struct timeval to set
ivo
parents: 22142
diff changeset
70 #else
6b245d6e56a9 winsocks expects an int in milliseconds instead of struct timeval to set
ivo
parents: 22142
diff changeset
71 struct timeval to;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
72 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
73
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
74 socket_server_fd = socket(af, SOCK_STREAM, 0);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
75
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
76
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
77 if( socket_server_fd==-1 ) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
78 // mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af));
19543
e053647fbeec Cosmetics: recommit patch changing return values to defines
reimar
parents: 19542
diff changeset
79 return TCP_ERROR_FATAL;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
80 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
81
22114
9bc7a6022cde Use defined() syntax instead without ().
reimar
parents: 22113
diff changeset
82 #if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
27461
5a30f5bc23a0 Rename HAVE_WINSOCK preprocessor condition to HAVE_WINSOCK_H.
diego
parents: 27359
diff changeset
83 #ifdef HAVE_WINSOCK2_H
22379
6b245d6e56a9 winsocks expects an int in milliseconds instead of struct timeval to set
ivo
parents: 22142
diff changeset
84 /* timeout in milliseconds */
6b245d6e56a9 winsocks expects an int in milliseconds instead of struct timeval to set
ivo
parents: 22142
diff changeset
85 to = 10 * 1000;
6b245d6e56a9 winsocks expects an int in milliseconds instead of struct timeval to set
ivo
parents: 22142
diff changeset
86 #else
6b245d6e56a9 winsocks expects an int in milliseconds instead of struct timeval to set
ivo
parents: 22142
diff changeset
87 to.tv_sec = 10;
6b245d6e56a9 winsocks expects an int in milliseconds instead of struct timeval to set
ivo
parents: 22142
diff changeset
88 to.tv_usec = 0;
6b245d6e56a9 winsocks expects an int in milliseconds instead of struct timeval to set
ivo
parents: 22142
diff changeset
89 #endif
6b245d6e56a9 winsocks expects an int in milliseconds instead of struct timeval to set
ivo
parents: 22142
diff changeset
90 setsockopt(socket_server_fd, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to));
6b245d6e56a9 winsocks expects an int in milliseconds instead of struct timeval to set
ivo
parents: 22142
diff changeset
91 setsockopt(socket_server_fd, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof(to));
22113
50c9dc00154d Add timeout to tcp connections, avoid hanging forever.
rtogni
parents: 19543
diff changeset
92 #endif
50c9dc00154d Add timeout to tcp connections, avoid hanging forever.
rtogni
parents: 19543
diff changeset
93
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
94 switch (af) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
95 case AF_INET: our_s_addr = (void *) &server_address.four.sin_addr; break;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
96 #ifdef HAVE_AF_INET6
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
97 case AF_INET6: our_s_addr = (void *) &server_address.six.sin6_addr; break;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
98 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
99 default:
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
100 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
19543
e053647fbeec Cosmetics: recommit patch changing return values to defines
reimar
parents: 19542
diff changeset
101 return TCP_ERROR_FATAL;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
102 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
103
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
104
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
105 memset(&server_address, 0, sizeof(server_address));
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
106
27461
5a30f5bc23a0 Rename HAVE_WINSOCK preprocessor condition to HAVE_WINSOCK_H.
diego
parents: 27359
diff changeset
107 #ifndef HAVE_WINSOCK2_H
27359
d788e177a35e Rename some preprocessor directives from CONFIG_* to HAVE_* where appropriate;
diego
parents: 27341
diff changeset
108 #ifdef HAVE_ATON
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
109 if (inet_aton(host, our_s_addr)!=1)
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
110 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
111 if (inet_pton(af, host, our_s_addr)!=1)
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
112 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
113 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
114 if ( inet_addr(host)==INADDR_NONE )
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
115 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
116 {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
117 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ResolvingHostForAF, host, af2String(af));
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
118
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
119 #ifdef HAVE_GETHOSTBYNAME2
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
120 hp=(struct hostent*)gethostbyname2( host, af );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
121 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
122 hp=(struct hostent*)gethostbyname( host );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
123 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
124 if( hp==NULL ) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
125 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantResolv, af2String(af), host);
19543
e053647fbeec Cosmetics: recommit patch changing return values to defines
reimar
parents: 19542
diff changeset
126 return TCP_ERROR_FATAL;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
127 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
128
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
129 memcpy( our_s_addr, (void*)hp->h_addr_list[0], hp->h_length );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
130 }
27461
5a30f5bc23a0 Rename HAVE_WINSOCK preprocessor condition to HAVE_WINSOCK_H.
diego
parents: 27359
diff changeset
131 #ifdef HAVE_WINSOCK2_H
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
132 else {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
133 unsigned long addr = inet_addr(host);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
134 memcpy( our_s_addr, (void*)&addr, sizeof(addr) );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
135 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
136 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
137
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
138 switch (af) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
139 case AF_INET:
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
140 server_address.four.sin_family=af;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
141 server_address.four.sin_port=htons(port);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
142 server_address_size = sizeof(server_address.four);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
143 break;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
144 #ifdef HAVE_AF_INET6
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
145 case AF_INET6:
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
146 server_address.six.sin6_family=af;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
147 server_address.six.sin6_port=htons(port);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
148 server_address_size = sizeof(server_address.six);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
149 break;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
150 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
151 default:
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
152 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
19543
e053647fbeec Cosmetics: recommit patch changing return values to defines
reimar
parents: 19542
diff changeset
153 return TCP_ERROR_FATAL;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
154 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
155
27461
5a30f5bc23a0 Rename HAVE_WINSOCK preprocessor condition to HAVE_WINSOCK_H.
diego
parents: 27359
diff changeset
156 #if defined(HAVE_ATON) || defined(HAVE_WINSOCK2_H)
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
157 strncpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
158 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
159 inet_ntop(af, our_s_addr, buf, 255);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
160 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
161 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ConnectingToServer, host, buf , port );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
162
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
163 // Turn the socket as non blocking so we can timeout on the connection
27461
5a30f5bc23a0 Rename HAVE_WINSOCK preprocessor condition to HAVE_WINSOCK_H.
diego
parents: 27359
diff changeset
164 #ifndef HAVE_WINSOCK2_H
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
165 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
166 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
167 val = 1;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
168 ioctlsocket( socket_server_fd, FIONBIO, &val );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
169 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
170 if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) {
27461
5a30f5bc23a0 Rename HAVE_WINSOCK preprocessor condition to HAVE_WINSOCK_H.
diego
parents: 27359
diff changeset
171 #ifndef HAVE_WINSOCK2_H
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
172 if( errno!=EINPROGRESS ) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
173 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
174 if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
175 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
176 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantConnect2Server, af2String(af));
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
177 closesocket(socket_server_fd);
19543
e053647fbeec Cosmetics: recommit patch changing return values to defines
reimar
parents: 19542
diff changeset
178 return TCP_ERROR_PORT;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
179 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
180 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
181 tv.tv_sec = 0;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
182 tv.tv_usec = 500000;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
183 FD_ZERO( &set );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
184 FD_SET( socket_server_fd, &set );
24558
97eaba07ef9d Fix a few typos
reimar
parents: 22379
diff changeset
185 // When the connection will be made, we will have a writeable fd
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
186 while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) {
26326
5bfc1d8bece9 Remove the need for code using stream to export an mp_input_check_interrupt()
albeu
parents: 24841
diff changeset
187 if(count > 30 || stream_check_interrupt(500)) {
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
188 if(count > 30)
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
189 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnTimeout);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
190 else
24558
97eaba07ef9d Fix a few typos
reimar
parents: 22379
diff changeset
191 mp_msg(MSGT_NETWORK,MSGL_V,"Connection interrupted by user\n");
19543
e053647fbeec Cosmetics: recommit patch changing return values to defines
reimar
parents: 19542
diff changeset
192 return TCP_ERROR_TIMEOUT;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
193 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
194 count++;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
195 FD_ZERO( &set );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
196 FD_SET( socket_server_fd, &set );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
197 tv.tv_sec = 0;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
198 tv.tv_usec = 500000;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
199 }
24559
7b6a3948f5f2 (Re)move idiotic checks, ret can't be < 0 or > 0 if the loop condition
reimar
parents: 24558
diff changeset
200 if (ret < 0) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_SelectFailed);
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
201
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
202 // Turn back the socket as blocking
27461
5a30f5bc23a0 Rename HAVE_WINSOCK preprocessor condition to HAVE_WINSOCK_H.
diego
parents: 27359
diff changeset
203 #ifndef HAVE_WINSOCK2_H
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
204 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
205 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
206 val = 0;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
207 ioctlsocket( socket_server_fd, FIONBIO, &val );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
208 #endif
24558
97eaba07ef9d Fix a few typos
reimar
parents: 22379
diff changeset
209 // Check if there were any errors
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
210 err_len = sizeof(int);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
211 ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
212 if(ret < 0) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
213 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_GetSockOptFailed,strerror(errno));
19543
e053647fbeec Cosmetics: recommit patch changing return values to defines
reimar
parents: 19542
diff changeset
214 return TCP_ERROR_FATAL;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
215 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
216 if(err > 0) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
217 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnectError,strerror(err));
19543
e053647fbeec Cosmetics: recommit patch changing return values to defines
reimar
parents: 19542
diff changeset
218 return TCP_ERROR_PORT;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
219 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
220
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
221 return socket_server_fd;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
222 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
223
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
224 // Connect to a server using a TCP connection
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
225 // return -2 for fatal error, like unable to resolve name, connection timeout...
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
226 // return -1 is unable to connect to a particular port
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
227
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
228
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
229 int
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
230 connect2Server(char *host, int port, int verb) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
231 #ifdef HAVE_AF_INET6
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
232 int r;
19543
e053647fbeec Cosmetics: recommit patch changing return values to defines
reimar
parents: 19542
diff changeset
233 int s = TCP_ERROR_FATAL;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
234
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
235 r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb);
19543
e053647fbeec Cosmetics: recommit patch changing return values to defines
reimar
parents: 19542
diff changeset
236 if (r >= 0) return r;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
237
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
238 s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb);
19543
e053647fbeec Cosmetics: recommit patch changing return values to defines
reimar
parents: 19542
diff changeset
239 if (s == TCP_ERROR_FATAL) return r;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
240 return s;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
241 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
242 return connect2Server_with_af(host, port, AF_INET,verb);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
243 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
244
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
245
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
246 }