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>
|
|
|
|
|
2011-08-16 23:35:29 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
#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>
|
2011-12-14 04:42:10 +04:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#ifndef TCP_KEEPIDLE
|
|
|
|
#define TCP_KEEPIDLE TCP_KEEPALIVE
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2011-08-17 01:40:29 +04:00
|
|
|
#else
|
2011-09-23 22:25:38 +04:00
|
|
|
#define SHUT_RDWR SD_BOTH
|
2011-08-17 01:40:29 +04:00
|
|
|
#define close(_fd) closesocket(_fd)
|
2011-08-16 22:41:12 +04:00
|
|
|
#endif
|
|
|
|
|
2012-03-06 23:41:07 +04:00
|
|
|
#include <freerdp/utils/tcp.h>
|
2012-05-22 15:57:19 +04:00
|
|
|
#include <freerdp/utils/uds.h>
|
2013-03-22 00:45:25 +04:00
|
|
|
#include <winpr/stream.h>
|
2011-07-03 23:34:15 +04:00
|
|
|
|
|
|
|
#include "tcp.h"
|
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
void 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
|
|
|
}
|
|
|
|
|
2013-11-08 00:14:59 +04:00
|
|
|
void 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)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "failed to obtain MAC address\n");
|
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
|
2011-07-12 10:53:26 +04:00
|
|
|
|
2013-03-29 02:06:34 +04:00
|
|
|
/* fprintf(stderr, "MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
|
2011-07-12 10:53:26 +04:00
|
|
|
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); */
|
|
|
|
}
|
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
BOOL tcp_connect(rdpTcp* tcp, const char* hostname, int port)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 option_value;
|
2011-12-12 03:59:35 +04:00
|
|
|
socklen_t option_len;
|
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] == '/')
|
|
|
|
{
|
|
|
|
tcp->sockfd = freerdp_uds_connect(hostname);
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2012-05-22 15:57:19 +04:00
|
|
|
if (tcp->sockfd < 0)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-05-22 15:57:19 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tcp->sockfd = freerdp_tcp_connect(hostname, port);
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2012-05-22 15:57:19 +04:00
|
|
|
if (tcp->sockfd < 0)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2013-05-01 01:16:38 +04:00
|
|
|
SetEventFileDescriptor(tcp->event, tcp->sockfd);
|
|
|
|
|
2012-05-22 15:57:19 +04:00
|
|
|
tcp_get_ip_address(tcp);
|
|
|
|
tcp_get_mac_address(tcp);
|
2011-12-12 03:59:35 +04:00
|
|
|
|
2012-05-22 15:57:19 +04:00
|
|
|
option_value = 1;
|
|
|
|
option_len = sizeof(option_value);
|
|
|
|
setsockopt(tcp->sockfd, IPPROTO_TCP, TCP_NODELAY, (void*) &option_value, option_len);
|
|
|
|
|
|
|
|
/* receive buffer must be a least 32 K */
|
|
|
|
if (getsockopt(tcp->sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &option_value, &option_len) == 0)
|
2011-12-12 03:59:35 +04:00
|
|
|
{
|
2012-05-22 15:57:19 +04:00
|
|
|
if (option_value < (1024 * 32))
|
|
|
|
{
|
|
|
|
option_value = 1024 * 32;
|
|
|
|
option_len = sizeof(option_value);
|
|
|
|
setsockopt(tcp->sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &option_value, option_len);
|
|
|
|
}
|
2011-12-12 03:59:35 +04:00
|
|
|
}
|
|
|
|
|
2012-05-22 15:57:19 +04:00
|
|
|
tcp_set_keep_alive_mode(tcp);
|
|
|
|
}
|
2011-12-12 04:51:58 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
int tcp_read(rdpTcp* tcp, BYTE* data, int length)
|
2011-07-10 23:34:43 +04:00
|
|
|
{
|
2012-03-06 23:41:07 +04:00
|
|
|
return freerdp_tcp_read(tcp->sockfd, data, length);
|
2011-07-10 23:34:43 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
int tcp_write(rdpTcp* tcp, BYTE* data, int length)
|
2011-07-10 23:34:43 +04:00
|
|
|
{
|
2012-03-06 23:41:07 +04:00
|
|
|
return freerdp_tcp_write(tcp->sockfd, data, length);
|
2011-07-10 23:34:43 +04:00
|
|
|
}
|
|
|
|
|
2012-12-21 12:23:50 +04:00
|
|
|
int tcp_wait_read(rdpTcp* tcp)
|
|
|
|
{
|
|
|
|
return freerdp_tcp_wait_read(tcp->sockfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int tcp_wait_write(rdpTcp* tcp)
|
|
|
|
{
|
|
|
|
return freerdp_tcp_wait_write(tcp->sockfd);
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL tcp_disconnect(rdpTcp* tcp)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2012-03-06 23:41:07 +04:00
|
|
|
freerdp_tcp_disconnect(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
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL tcp_set_blocking_mode(rdpTcp* tcp, BOOL blocking)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2011-08-17 01:40:29 +04:00
|
|
|
#ifndef _WIN32
|
2011-07-03 23:34:15 +04:00
|
|
|
int flags;
|
|
|
|
flags = fcntl(tcp->sockfd, F_GETFL);
|
|
|
|
|
|
|
|
if (flags == -1)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "tcp_set_blocking_mode: fcntl failed.\n");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
if (blocking == TRUE)
|
2011-07-03 23:34:15 +04:00
|
|
|
fcntl(tcp->sockfd, F_SETFL, flags & ~(O_NONBLOCK));
|
|
|
|
else
|
|
|
|
fcntl(tcp->sockfd, F_SETFL, flags | O_NONBLOCK);
|
2011-08-17 01:40:29 +04:00
|
|
|
#else
|
2012-06-13 07:09:30 +04:00
|
|
|
int status;
|
2011-08-17 01:40:29 +04:00
|
|
|
u_long arg = blocking;
|
2012-06-13 07:09:30 +04:00
|
|
|
|
|
|
|
status = ioctlsocket(tcp->sockfd, FIONBIO, &arg);
|
|
|
|
|
|
|
|
if (status != NO_ERROR)
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "ioctlsocket() failed with error: %ld\n", status);
|
2012-06-13 07:09:30 +04:00
|
|
|
|
2011-08-17 01:40:29 +04:00
|
|
|
tcp->wsa_event = WSACreateEvent();
|
|
|
|
WSAEventSelect(tcp->sockfd, tcp->wsa_event, FD_READ);
|
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL 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)
|
|
|
|
{
|
|
|
|
perror("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
|
|
|
{
|
2011-12-14 04:42:10 +04:00
|
|
|
perror("setsockopt() IPPROTO_TCP, SO_KEEPIDLE:");
|
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
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-12-12 04:51:58 +04:00
|
|
|
}
|
|
|
|
|
2013-07-21 06:34:05 +04:00
|
|
|
int tcp_attach(rdpTcp* tcp, int sockfd)
|
|
|
|
{
|
|
|
|
tcp->sockfd = sockfd;
|
|
|
|
SetEventFileDescriptor(tcp->event, tcp->sockfd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-01 01:16:38 +04:00
|
|
|
HANDLE tcp_get_event_handle(rdpTcp* tcp)
|
|
|
|
{
|
2013-11-02 04:23:56 +04:00
|
|
|
if (!tcp)
|
|
|
|
return NULL;
|
|
|
|
|
2013-05-01 01:16:38 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
return tcp->event;
|
|
|
|
#else
|
|
|
|
return (HANDLE) tcp->wsa_event;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-07-10 20:10:24 +04:00
|
|
|
rdpTcp* tcp_new(rdpSettings* settings)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2011-09-25 11:12:29 +04:00
|
|
|
rdpTcp* tcp;
|
|
|
|
|
2012-11-22 04:22:41 +04:00
|
|
|
tcp = (rdpTcp*) malloc(sizeof(rdpTcp));
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2013-10-11 13:07:33 +04:00
|
|
|
if (tcp)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2012-11-22 04:22:41 +04:00
|
|
|
ZeroMemory(tcp, sizeof(rdpTcp));
|
|
|
|
|
2011-07-03 23:34:15 +04:00
|
|
|
tcp->sockfd = -1;
|
2011-07-10 20:10:24 +04:00
|
|
|
tcp->settings = settings;
|
2013-05-01 01:16:38 +04:00
|
|
|
tcp->event = CreateFileDescriptorEvent(NULL, FALSE, FALSE, tcp->sockfd);
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return tcp;
|
|
|
|
}
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void tcp_free(rdpTcp* tcp)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2013-05-01 01:16:38 +04:00
|
|
|
if (tcp)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2013-05-01 01:16:38 +04:00
|
|
|
CloseHandle(tcp->event);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(tcp);
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
}
|