libfreerdp-core: more rdpTcp refactoring

This commit is contained in:
Marc-André Moreau 2015-02-13 16:51:08 -05:00
parent e241330daf
commit e904195e49
4 changed files with 50 additions and 89 deletions

View File

@ -229,7 +229,9 @@ static BOOL freerdp_peer_initialize(freerdp_peer* client)
static BOOL freerdp_peer_get_fds(freerdp_peer* client, void** rfds, int* rcount) static BOOL freerdp_peer_get_fds(freerdp_peer* client, void** rfds, int* rcount)
{ {
rfds[*rcount] = (void*)(long)(client->context->rdp->transport->TcpIn->sockfd); rdpTransport* transport = client->context->rdp->transport;
rfds[*rcount] = (void*)(long)(BIO_get_fd(transport->TcpIn->bufferedBio, NULL));
(*rcount)++; (*rcount)++;
return TRUE; return TRUE;
@ -237,7 +239,12 @@ static BOOL freerdp_peer_get_fds(freerdp_peer* client, void** rfds, int* rcount)
static HANDLE freerdp_peer_get_event_handle(freerdp_peer* client) static HANDLE freerdp_peer_get_event_handle(freerdp_peer* client)
{ {
return client->context->rdp->transport->TcpIn->event; HANDLE hEvent = NULL;
rdpTransport* transport = client->context->rdp->transport;
BIO_get_event(transport->TcpIn->bufferedBio, &hEvent);
return hEvent;
} }
static BOOL freerdp_peer_check_fds(freerdp_peer* peer) static BOOL freerdp_peer_check_fds(freerdp_peer* peer)

View File

@ -90,7 +90,6 @@
struct _WINPR_BIO_SIMPLE_SOCKET struct _WINPR_BIO_SIMPLE_SOCKET
{ {
BOOL win32;
SOCKET socket; SOCKET socket;
HANDLE hEvent; HANDLE hEvent;
}; };
@ -104,8 +103,9 @@ static void transport_bio_simple_check_reset_event(BIO* bio)
u_long nbytes = 0; u_long nbytes = 0;
WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*) bio->ptr; WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*) bio->ptr;
if (!ptr->win32) #ifndef _WIN32
return; return;
#endif
_ioctlsocket(ptr->socket, FIONREAD, &nbytes); _ioctlsocket(ptr->socket, FIONREAD, &nbytes);
@ -377,8 +377,7 @@ static int transport_bio_simple_init(BIO* bio, SOCKET socket, int shutdown)
bio->flags = BIO_FLAGS_SHOULD_RETRY; bio->flags = BIO_FLAGS_SHOULD_RETRY;
bio->init = 1; bio->init = 1;
if (ptr->win32) #ifdef _WIN32
{
ptr->hEvent = WSACreateEvent(); /* creates a manual reset event */ ptr->hEvent = WSACreateEvent(); /* creates a manual reset event */
if (!ptr->hEvent) if (!ptr->hEvent)
@ -386,14 +385,12 @@ static int transport_bio_simple_init(BIO* bio, SOCKET socket, int shutdown)
/* WSAEventSelect automatically sets the socket in non-blocking mode */ /* WSAEventSelect automatically sets the socket in non-blocking mode */
WSAEventSelect(ptr->socket, ptr->hEvent, FD_READ | FD_CLOSE); WSAEventSelect(ptr->socket, ptr->hEvent, FD_READ | FD_CLOSE);
} #else
else
{
ptr->hEvent = CreateFileDescriptorEvent(NULL, FALSE, FALSE, (int) ptr->socket); ptr->hEvent = CreateFileDescriptorEvent(NULL, FALSE, FALSE, (int) ptr->socket);
if (!ptr->hEvent) if (!ptr->hEvent)
return 0; return 0;
} #endif
return 1; return 1;
} }
@ -439,10 +436,6 @@ static int transport_bio_simple_new(BIO* bio)
bio->ptr = ptr; bio->ptr = ptr;
#ifdef _WIN32
ptr->win32 = TRUE;
#endif
return 1; return 1;
} }
@ -1042,20 +1035,22 @@ BOOL freerdp_tcp_set_keep_alive_mode(int sockfd)
BOOL freerdp_tcp_connect(rdpTcp* tcp, rdpSettings* settings, const char* hostname, int port, int timeout) BOOL freerdp_tcp_connect(rdpTcp* tcp, rdpSettings* settings, const char* hostname, int port, int timeout)
{ {
int status; int status;
UINT32 option_value; int sockfd;
socklen_t option_len; UINT32 optval;
socklen_t optlen;
BOOL ipcSocket = FALSE;
if (!hostname) if (!hostname)
return FALSE; return FALSE;
if (hostname[0] == '/') if (hostname[0] == '/')
tcp->ipcSocket = TRUE; ipcSocket = TRUE;
if (tcp->ipcSocket) if (ipcSocket)
{ {
tcp->sockfd = freerdp_uds_connect(hostname); sockfd = freerdp_uds_connect(hostname);
if (tcp->sockfd < 0) if (sockfd < 0)
return FALSE; return FALSE;
tcp->socketBio = BIO_new(BIO_s_simple_socket()); tcp->socketBio = BIO_new(BIO_s_simple_socket());
@ -1063,32 +1058,11 @@ BOOL freerdp_tcp_connect(rdpTcp* tcp, rdpSettings* settings, const char* hostnam
if (!tcp->socketBio) if (!tcp->socketBio)
return FALSE; return FALSE;
BIO_set_fd(tcp->socketBio, tcp->sockfd, BIO_CLOSE); BIO_set_fd(tcp->socketBio, sockfd, BIO_CLOSE);
} }
else else
{ {
#ifdef NO_IPV6 sockfd = -1;
tcp->socketBio = BIO_new(BIO_s_connect());
if (!tcp->socketBio)
return FALSE;
if (BIO_set_conn_hostname(tcp->socketBio, hostname) < 0 || BIO_set_conn_int_port(tcp->socketBio, &port) < 0)
return FALSE;
BIO_set_nbio(tcp->socketBio, 1);
status = BIO_do_connect(tcp->socketBio);
if ((status <= 0) && !BIO_should_retry(tcp->socketBio))
return FALSE;
tcp->sockfd = BIO_get_fd(tcp->socketBio, NULL);
if (tcp->sockfd < 0)
return FALSE;
#else /* NO_IPV6 */
tcp->sockfd = -1;
if (!settings->GatewayEnabled) if (!settings->GatewayEnabled)
{ {
@ -1097,7 +1071,7 @@ BOOL freerdp_tcp_connect(rdpTcp* tcp, rdpSettings* settings, const char* hostnam
if (settings->TargetNetAddressCount > 0) if (settings->TargetNetAddressCount > 0)
{ {
#ifndef _WIN32 #ifndef _WIN32
tcp->sockfd = freerdp_tcp_connect_multi(settings->TargetNetAddresses, sockfd = freerdp_tcp_connect_multi(settings->TargetNetAddresses,
settings->TargetNetAddressCount, port, timeout); settings->TargetNetAddressCount, port, timeout);
#else #else
hostname = settings->TargetNetAddresses[0]; hostname = settings->TargetNetAddresses[0];
@ -1106,7 +1080,7 @@ BOOL freerdp_tcp_connect(rdpTcp* tcp, rdpSettings* settings, const char* hostnam
} }
} }
if (tcp->sockfd <= 0) if (sockfd <= 0)
{ {
char port_str[16]; char port_str[16];
struct addrinfo hints; struct addrinfo hints;
@ -1127,6 +1101,7 @@ BOOL freerdp_tcp_connect(rdpTcp* tcp, rdpSettings* settings, const char* hostnam
} }
addr = result; addr = result;
if ((addr->ai_family == AF_INET6) && (addr->ai_next != 0)) if ((addr->ai_family == AF_INET6) && (addr->ai_next != 0))
{ {
while ((addr = addr->ai_next)) while ((addr = addr->ai_next))
@ -1134,18 +1109,20 @@ BOOL freerdp_tcp_connect(rdpTcp* tcp, rdpSettings* settings, const char* hostnam
if (addr->ai_family == AF_INET) if (addr->ai_family == AF_INET)
break; break;
} }
if (!addr) if (!addr)
addr = result; addr = result;
} }
tcp->sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (tcp->sockfd < 0) { if (sockfd < 0)
{
freeaddrinfo(result); freeaddrinfo(result);
return FALSE; return FALSE;
} }
if (!freerdp_tcp_connect_timeout(tcp->sockfd, addr->ai_addr, addr->ai_addrlen, timeout)) if (!freerdp_tcp_connect_timeout(sockfd, addr->ai_addr, addr->ai_addrlen, timeout))
{ {
fprintf(stderr, "failed to connect to %s\n", hostname); fprintf(stderr, "failed to connect to %s\n", hostname);
freeaddrinfo(result); freeaddrinfo(result);
@ -1155,45 +1132,37 @@ BOOL freerdp_tcp_connect(rdpTcp* tcp, rdpSettings* settings, const char* hostnam
freeaddrinfo(result); freeaddrinfo(result);
} }
tcp->socketBio = BIO_new_socket(tcp->sockfd, BIO_NOCLOSE);
#endif /* NO_IPV6 */
(void) BIO_set_close(tcp->socketBio, BIO_NOCLOSE);
BIO_free(tcp->socketBio);
tcp->socketBio = BIO_new(BIO_s_simple_socket()); tcp->socketBio = BIO_new(BIO_s_simple_socket());
if (!tcp->socketBio) if (!tcp->socketBio)
return FALSE; return FALSE;
BIO_set_fd(tcp->socketBio, tcp->sockfd, BIO_CLOSE); BIO_set_fd(tcp->socketBio, sockfd, BIO_CLOSE);
} }
BIO_get_event(tcp->socketBio, &tcp->event);
settings->IPv6Enabled = FALSE; settings->IPv6Enabled = FALSE;
free(settings->ClientAddress); free(settings->ClientAddress);
settings->ClientAddress = freerdp_tcp_get_ip_address(tcp->sockfd); settings->ClientAddress = freerdp_tcp_get_ip_address(sockfd);
option_value = 1; optval = 1;
option_len = sizeof(option_value); optlen = sizeof(optval);
if (!tcp->ipcSocket) if (!ipcSocket)
{ {
if (setsockopt(tcp->sockfd, IPPROTO_TCP, TCP_NODELAY, (void*) &option_value, option_len) < 0) if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (void*) &optval, optlen) < 0)
WLog_ERR(TAG, "unable to set TCP_NODELAY"); WLog_ERR(TAG, "unable to set TCP_NODELAY");
} }
/* receive buffer must be a least 32 K */ /* receive buffer must be a least 32 K */
if (getsockopt(tcp->sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &option_value, &option_len) == 0) if (getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &optval, &optlen) == 0)
{ {
if (option_value < (1024 * 32)) if (optval < (1024 * 32))
{ {
option_value = 1024 * 32; optval = 1024 * 32;
option_len = sizeof(option_value); optlen = sizeof(optval);
if (setsockopt(tcp->sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &option_value, option_len) < 0) if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &optval, optlen) < 0)
{ {
WLog_ERR(TAG, "unable to set receive buffer len"); WLog_ERR(TAG, "unable to set receive buffer len");
return FALSE; return FALSE;
@ -1201,9 +1170,9 @@ BOOL freerdp_tcp_connect(rdpTcp* tcp, rdpSettings* settings, const char* hostnam
} }
} }
if (!tcp->ipcSocket) if (!ipcSocket)
{ {
if (!freerdp_tcp_set_keep_alive_mode(tcp->sockfd)) if (!freerdp_tcp_set_keep_alive_mode(sockfd))
return FALSE; return FALSE;
} }
@ -1213,7 +1182,6 @@ BOOL freerdp_tcp_connect(rdpTcp* tcp, rdpSettings* settings, const char* hostnam
return FALSE; return FALSE;
tcp->bufferedBio->ptr = tcp; tcp->bufferedBio->ptr = tcp;
tcp->bufferedBio = BIO_push(tcp->bufferedBio, tcp->socketBio); tcp->bufferedBio = BIO_push(tcp->bufferedBio, tcp->socketBio);
return TRUE; return TRUE;
@ -1221,16 +1189,7 @@ BOOL freerdp_tcp_connect(rdpTcp* tcp, rdpSettings* settings, const char* hostnam
int freerdp_tcp_attach(rdpTcp* tcp, int sockfd) int freerdp_tcp_attach(rdpTcp* tcp, int sockfd)
{ {
tcp->sockfd = sockfd;
ringbuffer_commit_read_bytes(&tcp->xmitBuffer, ringbuffer_used(&tcp->xmitBuffer));
if (tcp->socketBio) if (tcp->socketBio)
{
if (BIO_set_fd(tcp->socketBio, sockfd, BIO_CLOSE) < 0)
return -1;
}
else
{ {
tcp->socketBio = BIO_new(BIO_s_simple_socket()); tcp->socketBio = BIO_new(BIO_s_simple_socket());
@ -1252,7 +1211,7 @@ int freerdp_tcp_attach(rdpTcp* tcp, int sockfd)
tcp->bufferedBio = BIO_push(tcp->bufferedBio, tcp->socketBio); tcp->bufferedBio = BIO_push(tcp->bufferedBio, tcp->socketBio);
} }
BIO_get_event(tcp->socketBio, &tcp->event); ringbuffer_commit_read_bytes(&tcp->xmitBuffer, ringbuffer_used(&tcp->xmitBuffer));
return 1; return 1;
} }
@ -1269,8 +1228,6 @@ rdpTcp* freerdp_tcp_new()
if (!ringbuffer_init(&tcp->xmitBuffer, 0x10000)) if (!ringbuffer_init(&tcp->xmitBuffer, 0x10000))
goto out_free; goto out_free;
tcp->sockfd = -1;
return tcp; return tcp;
out_free: out_free:

View File

@ -64,9 +64,6 @@ typedef struct rdp_tcp rdpTcp;
struct rdp_tcp struct rdp_tcp
{ {
int sockfd;
HANDLE event;
BOOL ipcSocket;
BIO* socketBio; BIO* socketBio;
BIO* bufferedBio; BIO* bufferedBio;
BOOL readBlocked; BOOL readBlocked;

View File

@ -680,10 +680,10 @@ void transport_get_fds(rdpTransport* transport, void** rfds, int* rcount)
void* pfd; void* pfd;
#ifdef _WIN32 #ifdef _WIN32
rfds[*rcount] = transport->TcpIn->event; BIO_get_event(transport->TcpIn->bufferedBio, &rfds[*rcount]);
(*rcount)++; (*rcount)++;
#else #else
rfds[*rcount] = (void*)(long)(transport->TcpIn->sockfd); rfds[*rcount] = (void*)(long)(BIO_get_fd(transport->TcpIn->bufferedBio, NULL));
(*rcount)++; (*rcount)++;
#endif #endif
@ -712,7 +712,7 @@ DWORD transport_get_event_handles(rdpTransport* transport, HANDLE* events)
DWORD nCount = 0; DWORD nCount = 0;
if (events) if (events)
events[nCount] = transport->TcpIn->event; BIO_get_event(transport->TcpIn->bufferedBio, &events[nCount]);
nCount++; nCount++;
if (transport->ReceiveEvent) if (transport->ReceiveEvent)