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