1 /*
2  * Network layer for MPlayer
3  *
4  * Copyright (C) 2001 Bertrand BAUDET <bertrand_baudet@yahoo.com>
5  *
6  * This file is part of MPlayer.
7  *
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.
12  *
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.
17  *
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.
21  */
22 
23 #define _BSD_SOURCE
24 
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 
29 #include <errno.h>
30 #include <ctype.h>
31 
32 #include <fcntl.h>
33 #include <sys/types.h>
34 
35 #include "config.h"
36 
37 #include "mp_msg.h"
38 #include "help_mp.h"
39 
40 #if !HAVE_WINSOCK2_H
41 #include <netdb.h>
42 #include <netinet/in.h>
43 #include <sys/socket.h>
44 #include <sys/time.h>
45 #include <arpa/inet.h>
46 #else
47 #include <winsock2.h>
48 #include <ws2tcpip.h>
49 #endif
50 
51 #include "network.h"
52 #include "stream.h"
53 #include "tcp.h"
54 #include "libavutil/avstring.h"
55 
56 /* IPv6 options */
57 int   network_prefer_ipv4 = 0;
58 
59 // Converts an address family constant to a string
60 
af2String(int af)61 static const char *af2String(int af) {
62 	switch (af) {
63 		case AF_INET:	return "AF_INET";
64 
65 #ifdef HAVE_AF_INET6
66 		case AF_INET6:	return "AF_INET6";
67 #endif
68 		default:	return "Unknown address family!";
69 	}
70 }
71 
72 
73 
74 // Connect to a server using a TCP connection, with specified address family
75 // return -2 for fatal error, like unable to resolve name, connection timeout...
76 // return -1 is unable to connect to a particular port
77 
78 static int
connect2Server_with_af(char * host,int port,int af,int verb)79 connect2Server_with_af(char *host, int port, int af,int verb) {
80 	int err_res = TCP_ERROR_FATAL;
81 	int socket_server_fd = -1;
82 	int err;
83         socklen_t err_len;
84 	int ret,count = 0;
85 	fd_set set;
86 	struct timeval tv;
87 	union {
88 		struct sockaddr_in four;
89 #ifdef HAVE_AF_INET6
90 		struct sockaddr_in6 six;
91 #endif
92 	} server_address;
93 	size_t server_address_size;
94 	void *our_s_addr;	// Pointer to sin_addr or sin6_addr
95 	struct hostent *hp=NULL;
96 	char buf[255];
97 
98 #if HAVE_WINSOCK2_H
99 	unsigned long val;
100 	int to;
101 #else
102 	struct timeval to;
103 #endif
104 
105 #if HAVE_WINSOCK2_H && defined(HAVE_AF_INET6)
106 	// our winsock name resolution code can not handle IPv6
107 	if (af == AF_INET6) {
108 		mp_msg(MSGT_NETWORK, MSGL_WARN, "IPv6 not supported for winsock2\n");
109 		goto err_out;
110 	}
111 #endif
112 
113 	socket_server_fd = socket(af, SOCK_STREAM, 0);
114 
115 
116 	if( socket_server_fd==-1 ) {
117 //		mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af));
118 		goto err_out;
119 	}
120 
121 #if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
122 #if HAVE_WINSOCK2_H
123 	/* timeout in milliseconds */
124 	to = 10 * 1000;
125 #else
126 	to.tv_sec = 10;
127 	to.tv_usec = 0;
128 #endif
129 	setsockopt(socket_server_fd, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to));
130 	setsockopt(socket_server_fd, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof(to));
131 #endif
132 
133 	switch (af) {
134 		case AF_INET:  our_s_addr = &server_address.four.sin_addr; break;
135 #ifdef HAVE_AF_INET6
136 		case AF_INET6: our_s_addr = &server_address.six.sin6_addr; break;
137 #endif
138 		default:
139 			mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
140 			goto err_out;
141 	}
142 
143 
144 	memset(&server_address, 0, sizeof(server_address));
145 
146 #if HAVE_INET_PTON
147 	if (inet_pton(af, host, our_s_addr)!=1)
148 #elif HAVE_INET_ATON
149 	if (inet_aton(host, our_s_addr)!=1)
150 #elif HAVE_WINSOCK2_H
151 	if ( inet_addr(host)==INADDR_NONE )
152 #endif
153 	{
154 		if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ResolvingHostForAF, host, af2String(af));
155 
156 #ifdef HAVE_GETHOSTBYNAME2
157 		hp=gethostbyname2( host, af );
158 #else
159 		hp=gethostbyname( host );
160 #endif
161 		if( hp==NULL ) {
162 			if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantResolv, af2String(af), host);
163 			goto err_out;
164 		}
165 
166 		if (af != hp->h_addrtype) goto err_out;
167 
168 		memcpy( our_s_addr, hp->h_addr_list[0], hp->h_length );
169 	}
170 #if HAVE_WINSOCK2_H
171 	else {
172 		unsigned long addr = inet_addr(host);
173 		memcpy( our_s_addr, &addr, sizeof(addr) );
174 	}
175 #endif
176 
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_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
192 			goto err_out;
193 	}
194 
195 #if HAVE_INET_PTON
196 	inet_ntop(af, our_s_addr, buf, 255);
197 #elif HAVE_INET_ATON || defined(HAVE_WINSOCK2_H)
198 	av_strlcpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255);
199 #endif
200 	if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ConnectingToServer, host, buf , port );
201 
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_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantConnect2Server, af2String(af));
216 			err_res = TCP_ERROR_PORT;
217 			goto err_out;
218 		}
219 	}
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_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnTimeout);
229 		else
230 		  mp_msg(MSGT_NETWORK,MSGL_V,"Connection interrupted by user\n");
231 		err_res = TCP_ERROR_TIMEOUT;
232 		goto err_out;
233 	      }
234 	      count++;
235 	      FD_ZERO( &set );
236 	      FD_SET( socket_server_fd, &set );
237 	      tv.tv_sec = 0;
238 	      tv.tv_usec = 500000;
239 	}
240 	if (ret < 0) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_SelectFailed);
241 
242 	// Turn back the socket as blocking
243 #if !HAVE_WINSOCK2_H
244 	fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK );
245 #else
246 	val = 0;
247 	ioctlsocket( socket_server_fd, FIONBIO, &val );
248 #endif
249 	// Check if there were any errors
250 	err_len = sizeof(int);
251 	ret =  getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len);
252 	if(ret < 0) {
253 		mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_GetSockOptFailed,strerror(errno));
254 		goto err_out;
255 	}
256 	if(err > 0) {
257 		mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnectError,strerror(err));
258 		err_res = TCP_ERROR_PORT;
259 		goto err_out;
260 	}
261 
262 	return socket_server_fd;
263 
264 err_out:
265 	if (socket_server_fd >= 0) closesocket(socket_server_fd);
266 	return err_res;
267 }
268 
269 // Connect to a server using a TCP connection
270 // return -2 for fatal error, like unable to resolve name, connection timeout...
271 // return -1 is unable to connect to a particular port
272 
273 
274 int
275 connect2Server(char *host, int  port, int verb) {
276 #ifdef HAVE_AF_INET6
277 	int r;
278 	int s = TCP_ERROR_FATAL;
279 
280 	r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb);
281 	if (r >= 0) return r;
282 
283 	s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb);
284 	if (s == TCP_ERROR_FATAL) return r;
285 	return s;
286 #else
287 	return connect2Server_with_af(host, port, AF_INET,verb);
288 #endif
289 
290 
291 }
292