sync with en/mplayer.1 rev. 30611
[mplayer/glamo.git] / stream / tcp.c
blobcf067c2ee7119f1be6a6617c602b472f8abb228b
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 socket_server_fd = socket(af, SOCK_STREAM, 0);
105 if( socket_server_fd==-1 ) {
106 // mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af));
107 return TCP_ERROR_FATAL;
110 #if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
111 #if HAVE_WINSOCK2_H
112 /* timeout in milliseconds */
113 to = 10 * 1000;
114 #else
115 to.tv_sec = 10;
116 to.tv_usec = 0;
117 #endif
118 setsockopt(socket_server_fd, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to));
119 setsockopt(socket_server_fd, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof(to));
120 #endif
122 switch (af) {
123 case AF_INET: our_s_addr = (void *) &server_address.four.sin_addr; break;
124 #ifdef HAVE_AF_INET6
125 case AF_INET6: our_s_addr = (void *) &server_address.six.sin6_addr; break;
126 #endif
127 default:
128 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
129 return TCP_ERROR_FATAL;
133 memset(&server_address, 0, sizeof(server_address));
135 #if HAVE_INET_PTON
136 if (inet_pton(af, host, our_s_addr)!=1)
137 #elif HAVE_INET_ATON
138 if (inet_aton(host, our_s_addr)!=1)
139 #elif HAVE_WINSOCK2_H
140 if ( inet_addr(host)==INADDR_NONE )
141 #endif
143 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ResolvingHostForAF, host, af2String(af));
145 #ifdef HAVE_GETHOSTBYNAME2
146 hp=(struct hostent*)gethostbyname2( host, af );
147 #else
148 hp=(struct hostent*)gethostbyname( host );
149 #endif
150 if( hp==NULL ) {
151 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantResolv, af2String(af), host);
152 return TCP_ERROR_FATAL;
155 memcpy( our_s_addr, (void*)hp->h_addr_list[0], hp->h_length );
157 #if HAVE_WINSOCK2_H
158 else {
159 unsigned long addr = inet_addr(host);
160 memcpy( our_s_addr, (void*)&addr, sizeof(addr) );
162 #endif
164 switch (af) {
165 case AF_INET:
166 server_address.four.sin_family=af;
167 server_address.four.sin_port=htons(port);
168 server_address_size = sizeof(server_address.four);
169 break;
170 #ifdef HAVE_AF_INET6
171 case AF_INET6:
172 server_address.six.sin6_family=af;
173 server_address.six.sin6_port=htons(port);
174 server_address_size = sizeof(server_address.six);
175 break;
176 #endif
177 default:
178 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
179 return TCP_ERROR_FATAL;
182 #if HAVE_INET_ATON || defined(HAVE_WINSOCK2_H)
183 av_strlcpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255);
184 #else
185 inet_ntop(af, our_s_addr, buf, 255);
186 #endif
187 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ConnectingToServer, host, buf , port );
189 // Turn the socket as non blocking so we can timeout on the connection
190 #if !HAVE_WINSOCK2_H
191 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK );
192 #else
193 val = 1;
194 ioctlsocket( socket_server_fd, FIONBIO, &val );
195 #endif
196 if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) {
197 #if !HAVE_WINSOCK2_H
198 if( errno!=EINPROGRESS ) {
199 #else
200 if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) {
201 #endif
202 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantConnect2Server, af2String(af));
203 closesocket(socket_server_fd);
204 return TCP_ERROR_PORT;
207 tv.tv_sec = 0;
208 tv.tv_usec = 500000;
209 FD_ZERO( &set );
210 FD_SET( socket_server_fd, &set );
211 // When the connection will be made, we will have a writeable fd
212 while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) {
213 if(count > 30 || stream_check_interrupt(500)) {
214 if(count > 30)
215 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnTimeout);
216 else
217 mp_msg(MSGT_NETWORK,MSGL_V,"Connection interrupted by user\n");
218 return TCP_ERROR_TIMEOUT;
220 count++;
221 FD_ZERO( &set );
222 FD_SET( socket_server_fd, &set );
223 tv.tv_sec = 0;
224 tv.tv_usec = 500000;
226 if (ret < 0) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_SelectFailed);
228 // Turn back the socket as blocking
229 #if !HAVE_WINSOCK2_H
230 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK );
231 #else
232 val = 0;
233 ioctlsocket( socket_server_fd, FIONBIO, &val );
234 #endif
235 // Check if there were any errors
236 err_len = sizeof(int);
237 ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len);
238 if(ret < 0) {
239 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_GetSockOptFailed,strerror(errno));
240 return TCP_ERROR_FATAL;
242 if(err > 0) {
243 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnectError,strerror(err));
244 return TCP_ERROR_PORT;
247 return socket_server_fd;
250 // Connect to a server using a TCP connection
251 // return -2 for fatal error, like unable to resolve name, connection timeout...
252 // return -1 is unable to connect to a particular port
256 connect2Server(char *host, int port, int verb) {
257 #ifdef HAVE_AF_INET6
258 int r;
259 int s = TCP_ERROR_FATAL;
261 r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb);
262 if (r >= 0) return r;
264 s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb);
265 if (s == TCP_ERROR_FATAL) return r;
266 return s;
267 #else
268 return connect2Server_with_af(host, port, AF_INET,verb);
269 #endif