annotate stream/tcp.c @ 22113:50c9dc00154d

Add timeout to tcp connections, avoid hanging forever. Based on patch for bugzilla #673
author rtogni
date Sun, 04 Feb 2007 14:18:56 +0000
parents e053647fbeec
children 9bc7a6022cde
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"
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
22
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
23 #ifndef HAVE_WINSOCK2
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
24 #include <netdb.h>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
25 #include <netinet/in.h>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
26 #include <sys/socket.h>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
27 #include <arpa/inet.h>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
28 #define closesocket close
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
29 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
30 #include <winsock2.h>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
31 #include <ws2tcpip.h>
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
32 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
33
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
34 #include "tcp.h"
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
35
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
36 /* IPv6 options */
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
37 int network_prefer_ipv4 = 0;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
38
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
39 // Converts an address family constant to a string
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 const char *af2String(int af) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
42 switch (af) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
43 case AF_INET: return "AF_INET";
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
44
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
45 #ifdef HAVE_AF_INET6
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
46 case AF_INET6: return "AF_INET6";
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
47 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
48 default: return "Unknown address family!";
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
49 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
50 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
51
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
52
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
53
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
54 // 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
55 // 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
56 // return -1 is unable to connect to a particular port
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
57
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
58 int
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
59 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
60 int socket_server_fd;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
61 int err;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
62 socklen_t err_len;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
63 int ret,count = 0;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
64 fd_set set;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
65 struct timeval tv;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
66 union {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
67 struct sockaddr_in four;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
68 #ifdef HAVE_AF_INET6
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
69 struct sockaddr_in6 six;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
70 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
71 } server_address;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
72 size_t server_address_size;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
73 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
74 struct hostent *hp=NULL;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
75 char buf[255];
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 #ifdef HAVE_WINSOCK2
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
78 u_long val;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
79 #endif
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 socket_server_fd = socket(af, SOCK_STREAM, 0);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
82
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
83
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
84 if( socket_server_fd==-1 ) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
85 // 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
86 return TCP_ERROR_FATAL;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
87 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
88
22113
50c9dc00154d Add timeout to tcp connections, avoid hanging forever.
rtogni
parents: 19543
diff changeset
89 #if defined SO_RCVTIMEO && defined SO_SNDTIMEO
50c9dc00154d Add timeout to tcp connections, avoid hanging forever.
rtogni
parents: 19543
diff changeset
90 tv.tv_sec = 10;
50c9dc00154d Add timeout to tcp connections, avoid hanging forever.
rtogni
parents: 19543
diff changeset
91 tv.tv_usec = 0;
50c9dc00154d Add timeout to tcp connections, avoid hanging forever.
rtogni
parents: 19543
diff changeset
92 setsockopt(socket_server_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
50c9dc00154d Add timeout to tcp connections, avoid hanging forever.
rtogni
parents: 19543
diff changeset
93 setsockopt(socket_server_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
50c9dc00154d Add timeout to tcp connections, avoid hanging forever.
rtogni
parents: 19543
diff changeset
94 #endif
50c9dc00154d Add timeout to tcp connections, avoid hanging forever.
rtogni
parents: 19543
diff changeset
95
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
96 switch (af) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
97 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
98 #ifdef HAVE_AF_INET6
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
99 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
100 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
101 default:
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
102 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
103 return TCP_ERROR_FATAL;
19542
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
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
106
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
107 memset(&server_address, 0, sizeof(server_address));
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
108
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
109 #ifndef HAVE_WINSOCK2
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
110 #ifdef USE_ATON
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
111 if (inet_aton(host, our_s_addr)!=1)
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
112 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
113 if (inet_pton(af, host, our_s_addr)!=1)
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
114 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
115 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
116 if ( inet_addr(host)==INADDR_NONE )
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
117 #endif
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 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
120
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
121 #ifdef HAVE_GETHOSTBYNAME2
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
122 hp=(struct hostent*)gethostbyname2( host, af );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
123 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
124 hp=(struct hostent*)gethostbyname( host );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
125 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
126 if( hp==NULL ) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
127 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
128 return TCP_ERROR_FATAL;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
129 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
130
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
131 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
132 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
133 #ifdef HAVE_WINSOCK2
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
134 else {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
135 unsigned long addr = inet_addr(host);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
136 memcpy( our_s_addr, (void*)&addr, sizeof(addr) );
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 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
139
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
140 switch (af) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
141 case AF_INET:
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
142 server_address.four.sin_family=af;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
143 server_address.four.sin_port=htons(port);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
144 server_address_size = sizeof(server_address.four);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
145 break;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
146 #ifdef HAVE_AF_INET6
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
147 case AF_INET6:
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
148 server_address.six.sin6_family=af;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
149 server_address.six.sin6_port=htons(port);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
150 server_address_size = sizeof(server_address.six);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
151 break;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
152 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
153 default:
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
154 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
155 return TCP_ERROR_FATAL;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
156 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
157
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
158 #if defined(USE_ATON) || defined(HAVE_WINSOCK2)
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
159 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
160 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
161 inet_ntop(af, our_s_addr, buf, 255);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
162 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
163 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
164
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
165 // Turn the socket as non blocking so we can timeout on the connection
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
166 #ifndef HAVE_WINSOCK2
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
167 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
168 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
169 val = 1;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
170 ioctlsocket( socket_server_fd, FIONBIO, &val );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
171 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
172 if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
173 #ifndef HAVE_WINSOCK2
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
174 if( errno!=EINPROGRESS ) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
175 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
176 if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
177 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
178 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
179 closesocket(socket_server_fd);
19543
e053647fbeec Cosmetics: recommit patch changing return values to defines
reimar
parents: 19542
diff changeset
180 return TCP_ERROR_PORT;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
181 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
182 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
183 tv.tv_sec = 0;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
184 tv.tv_usec = 500000;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
185 FD_ZERO( &set );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
186 FD_SET( socket_server_fd, &set );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
187 // When the connection will be made, we will have a writable fd
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
188 while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
189 if( ret<0 ) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_SelectFailed);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
190 else if(ret > 0) break;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
191 else if(count > 30 || mp_input_check_interrupt(500)) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
192 if(count > 30)
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
193 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnTimeout);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
194 else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
195 mp_msg(MSGT_NETWORK,MSGL_V,"Connection interuppted by user\n");
19543
e053647fbeec Cosmetics: recommit patch changing return values to defines
reimar
parents: 19542
diff changeset
196 return TCP_ERROR_TIMEOUT;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
197 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
198 count++;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
199 FD_ZERO( &set );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
200 FD_SET( socket_server_fd, &set );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
201 tv.tv_sec = 0;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
202 tv.tv_usec = 500000;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
203 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
204
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
205 // Turn back the socket as blocking
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
206 #ifndef HAVE_WINSOCK2
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
207 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
208 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
209 val = 0;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
210 ioctlsocket( socket_server_fd, FIONBIO, &val );
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
211 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
212 // Check if there were any error
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
213 err_len = sizeof(int);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
214 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
215 if(ret < 0) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
216 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
217 return TCP_ERROR_FATAL;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
218 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
219 if(err > 0) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
220 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
221 return TCP_ERROR_PORT;
19542
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 return socket_server_fd;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
225 }
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
226
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
227 // Connect to a server using a TCP connection
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
228 // 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
229 // return -1 is unable to connect to a particular port
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
230
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
231
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
232 int
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
233 connect2Server(char *host, int port, int verb) {
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
234 #ifdef HAVE_AF_INET6
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
235 int r;
19543
e053647fbeec Cosmetics: recommit patch changing return values to defines
reimar
parents: 19542
diff changeset
236 int s = TCP_ERROR_FATAL;
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 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
239 if (r >= 0) return r;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
240
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
241 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
242 if (s == TCP_ERROR_FATAL) return r;
19542
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
243 return s;
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
244 #else
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
245 return connect2Server_with_af(host, port, AF_INET,verb);
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
246 #endif
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
247
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
248
57980fe3ad96 Recreate tcp.c as partial copy from network.c
reimar
parents:
diff changeset
249 }