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.
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
33 #include <freerdp/utils/print.h>
34 #include <freerdp/utils/stream.h>
35 #include <freerdp/utils/memory.h>
39 void tcp_get_ip_address(rdpTcp * tcp)
43 struct sockaddr_in sockaddr;
45 length = sizeof(sockaddr);
47 if (getsockname(tcp->sockfd, (struct sockaddr*) &sockaddr, &length) == 0)
49 ip = (uint8*) (&sockaddr.sin_addr);
50 snprintf(tcp->ip_address, sizeof(tcp->ip_address),
51 "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
55 strncpy(tcp->ip_address, "127.0.0.1", sizeof(tcp->ip_address));
58 tcp->ip_address[sizeof(tcp->ip_address) - 1] = 0;
60 tcp->settings->ipv6 = 0;
61 tcp->settings->ip_address = tcp->ip_address;
64 void tcp_get_mac_address(rdpTcp * tcp)
69 struct if_nameindex* ni;
72 mac = tcp->mac_address;
74 while (ni->if_name != NULL)
76 if (strcmp(ni->if_name, "lo") != 0)
82 strncpy(if_req.ifr_name, ni->if_name, IF_NAMESIZE);
84 if (ioctl(tcp->sockfd, SIOCGIFHWADDR, &if_req) != 0)
86 printf("failed to obtain MAC address\n");
90 memmove((void*) mac, (void*) &if_req.ifr_ifru.ifru_hwaddr.sa_data[0], 6);
93 /* printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
94 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); */
97 boolean tcp_connect(rdpTcp* tcp, const char* hostname, uint16 port)
102 struct addrinfo hints = { 0 };
103 struct addrinfo * res, * ai;
105 hints.ai_family = AF_UNSPEC;
106 hints.ai_socktype = SOCK_STREAM;
108 snprintf(servname, sizeof(servname), "%d", port);
109 status = getaddrinfo(hostname, servname, &hints, &res);
113 printf("transport_connect: getaddrinfo (%s)\n", gai_strerror(status));
117 for (ai = res; ai; ai = ai->ai_next)
119 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
124 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) == 0)
126 printf("connected to %s:%s\n", hostname, servname);
136 printf("unable to connect to %s:%s\n", hostname, servname);
140 tcp->sockfd = sockfd;
141 tcp_get_ip_address(tcp);
142 tcp_get_mac_address(tcp);
147 int tcp_read(rdpTcp* tcp, uint8* data, int length)
151 status = recv(tcp->sockfd, data, length, 0);
155 if (errno == EAGAIN || errno == EWOULDBLOCK)
165 int tcp_write(rdpTcp* tcp, uint8* data, int length)
169 status = send(tcp->sockfd, data, length, MSG_NOSIGNAL);
173 if (errno == EAGAIN || errno == EWOULDBLOCK)
182 boolean tcp_disconnect(rdpTcp * tcp)
184 if (tcp->sockfd != -1)
193 boolean tcp_set_blocking_mode(rdpTcp* tcp, boolean blocking)
196 flags = fcntl(tcp->sockfd, F_GETFL);
200 printf("transport_configure_sockfd: fcntl failed.\n");
204 if (blocking == True)
207 fcntl(tcp->sockfd, F_SETFL, flags & ~(O_NONBLOCK));
212 fcntl(tcp->sockfd, F_SETFL, flags | O_NONBLOCK);
218 rdpTcp* tcp_new(rdpSettings* settings)
220 rdpTcp* tcp = (rdpTcp*) xzalloc(sizeof(rdpTcp));
225 tcp->settings = settings;
226 tcp->connect = tcp_connect;
227 tcp->disconnect = tcp_disconnect;
228 tcp->set_blocking_mode = tcp_set_blocking_mode;
234 void tcp_free(rdpTcp* tcp)