2006-08-26 19:23:14 +00:00
|
|
|
/*
|
|
|
|
* Network layer for MPlayer
|
|
|
|
* by Bertrand BAUDET <bertrand_baudet@yahoo.com>
|
|
|
|
* (C) 2001, MPlayer team.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "help_mp.h"
|
2008-08-31 13:59:54 +00:00
|
|
|
|
2009-02-01 13:42:27 +00:00
|
|
|
#if !HAVE_WINSOCK2_H
|
2008-08-31 13:59:54 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#else
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#endif
|
|
|
|
|
2008-09-01 07:31:57 +00:00
|
|
|
#include "network.h"
|
2008-05-15 16:11:50 +00:00
|
|
|
#include "stream.h"
|
2006-08-26 19:23:14 +00:00
|
|
|
#include "tcp.h"
|
2009-06-26 13:54:22 +00:00
|
|
|
#include "libavutil/avstring.h"
|
2006-08-26 19:23:14 +00:00
|
|
|
|
|
|
|
/* IPv6 options */
|
|
|
|
int network_prefer_ipv4 = 0;
|
|
|
|
|
|
|
|
// Converts an address family constant to a string
|
|
|
|
|
2007-10-27 17:46:09 +00:00
|
|
|
static const char *af2String(int af) {
|
2006-08-26 19:23:14 +00:00
|
|
|
switch (af) {
|
|
|
|
case AF_INET: return "AF_INET";
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2006-08-26 19:23:14 +00:00
|
|
|
#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
|
|
|
|
// return -2 for fatal error, like unable to resolve name, connection timeout...
|
|
|
|
// return -1 is unable to connect to a particular port
|
|
|
|
|
2007-10-27 17:46:09 +00:00
|
|
|
static int
|
2006-08-26 19:23:14 +00:00
|
|
|
connect2Server_with_af(char *host, int port, int af,int verb) {
|
|
|
|
int socket_server_fd;
|
|
|
|
int err;
|
|
|
|
socklen_t err_len;
|
|
|
|
int ret,count = 0;
|
|
|
|
fd_set set;
|
|
|
|
struct timeval tv;
|
|
|
|
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
|
|
|
|
struct hostent *hp=NULL;
|
|
|
|
char buf[255];
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2009-02-01 13:42:27 +00:00
|
|
|
#if HAVE_WINSOCK2_H
|
2008-12-27 12:03:51 +00:00
|
|
|
unsigned long val;
|
2007-03-01 14:57:44 +00:00
|
|
|
int to;
|
|
|
|
#else
|
|
|
|
struct timeval to;
|
2006-08-26 19:23:14 +00:00
|
|
|
#endif
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2006-08-26 19:23:14 +00:00
|
|
|
socket_server_fd = socket(af, SOCK_STREAM, 0);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
|
|
|
|
2006-08-26 19:23:14 +00:00
|
|
|
if( socket_server_fd==-1 ) {
|
|
|
|
// mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af));
|
2006-08-26 19:35:55 +00:00
|
|
|
return TCP_ERROR_FATAL;
|
2006-08-26 19:23:14 +00:00
|
|
|
}
|
|
|
|
|
2007-02-04 16:08:29 +00:00
|
|
|
#if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
|
2009-02-01 13:42:27 +00:00
|
|
|
#if HAVE_WINSOCK2_H
|
2007-03-01 14:57:44 +00:00
|
|
|
/* timeout in milliseconds */
|
|
|
|
to = 10 * 1000;
|
|
|
|
#else
|
|
|
|
to.tv_sec = 10;
|
|
|
|
to.tv_usec = 0;
|
|
|
|
#endif
|
|
|
|
setsockopt(socket_server_fd, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to));
|
|
|
|
setsockopt(socket_server_fd, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof(to));
|
2007-02-04 14:18:56 +00:00
|
|
|
#endif
|
|
|
|
|
2006-08-26 19:23:14 +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:
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_ERR, "Unknown address family %d\n", af);
|
2006-08-26 19:35:55 +00:00
|
|
|
return TCP_ERROR_FATAL;
|
2006-08-26 19:23:14 +00:00
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
|
|
|
|
2006-08-26 19:23:14 +00:00
|
|
|
memset(&server_address, 0, sizeof(server_address));
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2009-02-01 14:38:28 +00:00
|
|
|
#if HAVE_INET_PTON
|
2006-08-26 19:23:14 +00:00
|
|
|
if (inet_pton(af, host, our_s_addr)!=1)
|
2009-02-01 14:38:28 +00:00
|
|
|
#elif HAVE_INET_ATON
|
|
|
|
if (inet_aton(host, our_s_addr)!=1)
|
|
|
|
#elif HAVE_WINSOCK2_H
|
2006-08-26 19:23:14 +00:00
|
|
|
if ( inet_addr(host)==INADDR_NONE )
|
|
|
|
#endif
|
|
|
|
{
|
2009-07-06 22:15:02 +00:00
|
|
|
if(verb) mp_tmsg(MSGT_NETWORK,MSGL_STATUS,"Resolving %s for %s...\n", host, af2String(af));
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2006-08-26 19:23:14 +00:00
|
|
|
#ifdef HAVE_GETHOSTBYNAME2
|
|
|
|
hp=(struct hostent*)gethostbyname2( host, af );
|
|
|
|
#else
|
|
|
|
hp=(struct hostent*)gethostbyname( host );
|
|
|
|
#endif
|
|
|
|
if( hp==NULL ) {
|
2009-07-06 22:15:02 +00:00
|
|
|
if(verb) mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Couldn't resolve name for %s: %s\n", af2String(af), host);
|
2006-08-26 19:35:55 +00:00
|
|
|
return TCP_ERROR_FATAL;
|
2006-08-26 19:23:14 +00:00
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2006-08-26 19:23:14 +00:00
|
|
|
memcpy( our_s_addr, (void*)hp->h_addr_list[0], hp->h_length );
|
|
|
|
}
|
2009-02-01 13:42:27 +00:00
|
|
|
#if HAVE_WINSOCK2_H
|
2006-08-26 19:23:14 +00:00
|
|
|
else {
|
|
|
|
unsigned long addr = inet_addr(host);
|
|
|
|
memcpy( our_s_addr, (void*)&addr, sizeof(addr) );
|
|
|
|
}
|
|
|
|
#endif
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2006-08-26 19:23:14 +00:00
|
|
|
switch (af) {
|
|
|
|
case AF_INET:
|
|
|
|
server_address.four.sin_family=af;
|
2009-07-06 23:26:13 +00:00
|
|
|
server_address.four.sin_port=htons(port);
|
2006-08-26 19:23:14 +00:00
|
|
|
server_address_size = sizeof(server_address.four);
|
|
|
|
break;
|
2009-07-06 23:26:13 +00:00
|
|
|
#ifdef HAVE_AF_INET6
|
2006-08-26 19:23:14 +00:00
|
|
|
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:
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_ERR, "Unknown address family %d\n", af);
|
2006-08-26 19:35:55 +00:00
|
|
|
return TCP_ERROR_FATAL;
|
2006-08-26 19:23:14 +00:00
|
|
|
}
|
|
|
|
|
2009-02-01 13:33:35 +00:00
|
|
|
#if HAVE_INET_ATON || defined(HAVE_WINSOCK2_H)
|
2009-06-26 13:54:22 +00:00
|
|
|
av_strlcpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255);
|
2006-08-26 19:23:14 +00:00
|
|
|
#else
|
|
|
|
inet_ntop(af, our_s_addr, buf, 255);
|
|
|
|
#endif
|
2009-07-06 22:15:02 +00:00
|
|
|
if(verb) mp_tmsg(MSGT_NETWORK,MSGL_STATUS,"Connecting to server %s[%s]: %d...\n", host, buf , port );
|
2006-08-26 19:23:14 +00:00
|
|
|
|
|
|
|
// Turn the socket as non blocking so we can timeout on the connection
|
2009-02-01 13:42:27 +00:00
|
|
|
#if !HAVE_WINSOCK2_H
|
2006-08-26 19:23:14 +00:00
|
|
|
fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK );
|
|
|
|
#else
|
|
|
|
val = 1;
|
|
|
|
ioctlsocket( socket_server_fd, FIONBIO, &val );
|
|
|
|
#endif
|
|
|
|
if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) {
|
2009-02-01 13:42:27 +00:00
|
|
|
#if !HAVE_WINSOCK2_H
|
2006-08-26 19:23:14 +00:00
|
|
|
if( errno!=EINPROGRESS ) {
|
|
|
|
#else
|
|
|
|
if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) {
|
|
|
|
#endif
|
2009-07-06 22:15:02 +00:00
|
|
|
if(verb) mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Failed to connect to server with %s\n", af2String(af));
|
2006-08-26 19:23:14 +00:00
|
|
|
closesocket(socket_server_fd);
|
2006-08-26 19:35:55 +00:00
|
|
|
return TCP_ERROR_PORT;
|
2006-08-26 19:23:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 500000;
|
|
|
|
FD_ZERO( &set );
|
|
|
|
FD_SET( socket_server_fd, &set );
|
2007-09-19 12:54:10 +00:00
|
|
|
// When the connection will be made, we will have a writeable fd
|
2006-08-26 19:23:14 +00:00
|
|
|
while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) {
|
2008-04-09 00:36:28 +00:00
|
|
|
if(count > 30 || stream_check_interrupt(500)) {
|
2006-08-26 19:23:14 +00:00
|
|
|
if(count > 30)
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_ERR,"connection timeout\n");
|
2006-08-26 19:23:14 +00:00
|
|
|
else
|
2007-09-19 12:54:10 +00:00
|
|
|
mp_msg(MSGT_NETWORK,MSGL_V,"Connection interrupted by user\n");
|
2006-08-26 19:35:55 +00:00
|
|
|
return TCP_ERROR_TIMEOUT;
|
2006-08-26 19:23:14 +00:00
|
|
|
}
|
|
|
|
count++;
|
|
|
|
FD_ZERO( &set );
|
|
|
|
FD_SET( socket_server_fd, &set );
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 500000;
|
|
|
|
}
|
2009-07-06 22:15:02 +00:00
|
|
|
if (ret < 0) mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Select failed.\n");
|
2006-08-26 19:23:14 +00:00
|
|
|
|
|
|
|
// Turn back the socket as blocking
|
2009-02-01 13:42:27 +00:00
|
|
|
#if !HAVE_WINSOCK2_H
|
2006-08-26 19:23:14 +00:00
|
|
|
fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK );
|
|
|
|
#else
|
|
|
|
val = 0;
|
|
|
|
ioctlsocket( socket_server_fd, FIONBIO, &val );
|
|
|
|
#endif
|
2007-09-19 12:54:10 +00:00
|
|
|
// Check if there were any errors
|
2006-08-26 19:23:14 +00:00
|
|
|
err_len = sizeof(int);
|
|
|
|
ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len);
|
|
|
|
if(ret < 0) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_ERR,"getsockopt failed: %s\n",strerror(errno));
|
2006-08-26 19:35:55 +00:00
|
|
|
return TCP_ERROR_FATAL;
|
2006-08-26 19:23:14 +00:00
|
|
|
}
|
|
|
|
if(err > 0) {
|
2009-07-06 22:15:02 +00:00
|
|
|
mp_tmsg(MSGT_NETWORK,MSGL_ERR,"connect error: %s\n",strerror(err));
|
2006-08-26 19:35:55 +00:00
|
|
|
return TCP_ERROR_PORT;
|
2006-08-26 19:23:14 +00:00
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2006-08-26 19:23:14 +00:00
|
|
|
return socket_server_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
connect2Server(char *host, int port, int verb) {
|
|
|
|
#ifdef HAVE_AF_INET6
|
|
|
|
int r;
|
2006-08-26 19:35:55 +00:00
|
|
|
int s = TCP_ERROR_FATAL;
|
2006-08-26 19:23:14 +00:00
|
|
|
|
2009-07-06 23:26:13 +00:00
|
|
|
r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb);
|
2006-08-26 19:35:55 +00:00
|
|
|
if (r >= 0) return r;
|
2006-08-26 19:23:14 +00:00
|
|
|
|
|
|
|
s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb);
|
2006-08-26 19:35:55 +00:00
|
|
|
if (s == TCP_ERROR_FATAL) return r;
|
2006-08-26 19:23:14 +00:00
|
|
|
return s;
|
2009-07-06 23:26:13 +00:00
|
|
|
#else
|
2006-08-26 19:23:14 +00:00
|
|
|
return connect2Server_with_af(host, port, AF_INET,verb);
|
|
|
|
#endif
|
|
|
|
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2006-08-26 19:23:14 +00:00
|
|
|
}
|