Beispiel #1
0
static int FtpOpenPort(struct stream_priv_s* p) {
  int resp,fd;
  char rsp_txt[256];
  char* par,str[128];
  int num[6];

  resp = FtpSendCmd("PASV",p,rsp_txt);
  if(resp != 2) {
    mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command 'PASV' failed: %s\n",rsp_txt);
    return 0;
  }
  
  par = strchr(rsp_txt,'(');
  
  if(!par || !par[0] || !par[1]) {
    mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] invalid server response: %s ??\n",rsp_txt);
    return 0;
  }

  sscanf(par+1,"%u,%u,%u,%u,%u,%u",&num[0],&num[1],&num[2],
	 &num[3],&num[4],&num[5]);
  snprintf(str,127,"%d.%d.%d.%d",num[0],num[1],num[2],num[3]);
  fd = connect2Server(str,(num[4]<<8)+num[5],0);

  if(fd < 0)
    mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] failed to create data connection\n");

  return fd;
}
Beispiel #2
0
bool DeleteVM::applySC2FreeDisk()
{
	String request = "DEL NFS STOR:ID=" + vmID + ",TYPE=0";
	int sock = connect2Server( scServerIP.c_str(), scServerPort);
	if( sock == -1 )
	{
		log.write("connect sc node server error.", Log::ERROR);
		return false;
	}

	if( sendData( request.c_str(), request.size(), sock) == false )
	{
		log.write("send request to sc node server error.", Log::ERROR);
		return false;
	}
	String ack;

	if( recvData( sock, ack) == false )
	{
		log.write("recv ack message from sc node server error.", Log::ERROR);
		return false;
	}
	log.write("recv ack message:" + ack + " from sc node server.", Log::INFO);

	
	//分析结果
	int iRetn;
	if( getIntParaValue( ack, "retn", iRetn) == false )
	{
		log.write("get para ip error from ack message.", Log::ERROR);
		return false;
	}
	closeSock( sock );
	
	if( iRetn == 0 )
	{
		log.write("apply sc server to free disk success.", Log::INFO);
		return true;
	}
	

	log.write("apply sc server to free disk failed.", Log::ERROR);
	return false;
}
Beispiel #3
0
int
http_send_request( URL_t *url, off_t pos ) {
	HTTP_header_t *http_hdr;
	URL_t *server_url;
	char str[256];
	int fd = -1;
	int ret;
	int proxy = 0;		// Boolean

	http_hdr = http_new_header();

	if( !strcasecmp(url->protocol, "http_proxy") ) {
		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 );
	}
	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 );
	http_set_field( http_hdr, str);
	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);

	if( strcasecmp(url->protocol, "noicyx") )
	    http_set_field(http_hdr, "Icy-MetaData: 1");

	if(pos>0) {
	// Extend http_send_request with possibility to do partial content retrieval
	    snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos);
	    http_set_field(http_hdr, str);
	}

	if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );

	http_set_field( http_hdr, "Connection: close");
	http_add_basic_authentication( http_hdr, url->username, url->password );
	if( http_build_request( http_hdr )==NULL ) {
		goto err_out;
	}

	if( proxy ) {
		if( url->port==0 ) url->port = 8080;			// Default port for the proxy server
		fd = connect2Server( url->hostname, url->port,1 );
		url_free( server_url );
		server_url = NULL;
	} else {
		if( server_url->port==0 ) server_url->port = 80;	// Default port for the web server
		fd = connect2Server( server_url->hostname, server_url->port,1 );
	}
	if( fd<0 ) {
		goto err_out;
	}
	mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );

	ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 );
	if( ret!=(int)http_hdr->buffer_size ) {
		mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest);
		goto err_out;
	}

	http_free( http_hdr );

	return fd;
