Give better name to Inverse_Table_6_9
[mplayer/glamo.git] / stream / tcp.c
blobac3165b1a7ca96b8014cb5e8b138bb48a802e119
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"
37 /* IPv6 options */
38 int network_prefer_ipv4 = 0;
40 // Converts an address family constant to a string
42 static const char *af2String(int af) {
43 switch (af) {
44 case AF_INET: return "AF_INET";
46 #ifdef HAVE_AF_INET6
47 case AF_INET6: return "AF_INET6";
48 #endif
49 default: return "Unknown address family!";
55 // Connect to a server using a TCP connection, with specified address family
56 // return -2 for fatal error, like unable to resolve name, connection timeout...
57 // return -1 is unable to connect to a particular port
59 static int
60 connect2Server_with_af(char *host, int port, int af,int verb) {
61 int socket_server_fd;
62 int err;
63 socklen_t err_len;
64 int ret,count = 0;
65 fd_set set;
66 struct timeval tv;
67 union {
68 struct sockaddr_in four;
69 #ifdef HAVE_AF_INET6
70 struct sockaddr_in6 six;
71 #endif
72 } server_address;
73 size_t server_address_size;
74 void *our_s_addr; // Pointer to sin_addr or sin6_addr
75 struct hostent *hp=NULL;
76 char buf[255];
78 #if HAVE_WINSOCK2_H
79 unsigned long val;
80 int to;
81 #else
82 struct timeval to;
83 #endif
85 socket_server_fd = socket(af, SOCK_STREAM, 0);
88 if( socket_server_fd==-1 ) {
89 // mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af));
90 return TCP_ERROR_FATAL;
93 #if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
94 #if HAVE_WINSOCK2_H
95 /* timeout in milliseconds */
96 to = 10 * 1000;
97 #else
98 to.tv_sec = 10;
99 to.tv_usec = 0;
100 #endif
101 setsockopt(socket_server_fd, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to));
102 setsockopt(socket_server_fd, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof(to));
103 #endif
105 switch (af) {
106 case AF_INET: our_s_addr = (void *) &server_address.four.sin_addr; break;
107 #ifdef HAVE_AF_INET6
108 case AF_INET6: our_s_addr = (void *) &server_address.six.sin6_addr; break;
109 #endif
110 default:
111 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
112 return TCP_ERROR_FATAL;
116 memset(&server_address, 0, sizeof(server_address));
118 #if HAVE_INET_PTON
119 if (inet_pton(af, host, our_s_addr)!=1)
120 #elif HAVE_INET_ATON
121 if (inet_aton(host, our_s_addr)!=1)
122 #elif HAVE_WINSOCK2_H
123 if ( inet_addr(host)==INADDR_NONE )
124 #endif
126 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ResolvingHostForAF, host, af2String(af));
128 #ifdef HAVE_GETHOSTBYNAME2
129 hp=(struct hostent*)gethostbyname2( host, af );
130 #else
131 hp=(struct hostent*)gethostbyname( host );
132 #endif
133 if( hp==NULL ) {
134 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantResolv, af2String(af), host);
135 return TCP_ERROR_FATAL;
138 memcpy( our_s_addr, (void*)hp->h_addr_list[0], hp->h_length );
140 #if HAVE_WINSOCK2_H
141 else {
142 unsigned long addr = inet_addr(host);
143 memcpy( our_s_addr, (void*)&addr, sizeof(addr) );
145 #endif
147 switch (af) {
148 case AF_INET:
149 server_address.four.sin_family=af;
150 server_address.four.sin_port=htons(port);
151 server_address_size = sizeof(server_address.four);
152 break;
153 #ifdef HAVE_AF_INET6
154 case AF_INET6:
155 server_address.six.sin6_family=af;
156 server_address.six.sin6_port=htons(port);
157 server_address_size = sizeof(server_address.six);
158 break;
159 #endif
160 default:
161 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
162 return TCP_ERROR_FATAL;
165 #if HAVE_INET_ATON || defined(HAVE_WINSOCK2_H)
166 strncpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255);
167 #else
168 inet_ntop(af, our_s_addr, buf, 255);
169 #endif
170 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ConnectingToServer, host, buf , port );
172 // Turn the socket as non blocking so we can timeout on the connection
173 #if !HAVE_WINSOCK2_H
174 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK );
175 #else
176 val = 1;
177 ioctlsocket( socket_server_fd, FIONBIO, &val );
178 #endif
179 if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) {
180 #if !HAVE_WINSOCK2_H
181 if( errno!=EINPROGRESS ) {
182 #else
183 if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) {
184 #endif
185 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantConnect2Server, af2String(af));
186 closesocket(socket_server_fd);
187 return TCP_ERROR_PORT;
190 tv.tv_sec = 0;
191 tv.tv_usec = 500000;
192 FD_ZERO( &set );
193 FD_SET( socket_server_fd, &set );
194 // When the connection will be made, we will have a writeable fd
195 while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) {
196 if(count > 30 || stream_check_interrupt(500)) {
197 if(count > 30)
198 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnTimeout);
199 else
200 mp_msg(MSGT_NETWORK,MSGL_V,"Connection interrupted by user\n");
201 return TCP_ERROR_TIMEOUT;
203 count++;
204 FD_ZERO( &set );
205 FD_SET( socket_server_fd, &set );
206 tv.tv_sec = 0;
207 tv.tv_usec = 500000;
209 if (ret < 0) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_SelectFailed);
211 // Turn back the socket as blocking
212 #if !HAVE_WINSOCK2_H
213 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK );
214 #else
215 val = 0;
216 ioctlsocket( socket_server_fd, FIONBIO, &val );
217 #endif
218 // Check if there were any errors
219 err_len = sizeof(int);
220 ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len);
221 if(ret < 0) {
222 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_GetSockOptFailed,strerror(errno));
223 return TCP_ERROR_FATAL;
225 if(err > 0) {
226 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnectError,strerror(err));
227 return TCP_ERROR_PORT;
230 return socket_server_fd;
233 // Connect to a server using a TCP connection
234 // return -2 for fatal error, like unable to resolve name, connection timeout...
235 // return -1 is unable to connect to a particular port
239 connect2Server(char *host, int port, int verb) {
240 #ifdef HAVE_AF_INET6
241 int r;
242 int s = TCP_ERROR_FATAL;
244 r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb);
245 if (r >= 0) return r;
247 s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb);
248 if (s == TCP_ERROR_FATAL) return r;
249 return s;
250 #else
251 return connect2Server_with_af(host, port, AF_INET,verb);
252 #endif