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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2023-10-16 13:59:19 +03:00
|
|
|
#include "settings.h"
|
|
|
|
|
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>
|
2016-08-10 12:06:34 +03:00
|
|
|
#include <winpr/platform.h>
|
2014-06-02 05:37:20 +04:00
|
|
|
#include <winpr/winsock.h>
|
2012-10-09 07:42:01 +04:00
|
|
|
|
2020-09-02 14:37:04 +03:00
|
|
|
#include "rdp.h"
|
2022-04-19 15:29:17 +03:00
|
|
|
#include "utils.h"
|
2020-09-02 14:37:04 +03: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
|
|
|
|
2023-01-10 16:47:16 +03:00
|
|
|
#ifdef WINPR_HAVE_POLL_H
|
2014-07-02 17:15:46 +04:00
|
|
|
#include <poll.h>
|
|
|
|
#else
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
2015-04-21 21:42:06 +03:00
|
|
|
#if defined(__FreeBSD__) || defined(__OpenBSD__)
|
2014-05-01 17:09:35 +04:00
|
|
|
#ifndef SOL_TCP
|
2019-11-06 17:24:51 +03:00
|
|
|
#define SOL_TCP IPPROTO_TCP
|
2014-05-01 17:09:35 +04:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2011-12-14 04:42:10 +04:00
|
|
|
#ifdef __APPLE__
|
2014-01-21 17:27:21 +04:00
|
|
|
#ifndef SOL_TCP
|
2019-11-06 17:24:51 +03: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"
|
2016-11-21 19:28:54 +03:00
|
|
|
#include "../crypto/opensslcompat.h"
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2023-11-14 18:33:53 +03:00
|
|
|
#if defined(HAVE_AF_VSOCK_H)
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <linux/vm_sockets.h>
|
|
|
|
#endif
|
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#define TAG FREERDP_TAG("core")
|
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
/* Simple Socket BIO */
|
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
typedef struct
|
2015-01-28 22:54:03 +03:00
|
|
|
{
|
|
|
|
SOCKET socket;
|
|
|
|
HANDLE hEvent;
|
2022-02-14 16:59:22 +03:00
|
|
|
} WINPR_BIO_SIMPLE_SOCKET;
|
2015-01-28 22:54:03 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-11-07 15:02:57 +03:00
|
|
|
static long transport_bio_simple_callback(BIO* bio, int mode, const char* argp, int argi, long argl,
|
|
|
|
long ret)
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_simple_write(BIO* bio, const char* buf, int size)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int error = 0;
|
2014-06-02 05:37:20 +04:00
|
|
|
int status = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
|
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();
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
|
|
|
|
(error == WSAEALREADY))
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int error = 0;
|
2014-06-02 05:37:20 +04:00
|
|
|
int status = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
|
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();
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if ((error == WSAEWOULDBLOCK) || (error == WSAEINTR) || (error == WSAEINPROGRESS) ||
|
|
|
|
(error == WSAEALREADY))
|
2014-07-11 01:35:11 +04:00
|
|
|
{
|
|
|
|
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;
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
|
2015-01-28 22:54:03 +03:00
|
|
|
|
2022-04-25 10:47:14 +03:00
|
|
|
switch (cmd)
|
2015-01-28 05:18:26 +03:00
|
|
|
{
|
2022-04-25 10:47:14 +03:00
|
|
|
case BIO_C_SET_SOCKET:
|
|
|
|
transport_bio_simple_uninit(bio);
|
|
|
|
transport_bio_simple_init(bio, (SOCKET)arg2, (int)arg1);
|
|
|
|
return 1;
|
|
|
|
case BIO_C_GET_SOCKET:
|
|
|
|
if (!BIO_get_init(bio) || !arg2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*((SOCKET*)arg2) = ptr->socket;
|
|
|
|
return 1;
|
|
|
|
case BIO_C_GET_EVENT:
|
|
|
|
if (!BIO_get_init(bio) || !arg2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*((HANDLE*)arg2) = ptr->hEvent;
|
|
|
|
return 1;
|
|
|
|
case BIO_C_SET_NONBLOCK:
|
|
|
|
{
|
2015-01-28 05:18:26 +03:00
|
|
|
#ifndef _WIN32
|
2024-01-23 18:49:54 +03:00
|
|
|
int flags = 0;
|
2022-06-23 08:57:38 +03:00
|
|
|
flags = fcntl((int)ptr->socket, F_GETFL);
|
2015-01-28 05:18:26 +03:00
|
|
|
|
2022-06-23 08:57:38 +03:00
|
|
|
if (flags == -1)
|
|
|
|
return 0;
|
2015-01-28 05:18:26 +03:00
|
|
|
|
2022-06-23 08:57:38 +03:00
|
|
|
if (arg1)
|
|
|
|
fcntl((int)ptr->socket, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
else
|
|
|
|
fcntl((int)ptr->socket, F_SETFL, flags & ~(O_NONBLOCK));
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2015-01-28 05:18:26 +03:00
|
|
|
#else
|
2022-06-23 08:57:38 +03:00
|
|
|
/* the internal socket is always non-blocking */
|
2015-01-28 05:18:26 +03:00
|
|
|
#endif
|
2022-06-23 08:57:38 +03:00
|
|
|
return 1;
|
2022-04-25 10:47:14 +03:00
|
|
|
}
|
|
|
|
case BIO_C_WAIT_READ:
|
|
|
|
{
|
|
|
|
int timeout = (int)arg1;
|
|
|
|
int sockfd = (int)ptr->socket;
|
2023-01-10 16:47:16 +03:00
|
|
|
#ifdef WINPR_HAVE_POLL_H
|
2022-06-23 08:57:38 +03:00
|
|
|
struct pollfd pollset;
|
|
|
|
pollset.fd = sockfd;
|
|
|
|
pollset.events = POLLIN;
|
|
|
|
pollset.revents = 0;
|
2015-02-13 23:22:27 +03:00
|
|
|
|
2022-06-23 08:57:38 +03:00
|
|
|
do
|
|
|
|
{
|
|
|
|
status = poll(&pollset, 1, timeout);
|
|
|
|
} while ((status < 0) && (errno == EINTR));
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2015-02-13 23:22:27 +03:00
|
|
|
#else
|
2022-06-23 08:57:38 +03:00
|
|
|
fd_set rset;
|
|
|
|
struct timeval tv;
|
|
|
|
FD_ZERO(&rset);
|
|
|
|
FD_SET(sockfd, &rset);
|
2015-02-13 23:22:27 +03:00
|
|
|
|
2022-06-23 08:57:38 +03:00
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
}
|
2015-02-13 23:22:27 +03:00
|
|
|
|
2022-06-23 08:57:38 +03:00
|
|
|
do
|
|
|
|
{
|
|
|
|
status = select(sockfd + 1, &rset, NULL, NULL, timeout ? &tv : NULL);
|
|
|
|
} while ((status < 0) && (errno == EINTR));
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2015-02-13 23:22:27 +03:00
|
|
|
#endif
|
2024-06-24 11:30:38 +03:00
|
|
|
/* Convert timeout to error return */
|
|
|
|
if (status == 0)
|
|
|
|
errno = ETIMEDOUT;
|
2022-04-25 10:47:14 +03:00
|
|
|
}
|
|
|
|
break;
|
2024-06-24 11:30:38 +03:00
|
|
|
|
2022-04-25 10:47:14 +03:00
|
|
|
case BIO_C_WAIT_WRITE:
|
|
|
|
{
|
|
|
|
int timeout = (int)arg1;
|
|
|
|
int sockfd = (int)ptr->socket;
|
2023-01-10 16:47:16 +03:00
|
|
|
#ifdef WINPR_HAVE_POLL_H
|
2022-06-23 08:57:38 +03:00
|
|
|
struct pollfd pollset;
|
|
|
|
pollset.fd = sockfd;
|
|
|
|
pollset.events = POLLOUT;
|
|
|
|
pollset.revents = 0;
|
2015-02-13 23:22:27 +03:00
|
|
|
|
2022-06-23 08:57:38 +03:00
|
|
|
do
|
|
|
|
{
|
|
|
|
status = poll(&pollset, 1, timeout);
|
|
|
|
} while ((status < 0) && (errno == EINTR));
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2015-02-13 23:22:27 +03:00
|
|
|
#else
|
2022-06-23 08:57:38 +03:00
|
|
|
fd_set rset;
|
|
|
|
struct timeval tv;
|
|
|
|
FD_ZERO(&rset);
|
|
|
|
FD_SET(sockfd, &rset);
|
2015-02-13 23:22:27 +03:00
|
|
|
|
2022-06-23 08:57:38 +03:00
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
}
|
2015-02-13 23:22:27 +03:00
|
|
|
|
2022-06-23 08:57:38 +03:00
|
|
|
do
|
|
|
|
{
|
|
|
|
status = select(sockfd + 1, NULL, &rset, NULL, timeout ? &tv : NULL);
|
|
|
|
} while ((status < 0) && (errno == EINTR));
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2015-02-13 23:22:27 +03:00
|
|
|
#endif
|
2024-06-24 11:30:38 +03:00
|
|
|
/* Convert timeout to error return */
|
|
|
|
if (status == 0)
|
|
|
|
errno = ETIMEDOUT;
|
2022-04-25 10:47:14 +03:00
|
|
|
}
|
|
|
|
break;
|
2015-01-28 05:18:26 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
case BIO_C_SET_FD:
|
|
|
|
if (arg2)
|
|
|
|
{
|
2015-01-28 21:46:17 +03:00
|
|
|
transport_bio_simple_uninit(bio);
|
2019-11-06 17:24:51 +03:00
|
|
|
transport_bio_simple_init(bio, (SOCKET) * ((int*)arg2), (int)arg1);
|
2014-06-02 05:37:20 +04:00
|
|
|
status = 1;
|
|
|
|
}
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_C_GET_FD:
|
2016-11-21 19:28:54 +03:00
|
|
|
if (BIO_get_init(bio))
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
|
|
|
if (arg2)
|
2019-11-06 17:24:51 +03:00
|
|
|
*((int*)arg2) = (int)ptr->socket;
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
status = (int)ptr->socket;
|
2014-06-02 05:37:20 +04:00
|
|
|
}
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_GET_CLOSE:
|
2016-11-21 19:28:54 +03:00
|
|
|
status = BIO_get_shutdown(bio);
|
2014-06-02 05:37:20 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_SET_CLOSE:
|
2019-11-06 17:24:51 +03:00
|
|
|
BIO_set_shutdown(bio, (int)arg1);
|
2014-06-02 05:37:20 +04:00
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_FLUSH:
|
2024-06-24 11:30:38 +03:00
|
|
|
case BIO_CTRL_DUP:
|
2014-06-02 05:37:20 +04:00
|
|
|
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
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
|
2015-01-28 22:54:03 +03:00
|
|
|
ptr->socket = socket;
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_shutdown(bio, shutdown);
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
BIO_set_init(bio, 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
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "WSAEventSelect returned 0x%08X", WSAGetLastError());
|
2015-07-14 12:58:01 +03:00
|
|
|
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
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
|
2015-01-28 22:54:03 +03:00
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
if (BIO_get_shutdown(bio))
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
2018-11-07 19:01:28 +03:00
|
|
|
if (BIO_get_init(bio) && ptr)
|
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
|
|
|
|
2018-11-07 19:01:28 +03:00
|
|
|
if (ptr && 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
|
|
|
}
|
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_init(bio, 0);
|
|
|
|
BIO_set_flags(bio, 0);
|
2015-01-28 21:46:17 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_simple_new(BIO* bio)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
WINPR_BIO_SIMPLE_SOCKET* ptr = NULL;
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
2019-11-06 17:24:51 +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;
|
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_data(bio, ptr);
|
2015-01-28 21:46:17 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_simple_free(BIO* bio)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
WINPR_BIO_SIMPLE_SOCKET* ptr = NULL;
|
2016-11-21 19:28:54 +03:00
|
|
|
|
2015-01-28 21:46:17 +03:00
|
|
|
if (!bio)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
transport_bio_simple_uninit(bio);
|
2019-11-06 17:24:51 +03:00
|
|
|
ptr = (WINPR_BIO_SIMPLE_SOCKET*)BIO_get_data(bio);
|
2016-11-21 19:28:54 +03:00
|
|
|
|
|
|
|
if (ptr)
|
2015-01-28 22:54:03 +03:00
|
|
|
{
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_data(bio, NULL);
|
|
|
|
free(ptr);
|
2015-01-28 22:54:03 +03:00
|
|
|
}
|
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIO_METHOD* BIO_s_simple_socket(void)
|
|
|
|
{
|
2016-11-21 19:28:54 +03:00
|
|
|
static BIO_METHOD* bio_methods = NULL;
|
|
|
|
|
|
|
|
if (bio_methods == NULL)
|
|
|
|
{
|
|
|
|
if (!(bio_methods = BIO_meth_new(BIO_TYPE_SIMPLE, "SimpleSocket")))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
BIO_meth_set_write(bio_methods, transport_bio_simple_write);
|
|
|
|
BIO_meth_set_read(bio_methods, transport_bio_simple_read);
|
|
|
|
BIO_meth_set_puts(bio_methods, transport_bio_simple_puts);
|
|
|
|
BIO_meth_set_gets(bio_methods, transport_bio_simple_gets);
|
|
|
|
BIO_meth_set_ctrl(bio_methods, transport_bio_simple_ctrl);
|
|
|
|
BIO_meth_set_create(bio_methods, transport_bio_simple_new);
|
|
|
|
BIO_meth_set_destroy(bio_methods, transport_bio_simple_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bio_methods;
|
2014-06-02 05:37:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Buffered Socket BIO */
|
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
typedef struct
|
2015-02-14 18:14:13 +03:00
|
|
|
{
|
|
|
|
BIO* bufferedBio;
|
|
|
|
BOOL readBlocked;
|
|
|
|
BOOL writeBlocked;
|
|
|
|
RingBuffer xmitBuffer;
|
2022-02-14 16:59:22 +03:00
|
|
|
} WINPR_BIO_BUFFERED_SOCKET;
|
2015-02-14 18:14:13 +03:00
|
|
|
|
2019-11-07 15:02:57 +03:00
|
|
|
static long transport_bio_buffered_callback(BIO* bio, int mode, const char* argp, int argi,
|
|
|
|
long argl, long ret)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_buffered_write(BIO* bio, const char* buf, int num)
|
|
|
|
{
|
2023-02-01 18:45:43 +03:00
|
|
|
int ret = num;
|
|
|
|
int nchunks = 0;
|
|
|
|
size_t committedBytes = 0;
|
|
|
|
DataChunk chunks[2] = { 0 };
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO* next_bio = NULL;
|
2023-02-01 18:45:43 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(bio);
|
|
|
|
WINPR_ASSERT(ptr);
|
|
|
|
|
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.
|
|
|
|
*/
|
2019-11-06 17:24:51 +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;
|
|
|
|
}
|
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
nchunks = ringbuffer_peek(&ptr->xmitBuffer, chunks, ringbuffer_used(&ptr->xmitBuffer));
|
2016-11-21 19:28:54 +03:00
|
|
|
next_bio = BIO_next(bio);
|
2014-05-30 22:53:10 +04:00
|
|
|
|
2023-02-01 18:45:43 +03:00
|
|
|
for (int i = 0; i < nchunks; i++)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
|
|
|
while (chunks[i].size)
|
|
|
|
{
|
2022-07-01 06:30:21 +03:00
|
|
|
ERR_clear_error();
|
2023-02-01 18:45:43 +03:00
|
|
|
const int status = BIO_write(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)
|
|
|
|
{
|
2016-11-21 19:28:54 +03:00
|
|
|
if (!BIO_should_retry(next_bio))
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
ret = -1; /* fatal error */
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
if (BIO_should_write(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 */
|
|
|
|
}
|
|
|
|
}
|
2023-02-01 18:45:43 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
committedBytes += (size_t)status;
|
|
|
|
chunks[i].size -= (size_t)status;
|
|
|
|
chunks[i].data += status;
|
|
|
|
}
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2015-02-14 18:14:13 +03:00
|
|
|
ringbuffer_commit_read_bytes(&ptr->xmitBuffer, committedBytes);
|
2014-05-21 19:32:14 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_buffered_read(BIO* bio, char* buf, int size)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int status = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO* next_bio = BIO_next(bio);
|
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);
|
2022-07-01 06:30:21 +03:00
|
|
|
ERR_clear_error();
|
2016-11-21 19:28:54 +03:00
|
|
|
status = BIO_read(next_bio, buf, size);
|
2014-05-21 19:32:14 +04:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
if (status <= 0)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2016-11-21 19:28:54 +03:00
|
|
|
if (!BIO_should_retry(next_bio))
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
|
2016-11-21 19:28:54 +03:00
|
|
|
if (BIO_should_read(next_bio))
|
2014-06-02 05:37:20 +04:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2022-04-25 10:47:14 +03:00
|
|
|
long status = -1;
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
|
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;
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2015-02-13 22:26:02 +03:00
|
|
|
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:
|
2019-11-06 17:24:51 +03:00
|
|
|
status = (int)ptr->readBlocked;
|
2015-02-13 22:26:02 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_C_WRITE_BLOCKED:
|
2019-11-06 17:24:51 +03:00
|
|
|
status = (int)ptr->writeBlocked;
|
2015-02-13 22:26:02 +03:00
|
|
|
break;
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
default:
|
2016-11-21 19:28:54 +03:00
|
|
|
status = BIO_ctrl(BIO_next(bio), cmd, arg1, arg2);
|
2015-02-13 22:26:02 +03:00
|
|
|
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)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
WINPR_BIO_BUFFERED_SOCKET* ptr = NULL;
|
2016-11-21 19:28:54 +03:00
|
|
|
BIO_set_init(bio, 1);
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
2019-11-06 17:24:51 +03:00
|
|
|
ptr = (WINPR_BIO_BUFFERED_SOCKET*)calloc(1, sizeof(WINPR_BIO_BUFFERED_SOCKET));
|
2015-02-14 18:14:13 +03:00
|
|
|
|
|
|
|
if (!ptr)
|
|
|
|
return -1;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
BIO_set_data(bio, (void*)ptr);
|
2015-02-14 18:14:13 +03:00
|
|
|
|
|
|
|
if (!ringbuffer_init(&ptr->xmitBuffer, 0x10000))
|
|
|
|
return -1;
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-11-08 14:09:49 +03:00
|
|
|
/* Free the buffered BIO.
|
|
|
|
* Do not free other elements in the BIO stack,
|
|
|
|
* let BIO_free_all handle that. */
|
2014-05-21 19:32:14 +04:00
|
|
|
static int transport_bio_buffered_free(BIO* bio)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_BIO_BUFFERED_SOCKET* ptr = (WINPR_BIO_BUFFERED_SOCKET*)BIO_get_data(bio);
|
2015-02-14 18:14:13 +03:00
|
|
|
|
2018-11-08 14:09:49 +03:00
|
|
|
if (!ptr)
|
|
|
|
return 0;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIO_METHOD* BIO_s_buffered_socket(void)
|
|
|
|
{
|
2016-11-21 19:28:54 +03:00
|
|
|
static BIO_METHOD* bio_methods = NULL;
|
|
|
|
|
|
|
|
if (bio_methods == NULL)
|
|
|
|
{
|
|
|
|
if (!(bio_methods = BIO_meth_new(BIO_TYPE_BUFFERED, "BufferedSocket")))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
BIO_meth_set_write(bio_methods, transport_bio_buffered_write);
|
|
|
|
BIO_meth_set_read(bio_methods, transport_bio_buffered_read);
|
|
|
|
BIO_meth_set_puts(bio_methods, transport_bio_buffered_puts);
|
|
|
|
BIO_meth_set_gets(bio_methods, transport_bio_buffered_gets);
|
|
|
|
BIO_meth_set_ctrl(bio_methods, transport_bio_buffered_ctrl);
|
|
|
|
BIO_meth_set_create(bio_methods, transport_bio_buffered_new);
|
|
|
|
BIO_meth_set_destroy(bio_methods, transport_bio_buffered_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bio_methods;
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2018-09-27 17:08:28 +03:00
|
|
|
char* freerdp_tcp_address_to_string(const struct sockaddr_storage* addr, BOOL* pIPv6)
|
2011-07-10 20:10:24 +04:00
|
|
|
{
|
2018-02-08 18:31:26 +03:00
|
|
|
char ipAddress[INET6_ADDRSTRLEN + 1] = { 0 };
|
2021-08-02 13:13:34 +03:00
|
|
|
const struct sockaddr_in6* sockaddr_ipv6 = (const struct sockaddr_in6*)addr;
|
|
|
|
const struct sockaddr_in* sockaddr_ipv4 = (const struct sockaddr_in*)addr;
|
2011-07-10 20:10:24 +04:00
|
|
|
|
2018-03-15 08:37:30 +03:00
|
|
|
if (addr == NULL)
|
|
|
|
{
|
2017-11-27 12:54:49 +03:00
|
|
|
return NULL;
|
2018-03-15 08:37:30 +03:00
|
|
|
}
|
2017-11-27 12:54:49 +03:00
|
|
|
|
2018-02-08 18:31:26 +03:00
|
|
|
switch (sockaddr_ipv4->sin_family)
|
2011-07-27 03:14:11 +04:00
|
|
|
{
|
2017-11-27 12:54:49 +03:00
|
|
|
case AF_INET:
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!inet_ntop(sockaddr_ipv4->sin_family, &sockaddr_ipv4->sin_addr, ipAddress,
|
|
|
|
sizeof(ipAddress)))
|
2018-02-08 18:31:26 +03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2017-11-27 12:54:49 +03:00
|
|
|
case AF_INET6:
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!inet_ntop(sockaddr_ipv6->sin6_family, &sockaddr_ipv6->sin6_addr, ipAddress,
|
|
|
|
sizeof(ipAddress)))
|
2017-11-27 12:54:49 +03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_UNIX:
|
2024-08-26 16:39:33 +03:00
|
|
|
(void)sprintf_s(ipAddress, ARRAYSIZE(ipAddress), "127.0.0.1");
|
2017-11-27 12:54:49 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return NULL;
|
2011-07-27 03:14:11 +04:00
|
|
|
}
|
2011-07-10 20:10:24 +04:00
|
|
|
|
2018-03-15 08:37:30 +03:00
|
|
|
if (pIPv6 != NULL)
|
|
|
|
{
|
2018-02-08 18:31:26 +03:00
|
|
|
*pIPv6 = (sockaddr_ipv4->sin_family == AF_INET6);
|
2018-03-15 08:37:30 +03:00
|
|
|
}
|
2017-11-27 12:54:49 +03:00
|
|
|
|
2015-02-13 22:26:02 +03:00
|
|
|
return _strdup(ipAddress);
|
2011-07-10 20:10:24 +04:00
|
|
|
}
|
|
|
|
|
2018-03-15 08:37:30 +03:00
|
|
|
static char* freerdp_tcp_get_ip_address(int sockfd, BOOL* pIPv6)
|
|
|
|
{
|
|
|
|
struct sockaddr_storage saddr = { 0 };
|
|
|
|
socklen_t length = sizeof(struct sockaddr_storage);
|
|
|
|
|
|
|
|
if (getsockname(sockfd, (struct sockaddr*)&saddr, &length) != 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return freerdp_tcp_address_to_string(&saddr, pIPv6);
|
|
|
|
}
|
|
|
|
|
2018-05-03 13:24:16 +03:00
|
|
|
char* freerdp_tcp_get_peer_address(SOCKET sockfd)
|
2018-03-15 08:37:30 +03:00
|
|
|
{
|
|
|
|
struct sockaddr_storage saddr = { 0 };
|
|
|
|
socklen_t length = sizeof(struct sockaddr_storage);
|
|
|
|
|
|
|
|
if (getpeername(sockfd, (struct sockaddr*)&saddr, &length) != 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return freerdp_tcp_address_to_string(&saddr, NULL);
|
|
|
|
}
|
|
|
|
|
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
|
2024-01-23 18:49:54 +03:00
|
|
|
int status = 0;
|
|
|
|
int sockfd = 0;
|
2018-08-17 17:25:20 +03:00
|
|
|
struct sockaddr_un addr = { 0 };
|
2014-11-12 22:06:34 +03:00
|
|
|
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
if (sockfd == -1)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "socket");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
2018-08-17 17:25:20 +03:00
|
|
|
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
|
2019-11-06 17:24:51 +03:00
|
|
|
status = connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));
|
2014-11-12 22:06:34 +03:00
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "connect");
|
|
|
|
close(sockfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sockfd;
|
|
|
|
#else /* ifndef _WIN32 */
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-09-27 17:08:28 +03:00
|
|
|
struct addrinfo* freerdp_tcp_resolve_host(const char* hostname, int port, int ai_flags)
|
2014-12-26 21:49:25 +03:00
|
|
|
{
|
2018-09-27 17:08:28 +03:00
|
|
|
char* service = NULL;
|
|
|
|
char port_str[16];
|
2024-01-23 18:49:54 +03:00
|
|
|
int status = 0;
|
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;
|
2018-09-27 17:08:28 +03:00
|
|
|
hints.ai_flags = ai_flags;
|
|
|
|
|
|
|
|
if (port >= 0)
|
|
|
|
{
|
2024-08-26 16:39:33 +03:00
|
|
|
(void)sprintf_s(port_str, sizeof(port_str) - 1, "%d", port);
|
2018-09-27 17:08:28 +03:00
|
|
|
service = port_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = getaddrinfo(hostname, service, &hints, &result);
|
2014-12-26 21:49:25 +03:00
|
|
|
|
|
|
|
if (status)
|
2018-09-27 17:08:28 +03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL freerdp_tcp_is_hostname_resolvable(rdpContext* context, const char* hostname)
|
|
|
|
{
|
|
|
|
struct addrinfo* result = freerdp_tcp_resolve_host(hostname, -1, 0);
|
|
|
|
|
|
|
|
if (!result)
|
2016-12-19 15:49:40 +03:00
|
|
|
{
|
2020-01-09 12:34:27 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_DNS_NAME_NOT_FOUND);
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2014-12-26 21:49:25 +03:00
|
|
|
return FALSE;
|
2016-12-19 15:49:40 +03:00
|
|
|
}
|
2014-12-26 21:49:25 +03:00
|
|
|
|
2020-01-08 19:39:25 +03:00
|
|
|
freerdp_set_last_error_log(context, 0);
|
2014-12-26 21:49:25 +03:00
|
|
|
freeaddrinfo(result);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL freerdp_tcp_connect_timeout(rdpContext* context, int sockfd, struct sockaddr* addr,
|
2021-06-02 17:51:49 +03:00
|
|
|
socklen_t addrlen, UINT32 timeout)
|
2014-12-28 00:48:32 +03:00
|
|
|
{
|
2017-11-15 11:11:12 +03:00
|
|
|
BOOL rc = FALSE;
|
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;
|
2021-06-02 17:51:49 +03:00
|
|
|
DWORD tout = (timeout > 0) ? timeout : INFINITE;
|
2019-02-07 16:22:28 +03:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
handles[count] = CreateEvent(NULL, TRUE, FALSE, NULL);
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
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);
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
if (status < 0)
|
|
|
|
{
|
2016-11-25 14:40:11 +03:00
|
|
|
WLog_ERR(TAG, "WSAEventSelect failed with %d", WSAGetLastError());
|
2017-11-15 11:11:12 +03:00
|
|
|
goto fail;
|
2015-07-14 12:58:01 +03:00
|
|
|
}
|
2014-12-28 00:48:32 +03:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
handles[count++] = utils_get_abort_event(context->rdp);
|
2015-07-03 12:49:40 +03:00
|
|
|
status = _connect(sockfd, addr, addrlen);
|
2016-12-19 15:49:40 +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
|
|
|
status = WSAGetLastError();
|
2016-12-19 15:49:40 +03:00
|
|
|
|
|
|
|
switch (status)
|
2014-12-28 02:53:27 +03:00
|
|
|
{
|
2015-07-03 12:49:40 +03:00
|
|
|
case WSAEINPROGRESS:
|
|
|
|
case WSAEWOULDBLOCK:
|
|
|
|
break;
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
default:
|
2017-11-15 11:11:12 +03:00
|
|
|
goto fail;
|
2014-12-28 02:53:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
status = WaitForMultipleObjects(count, handles, FALSE, tout);
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
if (WAIT_OBJECT_0 != status)
|
2017-11-15 11:11:12 +03:00
|
|
|
goto fail;
|
2014-12-28 02:53:27 +03:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
status = recv(sockfd, NULL, 0, 0);
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
if (status == SOCKET_ERROR)
|
2014-12-28 02:53:27 +03:00
|
|
|
{
|
2015-07-03 12:49:40 +03:00
|
|
|
if (WSAGetLastError() == WSAECONNRESET)
|
2017-11-15 11:11:12 +03:00
|
|
|
goto fail;
|
2014-12-28 02:53:27 +03:00
|
|
|
}
|
|
|
|
|
2015-07-03 12:49:40 +03:00
|
|
|
status = WSAEventSelect(sockfd, handles[0], 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
|
|
|
{
|
2016-11-25 14:40:11 +03:00
|
|
|
WLog_ERR(TAG, "WSAEventSelect failed with %d", WSAGetLastError());
|
2017-11-15 11:11:12 +03:00
|
|
|
goto fail;
|
2014-12-28 02:53:27 +03:00
|
|
|
}
|
|
|
|
|
2015-07-14 12:58:01 +03:00
|
|
|
if (_ioctlsocket(sockfd, FIONBIO, &arg) != 0)
|
2017-11-15 11:11:12 +03:00
|
|
|
goto fail;
|
2015-07-14 12:58:01 +03:00
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
rc = TRUE;
|
|
|
|
fail:
|
|
|
|
CloseHandle(handles[0]);
|
|
|
|
return rc;
|
2014-12-28 02:53:27 +03:00
|
|
|
}
|
|
|
|
|
2018-11-12 14:37:33 +03:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
SOCKET s;
|
|
|
|
struct addrinfo* addr;
|
|
|
|
struct addrinfo* result;
|
|
|
|
} t_peer;
|
|
|
|
|
2018-11-22 21:08:25 +03:00
|
|
|
static void peer_free(t_peer* peer)
|
2018-11-12 14:37:33 +03:00
|
|
|
{
|
|
|
|
if (peer->s != INVALID_SOCKET)
|
|
|
|
closesocket(peer->s);
|
|
|
|
|
|
|
|
freeaddrinfo(peer->addr);
|
|
|
|
memset(peer, 0, sizeof(t_peer));
|
|
|
|
peer->s = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static int freerdp_tcp_connect_multi(rdpContext* context, char** hostnames, UINT32* ports,
|
2021-06-02 17:51:49 +03:00
|
|
|
UINT32 count, UINT16 port, UINT32 timeout)
|
2015-05-07 21:20:49 +03:00
|
|
|
{
|
2018-11-12 14:37:33 +03:00
|
|
|
UINT32 sindex = count;
|
|
|
|
int status = -1;
|
|
|
|
SOCKET sockfd = INVALID_SOCKET;
|
2024-01-23 18:49:54 +03:00
|
|
|
HANDLE* events = NULL;
|
|
|
|
struct addrinfo* addr = NULL;
|
|
|
|
struct addrinfo* result = NULL;
|
|
|
|
t_peer* peers = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
events = (HANDLE*)calloc(count + 1, sizeof(HANDLE));
|
2018-11-12 14:37:33 +03:00
|
|
|
peers = (t_peer*)calloc(count, sizeof(t_peer));
|
2015-05-07 21:20:49 +03:00
|
|
|
|
2018-11-12 14:37:33 +03:00
|
|
|
if (!peers || !events || (count < 1))
|
2015-05-22 21:14:57 +03:00
|
|
|
{
|
2018-11-12 14:37:33 +03:00
|
|
|
free(peers);
|
2015-05-22 21:14:57 +03:00
|
|
|
free(events);
|
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
|
|
|
|
2024-01-30 12:25:38 +03:00
|
|
|
for (UINT32 index = 0; index < count; index++)
|
2015-05-07 21:20:49 +03:00
|
|
|
{
|
2018-11-09 17:21:13 +03:00
|
|
|
int curPort = port;
|
2015-05-07 21:20:49 +03:00
|
|
|
|
|
|
|
if (ports)
|
2018-11-09 17:21:13 +03:00
|
|
|
curPort = ports[index];
|
2015-05-07 21:20:49 +03:00
|
|
|
|
2018-11-09 17:21:13 +03:00
|
|
|
result = freerdp_tcp_resolve_host(hostnames[index], curPort, 0);
|
2015-05-07 21:20:49 +03:00
|
|
|
|
2018-09-27 17:08:28 +03:00
|
|
|
if (!result)
|
2015-05-07 21:20:49 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:37:33 +03:00
|
|
|
peers[index].s = _socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
2015-05-07 21:20:49 +03:00
|
|
|
|
2018-11-12 14:37:33 +03:00
|
|
|
if (peers[index].s == INVALID_SOCKET)
|
2015-05-07 21:20:49 +03:00
|
|
|
{
|
|
|
|
freeaddrinfo(result);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:37:33 +03:00
|
|
|
peers[index].addr = addr;
|
|
|
|
peers[index].result = result;
|
2015-05-07 21:20:49 +03:00
|
|
|
}
|
|
|
|
|
2024-01-30 12:25:38 +03:00
|
|
|
for (UINT32 index = 0; index < count; index++)
|
2015-05-07 21:20:49 +03:00
|
|
|
{
|
2018-11-12 14:37:33 +03:00
|
|
|
sockfd = peers[index].s;
|
|
|
|
addr = peers[index].addr;
|
2018-11-23 12:32:52 +03:00
|
|
|
|
|
|
|
if ((sockfd == INVALID_SOCKET) || (!addr))
|
|
|
|
continue;
|
|
|
|
|
2018-11-12 14:37:33 +03:00
|
|
|
/* blocking tcp connect */
|
2015-05-07 21:20:49 +03:00
|
|
|
status = _connect(sockfd, addr->ai_addr, addr->ai_addrlen);
|
|
|
|
|
|
|
|
if (status >= 0)
|
|
|
|
{
|
|
|
|
/* connection success */
|
2018-11-12 14:37:33 +03:00
|
|
|
sindex = index;
|
2015-05-07 21:20:49 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:37:33 +03:00
|
|
|
if (sindex < count)
|
2015-05-07 21:20:49 +03:00
|
|
|
{
|
2018-11-12 14:37:33 +03:00
|
|
|
sockfd = peers[sindex].s;
|
|
|
|
peers[sindex].s = INVALID_SOCKET;
|
2015-05-07 21:20:49 +03:00
|
|
|
}
|
2018-11-12 14:37:33 +03:00
|
|
|
else
|
2020-01-08 19:39:25 +03:00
|
|
|
freerdp_set_last_error_log(context, FREERDP_ERROR_CONNECT_CANCELLED);
|
2015-07-03 12:49:40 +03:00
|
|
|
|
2024-01-30 12:25:38 +03:00
|
|
|
for (UINT32 index = 0; index < count; index++)
|
2018-11-12 14:37:33 +03:00
|
|
|
peer_free(&peers[index]);
|
2015-05-07 21:20:49 +03:00
|
|
|
|
2018-11-12 14:37:33 +03:00
|
|
|
free(peers);
|
2015-05-07 21:20:49 +03:00
|
|
|
free(events);
|
|
|
|
return sockfd;
|
|
|
|
}
|
|
|
|
|
2022-05-06 01:05:16 +03:00
|
|
|
BOOL freerdp_tcp_set_keep_alive_mode(const rdpSettings* settings, int sockfd)
|
2015-02-13 22:26:02 +03:00
|
|
|
{
|
2019-11-07 15:02:57 +03:00
|
|
|
const BOOL keepalive = (freerdp_settings_get_bool(settings, FreeRDP_TcpKeepAlive));
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT32 optval = 0;
|
|
|
|
socklen_t optlen = 0;
|
2019-11-07 15:02:57 +03:00
|
|
|
optval = keepalive ? 1 : 0;
|
2015-02-13 22:26:02 +03:00
|
|
|
optlen = sizeof(optval);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (void*)&optval, optlen) < 0)
|
2015-02-13 22:26:02 +03:00
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "setsockopt() SOL_SOCKET, SO_KEEPALIVE");
|
|
|
|
}
|
|
|
|
|
2019-11-07 15:02:57 +03:00
|
|
|
#ifndef _WIN32
|
2015-02-13 22:26:02 +03:00
|
|
|
#ifdef TCP_KEEPIDLE
|
2019-11-07 15:15:44 +03:00
|
|
|
optval = keepalive ? freerdp_settings_get_uint32(settings, FreeRDP_TcpKeepAliveDelay) : 0;
|
2015-02-13 22:26:02 +03:00
|
|
|
optlen = sizeof(optval);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (void*)&optval, optlen) < 0)
|
2015-02-13 22:26:02 +03:00
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "setsockopt() IPPROTO_TCP, TCP_KEEPIDLE");
|
|
|
|
}
|
|
|
|
|
2017-05-17 19:30:13 +03:00
|
|
|
#endif
|
|
|
|
#ifndef SOL_TCP
|
2017-11-27 12:54:49 +03:00
|
|
|
/* "tcp" from /etc/protocols as getprotobyname(3C) */
|
2017-05-17 19:30:13 +03:00
|
|
|
#define SOL_TCP 6
|
2016-12-19 15:49:40 +03:00
|
|
|
#endif
|
2015-02-13 22:26:02 +03:00
|
|
|
#ifdef TCP_KEEPCNT
|
2019-11-07 15:02:57 +03:00
|
|
|
optval = keepalive ? freerdp_settings_get_uint32(settings, FreeRDP_TcpKeepAliveRetries) : 0;
|
2015-02-13 22:26:02 +03:00
|
|
|
optlen = sizeof(optval);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, (void*)&optval, optlen) < 0)
|
2015-02-13 22:26:02 +03:00
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "setsockopt() SOL_TCP, TCP_KEEPCNT");
|
|
|
|
}
|
|
|
|
|
2016-12-19 15:49:40 +03:00
|
|
|
#endif
|
2015-02-13 22:26:02 +03:00
|
|
|
#ifdef TCP_KEEPINTVL
|
2019-11-07 15:02:57 +03:00
|
|
|
optval = keepalive ? freerdp_settings_get_uint32(settings, FreeRDP_TcpKeepAliveInterval) : 0;
|
2015-02-13 22:26:02 +03:00
|
|
|
optlen = sizeof(optval);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, (void*)&optval, optlen) < 0)
|
2015-02-13 22:26:02 +03:00
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "setsockopt() SOL_TCP, TCP_KEEPINTVL");
|
|
|
|
}
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2015-02-13 22:26:02 +03:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if defined(__MACOSX__) || defined(__IOS__)
|
|
|
|
optval = 1;
|
|
|
|
optlen = sizeof(optval);
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&optval, optlen) < 0)
|
2015-02-13 22:26:02 +03:00
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "setsockopt() SOL_SOCKET, SO_NOSIGPIPE");
|
|
|
|
}
|
2015-02-20 15:22:01 +03:00
|
|
|
|
2016-12-19 15:49:40 +03:00
|
|
|
#endif
|
2015-02-20 15:22:01 +03:00
|
|
|
#ifdef TCP_USER_TIMEOUT
|
2019-11-07 15:02:57 +03:00
|
|
|
optval = freerdp_settings_get_uint32(settings, FreeRDP_TcpAckTimeout);
|
2015-02-20 15:22:01 +03:00
|
|
|
optlen = sizeof(optval);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (setsockopt(sockfd, SOL_TCP, TCP_USER_TIMEOUT, (void*)&optval, optlen) < 0)
|
2015-02-20 15:22:01 +03:00
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "setsockopt() SOL_TCP, TCP_USER_TIMEOUT");
|
|
|
|
}
|
|
|
|
|
2016-12-19 15:49:40 +03:00
|
|
|
#endif
|
2015-02-13 22:26:02 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-09-06 12:01:36 +03:00
|
|
|
int freerdp_tcp_connect(rdpContext* context, const char* hostname, int port, DWORD timeout)
|
2020-09-02 14:37:04 +03:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
rdpTransport* transport = NULL;
|
2020-09-02 14:37:04 +03:00
|
|
|
if (!context || !context->rdp)
|
|
|
|
return -1;
|
|
|
|
transport = context->rdp->transport;
|
|
|
|
if (!transport)
|
|
|
|
return -1;
|
2021-09-06 12:01:36 +03:00
|
|
|
return transport_tcp_connect(context->rdp->transport, hostname, port, timeout);
|
2020-09-02 14:37:04 +03:00
|
|
|
}
|
|
|
|
|
2024-08-20 14:05:26 +03:00
|
|
|
static int get_next_addrinfo(rdpContext* context, struct addrinfo* input, struct addrinfo** result,
|
|
|
|
UINT32 errorCode)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
WINPR_ASSERT(result);
|
|
|
|
|
|
|
|
struct addrinfo* addr = input;
|
|
|
|
if (!addr)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (freerdp_settings_get_bool(context->settings, FreeRDP_PreferIPv6OverIPv4))
|
|
|
|
{
|
|
|
|
while (addr && (addr->ai_family != AF_INET6))
|
|
|
|
addr = addr->ai_next;
|
|
|
|
if (!addr)
|
|
|
|
addr = input;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We want to force IPvX, abort if not detected */
|
|
|
|
const UINT32 IPvX = freerdp_settings_get_uint32(context->settings, FreeRDP_ForceIPvX);
|
|
|
|
switch (IPvX)
|
|
|
|
{
|
|
|
|
case 4:
|
|
|
|
case 6:
|
|
|
|
{
|
|
|
|
const int family = (IPvX == 4) ? AF_INET : AF_INET6;
|
|
|
|
while (addr && (addr->ai_family != family))
|
|
|
|
addr = addr->ai_next;
|
|
|
|
if (!addr)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!addr)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
*result = addr;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
freerdp_set_last_error_if_not(context, errorCode);
|
|
|
|
freeaddrinfo(input);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-09-02 14:37:04 +03:00
|
|
|
int freerdp_tcp_default_connect(rdpContext* context, rdpSettings* settings, const char* hostname,
|
|
|
|
int port, DWORD timeout)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int sockfd = 0;
|
|
|
|
UINT32 optval = 0;
|
|
|
|
socklen_t optlen = 0;
|
2015-02-14 00:51:08 +03:00
|
|
|
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)
|
2018-04-11 10:00:32 +03:00
|
|
|
{
|
2020-01-09 12:34:27 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
|
2018-04-11 10:00:32 +03:00
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
return -1;
|
2018-04-11 10:00:32 +03:00
|
|
|
}
|
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;
|
|
|
|
|
2023-11-14 18:33:53 +03:00
|
|
|
const char* vsock = utils_is_vsock(hostname);
|
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)
|
2018-04-11 10:00:32 +03:00
|
|
|
{
|
2020-01-09 12:34:27 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
|
2018-04-11 10:00:32 +03:00
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
return -1;
|
2018-04-11 10:00:32 +03:00
|
|
|
}
|
2012-05-22 15:57:19 +04:00
|
|
|
}
|
2016-12-19 15:49:40 +03:00
|
|
|
else if (useExternalDefinedSocket)
|
|
|
|
sockfd = port;
|
2023-11-14 18:33:53 +03:00
|
|
|
else if (vsock)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_AF_VSOCK_H)
|
|
|
|
hostname = vsock;
|
|
|
|
sockfd = socket(AF_VSOCK, SOCK_STREAM, 0);
|
|
|
|
struct sockaddr_vm addr = { 0 };
|
|
|
|
|
|
|
|
addr.svm_family = AF_VSOCK;
|
|
|
|
addr.svm_port = port;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
char* ptr = NULL;
|
|
|
|
unsigned long val = strtoul(hostname, &ptr, 10);
|
|
|
|
if (errno || (val > UINT32_MAX))
|
|
|
|
{
|
2024-02-05 15:01:08 +03:00
|
|
|
char ebuffer[256] = { 0 };
|
2023-11-14 18:33:53 +03:00
|
|
|
WLog_ERR(TAG, "could not extract port from '%s', value=%ul, error=%s", hostname, val,
|
2024-02-05 15:01:08 +03:00
|
|
|
winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
|
2023-11-14 18:33:53 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
addr.svm_cid = val;
|
|
|
|
if (addr.svm_cid == 2)
|
|
|
|
{
|
|
|
|
addr.svm_flags = VMADDR_FLAG_TO_HOST;
|
|
|
|
}
|
|
|
|
if ((connect(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_vm))) == -1)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to connect to %s", hostname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
WLog_ERR(TAG, "Compiled without AF_VSOCK, '%s' not supported", hostname);
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
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)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!freerdp_tcp_is_hostname_resolvable(context, 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(
|
2019-11-06 17:24:51 +03:00
|
|
|
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
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
char* peerAddress = NULL;
|
|
|
|
struct addrinfo* addr = NULL;
|
|
|
|
struct addrinfo* result = NULL;
|
2019-09-11 11:43:17 +03:00
|
|
|
|
2018-09-27 17:08:28 +03:00
|
|
|
result = freerdp_tcp_resolve_host(hostname, port, 0);
|
2014-10-17 14:08:39 +04:00
|
|
|
|
2018-09-27 17:08:28 +03:00
|
|
|
if (!result)
|
2015-02-14 18:14:13 +03:00
|
|
|
{
|
2020-01-09 12:34:27 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_DNS_NAME_NOT_FOUND);
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
return -1;
|
2014-12-28 02:53:27 +03:00
|
|
|
}
|
2020-01-08 19:39:25 +03:00
|
|
|
freerdp_set_last_error_log(context, 0);
|
2014-12-28 02:53:27 +03:00
|
|
|
|
2024-06-28 09:42:16 +03:00
|
|
|
/* By default we take the first returned entry.
|
|
|
|
*
|
|
|
|
* If PreferIPv6OverIPv4 = TRUE we force to IPv6 if there
|
|
|
|
* is such an address available, but fall back to first if not found
|
|
|
|
*/
|
2024-08-20 14:05:26 +03:00
|
|
|
const int rc =
|
|
|
|
get_next_addrinfo(context, result, &addr, FREERDP_ERROR_DNS_NAME_NOT_FOUND);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
2015-02-14 00:51:08 +03:00
|
|
|
|
2024-08-20 14:05:26 +03:00
|
|
|
do
|
2014-10-17 14:08:39 +04:00
|
|
|
{
|
2024-08-20 14:05:26 +03:00
|
|
|
sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
|
|
|
if (sockfd < 0)
|
2014-12-28 02:53:27 +03:00
|
|
|
{
|
2024-08-20 14:05:26 +03:00
|
|
|
const int rc = get_next_addrinfo(context, addr->ai_next, &addr,
|
|
|
|
FREERDP_ERROR_CONNECT_FAILED);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
2014-12-28 02:53:27 +03:00
|
|
|
}
|
2024-08-20 14:05:26 +03:00
|
|
|
} while (sockfd < 0);
|
2014-12-28 02:53:27 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if ((peerAddress = freerdp_tcp_address_to_string(
|
|
|
|
(const struct sockaddr_storage*)addr->ai_addr, NULL)) != NULL)
|
2018-03-15 08:37:30 +03:00
|
|
|
{
|
|
|
|
WLog_DBG(TAG, "connecting to peer %s", peerAddress);
|
|
|
|
free(peerAddress);
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +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);
|
2018-04-11 10:00:32 +03:00
|
|
|
|
2020-01-09 12:34:27 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
|
2018-04-11 10:00:32 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-11-14 18:33:53 +03:00
|
|
|
if (!vsock)
|
2015-06-17 23:08:02 +03:00
|
|
|
{
|
2023-11-14 18:33:53 +03:00
|
|
|
free(settings->ClientAddress);
|
|
|
|
settings->ClientAddress = freerdp_tcp_get_ip_address(sockfd, &settings->IPv6Enabled);
|
2016-12-19 15:49:40 +03:00
|
|
|
|
2023-11-14 18:33:53 +03:00
|
|
|
if (!settings->ClientAddress)
|
|
|
|
{
|
|
|
|
if (!useExternalDefinedSocket)
|
|
|
|
close(sockfd);
|
2018-04-11 10:00:32 +03:00
|
|
|
|
2023-11-14 18:33:53 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
|
|
|
|
|
|
|
|
WLog_ERR(TAG, "Couldn't get socket ip address");
|
|
|
|
return -1;
|
|
|
|
}
|
2015-06-17 23:08:02 +03:00
|
|
|
}
|
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
|
|
|
{
|
2019-11-06 17:24:51 +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 */
|
2019-11-06 17:24:51 +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
|
|
|
|
2019-11-06 17:24:51 +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);
|
2018-04-11 10:00:32 +03:00
|
|
|
|
2020-01-09 12:34:27 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
|
2018-04-11 10:00:32 +03:00
|
|
|
|
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
|
|
|
{
|
2019-11-07 15:02:57 +03:00
|
|
|
if (!freerdp_tcp_set_keep_alive_mode(settings, sockfd))
|
2015-06-17 23:08:02 +03:00
|
|
|
{
|
|
|
|
close(sockfd);
|
2018-04-11 10:00:32 +03:00
|
|
|
|
2020-01-09 12:34:27 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_FAILED);
|
2018-04-11 10:00:32 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (WaitForSingleObject(utils_get_abort_event(context->rdp), 0) == WAIT_OBJECT_0)
|
2015-07-03 12:49:40 +03:00
|
|
|
{
|
|
|
|
close(sockfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
return sockfd;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|