2 * Network layer for MPlayer
3 * by Bertrand BAUDET <bertrand_baudet@yahoo.com>
4 * (C) 2001, MPlayer team.
23 #define closesocket close
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
;
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
},
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
},
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
},
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
},
85 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
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
},
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
);
103 memset( streaming_ctrl
, 0, sizeof(streaming_ctrl_t
) );
104 return streaming_ctrl
;
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
) {
121 case AF_INET
: return "AF_INET";
124 case AF_INET6
: return "AF_INET6";
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
;
144 struct sockaddr_in four
;
146 struct sockaddr_in6 six
;
149 size_t server_address_size
;
150 void *our_s_addr
; // Pointer to sin_addr or sin6_addr
151 struct hostent
*hp
=NULL
;
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));
167 case AF_INET
: our_s_addr
= (void *) &server_address
.four
.sin_addr
; break;
169 case AF_INET6
: our_s_addr
= (void *) &server_address
.six
.sin6_addr
; break;
172 mp_msg(MSGT_NETWORK
,MSGL_ERR
, MSGTR_MPDEMUX_NW_UnknownAF
, af
);
177 memset(&server_address
, 0, sizeof(server_address
));
179 #ifndef HAVE_WINSOCK2
181 if (inet_aton(host
, our_s_addr
)!=1)
183 if (inet_pton(af
, host
, our_s_addr
)!=1)
186 if ( inet_addr(host
)==INADDR_NONE
)
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
);
194 hp
=(struct hostent
*)gethostbyname( host
);
197 if(verb
) mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_CantResolv
, af2String(af
), host
);
201 memcpy( our_s_addr
, (void*)hp
->h_addr
, hp
->h_length
);
205 unsigned long addr
= inet_addr(host
);
206 memcpy( our_s_addr
, (void*)&addr
, sizeof(addr
) );
212 server_address
.four
.sin_family
=af
;
213 server_address
.four
.sin_port
=htons(port
);
214 server_address_size
= sizeof(server_address
.four
);
218 server_address
.six
.sin6_family
=af
;
219 server_address
.six
.sin6_port
=htons(port
);
220 server_address_size
= sizeof(server_address
.six
);
224 mp_msg(MSGT_NETWORK
,MSGL_ERR
, MSGTR_MPDEMUX_NW_UnknownAF
, af
);
228 #if defined(USE_ATON) || defined(HAVE_WINSOCK2)
229 strncpy( buf
, inet_ntoa( *((struct in_addr
*)our_s_addr
) ), 255);
231 inet_ntop(af
, our_s_addr
, buf
, 255);
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
);
240 ioctlsocket( socket_server_fd
, FIONBIO
, &val
);
242 if( connect( socket_server_fd
, (struct sockaddr
*)&server_address
, server_address_size
)==-1 ) {
243 #ifndef HAVE_WINSOCK2
244 if( errno
!=EINPROGRESS
) {
246 if( (WSAGetLastError() != WSAEINPROGRESS
) && (WSAGetLastError() != WSAEWOULDBLOCK
) ) {
248 if(verb
) mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_CantConnect2Server
, af2String(af
));
249 closesocket(socket_server_fd
);
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)) {
263 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ConnTimeout
);
265 mp_msg(MSGT_NETWORK
,MSGL_V
,"Connection interuppted by user\n");
270 FD_SET( socket_server_fd
, &set
);
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
);
280 ioctlsocket( socket_server_fd
, FIONBIO
, &val
);
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
);
286 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_GetSockOptFailed
,strerror(errno
));
290 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ConnectError
,strerror(err
));
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
) {
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
;
315 return connect2Server_with_af(host
, port
, AF_INET
,verb
);
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
);
330 // Check if the http_proxy environment variable is set.
331 if( !strcasecmp(url
->protocol
, "http") ) {
333 proxy
= getenv("http_proxy");
335 // We got a proxy, build the URL to use it
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
);
348 if (network_ipv4_only_proxy
&& (gethostbyname(url
->hostname
)==NULL
)) {
349 mp_msg(MSGT_NETWORK
,MSGL_WARN
,
350 MSGTR_MPDEMUX_NW_CantResolvTryingWithoutProxy
);
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
);
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
) {
368 url_free( proxy_url
);
374 url_free( proxy_url
);
381 http_send_request( URL_t
*url
, off_t pos
) {
382 HTTP_header_t
*http_hdr
;
387 int proxy
= 0; // Boolean
389 http_hdr
= http_new_header();
391 if( !strcasecmp(url
->protocol
, "http_proxy") ) {
393 server_url
= url_new( (url
->file
)+1 );
394 http_set_uri( http_hdr
, 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
);
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
);
410 http_set_field( http_hdr
, "User-Agent: MPlayer/"VERSION
);
412 http_set_field(http_hdr
, "Icy-MetaData: 1");
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
) {
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
);
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 );
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
);
447 http_free( http_hdr
);
456 http_read_response( int fd
) {
457 HTTP_header_t
*http_hdr
;
458 char response
[BUFFER_SIZE
];
461 http_hdr
= http_new_header();
462 if( http_hdr
==NULL
) {
467 i
= recv( fd
, response
, BUFFER_SIZE
, 0 );
469 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ReadFailed
);
470 http_free( http_hdr
);
474 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_Read0CouldBeEOF
);
475 http_free( http_hdr
);
478 http_response_append( http_hdr
, response
, i
);
479 } while( !http_is_header_entire( http_hdr
) );
480 http_response_parse( http_hdr
);
485 http_authenticate(HTTP_header_t
*http_hdr
, URL_t
*url
, int *auth_retry
) {
488 if( *auth_retry
==1 ) {
489 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_AuthFailed
);
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");
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
);
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
);
519 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_AuthFailed
);
522 if( network_password
) {
523 url
->password
= strdup(network_password
);
524 if( url
->password
==NULL
) {
525 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
529 mp_msg(MSGT_NETWORK
,MSGL_INFO
,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank
);
536 http_seek( stream_t
*stream
, off_t pos
) {
537 HTTP_header_t
*http_hdr
= NULL
;
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
);
545 http_hdr
= http_read_response( fd
);
547 if( http_hdr
==NULL
) return 0;
549 switch( http_hdr
->status_code
) {
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
);
562 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ErrServerReturned
, http_hdr
->status_code
, http_hdr
->reason_phrase
);
569 http_free( http_hdr
);
570 stream
->streaming_ctrl
->data
= NULL
;
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
);
587 memcpy( streaming_ctrl
->buffer
, buffer
, size
);
588 streaming_ctrl
->buffer_size
= size
;
593 nop_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*stream_ctrl
) {
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 );
615 ret
= recv( fd
, buffer
+len
, size
-len
, 0 );
617 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_read error : %s\n",strerror(errno
));
620 //printf("read %d bytes from network\n", len );
627 nop_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*stream_ctrl
) {
629 // To shut up gcc warning
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
;