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);
190 /* Peer disconnected. */
196 int wsa_error = WSAGetLastError();
198 /* No data available */
199 if (wsa_error == WSAEWOULDBLOCK)
202 printf("recv() error: %d\n", wsa_error);
204 /* No data available */
205 if (errno == EAGAIN || errno == EWOULDBLOCK)
216 int tcp_write(rdpTcp* tcp, uint8* data, int length)
220 status = send(tcp->sockfd, data, length, MSG_NOSIGNAL);
225 int wsa_error = WSAGetLastError();
227 /* No data available */
228 if (wsa_error == WSAEWOULDBLOCK)
233 if (errno == EAGAIN || errno == EWOULDBLOCK)
243 boolean tcp_disconnect(rdpTcp * tcp)
245 if (tcp->sockfd != -1)
247 shutdown(tcp->sockfd, SHUT_RDWR);
255 boolean tcp_set_blocking_mode(rdpTcp* tcp, boolean blocking)
259 flags = fcntl(tcp->sockfd, F_GETFL);
263 printf("tcp_set_blocking_mode: fcntl failed.\n");
267 if (blocking == true)
268 fcntl(tcp->sockfd, F_SETFL, flags & ~(O_NONBLOCK));
270 fcntl(tcp->sockfd, F_SETFL, flags | O_NONBLOCK);
272 u_long arg = blocking;
273 ioctlsocket(tcp->sockfd, FIONBIO, &arg);
274 tcp->wsa_event = WSACreateEvent();
275 WSAEventSelect(tcp->sockfd, tcp->wsa_event, FD_READ);
281 boolean tcp_set_keep_alive_mode(rdpTcp* tcp)
285 socklen_t option_len;
288 option_len = sizeof(option_value);
290 if (setsockopt(tcp->sockfd, SOL_SOCKET, SO_KEEPALIVE, (void*) &option_value, option_len) < 0)
292 perror("setsockopt() SOL_SOCKET, SO_KEEPALIVE:");
298 option_len = sizeof(option_value);
300 if (setsockopt(tcp->sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (void*) &option_value, option_len) < 0)
302 perror("setsockopt() IPPROTO_TCP, SO_KEEPIDLE:");
311 rdpTcp* tcp_new(rdpSettings* settings)
315 tcp = (rdpTcp*) xzalloc(sizeof(rdpTcp));
320 tcp->settings = settings;
326 void tcp_free(rdpTcp* tcp)