static void* jni_input_thread(void* arg) { HANDLE event[3]; wMessageQueue* queue; freerdp* instance = (freerdp*) arg; androidContext *aCtx = (androidContext*)instance->context; assert(NULL != instance); assert(NULL != aCtx); DEBUG_ANDROID("input_thread Start."); if (!(queue = freerdp_get_message_queue(instance, FREERDP_INPUT_MESSAGE_QUEUE))) goto fail_get_message_queue; if (!(event[0] = CreateFileDescriptorEvent(NULL, FALSE, FALSE, aCtx->event_queue->pipe_fd[0], WINPR_FD_READ))) goto fail_create_event_0; if (!(event[1] = CreateFileDescriptorEvent(NULL, FALSE, FALSE, aCtx->event_queue->pipe_fd[1], WINPR_FD_READ))) goto fail_create_event_1; if (!(event[2] = freerdp_get_message_queue_event_handle(instance, FREERDP_INPUT_MESSAGE_QUEUE))) goto fail_get_message_queue_event; do { DWORD rc = WaitForMultipleObjects(3, event, FALSE, INFINITE); if ((rc < WAIT_OBJECT_0) || (rc > WAIT_OBJECT_0 + 2)) continue; if (rc == WAIT_OBJECT_0 + 2) { wMessage msg; MessageQueue_Peek(queue, &msg, FALSE); if (msg.id == WMQ_QUIT) break; } if (android_check_fds(instance) != TRUE) break; } while(1); DEBUG_ANDROID("input_thread Quit."); fail_get_message_queue_event: CloseHandle(event[1]); fail_create_event_1: CloseHandle(event[0]); fail_create_event_0: MessageQueue_PostQuit(queue, 0); fail_get_message_queue: ExitThread(0); return NULL; }
rdpTcp* tcp_new(rdpSettings* settings) { rdpTcp* tcp; tcp = (rdpTcp*) calloc(1, sizeof(rdpTcp)); if (!tcp) return NULL; if (!ringbuffer_init(&tcp->xmitBuffer, 0x10000)) goto out_free; tcp->sockfd = -1; tcp->settings = settings; if (0) goto out_ringbuffer; /* avoid unreferenced label warning on Windows */ #ifndef _WIN32 tcp->event = CreateFileDescriptorEvent(NULL, FALSE, FALSE, tcp->sockfd); if (!tcp->event || tcp->event == INVALID_HANDLE_VALUE) goto out_ringbuffer; #endif return tcp; out_ringbuffer: ringbuffer_destroy(&tcp->xmitBuffer); out_free: free(tcp); return NULL; }
static BOOL wlf_client_new(freerdp* instance, rdpContext* context) { UwacReturnCode status; wlfContext* wfl = (wlfContext*) context; if (!instance || !context) return FALSE; instance->PreConnect = wl_pre_connect; instance->PostConnect = wl_post_connect; instance->PostDisconnect = wl_post_disconnect; instance->Authenticate = client_cli_authenticate; instance->GatewayAuthenticate = client_cli_gw_authenticate; instance->VerifyCertificate = client_cli_verify_certificate; instance->VerifyChangedCertificate = client_cli_verify_changed_certificate; instance->LogonErrorInfo = NULL; wfl->display = UwacOpenDisplay(NULL, &status); if (!wfl->display || (status != UWAC_SUCCESS)) return FALSE; wfl->displayHandle = CreateFileDescriptorEvent(NULL, FALSE, FALSE, UwacDisplayGetFd(wfl->display), WINPR_FD_READ); if (!wfl->displayHandle) return FALSE; return TRUE; }
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, WINPR_FD_READ); 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 int transport_bio_simple_init(BIO* bio, SOCKET socket, int shutdown) { WINPR_BIO_SIMPLE_SOCKET* ptr = (WINPR_BIO_SIMPLE_SOCKET*) bio->ptr; ptr->socket = socket; bio->shutdown = shutdown; bio->flags = BIO_FLAGS_SHOULD_RETRY; bio->init = 1; #ifdef _WIN32 ptr->hEvent = WSACreateEvent(); if (!ptr->hEvent) return 0; /* WSAEventSelect automatically sets the socket in non-blocking mode */ WSAEventSelect(ptr->socket, ptr->hEvent, FD_READ | FD_WRITE | FD_CLOSE); #else ptr->hEvent = CreateFileDescriptorEvent(NULL, FALSE, FALSE, (int) ptr->socket, FD_READ); if (!ptr->hEvent) return 0; #endif return 1; }
static void* tf_debug_channel_thread_func(void* arg) { void* fd; STREAM* s; void* buffer; UINT32 bytes_returned = 0; testPeerContext* context = (testPeerContext*) arg; freerdp_thread* thread = context->debug_channel_thread; if (WTSVirtualChannelQuery(context->debug_channel, WTSVirtualFileHandle, &buffer, &bytes_returned) == TRUE) { fd = *((void**) buffer); WTSFreeMemory(buffer); thread->signals[thread->num_signals++] = CreateFileDescriptorEvent(NULL, TRUE, FALSE, ((int) (long) fd)); } s = stream_new(4096); WTSVirtualChannelWrite(context->debug_channel, (BYTE*) "test1", 5, NULL); while (1) { freerdp_thread_wait(thread); if (freerdp_thread_is_stopped(thread)) break; stream_set_pos(s, 0); if (WTSVirtualChannelRead(context->debug_channel, 0, stream_get_head(s), stream_get_size(s), &bytes_returned) == FALSE) { if (bytes_returned == 0) break; stream_check_size(s, bytes_returned); if (WTSVirtualChannelRead(context->debug_channel, 0, stream_get_head(s), stream_get_size(s), &bytes_returned) == FALSE) { /* should not happen */ break; } } stream_set_pos(s, bytes_returned); printf("got %d bytes\n", bytes_returned); } stream_free(s); freerdp_thread_quit(thread); return 0; }
static void* rpc_client_thread(void* arg) { rdpRpc* rpc; DWORD status; DWORD nCount; HANDLE events[3]; HANDLE ReadEvent; int fd; rpc = (rdpRpc*) arg; fd = BIO_get_fd(rpc->TlsOut->bio, NULL); ReadEvent = CreateFileDescriptorEvent(NULL, TRUE, FALSE, fd); nCount = 0; events[nCount++] = rpc->client->StopEvent; events[nCount++] = Queue_Event(rpc->client->SendQueue); events[nCount++] = ReadEvent; /* Do a first free run in case some bytes were set from the HTTP headers. * We also have to do it because most of the time the underlying socket has notified, * and the ssl layer has eaten all bytes, so we won't be notified any more even if the * bytes are buffered locally */ if (rpc_client_on_read_event(rpc) < 0) { WLog_ERR(TAG, "an error occured when treating first packet"); goto out; } while (rpc->transport->layer != TRANSPORT_LAYER_CLOSED) { status = WaitForMultipleObjects(nCount, events, FALSE, 100); if (status == WAIT_TIMEOUT) continue; if (WaitForSingleObject(rpc->client->StopEvent, 0) == WAIT_OBJECT_0) break; if (WaitForSingleObject(ReadEvent, 0) == WAIT_OBJECT_0) { if (rpc_client_on_read_event(rpc) < 0) break; } if (WaitForSingleObject(Queue_Event(rpc->client->SendQueue), 0) == WAIT_OBJECT_0) { rpc_send_dequeue_pdu(rpc); } } out: CloseHandle(ReadEvent); return NULL; }
static BOOL freerdp_listener_open_local(freerdp_listener* instance, const char* path) { #ifndef _WIN32 int status; int sockfd; struct sockaddr_un addr; rdpListener* listener = (rdpListener*) instance->listener; sockfd = socket(AF_UNIX, SOCK_STREAM, 0); if (sockfd == -1) { perror("socket"); return FALSE; } fcntl(sockfd, F_SETFL, O_NONBLOCK); addr.sun_family = AF_UNIX; strncpy(addr.sun_path, path, sizeof(addr.sun_path)); unlink(path); status = bind(sockfd, (struct sockaddr*) &addr, sizeof(addr)); if (status != 0) { perror("bind"); close(sockfd); return FALSE; } status = listen(sockfd, 10); if (status != 0) { perror("listen"); close(sockfd); return FALSE; } listener->sockfds[listener->num_sockfds] = sockfd; listener->events[listener->num_sockfds] = CreateFileDescriptorEvent(NULL, FALSE, FALSE, sockfd); listener->num_sockfds++; fprintf(stderr, "Listening on socket %s.\n", addr.sun_path); return TRUE; #else return TRUE; #endif }
int main(int argc, char* argv[]) { UwacReturnCode status; freerdp* instance; g_display = UwacOpenDisplay(NULL, &status); if (!g_display) exit(1); g_displayHandle = CreateFileDescriptorEvent(NULL, FALSE, FALSE, UwacDisplayGetFd(g_display), WINPR_FD_READ); if (!g_displayHandle) exit(1); //if (!handle_uwac_events(NULL, g_display)) // exit(1); instance = freerdp_new(); instance->PreConnect = wl_pre_connect; instance->PostConnect = wl_post_connect; instance->PostDisconnect = wl_post_disconnect; instance->Authenticate = client_cli_authenticate; instance->GatewayAuthenticate = client_cli_gw_authenticate; instance->VerifyCertificate = client_cli_verify_certificate; instance->VerifyChangedCertificate = client_cli_verify_changed_certificate; instance->ContextSize = sizeof(wlfContext); instance->ContextNew = wl_context_new; instance->ContextFree = wl_context_free; freerdp_context_new(instance); status = freerdp_client_settings_parse_command_line_arguments(instance->settings, argc, argv, FALSE); status = freerdp_client_settings_command_line_status_print(instance->settings, status, argc, argv); if (status) exit(0); freerdp_client_load_addins(instance->context->channels, instance->settings); wlfreerdp_run(instance); freerdp_context_free(instance); freerdp_free(instance); return 0; }
rdpTcp* tcp_new(rdpSettings* settings) { rdpTcp* tcp; tcp = (rdpTcp*) malloc(sizeof(rdpTcp)); if (tcp != NULL) { ZeroMemory(tcp, sizeof(rdpTcp)); tcp->sockfd = -1; tcp->settings = settings; tcp->event = CreateFileDescriptorEvent(NULL, FALSE, FALSE, tcp->sockfd); } return tcp; }
static void* rpc_client_thread(void* arg) { rdpRpc* rpc; DWORD status; DWORD nCount; HANDLE events[3]; HANDLE ReadEvent; rpc = (rdpRpc*) arg; ReadEvent = CreateFileDescriptorEvent(NULL, TRUE, FALSE, rpc->TlsOut->sockfd); nCount = 0; events[nCount++] = rpc->client->StopEvent; events[nCount++] = Queue_Event(rpc->client->SendQueue); events[nCount++] = ReadEvent; while (1) { status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE); if (WaitForSingleObject(rpc->client->StopEvent, 0) == WAIT_OBJECT_0) { break; } if (WaitForSingleObject(ReadEvent, 0) == WAIT_OBJECT_0) { if (rpc_client_on_read_event(rpc) < 0) break; } if (WaitForSingleObject(Queue_Event(rpc->client->SendQueue), 0) == WAIT_OBJECT_0) { rpc_send_dequeue_pdu(rpc); } } CloseHandle(ReadEvent); rpc_client_free(rpc); return NULL; }
static BOOL freerdp_listener_open_local(freerdp_listener* instance, const char* path) { #ifndef _WIN32 int status; int sockfd; struct sockaddr_un addr; 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) { WLog_ERR(TAG, "socket"); return FALSE; } fcntl(sockfd, F_SETFL, O_NONBLOCK); addr.sun_family = AF_UNIX; strncpy(addr.sun_path, path, sizeof(addr.sun_path)); unlink(path); status = _bind(sockfd, (struct sockaddr*) &addr, sizeof(addr)); if (status != 0) { WLog_ERR(TAG, "bind"); closesocket((SOCKET) sockfd); return FALSE; } status = _listen(sockfd, 10); if (status != 0) { WLog_ERR(TAG, "listen"); closesocket((SOCKET) sockfd); return FALSE; } hevent = CreateFileDescriptorEvent(NULL, FALSE, FALSE, sockfd, WINPR_FD_READ); if (!hevent) { WLog_ERR(TAG, "failed to create sockfd event"); closesocket((SOCKET) sockfd); return FALSE; } listener->sockfds[listener->num_sockfds] = sockfd; listener->events[listener->num_sockfds] = hevent; listener->num_sockfds++; WLog_INFO(TAG, "Listening on socket %s.", addr.sun_path); return TRUE; #else return TRUE; #endif }
static BOOL freerdp_listener_open(freerdp_listener* instance, const char* bind_address, UINT16 port) { rdpListener* listener = (rdpListener*) instance->listener; int status; int sockfd; char servname[10]; struct addrinfo hints = { 0 }; struct addrinfo* res; struct addrinfo* ai; int option_value; void* sin_addr; char buf[50]; #ifdef _WIN32 u_long arg; #endif hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if (bind_address == NULL) hints.ai_flags = AI_PASSIVE; sprintf_s(servname, sizeof(servname), "%d", port); status = getaddrinfo(bind_address, servname, &hints, &res); if (status != 0) { #ifdef _WIN32 _tprintf(_T("getaddrinfo error: %s\n"), gai_strerror(status)); #else perror("getaddrinfo"); #endif return FALSE; } for (ai = res; ai && listener->num_sockfds < 5; ai = ai->ai_next) { if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) continue; sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (sockfd == -1) { perror("socket"); continue; } option_value = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*) &option_value, sizeof(option_value)) == -1) perror("setsockopt"); #ifndef _WIN32 fcntl(sockfd, F_SETFL, O_NONBLOCK); #else arg = 1; ioctlsocket(sockfd, FIONBIO, &arg); #endif status = bind(sockfd, ai->ai_addr, ai->ai_addrlen); if (status != 0) { #ifdef _WIN32 _tprintf(L"bind() failed with error: %u\n", WSAGetLastError()); WSACleanup(); #else perror("bind"); close(sockfd); #endif continue; } status = listen(sockfd, 10); if (status != 0) { perror("listen"); close(sockfd); continue; } listener->sockfds[listener->num_sockfds] = sockfd; listener->events[listener->num_sockfds] = CreateFileDescriptorEvent(NULL, FALSE, FALSE, sockfd); listener->num_sockfds++; if (ai->ai_family == AF_INET) sin_addr = &(((struct sockaddr_in*) ai->ai_addr)->sin_addr); else sin_addr = &(((struct sockaddr_in6*) ai->ai_addr)->sin6_addr); fprintf(stderr, "Listening on %s port %s.\n", inet_ntop(ai->ai_family, sin_addr, buf, sizeof(buf)), servname); } freeaddrinfo(res); return (listener->num_sockfds > 0 ? TRUE : FALSE); }
static BOOL freerdp_listener_open(freerdp_listener* instance, const char* bind_address, UINT16 port) { int status; int sockfd; char addr[64]; void* sin_addr; int option_value; char servname[16]; struct addrinfo* ai; struct addrinfo* res; struct addrinfo hints = { 0 }; rdpListener* listener = (rdpListener*) instance->listener; #ifdef _WIN32 u_long arg; #endif hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if (!bind_address) hints.ai_flags = AI_PASSIVE; sprintf_s(servname, sizeof(servname), "%d", port); status = getaddrinfo(bind_address, servname, &hints, &res); if (status != 0) { #ifdef _WIN32 WLog_ERR("getaddrinfo error: %s", gai_strerrorA(status)); #else WLog_ERR(TAG, "getaddrinfo"); #endif return FALSE; } for (ai = res; ai && (listener->num_sockfds < 5); ai = ai->ai_next) { if ((ai->ai_family != AF_INET) && (ai->ai_family != AF_INET6)) continue; sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (sockfd == -1) { WLog_ERR(TAG, "socket"); continue; } if (ai->ai_family == AF_INET) sin_addr = &(((struct sockaddr_in*) ai->ai_addr)->sin_addr); else sin_addr = &(((struct sockaddr_in6*) ai->ai_addr)->sin6_addr); inet_ntop(ai->ai_family, sin_addr, addr, sizeof(addr)); option_value = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*) &option_value, sizeof(option_value)) == -1) WLog_ERR(TAG, "setsockopt"); #ifndef _WIN32 fcntl(sockfd, F_SETFL, O_NONBLOCK); #else arg = 1; ioctlsocket(sockfd, FIONBIO, &arg); #endif status = bind(sockfd, ai->ai_addr, ai->ai_addrlen); if (status != 0) { #ifdef _WIN32 WLog_ERR("bind() failed with error: %u", WSAGetLastError()); WSACleanup(); #else WLog_ERR(TAG, "bind"); close(sockfd); #endif continue; } status = listen(sockfd, 10); if (status != 0) { WLog_ERR(TAG, "listen"); close(sockfd); continue; } /* FIXME: these file descriptors do not work on Windows */ listener->sockfds[listener->num_sockfds] = sockfd; listener->events[listener->num_sockfds] = CreateFileDescriptorEvent(NULL, FALSE, FALSE, sockfd); listener->num_sockfds++; WLog_INFO(TAG, "Listening on %s:%s", addr, servname); } freeaddrinfo(res); return (listener->num_sockfds > 0 ? TRUE : FALSE); }