Fix vf_tcdump's compilation
[mplayer/kovensky.git] / stream / tcp.c
blobf66131046da343d3eef8fde8ec622ee35cf1e22c
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 =
55 #ifdef _WIN32
57 #else
59 #endif
62 // Converts an address family constant to a string
64 static const char *af2String(int af) {
65 switch (af) {
66 case AF_INET: return "AF_INET";
68 #ifdef HAVE_AF_INET6
69 case AF_INET6: return "AF_INET6";
70 #endif
71 default: return "Unknown address family!";
77 // Connect to a server using a TCP connection, with specified address family
78 // return -2 for fatal error, like unable to resolve name, connection timeout...
79 // return -1 is unable to connect to a particular port
81 static int
82 connect2Server_with_af(char *host, int port, int af,int verb) {
83 int socket_server_fd;
84 int err;
85 socklen_t err_len;
86 int ret,count = 0;
87 fd_set set;
88 struct timeval tv;
89 union {
90 struct sockaddr_in four;
91 #ifdef HAVE_AF_INET6
92 struct sockaddr_in6 six;
93 #endif
94 } server_address;
95 size_t server_address_size;
96 void *our_s_addr; // Pointer to sin_addr or sin6_addr
97 struct hostent *hp=NULL;
98 char buf[255];
100 #if HAVE_WINSOCK2_H
101 unsigned long val;
102 int to;
103 #else
104 struct timeval to;
105 #endif
107 #if HAVE_WINSOCK2_H && defined(HAVE_AF_INET6)
108 // our winsock name resolution code can not handle IPv6
109 if (af == AF_INET6) {
110 mp_msg(MSGT_NETWORK, MSGL_WARN, "IPv6 not supported for winsock2\n");
111 return TCP_ERROR_FATAL;
113 #endif
115 socket_server_fd = socket(af, SOCK_STREAM, 0);
118 if( socket_server_fd==-1 ) {
119 // mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af));
120 return TCP_ERROR_FATAL;
123 #if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
124 #if HAVE_WINSOCK2_H
125 /* timeout in milliseconds */
126 to = 10 * 1000;
127 #else
128 to.tv_sec = 10;
129 to.tv_usec = 0;
130 #endif
131 setsockopt(socket_server_fd, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to));
132 setsockopt(socket_server_fd, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof(to));
133 #endif
135 switch (af) {
136 case AF_INET: our_s_addr = (void *) &server_address.four.sin_addr; break;
137 #ifdef HAVE_AF_INET6
138 case AF_INET6: our_s_addr = (void *) &server_address.six.sin6_addr; break;
139 #endif
140 default:
141 mp_tmsg(MSGT_NETWORK,MSGL_ERR, "Unknown address family %d\n", af);
142 return TCP_ERROR_FATAL;
146 memset(&server_address, 0, sizeof(server_address));
148 #if HAVE_INET_PTON
149 if (inet_pton(af, host, our_s_addr)!=1)
150 #elif HAVE_INET_ATON
151 if (inet_aton(host, our_s_addr)!=1)
152 #elif HAVE_WINSOCK2_H
153 if ( inet_addr(host)==INADDR_NONE )
154 #endif
156 if(verb) mp_tmsg(MSGT_NETWORK,MSGL_STATUS,"Resolving %s for %s...\n", host, af2String(af));
158 #ifdef HAVE_GETHOSTBYNAME2
159 hp=(struct hostent*)gethostbyname2( host, af );
160 #else
161 hp=(struct hostent*)gethostbyname( host );
162 #endif
163 if( hp==NULL ) {
164 if(verb) mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Couldn't resolve name for %s: %s\n", af2String(af), host);
165 return TCP_ERROR_FATAL;
168 memcpy( our_s_addr, (void*)hp->h_addr_list[0], hp->h_length );
170 #if HAVE_WINSOCK2_H
171 else {
172 unsigned long addr = inet_addr(host);
173 memcpy( our_s_addr, (void*)&addr, sizeof(addr) );
175 #endif
177 switch (af) {
178 case AF_INET:
179 server_address.four.sin_family=af;
180 server_address.four.sin_port=htons(port);
181 server_address_size = sizeof(server_address.four);
182 break;
183 #ifdef HAVE_AF_INET6
184 case AF_INET6:
185 server_address.six.sin6_family=af;
186 server_address.six.sin6_port=htons(port);
187 server_address_size = sizeof(server_address.six);
188 break;
189 #endif
190 default:
191 mp_tmsg(MSGT_NETWORK,MSGL_ERR, "Unknown address family %d\n", af);
192 return TCP_ERROR_FATAL;
195 #if HAVE_INET_ATON || defined(HAVE_WINSOCK2_H)
196 av_strlcpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255);
197 #else
198 inet_ntop(af, our_s_addr, buf, 255);
199 #endif
200 if(verb) mp_tmsg(MSGT_NETWORK,MSGL_STATUS,"Connecting to server %s[%s]: %d...\n", host, buf , port );
202 // Turn the socket as non blocking so we can timeout on the connection
203 #if !HAVE_WINSOCK2_H
204 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK );
205 #else
206 val = 1;
207 ioctlsocket( socket_server_fd, FIONBIO, &val );
208 #endif
209 if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) {
210 #if !HAVE_WINSOCK2_H
211 if( errno!=EINPROGRESS ) {
212 #else
213 if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) {
214 #endif
215 if(verb) mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Failed to connect to server with %s\n", af2String(af));
216 closesocket(socket_server_fd);
217 return TCP_ERROR_PORT;
220 tv.tv_sec = 0;
221 tv.tv_usec = 500000;
222 FD_ZERO( &set );
223 FD_SET( socket_server_fd, &set );
224 // When the connection will be made, we will have a writeable fd
225 while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) {
226 if(count > 30 || stream_check_interrupt(500)) {
227 if(count > 30)
228 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"connection timeout\n");
229 else
230 mp_msg(MSGT_NETWORK,MSGL_V,"Connection interrupted by user\n");
231 return TCP_ERROR_TIMEOUT;
233 count++;
234 FD_ZERO( &set );
235 FD_SET( socket_server_fd, &set );
236 tv.tv_sec = 0;
237 tv.tv_usec = 500000;
239 if (ret < 0) mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Select failed.\n");
241 // Turn back the socket as blocking
242 #if !HAVE_WINSOCK2_H
243 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK );
244 #else
245 val = 0;
246 ioctlsocket( socket_server_fd, FIONBIO, &val );
247 #endif
248 // Check if there were any errors
249 err_len = sizeof(int);
250 ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len);
251 if(ret < 0) {
252 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"getsockopt failed: %s\n",strerror(errno));
253 return TCP_ERROR_FATAL;
255 if(err > 0) {
256 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"connect error: %s\n",strerror(err));
257 return TCP_ERROR_PORT;
260 return socket_server_fd;
263 // Connect to a server using a TCP connection
264 // return -2 for fatal error, like unable to resolve name, connection timeout...
265 // return -1 is unable to connect to a particular port
269 connect2Server(char *host, int port, int verb) {
270 #ifdef HAVE_AF_INET6
271 int r;
272 int s = TCP_ERROR_FATAL;
274 r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb);
275 if (r >= 0) return r;
277 s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb);
278 if (s == TCP_ERROR_FATAL) return r;
279 return s;
280 #else
281 return connect2Server_with_af(host, port, AF_INET,verb);
282 #endif