Fix a possible crash on 64 bit systems when the lumSrcPtr or chrSrcPtr
[mplayer/glamo.git] / stream / tcp.c
blob297f6901feffbe27f079e86f98981cac404cf6c3
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"
22 #include "input/input.h"
24 #ifndef HAVE_WINSOCK2
25 #include <netdb.h>
26 #include <netinet/in.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #define closesocket close
30 #else
31 #include <winsock2.h>
32 #include <ws2tcpip.h>
33 #endif
35 #include "tcp.h"
37 /* IPv6 options */
38 int network_prefer_ipv4 = 0;
40 // Converts an address family constant to a string
42 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 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 #ifdef HAVE_WINSOCK2
79 u_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 #ifdef HAVE_WINSOCK2
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 #ifndef HAVE_WINSOCK2
119 #ifdef USE_ATON
120 if (inet_aton(host, our_s_addr)!=1)
121 #else
122 if (inet_pton(af, host, our_s_addr)!=1)
123 #endif
124 #else
125 if ( inet_addr(host)==INADDR_NONE )
126 #endif
128 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ResolvingHostForAF, host, af2String(af));
130 #ifdef HAVE_GETHOSTBYNAME2
131 hp=(struct hostent*)gethostbyname2( host, af );
132 #else
133 hp=(struct hostent*)gethostbyname( host );
134 #endif
135 if( hp==NULL ) {
136 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantResolv, af2String(af), host);
137 return TCP_ERROR_FATAL;
140 memcpy( our_s_addr, (void*)hp->h_addr_list[0], hp->h_length );
142 #ifdef HAVE_WINSOCK2
143 else {
144 unsigned long addr = inet_addr(host);
145 memcpy( our_s_addr, (void*)&addr, sizeof(addr) );
147 #endif
149 switch (af) {
150 case AF_INET:
151 server_address.four.sin_family=af;
152 server_address.four.sin_port=htons(port);
153 server_address_size = sizeof(server_address.four);
154 break;
155 #ifdef HAVE_AF_INET6
156 case AF_INET6:
157 server_address.six.sin6_family=af;
158 server_address.six.sin6_port=htons(port);
159 server_address_size = sizeof(server_address.six);
160 break;
161 #endif
162 default:
163 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
164 return TCP_ERROR_FATAL;
167 #if defined(USE_ATON) || defined(HAVE_WINSOCK2)
168 strncpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255);
169 #else
170 inet_ntop(af, our_s_addr, buf, 255);
171 #endif
172 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ConnectingToServer, host, buf , port );
174 // Turn the socket as non blocking so we can timeout on the connection
175 #ifndef HAVE_WINSOCK2
176 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK );
177 #else
178 val = 1;
179 ioctlsocket( socket_server_fd, FIONBIO, &val );
180 #endif
181 if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) {
182 #ifndef HAVE_WINSOCK2
183 if( errno!=EINPROGRESS ) {
184 #else
185 if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) {
186 #endif
187 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantConnect2Server, af2String(af));
188 closesocket(socket_server_fd);
189 return TCP_ERROR_PORT;
192 tv.tv_sec = 0;
193 tv.tv_usec = 500000;
194 FD_ZERO( &set );
195 FD_SET( socket_server_fd, &set );
196 // When the connection will be made, we will have a writable fd
197 while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) {
198 if( ret<0 ) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_SelectFailed);
199 else if(ret > 0) break;
200 else if(count > 30 || mp_input_check_interrupt(500)) {
201 if(count > 30)
202 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnTimeout);
203 else
204 mp_msg(MSGT_NETWORK,MSGL_V,"Connection interuppted by user\n");
205 return TCP_ERROR_TIMEOUT;
207 count++;
208 FD_ZERO( &set );
209 FD_SET( socket_server_fd, &set );
210 tv.tv_sec = 0;
211 tv.tv_usec = 500000;
214 // Turn back the socket as blocking
215 #ifndef HAVE_WINSOCK2
216 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK );
217 #else
218 val = 0;
219 ioctlsocket( socket_server_fd, FIONBIO, &val );
220 #endif
221 // Check if there were any error
222 err_len = sizeof(int);
223 ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len);
224 if(ret < 0) {
225 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_GetSockOptFailed,strerror(errno));
226 return TCP_ERROR_FATAL;
228 if(err > 0) {
229 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnectError,strerror(err));
230 return TCP_ERROR_PORT;
233 return socket_server_fd;
236 // Connect to a server using a TCP connection
237 // return -2 for fatal error, like unable to resolve name, connection timeout...
238 // return -1 is unable to connect to a particular port
242 connect2Server(char *host, int port, int verb) {
243 #ifdef HAVE_AF_INET6
244 int r;
245 int s = TCP_ERROR_FATAL;
247 r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb);
248 if (r >= 0) return r;
250 s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb);
251 if (s == TCP_ERROR_FATAL) return r;
252 return s;
253 #else
254 return connect2Server_with_af(host, port, AF_INET,verb);
255 #endif