Fix vf_tcdump's compilation
[mplayer/kovensky.git] / stream / network.c
blob537555f73a809235adccd816745a25a203c75499
1 /*
2 * Network layer for MPlayer
4 * Copyright (C) 2001 Bertrand Baudet <bertrand_baudet@yahoo.com>
6 * This file is part of MPlayer.
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.
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.
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.
23 //#define DUMP2FILE
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include <errno.h>
31 #include <ctype.h>
33 #include "config.h"
35 #include "mp_msg.h"
37 #if HAVE_WINSOCK2_H
38 #include <winsock2.h>
39 #include <ws2tcpip.h>
40 #endif
42 #include "stream.h"
43 #include "libmpdemux/demuxer.h"
44 #include "m_config.h"
46 #include "network.h"
47 #include "tcp.h"
48 #include "http.h"
49 #include "cookies.h"
50 #include "url.h"
52 #include "version.h"
54 extern int stream_cache_size;
56 /* Variables for the command line option -user, -passwd, -bandwidth,
57 -user-agent and -nocookies */
59 char *network_username=NULL;
60 char *network_password=NULL;
61 int network_bandwidth=0;
62 int network_cookies_enabled = 0;
63 char *network_useragent=NULL;
65 /* IPv6 options */
66 int network_ipv4_only_proxy = 0;
69 const mime_struct_t mime_type_table[] = {
70 #ifdef CONFIG_LIBAVFORMAT
71 // Flash Video
72 { "video/x-flv", DEMUXER_TYPE_LAVF_PREFERRED},
73 // do not force any demuxer in this case!
74 // we want the lavf demuxer to be tried first (happens automatically anyway),
75 // but for mov reference files to work we must also try
76 // the native demuxer if lavf fails.
77 { "video/quicktime", 0 },
78 #endif
79 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
80 { "audio/mpeg", DEMUXER_TYPE_AUDIO },
81 // MPEG streaming
82 { "video/mpeg", DEMUXER_TYPE_UNKNOWN },
83 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
84 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
85 // AVI ??? => video/x-msvideo
86 { "video/x-msvideo", DEMUXER_TYPE_AVI },
87 // MOV => video/quicktime
88 { "video/quicktime", DEMUXER_TYPE_MOV },
89 // ASF
90 { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
91 { "audio/x-ms-wma", DEMUXER_TYPE_ASF },
92 { "video/x-ms-asf", DEMUXER_TYPE_ASF },
93 { "video/x-ms-afs", DEMUXER_TYPE_ASF },
94 { "video/x-ms-wmv", DEMUXER_TYPE_ASF },
95 { "video/x-ms-wma", DEMUXER_TYPE_ASF },
96 { "application/x-mms-framed", DEMUXER_TYPE_ASF },
97 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
98 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN },
99 // Playlists
100 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
101 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST },
102 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
103 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
104 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
105 // Real Media
106 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
107 // OGG Streaming
108 { "application/x-ogg", DEMUXER_TYPE_OGG },
109 // NullSoft Streaming Video
110 { "video/nsv", DEMUXER_TYPE_NSV},
111 { "misc/ultravox", DEMUXER_TYPE_NSV},
112 { NULL, DEMUXER_TYPE_UNKNOWN},
116 streaming_ctrl_t *
117 streaming_ctrl_new(void) {
118 streaming_ctrl_t *streaming_ctrl;
119 streaming_ctrl = malloc(sizeof(streaming_ctrl_t));
120 if( streaming_ctrl==NULL ) {
121 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
122 return NULL;
124 memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) );
125 return streaming_ctrl;
128 void
129 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
130 if( streaming_ctrl==NULL ) return;
131 if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
132 if( streaming_ctrl->buffer ) free( streaming_ctrl->buffer );
133 if( streaming_ctrl->data ) free( streaming_ctrl->data );
134 free( streaming_ctrl );
137 URL_t*
138 check4proxies( URL_t *url ) {
139 URL_t *url_out = NULL;
140 if( url==NULL ) return NULL;
141 url_out = url_new( url->url );
142 if( !strcasecmp(url->protocol, "http_proxy") ) {
143 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
144 return url_out;
146 // Check if the http_proxy environment variable is set.
147 if( !strcasecmp(url->protocol, "http") ) {
148 char *proxy;
149 proxy = getenv("http_proxy");
150 if( proxy!=NULL ) {
151 // We got a proxy, build the URL to use it
152 int len;
153 char *new_url;
154 URL_t *tmp_url;
155 URL_t *proxy_url = url_new( proxy );
157 if( proxy_url==NULL ) {
158 mp_tmsg(MSGT_NETWORK,MSGL_WARN,
159 "Invalid proxy setting... Trying without proxy.\n");
160 return url_out;
163 #ifdef HAVE_AF_INET6
164 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
165 mp_tmsg(MSGT_NETWORK,MSGL_WARN,
166 "Could not resolve remote hostname for AF_INET. Trying without proxy.\n");
167 url_free(proxy_url);
168 return url_out;
170 #endif
172 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
173 len = strlen( proxy_url->hostname ) + strlen( url->url ) + 20; // 20 = http_proxy:// + port
174 new_url = malloc( len+1 );
175 if( new_url==NULL ) {
176 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
177 url_free(proxy_url);
178 return url_out;
180 sprintf(new_url, "http_proxy://%s:%d/%s", proxy_url->hostname, proxy_url->port, url->url );
181 tmp_url = url_new( new_url );
182 if( tmp_url==NULL ) {
183 free( new_url );
184 url_free( proxy_url );
185 return url_out;
187 url_free( url_out );
188 url_out = tmp_url;
189 free( new_url );
190 url_free( proxy_url );
193 return url_out;
197 http_send_request( URL_t *url, off_t pos ) {
198 HTTP_header_t *http_hdr;
199 URL_t *server_url;
200 char str[256];
201 int fd = -1;
202 int ret;
203 int proxy = 0; // Boolean
205 http_hdr = http_new_header();
207 if( !strcasecmp(url->protocol, "http_proxy") ) {
208 proxy = 1;
209 server_url = url_new( (url->file)+1 );
210 http_set_uri( http_hdr, server_url->url );
211 } else {
212 server_url = url;
213 http_set_uri( http_hdr, server_url->file );
215 if (server_url->port && server_url->port != 80)
216 snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port );
217 else
218 snprintf(str, 256, "Host: %s", server_url->hostname );
219 http_set_field( http_hdr, str);
220 if (network_useragent)
222 snprintf(str, 256, "User-Agent: %s", network_useragent);
223 http_set_field(http_hdr, str);
225 else
226 http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION);
228 if( strcasecmp(url->protocol, "noicyx") )
229 http_set_field(http_hdr, "Icy-MetaData: 1");
231 if(pos>0) {
232 // Extend http_send_request with possibility to do partial content retrieval
233 snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos);
234 http_set_field(http_hdr, str);
237 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
239 http_set_field( http_hdr, "Connection: close");
240 http_add_basic_authentication( http_hdr, url->username, url->password );
241 if( http_build_request( http_hdr )==NULL ) {
242 goto err_out;
245 if( proxy ) {
246 if( url->port==0 ) url->port = 8080; // Default port for the proxy server
247 fd = connect2Server( url->hostname, url->port,1 );
248 url_free( server_url );
249 server_url = NULL;
250 } else {
251 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
252 fd = connect2Server( server_url->hostname, server_url->port,1 );
254 if( fd<0 ) {
255 goto err_out;
257 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
259 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 );
260 if( ret!=(int)http_hdr->buffer_size ) {
261 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while sending HTTP request: Didn't send all the request.\n");
262 goto err_out;
265 http_free( http_hdr );
267 return fd;
268 err_out:
269 if (fd > 0) closesocket(fd);
270 http_free(http_hdr);
271 if (proxy && server_url)
272 url_free(server_url);
273 return -1;
276 HTTP_header_t *
277 http_read_response( int fd ) {
278 HTTP_header_t *http_hdr;
279 char response[BUFFER_SIZE];
280 int i;
282 http_hdr = http_new_header();
283 if( http_hdr==NULL ) {
284 return NULL;
287 do {
288 i = recv( fd, response, BUFFER_SIZE, 0 );
289 if( i<0 ) {
290 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Read failed.\n");
291 http_free( http_hdr );
292 return NULL;
294 if( i==0 ) {
295 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"http_read_response read 0 (i.e. EOF).\n");
296 http_free( http_hdr );
297 return NULL;
299 http_response_append( http_hdr, response, i );
300 } while( !http_is_header_entire( http_hdr ) );
301 http_response_parse( http_hdr );
302 return http_hdr;
306 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
307 char *aut;
309 #define MPDEMUX_NW_AuthFailed _(\
310 "Authentication failed. Please use the -user and -passwd options to provide your\n"\
311 "username/password for a list of URLs, or form an URL like:\n"\
312 "http://username:password@hostname/file\n")
315 if( *auth_retry==1 ) {
316 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
317 return -1;
319 if( *auth_retry>0 ) {
320 if( url->username ) {
321 free( url->username );
322 url->username = NULL;
324 if( url->password ) {
325 free( url->password );
326 url->password = NULL;
330 aut = http_get_field(http_hdr, "WWW-Authenticate");
331 if( aut!=NULL ) {
332 char *aut_space;
333 aut_space = strstr(aut, "realm=");
334 if( aut_space!=NULL ) aut_space += 6;
335 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required for %s\n", aut_space);
336 } else {
337 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required.\n");
339 if( network_username ) {
340 url->username = strdup(network_username);
341 if( url->username==NULL ) {
342 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
343 return -1;
345 } else {
346 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
347 return -1;
349 if( network_password ) {
350 url->password = strdup(network_password);
351 if( url->password==NULL ) {
352 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
353 return -1;
355 } else {
356 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"No password provided, trying blank password.\n");
358 (*auth_retry)++;
359 return 0;
363 http_seek( stream_t *stream, off_t pos ) {
364 HTTP_header_t *http_hdr = NULL;
365 int fd;
366 if( stream==NULL ) return 0;
368 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
369 fd = http_send_request( stream->streaming_ctrl->url, pos );
370 if( fd<0 ) return 0;
372 http_hdr = http_read_response( fd );
374 if( http_hdr==NULL ) return 0;
376 if( mp_msg_test(MSGT_NETWORK,MSGL_V) )
377 http_debug_hdr( http_hdr );
379 switch( http_hdr->status_code ) {
380 case 200:
381 case 206: // OK
382 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
383 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
384 if( http_hdr->body_size>0 ) {
385 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
386 http_free( http_hdr );
387 return -1;
390 break;
391 default:
392 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Server returns %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
393 closesocket( fd );
394 fd = -1;
396 stream->fd = fd;
398 if( http_hdr ) {
399 http_free( http_hdr );
400 stream->streaming_ctrl->data = NULL;
403 stream->pos=pos;
405 return 1;
410 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
411 //printf("streaming_bufferize\n");
412 streaming_ctrl->buffer = malloc(size);
413 if( streaming_ctrl->buffer==NULL ) {
414 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
415 return -1;
417 memcpy( streaming_ctrl->buffer, buffer, size );
418 streaming_ctrl->buffer_size = size;
419 return size;
423 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
424 int len=0;
425 //printf("nop_streaming_read\n");
426 if( stream_ctrl->buffer_size!=0 ) {
427 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
428 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
429 len = (size<buffer_len)?size:buffer_len;
430 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
431 stream_ctrl->buffer_pos += len;
432 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
433 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
434 free( stream_ctrl->buffer );
435 stream_ctrl->buffer = NULL;
436 stream_ctrl->buffer_size = 0;
437 stream_ctrl->buffer_pos = 0;
438 //printf("buffer cleaned\n");
440 //printf("read %d bytes from buffer\n", len );
443 if( len<size ) {
444 int ret;
445 ret = recv( fd, buffer+len, size-len, 0 );
446 if( ret<0 ) {
447 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
449 len += ret;
450 //printf("read %d bytes from network\n", len );
453 return len;
457 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
458 return -1;
459 // To shut up gcc warning
460 fd++;
461 pos++;
462 stream_ctrl=NULL;
466 void fixup_network_stream_cache(stream_t *stream) {
467 if(stream->streaming_ctrl->buffering) {
468 if(stream_cache_size<0) {
469 // cache option not set, will use our computed value.
470 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
471 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
472 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
474 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Cache size set to %d KBytes\n", stream_cache_size);