af_scaletempo: Fix delay value after changing scale to 1
[mplayer.git] / stream / tcp.c
blob0291fba54485218e46464c0f29c8a117f78f644b
1 /*
2 * Network layer for MPlayer
3 * by Bertrand BAUDET <bertrand_baudet@yahoo.com>
4 * (C) 2001, MPlayer team.
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
11 #include <errno.h>
12 #include <ctype.h>
14 #include <fcntl.h>
15 #include <sys/time.h>
16 #include <sys/types.h>
18 #include "config.h"
20 #include "mp_msg.h"
21 #include "help_mp.h"
23 #if !HAVE_WINSOCK2_H
24 #include <netdb.h>
25 #include <netinet/in.h>
26 #include <sys/socket.h>
27 #include <arpa/inet.h>
28 #else
29 #include <winsock2.h>
30 #include <ws2tcpip.h>
31 #endif
33 #include "network.h"
34 #include "stream.h"
35 #include "tcp.h"
36 #include "libavutil/avstring.h"
38 /* IPv6 options */
39 int network_prefer_ipv4 = 0;
41 // Converts an address family constant to a string
43 static const char *af2String(int af) {
44 switch (af) {
45 case AF_INET: return "AF_INET";
47 #ifdef HAVE_AF_INET6
48 case AF_INET6: return "AF_INET6";
49 #endif
50 default: return "Unknown address family!";
56 // Connect to a server using a TCP connection, with specified address family
57 // return -2 for fatal error, like unable to resolve name, connection timeout...
58 // return -1 is unable to connect to a particular port
60 static int
61 connect2Server_with_af(char *host, int port, int af,int verb) {
62 int socket_server_fd;
63 int err;
64 socklen_t err_len;
65 int ret,count = 0;
66 fd_set set;
67 struct timeval tv;
68 union {
69 struct sockaddr_in four;
70 #ifdef HAVE_AF_INET6
71 struct sockaddr_in6 six;
72 #endif
73 } server_address;
74 size_t server_address_size;
75 void *our_s_addr; // Pointer to sin_addr or sin6_addr
76 struct hostent *hp=NULL;
77 char buf[255];
79 #if HAVE_WINSOCK2_H
80 unsigned long val;
81 int to;
82 #else
83 struct timeval to;
84 #endif
86 socket_server_fd = socket(af, SOCK_STREAM, 0);
89 if( socket_server_fd==-1 ) {
90 // mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af));
91 return TCP_ERROR_FATAL;
94 #if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
95 #if HAVE_WINSOCK2_H
96 /* timeout in milliseconds */
97 to = 10 * 1000;
98 #else
99 to.tv_sec = 10;
100 to.tv_usec = 0;
101 #endif
102 setsockopt(socket_server_fd, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to));
103 setsockopt(socket_server_fd, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof(to));
104 #endif
106 switch (af) {
107 case AF_INET: our_s_addr = (void *) &server_address.four.sin_addr; break;
108 #ifdef HAVE_AF_INET6
109 case AF_INET6: our_s_addr = (void *) &server_address.six.sin6_addr; break;
110 #endif
111 default:
112 mp_tmsg(MSGT_NETWORK,MSGL_ERR, "Unknown address family %d\n", af);
113 return TCP_ERROR_FATAL;
117 memset(&server_address, 0, sizeof(server_address));
119 #if HAVE_INET_PTON
120 if (inet_pton(af, host, our_s_addr)!=1)
121 #elif HAVE_INET_ATON
122 if (inet_aton(host, our_s_addr)!=1)
123 #elif HAVE_WINSOCK2_H
124 if ( inet_addr(host)==INADDR_NONE )
125 #endif
127 if(verb) mp_tmsg(MSGT_NETWORK,MSGL_STATUS,"Resolving %s for %s...\n", host, af2String(af));
129 #ifdef HAVE_GETHOSTBYNAME2
130 hp=(struct hostent*)gethostbyname2( host, af );
131 #else
132 hp=(struct hostent*)gethostbyname( host );
133 #endif
134 if( hp==NULL ) {
135 if(verb) mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Couldn't resolve name for %s: %s\n", af2String(af), host);
136 return TCP_ERROR_FATAL;
139 memcpy( our_s_addr, (void*)hp->h_addr_list[0], hp->h_length );
141 #if HAVE_WINSOCK2_H
142 else {
143 unsigned long addr = inet_addr(host);
144 memcpy( our_s_addr, (void*)&addr, sizeof(addr) );
146 #endif
148 switch (af) {
149 case AF_INET:
150 server_address.four.sin_family=af;
151 server_address.four.sin_port=htons(port);
152 server_address_size = sizeof(server_address.four);
153 break;
154 #ifdef HAVE_AF_INET6
155 case AF_INET6:
156 server_address.six.sin6_family=af;
157 server_address.six.sin6_port=htons(port);
158 server_address_size = sizeof(server_address.six);
159 break;
160 #endif
161 default:
162 mp_tmsg(MSGT_NETWORK,MSGL_ERR, "Unknown address family %d\n", af);
163 return TCP_ERROR_FATAL;
166 #if HAVE_INET_ATON || defined(HAVE_WINSOCK2_H)
167 av_strlcpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255);
168 #else
169 inet_ntop(af, our_s_addr, buf, 255);
170 #endif
171 if(verb) mp_tmsg(MSGT_NETWORK,MSGL_STATUS,"Connecting to server %s[%s]: %d...\n", host, buf , port );
173 // Turn the socket as non blocking so we can timeout on the connection
174 #if !HAVE_WINSOCK2_H
175 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK );
176 #else
177 val = 1;
178 ioctlsocket( socket_server_fd, FIONBIO, &val );
179 #endif
180 if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) {
181 #if !HAVE_WINSOCK2_H
182 if( errno!=EINPROGRESS ) {
183 #else
184 if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) {
185 #endif
186 if(verb) mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Failed to connect to server with %s\n", af2String(af));
187 closesocket(socket_server_fd);
188 return TCP_ERROR_PORT;
191 tv.tv_sec = 0;
192 tv.tv_usec = 500000;
193 FD_ZERO( &set );
194 FD_SET( socket_server_fd, &set );
195 // When the connection will be made, we will have a writeable fd
196 while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) {
197 if(count > 30 || stream_check_interrupt(500)) {
198 if(count > 30)
199 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"connection timeout\n");
200 else
201 mp_msg(MSGT_NETWORK,MSGL_V,"Connection interrupted by user\n");
202 return TCP_ERROR_TIMEOUT;
204 count++;
205 FD_ZERO( &set );
206 FD_SET( socket_server_fd, &set );
207 tv.tv_sec = 0;
208 tv.tv_usec = 500000;
210 if (ret < 0) mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Select failed.\n");
212 // Turn back the socket as blocking
213 #if !HAVE_WINSOCK2_H
214 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK );
215 #else
216 val = 0;
217 ioctlsocket( socket_server_fd, FIONBIO, &val );
218 #endif
219 // Check if there were any errors
220 err_len = sizeof(int);
221 ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len);
222 if(ret < 0) {
223 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"getsockopt failed: %s\n",strerror(errno));
224 return TCP_ERROR_FATAL;
226 if(err > 0) {
227 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"connect error: %s\n",strerror(err));
228 return TCP_ERROR_PORT;
231 return socket_server_fd;
234 // Connect to a server using a TCP connection
235 // return -2 for fatal error, like unable to resolve name, connection timeout...
236 // return -1 is unable to connect to a particular port
240 connect2Server(char *host, int port, int verb) {
241 #ifdef HAVE_AF_INET6
242 int r;
243 int s = TCP_ERROR_FATAL;
245 r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb);
246 if (r >= 0) return r;
248 s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb);
249 if (s == TCP_ERROR_FATAL) return r;
250 return s;
251 #else
252 return connect2Server_with_af(host, port, AF_INET,verb);
253 #endif