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 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <ctype.h>
29 
30 #include "config.h"
31 
32 #include "libavutil/avstring.h"
33 #include "mp_msg.h"
34 #include "help_mp.h"
35 
36 #if HAVE_WINSOCK2_H
37 #include <winsock2.h>
38 #include <ws2tcpip.h>
39 #endif
40 
41 #include "stream.h"
42 #include "libmpdemux/demuxer.h"
43 #include "m_config.h"
44 #include "mpcommon.h"
45 #include "network.h"
46 #include "tcp.h"
47 #include "http.h"
48 #include "cookies.h"
49 #include "url.h"
50 
51 /* Variables for the command line option -user, -passwd, -bandwidth,
52    -user-agent and -nocookies */
53 
54 char *network_username=NULL;
55 char *network_password=NULL;
56 int   network_bandwidth=0;
57 int   network_cookies_enabled = 0;
58 char *network_useragent=NULL;
59 char *network_referrer=NULL;
60 char **network_http_header_fields=NULL;
61 
62 /* IPv6 options */
63 int   network_ipv4_only_proxy = 0;
64 
65 
66 const mime_struct_t mime_type_table[] = {
67 #ifdef CONFIG_FFMPEG
68 	// Flash Video
69 	{ "video/x-flv", DEMUXER_TYPE_LAVF_PREFERRED},
70 	// do not force any demuxer in this case!
71 	// we want the lavf demuxer to be tried first (happens automatically anyway),
72 	// but for mov reference files to work we must also try
73 	// the native demuxer if lavf fails.
74 	{ "video/quicktime", 0 },
75 #endif
76 	// MP3 streaming, some MP3 streaming server answer with audio/mpeg
77 	{ "audio/mpeg", DEMUXER_TYPE_AUDIO },
78 	// MPEG streaming
79 	{ "video/mpeg", DEMUXER_TYPE_UNKNOWN },
80 	{ "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
81 	{ "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
82 	// AVI ??? => video/x-msvideo
83 	{ "video/x-msvideo", DEMUXER_TYPE_AVI },
84 	// MOV => video/quicktime
85 	{ "video/quicktime", DEMUXER_TYPE_MOV },
86 	// ASF
87         { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
88 	{ "audio/x-ms-wma", DEMUXER_TYPE_ASF },
89 	{ "video/x-ms-asf", DEMUXER_TYPE_ASF },
90 	{ "video/x-ms-afs", DEMUXER_TYPE_ASF },
91 	{ "video/x-ms-wmv", DEMUXER_TYPE_ASF },
92 	{ "video/x-ms-wma", DEMUXER_TYPE_ASF },
93 	{ "application/x-mms-framed", DEMUXER_TYPE_ASF },
94 	{ "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
95 	{ "application/octet-stream", DEMUXER_TYPE_UNKNOWN },
96 	// Playlists
97 	{ "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
98 	{ "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST },
99 	{ "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
100 	{ "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
101 	{ "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
102 	// Real Media
103 //	{ "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
104 	// OGG Streaming
105 	{ "application/ogg", DEMUXER_TYPE_OGG },
106 	{ "application/x-ogg", DEMUXER_TYPE_OGG },
107 	{ "audio/ogg", DEMUXER_TYPE_OGG },
108 	{ "video/ogg", DEMUXER_TYPE_OGG },
109 	// NullSoft Streaming Video
110 	{ "video/nsv", DEMUXER_TYPE_NSV},
111 	{ "misc/ultravox", DEMUXER_TYPE_NSV},
112 	// HLS Streaming/AppleHttp
113 	{ "application/vnd.apple.mpegurl", DEMUXER_TYPE_LAVF},
114 	{ "application/x-mpegURL", DEMUXER_TYPE_LAVF},
115 	{ NULL, DEMUXER_TYPE_UNKNOWN},
116 };
117 
118 
119 streaming_ctrl_t *
streaming_ctrl_new(void)120 streaming_ctrl_new(void) {
121 	streaming_ctrl_t *streaming_ctrl = calloc(1, sizeof(*streaming_ctrl));
122 	if( streaming_ctrl==NULL ) {
123 		mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
124 		return NULL;
125 	}
126 	return streaming_ctrl;
127 }
128 
129 void
streaming_ctrl_free(streaming_ctrl_t * streaming_ctrl)130 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
131 	if( streaming_ctrl==NULL ) return;
132 	if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
133 	free(streaming_ctrl->buffer);
134 	free(streaming_ctrl->data);
135 	free(streaming_ctrl);
136 }
137 
138 URL_t*
check4proxies(const URL_t * url)139 check4proxies( const URL_t *url ) {
140 	URL_t *url_out = NULL;
141 	if( url==NULL ) return NULL;
142 	url_out = url_new( url->url );
143 	if( !av_strcasecmp(url->protocol, "http_proxy") ) {
144 		mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
145 		return url_out;
146 	}
147 	// Check if the http_proxy environment variable is set.
148 	if( !av_strcasecmp(url->protocol, "http") ) {
149 		char *proxy;
150 		proxy = getenv("http_proxy");
151 		if( proxy!=NULL ) {
152 			// We got a proxy, build the URL to use it
153 			char *new_url;
154 			URL_t *tmp_url;
155 			URL_t *proxy_url = url_new( proxy );
156 
157 			if( proxy_url==NULL ) {
158 				mp_msg(MSGT_NETWORK,MSGL_WARN,
159 					MSGTR_MPDEMUX_NW_InvalidProxySettingTryingWithout);
160 				return url_out;
161 			}
162 
163 #ifdef HAVE_AF_INET6
164 			if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
165 				mp_msg(MSGT_NETWORK,MSGL_WARN,
166 					MSGTR_MPDEMUX_NW_CantResolvTryingWithoutProxy);
167 				url_free(proxy_url);
168 				return url_out;
169 			}
170 #endif
171 
172 			mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
173 			new_url = get_http_proxy_url(proxy_url, url->url);
174 			if( new_url==NULL ) {
175 				mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
176 				url_free(proxy_url);
177 				return url_out;
178 			}
179 			tmp_url = url_new( new_url );
180 			if( tmp_url==NULL ) {
181 				free( new_url );
182 				url_free( proxy_url );
183 				return url_out;
184 			}
185 			url_free( url_out );
186 			url_out = tmp_url;
187 			free( new_url );
188 			url_free( proxy_url );
189 		}
190 	}
191 	return url_out;
192 }
193 
url_new_with_proxy(const char * urlstr)194 URL_t *url_new_with_proxy(const char *urlstr)
195 {
196 	URL_t *url = url_new(urlstr);
197 	URL_t *url_with_proxy = check4proxies(url);
198 	url_free(url);
199 	return url_with_proxy;
200 }
201 
202 int
http_send_request(URL_t * url,int64_t pos)203 http_send_request( URL_t *url, int64_t pos ) {
204 	HTTP_header_t *http_hdr;
205 	URL_t *server_url;
206 	char str[256];
207 	int fd = -1;
208 	int ret;
209 	int proxy = 0;		// Boolean
210 
211 	http_hdr = http_new_header();
212 
213 	if( !av_strcasecmp(url->protocol, "http_proxy") ) {
214 		proxy = 1;
215 		server_url = url_new( (url->file)+1 );
216 		if (!server_url) {
217 			mp_msg(MSGT_NETWORK, MSGL_ERR, "Invalid URL '%s' to proxify\n", url->file+1);
218 			goto err_out;
219 		}
220 		http_set_uri( http_hdr, server_url->noauth_url );
221 	} else {
222 		server_url = url;
223 		http_set_uri( http_hdr, server_url->file );
224 	}
225 	if (server_url->port && server_url->port != 80)
226 	    snprintf(str, sizeof(str), "Host: %s:%d", server_url->hostname, server_url->port );
227 	else
228 	    snprintf(str, sizeof(str), "Host: %s", server_url->hostname );
229 	http_set_field( http_hdr, str);
230 	if (network_useragent)
231 	    snprintf(str, sizeof(str), "User-Agent: %s", network_useragent);
232 	else
233 	    snprintf(str, sizeof(str), "User-Agent: %s", mplayer_version);
234         http_set_field(http_hdr, str);
235 
236 	if (network_referrer) {
237 	    char *referrer = NULL;
238 	    size_t len = strlen(network_referrer) + 10;
239 
240 	    // Check len to ensure we don't do something really bad in case of an overflow
241 	    if (len > 10)
242 		referrer = malloc(len);
243 
244 	    if (referrer == NULL) {
245 		mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MemAllocFailed);
246 	    } else {
247 		snprintf(referrer, len, "Referer: %s", network_referrer);
248 		http_set_field(http_hdr, referrer);
249 		free(referrer);
250 	    }
251 	}
252 
253 	if( av_strcasecmp(url->protocol, "noicyx") )
254 	    http_set_field(http_hdr, "Icy-MetaData: 1");
255 
256 	if(pos>0) {
257 	// Extend http_send_request with possibility to do partial content retrieval
258 	    snprintf(str, sizeof(str), "Range: bytes=%"PRId64"-", (int64_t)pos);
259 	    http_set_field(http_hdr, str);
260 	}
261 
262 	if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
263 
264 	if (network_http_header_fields) {
265 		int i=0;
266 		while (network_http_header_fields[i])
267 			http_set_field(http_hdr, network_http_header_fields[i++]);
268 	}
269 
270 	http_set_field( http_hdr, "Connection: close");
271 	if (proxy)
272 		http_add_basic_proxy_authentication(http_hdr, url->username, url->password);
273 	http_add_basic_authentication(http_hdr, server_url->username, server_url->password);
274 	if( http_build_request( http_hdr )==NULL ) {
275 		goto err_out;
276 	}
277 
278 	if( proxy ) {
279 		if( url->port==0 ) url->port = 8080;			// Default port for the proxy server
280 		fd = connect2Server( url->hostname, url->port,1 );
281 		url_free( server_url );
282 		server_url = NULL;
283 	} else {
284 		if( server_url->port==0 ) server_url->port = 80;	// Default port for the web server
285 		fd = connect2Server( server_url->hostname, server_url->port,1 );
286 	}
287 	if( fd<0 ) {
288 		goto err_out;
289 	}
290 	mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
291 
292 	ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, DEFAULT_SEND_FLAGS );
293 	if( ret!=(int)http_hdr->buffer_size ) {
294 		mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest);
295 		goto err_out;
296 	}
297 
298 	http_free( http_hdr );
299 
300 	return fd;
301 err_out:
302 	if (fd > 0) closesocket(fd);
303 	http_free(http_hdr);
304 	if (proxy && server_url)
305 		url_free(server_url);
306 	return -1;
307 }
308 
309 HTTP_header_t *
http_read_response(int fd)310 http_read_response( int fd ) {
311 	HTTP_header_t *http_hdr;
312 	char response[BUFFER_SIZE];
313 	int i;
314 
315 	http_hdr = http_new_header();
316 	if( http_hdr==NULL ) {
317 		return NULL;
318 	}
319 
320 	do {
321 		i = recv( fd, response, BUFFER_SIZE, 0 );
322 		if( i<0 ) {
323 			mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ReadFailed);
324 			http_free( http_hdr );
325 			return NULL;
326 		}
327 		if( i==0 ) {
328 			mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_Read0CouldBeEOF);
329 			http_free( http_hdr );
330 			return NULL;
331 		}
332 		http_response_append( http_hdr, response, i );
333 	} while( !http_is_header_entire( http_hdr ) );
334 	if (http_response_parse( http_hdr ) < 0) {
335 		http_free( http_hdr );
336 		return NULL;
337 	}
338 	return http_hdr;
339 }
340 
341 int
http_authenticate(HTTP_header_t * http_hdr,URL_t * url,int * auth_retry)342 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
343 	char *aut;
344 
345 	if( *auth_retry==1 ) {
346 		mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
347 		return -1;
348 	}
349 	if( *auth_retry>0 ) {
350 		free(url->username);
351 		url->username = NULL;
352 		free(url->password);
353 		url->password = NULL;
354 	}
355 
356 	aut = http_get_field(http_hdr, "WWW-Authenticate");
357 	if( aut!=NULL ) {
358 		char *aut_space;
359 		aut_space = strstr(aut, "realm=");
360 		if( aut_space!=NULL ) aut_space += 6;
361 		mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequiredFor, aut_space);
362 	} else {
363 		mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequired);
364 	}
365 	if( network_username ) {
366 		url->username = strdup(network_username);
367 		if( url->username==NULL ) {
368 			mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
369 			return -1;
370 		}
371 	} else {
372 		mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
373 		return -1;
374 	}
375 	if( network_password ) {
376 		url->password = strdup(network_password);
377 		if( url->password==NULL ) {
378 			mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
379 			return -1;
380 		}
381 	} else {
382 		mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank);
383 	}
384 	(*auth_retry)++;
385 	return 0;
386 }
387 
388 int
http_seek(stream_t * stream,int64_t pos)389 http_seek( stream_t *stream, int64_t pos ) {
390 	HTTP_header_t *http_hdr = NULL;
391 	int fd;
392 	if( stream==NULL ) return 0;
393 
394 	if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
395 	fd = http_send_request( stream->streaming_ctrl->url, pos );
396 	if( fd<0 ) return 0;
397 
398 	http_hdr = http_read_response( fd );
399 
400 	if( http_hdr==NULL ) return 0;
401 
402 	if( mp_msg_test(MSGT_NETWORK,MSGL_V) )
403 		http_debug_hdr( http_hdr );
404 
405 	switch( http_hdr->status_code ) {
406 		case 200:
407 		case 206: // OK
408 			mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
409 			mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
410 			if( http_hdr->body_size>0 ) {
411 				if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
412 					http_free( http_hdr );
413 					return -1;
414 				}
415 			}
416 			break;
417 		default:
418 			mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase );
419 			closesocket( fd );
420 			fd = -1;
421 	}
422 	stream->fd = fd;
423 
424 	if( http_hdr ) {
425 		http_free( http_hdr );
426 		stream->streaming_ctrl->data = NULL;
427 	}
428 
429 	stream->pos=pos;
430 
431 	return 1;
432 }
433 
434 
435 int
streaming_bufferize(streaming_ctrl_t * streaming_ctrl,char * buffer,int size)436 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
437 //printf("streaming_bufferize\n");
438 	streaming_ctrl->buffer = malloc(size);
439 	if( streaming_ctrl->buffer==NULL ) {
440 		mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
441 		return -1;
442 	}
443 	memcpy( streaming_ctrl->buffer, buffer, size );
444 	streaming_ctrl->buffer_size = size;
445 	return size;
446 }
447 
448 int
nop_streaming_read(int fd,char * buffer,int size,streaming_ctrl_t * stream_ctrl)449 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
450 	int len=0;
451 //printf("nop_streaming_read\n");
452 	if( stream_ctrl->buffer_size!=0 ) {
453 		int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
454 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
455 		len = (size<buffer_len)?size:buffer_len;
456 		memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
457 		stream_ctrl->buffer_pos += len;
458 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
459 		if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
460 			free( stream_ctrl->buffer );
461 			stream_ctrl->buffer = NULL;
462 			stream_ctrl->buffer_size = 0;
463 			stream_ctrl->buffer_pos = 0;
464 //printf("buffer cleaned\n");
465 		}
466 //printf("read %d bytes from buffer\n", len );
467 	}
468 
469 	if( len<size ) {
470 		int ret;
471 		ret = recv( fd, buffer+len, size-len, 0 );
472 		if( ret<0 ) {
473 			mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
474 			ret = 0;
475 		} else if (ret == 0)
476 			stream_ctrl->status = streaming_stopped_e;
477 		len += ret;
478 //printf("read %d bytes from network\n", len );
479 	}
480 
481 	return len;
482 }
483 
484 int
nop_streaming_seek(int fd,int64_t pos,streaming_ctrl_t * stream_ctrl)485 nop_streaming_seek( int fd, int64_t pos, streaming_ctrl_t *stream_ctrl ) {
486 	return -1;
487 }
488 
489 
fixup_network_stream_cache(stream_t * stream)490 void fixup_network_stream_cache(stream_t *stream) {
491   if(stream->streaming_ctrl->buffering) {
492     if(stream_cache_size<0) {
493       // cache option not set, will use our computed value.
494       // buffer in KBytes, *5 because the prefill is 20% of the buffer.
495       stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
496       if( stream_cache_size<64 ) stream_cache_size = 64;	// 16KBytes min buffer
497     }
498     mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_CacheSizeSetTo, stream_cache_size);
499   }
500 }
501 
502