configure: drop unused liblzo and bzlib tests
[mplayer/greg.git] / stream / tcp.c
blob1f93571b55f8101baad25f11712c79972c70b35a
1 /*
2 * Network layer for MPlayer
4 * Copyright (C) 2001 Bertrand BAUDET <bertrand_baudet@yahoo.com>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include <errno.h>
28 #include <ctype.h>
30 #include <fcntl.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
34 #include "config.h"
36 #include "mp_msg.h"
38 #if !HAVE_WINSOCK2_H
39 #include <netdb.h>
40 #include <netinet/in.h>
41 #include <sys/socket.h>
42 #include <arpa/inet.h>
43 #else
44 #include <winsock2.h>
45 #include <ws2tcpip.h>
46 #endif
48 #include "network.h"
49 #include "stream.h"
50 #include "tcp.h"
51 #include "libavutil/avstring.h"
53 /* IPv6 options */
54 int network_prefer_ipv4 = 0;
56 // Converts an address family constant to a string
58 static const char *af2String(int af) {
59 switch (af) {
60 case AF_INET: return "AF_INET";
62 #ifdef HAVE_AF_INET6
63 case AF_INET6: return "AF_INET6";
64 #endif
65 default: return "Unknown address family!";
71 // Connect to a server using a TCP connection, with specified address family
72 // return -2 for fatal error, like unable to resolve name, connection timeout...
73 // return -1 is unable to connect to a particular port
75 static int
76 connect2Server_with_af(char *host, int port, int af,int verb) {
77 int socket_server_fd;
78 int err;
79 socklen_t err_len;
80 int ret,count = 0;
81 fd_set set;
82 struct timeval tv;
83 union {
84 struct sockaddr_in four;
85 #ifdef HAVE_AF_INET6
86 struct sockaddr_in6 six;
87 #endif
88 } server_address;
89 size_t server_address_size;
90 void *our_s_addr; // Pointer to sin_addr or sin6_addr
91 struct hostent *hp=NULL;
92 char buf[255];
94 #if HAVE_WINSOCK2_H
95 unsigned long val;
96 int to;
97 #else
98 struct timeval to;
99 #endif
101 #if HAVE_WINSOCK2_H && defined(HAVE_AF_INET6)
102 // our winsock name resolution code can not handle IPv6
103 if (af == AF_INET6) {
104 mp_msg(MSGT_NETWORK, MSGL_WARN, "IPv6 not supported for winsock2\n");
105 return TCP_ERROR_FATAL;
107 #endif
109 socket_server_fd = socket(af, SOCK_STREAM, 0);
112 if( socket_server_fd==-1 ) {
113 // mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af));
114 return TCP_ERROR_FATAL;
117 #if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
118 #if HAVE_WINSOCK2_H
119 /* timeout in milliseconds */
120 to = 10 * 1000;
121 #else
122 to.tv_sec = 10;
123 to.tv_usec = 0;
124 #endif
125 setsockopt(socket_server_fd, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to));
126 setsockopt(socket_server_fd, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof(to));
127 #endif
129 switch (af) {
130 case AF_INET: our_s_addr = (void *) &server_address.four.sin_addr; break;
131 #ifdef HAVE_AF_INET6
132 case AF_INET6: our_s_addr = (void *) &server_address.six.sin6_addr; break;
133 #endif
134 default:
135 mp_tmsg(MSGT_NETWORK,MSGL_ERR, "Unknown address family %d\n", af);
136 return TCP_ERROR_FATAL;
140 memset(&server_address, 0, sizeof(server_address));
142 #if HAVE_INET_PTON
143 if (inet_pton(af, host, our_s_addr)!=1)
144 #elif HAVE_INET_ATON
145 if (inet_aton(host, our_s_addr)!=1)
146 #elif HAVE_WINSOCK2_H
147 if ( inet_addr(host)==INADDR_NONE )
148 #endif
150 if(verb) mp_tmsg(MSGT_NETWORK,MSGL_STATUS,"Resolving %s for %s...\n", host, af2String(af));
152 #ifdef HAVE_GETHOSTBYNAME2
153 hp=(struct hostent*)gethostbyname2( host, af );
154 #else
155 hp=(struct hostent*)gethostbyname( host );
156 #endif
157 if( hp==NULL ) {
158 if(verb) mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Couldn't resolve name for %s: %s\n", af2String(af), host);
159 return TCP_ERROR_FATAL;
162 memcpy( our_s_addr, (void*)hp->h_addr_list[0], hp->h_length );
164 #if HAVE_WINSOCK2_H
165 else {
166 unsigned long addr = inet_addr(host);
167 memcpy( our_s_addr, (void*)&addr, sizeof(addr) );
169 #endif
171 switch (af) {
172 case AF_INET:
173 server_address.four.sin_family=af;
174 server_address.four.sin_port=htons(port);
175 server_address_size = sizeof(server_address.four);
176 break;
177 #ifdef HAVE_AF_INET6
178 case AF_INET6:
179 server_address.six.sin6_family=af;
180 server_address.six.sin6_port=htons(port);
181 server_address_size = sizeof(server_address.six);
182 break;
183 #endif
184 default:
185 mp_tmsg(MSGT_NETWORK,MSGL_ERR, "Unknown address family %d\n", af);
186 return TCP_ERROR_FATAL;
189 #if HAVE_INET_PTON
190 inet_ntop(af, our_s_addr, buf, 255);
191 #elif HAVE_INET_ATON || defined(HAVE_WINSOCK2_H)
192 av_strlcpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255);
193 #endif
194 if(verb) mp_tmsg(MSGT_NETWORK,MSGL_STATUS,"Connecting to server %s[%s]: %d...\n", host, buf , port );
196 // Turn the socket as non blocking so we can timeout on the connection
197 #if !HAVE_WINSOCK2_H
198 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK );
199 #else
200 val = 1;
201 ioctlsocket( socket_server_fd, FIONBIO, &val );
202 #endif
203 if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) {
204 #if !HAVE_WINSOCK2_H
205 if( errno!=EINPROGRESS ) {
206 #else
207 if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) {
208 #endif
209 if(verb) mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Failed to connect to server with %s\n", af2String(af));
210 closesocket(socket_server_fd);
211 return TCP_ERROR_PORT;
214 tv.tv_sec = 0;
215 tv.tv_usec = 500000;
216 FD_ZERO( &set );
217 FD_SET( socket_server_fd, &set );
218 // When the connection will be made, we will have a writeable fd
219 while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) {
220 if(count > 30 || stream_check_interrupt(500)) {
221 if(count > 30)
222 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"connection timeout\n");
223 else
224 mp_msg(MSGT_NETWORK,MSGL_V,"Connection interrupted by user\n");
225 return TCP_ERROR_TIMEOUT;
227 count++;
228 FD_ZERO( &set );
229 FD_SET( socket_server_fd, &set );
230 tv.tv_sec = 0;
231 tv.tv_usec = 500000;
233 if (ret < 0) mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Select failed.\n");
235 // Turn back the socket as blocking
236 #if !HAVE_WINSOCK2_H
237 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK );
238 #else
239 val = 0;
240 ioctlsocket( socket_server_fd, FIONBIO, &val );
241 #endif
242 // Check if there were any errors
243 err_len = sizeof(int);
244 ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len);
245 if(ret < 0) {
246 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"getsockopt failed: %s\n",strerror(errno));
247 return TCP_ERROR_FATAL;
249 if(err > 0) {
250 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"connect error: %s\n",strerror(err));
251 return TCP_ERROR_PORT;
254 return socket_server_fd;
257 // Connect to a server using a TCP connection
258 // return -2 for fatal error, like unable to resolve name, connection timeout...
259 // return -1 is unable to connect to a particular port
263 connect2Server(char *host, int port, int verb) {
264 #ifdef HAVE_AF_INET6
265 int r;
266 int s = TCP_ERROR_FATAL;
268 r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb);
269 if (r >= 0) return r;
271 s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb);
272 if (s == TCP_ERROR_FATAL) return r;
273 return s;
274 #else
275 return connect2Server_with_af(host, port, AF_INET,verb);
276 #endif