Patch by Stefan Huehner / stefan % huehner ! org \
[mplayer/glamo.git] / libmpdemux / network.c
blobbd1c5e34e2893e1ff28d683b1531cb480c2eea21
1 /*
2 * Network layer for MPlayer
3 * by Bertrand BAUDET <bertrand_baudet@yahoo.com>
4 * (C) 2001, MPlayer team.
5 */
7 //#define DUMP2FILE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
14 #include <errno.h>
15 #include <ctype.h>
17 #include "config.h"
19 #include "mp_msg.h"
20 #include "help_mp.h"
22 #ifndef HAVE_WINSOCK2
23 #define closesocket close
24 #else
25 #include <winsock2.h>
26 #include <ws2tcpip.h>
27 #endif
29 #include "stream.h"
30 #include "demuxer.h"
31 #include "m_config.h"
33 #include "network.h"
34 #include "http.h"
35 #include "cookies.h"
36 #include "url.h"
38 #include "version.h"
40 extern int verbose;
41 extern int stream_cache_size;
43 extern int mp_input_check_interrupt(int time);
45 /* Variables for the command line option -user, -passwd, -bandwidth,
46 -user-agent and -nocookies */
48 char *network_username=NULL;
49 char *network_password=NULL;
50 int network_bandwidth=0;
51 int network_cookies_enabled = 0;
52 char *network_useragent=NULL;
54 /* IPv6 options */
55 int network_prefer_ipv4 = 0;
56 int network_ipv4_only_proxy = 0;
59 mime_struct_t mime_type_table[] = {
60 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
61 { "audio/mpeg", DEMUXER_TYPE_AUDIO },
62 // MPEG streaming
63 { "video/mpeg", DEMUXER_TYPE_UNKNOWN },
64 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
65 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
66 // AVI ??? => video/x-msvideo
67 { "video/x-msvideo", DEMUXER_TYPE_AVI },
68 // MOV => video/quicktime
69 { "video/quicktime", DEMUXER_TYPE_MOV },
70 // ASF
71 { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
72 { "audio/x-ms-wma", DEMUXER_TYPE_ASF },
73 { "video/x-ms-asf", DEMUXER_TYPE_ASF },
74 { "video/x-ms-afs", DEMUXER_TYPE_ASF },
75 { "video/x-ms-wvx", DEMUXER_TYPE_ASF },
76 { "video/x-ms-wmv", DEMUXER_TYPE_ASF },
77 { "video/x-ms-wma", DEMUXER_TYPE_ASF },
78 { "application/x-mms-framed", DEMUXER_TYPE_ASF },
79 // Playlists
80 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
81 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
82 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
83 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
84 // Real Media
85 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
86 // OGG Streaming
87 { "application/x-ogg", DEMUXER_TYPE_OGG },
88 // NullSoft Streaming Video
89 { "video/nsv", DEMUXER_TYPE_NSV},
90 { "misc/ultravox", DEMUXER_TYPE_NSV},
91 { NULL, DEMUXER_TYPE_UNKNOWN},
95 streaming_ctrl_t *
96 streaming_ctrl_new(void) {
97 streaming_ctrl_t *streaming_ctrl;
98 streaming_ctrl = (streaming_ctrl_t*)malloc(sizeof(streaming_ctrl_t));
99 if( streaming_ctrl==NULL ) {
100 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
101 return NULL;
103 memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) );
104 return streaming_ctrl;
107 void
108 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
109 if( streaming_ctrl==NULL ) return;
110 if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
111 if( streaming_ctrl->buffer ) free( streaming_ctrl->buffer );
112 if( streaming_ctrl->data ) free( streaming_ctrl->data );
113 free( streaming_ctrl );
117 // Converts an address family constant to a string
119 char *af2String(int af) {
120 switch (af) {
121 case AF_INET: return "AF_INET";
123 #ifdef HAVE_AF_INET6
124 case AF_INET6: return "AF_INET6";
125 #endif
126 default: return "Unknown address family!";
132 // Connect to a server using a TCP connection, with specified address family
133 // return -2 for fatal error, like unable to resolve name, connection timeout...
134 // return -1 is unable to connect to a particular port
137 connect2Server_with_af(char *host, int port, int af,int verb) {
138 int socket_server_fd;
139 int err, err_len;
140 int ret,count = 0;
141 fd_set set;
142 struct timeval tv;
143 union {
144 struct sockaddr_in four;
145 #ifdef HAVE_AF_INET6
146 struct sockaddr_in6 six;
147 #endif
148 } server_address;
149 size_t server_address_size;
150 void *our_s_addr; // Pointer to sin_addr or sin6_addr
151 struct hostent *hp=NULL;
152 char buf[255];
154 #ifdef HAVE_WINSOCK2
155 u_long val;
156 #endif
158 socket_server_fd = socket(af, SOCK_STREAM, 0);
161 if( socket_server_fd==-1 ) {
162 // mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af));
163 return -2;
166 switch (af) {
167 case AF_INET: our_s_addr = (void *) &server_address.four.sin_addr; break;
168 #ifdef HAVE_AF_INET6
169 case AF_INET6: our_s_addr = (void *) &server_address.six.sin6_addr; break;
170 #endif
171 default:
172 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
173 return -2;
177 memset(&server_address, 0, sizeof(server_address));
179 #ifndef HAVE_WINSOCK2
180 #ifdef USE_ATON
181 if (inet_aton(host, our_s_addr)!=1)
182 #else
183 if (inet_pton(af, host, our_s_addr)!=1)
184 #endif
185 #else
186 if ( inet_addr(host)==INADDR_NONE )
187 #endif
189 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ResolvingHostForAF, host, af2String(af));
191 #ifdef HAVE_GETHOSTBYNAME2
192 hp=(struct hostent*)gethostbyname2( host, af );
193 #else
194 hp=(struct hostent*)gethostbyname( host );
195 #endif
196 if( hp==NULL ) {
197 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantResolv, af2String(af), host);
198 return -2;
201 memcpy( our_s_addr, (void*)hp->h_addr, hp->h_length );
203 #ifdef HAVE_WINSOCK2
204 else {
205 unsigned long addr = inet_addr(host);
206 memcpy( our_s_addr, (void*)&addr, sizeof(addr) );
208 #endif
210 switch (af) {
211 case AF_INET:
212 server_address.four.sin_family=af;
213 server_address.four.sin_port=htons(port);
214 server_address_size = sizeof(server_address.four);
215 break;
216 #ifdef HAVE_AF_INET6
217 case AF_INET6:
218 server_address.six.sin6_family=af;
219 server_address.six.sin6_port=htons(port);
220 server_address_size = sizeof(server_address.six);
221 break;
222 #endif
223 default:
224 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
225 return -2;
228 #if defined(USE_ATON) || defined(HAVE_WINSOCK2)
229 strncpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255);
230 #else
231 inet_ntop(af, our_s_addr, buf, 255);
232 #endif
233 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ConnectingToServer, host, buf , port );
235 // Turn the socket as non blocking so we can timeout on the connection
236 #ifndef HAVE_WINSOCK2
237 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK );
238 #else
239 val = 1;
240 ioctlsocket( socket_server_fd, FIONBIO, &val );
241 #endif
242 if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) {
243 #ifndef HAVE_WINSOCK2
244 if( errno!=EINPROGRESS ) {
245 #else
246 if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) {
247 #endif
248 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantConnect2Server, af2String(af));
249 closesocket(socket_server_fd);
250 return -1;
253 tv.tv_sec = 0;
254 tv.tv_usec = 500000;
255 FD_ZERO( &set );
256 FD_SET( socket_server_fd, &set );
257 // When the connection will be made, we will have a writable fd
258 while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) {
259 if( ret<0 ) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_SelectFailed);
260 else if(ret > 0) break;
261 else if(count > 30 || mp_input_check_interrupt(500)) {
262 if(count > 30)
263 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnTimeout);
264 else
265 mp_msg(MSGT_NETWORK,MSGL_V,"Connection interuppted by user\n");
266 return -3;
268 count++;
269 FD_ZERO( &set );
270 FD_SET( socket_server_fd, &set );
271 tv.tv_sec = 0;
272 tv.tv_usec = 500000;
275 // Turn back the socket as blocking
276 #ifndef HAVE_WINSOCK2
277 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK );
278 #else
279 val = 0;
280 ioctlsocket( socket_server_fd, FIONBIO, &val );
281 #endif
282 // Check if there were any error
283 err_len = sizeof(int);
284 ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len);
285 if(ret < 0) {
286 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_GetSockOptFailed,strerror(errno));
287 return -2;
289 if(err > 0) {
290 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnectError,strerror(err));
291 return -1;
294 return socket_server_fd;
297 // Connect to a server using a TCP connection
298 // return -2 for fatal error, like unable to resolve name, connection timeout...
299 // return -1 is unable to connect to a particular port
303 connect2Server(char *host, int port, int verb) {
304 #ifdef HAVE_AF_INET6
305 int r;
306 int s = -2;
308 r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb);
309 if (r > -1) return r;
311 s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb);
312 if (s == -2) return r;
313 return s;
314 #else
315 return connect2Server_with_af(host, port, AF_INET,verb);
316 #endif
321 URL_t*
322 check4proxies( URL_t *url ) {
323 URL_t *url_out = NULL;
324 if( url==NULL ) return NULL;
325 url_out = url_new( url->url );
326 if( !strcasecmp(url->protocol, "http_proxy") ) {
327 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
328 return url_out;
330 // Check if the http_proxy environment variable is set.
331 if( !strcasecmp(url->protocol, "http") ) {
332 char *proxy;
333 proxy = getenv("http_proxy");
334 if( proxy!=NULL ) {
335 // We got a proxy, build the URL to use it
336 int len;
337 char *new_url;
338 URL_t *tmp_url;
339 URL_t *proxy_url = url_new( proxy );
341 if( proxy_url==NULL ) {
342 mp_msg(MSGT_NETWORK,MSGL_WARN,
343 MSGTR_MPDEMUX_NW_InvalidProxySettingTryingWithout);
344 return url_out;
347 #ifdef HAVE_AF_INET6
348 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
349 mp_msg(MSGT_NETWORK,MSGL_WARN,
350 MSGTR_MPDEMUX_NW_CantResolvTryingWithoutProxy);
351 url_free(proxy_url);
352 return url_out;
354 #endif
356 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
357 len = strlen( proxy_url->hostname ) + strlen( url->url ) + 20; // 20 = http_proxy:// + port
358 new_url = malloc( len+1 );
359 if( new_url==NULL ) {
360 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
361 url_free(proxy_url);
362 return url_out;
364 sprintf(new_url, "http_proxy://%s:%d/%s", proxy_url->hostname, proxy_url->port, url->url );
365 tmp_url = url_new( new_url );
366 if( tmp_url==NULL ) {
367 free( new_url );
368 url_free( proxy_url );
369 return url_out;
371 url_free( url_out );
372 url_out = tmp_url;
373 free( new_url );
374 url_free( proxy_url );
377 return url_out;
381 http_send_request( URL_t *url, off_t pos ) {
382 HTTP_header_t *http_hdr;
383 URL_t *server_url;
384 char str[256];
385 int fd;
386 int ret;
387 int proxy = 0; // Boolean
389 http_hdr = http_new_header();
391 if( !strcasecmp(url->protocol, "http_proxy") ) {
392 proxy = 1;
393 server_url = url_new( (url->file)+1 );
394 http_set_uri( http_hdr, server_url->url );
395 } else {
396 server_url = url;
397 http_set_uri( http_hdr, server_url->file );
399 if (server_url->port && server_url->port != 80)
400 snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port );
401 else
402 snprintf(str, 256, "Host: %s", server_url->hostname );
403 http_set_field( http_hdr, str);
404 if (network_useragent)
406 snprintf(str, 256, "User-Agent: %s", network_useragent);
407 http_set_field(http_hdr, str);
409 else
410 http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION);
412 http_set_field(http_hdr, "Icy-MetaData: 1");
414 if(pos>0) {
415 // Extend http_send_request with possibility to do partial content retrieval
416 snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos);
417 http_set_field(http_hdr, str);
420 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
422 http_set_field( http_hdr, "Connection: close");
423 http_add_basic_authentication( http_hdr, url->username, url->password );
424 if( http_build_request( http_hdr )==NULL ) {
425 goto err_out;
428 if( proxy ) {
429 if( url->port==0 ) url->port = 8080; // Default port for the proxy server
430 fd = connect2Server( url->hostname, url->port,1 );
431 url_free( server_url );
432 } else {
433 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
434 fd = connect2Server( server_url->hostname, server_url->port,1 );
436 if( fd<0 ) {
437 goto err_out;
439 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
441 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 );
442 if( ret!=(int)http_hdr->buffer_size ) {
443 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest);
444 goto err_out;
447 http_free( http_hdr );
449 return fd;
450 err_out:
451 http_free(http_hdr);
452 return -1;
455 HTTP_header_t *
456 http_read_response( int fd ) {
457 HTTP_header_t *http_hdr;
458 char response[BUFFER_SIZE];
459 int i;
461 http_hdr = http_new_header();
462 if( http_hdr==NULL ) {
463 return NULL;
466 do {
467 i = recv( fd, response, BUFFER_SIZE, 0 );
468 if( i<0 ) {
469 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ReadFailed);
470 http_free( http_hdr );
471 return NULL;
473 if( i==0 ) {
474 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_Read0CouldBeEOF);
475 http_free( http_hdr );
476 return NULL;
478 http_response_append( http_hdr, response, i );
479 } while( !http_is_header_entire( http_hdr ) );
480 http_response_parse( http_hdr );
481 return http_hdr;
485 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
486 char *aut;
488 if( *auth_retry==1 ) {
489 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
490 return -1;
492 if( *auth_retry>0 ) {
493 if( url->username ) {
494 free( url->username );
495 url->username = NULL;
497 if( url->password ) {
498 free( url->password );
499 url->password = NULL;
503 aut = http_get_field(http_hdr, "WWW-Authenticate");
504 if( aut!=NULL ) {
505 char *aut_space;
506 aut_space = strstr(aut, "realm=");
507 if( aut_space!=NULL ) aut_space += 6;
508 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequiredFor, aut_space);
509 } else {
510 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequired);
512 if( network_username ) {
513 url->username = strdup(network_username);
514 if( url->username==NULL ) {
515 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
516 return -1;
518 } else {
519 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
520 return -1;
522 if( network_password ) {
523 url->password = strdup(network_password);
524 if( url->password==NULL ) {
525 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
526 return -1;
528 } else {
529 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank);
531 (*auth_retry)++;
532 return 0;
536 http_seek( stream_t *stream, off_t pos ) {
537 HTTP_header_t *http_hdr = NULL;
538 int fd;
539 if( stream==NULL ) return 0;
541 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
542 fd = http_send_request( stream->streaming_ctrl->url, pos );
543 if( fd<0 ) return 0;
545 http_hdr = http_read_response( fd );
547 if( http_hdr==NULL ) return 0;
549 switch( http_hdr->status_code ) {
550 case 200:
551 case 206: // OK
552 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
553 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
554 if( http_hdr->body_size>0 ) {
555 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
556 http_free( http_hdr );
557 return -1;
560 break;
561 default:
562 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase );
563 close( fd );
564 fd = -1;
566 stream->fd = fd;
568 if( http_hdr ) {
569 http_free( http_hdr );
570 stream->streaming_ctrl->data = NULL;
573 stream->pos=pos;
575 return 1;
580 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
581 //printf("streaming_bufferize\n");
582 streaming_ctrl->buffer = (char*)malloc(size);
583 if( streaming_ctrl->buffer==NULL ) {
584 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
585 return -1;
587 memcpy( streaming_ctrl->buffer, buffer, size );
588 streaming_ctrl->buffer_size = size;
589 return size;
593 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
594 int len=0;
595 //printf("nop_streaming_read\n");
596 if( stream_ctrl->buffer_size!=0 ) {
597 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
598 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
599 len = (size<buffer_len)?size:buffer_len;
600 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
601 stream_ctrl->buffer_pos += len;
602 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
603 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
604 free( stream_ctrl->buffer );
605 stream_ctrl->buffer = NULL;
606 stream_ctrl->buffer_size = 0;
607 stream_ctrl->buffer_pos = 0;
608 //printf("buffer cleaned\n");
610 //printf("read %d bytes from buffer\n", len );
613 if( len<size ) {
614 int ret;
615 ret = recv( fd, buffer+len, size-len, 0 );
616 if( ret<0 ) {
617 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
619 len += ret;
620 //printf("read %d bytes from network\n", len );
623 return len;
627 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
628 return -1;
629 // To shut up gcc warning
630 fd++;
631 pos++;
632 stream_ctrl=NULL;
636 void fixup_network_stream_cache(stream_t *stream) {
637 if(stream->streaming_ctrl->buffering) {
638 if(stream_cache_size<0) {
639 // cache option not set, will use our computed value.
640 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
641 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
642 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
644 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_CacheSizeSetTo, stream_cache_size);
650 streaming_stop( stream_t *stream ) {
651 stream->streaming_ctrl->status = streaming_stopped_e;
652 return 0;