err_out:
	if (fd > 0) closesocket(fd);
	http_free(http_hdr);
	if (proxy && server_url)
		url_free(server_url);
	return -1;
}
Beispiel #4
0
static int open_s(stream_t *stream, int mode, void* opts, int* file_format) {
  int f;
  struct stream_priv_s* p = (struct stream_priv_s*)opts;

  if(mode != STREAM_READ)
    return STREAM_UNSUPORTED;

  if(!p->host) {
    mp_msg(MSGT_OPEN, MSGL_ERR, "We need a host name (ex: tivo://hostname/fsid)\n");
    m_struct_free(&stream_opts, opts);
    return STREAM_ERROR;
  }

  if(!p->fsid || strlen(p->fsid) == 0) {
    mp_msg(MSGT_OPEN, MSGL_ERR, "We need an fsid (ex: tivo://hostname/fsid)\n");
    m_struct_free(&stream_opts, opts);
    return STREAM_ERROR;
  }

  f = connect2Server(p->host, VSERVER_PORT, 1);

  if(f < 0) {
    mp_msg(MSGT_OPEN, MSGL_ERR, "Connection to %s failed\n", p->host);
    m_struct_free(&stream_opts, opts);
    return STREAM_ERROR;
  }
  stream->fd = f;

  vstream_set_socket_fd(f);

  if (!strcmp(p->fsid, "list")) {
    vstream_list_streams(0);
    return STREAM_ERROR;
  } else if (!strcmp(p->fsid, "llist")) {
    vstream_list_streams(1);
    return STREAM_ERROR;
  }

  if (vstream_start()) {
    mp_msg(MSGT_OPEN, MSGL_ERR, "Cryptic internal error #1\n");
    m_struct_free(&stream_opts, opts);
    return STREAM_ERROR;
  }
  if (vstream_startstream(p->fsid)) {
    mp_msg(MSGT_OPEN, MSGL_ERR, "Cryptic internal error #2\n");
    m_struct_free(&stream_opts, opts);
    return STREAM_ERROR;
  }
  
  stream->start_pos = 0;
  stream->end_pos = vstream_streamsize();
  mp_msg(MSGT_OPEN, MSGL_DBG2, "Tivo stream size is %d\n", stream->end_pos);

  stream->priv = p;
  stream->fill_buffer = fill_buffer;
  stream->control = control;
  stream->seek = seek;
  stream->close = close_s;
  stream->type = STREAMTYPE_VSTREAM;

  return STREAM_OK;
}
Beispiel #5
0
int
http_send_request( URL_t *url, int64_t pos ) {
	HTTP_header_t *http_hdr;
	URL_t *server_url;
	char str[256];
	int fd = -1;
	int ret;
	int proxy = 0;		// Boolean

	http_hdr = http_new_header();

	if( !strcasecmp(url->protocol, "http_proxy") ) {
		proxy = 1;
		server_url = url_new( (url->file)+1 );
		if (!server_url) {
			mp_msg(MSGT_NETWORK, MSGL_ERR, "Invalid URL '%s' to proxify\n", url->file+1);
			goto err_out;
		}
		http_set_uri( http_hdr, server_url->noauth_url );
	} else {
		server_url = url;
		http_set_uri( http_hdr, server_url->file );
	}
	if (server_url->port && server_url->port != 80)
	    snprintf(str, sizeof(str), "Host: %s:%d", server_url->hostname, server_url->port );
	else
	    snprintf(str, sizeof(str), "Host: %s", server_url->hostname );
	http_set_field( http_hdr, str);
	if (network_useragent)
	    snprintf(str, sizeof(str), "User-Agent: %s", network_useragent);
	else
	    snprintf(str, sizeof(str), "User-Agent: %s", mplayer_version);
        http_set_field(http_hdr, str);

	if (network_referrer) {
	    char *referrer = NULL;
	    size_t len = strlen(network_referrer) + 10;

	    // Check len to ensure we don't do something really bad in case of an overflow
	    if (len > 10)
		referrer = malloc(len);

	    if (referrer == NULL) {
		mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MemAllocFailed);
	    } else {
		snprintf(referrer, len, "Referer: %s", network_referrer);
		http_set_field(http_hdr, referrer);
		free(referrer);
	    }
	}

	if( strcasecmp(url->protocol, "noicyx") )
	    http_set_field(http_hdr, "Icy-MetaData: 1");

	if(pos>0) {
	// Extend http_send_request with possibility to do partial content retrieval
	    snprintf(str, sizeof(str), "Range: bytes=%"PRId64"-", (int64_t)pos);
	    http_set_field(http_hdr, str);
	}

	if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );

	if (network_http_header_fields) {
		int i=0;
		while (network_http_header_fields[i])
			http_set_field(http_hdr, network_http_header_fields[i++]);
	}

	http_set_field( http_hdr, "Connection: close");
	if (proxy)
		http_add_basic_proxy_authentication(http_hdr, url->username, url->password);
	http_add_basic_authentication(http_hdr, server_url->username, server_url->password);
	if( http_build_request( http_hdr )==NULL ) {
		goto err_out;
	}

	if( proxy ) {
		if( url->port==0 ) url->port = 8080;			// Default port for the proxy server
		fd = connect2Server( url->hostname, url->port,1 );
		url_free( server_url );
		server_url = NULL;
	} else {
		if( server_url->port==0 ) server_url->port = 80;	// Default port for the web server
		fd = connect2Server( server_url->hostname, server_url->port,1 );
	}
	if( fd<0 ) {
		goto err_out;
	}
	mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );

	ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, DEFAULT_SEND_FLAGS );
	if( ret!=(int)http_hdr->buffer_size ) {
		mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest);
		goto err_out;
	}

	http_free( http_hdr );

	return fd;
