Raise LIBASS_VERSION, forgotten in r31293.
[mplayer/glamo.git] / stream / tcp.c
blob5dfaa8304f083bd52ece07c8017027eb6aaffb5e
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"
37 #include "help_mp.h"
39 #if !HAVE_WINSOCK2_H
40 #include <netdb.h>
41 #include <netinet/in.h>
42 #include <sys/socket.h>
43 #include <arpa/inet.h>
44 #else
45 #include <winsock2.h>
46 #include <ws2tcpip.h>
47 #endif
49 #include "network.h"
50 #include "stream.h"
51 #include "tcp.h"
52 #include "libavutil/avstring.h"
54 /* IPv6 options */
55 int network_prefer_ipv4 = 0;
57 // Converts an address family constant to a string
59 static const char *af2String(int af) {
60 switch (af) {
61 case AF_INET: return "AF_INET";
63 #ifdef HAVE_AF_INET6
64 case AF_INET6: return "AF_INET6";
65 #endif
66 default: return "Unknown address family!";
72 // Connect to a server using a TCP connection, with specified address family
73 // return -2 for fatal error, like unable to resolve name, connection timeout...
74 // return -1 is unable to connect to a particular port
76 static int
77 connect2Server_with_af(char *host, int port, int af,int verb) {
78 int socket_server_fd;
79 int err;
80 socklen_t err_len;
81 int ret,count = 0;
82 fd_set set;
83 struct timeval tv;
84 union {
85 struct sockaddr_in four;
86 #ifdef HAVE_AF_INET6
87 struct sockaddr_in6 six;
88 #endif
89 } server_address;
90 size_t server_address_size;
91 void *our_s_addr; // Pointer to sin_addr or sin6_addr
92 struct hostent *hp=NULL;
93 char buf[255];
95 #if HAVE_WINSOCK2_H
96 unsigned long val;
97 int to;
98 #else
99 struct timeval to;
100 #endif
102 #if HAVE_WINSOCK2_H && defined(HAVE_AF_INET6)
103 // our winsock name resolution code can not handle IPv6
104 if (af == AF_INET6) {
105 mp_msg(MSGT_NETWORK, MSGL_WARN, "IPv6 not supported for winsock2\n");
106 return TCP_ERROR_FATAL;
108 #endif
110 socket_server_fd = socket(af, SOCK_STREAM, 0);
113 if( socket_server_fd==-1 ) {
114 // mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af));
115 return TCP_ERROR_FATAL;
118 #if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
119 #if HAVE_WINSOCK2_H
120 /* timeout in milliseconds */
121 to = 10 * 1000;
122 #else
123 to.tv_sec = 10;
124 to.tv_usec = 0;
125 #endif
126 setsockopt(socket_server_fd, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to));
127 setsockopt(socket_server_fd, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof(to));
128 #endif
130 switch (af) {
131 case AF_INET: our_s_addr = (void *) &server_address.four.sin_addr; break;
132 #ifdef HAVE_AF_INET6
133 case AF_INET6: our_s_addr = (void *) &server_address.six.sin6_addr; break;
134 #endif
135 default:
136 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
137 return TCP_ERROR_FATAL;
141 memset(&server_address, 0, sizeof(server_address));
143 #if HAVE_INET_PTON
144 if (inet_pton(af, host, our_s_addr)!=1)
145 #elif HAVE_INET_ATON
146 if (inet_aton(host, our_s_addr)!=1)
147 #elif HAVE_WINSOCK2_H
148 if ( inet_addr(host)==INADDR_NONE )
149 #endif
151 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ResolvingHostForAF, host, af2String(af));
153 #ifdef HAVE_GETHOSTBYNAME2
154 hp=(struct hostent*)gethostbyname2( host, af );
155 #else
156 hp=(struct hostent*)gethostbyname( host );
157 #endif
158 if( hp==NULL ) {
159 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantResolv, af2String(af), host);
160 return TCP_ERROR_FATAL;
163 memcpy( our_s_addr, (void*)hp->h_addr_list[0], hp->h_length );
165 #if HAVE_WINSOCK2_H
166 else {
167 unsigned long addr = inet_addr(host);
168 memcpy( our_s_addr, (void*)&addr, sizeof(addr) );
170 #endif
172 switch (af) {
173 case AF_INET:
174 server_address.four.sin_family=af;
175 server_address.four.sin_port=htons(port);
176 server_address_size = sizeof(server_address.four);
177 break;
178 #ifdef HAVE_AF_INET6
179 case AF_INET6:
180 server_address.six.sin6_family=af;
181 server_address.six.sin6_port=htons(port);
182 server_address_size = sizeof(server_address.six);
183 break;
184 #endif
185 default:
186 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
187 return TCP_ERROR_FATAL;
190 #if HAVE_INET_ATON || defined(HAVE_WINSOCK2_H)
191 av_strlcpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255);
192 #else
193 inet_ntop(af, our_s_addr, buf, 255);
194 #endif
195 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ConnectingToServer, host, buf , port );
197 // Turn the socket as non blocking so we can timeout on the connection
198 #if !HAVE_WINSOCK2_H
199 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK );
200 #else
201 val = 1;
202 ioctlsocket( socket_server_fd, FIONBIO, &val );
203 #endif
204 if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) {
205 #if !HAVE_WINSOCK2_H
206 if( errno!=EINPROGRESS ) {
207 #else
208 if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) {
209 #endif
210 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantConnect2Server, af2String(af));
211 closesocket(socket_server_fd);
212 return TCP_ERROR_PORT;
215 tv.tv_sec = 0;
216 tv.tv_usec = 500000;
217 FD_ZERO( &set );
218 FD_SET( socket_server_fd, &set );
219 // When the connection will be made, we will have a writeable fd
220 while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) {
221 if(count > 30 || stream_check_interrupt(500)) {
222 if(count > 30)
223 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnTimeout);
224 else
225 mp_msg(MSGT_NETWORK,MSGL_V,"Connection interrupted by user\n");
226 return TCP_ERROR_TIMEOUT;
228 count++;
229 FD_ZERO( &set );
230 FD_SET( socket_server_fd, &set );
231 tv.tv_sec = 0;
232 tv.tv_usec = 500000;
234 if (ret < 0) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_SelectFailed);
236 // Turn back the socket as blocking
237 #if !HAVE_WINSOCK2_H
238 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK );
239 #else
240 val = 0;
241 ioctlsocket( socket_server_fd, FIONBIO, &val );
242 #endif
243 // Check if there were any errors
244 err_len = sizeof(int);
245 ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len);
246 if(ret < 0) {
247 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_GetSockOptFailed,strerror(errno));
248 return TCP_ERROR_FATAL;
250 if(err > 0) {
251 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnectError,strerror(err));
252 return TCP_ERROR_PORT;
255 return socket_server_fd;
258 // Connect to a server using a TCP connection
259 // return -2 for fatal error, like unable to resolve name, connection timeout...
260 // return -1 is unable to connect to a particular port
264 connect2Server(char *host, int port, int verb) {
265 #ifdef HAVE_AF_INET6
266 int r;
267 int s = TCP_ERROR_FATAL;
269 r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb);
270 if (r >= 0) return r;
272 s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb);
273 if (s == TCP_ERROR_FATAL) return r;
274 return s;
275 #else
276 return connect2Server_with_af(host, port, AF_INET,verb);
277 #endif