odstraneni nekolika gui bugu, pridani librtmp, prepsani parseru na aktualni verzi...
[ctkuk.git] / mplayer / tcp.c
blob6d204f5b1a917b94f0e31b00648eef177577dac3
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 #if !defined(WIN32) && !defined(WIN64)
24 #include <unistd.h>
25 #include <sys/time.h>
26 #endif /* Def WIN32 or Def WIN64 */
28 #include <stdlib.h>
29 #include <string.h>
31 #include <errno.h>
32 #include <ctype.h>
34 #include <fcntl.h>
35 #include <sys/types.h>
37 #include "config.h"
39 #include "mp_msg.h"
40 #include "help_mp.h"
42 #if !HAVE_WINSOCK2_H
43 #include <netdb.h>
44 #include <netinet/in.h>
45 #include <sys/socket.h>
46 #include <arpa/inet.h>
47 #else
48 #include <winsock2.h>
49 #include <ws2tcpip.h>
50 #endif
52 #include "network.h"
53 #include "stream.h"
54 #include "tcp.h"
55 #include "avstring.h"
57 /* IPv6 options */
58 int network_prefer_ipv4 = 0;
60 // Converts an address family constant to a string
62 static const char *af2String(int af) {
63 switch (af) {
64 case AF_INET: return "AF_INET";
66 #ifdef HAVE_AF_INET6
67 case AF_INET6: return "AF_INET6";
68 #endif
69 default: return "Unknown address family!";
75 // Connect to a server using a TCP connection, with specified address family
76 // return -2 for fatal error, like unable to resolve name, connection timeout...
77 // return -1 is unable to connect to a particular port
79 static int
80 connect2Server_with_af(char *host, int port, int af,int verb) {
81 int socket_server_fd;
82 int err;
83 socklen_t err_len;
84 int ret,count = 0;
85 fd_set set;
86 struct timeval tv;
87 union {
88 struct sockaddr_in four;
89 #ifdef HAVE_AF_INET6
90 struct sockaddr_in6 six;
91 #endif
92 } server_address;
93 size_t server_address_size;
94 void *our_s_addr; // Pointer to sin_addr or sin6_addr
95 struct hostent *hp=NULL;
96 char buf[255];
98 #if HAVE_WINSOCK2_H
99 unsigned long val;
100 int to;
101 #else
102 struct timeval to;
103 #endif
105 #if HAVE_WINSOCK2_H && defined(HAVE_AF_INET6)
106 // our winsock name resolution code can not handle IPv6
107 if (af == AF_INET6) {
108 mp_msg(MSGT_NETWORK, MSGL_WARN, "IPv6 not supported for winsock2\n");
109 return TCP_ERROR_FATAL;
111 #endif
113 socket_server_fd = socket(af, SOCK_STREAM, 0);
116 if( socket_server_fd==-1 ) {
117 // mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af));
118 return TCP_ERROR_FATAL;
121 #if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
122 #if HAVE_WINSOCK2_H
123 /* timeout in milliseconds */
124 to = 10 * 1000;
125 #else
126 to.tv_sec = 10;
127 to.tv_usec = 0;
128 #endif
129 setsockopt(socket_server_fd, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to));
130 setsockopt(socket_server_fd, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof(to));
131 #endif
133 switch (af) {
134 case AF_INET: our_s_addr = (void *) &server_address.four.sin_addr; break;
135 #ifdef HAVE_AF_INET6
136 case AF_INET6: our_s_addr = (void *) &server_address.six.sin6_addr; break;
137 #endif
138 default:
139 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
140 return TCP_ERROR_FATAL;
144 memset(&server_address, 0, sizeof(server_address));
146 #if HAVE_INET_PTON
147 if (inet_pton(af, host, our_s_addr)!=1)
148 #elif HAVE_INET_ATON
149 if (inet_aton(host, our_s_addr)!=1)
150 #elif HAVE_WINSOCK2_H
151 if ( inet_addr(host)==INADDR_NONE )
152 #endif
154 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ResolvingHostForAF, host, af2String(af));
156 #ifdef HAVE_GETHOSTBYNAME2
157 hp=(struct hostent*)gethostbyname2( host, af );
158 #else
159 hp=(struct hostent*)gethostbyname( host );
160 #endif
161 if( hp==NULL ) {
162 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantResolv, af2String(af), host);
163 return TCP_ERROR_FATAL;
166 memcpy( our_s_addr, (void*)hp->h_addr_list[0], hp->h_length );
168 #if HAVE_WINSOCK2_H
169 else {
170 unsigned long addr = inet_addr(host);
171 memcpy( our_s_addr, (void*)&addr, sizeof(addr) );
173 #endif
175 switch (af) {
176 case AF_INET:
177 server_address.four.sin_family=af;
178 server_address.four.sin_port=htons(port);
179 server_address_size = sizeof(server_address.four);
180 break;
181 #ifdef HAVE_AF_INET6
182 case AF_INET6:
183 server_address.six.sin6_family=af;
184 server_address.six.sin6_port=htons(port);
185 server_address_size = sizeof(server_address.six);
186 break;
187 #endif
188 default:
189 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
190 return TCP_ERROR_FATAL;
193 #if HAVE_INET_ATON || defined(HAVE_WINSOCK2_H)
194 av_strlcpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255);
195 #else
196 inet_ntop(af, our_s_addr, buf, 255);
197 #endif
198 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ConnectingToServer, host, buf , port );
200 // Turn the socket as non blocking so we can timeout on the connection
201 #if !HAVE_WINSOCK2_H
202 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK );
203 #else
204 val = 1;
205 ioctlsocket( socket_server_fd, FIONBIO, &val );
206 #endif
207 if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) {
208 #if !HAVE_WINSOCK2_H
209 if( errno!=EINPROGRESS ) {
210 #else
211 if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) {
212 #endif
213 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantConnect2Server, af2String(af));
214 closesocket(socket_server_fd);
215 return TCP_ERROR_PORT;
218 tv.tv_sec = 0;
219 tv.tv_usec = 500000;
220 FD_ZERO( &set );
221 FD_SET( socket_server_fd, &set );
222 // When the connection will be made, we will have a writeable fd
223 while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) {
224 if(count > 30 || stream_check_interrupt(500)) {
225 if(count > 30)
226 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnTimeout);
227 else
228 mp_msg(MSGT_NETWORK,MSGL_V,"Connection interrupted by user\n");
229 return TCP_ERROR_TIMEOUT;
231 count++;
232 FD_ZERO( &set );
233 FD_SET( socket_server_fd, &set );
234 tv.tv_sec = 0;
235 tv.tv_usec = 500000;
237 if (ret < 0) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_SelectFailed);
239 // Turn back the socket as blocking
240 #if !HAVE_WINSOCK2_H
241 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK );
242 #else
243 val = 0;
244 ioctlsocket( socket_server_fd, FIONBIO, &val );
245 #endif
246 // Check if there were any errors
247 err_len = sizeof(int);
248 ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len);
249 if(ret < 0) {
250 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_GetSockOptFailed,strerror(errno));
251 return TCP_ERROR_FATAL;
253 if(err > 0) {
254 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnectError,strerror(err));
255 return TCP_ERROR_PORT;
258 return socket_server_fd;
261 // Connect to a server using a TCP connection
262 // return -2 for fatal error, like unable to resolve name, connection timeout...
263 // return -1 is unable to connect to a particular port
267 connect2Server(char *host, int port, int verb) {
268 #ifdef HAVE_AF_INET6
269 int r;
270 int s = TCP_ERROR_FATAL;
272 r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb);
273 if (r >= 0) return r;
275 s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb);
276 if (s == TCP_ERROR_FATAL) return r;
277 return s;
278 #else
279 return connect2Server_with_af(host, port, AF_INET,verb);
280 #endif