20 #include <freerdp/config.h>
28 #include <winpr/crt.h>
29 #include <winpr/windows.h>
30 #include <freerdp/log.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <arpa/inet.h>
39 #include <netinet/in.h>
43 #if defined(HAVE_AF_VSOCK_H)
45 #include <linux/vm_sockets.h>
48 #include <winpr/handle.h>
53 #define TAG FREERDP_TAG("core.listener")
55 static BOOL freerdp_listener_open_from_vsock(freerdp_listener* instance,
const char* bind_address,
58 #if defined(HAVE_AF_VSOCK_H)
59 rdpListener* listener = (rdpListener*)instance->listener;
60 const int sockfd = socket(AF_VSOCK, SOCK_STREAM, 0);
63 char ebuffer[256] = { 0 };
64 WLog_ERR(TAG,
"Error creating socket: %s", winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
67 const int flags = fcntl(sockfd, F_GETFL, 0);
68 if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1)
70 char ebuffer[256] = { 0 };
71 WLog_ERR(TAG,
"Error making socket nonblocking: %s",
72 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
76 struct sockaddr_vm addr = { 0 };
78 addr.svm_family = AF_VSOCK;
83 unsigned long val = strtoul(bind_address, &ptr, 10);
84 if (errno || (val > UINT32_MAX))
86 char ebuffer[256] = { 0 };
87 WLog_ERR(TAG,
"could not extract port from '%s', value=%ul, error=%s", bind_address, val,
88 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
92 addr.svm_cid = WINPR_ASSERTING_INT_CAST(
unsigned int, val);
93 if (bind(sockfd, (
struct sockaddr*)&addr,
sizeof(
struct sockaddr_vm)) == -1)
95 char ebuffer[256] = { 0 };
96 WLog_ERR(TAG,
"Error binding vsock at cid %d port %d: %s", addr.svm_cid, port,
97 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
102 if (listen(sockfd, 10) == -1)
104 char ebuffer[256] = { 0 };
105 WLog_ERR(TAG,
"Error listening to socket at cid %d port %d: %s", addr.svm_cid, port,
106 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
110 listener->sockfds[listener->num_sockfds] = sockfd;
111 listener->events[listener->num_sockfds] = WSACreateEvent();
113 if (!listener->events[listener->num_sockfds])
115 listener->num_sockfds = 0;
118 WSAEventSelect((SOCKET)sockfd, listener->events[listener->num_sockfds],
119 FD_READ | FD_ACCEPT | FD_CLOSE);
120 listener->num_sockfds++;
122 WLog_INFO(TAG,
"Listening on %s:%d", bind_address, port);
125 WLog_ERR(TAG,
"compiled without AF_VSOCK, '%s' not supported", bind_address);
130 static BOOL freerdp_listener_open(freerdp_listener* instance,
const char* bind_address, UINT16 port)
136 void* sin_addr = NULL;
137 int option_value = 0;
138 struct addrinfo* res = NULL;
139 rdpListener* listener = (rdpListener*)instance->listener;
145 ai_flags = AI_PASSIVE;
147 if (utils_is_vsock(bind_address))
149 bind_address = utils_is_vsock(bind_address);
150 return freerdp_listener_open_from_vsock(instance, bind_address, port);
153 res = freerdp_tcp_resolve_host(bind_address, port, ai_flags);
158 for (
struct addrinfo* ai = res; ai && (listener->num_sockfds < 5); ai = ai->ai_next)
160 if ((ai->ai_family != AF_INET) && (ai->ai_family != AF_INET6))
163 if (listener->num_sockfds == MAX_LISTENER_HANDLES)
165 WLog_ERR(TAG,
"too many listening sockets");
169 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
173 WLog_ERR(TAG,
"socket");
179 if (ai->ai_family == AF_INET)
180 sin_addr = &(((
struct sockaddr_in*)ai->ai_addr)->sin_addr);
183 sin_addr = &(((
struct sockaddr_in6*)ai->ai_addr)->sin6_addr);
184 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (
void*)&option_value,
185 sizeof(option_value)) == -1)
186 WLog_ERR(TAG,
"setsockopt");
189 inet_ntop(ai->ai_family, sin_addr, addr,
sizeof(addr));
191 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (
void*)&option_value,
192 sizeof(option_value)) == -1)
193 WLog_ERR(TAG,
"setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR)");
196 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0)
197 WLog_ERR(TAG,
"fcntl(sockfd, F_SETFL, O_NONBLOCK)");
200 ioctlsocket(sockfd, FIONBIO, &arg);
202 status = _bind((SOCKET)sockfd, ai->ai_addr, WINPR_ASSERTING_INT_CAST(
int, ai->ai_addrlen));
206 closesocket((SOCKET)sockfd);
210 status = _listen((SOCKET)sockfd, 10);
214 WLog_ERR(TAG,
"listen");
215 closesocket((SOCKET)sockfd);
220 listener->sockfds[listener->num_sockfds] = sockfd;
221 listener->events[listener->num_sockfds] = WSACreateEvent();
223 if (!listener->events[listener->num_sockfds])
225 listener->num_sockfds = 0;
229 WSAEventSelect((SOCKET)sockfd, listener->events[listener->num_sockfds],
230 FD_READ | FD_ACCEPT | FD_CLOSE);
231 listener->num_sockfds++;
232 WLog_INFO(TAG,
"Listening on [%s]:%" PRIu16, addr, port);
236 return (listener->num_sockfds > 0 ? TRUE : FALSE);
239 static BOOL freerdp_listener_open_local(freerdp_listener* instance,
const char* path)
244 struct sockaddr_un addr = { 0 };
245 rdpListener* listener = (rdpListener*)instance->listener;
246 HANDLE hevent = NULL;
248 if (listener->num_sockfds == MAX_LISTENER_HANDLES)
250 WLog_ERR(TAG,
"too many listening sockets");
254 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
258 WLog_ERR(TAG,
"socket");
262 int rc = fcntl(sockfd, F_SETFL, O_NONBLOCK);
265 WLog_ERR(TAG,
"fcntl(sockfd, F_SETFL, O_NONBLOCK)");
266 closesocket((SOCKET)sockfd);
270 addr.sun_family = AF_UNIX;
271 strncpy(addr.sun_path, path,
sizeof(addr.sun_path) - 1);
273 status = _bind((SOCKET)sockfd, (
struct sockaddr*)&addr,
sizeof(addr));
277 WLog_ERR(TAG,
"bind");
278 closesocket((SOCKET)sockfd);
282 status = _listen((SOCKET)sockfd, 10);
286 WLog_ERR(TAG,
"listen");
287 closesocket((SOCKET)sockfd);
291 hevent = CreateFileDescriptorEvent(NULL, FALSE, FALSE, sockfd, WINPR_FD_READ);
295 WLog_ERR(TAG,
"failed to create sockfd event");
296 closesocket((SOCKET)sockfd);
300 listener->sockfds[listener->num_sockfds] = sockfd;
301 listener->events[listener->num_sockfds] = hevent;
302 listener->num_sockfds++;
303 WLog_INFO(TAG,
"Listening on socket %s.", addr.sun_path);
310 static BOOL freerdp_listener_open_from_socket(freerdp_listener* instance,
int fd)
313 rdpListener* listener = (rdpListener*)instance->listener;
315 if (listener->num_sockfds == MAX_LISTENER_HANDLES)
317 WLog_ERR(TAG,
"too many listening sockets");
321 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
324 listener->sockfds[listener->num_sockfds] = fd;
325 listener->events[listener->num_sockfds] = WSACreateEvent();
327 if (!listener->events[listener->num_sockfds])
330 WSAEventSelect((SOCKET)fd, listener->events[listener->num_sockfds],
331 FD_READ | FD_ACCEPT | FD_CLOSE);
333 listener->num_sockfds++;
334 WLog_INFO(TAG,
"Listening on socket %d.", fd);
341 static void freerdp_listener_close(freerdp_listener* instance)
343 rdpListener* listener = (rdpListener*)instance->listener;
345 for (
int i = 0; i < listener->num_sockfds; i++)
347 closesocket((SOCKET)listener->sockfds[i]);
348 (void)CloseHandle(listener->events[i]);
351 listener->num_sockfds = 0;
354 #if defined(WITH_FREERDP_DEPRECATED)
355 static BOOL freerdp_listener_get_fds(freerdp_listener* instance,
void** rfds,
int* rcount)
357 rdpListener* listener = (rdpListener*)instance->listener;
359 if (listener->num_sockfds < 1)
362 for (
int index = 0; index < listener->num_sockfds; index++)
364 rfds[*rcount] = (
void*)(
long)(listener->sockfds[index]);
372 static DWORD freerdp_listener_get_event_handles(freerdp_listener* instance, HANDLE* events,
375 rdpListener* listener = (rdpListener*)instance->listener;
377 if (listener->num_sockfds < 1)
380 if (listener->num_sockfds > (INT64)nCount)
383 for (
int index = 0; index < listener->num_sockfds; index++)
385 events[index] = listener->events[index];
388 return WINPR_ASSERTING_INT_CAST(uint32_t, listener->num_sockfds);
391 BOOL freerdp_peer_set_local_and_hostname(freerdp_peer* client,
392 const struct sockaddr_storage* peer_addr)
394 const void* sin_addr = NULL;
395 const BYTE localhost6_bytes[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
397 WINPR_ASSERT(client);
398 WINPR_ASSERT(peer_addr);
400 if (peer_addr->ss_family == AF_INET)
402 const UINT32* usin_addr = sin_addr = &(((
const struct sockaddr_in*)peer_addr)->sin_addr);
404 if ((*usin_addr) == 0x0100007f)
405 client->local = TRUE;
407 else if (peer_addr->ss_family == AF_INET6)
409 const struct sockaddr_in6* usin_addr = sin_addr =
410 &(((
const struct sockaddr_in6*)peer_addr)->sin6_addr);
412 if (memcmp(usin_addr, localhost6_bytes, 16) == 0)
413 client->local = TRUE;
417 #if defined(HAVE_AF_VSOCK_H)
418 else if (peer_addr->ss_family == AF_UNIX || peer_addr->ss_family == AF_VSOCK)
420 else if (peer_addr->ss_family == AF_UNIX)
422 client->local = TRUE;
426 WLog_INFO(TAG,
"Accepting client from localhost");
429 inet_ntop(peer_addr->ss_family, sin_addr, client->hostname,
sizeof(client->hostname));
434 static BOOL freerdp_check_and_create_client(freerdp_listener* instance,
int peer_sockfd,
435 const struct sockaddr_storage* peer_addr)
437 WINPR_ASSERT(instance);
438 WINPR_ASSERT(peer_sockfd >= 0);
439 WINPR_ASSERT(peer_addr);
441 const BOOL check = IFCALLRESULT(TRUE, instance->CheckPeerAcceptRestrictions, instance);
444 closesocket((SOCKET)peer_sockfd);
448 freerdp_peer* client = freerdp_peer_new(peer_sockfd);
451 closesocket((SOCKET)peer_sockfd);
455 if (!freerdp_peer_set_local_and_hostname(client, peer_addr))
457 freerdp_peer_free(client);
461 const BOOL peer_accepted = IFCALLRESULT(FALSE, instance->PeerAccepted, instance, client);
464 WLog_ERR(TAG,
"PeerAccepted callback failed");
465 freerdp_peer_free(client);
471 static BOOL freerdp_listener_check_fds(freerdp_listener* instance)
473 rdpListener* listener = (rdpListener*)instance->listener;
475 if (listener->num_sockfds < 1)
478 for (
int i = 0; i < listener->num_sockfds; i++)
480 struct sockaddr_storage peer_addr = { 0 };
482 (void)WSAResetEvent(listener->events[i]);
483 int peer_addr_size =
sizeof(peer_addr);
485 _accept((SOCKET)listener->sockfds[i], (
struct sockaddr*)&peer_addr, &peer_addr_size);
487 if (peer_sockfd == (SOCKET)-1)
489 char buffer[128] = { 0 };
491 int wsa_error = WSAGetLastError();
494 if (wsa_error == WSAEWOULDBLOCK)
499 if (errno == EAGAIN || errno == EWOULDBLOCK)
503 WLog_WARN(TAG,
"accept failed with %s", winpr_strerror(errno, buffer,
sizeof(buffer)));
507 if (!freerdp_check_and_create_client(instance, (
int)peer_sockfd, &peer_addr))
514 freerdp_listener* freerdp_listener_new(
void)
516 freerdp_listener* instance = NULL;
517 rdpListener* listener = NULL;
518 instance = (freerdp_listener*)calloc(1,
sizeof(freerdp_listener));
523 instance->Open = freerdp_listener_open;
524 instance->OpenLocal = freerdp_listener_open_local;
525 instance->OpenFromSocket = freerdp_listener_open_from_socket;
526 #if defined(WITH_FREERDP_DEPRECATED)
527 instance->GetFileDescriptor = freerdp_listener_get_fds;
529 instance->GetEventHandles = freerdp_listener_get_event_handles;
530 instance->CheckFileDescriptor = freerdp_listener_check_fds;
531 instance->Close = freerdp_listener_close;
532 listener = (rdpListener*)calloc(1,
sizeof(rdpListener));
540 listener->instance = instance;
541 instance->listener = (
void*)listener;
545 void freerdp_listener_free(freerdp_listener* instance)
549 free(instance->listener);