+#if !defined( SYS_BEOS ) && !defined( SYS_NTO )
+/*****************************************************************************
+ * NetworkOpen : open a network socket
+ *****************************************************************************/
+static void NetworkOpen( input_thread_t * p_input )
+{
+ char *psz_server = NULL;
+ char *psz_bind = NULL;
+ int i_server_port = 0;
+ int i_bind_port = 0;
+ int i_opt;
+ int i_opt_size;
+ struct sockaddr_in sock;
+
+ /* Get the remote server. Syntax is :
+ * udp[stream]:[/][/][serveraddr[:serverport]][@[bindaddr]:[bindport]] */
+ if( p_input->p_source != NULL )
+ {
+ char * psz_parser = p_input->p_source;
+ char * psz_server_port = NULL;
+ char * psz_bind_port = NULL;
+
+ /* Skip the protocol name */
+ while( *psz_parser && *psz_parser != ':' )
+ {
+ psz_parser++;
+ }
+
+ /* Skip the "://" part */
+ while( *psz_parser && (*psz_parser == ':' || *psz_parser == '/') )
+ {
+ psz_parser++;
+ }
+
+ if( *psz_parser && *psz_parser != '@' )
+ {
+ /* Found server */
+ psz_server = psz_parser;
+
+ while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
+ {
+ psz_parser++;
+ }
+
+ if( *psz_parser == ':' )
+ {
+ /* Found server port */
+ *psz_parser = '\0'; /* Terminate server name */
+ psz_parser++;
+ psz_server_port = psz_parser;
+
+ while( *psz_parser && *psz_parser != '@' )
+ {
+ psz_parser++;
+ }
+ }
+ }
+
+ if( *psz_parser == '@' )
+ {
+ /* Found bind address or bind port */
+ *psz_parser = '\0'; /* Terminate server port or name if necessary */
+ psz_parser++;
+
+ if( *psz_parser && *psz_parser != ':' )
+ {
+ /* Found bind address */
+ psz_bind = psz_parser;
+
+ while( *psz_parser && *psz_parser != ':' )
+ {
+ psz_parser++;
+ }
+ }
+
+ if( *psz_parser == ':' )
+ {
+ /* Found bind port */
+ *psz_parser = '\0'; /* Terminate bind address if necessary */
+ psz_parser++;
+
+ psz_bind_port = psz_parser;
+ }
+ }
+
+ /* Convert ports format */
+ if( psz_server_port != NULL )
+ {
+ i_server_port = strtol( psz_server_port, &psz_parser, 10 );
+ if( *psz_parser )
+ {
+ intf_ErrMsg( "input error: cannot parse server port near %s",
+ psz_parser );
+ p_input->b_error = 1;
+ return;
+ }
+ }
+
+ if( psz_bind_port != NULL )
+ {
+ i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
+ if( *psz_parser )
+ {
+ intf_ErrMsg( "input error: cannot parse bind port near %s",
+ psz_parser );
+ p_input->b_error = 1;
+ return;
+ }
+ }
+ }
+ else
+ {
+ /* This is required or NetworkClose will never be called */
+ p_input->p_source = "ts: network input";
+ }
+
+ /* Check that we got a valid port */
+ if( i_bind_port == 0 )
+ {
+ i_bind_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
+ }
+
+ intf_WarnMsg( 2, "input: server=%s:%d local=%s:%d",
+ psz_server, i_server_port, psz_bind, i_bind_port );
+
+ /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
+ * protocol */
+ p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
+ if( p_input->i_handle == -1 )
+ {
+ intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
+ p_input->b_error = 1;
+ return;
+ }
+
+ /* We may want to reuse an already used socket */
+ i_opt = 1;
+ if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
+ (void *) &i_opt, sizeof( i_opt ) ) == -1 )
+ {
+ intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
+ strerror(errno));
+ close( p_input->i_handle );
+ p_input->b_error = 1;
+ return;
+ }
+
+ /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
+ * packet loss caused by scheduling problems */
+ i_opt = 0x80000;
+ if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
+ (void *) &i_opt, sizeof( i_opt ) ) == -1 )
+ {
+ intf_WarnMsg( 1, "input warning: can't configure socket (SO_RCVBUF: %s)",
+ strerror(errno));
+ }
+
+ /* Check if we really got what we have asked for, because Linux, etc.
+ * will silently limit the max buffer size to net.core.rmem_max which
+ * is typically only 65535 bytes */
+ i_opt = 0;
+ i_opt_size = sizeof( i_opt );
+ if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
+ (void*) &i_opt, &i_opt_size ) == -1 )
+ {
+ intf_WarnMsg( 1, "input warning: can't query socket (SO_RCVBUF: %s)",
+ strerror(errno));
+ }
+ else if( i_opt < 0x80000 )
+ {
+ intf_WarnMsg( 1, "input warning: socket buffer size is 0x%x"
+ " instead of 0x%x", i_opt, 0x80000 );
+ }
+
+ /* Build the local socket */
+ if ( network_BuildAddr( &sock, psz_bind, i_bind_port ) == -1 )
+ {
+ intf_ErrMsg( "input error: can't build local address" );
+ close( p_input->i_handle );
+ p_input->b_error = 1;
+ return;
+ }
+
+ /* Bind it */
+ if( bind( p_input->i_handle, (struct sockaddr *)&sock,
+ sizeof( sock ) ) < 0 )
+ {
+ intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
+ close( p_input->i_handle );
+ p_input->b_error = 1;
+ return;
+ }
+
+ /* Allow broadcast reception if we bound on INADDR_ANY */
+ if( psz_bind == NULL )
+ {
+ i_opt = 1;
+ if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_BROADCAST,
+ (void*) &i_opt, sizeof( i_opt ) ) == -1 )
+ {
+ intf_WarnMsg( 1, "input warning: can't configure socket (SO_BROADCAST: %s)",
+ strerror(errno));
+ }
+ }
+
+ /* Join the multicast group if the socket is a multicast address */
+#ifndef IN_MULTICAST
+# define IN_MULTICAST(a) IN_CLASSD(a)
+#endif
+
+ if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
+ {
+ struct ip_mreq imr;
+
+ imr.imr_interface.s_addr = INADDR_ANY;
+ imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
+ if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
+ (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
+ {
+ intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
+ strerror(errno) );
+ close( p_input->i_handle );
+ p_input->b_error = 1;
+ return;
+ }
+ }
+
+ if( psz_server != NULL )
+ {
+ /* Build socket for remote connection */
+ if ( network_BuildAddr( &sock, psz_server, i_server_port ) == -1 )
+ {
+ intf_ErrMsg( "input error: can't build remote address" );
+ close( p_input->i_handle );
+ p_input->b_error = 1;
+ return;
+ }
+
+ /* Connect the socket */
+ if( connect( p_input->i_handle, (struct sockaddr *) &sock,
+ sizeof( sock ) ) == (-1) )
+ {
+ intf_ErrMsg( "input error: can't connect socket (%s)",
+ strerror(errno) );
+ close( p_input->i_handle );
+ p_input->b_error = 1;
+ return;
+ }
+ }
+
+ p_input->stream.b_pace_control = 0;
+ p_input->stream.b_seekable = 0;
+
+ intf_WarnMsg( 3, "input: successfully opened network mode" );
+
+ return;
+}
+
+/*****************************************************************************
+ * NetworkClose : close a network socket
+ *****************************************************************************/
+static void NetworkClose( input_thread_t * p_input )
+{
+ intf_WarnMsg( 2, "input: closing network target `%s'", p_input->p_source );
+
+ close( p_input->i_handle );
+}
+
+/*****************************************************************************
+ * HTTPOpen : make an HTTP request
+ *****************************************************************************/
+static void HTTPOpen( input_thread_t * p_input )
+{
+ char *psz_server = NULL;
+ char *psz_path = NULL;
+ char *psz_proxy;
+ int i_port = 0;
+ int i_opt;
+ struct sockaddr_in sock;
+ char psz_buffer[256];
+
+ /* Get the remote server */
+ if( p_input->p_source != NULL )
+ {
+ psz_server = p_input->p_source;
+
+ /* Skip the protocol name */
+ while( *psz_server && *psz_server != ':' )
+ {
+ psz_server++;
+ }
+
+ /* Skip the "://" part */
+ while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
+ {
+ psz_server++;
+ }
+
+ /* Found a server name */
+ if( *psz_server )
+ {
+ char *psz_port = psz_server;
+
+ /* Skip the hostname part */
+ while( *psz_port && *psz_port != ':' && *psz_port != '/' )
+ {
+ psz_port++;
+ }
+
+ /* Found a port name */
+ if( *psz_port )
+ {
+ if( *psz_port == ':' )
+ {
+ /* Replace ':' with '\0' */
+ *psz_port = '\0';
+ psz_port++;
+ }
+
+ psz_path = psz_port;
+ while( *psz_path && *psz_path != '/' )
+ {
+ psz_path++;
+ }
+
+ if( *psz_path )
+ {
+ *psz_path = '\0';
+ psz_path++;
+ }
+ else
+ {
+ psz_path = NULL;
+ }
+
+ if( *psz_port != '\0' )
+ {
+ i_port = atoi( psz_port );
+ }
+ }
+ }
+ else
+ {
+ psz_server = NULL;
+ }
+ }
+
+ /* Check that we got a valid server */
+ if( psz_server == NULL )
+ {
+ intf_ErrMsg( "input error: No server given" );
+ p_input->b_error = 1;
+ return;
+ }
+
+ /* Check that we got a valid port */
+ if( i_port == 0 )
+ {
+ i_port = 80; /* FIXME */
+ }
+
+ intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
+ i_port, psz_path );
+
+ /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
+ * * protocol */
+ p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
+ if( p_input->i_handle == -1 )
+ {
+ intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) ); p_input->b_error = 1;
+ return;
+ }
+
+ /* We may want to reuse an already used socket */
+ i_opt = 1;
+ if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
+ (void *) &i_opt, sizeof( i_opt ) ) == -1 )
+ {
+ intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
+ strerror(errno));
+ close( p_input->i_handle );
+ p_input->b_error = 1;
+ return;
+ }
+
+ /* Check proxy */
+ if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
+ {
+ /* http://myproxy.mydomain:myport/ */
+ int i_proxy_port = 0;
+
+ /* Skip the protocol name */
+ while( *psz_proxy && *psz_proxy != ':' )
+ {
+ psz_proxy++;
+ }
+
+ /* Skip the "://" part */
+ while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
+ {
+ psz_proxy++;
+ }
+
+ /* Found a proxy name */
+ if( *psz_proxy )
+ {
+ char *psz_port = psz_proxy;
+
+ /* Skip the hostname part */
+ while( *psz_port && *psz_port != ':' && *psz_port != '/' )
+ {
+ psz_port++;
+ }
+
+ /* Found a port name */
+ if( *psz_port )
+ {
+ char * psz_junk;
+
+ /* Replace ':' with '\0' */
+ *psz_port = '\0';
+ psz_port++;
+
+ psz_junk = psz_port;
+ while( *psz_junk && *psz_junk != '/' )
+ {
+ psz_junk++;
+ }
+
+ if( *psz_junk )
+ {
+ *psz_junk = '\0';
+ }
+
+ if( *psz_port != '\0' )
+ {
+ i_proxy_port = atoi( psz_port );
+ }
+ }
+ }
+ else
+ {
+ intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
+ close( p_input->i_handle );
+ p_input->b_error = 1;
+ return;
+ }
+
+ /* Build socket for proxy connection */
+ if ( network_BuildAddr( &sock, psz_proxy, i_proxy_port ) == -1 )
+ {
+ intf_ErrMsg( "input error: can't build remote address" );
+ close( p_input->i_handle );
+ p_input->b_error = 1;
+ return;
+ }
+ }
+ else
+ {
+ /* No proxy, direct connection */
+ if ( network_BuildAddr( &sock, psz_server, i_port ) == -1 )
+ {
+ intf_ErrMsg( "input error: can't build remote address" );
+ close( p_input->i_handle );
+ p_input->b_error = 1;
+ return;
+ }
+ }
+
+ /* Connect the socket */
+ if( connect( p_input->i_handle, (struct sockaddr *) &sock,
+ sizeof( sock ) ) == (-1) )
+ {
+ intf_ErrMsg( "input error: can't connect socket (%s)",
+ strerror(errno) );
+ close( p_input->i_handle );
+ p_input->b_error = 1;
+ return;
+ }
+
+ p_input->stream.b_seekable = 0;
+ p_input->stream.b_pace_control = 1; /* TCP/IP... */
+
+# define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
+# define HTTP_END "\r\n"
+
+ /* Prepare GET ... */
+ if( psz_proxy != NULL )
+ {
+ snprintf( psz_buffer, sizeof(psz_buffer),
+ "GET http://%s:%d/%s HTTP/1.0\r\n"
+ HTTP_USERAGENT HTTP_END,
+ psz_server, i_port, psz_path );
+ }
+ else
+ {
+ snprintf( psz_buffer, sizeof(psz_buffer),
+ "GET /%s HTTP/1.0\r\nHost: %s\r\n"
+ HTTP_USERAGENT HTTP_END,
+ psz_path, psz_server );
+ }
+ psz_buffer[sizeof(psz_buffer) - 1] = '\0';
+
+ /* Send GET ... */
+ if( write( p_input->i_handle, psz_buffer, strlen( psz_buffer ) ) == (-1) )
+ {
+ intf_ErrMsg( "input error: can't send request (%s)", strerror(errno) );
+ close( p_input->i_handle );
+ p_input->b_error = 1;
+ return;
+ }
+
+ /* Read HTTP header - this is gonna be fun with plug-ins which do not
+ * use p_input->p_stream :-( */
+ if( (p_input->p_stream = fdopen( p_input->i_handle, "r+" )) == NULL )
+ {
+ intf_ErrMsg( "input error: can't reopen socket (%s)", strerror(errno) );
+ close( p_input->i_handle );
+ p_input->b_error = 1;
+ return;
+ }
+
+ while( !feof( p_input->p_stream ) && !ferror( p_input->p_stream ) )
+ {
+ if( fgets( psz_buffer, sizeof(psz_buffer), p_input->p_stream ) == NULL
+ || *psz_buffer == '\r' || *psz_buffer == '\0' )
+ {
+ break;
+ }
+ /* FIXME : check Content-Type one day */
+ }
+
+ intf_WarnMsg( 3, "input: successfully opened HTTP mode" );
+}
+
+#endif /* !defined( SYS_BEOS ) && !defined( SYS_NTO ) */
+