2
* FreeRDP: A Remote Desktop Protocol Client
3
* Transmission Control Protocol (TCP)
5
* Copyright 2011 Vic Lee
6
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
31
#include <sys/ioctl.h>
32
#include <sys/socket.h>
33
#include <netinet/in.h>
34
#include <netinet/tcp.h>
39
#define TCP_KEEPIDLE TCP_KEEPALIVE
44
#define SHUT_RDWR SD_BOTH
45
#define close(_fd) closesocket(_fd)
48
#include <freerdp/utils/print.h>
49
#include <freerdp/utils/stream.h>
50
#include <freerdp/utils/memory.h>
54
void tcp_get_ip_address(rdpTcp * tcp)
58
struct sockaddr_in sockaddr;
60
length = sizeof(sockaddr);
62
if (getsockname(tcp->sockfd, (struct sockaddr*) &sockaddr, &length) == 0)
64
ip = (uint8*) (&sockaddr.sin_addr);
65
snprintf(tcp->ip_address, sizeof(tcp->ip_address),
66
"%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
70
strncpy(tcp->ip_address, "127.0.0.1", sizeof(tcp->ip_address));
73
tcp->ip_address[sizeof(tcp->ip_address) - 1] = 0;
75
tcp->settings->ipv6 = 0;
76
tcp->settings->ip_address = xstrdup(tcp->ip_address);
79
void tcp_get_mac_address(rdpTcp * tcp)
84
struct if_nameindex* ni;
87
mac = tcp->mac_address;
89
while (ni->if_name != NULL)
91
if (strcmp(ni->if_name, "lo") != 0)
97
strncpy(if_req.ifr_name, ni->if_name, IF_NAMESIZE);
99
if (ioctl(tcp->sockfd, SIOCGIFHWADDR, &if_req) != 0)
101
printf("failed to obtain MAC address\n");
105
memmove((void*) mac, (void*) &if_req.ifr_ifru.ifru_hwaddr.sa_data[0], 6);
108
/* printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
109
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); */
112
boolean tcp_connect(rdpTcp* tcp, const char* hostname, uint16 port)
117
socklen_t option_len;
118
struct addrinfo hints = { 0 };
119
struct addrinfo * res, * ai;
121
memset(&hints, 0, sizeof(struct addrinfo));
122
hints.ai_family = AF_UNSPEC;
123
hints.ai_socktype = SOCK_STREAM;
125
snprintf(servname, sizeof(servname), "%d", port);
126
status = getaddrinfo(hostname, servname, &hints, &res);
130
printf("transport_connect: getaddrinfo (%s)\n", gai_strerror(status));
135
for (ai = res; ai; ai = ai->ai_next)
137
tcp->sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
142
if (connect(tcp->sockfd, ai->ai_addr, ai->ai_addrlen) == 0)
144
printf("connected to %s:%s\n", hostname, servname);
153
if (tcp->sockfd == -1)
155
printf("unable to connect to %s:%s\n", hostname, servname);
159
tcp_get_ip_address(tcp);
160
tcp_get_mac_address(tcp);
163
option_len = sizeof(option_value);
164
setsockopt(tcp->sockfd, IPPROTO_TCP, TCP_NODELAY, (void*) &option_value, option_len);
166
/* receive buffer must be a least 32 K */
167
if (getsockopt(tcp->sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &option_value, &option_len) == 0)
169
if (option_value < (1024 * 32))
171
option_value = 1024 * 32;
172
option_len = sizeof(option_value);
173
setsockopt(tcp->sockfd, SOL_SOCKET, SO_RCVBUF, (void*) &option_value, option_len);
177
tcp_set_keep_alive_mode(tcp);
182
int tcp_read(rdpTcp* tcp, uint8* data, int length)
186
status = recv(tcp->sockfd, data, length, 0);
191
int wsa_error = WSAGetLastError();
193
/* No data available */
194
if (wsa_error == WSAEWOULDBLOCK)
197
/* When peer disconnects we get status 0 with no error. */
199
printf("recv() error: %d\n", wsa_error);
201
/* No data available */
202
if (errno == EAGAIN || errno == EWOULDBLOCK)
205
/* When peer disconnects we get status 0 with no error. */
215
int tcp_write(rdpTcp* tcp, uint8* data, int length)
219
status = send(tcp->sockfd, data, length, MSG_NOSIGNAL);
223
if (errno == EAGAIN || errno == EWOULDBLOCK)
232
boolean tcp_disconnect(rdpTcp * tcp)
234
if (tcp->sockfd != -1)
236
shutdown(tcp->sockfd, SHUT_RDWR);
244
boolean tcp_set_blocking_mode(rdpTcp* tcp, boolean blocking)
248
flags = fcntl(tcp->sockfd, F_GETFL);
252
printf("tcp_set_blocking_mode: fcntl failed.\n");
256
if (blocking == true)
257
fcntl(tcp->sockfd, F_SETFL, flags & ~(O_NONBLOCK));
259
fcntl(tcp->sockfd, F_SETFL, flags | O_NONBLOCK);
261
u_long arg = blocking;
262
ioctlsocket(tcp->sockfd, FIONBIO, &arg);
263
tcp->wsa_event = WSACreateEvent();
264
WSAEventSelect(tcp->sockfd, tcp->wsa_event, FD_READ);
270
boolean tcp_set_keep_alive_mode(rdpTcp* tcp)
274
socklen_t option_len;
277
option_len = sizeof(option_value);
279
if (setsockopt(tcp->sockfd, SOL_SOCKET, SO_KEEPALIVE, (void*) &option_value, option_len) < 0)
281
perror("setsockopt() SOL_SOCKET, SO_KEEPALIVE:");
287
option_len = sizeof(option_value);
289
if (setsockopt(tcp->sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (void*) &option_value, option_len) < 0)
291
perror("setsockopt() IPPROTO_TCP, SO_KEEPIDLE:");
300
rdpTcp* tcp_new(rdpSettings* settings)
304
tcp = (rdpTcp*) xzalloc(sizeof(rdpTcp));
309
tcp->settings = settings;
315
void tcp_free(rdpTcp* tcp)