Merge pull request #2577 from hardening/listen_from_socket

Add the ability to have listener from an existing socket
This commit is contained in:
Norbert Federa 2015-05-08 11:17:33 +02:00
commit 9fe7ad2878
2 changed files with 45 additions and 0 deletions

View File

@ -33,6 +33,7 @@ extern "C" {
typedef BOOL (*psListenerOpen)(freerdp_listener* instance, const char* bind_address, UINT16 port);
typedef BOOL (*psListenerOpenLocal)(freerdp_listener* instance, const char* path);
typedef BOOL (*psListenerOpenFromSocket)(freerdp_listener* instance, int fd);
typedef BOOL (*psListenerGetFileDescriptor)(freerdp_listener* instance, void** rfds, int* rcount);
typedef DWORD (*psListenerGetEventHandles)(freerdp_listener* instance, HANDLE* events, DWORD nCount);
typedef BOOL (*psListenerCheckFileDescriptor)(freerdp_listener* instance);
@ -56,6 +57,7 @@ struct rdp_freerdp_listener
psListenerClose Close;
psPeerAccepted PeerAccepted;
psListenerOpenFromSocket OpenFromSocket;
};
FREERDP_API freerdp_listener* freerdp_listener_new(void);

View File

@ -115,6 +115,12 @@ static BOOL freerdp_listener_open(freerdp_listener* instance, const char* bind_a
if ((ai->ai_family != AF_INET) && (ai->ai_family != AF_INET6))
continue;
if (listener->num_sockfds == MAX_LISTENER_HANDLES)
{
WLog_ERR(TAG, "too many listening sockets");
continue;
}
sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (sockfd == -1)
@ -182,6 +188,12 @@ static BOOL freerdp_listener_open_local(freerdp_listener* instance, const char*
rdpListener* listener = (rdpListener*) instance->listener;
HANDLE hevent;
if (listener->num_sockfds == MAX_LISTENER_HANDLES)
{
WLog_ERR(TAG, "too many listening sockets");
return FALSE;
}
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd == -1)
@ -231,6 +243,36 @@ static BOOL freerdp_listener_open_local(freerdp_listener* instance, const char*
#endif
}
static BOOL freerdp_listener_open_from_socket(freerdp_listener* instance, int fd)
{
#ifndef _WIN32
rdpListener* listener = (rdpListener*) instance->listener;
if (listener->num_sockfds == MAX_LISTENER_HANDLES)
{
WLog_ERR(TAG, "too many listening sockets");
return FALSE;
}
if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
return FALSE;
listener->sockfds[listener->num_sockfds] = fd;
listener->events[listener->num_sockfds] = CreateFileDescriptorEvent(NULL, FALSE, FALSE, fd);
if (!listener->events[listener->num_sockfds])
return FALSE;
listener->num_sockfds++;
WLog_INFO(TAG, "Listening on socket %d.", fd);
return TRUE;
#else
return FALSE;
#endif
}
static void freerdp_listener_close(freerdp_listener* instance)
{
int i;
@ -375,6 +417,7 @@ freerdp_listener* freerdp_listener_new(void)
instance->Open = freerdp_listener_open;
instance->OpenLocal = freerdp_listener_open_local;
instance->OpenFromSocket = freerdp_listener_open_from_socket;
instance->GetFileDescriptor = freerdp_listener_get_fds;
instance->GetEventHandles = freerdp_listener_get_event_handles;
instance->CheckFileDescriptor = freerdp_listener_check_fds;