2011-07-03 23:34:15 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Client
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <netdb.h>
|
2011-07-12 10:53:26 +04:00
|
|
|
#include <net/if.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>
|
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
|
|
|
|
|
2011-08-16 23:00:25 +04:00
|
|
|
#include <freerdp/utils/print.h>
|
2011-07-03 23:34:15 +04:00
|
|
|
#include <freerdp/utils/stream.h>
|
|
|
|
#include <freerdp/utils/memory.h>
|
|
|
|
|
|
|
|
#include "tcp.h"
|
|
|
|
|
2011-07-12 10:53:26 +04:00
|
|
|
void tcp_get_ip_address(rdpTcp * tcp)
|
2011-07-10 20:10:24 +04:00
|
|
|
{
|
|
|
|
uint8* ip;
|
|
|
|
socklen_t length;
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
|
|
|
|
length = sizeof(sockaddr);
|
|
|
|
|
|
|
|
if (getsockname(tcp->sockfd, (struct sockaddr*) &sockaddr, &length) == 0)
|
|
|
|
{
|
|
|
|
ip = (uint8*) (&sockaddr.sin_addr);
|
|
|
|
snprintf(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
|
|
|
{
|
2011-07-10 20:10:24 +04:00
|
|
|
strncpy(tcp->ip_address, "127.0.0.1", sizeof(tcp->ip_address));
|
2011-07-27 03:14:11 +04:00
|
|
|
}
|
2011-07-10 20:10:24 +04:00
|
|
|
|
|
|
|
tcp->ip_address[sizeof(tcp->ip_address) - 1] = 0;
|
|
|
|
|
|
|
|
tcp->settings->ipv6 = 0;
|
2011-09-07 10:43:06 +04:00
|
|
|
tcp->settings->ip_address = xstrdup(tcp->ip_address);
|
2011-07-10 20:10:24 +04:00
|
|
|
}
|
|
|
|
|
2011-07-12 10:53:26 +04:00
|
|
|
void tcp_get_mac_address(rdpTcp * tcp)
|
|
|
|
{
|
2011-07-25 02:09:53 +04:00
|
|
|
#ifdef LINUX
|
2011-07-12 10:53:26 +04:00
|
|
|
uint8* mac;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
printf("failed to obtain MAC address\n");
|
|
|
|
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
|
|
|
|
|
|
|
/* printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
|
|
|
|
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); */
|
|
|
|
}
|
|
|
|
|
2011-07-27 03:14:11 +04:00
|
|
|
boolean tcp_connect(rdpTcp* tcp, const char* hostname, uint16 port)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
char servname[10];
|
2011-12-12 03:59:35 +04:00
|
|
|
uint32 option_value;
|
|
|
|
socklen_t option_len;
|
2011-07-03 23:34:15 +04:00
|
|
|
struct addrinfo hints = { 0 };
|
|
|
|
struct addrinfo * res, * ai;
|
|
|
|
|
2011-09-25 11:12:29 +04:00
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
2011-07-03 23:34:15 +04:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
snprintf(servname, sizeof(servname), "%d", port);
|
|
|
|
status = getaddrinfo(hostname, servname, &hints, &res);
|
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("transport_connect: getaddrinfo (%s)\n", gai_strerror(status));
|
2011-11-19 21:19:16 +04:00
|
|
|
return false;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
2011-08-24 19:07:55 +04:00
|
|
|
tcp->sockfd = -1;
|
2011-07-03 23:34:15 +04:00
|
|
|
for (ai = res; ai; ai = ai->ai_next)
|
|
|
|
{
|
2011-08-24 19:07:55 +04:00
|
|
|
tcp->sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2011-08-24 19:07:55 +04:00
|
|
|
if (tcp->sockfd < 0)
|
2011-07-03 23:34:15 +04:00
|
|
|
continue;
|
|
|
|
|
2011-08-24 19:07:55 +04:00
|
|
|
if (connect(tcp->sockfd, ai->ai_addr, ai->ai_addrlen) == 0)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
|
|
|
printf("connected to %s:%s\n", hostname, servname);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-08-24 19:07:55 +04:00
|
|
|
close(tcp->sockfd);
|
|
|
|
tcp->sockfd = -1;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
freeaddrinfo(res);
|
|
|
|
|
2011-08-24 19:07:55 +04:00
|
|
|
if (tcp->sockfd == -1)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
|
|
|
printf("unable to connect to %s:%s\n", hostname, servname);
|
2011-11-19 21:19:16 +04:00
|
|
|
return false;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
2011-07-10 20:10:24 +04:00
|
|
|
tcp_get_ip_address(tcp);
|
2011-07-12 10:53:26 +04:00
|
|
|
tcp_get_mac_address(tcp);
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2011-12-12 03:59:35 +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)
|
|
|
|
{
|
|
|
|
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 04:51:58 +04:00
|
|
|
tcp_set_keep_alive_mode(tcp);
|
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
return true;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
int tcp_read(rdpTcp* tcp, uint8* data, int length)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
status = recv(tcp->sockfd, data, length, 0);
|
|
|
|
|
2011-08-18 19:14:46 +04:00
|
|
|
if (status <= 0)
|
2011-07-10 23:34:43 +04:00
|
|
|
{
|
2011-09-25 22:54:07 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
int wsa_error = WSAGetLastError();
|
|
|
|
|
|
|
|
/* No data available */
|
|
|
|
if (wsa_error == WSAEWOULDBLOCK)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* When peer disconnects we get status 0 with no error. */
|
|
|
|
if (status < 0)
|
|
|
|
printf("recv() error: %d\n", wsa_error);
|
|
|
|
#else
|
2011-08-18 19:14:46 +04:00
|
|
|
/* No data available */
|
2011-07-10 23:34:43 +04:00
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
return 0;
|
2011-09-25 22:54:07 +04:00
|
|
|
|
2011-08-18 19:14:46 +04:00
|
|
|
/* When peer disconnects we get status 0 with no error. */
|
|
|
|
if (status < 0)
|
|
|
|
perror("recv");
|
2011-09-25 22:54:07 +04:00
|
|
|
#endif
|
2011-07-10 23:34:43 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tcp_write(rdpTcp* tcp, uint8* data, int length)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
2011-08-01 11:04:07 +04:00
|
|
|
status = send(tcp->sockfd, data, length, MSG_NOSIGNAL);
|
2011-07-10 23:34:43 +04:00
|
|
|
|
2011-08-01 11:04:07 +04:00
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
status = 0;
|
2011-07-10 23:34:43 +04:00
|
|
|
else
|
2011-08-01 11:04:07 +04:00
|
|
|
perror("send");
|
2011-07-10 23:34:43 +04:00
|
|
|
}
|
|
|
|
|
2011-08-01 11:04:07 +04:00
|
|
|
return status;
|
2011-07-10 23:34:43 +04:00
|
|
|
}
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
boolean tcp_disconnect(rdpTcp * tcp)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
|
|
|
if (tcp->sockfd != -1)
|
|
|
|
{
|
2011-09-19 12:34:53 +04:00
|
|
|
shutdown(tcp->sockfd, SHUT_RDWR);
|
2011-07-03 23:34:15 +04:00
|
|
|
close(tcp->sockfd);
|
|
|
|
tcp->sockfd = -1;
|
|
|
|
}
|
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
return true;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
boolean tcp_set_blocking_mode(rdpTcp* tcp, boolean 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)
|
|
|
|
{
|
2011-12-12 03:05:32 +04:00
|
|
|
printf("tcp_set_blocking_mode: fcntl failed.\n");
|
2011-11-19 21:19:16 +04:00
|
|
|
return false;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
2011-11-19 21:19:16 +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
|
|
|
|
u_long arg = blocking;
|
|
|
|
ioctlsocket(tcp->sockfd, FIONBIO, &arg);
|
|
|
|
tcp->wsa_event = WSACreateEvent();
|
|
|
|
WSAEventSelect(tcp->sockfd, tcp->wsa_event, FD_READ);
|
|
|
|
#endif
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2011-11-19 21:19:16 +04:00
|
|
|
return true;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
2011-12-12 04:51:58 +04:00
|
|
|
boolean tcp_set_keep_alive_mode(rdpTcp* tcp)
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
uint32 option_value;
|
|
|
|
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:");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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:");
|
2011-12-12 04:51:58 +04:00
|
|
|
return false;
|
|
|
|
}
|
2011-12-17 01:09:21 +04:00
|
|
|
#endif
|
2011-12-12 04:51:58 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
tcp = (rdpTcp*) xzalloc(sizeof(rdpTcp));
|
2011-07-03 23:34:15 +04:00
|
|
|
|
|
|
|
if (tcp != NULL)
|
|
|
|
{
|
|
|
|
tcp->sockfd = -1;
|
2011-07-10 20:10:24 +04:00
|
|
|
tcp->settings = settings;
|
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
|
|
|
{
|
|
|
|
if (tcp != NULL)
|
|
|
|
{
|
|
|
|
xfree(tcp);
|
|
|
|
}
|
|
|
|
}
|