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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#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
|
|
|
|
|
2014-05-01 17:09:35 +04:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#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 */
|
|
|
|
|
|
|
|
static int transport_bio_simple_new(BIO* bio);
|
|
|
|
static int transport_bio_simple_free(BIO* bio);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_WRITE);
|
|
|
|
|
|
|
|
status = _send((SOCKET) bio->num, buf, size, 0);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_READ);
|
|
|
|
|
|
|
|
status = _recv((SOCKET) bio->num, buf, size, 0);
|
2014-07-11 01:35:11 +04:00
|
|
|
if (status > 0)
|
|
|
|
return status;
|
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 05:18:26 +03:00
|
|
|
if (cmd == BIO_C_GET_EVENT)
|
|
|
|
{
|
|
|
|
if (!bio->init || !arg2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
if (!bio->ptr)
|
|
|
|
bio->ptr = CreateFileDescriptorEvent(NULL, FALSE, FALSE, bio->num);
|
|
|
|
#else
|
|
|
|
if (!bio->ptr)
|
|
|
|
bio->ptr = (void*) WSACreateEvent();
|
|
|
|
|
|
|
|
WSAEventSelect(bio->num, (HANDLE) bio->ptr, FD_READ);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
*((ULONG_PTR*) arg2) = (ULONG_PTR) bio->ptr;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (cmd == BIO_C_SET_NONBLOCK)
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
flags = fcntl(bio->num, F_GETFL);
|
|
|
|
|
|
|
|
if (flags == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (arg1)
|
|
|
|
fcntl(bio->num, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
else
|
|
|
|
fcntl(bio->num, F_SETFL, flags & ~(O_NONBLOCK));
|
|
|
|
#else
|
|
|
|
if (arg1)
|
|
|
|
{
|
|
|
|
if (!bio->ptr)
|
|
|
|
bio->ptr = (void*) WSACreateEvent();
|
|
|
|
|
|
|
|
WSAEventSelect(bio->num, (HANDLE) bio->ptr, FD_READ);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (bio->ptr)
|
|
|
|
WSAEventSelect(bio->num, (HANDLE) bio->ptr, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case BIO_C_SET_FD:
|
|
|
|
if (arg2)
|
|
|
|
{
|
|
|
|
transport_bio_simple_free(bio);
|
|
|
|
bio->flags = BIO_FLAGS_SHOULD_RETRY;
|
|
|
|
bio->num = *((int*) arg2);
|
|
|
|
bio->shutdown = (int) arg1;
|
|
|
|
bio->init = 1;
|
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_C_GET_FD:
|
|
|
|
if (bio->init)
|
|
|
|
{
|
|
|
|
if (arg2)
|
|
|
|
*((int*) arg2) = bio->num;
|
|
|
|
status = bio->num;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_simple_new(BIO* bio)
|
|
|
|
{
|
|
|
|
bio->init = 0;
|
|
|
|
bio->num = 0;
|
|
|
|
bio->ptr = NULL;
|
|
|
|
bio->flags = BIO_FLAGS_SHOULD_RETRY;
|
2015-01-28 05:18:26 +03:00
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_simple_free(BIO* bio)
|
|
|
|
{
|
|
|
|
if (!bio)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (bio->shutdown)
|
|
|
|
{
|
|
|
|
if (bio->init)
|
2015-01-28 05:18:26 +03:00
|
|
|
{
|
|
|
|
if (bio->ptr)
|
|
|
|
{
|
|
|
|
CloseHandle((HANDLE) bio->ptr);
|
|
|
|
bio->ptr = NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
closesocket((SOCKET) bio->num);
|
2015-01-28 05:18:26 +03:00
|
|
|
}
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
bio->init = 0;
|
|
|
|
bio->flags = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
int status, ret;
|
2014-05-30 22:53:10 +04:00
|
|
|
rdpTcp* tcp = (rdpTcp*) bio->ptr;
|
2014-05-21 19:32:14 +04:00
|
|
|
int nchunks, committedBytes, i;
|
|
|
|
DataChunk chunks[2];
|
|
|
|
|
|
|
|
ret = num;
|
|
|
|
tcp->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.
|
|
|
|
*/
|
2014-05-30 22:53:10 +04:00
|
|
|
if (buf && num && !ringbuffer_write(&tcp->xmitBuffer, (const BYTE*) buf, num))
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "an error occured when writing(toWrite=%d)", num);
|
2014-05-21 19:32:14 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
committedBytes = 0;
|
|
|
|
nchunks = ringbuffer_peek(&tcp->xmitBuffer, chunks, ringbuffer_used(&tcp->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);
|
2014-05-21 19:32:14 +04:00
|
|
|
tcp->writeBlocked = TRUE;
|
|
|
|
goto out; /* EWOULDBLOCK */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
committedBytes += status;
|
|
|
|
chunks[i].size -= status;
|
|
|
|
chunks[i].data += status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
ringbuffer_commit_read_bytes(&tcp->xmitBuffer, committedBytes);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_buffered_read(BIO* bio, char* buf, int size)
|
|
|
|
{
|
|
|
|
int status;
|
2014-05-30 22:53:10 +04:00
|
|
|
rdpTcp* tcp = (rdpTcp*) bio->ptr;
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
tcp->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);
|
|
|
|
tcp->readBlocked = TRUE;
|
|
|
|
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)
|
|
|
|
{
|
2014-06-02 05:37:20 +04:00
|
|
|
rdpTcp* tcp = (rdpTcp*) bio->ptr;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2014-06-02 05:37:20 +04:00
|
|
|
case BIO_CTRL_FLUSH:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case BIO_CTRL_WPENDING:
|
|
|
|
return ringbuffer_used(&tcp->xmitBuffer);
|
|
|
|
|
|
|
|
case BIO_CTRL_PENDING:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return BIO_ctrl(bio->next_bio, cmd, arg1, arg2);
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_buffered_new(BIO* bio)
|
|
|
|
{
|
|
|
|
bio->init = 1;
|
|
|
|
bio->num = 0;
|
|
|
|
bio->ptr = NULL;
|
2014-06-02 05:37:20 +04:00
|
|
|
bio->flags = BIO_FLAGS_SHOULD_RETRY;
|
2014-05-21 19:32:14 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_bio_buffered_free(BIO* bio)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL transport_bio_buffered_drain(BIO *bio)
|
|
|
|
{
|
|
|
|
int status;
|
2014-06-02 05:37:20 +04:00
|
|
|
rdpTcp* tcp = (rdpTcp*) bio->ptr;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
|
|
|
if (!ringbuffer_used(&tcp->xmitBuffer))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
status = transport_bio_buffered_write(bio, NULL, 0);
|
2014-06-02 05:37:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
return status >= 0;
|
|
|
|
}
|
|
|
|
|
2014-11-29 23:14:39 +03:00
|
|
|
void freerdp_tcp_get_ip_address(rdpTcp* tcp)
|
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;
|
|
|
|
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
|
|
|
|
|
|
|
if (getsockname(tcp->sockfd, (struct sockaddr*) &sockaddr, &length) == 0)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
ip = (BYTE*) (&sockaddr.sin_addr);
|
2012-12-14 22:11:07 +04:00
|
|
|
sprintf_s(tcp->ip_address, sizeof(tcp->ip_address),
|
2011-07-27 03:14:11 +04:00
|
|
|
"%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
|
|
|
{
|
2013-11-08 02:37:58 +04:00
|
|
|
strcpy(tcp->ip_address, "127.0.0.1");
|
2011-07-27 03:14:11 +04:00
|
|
|
}
|
2011-07-10 20:10:24 +04:00
|
|
|
|
2012-11-08 03:23:25 +04:00
|
|
|
tcp->settings->IPv6Enabled = 0;
|
2013-11-01 07:35:24 +04:00
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
free(tcp->settings->ClientAddress);
|
2012-11-08 03:23:25 +04:00
|
|
|
tcp->settings->ClientAddress = _strdup(tcp->ip_address);
|
2011-07-10 20:10:24 +04:00
|
|
|
}
|
|
|
|
|
2014-11-29 23:14:39 +03:00
|
|
|
void freerdp_tcp_get_mac_address(rdpTcp* tcp)
|
2011-07-12 10:53:26 +04:00
|
|
|
{
|
2011-07-25 02:09:53 +04:00
|
|
|
#ifdef LINUX
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* mac;
|
2011-07-12 10:53:26 +04:00
|
|
|
struct ifreq if_req;
|
|
|
|
struct if_nameindex* ni;
|
|
|
|
|
|
|
|
ni = if_nameindex();
|
|
|
|
mac = tcp->mac_address;
|
|
|
|
|
|
|
|
while (ni->if_name != NULL)
|
|
|
|
{
|
|
|
|
if (strcmp(ni->if_name, "lo") != 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ni++;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(if_req.ifr_name, ni->if_name, IF_NAMESIZE);
|
|
|
|
|
|
|
|
if (ioctl(tcp->sockfd, SIOCGIFHWADDR, &if_req) != 0)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "failed to obtain MAC address");
|
2011-07-12 10:53:26 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove((void*) mac, (void*) &if_req.ifr_ifru.ifru_hwaddr.sa_data[0], 6);
|
2011-07-25 02:09:53 +04:00
|
|
|
#endif
|
2014-09-12 16:36:29 +04:00
|
|
|
/* WLog_ERR(TAG, "MAC: %02X:%02X:%02X:%02X:%02X:%02X",
|
2011-07-12 10:53:26 +04:00
|
|
|
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); */
|
|
|
|
}
|
|
|
|
|
2014-11-12 22:06:34 +03:00
|
|
|
int uds_connect(const char* path)
|
|
|
|
{
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2014-12-28 00:48:32 +03:00
|
|
|
BOOL freerdp_tcp_connect_timeout(int sockfd, struct sockaddr* addr, socklen_t addrlen, int timeout)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
int flags;
|
|
|
|
fd_set cfds;
|
|
|
|
socklen_t optlen;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
/* set socket in non-blocking mode */
|
|
|
|
|
|
|
|
flags = fcntl(sockfd, F_GETFL);
|
|
|
|
|
|
|
|
if (flags < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
|
|
|
|
/* non-blocking tcp connect */
|
|
|
|
|
|
|
|
status = connect(sockfd, addr, addrlen);
|
|
|
|
|
|
|
|
if (status >= 0)
|
|
|
|
return TRUE; /* connection success */
|
|
|
|
|
|
|
|
if (errno != EINPROGRESS)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
FD_ZERO(&cfds);
|
|
|
|
FD_SET(sockfd, &cfds);
|
|
|
|
|
|
|
|
tv.tv_sec = timeout;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
status = _select(sockfd + 1, NULL, &cfds, NULL, &tv);
|
|
|
|
|
|
|
|
if (status != 1)
|
|
|
|
return FALSE; /* connection timeout or error */
|
|
|
|
|
|
|
|
status = 0;
|
|
|
|
optlen = sizeof(status);
|
|
|
|
|
|
|
|
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*) &status, &optlen) < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* set socket in blocking mode */
|
|
|
|
|
|
|
|
flags = fcntl(sockfd, F_GETFL);
|
|
|
|
|
|
|
|
if (flags < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
fcntl(sockfd, F_SETFL, flags & ~O_NONBLOCK);
|
|
|
|
|
|
|
|
#else
|
|
|
|
status = connect(sockfd, addr, addrlen);
|
|
|
|
|
|
|
|
if (status >= 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-12-28 02:53:27 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
int freerdp_tcp_connect_multi(char** hostnames, int count, int port, int timeout)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int sindex;
|
|
|
|
int status;
|
|
|
|
int flags;
|
|
|
|
int maxfds;
|
|
|
|
fd_set cfds;
|
|
|
|
int sockfd;
|
|
|
|
int* sockfds;
|
|
|
|
char port_str[16];
|
|
|
|
socklen_t optlen;
|
|
|
|
struct timeval tv;
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo* addr;
|
|
|
|
struct addrinfo* result;
|
|
|
|
struct addrinfo** addrs;
|
|
|
|
struct addrinfo** results;
|
|
|
|
|
|
|
|
sindex = -1;
|
|
|
|
|
|
|
|
sprintf_s(port_str, sizeof(port_str) - 1, "%u", port);
|
|
|
|
|
|
|
|
sockfds = (int*) calloc(count, sizeof(int));
|
|
|
|
addrs = (struct addrinfo**) calloc(count, sizeof(struct addrinfo*));
|
|
|
|
results = (struct addrinfo**) calloc(count, sizeof(struct addrinfo*));
|
|
|
|
|
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
{
|
|
|
|
ZeroMemory(&hints, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (sockfds[index] < 0)
|
|
|
|
{
|
|
|
|
freeaddrinfo(result);
|
|
|
|
sockfds[index] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs[index] = addr;
|
|
|
|
results[index] = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
maxfds = 0;
|
|
|
|
FD_ZERO(&cfds);
|
|
|
|
|
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
{
|
|
|
|
if (!sockfds[index])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sockfd = sockfds[index];
|
|
|
|
addr = addrs[index];
|
|
|
|
|
|
|
|
/* set socket in non-blocking mode */
|
|
|
|
|
|
|
|
flags = fcntl(sockfd, F_GETFL);
|
|
|
|
|
|
|
|
if (flags < 0)
|
|
|
|
{
|
|
|
|
sockfds[index] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
|
|
|
|
/* non-blocking tcp connect */
|
|
|
|
|
|
|
|
status = connect(sockfd, addr->ai_addr, addr->ai_addrlen);
|
|
|
|
|
|
|
|
if (status >= 0)
|
|
|
|
{
|
|
|
|
/* connection success */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno != EINPROGRESS)
|
|
|
|
{
|
|
|
|
sockfds[index] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
FD_SET(sockfd, &cfds);
|
|
|
|
|
|
|
|
if (sockfd > maxfds)
|
|
|
|
maxfds = sockfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
tv.tv_sec = timeout;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
status = _select(maxfds + 1, NULL, &cfds, NULL, &tv);
|
|
|
|
|
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
{
|
|
|
|
if (!sockfds[index])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sockfd = sockfds[index];
|
|
|
|
|
|
|
|
if (FD_ISSET(sockfd, &cfds))
|
|
|
|
{
|
|
|
|
status = 0;
|
|
|
|
optlen = sizeof(status);
|
|
|
|
|
|
|
|
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*) &status, &optlen) < 0)
|
|
|
|
{
|
|
|
|
sockfds[index] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
sockfds[index] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set socket in blocking mode */
|
|
|
|
|
|
|
|
flags = fcntl(sockfd, F_GETFL);
|
|
|
|
|
|
|
|
if (flags < 0)
|
|
|
|
{
|
|
|
|
sockfds[index] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fcntl(sockfd, F_SETFL, flags & ~O_NONBLOCK);
|
|
|
|
|
|
|
|
sindex = index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sindex >= 0)
|
|
|
|
{
|
|
|
|
sockfd = sockfds[sindex];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
{
|
|
|
|
if (results[index])
|
|
|
|
freeaddrinfo(results[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(addrs);
|
|
|
|
free(results);
|
|
|
|
free(sockfds);
|
|
|
|
|
|
|
|
return sockfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-11-29 23:14:39 +03:00
|
|
|
BOOL freerdp_tcp_connect(rdpTcp* tcp, 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;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 option_value;
|
2011-12-12 03:59:35 +04:00
|
|
|
socklen_t option_len;
|
2014-12-26 21:49:25 +03:00
|
|
|
rdpSettings* settings = tcp->settings;
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2013-10-11 13:07:33 +04:00
|
|
|
if (!hostname)
|
2013-05-14 12:24:43 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2012-05-22 15:57:19 +04:00
|
|
|
if (hostname[0] == '/')
|
2014-08-06 20:08:00 +04:00
|
|
|
tcp->ipcSocket = TRUE;
|
|
|
|
|
|
|
|
if (tcp->ipcSocket)
|
2012-05-22 15:57:19 +04:00
|
|
|
{
|
2014-11-12 22:06:34 +03:00
|
|
|
tcp->sockfd = uds_connect(hostname);
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2012-05-22 15:57:19 +04:00
|
|
|
if (tcp->sockfd < 0)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
2014-08-06 20:08:00 +04:00
|
|
|
tcp->socketBio = BIO_new(BIO_s_simple_socket());
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tcp->socketBio)
|
|
|
|
return FALSE;
|
2014-08-06 20:08:00 +04:00
|
|
|
|
|
|
|
BIO_set_fd(tcp->socketBio, tcp->sockfd, BIO_CLOSE);
|
2012-05-22 15:57:19 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-17 14:08:39 +04:00
|
|
|
#ifdef NO_IPV6
|
2014-05-21 19:32:14 +04:00
|
|
|
tcp->socketBio = BIO_new(BIO_s_connect());
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tcp->socketBio)
|
|
|
|
return FALSE;
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2014-05-30 22:03:20 +04:00
|
|
|
if (BIO_set_conn_hostname(tcp->socketBio, hostname) < 0 || BIO_set_conn_int_port(tcp->socketBio, &port) < 0)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2014-05-30 22:03:20 +04:00
|
|
|
BIO_set_nbio(tcp->socketBio, 1);
|
|
|
|
|
|
|
|
status = BIO_do_connect(tcp->socketBio);
|
|
|
|
|
|
|
|
if ((status <= 0) && !BIO_should_retry(tcp->socketBio))
|
2014-05-21 19:32:14 +04:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
tcp->sockfd = BIO_get_fd(tcp->socketBio, NULL);
|
2014-05-30 22:03:20 +04:00
|
|
|
|
|
|
|
if (tcp->sockfd < 0)
|
|
|
|
return FALSE;
|
2014-10-17 14:08:39 +04:00
|
|
|
#else /* NO_IPV6 */
|
2014-12-28 02:53:27 +03:00
|
|
|
tcp->sockfd = -1;
|
2014-10-17 14:08:39 +04:00
|
|
|
|
2014-12-26 21:49:25 +03:00
|
|
|
if (!settings->GatewayEnabled)
|
|
|
|
{
|
|
|
|
if (!freerdp_tcp_resolve_hostname(hostname))
|
|
|
|
{
|
|
|
|
if (settings->TargetNetAddressCount > 0)
|
|
|
|
{
|
2014-12-28 02:53:27 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
tcp->sockfd = freerdp_tcp_connect_multi(settings->TargetNetAddresses,
|
|
|
|
settings->TargetNetAddressCount, port, timeout);
|
|
|
|
#else
|
2014-12-26 21:49:25 +03:00
|
|
|
hostname = settings->TargetNetAddresses[0];
|
2014-12-28 02:53:27 +03:00
|
|
|
#endif
|
2014-12-26 21:49:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-28 02:53:27 +03:00
|
|
|
if (tcp->sockfd <= 0)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2014-12-28 02:53:27 +03:00
|
|
|
if (status) {
|
|
|
|
WLog_ERR(TAG, "getaddrinfo: %s", gai_strerror(status));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = result;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (!addr)
|
|
|
|
addr = result;
|
2014-10-17 14:08:39 +04:00
|
|
|
}
|
2014-12-28 00:48:32 +03:00
|
|
|
|
2014-12-28 02:53:27 +03:00
|
|
|
tcp->sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
2014-10-17 14:08:39 +04:00
|
|
|
|
2014-12-28 02:53:27 +03:00
|
|
|
if (tcp->sockfd < 0) {
|
|
|
|
freeaddrinfo(result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!freerdp_tcp_connect_timeout(tcp->sockfd, addr->ai_addr, addr->ai_addrlen, timeout))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to connect to %s\n", hostname);
|
|
|
|
freeaddrinfo(result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-10-17 14:08:39 +04:00
|
|
|
|
|
|
|
freeaddrinfo(result);
|
|
|
|
}
|
2014-12-28 00:48:32 +03:00
|
|
|
|
2014-10-17 14:08:39 +04:00
|
|
|
tcp->socketBio = BIO_new_socket(tcp->sockfd, BIO_NOCLOSE);
|
|
|
|
#endif /* NO_IPV6 */
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-12-28 02:53:27 +03:00
|
|
|
(void) BIO_set_close(tcp->socketBio, BIO_NOCLOSE);
|
2014-06-02 05:37:20 +04:00
|
|
|
BIO_free(tcp->socketBio);
|
|
|
|
|
|
|
|
tcp->socketBio = BIO_new(BIO_s_simple_socket());
|
|
|
|
|
|
|
|
if (!tcp->socketBio)
|
2014-08-06 20:08:00 +04:00
|
|
|
return FALSE;
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
BIO_set_fd(tcp->socketBio, tcp->sockfd, BIO_CLOSE);
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2015-01-28 05:18:26 +03:00
|
|
|
BIO_get_event(tcp->socketBio, &tcp->event);
|
2013-05-01 01:16:38 +04:00
|
|
|
|
2014-11-29 23:14:39 +03:00
|
|
|
freerdp_tcp_get_ip_address(tcp);
|
|
|
|
freerdp_tcp_get_mac_address(tcp);
|
2011-12-12 03:59:35 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
option_value = 1;
|
|
|
|
option_len = sizeof(option_value);
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-08-06 20:08:00 +04:00
|
|
|
if (!tcp->ipcSocket)
|
|
|
|
{
|
|
|
|
if (setsockopt(tcp->sockfd, IPPROTO_TCP, TCP_NODELAY, (void*) &option_value, option_len) < 0)
|
2014-09-12 16:36:29 +04: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 */
|
|
|
|
if (getsockopt(tcp->sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &option_value, &option_len) == 0)
|
|
|
|
{
|
|
|
|
if (option_value < (1024 * 32))
|
2011-12-12 03:59:35 +04:00
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
option_value = 1024 * 32;
|
|
|
|
option_len = sizeof(option_value);
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (setsockopt(tcp->sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &option_value, option_len) < 0)
|
2012-05-22 15:57:19 +04:00
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "unable to set receive buffer len");
|
2014-05-21 19:32:14 +04:00
|
|
|
return FALSE;
|
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
|
|
|
|
2014-08-06 20:08:00 +04:00
|
|
|
if (!tcp->ipcSocket)
|
|
|
|
{
|
2014-11-29 23:14:39 +03:00
|
|
|
if (!freerdp_tcp_set_keep_alive_mode(tcp))
|
2014-08-06 20:08:00 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-07-10 23:34:43 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
tcp->bufferedBio = BIO_new(BIO_s_buffered_socket());
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tcp->bufferedBio)
|
|
|
|
return FALSE;
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
tcp->bufferedBio->ptr = tcp;
|
2011-07-10 23:34:43 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
tcp->bufferedBio = BIO_push(tcp->bufferedBio, tcp->socketBio);
|
2014-05-30 22:03:20 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
return TRUE;
|
2012-12-21 12:23:50 +04:00
|
|
|
}
|
|
|
|
|
2014-11-29 23:14:39 +03:00
|
|
|
BOOL freerdp_tcp_disconnect(rdpTcp* tcp)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2014-11-12 22:06:34 +03:00
|
|
|
if (tcp->sockfd != -1)
|
|
|
|
{
|
|
|
|
shutdown(tcp->sockfd, SHUT_RDWR);
|
|
|
|
close(tcp->sockfd);
|
|
|
|
|
|
|
|
tcp->sockfd = -1;
|
|
|
|
}
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
2014-11-29 23:14:39 +03:00
|
|
|
BOOL freerdp_tcp_set_blocking_mode(rdpTcp* tcp, BOOL blocking)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2015-01-28 05:18:26 +03:00
|
|
|
return BIO_set_nonblock(tcp->socketBio, blocking ? 0 : 1) ? TRUE : FALSE;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
2014-11-29 23:14:39 +03:00
|
|
|
BOOL freerdp_tcp_set_keep_alive_mode(rdpTcp* tcp)
|
2011-12-12 04:51:58 +04:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 option_value;
|
2011-12-12 04:51:58 +04:00
|
|
|
socklen_t option_len;
|
|
|
|
|
|
|
|
option_value = 1;
|
|
|
|
option_len = sizeof(option_value);
|
|
|
|
|
|
|
|
if (setsockopt(tcp->sockfd, SOL_SOCKET, SO_KEEPALIVE, (void*) &option_value, option_len) < 0)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "setsockopt() SOL_SOCKET, SO_KEEPALIVE:");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-12-12 04:51:58 +04:00
|
|
|
}
|
|
|
|
|
2011-12-17 01:09:21 +04:00
|
|
|
#ifdef TCP_KEEPIDLE
|
2011-12-12 04:51:58 +04:00
|
|
|
option_value = 5;
|
|
|
|
option_len = sizeof(option_value);
|
|
|
|
|
2011-12-14 04:42:10 +04:00
|
|
|
if (setsockopt(tcp->sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (void*) &option_value, option_len) < 0)
|
2011-12-12 04:51:58 +04:00
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "setsockopt() IPPROTO_TCP, TCP_KEEPIDLE:");
|
2014-01-17 02:38:56 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TCP_KEEPCNT
|
|
|
|
option_value = 3;
|
|
|
|
option_len = sizeof(option_value);
|
|
|
|
|
|
|
|
if (setsockopt(tcp->sockfd, SOL_TCP, TCP_KEEPCNT, (void *) &option_value, option_len) < 0)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "setsockopt() SOL_TCP, TCP_KEEPCNT:");
|
2014-01-17 02:38:56 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TCP_KEEPINTVL
|
|
|
|
option_value = 2;
|
|
|
|
option_len = sizeof(option_value);
|
|
|
|
|
|
|
|
if (setsockopt(tcp->sockfd, SOL_TCP, TCP_KEEPINTVL, (void *) &option_value, option_len) < 0)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "setsockopt() SOL_TCP, TCP_KEEPINTVL:");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-12-12 04:51:58 +04:00
|
|
|
}
|
2011-12-17 01:09:21 +04:00
|
|
|
#endif
|
2011-12-12 04:51:58 +04:00
|
|
|
#endif
|
|
|
|
|
2014-11-10 22:52:29 +03:00
|
|
|
#if defined(__MACOSX__) || defined(__IOS__)
|
2014-04-11 00:26:44 +04:00
|
|
|
option_value = 1;
|
|
|
|
option_len = sizeof(option_value);
|
|
|
|
if (setsockopt(tcp->sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void *) &option_value, option_len) < 0)
|
|
|
|
{
|
2014-09-12 16:36:29 +04:00
|
|
|
WLog_ERR(TAG, "setsockopt() SOL_SOCKET, SO_NOSIGPIPE:");
|
2014-04-11 00:26:44 +04:00
|
|
|
}
|
|
|
|
#endif
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-12-12 04:51:58 +04:00
|
|
|
}
|
|
|
|
|
2014-11-29 23:14:39 +03:00
|
|
|
int freerdp_tcp_attach(rdpTcp* tcp, int sockfd)
|
2013-07-21 06:34:05 +04:00
|
|
|
{
|
|
|
|
tcp->sockfd = sockfd;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
|
|
|
ringbuffer_commit_read_bytes(&tcp->xmitBuffer, ringbuffer_used(&tcp->xmitBuffer));
|
|
|
|
|
|
|
|
if (tcp->socketBio)
|
|
|
|
{
|
|
|
|
if (BIO_set_fd(tcp->socketBio, sockfd, 1) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-02 05:37:20 +04:00
|
|
|
tcp->socketBio = BIO_new(BIO_s_simple_socket());
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tcp->socketBio)
|
|
|
|
return -1;
|
2014-06-02 05:37:20 +04:00
|
|
|
|
|
|
|
BIO_set_fd(tcp->socketBio, sockfd, BIO_CLOSE);
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!tcp->bufferedBio)
|
|
|
|
{
|
|
|
|
tcp->bufferedBio = BIO_new(BIO_s_buffered_socket());
|
2014-06-01 19:20:13 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tcp->bufferedBio)
|
|
|
|
return FALSE;
|
2014-06-01 19:20:13 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
tcp->bufferedBio->ptr = tcp;
|
|
|
|
|
|
|
|
tcp->bufferedBio = BIO_push(tcp->bufferedBio, tcp->socketBio);
|
|
|
|
}
|
|
|
|
|
2015-01-28 05:18:26 +03:00
|
|
|
BIO_get_event(tcp->socketBio, &tcp->event);
|
|
|
|
|
|
|
|
return 1;
|
2013-07-21 06:34:05 +04:00
|
|
|
}
|
|
|
|
|
2014-11-29 23:14:39 +03:00
|
|
|
HANDLE freerdp_tcp_get_event_handle(rdpTcp* tcp)
|
2013-05-01 01:16:38 +04:00
|
|
|
{
|
2013-11-02 04:23:56 +04:00
|
|
|
if (!tcp)
|
|
|
|
return NULL;
|
|
|
|
|
2013-05-01 01:16:38 +04:00
|
|
|
return tcp->event;
|
|
|
|
}
|
|
|
|
|
2014-11-29 23:14:39 +03:00
|
|
|
int freerdp_tcp_wait_read(rdpTcp* tcp, DWORD dwMilliSeconds)
|
2014-07-02 17:15:46 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
struct pollfd pollset;
|
|
|
|
|
|
|
|
pollset.fd = tcp->sockfd;
|
|
|
|
pollset.events = POLLIN;
|
|
|
|
pollset.revents = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = poll(&pollset, 1, dwMilliSeconds);
|
|
|
|
}
|
|
|
|
while ((status < 0) && (errno == EINTR));
|
|
|
|
#else
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set rset;
|
|
|
|
|
|
|
|
FD_ZERO(&rset);
|
|
|
|
FD_SET(tcp->sockfd, &rset);
|
|
|
|
|
|
|
|
if (dwMilliSeconds)
|
|
|
|
{
|
|
|
|
tv.tv_sec = dwMilliSeconds / 1000;
|
|
|
|
tv.tv_usec = (dwMilliSeconds % 1000) * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = select(tcp->sockfd + 1, &rset, NULL, NULL, dwMilliSeconds ? &tv : NULL);
|
|
|
|
}
|
|
|
|
while ((status < 0) && (errno == EINTR));
|
|
|
|
#endif
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-11-29 23:14:39 +03:00
|
|
|
int freerdp_tcp_wait_write(rdpTcp* tcp, DWORD dwMilliSeconds)
|
2014-07-02 17:15:46 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
struct pollfd pollset;
|
|
|
|
|
|
|
|
pollset.fd = tcp->sockfd;
|
|
|
|
pollset.events = POLLOUT;
|
|
|
|
pollset.revents = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = poll(&pollset, 1, dwMilliSeconds);
|
|
|
|
}
|
|
|
|
while ((status < 0) && (errno == EINTR));
|
|
|
|
#else
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set rset;
|
|
|
|
|
|
|
|
FD_ZERO(&rset);
|
|
|
|
FD_SET(tcp->sockfd, &rset);
|
|
|
|
|
|
|
|
if (dwMilliSeconds)
|
|
|
|
{
|
|
|
|
tv.tv_sec = dwMilliSeconds / 1000;
|
|
|
|
tv.tv_usec = (dwMilliSeconds % 1000) * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = select(tcp->sockfd + 1, NULL, &rset, NULL, dwMilliSeconds ? &tv : NULL);
|
|
|
|
}
|
|
|
|
while ((status < 0) && (errno == EINTR));
|
|
|
|
#endif
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-11-29 23:14:39 +03:00
|
|
|
rdpTcp* freerdp_tcp_new(rdpSettings* settings)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2011-09-25 11:12:29 +04:00
|
|
|
rdpTcp* tcp;
|
|
|
|
|
2014-05-30 22:03:20 +04:00
|
|
|
tcp = (rdpTcp*) calloc(1, sizeof(rdpTcp));
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tcp)
|
|
|
|
return NULL;
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!ringbuffer_init(&tcp->xmitBuffer, 0x10000))
|
|
|
|
goto out_free;
|
2012-11-22 04:22:41 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
tcp->sockfd = -1;
|
|
|
|
tcp->settings = settings;
|
|
|
|
|
2011-07-03 23:34:15 +04:00
|
|
|
return tcp;
|
2015-01-28 05:18:26 +03:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
out_free:
|
|
|
|
free(tcp);
|
|
|
|
return NULL;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
2014-11-29 23:14:39 +03:00
|
|
|
void freerdp_tcp_free(rdpTcp* tcp)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!tcp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ringbuffer_destroy(&tcp->xmitBuffer);
|
2015-01-28 05:18:26 +03:00
|
|
|
|
|
|
|
if (tcp->socketBio)
|
|
|
|
{
|
|
|
|
BIO_free(tcp->socketBio);
|
|
|
|
tcp->socketBio = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tcp->bufferedBio)
|
|
|
|
{
|
|
|
|
BIO_free(tcp->bufferedBio);
|
|
|
|
tcp->bufferedBio = NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
free(tcp);
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|