err_out:
	if (fd > 0) closesocket(fd);
	http_free(http_hdr);
	if (proxy && server_url)
		url_free(server_url);
	return -1;
}
Beispiel #6
0
static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
  int f;
  struct stream_priv_s* p = (struct stream_priv_s*)opts;
  mp_net_stream_packet_t* pack;
  mp_net_stream_opened_t* opened;

  if(mode != STREAM_READ)
    return STREAM_UNSUPPORTED;

  if(!p->host) {
    mp_msg(MSGT_OPEN,MSGL_ERR, "We need an host name (ex: mpst://server.net/cdda://5)\n");
    m_struct_free(&stream_opts,opts);
    return STREAM_ERROR;
  }
  if(!p->url || strlen(p->url) == 0) {
    mp_msg(MSGT_OPEN,MSGL_ERR, "We need a remote url (ex: mpst://server.net/cdda://5)\n");
    m_struct_free(&stream_opts,opts);
    return STREAM_ERROR;
  }

  f = connect2Server(p->host,p->port,1);
  if(f < 0) {
    mp_msg(MSGT_OPEN,MSGL_ERR, "Connection to %s:%d failed\n",p->host,p->port);
    m_struct_free(&stream_opts,opts);
    return STREAM_ERROR;
  }
  stream->fd = f;
  /// Now send an open command
  pack = send_net_stream_cmd(stream,NET_STREAM_OPEN,p->url,strlen(p->url) + 1);
  if(!pack) {
    goto error;
  }

  if(pack->len != sizeof(mp_net_stream_packet_t) +
     sizeof(mp_net_stream_opened_t)) {
    mp_msg(MSGT_OPEN,MSGL_ERR, "Invalid open response packet len (%d bytes)\n",pack->len);
    free(pack);
    goto error;
  }

  opened = (mp_net_stream_opened_t*)pack->data;
  net_stream_opened_2_me(opened);

  *file_format = opened->file_format;
  stream->flags = opened->flags;
  stream->sector_size = opened->sector_size;
  stream->start_pos = opened->start_pos;
  stream->end_pos = opened->end_pos;

  stream->fill_buffer = fill_buffer;
  stream->control = control;
  if(stream->flags & MP_STREAM_SEEK)
    stream->seek = seek;
  stream->close = close_s;

  free(pack);
  m_struct_free(&stream_opts,opts);

  return STREAM_OK;

  error:
  closesocket(f);
  m_struct_free(&stream_opts,opts);
  return STREAM_ERROR;
}
Beispiel #7
0
int asf_mmst_streaming_start(stream_t *stream)
{
    char                 str[1024];
    char                 data[BUF_SIZE];
    uint8_t              asf_header[HDR_BUF_SIZE];
    int                  asf_header_len;
    int                  i, packet_length;
    char                *path, *unescpath;
    URL_t *url1 = stream->streaming_ctrl->url;
    int s = stream->fd;

    if( s>0 ) {
        closesocket( stream->fd );
        stream->fd = -1;
    }

    /* parse url */
    path = strchr(url1->file,'/') + 1;

    /* mmst filename are not url_escaped by MS MediaPlayer and are expected as
     * "plain text" by the server, so need to decode it here
     */
    unescpath=malloc(strlen(path)+1);
    if (!unescpath) {
        mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
        return -1;
    }
    url_unescape_string(unescpath,path);
    path=unescpath;


    if( url1->port==0 ) {
        url1->port=1755;
    }
    s = connect2Server( url1->hostname, url1->port, 1);
    if( s<0 ) {
        free(path);
        return s;
    }
    mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Connected\n");

    seq_num=0;

    /*
    * Send the initial connect info including player version no. Client GUID (random) and the host address being connected to.
    * This command is sent at the very start of protocol initiation. It sends local information to the serve
    * cmd 1 0x01
    * */

    /* prepare for the url encoding conversion */
#ifdef CONFIG_ICONV
    url_conv = iconv_open("UTF-16LE", "UTF-8");
#endif

    snprintf (str, 1023, "\034\003NSPlayer/7.0.0.1956; {33715801-BAB3-9D85-24E9-03B90328270A}; Host: %s", url1->hostname);
    string_utf16 (data, str, strlen(str));
// send_command(s, commandno ....)
    send_command (s, 1, 0, 0x0004000b, strlen(str)*2+2, data);

    recv (s, data, BUF_SIZE, 0) ;

    /*This sends details of the local machine IP address to a Funnel system at the server.
    * Also, the TCP or UDP transport selection is sent.
    *
    * here 192.168.0.1 is local ip address TCP/UDP states the tronsport we r using
    * and 1037 is the  local TCP or UDP socket number
    * cmd 2 0x02
    *  */

    string_utf16 (&data[8], "\002\000\\\\192.168.0.1\\TCP\\1037", 24);
    memset (data, 0, 8);
    send_command (s, 2, 0, 0, 24*2+10, data);

    recv (s, data, BUF_SIZE, 0) ;

    /* This command sends file path (at server) and file name request to the server.
    * 0x5 */

    string_utf16 (&data[8], path, strlen(path));
    memset (data, 0, 8);
    send_command (s, 5, 0, 0, strlen(path)*2+10, data);
    free(path);

    get_answer (s);

    /* The ASF header chunk request. Includes ?session' variable for pre header value.
    * After this command is sent,
    * the server replies with 0x11 command and then the header chunk with header data follows.
    * 0x15 */

    memset (data, 0, 40);
    data[32] = 2;

    send_command (s, 0x15, 1, 0, 40, data);

    num_stream_ids = 0;
    /* get_headers(s, asf_header);  */

    asf_header_len = get_header (s, asf_header, stream->streaming_ctrl);
//  mp_msg(MSGT_NETWORK,MSGL_INFO,"---------------------------------- asf_header %d\n",asf_header);
    if (asf_header_len==0) { //error reading header
        closesocket(s);
        return -1;
    }
    packet_length = interp_header (asf_header, asf_header_len);


    /*
    * This command is the media stream MBR selector. Switches are always 6 bytes in length.
    * After all switch elements, data ends with bytes [00 00] 00 20 ac 40 [02].
    * Where:
    * [00 00] shows 0x61 0x00 (on the first 33 sent) or 0xff 0xff for ASF files, and with no ending data for WMV files.
    * It is not yet understood what all this means.
    * And the last [02] byte is probably the header ?session' value.
    *
    *  0x33 */

    memset (data, 0, 40);

    int audio_id = stream->opts->audio_id;
    if (audio_id > 0) {
        data[2] = 0xFF;
        data[3] = 0xFF;
        data[4] = audio_id;
        send_command(s, 0x33, num_stream_ids, 0xFFFF | audio_id << 16, 8, data);
    } else {
        for (i=1; i<num_stream_ids; i++) {
            data [ (i-1) * 6 + 2 ] = 0xFF;
            data [ (i-1) * 6 + 3 ] = 0xFF;
            data [ (i-1) * 6 + 4 ] = stream_ids[i];
            data [ (i-1) * 6 + 5 ] = 0x00;
        }

        send_command (s, 0x33, num_stream_ids, 0xFFFF | stream_ids[0] << 16, (num_stream_ids-1)*6+2 , data);
    }

    get_answer (s);

    /* Start sending file from packet xx.
    * This command is also used for resume downloads or requesting a lost packet.
    * Also used for seeking by sending a play point value which seeks to the media time point.
    * Includes ?session' value in pre header and the maximum media stream time.
    * 0x07 */

    memset (data, 0, 40);

    for (i=8; i<16; i++)
        data[i] = 0xFF;

    data[20] = 0x04;

    send_command (s, 0x07, 1, 0xFFFF | stream_ids[0] << 16, 24, data);

    stream->fd = s;
    stream->streaming_ctrl->streaming_read = asf_mmst_streaming_read;
    stream->streaming_ctrl->streaming_seek = asf_mmst_streaming_seek;
    stream->streaming_ctrl->status = streaming_playing_e;
    stream->streaming = true;

    packet_length1 = packet_length;
    mp_msg(MSGT_NETWORK,MSGL_INFO,"mmst packet_length = %d\n", packet_length);

#ifdef CONFIG_ICONV
    if (url_conv != (iconv_t)(-1))
        iconv_close(url_conv);
#endif

    return 0;
}
Beispiel #8
0
static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
  int len = 0,resp;
  struct stream_priv_s* p = (struct stream_priv_s*)opts;
  char str[256],rsp_txt[256];

  if(mode != STREAM_READ) {
    mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Unknown open mode %d\n",mode);
    m_struct_free(&stream_opts,opts);
    return STREAM_UNSUPPORTED;
  }

  if(!p->filename || !p->host) {
    mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] Bad url\n");
    m_struct_free(&stream_opts,opts);
    return STREAM_ERROR;
  }

  // Open the control connection
  p->handle = connect2Server(p->host,p->port,1);
  
  if(p->handle < 0) {
    m_struct_free(&stream_opts,opts);
    return STREAM_ERROR;
  }

  // We got a connection, let's start serious things
  stream->fd = -1;
  stream->priv = p;
  p->buf = malloc(BUFSIZE);

  if (readresp(p, NULL) == 0) {
    close_f(stream);
    m_struct_free(&stream_opts,opts);
    return STREAM_ERROR;
  }

  // Login
  snprintf(str,255,"USER %s",p->user);
  resp = FtpSendCmd(str,p,rsp_txt);

  // password needed
  if(resp == 3) {
    snprintf(str,255,"PASS %s",p->pass);
    resp = FtpSendCmd(str,p,rsp_txt);
    if(resp != 2) {
      mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
      close_f(stream);
      return STREAM_ERROR;
    }
  } else if(resp != 2) {
    mp_msg(MSGT_OPEN,MSGL_ERR, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
    close_f(stream);
    return STREAM_ERROR;
  }
    
  // Set the transfer type
  resp = FtpSendCmd("TYPE I",p,rsp_txt);
  if(resp != 2) {
    mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command 'TYPE I' failed: %s\n",rsp_txt);
    close_f(stream);
    return STREAM_ERROR;
  }

  // Get the filesize
  snprintf(str,255,"SIZE %s",p->filename);
  resp = FtpSendCmd(str,p,rsp_txt);
  if(resp != 2) {
    mp_msg(MSGT_OPEN,MSGL_WARN, "[ftp] command '%s' failed: %s\n",str,rsp_txt);
  } else {
    int dummy;
    sscanf(rsp_txt,"%d %d",&dummy,&len);
  }

  if(len > 0) {
    stream->seek = seek;
    stream->end_pos = len;
  }

  // The data connection is really opened only at the first
  // read/seek. This must be done when the cache is used
  // because the connection would stay open in the main process,
  // preventing correct abort with many servers.
  stream->fd = -1;
  stream->priv = p;
  stream->fill_buffer = fill_buffer;
  stream->close = close_f;

  return STREAM_OK;
}
Beispiel #9
0
static int
rtsp_streaming_start (stream_t *stream)
{
  int fd;
  rtsp_session_t *rtsp;
  char *mrl;
  char *file;
  int port;
  int redirected, temp;

  if (!stream)
    return -1;

  /* counter so we don't get caught in infinite redirections */
  temp = 5;

  do {
    redirected = 0;

    fd = connect2Server (stream->streaming_ctrl->url->hostname,
                         port = (stream->streaming_ctrl->url->port ?
                                 stream->streaming_ctrl->url->port :
                                 RTSP_DEFAULT_PORT), 1);

    if (fd < 0 && !stream->streaming_ctrl->url->port)
      fd = connect2Server (stream->streaming_ctrl->url->hostname,
                           port = 7070, 1);

    if (fd < 0)
      return -1;

    file = stream->streaming_ctrl->url->file;
    if (file[0] == '/')
      file++;

    mrl = malloc (strlen (stream->streaming_ctrl->url->hostname)
                  + strlen (file) + 16);

    sprintf (mrl, "rtsp://%s:%i/%s",
             stream->streaming_ctrl->url->hostname, port, file);

    rtsp = rtsp_session_start (fd, &mrl, file,
                               stream->streaming_ctrl->url->hostname,
                               port, &redirected,
                               stream->streaming_ctrl->bandwidth,
                               stream->streaming_ctrl->url->username,
                               stream->streaming_ctrl->url->password);

    if (redirected == 1)
    {
      url_free (stream->streaming_ctrl->url);
      stream->streaming_ctrl->url = url_new (mrl);
      closesocket (fd);
    }

    free (mrl);
    temp--;
  } while ((redirected != 0) && (temp > 0));

  if (!rtsp)
    return -1;

  stream->fd = fd;
  stream->streaming_ctrl->data = rtsp;

  stream->streaming_ctrl->streaming_read = rtsp_streaming_read;
  stream->streaming_ctrl->streaming_seek = NULL;
  stream->streaming_ctrl->prebuffer_size = 128*1024;  // 640 KBytes
  stream->streaming_ctrl->buffering = 1;
  stream->streaming_ctrl->status = streaming_playing_e;

  return 0;
}
Beispiel #10
0
static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
	HTTP_header_t *http_hdr=NULL;
	URL_t *url = stream->streaming_ctrl->url;
	asf_http_streaming_ctrl_t *asf_http_ctrl;
	char buffer[BUFFER_SIZE];
	int i, ret;
	int fd = stream->fd;
	int done;
	int auth_retry = 0;

	asf_http_ctrl = malloc(sizeof(asf_http_streaming_ctrl_t));
	if( asf_http_ctrl==NULL ) {
		mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
		return -1;
	}
	asf_http_ctrl->streaming_type = ASF_Unknown_e;
	asf_http_ctrl->request = 1;
	asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
	asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
	stream->streaming_ctrl->data = (void*)asf_http_ctrl;

	do {
		done = 1;
		if( fd>0 ) closesocket( fd );

		if( !strcasecmp( url->protocol, "http_proxy" ) ) {
			if( url->port==0 ) url->port = 8080;
		} else {
			if( url->port==0 ) url->port = 80;
		}
		fd = connect2Server( url->hostname, url->port, 1);
		if( fd<0 ) return fd;

		http_hdr = asf_http_request( stream->streaming_ctrl );
		mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
		for(i=0; i < (int)http_hdr->buffer_size ; ) {
			int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, DEFAULT_SEND_FLAGS );
			if(r <0) {
				mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SocketWriteError,strerror(errno));
				goto err_out;
			}
			i += r;
		}
		http_free( http_hdr );
		http_hdr = http_new_header();
		do {
			i = recv( fd, buffer, BUFFER_SIZE, 0 );
//printf("read: %d\n", i );
			if( i<=0 ) {
				perror("read");
				goto err_out;
			}
			http_response_append( http_hdr, buffer, i );
		} while( !http_is_header_entire( http_hdr ) );
		if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
			http_hdr->buffer[http_hdr->buffer_size]='\0';
			mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
		}
		ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
		if( ret<0 ) {
			mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_HeaderParseFailed);
			goto err_out;
		}
		switch( asf_http_ctrl->streaming_type ) {
			case ASF_Live_e:
			case ASF_Prerecorded_e:
			case ASF_PlainText_e:
				if( http_hdr->body_size>0 ) {
					if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
						goto err_out;
					}
				}
				if( asf_http_ctrl->request==1 ) {
					if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
						// First request, we only got the ASF header.
						ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
						if(ret < 0) goto err_out;
						if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
							mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoStreamFound);
							goto err_out;
						}
						asf_http_ctrl->request++;
						done = 0;
					} else {
						done = 1;
					}
				}
				break;
			case ASF_Redirector_e:
				if( http_hdr->body_size>0 ) {
					if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
						goto err_out;
					}
				}
				*demuxer_type = DEMUXER_TYPE_PLAYLIST;
				done = 1;
				break;
			case ASF_Authenticate_e:
				if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
				asf_http_ctrl->streaming_type = ASF_Unknown_e;
				done = 0;
				break;
			case ASF_Unknown_e:
			default:
				mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType);
				goto err_out;
		}
	// Check if we got a redirect.
	} while(!done);

	stream->fd = fd;
	if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
		stream->streaming_ctrl->streaming_read = nop_streaming_read;
		stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
	} else {
		stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
		stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
		stream->streaming_ctrl->buffering = 1;
	}
	stream->streaming_ctrl->status = streaming_playing_e;
	stream->close = close_s;

	http_free( http_hdr );
	return 0;

err_out:
	if (fd > 0)
		closesocket(fd);
	stream->fd = -1;
	http_free(http_hdr);
	return -1;
}