2001-05-29 17:03:17 +00:00
|
|
|
/*
|
|
|
|
* Network layer for MPlayer
|
|
|
|
* by Bertrand BAUDET <bertrand_baudet@yahoo.com>
|
|
|
|
* (C) 2001, MPlayer team.
|
|
|
|
*/
|
|
|
|
|
2001-06-05 08:49:11 +00:00
|
|
|
//#define DUMP2FILE
|
2001-06-04 17:52:33 +00:00
|
|
|
|
2001-08-01 09:14:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-05-18 16:14:06 +00:00
|
|
|
#include <unistd.h>
|
2001-08-01 09:14:02 +00:00
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
#include <errno.h>
|
2001-05-29 17:03:17 +00:00
|
|
|
#include <ctype.h>
|
2001-05-18 16:14:06 +00:00
|
|
|
|
2001-10-30 17:03:11 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-12-10 01:40:18 +00:00
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "help_mp.h"
|
|
|
|
|
2003-06-11 16:48:09 +00:00
|
|
|
#ifndef HAVE_WINSOCK2
|
|
|
|
#define closesocket close
|
|
|
|
#else
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#endif
|
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
#include "stream.h"
|
|
|
|
#include "demuxer.h"
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "m_config.h"
|
2001-06-04 17:52:33 +00:00
|
|
|
|
2001-05-20 12:58:41 +00:00
|
|
|
#include "network.h"
|
2001-05-29 17:03:17 +00:00
|
|
|
#include "http.h"
|
2003-12-08 13:25:35 +00:00
|
|
|
#include "cookies.h"
|
2001-05-29 17:03:17 +00:00
|
|
|
#include "url.h"
|
2001-05-20 12:58:41 +00:00
|
|
|
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "version.h"
|
2002-06-20 08:50:40 +00:00
|
|
|
|
2002-01-12 21:08:12 +00:00
|
|
|
extern int verbose;
|
2002-10-23 17:21:01 +00:00
|
|
|
extern int stream_cache_size;
|
2002-01-12 21:08:12 +00:00
|
|
|
|
2002-10-23 14:46:20 +00:00
|
|
|
extern int mp_input_check_interrupt(int time);
|
|
|
|
|
2003-12-08 13:25:35 +00:00
|
|
|
/* Variables for the command line option -user, -passwd, -bandwidth,
|
|
|
|
-user-agent and -nocookies */
|
|
|
|
|
2002-10-23 17:21:01 +00:00
|
|
|
char *network_username=NULL;
|
|
|
|
char *network_password=NULL;
|
|
|
|
int network_bandwidth=0;
|
2003-12-08 13:25:35 +00:00
|
|
|
int network_cookies_enabled = 0;
|
2003-10-22 17:54:32 +00:00
|
|
|
char *network_useragent=NULL;
|
2002-06-25 08:04:52 +00:00
|
|
|
|
2003-03-26 11:35:13 +00:00
|
|
|
/* IPv6 options */
|
|
|
|
int network_prefer_ipv4 = 0;
|
|
|
|
int network_ipv4_only_proxy = 0;
|
|
|
|
|
2002-06-25 08:04:52 +00:00
|
|
|
|
2005-05-29 12:54:00 +00:00
|
|
|
mime_struct_t mime_type_table[] = {
|
2001-11-20 22:20:20 +00:00
|
|
|
// MP3 streaming, some MP3 streaming server answer with audio/mpeg
|
2002-02-16 21:48:59 +00:00
|
|
|
{ "audio/mpeg", DEMUXER_TYPE_AUDIO },
|
2001-11-20 22:20:20 +00:00
|
|
|
// MPEG streaming
|
2004-11-21 12:16:14 +00:00
|
|
|
{ "video/mpeg", DEMUXER_TYPE_UNKNOWN },
|
|
|
|
{ "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
|
|
|
|
{ "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
|
2001-11-20 22:20:20 +00:00
|
|
|
// AVI ??? => video/x-msvideo
|
|
|
|
{ "video/x-msvideo", DEMUXER_TYPE_AVI },
|
|
|
|
// MOV => video/quicktime
|
|
|
|
{ "video/quicktime", DEMUXER_TYPE_MOV },
|
|
|
|
// ASF
|
|
|
|
{ "audio/x-ms-wax", DEMUXER_TYPE_ASF },
|
|
|
|
{ "audio/x-ms-wma", DEMUXER_TYPE_ASF },
|
|
|
|
{ "video/x-ms-asf", DEMUXER_TYPE_ASF },
|
|
|
|
{ "video/x-ms-afs", DEMUXER_TYPE_ASF },
|
|
|
|
{ "video/x-ms-wvx", DEMUXER_TYPE_ASF },
|
|
|
|
{ "video/x-ms-wmv", DEMUXER_TYPE_ASF },
|
|
|
|
{ "video/x-ms-wma", DEMUXER_TYPE_ASF },
|
2005-12-24 18:17:39 +00:00
|
|
|
{ "application/x-mms-framed", DEMUXER_TYPE_ASF },
|
2002-02-21 13:14:52 +00:00
|
|
|
// Playlists
|
2002-09-13 22:39:48 +00:00
|
|
|
{ "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
|
2002-02-21 13:14:52 +00:00
|
|
|
{ "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
|
|
|
|
{ "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
|
2002-05-09 14:52:23 +00:00
|
|
|
{ "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
|
|
|
|
// Real Media
|
2005-04-17 20:33:25 +00:00
|
|
|
// { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
|
2002-11-05 19:03:52 +00:00
|
|
|
// OGG Streaming
|
2004-04-12 14:19:12 +00:00
|
|
|
{ "application/x-ogg", DEMUXER_TYPE_OGG },
|
|
|
|
// NullSoft Streaming Video
|
2004-05-14 23:10:43 +00:00
|
|
|
{ "video/nsv", DEMUXER_TYPE_NSV},
|
2005-05-29 12:54:00 +00:00
|
|
|
{ "misc/ultravox", DEMUXER_TYPE_NSV},
|
|
|
|
{ NULL, DEMUXER_TYPE_UNKNOWN},
|
2001-11-20 22:20:20 +00:00
|
|
|
};
|
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
|
|
|
|
streaming_ctrl_t *
|
|
|
|
streaming_ctrl_new( ) {
|
|
|
|
streaming_ctrl_t *streaming_ctrl;
|
|
|
|
streaming_ctrl = (streaming_ctrl_t*)malloc(sizeof(streaming_ctrl_t));
|
|
|
|
if( streaming_ctrl==NULL ) {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2001-06-04 17:52:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) );
|
|
|
|
return streaming_ctrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
|
|
|
|
if( streaming_ctrl==NULL ) return;
|
2002-01-19 09:04:02 +00:00
|
|
|
if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
|
|
|
|
if( streaming_ctrl->buffer ) free( streaming_ctrl->buffer );
|
|
|
|
if( streaming_ctrl->data ) free( streaming_ctrl->data );
|
2001-06-04 17:52:33 +00:00
|
|
|
free( streaming_ctrl );
|
|
|
|
}
|
|
|
|
|
2003-03-26 11:35:13 +00:00
|
|
|
|
|
|
|
// Converts an address family constant to a string
|
|
|
|
|
|
|
|
char *af2String(int af) {
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET: return "AF_INET";
|
|
|
|
|
|
|
|
#ifdef HAVE_AF_INET6
|
|
|
|
case AF_INET6: return "AF_INET6";
|
|
|
|
#endif
|
|
|
|
default: return "Unknown address family!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Connect to a server using a TCP connection, with specified address family
|
2002-09-03 01:29:44 +00:00
|
|
|
// return -2 for fatal error, like unable to resolve name, connection timeout...
|
|
|
|
// return -1 is unable to connect to a particular port
|
2003-03-26 11:35:13 +00:00
|
|
|
|
2001-05-18 16:14:06 +00:00
|
|
|
int
|
2003-08-15 19:13:23 +00:00
|
|
|
connect2Server_with_af(char *host, int port, int af,int verb) {
|
2001-05-18 16:14:06 +00:00
|
|
|
int socket_server_fd;
|
2001-06-04 17:52:33 +00:00
|
|
|
int err, err_len;
|
2002-02-23 21:22:55 +00:00
|
|
|
int ret,count = 0;
|
2001-06-04 17:52:33 +00:00
|
|
|
fd_set set;
|
|
|
|
struct timeval tv;
|
2003-03-26 11:35:13 +00:00
|
|
|
union {
|
|
|
|
struct sockaddr_in four;
|
|
|
|
#ifdef HAVE_AF_INET6
|
|
|
|
struct sockaddr_in6 six;
|
|
|
|
#endif
|
|
|
|
} server_address;
|
|
|
|
size_t server_address_size;
|
|
|
|
void *our_s_addr; // Pointer to sin_addr or sin6_addr
|
2002-10-29 09:18:53 +00:00
|
|
|
struct hostent *hp=NULL;
|
2003-03-26 11:35:13 +00:00
|
|
|
char buf[255];
|
|
|
|
|
2003-06-11 16:48:09 +00:00
|
|
|
#ifdef HAVE_WINSOCK2
|
|
|
|
u_long val;
|
|
|
|
#endif
|
|
|
|
|
2003-03-26 11:35:13 +00:00
|
|
|
socket_server_fd = socket(af, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
|
2001-05-18 16:14:06 +00:00
|
|
|
if( socket_server_fd==-1 ) {
|
2003-10-22 17:36:47 +00:00
|
|
|
// mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af));
|
2002-09-03 01:29:44 +00:00
|
|
|
return -2;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
|
|
|
|
2003-03-26 11:35:13 +00:00
|
|
|
switch (af) {
|
|
|
|
case AF_INET: our_s_addr = (void *) &server_address.four.sin_addr; break;
|
|
|
|
#ifdef HAVE_AF_INET6
|
|
|
|
case AF_INET6: our_s_addr = (void *) &server_address.six.sin6_addr; break;
|
|
|
|
#endif
|
|
|
|
default:
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
|
2003-03-26 11:35:13 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-04 18:31:04 +00:00
|
|
|
memset(&server_address, 0, sizeof(server_address));
|
2003-03-26 11:35:13 +00:00
|
|
|
|
2003-06-11 16:48:09 +00:00
|
|
|
#ifndef HAVE_WINSOCK2
|
2003-03-26 11:35:13 +00:00
|
|
|
#ifdef USE_ATON
|
|
|
|
if (inet_aton(host, our_s_addr)!=1)
|
|
|
|
#else
|
|
|
|
if (inet_pton(af, host, our_s_addr)!=1)
|
2003-06-11 16:48:09 +00:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
if ( inet_addr(host)==INADDR_NONE )
|
2003-03-26 11:35:13 +00:00
|
|
|
#endif
|
|
|
|
{
|
2005-12-10 01:40:18 +00:00
|
|
|
if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ResolvingHostForAF, host, af2String(af));
|
2003-03-26 11:35:13 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_GETHOSTBYNAME2
|
|
|
|
hp=(struct hostent*)gethostbyname2( host, af );
|
|
|
|
#else
|
2001-11-20 22:20:20 +00:00
|
|
|
hp=(struct hostent*)gethostbyname( host );
|
2003-03-26 11:35:13 +00:00
|
|
|
#endif
|
2001-05-20 12:58:41 +00:00
|
|
|
if( hp==NULL ) {
|
2005-12-10 01:40:18 +00:00
|
|
|
if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantResolv, af2String(af), host);
|
2002-09-03 01:29:44 +00:00
|
|
|
return -2;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
2003-03-26 11:35:13 +00:00
|
|
|
|
|
|
|
memcpy( our_s_addr, (void*)hp->h_addr, hp->h_length );
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
2003-06-11 16:48:09 +00:00
|
|
|
#ifdef HAVE_WINSOCK2
|
|
|
|
else {
|
|
|
|
unsigned long addr = inet_addr(host);
|
|
|
|
memcpy( our_s_addr, (void*)&addr, sizeof(addr) );
|
|
|
|
}
|
|
|
|
#endif
|
2001-11-20 22:20:20 +00:00
|
|
|
|
2003-03-26 11:35:13 +00:00
|
|
|
switch (af) {
|
|
|
|
case AF_INET:
|
|
|
|
server_address.four.sin_family=af;
|
|
|
|
server_address.four.sin_port=htons(port);
|
|
|
|
server_address_size = sizeof(server_address.four);
|
|
|
|
break;
|
|
|
|
#ifdef HAVE_AF_INET6
|
|
|
|
case AF_INET6:
|
|
|
|
server_address.six.sin6_family=af;
|
|
|
|
server_address.six.sin6_port=htons(port);
|
|
|
|
server_address_size = sizeof(server_address.six);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af);
|
2003-03-26 11:35:13 +00:00
|
|
|
return -2;
|
2002-09-27 19:20:20 +00:00
|
|
|
}
|
2003-03-26 11:35:13 +00:00
|
|
|
|
2003-06-11 16:48:09 +00:00
|
|
|
#if defined(USE_ATON) || defined(HAVE_WINSOCK2)
|
2003-04-10 10:55:18 +00:00
|
|
|
strncpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255);
|
|
|
|
#else
|
|
|
|
inet_ntop(af, our_s_addr, buf, 255);
|
|
|
|
#endif
|
2005-12-10 01:40:18 +00:00
|
|
|
if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ConnectingToServer, host, buf , port );
|
2003-03-26 11:35:13 +00:00
|
|
|
|
|
|
|
// Turn the socket as non blocking so we can timeout on the connection
|
2003-06-11 16:48:09 +00:00
|
|
|
#ifndef HAVE_WINSOCK2
|
2001-11-20 22:20:20 +00:00
|
|
|
fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK );
|
2003-06-11 16:48:09 +00:00
|
|
|
#else
|
|
|
|
val = 1;
|
|
|
|
ioctlsocket( socket_server_fd, FIONBIO, &val );
|
|
|
|
#endif
|
2003-03-26 11:35:13 +00:00
|
|
|
if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) {
|
2003-06-11 16:48:09 +00:00
|
|
|
#ifndef HAVE_WINSOCK2
|
2001-06-04 17:52:33 +00:00
|
|
|
if( errno!=EINPROGRESS ) {
|
2003-06-11 16:48:09 +00:00
|
|
|
#else
|
|
|
|
if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) {
|
|
|
|
#endif
|
2005-12-10 01:40:18 +00:00
|
|
|
if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantConnect2Server, af2String(af));
|
2003-06-11 16:48:09 +00:00
|
|
|
closesocket(socket_server_fd);
|
2001-06-04 17:52:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2002-06-21 07:06:46 +00:00
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 500000;
|
2001-06-04 17:52:33 +00:00
|
|
|
FD_ZERO( &set );
|
|
|
|
FD_SET( socket_server_fd, &set );
|
2001-11-20 22:20:20 +00:00
|
|
|
// When the connection will be made, we will have a writable fd
|
2002-02-23 21:22:55 +00:00
|
|
|
while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) {
|
2005-12-10 01:40:18 +00:00
|
|
|
if( ret<0 ) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_SelectFailed);
|
2002-02-23 21:22:55 +00:00
|
|
|
else if(ret > 0) break;
|
2002-10-23 14:46:20 +00:00
|
|
|
else if(count > 30 || mp_input_check_interrupt(500)) {
|
2002-06-21 07:06:46 +00:00
|
|
|
if(count > 30)
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnTimeout);
|
2002-02-23 21:22:55 +00:00
|
|
|
else
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Connection interuppted by user\n");
|
2003-01-06 16:43:42 +00:00
|
|
|
return -3;
|
2002-02-23 21:22:55 +00:00
|
|
|
}
|
|
|
|
count++;
|
|
|
|
FD_ZERO( &set );
|
|
|
|
FD_SET( socket_server_fd, &set );
|
2002-06-21 07:06:46 +00:00
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 500000;
|
2001-05-18 16:14:06 +00:00
|
|
|
}
|
2001-11-20 22:20:20 +00:00
|
|
|
|
|
|
|
// Turn back the socket as blocking
|
2003-06-11 16:48:09 +00:00
|
|
|
#ifndef HAVE_WINSOCK2
|
2001-11-20 22:20:20 +00:00
|
|
|
fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK );
|
2003-06-11 16:48:09 +00:00
|
|
|
#else
|
|
|
|
val = 0;
|
|
|
|
ioctlsocket( socket_server_fd, FIONBIO, &val );
|
|
|
|
#endif
|
2001-12-14 20:45:30 +00:00
|
|
|
// Check if there were any error
|
|
|
|
err_len = sizeof(int);
|
|
|
|
ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len);
|
|
|
|
if(ret < 0) {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_GetSockOptFailed,strerror(errno));
|
2002-09-03 01:29:44 +00:00
|
|
|
return -2;
|
2001-12-14 20:45:30 +00:00
|
|
|
}
|
|
|
|
if(err > 0) {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnectError,strerror(err));
|
2001-12-14 20:45:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2003-03-26 11:35:13 +00:00
|
|
|
|
2001-05-18 16:14:06 +00:00
|
|
|
return socket_server_fd;
|
|
|
|
}
|
|
|
|
|
2003-03-26 11:35:13 +00:00
|
|
|
// Connect to a server using a TCP connection
|
|
|
|
// return -2 for fatal error, like unable to resolve name, connection timeout...
|
|
|
|
// return -1 is unable to connect to a particular port
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2003-08-15 19:13:23 +00:00
|
|
|
connect2Server(char *host, int port, int verb) {
|
2003-03-26 11:35:13 +00:00
|
|
|
#ifdef HAVE_AF_INET6
|
|
|
|
int r;
|
|
|
|
int s = -2;
|
|
|
|
|
2003-08-15 19:13:23 +00:00
|
|
|
r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb);
|
2003-03-26 11:35:13 +00:00
|
|
|
if (r > -1) return r;
|
|
|
|
|
2003-08-15 19:13:23 +00:00
|
|
|
s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb);
|
2003-03-26 11:35:13 +00:00
|
|
|
if (s == -2) return r;
|
|
|
|
return s;
|
|
|
|
#else
|
2003-08-15 19:13:23 +00:00
|
|
|
return connect2Server_with_af(host, port, AF_INET,verb);
|
2003-03-26 11:35:13 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-01-14 06:44:30 +00:00
|
|
|
URL_t*
|
|
|
|
check4proxies( URL_t *url ) {
|
2002-02-11 05:16:09 +00:00
|
|
|
URL_t *url_out = NULL;
|
2002-01-19 09:04:02 +00:00
|
|
|
if( url==NULL ) return NULL;
|
2002-02-11 05:16:09 +00:00
|
|
|
url_out = url_new( url->url );
|
2002-01-14 06:44:30 +00:00
|
|
|
if( !strcasecmp(url->protocol, "http_proxy") ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
|
2002-02-11 05:16:09 +00:00
|
|
|
return url_out;
|
2002-01-14 06:44:30 +00:00
|
|
|
}
|
|
|
|
// Check if the http_proxy environment variable is set.
|
|
|
|
if( !strcasecmp(url->protocol, "http") ) {
|
|
|
|
char *proxy;
|
|
|
|
proxy = getenv("http_proxy");
|
|
|
|
if( proxy!=NULL ) {
|
|
|
|
// We got a proxy, build the URL to use it
|
|
|
|
int len;
|
|
|
|
char *new_url;
|
|
|
|
URL_t *tmp_url;
|
|
|
|
URL_t *proxy_url = url_new( proxy );
|
|
|
|
|
|
|
|
if( proxy_url==NULL ) {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_WARN,
|
|
|
|
MSGTR_MPDEMUX_NW_InvalidProxySettingTryingWithout);
|
2002-02-11 05:16:09 +00:00
|
|
|
return url_out;
|
2002-01-14 06:44:30 +00:00
|
|
|
}
|
2003-03-26 11:35:13 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_AF_INET6
|
|
|
|
if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_WARN,
|
2005-12-10 01:40:18 +00:00
|
|
|
MSGTR_MPDEMUX_NW_CantResolvTryingWithoutProxy);
|
2005-09-06 21:14:55 +00:00
|
|
|
url_free(proxy_url);
|
2003-03-26 11:35:13 +00:00
|
|
|
return url_out;
|
|
|
|
}
|
|
|
|
#endif
|
2002-01-14 06:44:30 +00:00
|
|
|
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
|
2002-01-14 06:44:30 +00:00
|
|
|
len = strlen( proxy_url->hostname ) + strlen( url->url ) + 20; // 20 = http_proxy:// + port
|
|
|
|
new_url = malloc( len+1 );
|
|
|
|
if( new_url==NULL ) {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2005-09-06 21:14:55 +00:00
|
|
|
url_free(proxy_url);
|
2002-02-11 05:16:09 +00:00
|
|
|
return url_out;
|
2002-01-14 06:44:30 +00:00
|
|
|
}
|
2002-04-30 16:58:54 +00:00
|
|
|
sprintf(new_url, "http_proxy://%s:%d/%s", proxy_url->hostname, proxy_url->port, url->url );
|
2002-01-14 06:44:30 +00:00
|
|
|
tmp_url = url_new( new_url );
|
|
|
|
if( tmp_url==NULL ) {
|
2005-09-06 21:14:55 +00:00
|
|
|
free( new_url );
|
|
|
|
url_free( proxy_url );
|
2002-02-11 05:16:09 +00:00
|
|
|
return url_out;
|
2002-01-14 06:44:30 +00:00
|
|
|
}
|
2002-02-11 05:16:09 +00:00
|
|
|
url_free( url_out );
|
|
|
|
url_out = tmp_url;
|
2002-01-14 06:44:30 +00:00
|
|
|
free( new_url );
|
|
|
|
url_free( proxy_url );
|
|
|
|
}
|
|
|
|
}
|
2002-02-11 05:16:09 +00:00
|
|
|
return url_out;
|
2002-01-14 06:44:30 +00:00
|
|
|
}
|
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
int
|
2004-02-17 12:30:45 +00:00
|
|
|
http_send_request( URL_t *url, off_t pos ) {
|
2001-06-04 17:52:33 +00:00
|
|
|
HTTP_header_t *http_hdr;
|
2002-01-12 21:08:12 +00:00
|
|
|
URL_t *server_url;
|
2003-10-22 17:54:32 +00:00
|
|
|
char str[256];
|
2001-06-04 17:52:33 +00:00
|
|
|
int fd;
|
2002-01-12 21:08:12 +00:00
|
|
|
int ret;
|
|
|
|
int proxy = 0; // Boolean
|
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
http_hdr = http_new_header();
|
2002-01-12 21:08:12 +00:00
|
|
|
|
2002-01-14 01:12:44 +00:00
|
|
|
if( !strcasecmp(url->protocol, "http_proxy") ) {
|
2002-01-12 21:08:12 +00:00
|
|
|
proxy = 1;
|
|
|
|
server_url = url_new( (url->file)+1 );
|
|
|
|
http_set_uri( http_hdr, server_url->url );
|
|
|
|
} else {
|
|
|
|
server_url = url;
|
|
|
|
http_set_uri( http_hdr, server_url->file );
|
|
|
|
}
|
2004-06-25 15:14:27 +00:00
|
|
|
if (server_url->port && server_url->port != 80)
|
|
|
|
snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port );
|
|
|
|
else
|
|
|
|
snprintf(str, 256, "Host: %s", server_url->hostname );
|
2001-12-18 18:45:00 +00:00
|
|
|
http_set_field( http_hdr, str);
|
2003-10-22 17:54:32 +00:00
|
|
|
if (network_useragent)
|
|
|
|
{
|
|
|
|
snprintf(str, 256, "User-Agent: %s", network_useragent);
|
|
|
|
http_set_field(http_hdr, str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION);
|
2004-02-17 12:30:45 +00:00
|
|
|
|
2005-07-19 12:07:04 +00:00
|
|
|
http_set_field(http_hdr, "Icy-MetaData: 1");
|
|
|
|
|
2004-02-17 12:30:45 +00:00
|
|
|
if(pos>0) {
|
|
|
|
// Extend http_send_request with possibility to do partial content retrieval
|
2005-10-13 18:33:56 +00:00
|
|
|
snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos);
|
2004-02-17 12:30:45 +00:00
|
|
|
http_set_field(http_hdr, str);
|
|
|
|
}
|
2003-12-08 13:25:35 +00:00
|
|
|
|
|
|
|
if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
|
|
|
|
|
2004-04-29 21:34:50 +00:00
|
|
|
http_set_field( http_hdr, "Connection: close");
|
2002-06-23 09:17:52 +00:00
|
|
|
http_add_basic_authentication( http_hdr, url->username, url->password );
|
2001-06-04 17:52:33 +00:00
|
|
|
if( http_build_request( http_hdr )==NULL ) {
|
2005-09-06 21:00:57 +00:00
|
|
|
goto err_out;
|
2001-06-04 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
2002-01-12 21:08:12 +00:00
|
|
|
if( proxy ) {
|
|
|
|
if( url->port==0 ) url->port = 8080; // Default port for the proxy server
|
2003-08-15 19:13:23 +00:00
|
|
|
fd = connect2Server( url->hostname, url->port,1 );
|
2002-01-12 21:08:12 +00:00
|
|
|
url_free( server_url );
|
|
|
|
} else {
|
|
|
|
if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
|
2003-08-15 19:13:23 +00:00
|
|
|
fd = connect2Server( server_url->hostname, server_url->port,1 );
|
2002-01-12 21:08:12 +00:00
|
|
|
}
|
2001-06-04 17:52:33 +00:00
|
|
|
if( fd<0 ) {
|
2005-09-06 21:00:57 +00:00
|
|
|
goto err_out;
|
2001-06-04 17:52:33 +00:00
|
|
|
}
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
|
2002-01-12 21:08:12 +00:00
|
|
|
|
2003-05-29 19:36:58 +00:00
|
|
|
ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 );
|
2002-10-29 09:18:53 +00:00
|
|
|
if( ret!=(int)http_hdr->buffer_size ) {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest);
|
2005-09-06 21:00:57 +00:00
|
|
|
goto err_out;
|
2002-01-12 21:08:12 +00:00
|
|
|
}
|
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
http_free( http_hdr );
|
|
|
|
|
|
|
|
return fd;
|
2005-09-06 21:00:57 +00:00
|
|
|
err_out:
|
|
|
|
http_free(http_hdr);
|
|
|
|
return -1;
|
2001-06-04 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HTTP_header_t *
|
|
|
|
http_read_response( int fd ) {
|
|
|
|
HTTP_header_t *http_hdr;
|
|
|
|
char response[BUFFER_SIZE];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
http_hdr = http_new_header();
|
|
|
|
if( http_hdr==NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2003-05-29 19:36:58 +00:00
|
|
|
i = recv( fd, response, BUFFER_SIZE, 0 );
|
2001-06-04 17:52:33 +00:00
|
|
|
if( i<0 ) {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ReadFailed);
|
2001-12-07 07:17:05 +00:00
|
|
|
http_free( http_hdr );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if( i==0 ) {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_Read0CouldBeEOF);
|
2001-12-07 07:17:05 +00:00
|
|
|
http_free( http_hdr );
|
|
|
|
return NULL;
|
2001-06-04 17:52:33 +00:00
|
|
|
}
|
|
|
|
http_response_append( http_hdr, response, i );
|
2001-10-26 18:26:06 +00:00
|
|
|
} while( !http_is_header_entire( http_hdr ) );
|
2001-06-04 17:52:33 +00:00
|
|
|
http_response_parse( http_hdr );
|
|
|
|
return http_hdr;
|
|
|
|
}
|
|
|
|
|
2002-06-24 20:12:18 +00:00
|
|
|
int
|
|
|
|
http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
|
|
|
|
char *aut;
|
2002-10-29 09:18:53 +00:00
|
|
|
|
2002-06-25 08:04:52 +00:00
|
|
|
if( *auth_retry==1 ) {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
|
2002-06-24 20:12:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if( *auth_retry>0 ) {
|
|
|
|
if( url->username ) {
|
|
|
|
free( url->username );
|
|
|
|
url->username = NULL;
|
|
|
|
}
|
|
|
|
if( url->password ) {
|
|
|
|
free( url->password );
|
|
|
|
url->password = NULL;
|
|
|
|
}
|
|
|
|
}
|
2002-06-25 08:04:52 +00:00
|
|
|
|
2002-06-24 20:12:18 +00:00
|
|
|
aut = http_get_field(http_hdr, "WWW-Authenticate");
|
|
|
|
if( aut!=NULL ) {
|
|
|
|
char *aut_space;
|
|
|
|
aut_space = strstr(aut, "realm=");
|
|
|
|
if( aut_space!=NULL ) aut_space += 6;
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequiredFor, aut_space);
|
2002-06-24 20:12:18 +00:00
|
|
|
} else {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequired);
|
2002-06-24 20:12:18 +00:00
|
|
|
}
|
2002-10-23 17:21:01 +00:00
|
|
|
if( network_username ) {
|
|
|
|
url->username = strdup(network_username);
|
2002-06-24 20:12:18 +00:00
|
|
|
if( url->username==NULL ) {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2002-06-24 20:12:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
|
2002-06-24 20:12:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-10-23 17:21:01 +00:00
|
|
|
if( network_password ) {
|
|
|
|
url->password = strdup(network_password);
|
2002-06-25 08:04:52 +00:00
|
|
|
if( url->password==NULL ) {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2002-06-25 08:04:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank);
|
2002-06-25 08:04:52 +00:00
|
|
|
}
|
2002-06-24 20:12:18 +00:00
|
|
|
(*auth_retry)++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-02-17 12:30:45 +00:00
|
|
|
int
|
|
|
|
http_seek( stream_t *stream, off_t pos ) {
|
|
|
|
HTTP_header_t *http_hdr = NULL;
|
|
|
|
int fd;
|
|
|
|
if( stream==NULL ) return 0;
|
|
|
|
|
|
|
|
if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
|
|
|
|
fd = http_send_request( stream->streaming_ctrl->url, pos );
|
|
|
|
if( fd<0 ) return 0;
|
|
|
|
|
|
|
|
http_hdr = http_read_response( fd );
|
|
|
|
|
|
|
|
if( http_hdr==NULL ) return 0;
|
|
|
|
|
|
|
|
switch( http_hdr->status_code ) {
|
|
|
|
case 200:
|
|
|
|
case 206: // OK
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
|
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
|
|
|
|
if( http_hdr->body_size>0 ) {
|
|
|
|
if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
|
|
|
|
http_free( http_hdr );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase );
|
2004-02-17 12:30:45 +00:00
|
|
|
close( fd );
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
stream->fd = fd;
|
|
|
|
|
|
|
|
if( http_hdr ) {
|
|
|
|
http_free( http_hdr );
|
|
|
|
stream->streaming_ctrl->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->pos=pos;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
int
|
2001-11-20 22:20:20 +00:00
|
|
|
streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
|
2002-01-19 09:04:02 +00:00
|
|
|
//printf("streaming_bufferize\n");
|
2001-11-20 22:20:20 +00:00
|
|
|
streaming_ctrl->buffer = (char*)malloc(size);
|
|
|
|
if( streaming_ctrl->buffer==NULL ) {
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
|
2001-11-20 22:20:20 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy( streaming_ctrl->buffer, buffer, size );
|
|
|
|
streaming_ctrl->buffer_size = size;
|
2001-12-19 01:59:25 +00:00
|
|
|
return size;
|
2001-11-20 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
|
|
|
|
int len=0;
|
|
|
|
//printf("nop_streaming_read\n");
|
|
|
|
if( stream_ctrl->buffer_size!=0 ) {
|
|
|
|
int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
|
2002-01-19 09:04:02 +00:00
|
|
|
//printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
|
2001-11-20 22:20:20 +00:00
|
|
|
len = (size<buffer_len)?size:buffer_len;
|
|
|
|
memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
|
|
|
|
stream_ctrl->buffer_pos += len;
|
2002-01-19 09:04:02 +00:00
|
|
|
//printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
|
2001-11-20 22:20:20 +00:00
|
|
|
if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
|
|
|
|
free( stream_ctrl->buffer );
|
|
|
|
stream_ctrl->buffer = NULL;
|
|
|
|
stream_ctrl->buffer_size = 0;
|
|
|
|
stream_ctrl->buffer_pos = 0;
|
2002-01-19 09:04:02 +00:00
|
|
|
//printf("buffer cleaned\n");
|
2001-11-20 22:20:20 +00:00
|
|
|
}
|
2002-01-19 09:04:02 +00:00
|
|
|
//printf("read %d bytes from buffer\n", len );
|
2001-11-20 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( len<size ) {
|
2001-12-07 07:17:05 +00:00
|
|
|
int ret;
|
2003-05-29 19:36:58 +00:00
|
|
|
ret = recv( fd, buffer+len, size-len, 0 );
|
2001-12-14 20:45:30 +00:00
|
|
|
if( ret<0 ) {
|
2002-04-30 16:58:54 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
|
2001-12-07 07:17:05 +00:00
|
|
|
}
|
|
|
|
len += ret;
|
2001-11-20 22:20:20 +00:00
|
|
|
//printf("read %d bytes from network\n", len );
|
|
|
|
}
|
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
return len;
|
|
|
|
}
|
2001-05-29 17:03:17 +00:00
|
|
|
|
2001-06-04 17:52:33 +00:00
|
|
|
int
|
2001-11-20 22:20:20 +00:00
|
|
|
nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
|
|
|
|
return -1;
|
2002-10-29 09:18:53 +00:00
|
|
|
// To shut up gcc warning
|
|
|
|
fd++;
|
|
|
|
pos++;
|
|
|
|
stream_ctrl=NULL;
|
2001-11-20 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
2001-12-23 22:09:02 +00:00
|
|
|
|
2005-07-10 14:09:45 +00:00
|
|
|
void fixup_network_stream_cache(stream_t *stream) {
|
2005-05-29 12:54:00 +00:00
|
|
|
if(stream->streaming_ctrl->buffering) {
|
|
|
|
if(stream_cache_size<0) {
|
|
|
|
// cache option not set, will use our computed value.
|
|
|
|
// buffer in KBytes, *5 because the prefill is 20% of the buffer.
|
|
|
|
stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
|
|
|
|
if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
|
|
|
|
}
|
2005-12-10 01:40:18 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_CacheSizeSetTo, stream_cache_size);
|
2005-05-29 12:54:00 +00:00
|
|
|
}
|
2001-12-23 22:09:02 +00:00
|
|
|
}
|
|
|
|
|
2001-05-29 17:03:17 +00:00
|
|
|
|
|
|
|
int
|
2001-11-20 22:20:20 +00:00
|
|
|
streaming_stop( stream_t *stream ) {
|
|
|
|
stream->streaming_ctrl->status = streaming_stopped_e;
|
2001-06-04 17:52:33 +00:00
|
|
|
return 0;
|
2001-05-29 17:03:17 +00:00
|
|
|
}
|