2011-07-03 23:34:15 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-07-03 23:34:15 +04:00
|
|
|
* Transmission Control Protocol (TCP)
|
|
|
|
*
|
|
|
|
* Copyright 2011 Vic Lee
|
|
|
|
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-07-03 23:34:15 +04:00
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2011-08-16 23:35:29 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
#include <winpr/crt.h>
|
2014-06-02 05:37:20 +04:00
|
|
|
#include <winpr/winsock.h>
|
2012-10-09 07:42:01 +04:00
|
|
|
|
2014-11-12 22:06:34 +03:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
|
2011-08-16 23:35:29 +04:00
|
|
|
#include <netdb.h>
|
2011-08-17 01:40:29 +04:00
|
|
|
#include <unistd.h>
|
2011-07-12 10:53:26 +04:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
2011-09-12 14:45:38 +04:00
|
|
|
#include <netinet/in.h>
|
2011-12-12 03:59:35 +04:00
|
|
|
#include <netinet/tcp.h>
|
2012-01-16 17:27:07 +04:00
|
|
|
#include <net/if.h>
|
2014-10-17 14:08:39 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <arpa/inet.h>
|
2011-12-14 04:42:10 +04:00
|
|
|
|
2014-07-02 17:15:46 +04:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#else
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
2015-02-04 00:29:35 +03:00
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
2015-02-02 19:48:54 +03:00
|
|
|
#include <sys/filio.h>
|
2015-02-02 21:59:51 +03:00
|
|
|
#endif
|
2015-02-02 19:48:54 +03:00
|
|
|
|
2015-04-21 21:42:06 +03:00
|
|
|
#if defined(__FreeBSD__) || defined(__OpenBSD__)
|
2014-05-01 17:09:35 +04:00
|
|
|
#ifndef SOL_TCP
|
|
|
|
#define SOL_TCP IPPROTO_TCP
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2011-12-14 04:42:10 +04:00
|
|
|
#ifdef __APPLE__
|
2014-01-21 17:27:21 +04:00
|
|
|
#ifndef SOL_TCP
|
2014-01-21 20:06:29 +04:00
|
|
|
#define SOL_TCP IPPROTO_TCP
|
2014-01-21 17:27:21 +04:00
|
|
|
#endif
|
2011-12-14 04:42:10 +04:00
|
|
|
#ifndef TCP_KEEPIDLE
|
|
|
|
#define TCP_KEEPIDLE TCP_KEEPALIVE
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2014-11-12 22:06:34 +03:00
|
|
|
#else
|
|
|
|
|
|
|
|
#include <winpr/windows.h>
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
|
|
|
#define SHUT_RDWR SD_BOTH
|
|
|
|
#define close(_fd) closesocket(_fd)
|
|
|
|
|
2011-08-16 22:41:12 +04:00
|
|
|
#endif
|
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#include <freerdp/log.h>
|
2014-11-12 22:06:34 +03:00
|
|
|
|
2013-03-22 00:45:25 +04:00
|
|
|
#include <winpr/stream.h>
|
2011-07-03 23:34:15 +04:00
|
|
|
|
|
|
|
#include "tcp.h"
|
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#define TAG FREERDP_TAG("core")
|
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
/* Simple Socket BIO */
|
|
|
|
|
2015-01-28 22:54:03 +03:00
|
|
|
struct _WINPR_BIO_SIMPLE_SOCKET
|
|
|
|
{
|
|
|
|
SOCKET socket;
|
|
|
|
HANDLE hEvent;
|
|
|
|
};
|
|
|
|
typedef struct _WINPR_BIO_SIMPLE_SOCKET WINPR_BIO_SIMPLE_SOCKET;
|
|
|
|
|
|
|
|
static int transport_bio_simple_init(BIO* bio, SOCKET socket, int shutdown);
|
2015-01-28 21:46:17 +03:00
|
|
|
static int transport_bio_simple_uninit(BIO* bio);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
long transport_bio_simple_callback(BIO* bio, int mode, const char* argp, int argi, long argl, long ret)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_simple_write(BIO* bio, const char* buf, int size)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
int status = 0;
|
2015-01-28 22:54:03 +03:00
|
|
|
WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*) bio->ptr;
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_WRITE);
|
|
|
|
|
2015-01-28 22:54:03 +03:00
|
|
|
status = _send(ptr->socket, buf, size, 0);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
if (status <= 0)
|
|
|
|
{
|
|
|
|
error = WSAGetLastError();
|
|
|
|
|
|
|
|
if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) ||
|
|
|
|
(error == WSAEINPROGRESS) || (error == WSAEALREADY))
|
|
|
|
{
|
|
|
|
BIO_set_flags(bio, (BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_simple_read(BIO* bio, char* buf, int size)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
int status = 0;
|
2015-01-28 22:54:03 +03:00
|
|
|
WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*) bio->ptr;
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_READ);
|
|
|
|
|
2015-09-05 16:18:01 +03:00
|
|
|
WSAResetEvent(ptr->hEvent);
|
|
|
|
|
2015-01-28 22:54:03 +03:00
|
|
|
status = _recv(ptr->socket, buf, size, 0);
|
|
|
|
|
2014-07-11 01:35:11 +04:00
|
|
|
if (status > 0)
|
2015-01-29 19:35:52 +03:00
|
|
|
{
|
2014-07-11 01:35:11 +04:00
|
|
|
return status;
|
2015-01-29 19:35:52 +03:00
|
|
|
}
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2014-07-11 01:35:11 +04:00
|
|
|
if (status == 0)
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
2014-07-11 01:35:11 +04:00
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2014-07-11 01:35:11 +04:00
|
|
|
error = WSAGetLastError();
|
|
|
|
|
|
|
|
if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) ||
|
|
|
|
(error == WSAEINPROGRESS) || (error == WSAEALREADY))
|
|
|
|
{
|
|
|
|
BIO_set_flags(bio, (BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
2014-06-02 05:37:20 +04:00
|
|
|
}
|
|
|
|
|
2014-07-11 01:35:11 +04:00
|
|
|
return -1;
|
2014-06-02 05:37:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_simple_puts(BIO* bio, const char* str)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_simple_gets(BIO* bio, char* str, int size)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long transport_bio_simple_ctrl(BIO* bio, int cmd, long arg1, void* arg2)
|
|
|
|
{
|
|
|
|
int status = -1;
|
2015-01-28 22:54:03 +03:00
|
|
|
WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*) bio->ptr;
|
|
|
|
|
|
|
|
if (cmd == BIO_C_SET_SOCKET)
|
|
|
|
{
|
|
|
|
transport_bio_simple_uninit(bio);
|
|
|
|
transport_bio_simple_init(bio, (SOCKET) arg2, (int) arg1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (cmd == BIO_C_GET_SOCKET)
|
|
|
|
{
|
|
|
|
if (!bio->init || !arg2)
|
|
|
|
return 0;
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2015-01-28 22:54:03 +03:00
|
|
|
*((ULONG_PTR*) arg2) = (ULONG_PTR) ptr->socket;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (cmd == BIO_C_GET_EVENT)
|
2015-01-28 05:18:26 +03:00
|
|
|
{
|
|
|
|
if (!bio->init || !arg2)
|
|
|
|
return 0;
|
|
|
|
|
2015-01-28 22:54:03 +03:00
|
|
|
*((ULONG_PTR*) arg2) = (ULONG_PTR) ptr->hEvent;
|
2015-01-28 05:18:26 +03:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (cmd == BIO_C_SET_NONBLOCK)
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
int flags;
|
|
|
|
|
2015-01-28 22:54:03 +03:00
|
|
|
flags = fcntl((int) ptr->socket, F_GETFL);
|
2015-01-28 05:18:26 +03:00
|
|
|
|
|
|
|
if (flags == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (arg1)
|
2015-01-28 22:54:03 +03:00
|
|
|
fcntl((int) ptr->socket, F_SETFL, flags | O_NONBLOCK);
|
2015-01-28 05:18:26 +03:00
|
|
|
else
|
2015-01-28 22:54:03 +03:00
|
|
|
fcntl((int) ptr->socket, F_SETFL, flags & ~(O_NONBLOCK));
|
2015-01-28 05:18:26 +03:00
|
|
|
#else
|
2015-01-29 19:35:52 +03:00
|
|
|
/* the internal socket is always non-blocking */
|
2015-01-28 05:18:26 +03:00
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
2015-02-13 23:22:27 +03:00
|
|
|
else if (cmd == BIO_C_WAIT_READ)
|
|
|
|
{
|
|
|
|
int timeout = (int) arg1;
|
|
|
|
int sockfd = (int) ptr->socket;
|
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
struct pollfd pollset;
|
|
|
|
|
|
|
|
pollset.fd = sockfd;
|
|
|
|
pollset.events = POLLIN;
|
|
|
|
pollset.revents = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = poll(&pollset, 1, timeout);
|
|
|
|
}
|
|
|
|
while ((status < 0) && (errno == EINTR));
|
|
|
|
#else
|
|
|
|
fd_set rset;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
FD_ZERO(&rset);
|
|
|
|
FD_SET(sockfd, &rset);
|
|
|
|
|
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = select(sockfd + 1, &rset, NULL, NULL, timeout ? &tv : NULL);
|
|
|
|
}
|
|
|
|
while ((status < 0) && (errno == EINTR));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (cmd == BIO_C_WAIT_WRITE)
|
|
|
|
{
|
|
|
|
int timeout = (int) arg1;
|
|
|
|
int sockfd = (int) ptr->socket;
|
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
struct pollfd pollset;
|
|
|
|
|
|
|
|
pollset.fd = sockfd;
|
|
|
|
pollset.events = POLLOUT;
|
|
|
|
pollset.revents = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = poll(&pollset, 1, timeout);
|
|
|
|
}
|
|
|
|
while ((status < 0) && (errno == EINTR));
|
|
|
|
#else
|
|
|
|
fd_set rset;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
FD_ZERO(&rset);
|
|
|
|
FD_SET(sockfd, &rset);
|
|
|
|
|
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = select(sockfd + 1, NULL, &rset, NULL, timeout ? &tv : NULL);
|
|
|
|
}
|
|
|
|
while ((status < 0) && (errno == EINTR));
|
|
|
|
#endif
|
|
|
|
}
|
2015-01-28 05:18:26 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case BIO_C_SET_FD:
|
|
|
|
if (arg2)
|
|
|
|
{
|
2015-01-28 21:46:17 +03:00
|
|
|
transport_bio_simple_uninit(bio);
|
2015-01-28 22:54:03 +03:00
|
|
|
transport_bio_simple_init(bio, (SOCKET) *((int*) arg2), (int) arg1);
|
2014-06-02 05:37:20 +04:00
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_C_GET_FD:
|
|
|
|
if (bio->init)
|
|
|
|
{
|
|
|
|
if (arg2)
|
2015-01-28 22:54:03 +03:00
|
|
|
*((int*) arg2) = (int) ptr->socket;
|
|
|
|
status = (int) ptr->socket;
|
2014-06-02 05:37:20 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_GET_CLOSE:
|
|
|
|
status = bio->shutdown;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_SET_CLOSE:
|
|
|
|
bio->shutdown = (int) arg1;
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_DUP:
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_FLUSH:
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2015-01-28 22:54:03 +03:00
|
|
|
static int transport_bio_simple_init(BIO* bio, SOCKET socket, int shutdown)
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
2015-01-28 22:54:03 +03:00
|
|
|
WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*) bio->ptr;
|
|
|
|
|
|
|
|
ptr->socket = socket;
|
|
|
|
|
2015-01-28 21:46:17 +03:00
|
|
|
bio->shutdown = shutdown;
|
2014-06-02 05:37:20 +04:00
|
|
|
bio->flags = BIO_FLAGS_SHOULD_RETRY;
|
2015-01-28 21:46:17 +03:00
|
|
|
bio->init = 1;
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
ptr->hEvent = WSACreateEvent();
|
2015-01-29 19:35:52 +03:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
if (!ptr->hEvent)
|
|
|
|
return 0;
|
2015-01-29 19:35:52 +03:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
/* WSAEventSelect automatically sets the socket in non-blocking mode */
|
2015-09-05 16:18:01 +03:00
|
|
|
if (WSAEventSelect(ptr->socket, ptr->hEvent, FD_READ | FD_ACCEPT | FD_CLOSE))
|
2015-07-14 12:58:01 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WSAEventSelect returned %08X", WSAGetLastError());
|
|
|
|
return 0;
|
|
|
|
}
|
2015-01-28 05:18:26 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-01-28 21:46:17 +03:00
|
|
|
static int transport_bio_simple_uninit(BIO* bio)
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
2015-01-28 22:54:03 +03:00
|
|
|
WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*) bio->ptr;
|
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
if (bio->shutdown)
|
|
|
|
{
|
|
|
|
if (bio->init)
|
2015-01-28 05:18:26 +03:00
|
|
|
{
|
2015-02-11 18:57:14 +03:00
|
|
|
_shutdown(ptr->socket, SD_BOTH);
|
2015-01-28 22:54:03 +03:00
|
|
|
closesocket(ptr->socket);
|
|
|
|
ptr->socket = 0;
|
2015-01-28 05:18:26 +03:00
|
|
|
}
|
2015-01-28 21:46:17 +03:00
|
|
|
}
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2015-01-28 22:54:03 +03:00
|
|
|
if (ptr->hEvent)
|
2015-01-28 21:46:17 +03:00
|
|
|
{
|
2015-01-28 22:54:03 +03:00
|
|
|
CloseHandle(ptr->hEvent);
|
|
|
|
ptr->hEvent = NULL;
|
2014-06-02 05:37:20 +04:00
|
|
|
}
|
|
|
|
|
2015-01-28 21:46:17 +03:00
|
|
|
bio->init = 0;
|
|
|
|
bio->flags = 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_simple_new(BIO* bio)
|
|
|
|
{
|
2015-01-29 19:35:52 +03:00
|
|
|
WINPR_BIO_SIMPLE_SOCKET* ptr;
|
|
|
|
|
2015-01-28 21:46:17 +03:00
|
|
|
bio->init = 0;
|
|
|
|
bio->ptr = NULL;
|
|
|
|
bio->flags = BIO_FLAGS_SHOULD_RETRY;
|
|
|
|
|
2015-01-29 19:35:52 +03:00
|
|
|
ptr = (WINPR_BIO_SIMPLE_SOCKET*) calloc(1, sizeof(WINPR_BIO_SIMPLE_SOCKET));
|
2015-01-28 22:54:03 +03:00
|
|
|
|
2015-01-29 19:35:52 +03:00
|
|
|
if (!ptr)
|
2015-01-28 22:54:03 +03:00
|
|
|
return 0;
|
|
|
|
|
2015-01-29 19:35:52 +03:00
|
|
|
bio->ptr = ptr;
|
|
|
|
|
2015-01-28 21:46:17 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_simple_free(BIO* bio)
|
|
|
|
{
|
|
|
|
if (!bio)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
transport_bio_simple_uninit(bio);
|
|
|
|
|
2015-01-28 22:54:03 +03:00
|
|
|
if (bio->ptr)
|
|
|
|
{
|
|
|
|
free(bio->ptr);
|
|
|
|
bio->ptr = NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BIO_METHOD transport_bio_simple_socket_methods =
|
|
|
|
{
|
|
|
|
BIO_TYPE_SIMPLE,
|
|
|
|
"SimpleSocket",
|
|
|
|
transport_bio_simple_write,
|
|
|
|
transport_bio_simple_read,
|
|
|
|
transport_bio_simple_puts,
|
|
|
|
transport_bio_simple_gets,
|
|
|
|
transport_bio_simple_ctrl,
|
|
|
|
transport_bio_simple_new,
|
|
|
|
transport_bio_simple_free,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
BIO_METHOD* BIO_s_simple_socket(void)
|
|
|
|
{
|
|
|
|
return &transport_bio_simple_socket_methods;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Buffered Socket BIO */
|
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
struct _WINPR_BIO_BUFFERED_SOCKET
|
|
|
|
{
|
|
|
|
BIO* bufferedBio;
|
|
|
|
BOOL readBlocked;
|
|
|
|
BOOL writeBlocked;
|
|
|
|
RingBuffer xmitBuffer;
|
|
|
|
};
|
|
|
|
typedef struct _WINPR_BIO_BUFFERED_SOCKET WINPR_BIO_BUFFERED_SOCKET;
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
long transport_bio_buffered_callback(BIO* bio, int mode, const char* argp, int argi, long argl, long ret)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_buffered_write(BIO* bio, const char* buf, int num)
|
|
|
|
{
|
2015-02-14 18:14:13 +03:00
|
|
|
int i, ret;
|
|
|
|
int status;
|
|
|
|
int nchunks;
|
|
|
|
int committedBytes;
|
2014-05-21 19:32:14 +04:00
|
|
|
DataChunk chunks[2];
|
2015-02-14 18:14:13 +03:00
|
|
|
WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*) bio->ptr;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
|
|
|
ret = num;
|
2015-02-14 18:14:13 +03:00
|
|
|
ptr->writeBlocked = FALSE;
|
2014-06-02 05:37:20 +04:00
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_WRITE);
|
2014-05-21 19:32:14 +04:00
|
|
|
|
|
|
|
/* we directly append extra bytes in the xmit buffer, this could be prevented
|
|
|
|
* but for now it makes the code more simple.
|
|
|
|
*/
|
2015-02-14 18:14:13 +03:00
|
|
|
if (buf && num && !ringbuffer_write(&ptr->xmitBuffer, (const BYTE*) buf, num))
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2016-02-22 19:01:43 +03:00
|
|
|
WLog_ERR(TAG, "an error occurred when writing (num: %d)", num);
|
2014-05-21 19:32:14 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
committedBytes = 0;
|
2015-02-14 18:14:13 +03:00
|
|
|
nchunks = ringbuffer_peek(&ptr->xmitBuffer, chunks, ringbuffer_used(&ptr->xmitBuffer));
|
2014-05-30 22:53:10 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
for (i = 0; i < nchunks; i++)
|
|
|
|
{
|
|
|
|
while (chunks[i].size)
|
|
|
|
{
|
|
|
|
status = BIO_write(bio->next_bio, chunks[i].data, chunks[i].size);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (status <= 0)
|
|
|
|
{
|
2014-06-02 05:37:20 +04:00
|
|
|
if (!BIO_should_retry(bio->next_bio))
|
|
|
|
{
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
ret = -1; /* fatal error */
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BIO_should_write(bio->next_bio))
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2014-06-02 05:37:20 +04:00
|
|
|
BIO_set_flags(bio, BIO_FLAGS_WRITE);
|
2015-02-14 18:14:13 +03:00
|
|
|
ptr->writeBlocked = TRUE;
|
2014-05-21 19:32:14 +04:00
|
|
|
goto out; /* EWOULDBLOCK */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
committedBytes += status;
|
|
|
|
chunks[i].size -= status;
|
|
|
|
chunks[i].data += status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2015-02-14 18:14:13 +03:00
|
|
|
ringbuffer_commit_read_bytes(&ptr->xmitBuffer, committedBytes);
|
2015-02-13 22:26:02 +03:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_buffered_read(BIO* bio, char* buf, int size)
|
|
|
|
{
|
|
|
|
int status;
|
2015-02-14 18:14:13 +03:00
|
|
|
WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*) bio->ptr;
|
2014-05-30 22:53:10 +04:00
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
ptr->readBlocked = FALSE;
|
2014-06-02 05:37:20 +04:00
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_READ);
|
2014-05-21 19:32:14 +04:00
|
|
|
|
|
|
|
status = BIO_read(bio->next_bio, buf, size);
|
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
if (status <= 0)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2014-06-02 05:37:20 +04:00
|
|
|
if (!BIO_should_retry(bio->next_bio))
|
|
|
|
{
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
|
|
|
|
if (BIO_should_read(bio->next_bio))
|
|
|
|
{
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_READ);
|
2015-02-14 18:14:13 +03:00
|
|
|
ptr->readBlocked = TRUE;
|
2014-06-02 05:37:20 +04:00
|
|
|
goto out;
|
|
|
|
}
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
out:
|
2014-05-21 19:32:14 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_buffered_puts(BIO* bio, const char* str)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_buffered_gets(BIO* bio, char* str, int size)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long transport_bio_buffered_ctrl(BIO* bio, int cmd, long arg1, void* arg2)
|
|
|
|
{
|
2015-02-13 22:26:02 +03:00
|
|
|
int status = -1;
|
2015-02-14 18:14:13 +03:00
|
|
|
WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*) bio->ptr;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2014-06-02 05:37:20 +04:00
|
|
|
case BIO_CTRL_FLUSH:
|
2015-02-14 18:14:13 +03:00
|
|
|
if (!ringbuffer_used(&ptr->xmitBuffer))
|
2015-02-13 22:26:02 +03:00
|
|
|
status = 1;
|
|
|
|
else
|
|
|
|
status = (transport_bio_buffered_write(bio, NULL, 0) >= 0) ? 1 : -1;
|
|
|
|
break;
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
case BIO_CTRL_WPENDING:
|
2015-02-14 18:14:13 +03:00
|
|
|
status = ringbuffer_used(&ptr->xmitBuffer);
|
2015-02-13 22:26:02 +03:00
|
|
|
break;
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
case BIO_CTRL_PENDING:
|
2015-02-13 22:26:02 +03:00
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_C_READ_BLOCKED:
|
2015-02-14 18:14:13 +03:00
|
|
|
status = (int) ptr->readBlocked;
|
2015-02-13 22:26:02 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_C_WRITE_BLOCKED:
|
2015-02-14 18:14:13 +03:00
|
|
|
status = (int) ptr->writeBlocked;
|
2015-02-13 22:26:02 +03:00
|
|
|
break;
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
default:
|
2015-02-13 22:26:02 +03:00
|
|
|
status = BIO_ctrl(bio->next_bio, cmd, arg1, arg2);
|
|
|
|
break;
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2015-02-13 22:26:02 +03:00
|
|
|
return status;
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_buffered_new(BIO* bio)
|
|
|
|
{
|
2015-02-14 18:14:13 +03:00
|
|
|
WINPR_BIO_BUFFERED_SOCKET* ptr;
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
bio->init = 1;
|
|
|
|
bio->num = 0;
|
|
|
|
bio->ptr = NULL;
|
2014-06-02 05:37:20 +04:00
|
|
|
bio->flags = BIO_FLAGS_SHOULD_RETRY;
|
2015-02-14 18:14:13 +03:00
|
|
|
|
|
|
|
ptr = (WINPR_BIO_BUFFERED_SOCKET*) calloc(1, sizeof(WINPR_BIO_BUFFERED_SOCKET));
|
|
|
|
|
|
|
|
if (!ptr)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
bio->ptr = (void*) ptr;
|
|
|
|
|
|
|
|
if (!ringbuffer_init(&ptr->xmitBuffer, 0x10000))
|
|
|
|
return -1;
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_buffered_free(BIO* bio)
|
|
|
|
{
|
2015-02-14 18:14:13 +03:00
|
|
|
WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*) bio->ptr;
|
|
|
|
|
2015-02-18 23:36:57 +03:00
|
|
|
if (bio->next_bio)
|
2015-02-14 18:14:13 +03:00
|
|
|
{
|
2015-02-18 23:36:57 +03:00
|
|
|
BIO_free(bio->next_bio);
|
|
|
|
bio->next_bio = NULL;
|
2015-02-14 18:14:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ringbuffer_destroy(&ptr->xmitBuffer);
|
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
free(ptr);
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BIO_METHOD transport_bio_buffered_socket_methods =
|
|
|
|
{
|
|
|
|
BIO_TYPE_BUFFERED,
|
|
|
|
"BufferedSocket",
|
|
|
|
transport_bio_buffered_write,
|
|
|
|
transport_bio_buffered_read,
|
|
|
|
transport_bio_buffered_puts,
|
|
|
|
transport_bio_buffered_gets,
|
|
|
|
transport_bio_buffered_ctrl,
|
|
|
|
transport_bio_buffered_new,
|
|
|
|
transport_bio_buffered_free,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
BIO_METHOD* BIO_s_buffered_socket(void)
|
|
|
|
{
|
|
|
|
return &transport_bio_buffered_socket_methods;
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:26:02 +03:00
|
|
|
char* freerdp_tcp_get_ip_address(int sockfd)
|
2011-07-10 20:10:24 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* ip;
|
2011-07-10 20:10:24 +04:00
|
|
|
socklen_t length;
|
2015-02-13 22:26:02 +03:00
|
|
|
char ipAddress[32];
|
2011-07-10 20:10:24 +04:00
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
|
|
|
|
length = sizeof(sockaddr);
|
2013-11-08 02:37:58 +04:00
|
|
|
ZeroMemory(&sockaddr, length);
|
2011-07-10 20:10:24 +04:00
|
|
|
|
2015-02-13 22:26:02 +03:00
|
|
|
if (getsockname(sockfd, (struct sockaddr*) &sockaddr, &length) == 0)
|
2011-07-10 20:10:24 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
ip = (BYTE*) (&sockaddr.sin_addr);
|
2015-02-13 22:26:02 +03:00
|
|
|
sprintf_s(ipAddress, sizeof(ipAddress), "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
|
2011-07-10 20:10:24 +04:00
|
|
|
}
|
|
|
|
else
|
2011-07-27 03:14:11 +04:00
|
|
|
{
|
2015-02-13 22:26:02 +03:00
|
|
|
strcpy(ipAddress, "127.0.0.1");
|
2011-07-27 03:14:11 +04:00
|
|
|
}
|
2011-07-10 20:10:24 +04:00
|
|
|
|
2015-02-13 22:26:02 +03:00
|
|
|
return _strdup(ipAddress);
|
2011-07-10 20:10:24 +04:00
|
|
|
}
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
static int freerdp_uds_connect(const char* path)
|
2014-11-12 22:06:34 +03:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
int status;
|
|
|
|
int sockfd;
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
|
|
|
|
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
if (sockfd == -1)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "socket");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strncpy(addr.sun_path, path, sizeof(addr.sun_path));
|
|
|
|
status = connect(sockfd, (struct sockaddr *) &addr, sizeof(addr));
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "connect");
|
|
|
|
close(sockfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sockfd;
|
|
|
|
#else /* ifndef _WIN32 */
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-12-26 21:49:25 +03:00
|
|
|
BOOL freerdp_tcp_resolve_hostname(const char* hostname)
|
|
|
|
{
|
|
|
|
int status;
|
2014-12-27 21:50:50 +03:00
|
|
|
struct addrinfo hints = { 0 };
|
2014-12-26 21:49:25 +03:00
|
|
|
struct addrinfo* result = NULL;
|
|
|
|
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
status = getaddrinfo(hostname, NULL, &hints, &result);
|
|
|
|
|
|
|
|
if (status)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
freeaddrinfo(result);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
static BOOL freerdp_tcp_connect_timeout(rdpContext* context, int sockfd,
|
|
|
|
struct sockaddr* addr,
|
|
|
|
socklen_t addrlen, int timeout)
|
2014-12-28 00:48:32 +03:00
|
|
|
{
|
2015-07-03 12:49:40 +03:00
|
|
|
HANDLE handles[2];
|
|
|
|
int status = 0;
|
|
|
|
int count = 0;
|
2015-07-14 12:58:01 +03:00
|
|
|
u_long arg = 0;
|
2015-07-03 12:49:40 +03:00
|
|
|
DWORD tout = (timeout) ? timeout * 1000 : INFINITE;
|
2014-12-28 00:48:32 +03:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
handles[count] = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!handles[count])
|
2014-12-28 00:48:32 +03:00
|
|
|
return FALSE;
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
status = WSAEventSelect(sockfd, handles[count++], FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE);
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WSAEventSelect failed with %lX", WSAGetLastError());
|
2014-12-28 00:48:32 +03:00
|
|
|
return FALSE;
|
2015-07-14 12:58:01 +03:00
|
|
|
}
|
2014-12-28 00:48:32 +03:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
handles[count++] = context->abortEvent;
|
2014-12-28 02:53:27 +03:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
status = _connect(sockfd, addr, addrlen);
|
|
|
|
if (status < 0)
|
2014-12-28 02:53:27 +03:00
|
|
|
{
|
2015-07-03 12:49:40 +03:00
|
|
|
status = WSAGetLastError();
|
|
|
|
switch(status)
|
2014-12-28 02:53:27 +03:00
|
|
|
{
|
2015-07-03 12:49:40 +03:00
|
|
|
case WSAEINPROGRESS:
|
|
|
|
case WSAEWOULDBLOCK:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
2014-12-28 02:53:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
status = WaitForMultipleObjects(count, handles, FALSE, tout);
|
|
|
|
if (WAIT_OBJECT_0 != status)
|
2014-12-28 02:53:27 +03:00
|
|
|
{
|
2015-07-03 12:49:40 +03:00
|
|
|
if (status == WAIT_OBJECT_0 + 1)
|
|
|
|
freerdp_set_last_error(context, FREERDP_ERROR_CONNECT_CANCELLED);
|
2014-12-28 02:53:27 +03:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
return FALSE;
|
2014-12-28 02:53:27 +03:00
|
|
|
}
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
status = recv(sockfd, NULL, 0, 0);
|
|
|
|
if (status == SOCKET_ERROR)
|
2014-12-28 02:53:27 +03:00
|
|
|
{
|
2015-07-03 12:49:40 +03:00
|
|
|
if (WSAGetLastError() == WSAECONNRESET)
|
|
|
|
return FALSE;
|
2014-12-28 02:53:27 +03:00
|
|
|
}
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
status = WSAEventSelect(sockfd, handles[0], 0);
|
|
|
|
CloseHandle(handles[0]);
|
2014-12-28 02:53:27 +03:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
if (status < 0)
|
2014-12-28 02:53:27 +03:00
|
|
|
{
|
2015-07-03 12:49:40 +03:00
|
|
|
WLog_ERR(TAG, "WSAEventSelect failed with %lX", WSAGetLastError());
|
|
|
|
return FALSE;
|
2014-12-28 02:53:27 +03:00
|
|
|
}
|
|
|
|
|
2015-07-14 12:58:01 +03:00
|
|
|
if (_ioctlsocket(sockfd, FIONBIO, &arg) != 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
return TRUE;
|
2014-12-28 02:53:27 +03:00
|
|
|
}
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
static int freerdp_tcp_connect_multi(rdpContext* context, char** hostnames,
|
|
|
|
UINT32* ports, int count, int port,
|
|
|
|
int timeout)
|
2015-05-07 21:20:49 +03:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int sindex;
|
|
|
|
int status;
|
2015-07-03 12:49:40 +03:00
|
|
|
SOCKET sockfd = -1;
|
2015-05-07 21:20:49 +03:00
|
|
|
SOCKET* sockfds;
|
|
|
|
HANDLE* events;
|
|
|
|
DWORD waitStatus;
|
|
|
|
char port_str[16];
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo* addr;
|
|
|
|
struct addrinfo* result;
|
|
|
|
struct addrinfo** addrs;
|
|
|
|
struct addrinfo** results;
|
|
|
|
|
|
|
|
sprintf_s(port_str, sizeof(port_str) - 1, "%u", port);
|
|
|
|
|
|
|
|
sockfds = (SOCKET*) calloc(count, sizeof(SOCKET));
|
2015-07-03 12:49:40 +03:00
|
|
|
events = (HANDLE*) calloc(count + 1, sizeof(HANDLE));
|
2015-05-07 21:20:49 +03:00
|
|
|
addrs = (struct addrinfo**) calloc(count, sizeof(struct addrinfo*));
|
|
|
|
results = (struct addrinfo**) calloc(count, sizeof(struct addrinfo*));
|
|
|
|
|
|
|
|
if (!sockfds || !events || !addrs || !results)
|
2015-05-22 21:14:57 +03:00
|
|
|
{
|
|
|
|
free(sockfds);
|
|
|
|
free(events);
|
|
|
|
free(addrs);
|
|
|
|
free(results);
|
2015-05-07 21:20:49 +03:00
|
|
|
return -1;
|
2015-05-22 21:14:57 +03:00
|
|
|
}
|
2015-05-07 21:20:49 +03:00
|
|
|
|
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
{
|
|
|
|
ZeroMemory(&hints, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
if (ports)
|
|
|
|
sprintf_s(port_str, sizeof(port_str) - 1, "%u", ports[index]);
|
|
|
|
|
|
|
|
status = getaddrinfo(hostnames[index], port_str, &hints, &result);
|
|
|
|
|
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = result;
|
|
|
|
|
|
|
|
if ((addr->ai_family == AF_INET6) && (addr->ai_next != 0))
|
|
|
|
{
|
|
|
|
while ((addr = addr->ai_next))
|
|
|
|
{
|
|
|
|
if (addr->ai_family == AF_INET)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!addr)
|
|
|
|
addr = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
sockfds[index] = _socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
|
|
|
|
2016-02-03 13:45:22 +03:00
|
|
|
if (sockfds[index] == INVALID_SOCKET)
|
2015-05-07 21:20:49 +03:00
|
|
|
{
|
|
|
|
freeaddrinfo(result);
|
|
|
|
sockfds[index] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs[index] = addr;
|
|
|
|
results[index] = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
{
|
|
|
|
if (!sockfds[index])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sockfd = sockfds[index];
|
|
|
|
addr = addrs[index];
|
|
|
|
|
|
|
|
/* set socket in non-blocking mode */
|
2015-07-29 16:52:52 +03:00
|
|
|
events[index] = WSACreateEvent();
|
|
|
|
if (!events[index])
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WSACreateEvent returned %08X", WSAGetLastError());
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-07 21:20:49 +03:00
|
|
|
|
2015-07-14 12:58:01 +03:00
|
|
|
if (WSAEventSelect(sockfd, events[index], FD_READ | FD_WRITE | FD_CONNECT | FD_CLOSE))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WSAEventSelect returned %08X", WSAGetLastError());
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-07 21:20:49 +03:00
|
|
|
|
|
|
|
/* non-blocking tcp connect */
|
|
|
|
|
|
|
|
status = _connect(sockfd, addr->ai_addr, addr->ai_addrlen);
|
|
|
|
|
|
|
|
if (status >= 0)
|
|
|
|
{
|
|
|
|
/* connection success */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
events[count] = context->abortEvent;
|
|
|
|
|
|
|
|
waitStatus = WaitForMultipleObjects(count + 1, events, FALSE, timeout * 1000);
|
2015-05-07 21:20:49 +03:00
|
|
|
|
|
|
|
sindex = waitStatus - WAIT_OBJECT_0;
|
|
|
|
|
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
{
|
2015-07-14 12:58:01 +03:00
|
|
|
u_long arg = 0;
|
|
|
|
|
2015-05-07 21:20:49 +03:00
|
|
|
if (!sockfds[index])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sockfd = sockfds[index];
|
|
|
|
|
|
|
|
/* set socket in blocking mode */
|
2015-07-14 12:58:01 +03:00
|
|
|
if (WSAEventSelect(sockfd, NULL, 0))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WSAEventSelect returned %08X", WSAGetLastError());
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-07 21:20:49 +03:00
|
|
|
|
2015-07-14 12:58:01 +03:00
|
|
|
if (_ioctlsocket(sockfd, FIONBIO, &arg))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "_ioctlsocket failed");
|
|
|
|
}
|
2015-05-07 21:20:49 +03:00
|
|
|
}
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
if ((sindex >= 0) && (sindex < count))
|
2015-05-07 21:20:49 +03:00
|
|
|
{
|
|
|
|
sockfd = sockfds[sindex];
|
|
|
|
}
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
if (sindex == count)
|
|
|
|
freerdp_set_last_error(context, FREERDP_ERROR_CONNECT_CANCELLED);
|
|
|
|
|
2015-05-07 21:20:49 +03:00
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
{
|
|
|
|
if (results[index])
|
|
|
|
freeaddrinfo(results[index]);
|
2015-07-29 16:52:52 +03:00
|
|
|
CloseHandle(events[index]);
|
2015-05-07 21:20:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
free(addrs);
|
|
|
|
free(results);
|
|
|
|
free(sockfds);
|
|
|
|
free(events);
|
|
|
|
|
|
|
|
return sockfd;
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:26:02 +03:00
|
|
|
BOOL freerdp_tcp_set_keep_alive_mode(int sockfd)
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
UINT32 optval;
|
|
|
|
socklen_t optlen;
|
|
|
|
|
|
|
|
optval = 1;
|
|
|
|
optlen = sizeof(optval);
|
|
|
|
|
|
|
|
if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (void*) &optval, optlen) < 0)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "setsockopt() SOL_SOCKET, SO_KEEPALIVE");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TCP_KEEPIDLE
|
|
|
|
optval = 5;
|
|
|
|
optlen = sizeof(optval);
|
|
|
|
|
|
|
|
if (setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (void*) &optval, optlen) < 0)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "setsockopt() IPPROTO_TCP, TCP_KEEPIDLE");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TCP_KEEPCNT
|
|
|
|
optval = 3;
|
|
|
|
optlen = sizeof(optval);
|
|
|
|
|
|
|
|
if (setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, (void*) &optval, optlen) < 0)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "setsockopt() SOL_TCP, TCP_KEEPCNT");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TCP_KEEPINTVL
|
|
|
|
optval = 2;
|
|
|
|
optlen = sizeof(optval);
|
|
|
|
|
|
|
|
if (setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, (void*) &optval, optlen) < 0)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "setsockopt() SOL_TCP, TCP_KEEPINTVL");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__MACOSX__) || defined(__IOS__)
|
|
|
|
optval = 1;
|
|
|
|
optlen = sizeof(optval);
|
|
|
|
if (setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void*) &optval, optlen) < 0)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "setsockopt() SOL_SOCKET, SO_NOSIGPIPE");
|
|
|
|
}
|
|
|
|
#endif
|
2015-02-20 15:22:01 +03:00
|
|
|
|
|
|
|
#ifdef TCP_USER_TIMEOUT
|
2016-02-23 17:34:58 +03:00
|
|
|
optval = 9000;
|
2015-02-20 15:22:01 +03:00
|
|
|
optlen = sizeof(optval);
|
|
|
|
|
|
|
|
if (setsockopt(sockfd, SOL_TCP, TCP_USER_TIMEOUT, (void*) &optval, optlen) < 0)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "setsockopt() SOL_TCP, TCP_USER_TIMEOUT");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-13 22:26:02 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
int freerdp_tcp_connect(rdpContext* context, rdpSettings* settings,
|
|
|
|
const char* hostname, int port, int timeout)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2014-05-30 22:03:20 +04:00
|
|
|
int status;
|
2015-02-14 00:51:08 +03:00
|
|
|
int sockfd;
|
|
|
|
UINT32 optval;
|
|
|
|
socklen_t optlen;
|
|
|
|
BOOL ipcSocket = FALSE;
|
2015-11-20 04:44:35 +03:00
|
|
|
BOOL useExternalDefinedSocket = FALSE;
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2013-10-11 13:07:33 +04:00
|
|
|
if (!hostname)
|
2015-02-14 18:14:13 +03:00
|
|
|
return -1;
|
2013-05-14 12:24:43 +04:00
|
|
|
|
2012-05-22 15:57:19 +04:00
|
|
|
if (hostname[0] == '/')
|
2015-02-14 00:51:08 +03:00
|
|
|
ipcSocket = TRUE;
|
2014-08-06 20:08:00 +04:00
|
|
|
|
2015-11-20 04:44:35 +03:00
|
|
|
if (hostname[0] == '|')
|
|
|
|
useExternalDefinedSocket = TRUE;
|
|
|
|
|
2015-02-14 00:51:08 +03:00
|
|
|
if (ipcSocket)
|
2012-05-22 15:57:19 +04:00
|
|
|
{
|
2015-02-14 00:51:08 +03:00
|
|
|
sockfd = freerdp_uds_connect(hostname);
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2015-02-14 00:51:08 +03:00
|
|
|
if (sockfd < 0)
|
2015-02-14 18:14:13 +03:00
|
|
|
return -1;
|
2012-05-22 15:57:19 +04:00
|
|
|
}
|
2015-11-26 11:50:03 +03:00
|
|
|
else if (useExternalDefinedSocket)
|
|
|
|
sockfd = port;
|
2012-05-22 15:57:19 +04:00
|
|
|
else
|
|
|
|
{
|
2015-02-14 00:51:08 +03:00
|
|
|
sockfd = -1;
|
2014-10-17 14:08:39 +04:00
|
|
|
|
2014-12-26 21:49:25 +03:00
|
|
|
if (!settings->GatewayEnabled)
|
|
|
|
{
|
2015-05-06 23:32:45 +03:00
|
|
|
if (!freerdp_tcp_resolve_hostname(hostname) || settings->RemoteAssistanceMode)
|
2014-12-26 21:49:25 +03:00
|
|
|
{
|
|
|
|
if (settings->TargetNetAddressCount > 0)
|
|
|
|
{
|
2015-07-03 12:49:40 +03:00
|
|
|
sockfd = freerdp_tcp_connect_multi(
|
|
|
|
context,
|
|
|
|
settings->TargetNetAddresses,
|
|
|
|
settings->TargetNetPorts,
|
|
|
|
settings->TargetNetAddressCount,
|
|
|
|
port, timeout);
|
2014-12-26 21:49:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-14 00:51:08 +03:00
|
|
|
if (sockfd <= 0)
|
2014-12-28 02:53:27 +03:00
|
|
|
{
|
|
|
|
char port_str[16];
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo* addr;
|
|
|
|
struct addrinfo* result;
|
2014-12-26 21:49:25 +03:00
|
|
|
|
2014-12-28 02:53:27 +03:00
|
|
|
ZeroMemory(&hints, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2014-10-17 14:08:39 +04:00
|
|
|
|
2014-12-28 02:53:27 +03:00
|
|
|
sprintf_s(port_str, sizeof(port_str) - 1, "%u", port);
|
2014-12-28 00:48:32 +03:00
|
|
|
|
2014-12-28 02:53:27 +03:00
|
|
|
status = getaddrinfo(hostname, port_str, &hints, &result);
|
2014-10-17 14:08:39 +04:00
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
if (status)
|
|
|
|
{
|
2014-12-28 02:53:27 +03:00
|
|
|
WLog_ERR(TAG, "getaddrinfo: %s", gai_strerror(status));
|
2015-02-14 18:14:13 +03:00
|
|
|
return -1;
|
2014-12-28 02:53:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
addr = result;
|
2015-02-14 00:51:08 +03:00
|
|
|
|
2014-12-28 02:53:27 +03:00
|
|
|
if ((addr->ai_family == AF_INET6) && (addr->ai_next != 0))
|
2014-10-17 14:08:39 +04:00
|
|
|
{
|
2014-12-28 02:53:27 +03:00
|
|
|
while ((addr = addr->ai_next))
|
|
|
|
{
|
|
|
|
if (addr->ai_family == AF_INET)
|
|
|
|
break;
|
|
|
|
}
|
2015-02-14 00:51:08 +03:00
|
|
|
|
2014-12-28 02:53:27 +03:00
|
|
|
if (!addr)
|
|
|
|
addr = result;
|
2014-10-17 14:08:39 +04:00
|
|
|
}
|
2014-12-28 00:48:32 +03:00
|
|
|
|
2015-02-14 00:51:08 +03:00
|
|
|
sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
2014-10-17 14:08:39 +04:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
if (sockfd < 0)
|
2015-02-14 00:51:08 +03:00
|
|
|
{
|
2014-12-28 02:53:27 +03:00
|
|
|
freeaddrinfo(result);
|
2015-02-14 18:14:13 +03:00
|
|
|
return -1;
|
2014-12-28 02:53:27 +03:00
|
|
|
}
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
if (!freerdp_tcp_connect_timeout(context, sockfd, addr->ai_addr,
|
|
|
|
addr->ai_addrlen, timeout))
|
2014-12-28 02:53:27 +03:00
|
|
|
{
|
|
|
|
freeaddrinfo(result);
|
2015-06-17 23:08:02 +03:00
|
|
|
close(sockfd);
|
|
|
|
WLog_ERR(TAG, "failed to connect to %s", hostname);
|
2015-02-14 18:14:13 +03:00
|
|
|
return -1;
|
2014-12-28 02:53:27 +03:00
|
|
|
}
|
2014-10-17 14:08:39 +04:00
|
|
|
|
|
|
|
freeaddrinfo(result);
|
|
|
|
}
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2015-02-13 22:26:02 +03:00
|
|
|
settings->IPv6Enabled = FALSE;
|
|
|
|
|
|
|
|
free(settings->ClientAddress);
|
2015-02-14 00:51:08 +03:00
|
|
|
settings->ClientAddress = freerdp_tcp_get_ip_address(sockfd);
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!settings->ClientAddress)
|
|
|
|
{
|
2015-11-25 10:36:49 +03:00
|
|
|
if (!useExternalDefinedSocket)
|
|
|
|
close(sockfd);
|
2015-06-17 23:08:02 +03:00
|
|
|
WLog_ERR(TAG, "Couldn't get socket ip address");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-12-12 03:59:35 +04:00
|
|
|
|
2015-02-14 00:51:08 +03:00
|
|
|
optval = 1;
|
|
|
|
optlen = sizeof(optval);
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2015-11-25 10:36:49 +03:00
|
|
|
if (!ipcSocket && !useExternalDefinedSocket)
|
2014-08-06 20:08:00 +04:00
|
|
|
{
|
2015-02-14 00:51:08 +03:00
|
|
|
if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (void*) &optval, optlen) < 0)
|
2015-02-14 00:02:37 +03:00
|
|
|
WLog_ERR(TAG, "unable to set TCP_NODELAY");
|
2014-08-06 20:08:00 +04:00
|
|
|
}
|
2012-05-22 15:57:19 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
/* receive buffer must be a least 32 K */
|
2015-02-14 00:51:08 +03:00
|
|
|
if (getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &optval, &optlen) == 0)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2015-02-14 00:51:08 +03:00
|
|
|
if (optval < (1024 * 32))
|
2011-12-12 03:59:35 +04:00
|
|
|
{
|
2015-02-14 00:51:08 +03:00
|
|
|
optval = 1024 * 32;
|
|
|
|
optlen = sizeof(optval);
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2015-02-14 00:51:08 +03:00
|
|
|
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &optval, optlen) < 0)
|
2012-05-22 15:57:19 +04:00
|
|
|
{
|
2015-06-17 23:08:02 +03:00
|
|
|
close(sockfd);
|
2015-02-14 00:02:37 +03:00
|
|
|
WLog_ERR(TAG, "unable to set receive buffer len");
|
2015-02-14 18:14:13 +03:00
|
|
|
return -1;
|
2012-05-22 15:57:19 +04:00
|
|
|
}
|
2011-12-12 03:59:35 +04:00
|
|
|
}
|
2012-05-22 15:57:19 +04:00
|
|
|
}
|
2011-12-12 04:51:58 +04:00
|
|
|
|
2015-11-25 10:36:49 +03:00
|
|
|
if (!ipcSocket && !useExternalDefinedSocket)
|
2014-08-06 20:08:00 +04:00
|
|
|
{
|
2015-02-14 00:51:08 +03:00
|
|
|
if (!freerdp_tcp_set_keep_alive_mode(sockfd))
|
2015-06-17 23:08:02 +03:00
|
|
|
{
|
|
|
|
close(sockfd);
|
|
|
|
WLog_ERR(TAG, "Couldn't set keep alive mode.");
|
2014-05-21 19:32:14 +04:00
|
|
|
return -1;
|
2015-06-17 23:08:02 +03:00
|
|
|
}
|
2015-01-28 05:18:26 +03:00
|
|
|
}
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
if (WaitForSingleObject(context->abortEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
close(sockfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
return sockfd;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|