diff --git a/src/protocols/rdp/rdp.c b/src/protocols/rdp/rdp.c index 1a16780b5f..830a42ef8e 100644 --- a/src/protocols/rdp/rdp.c +++ b/src/protocols/rdp/rdp.c @@ -76,6 +76,7 @@ #include #include #include +#include #include #include #include @@ -681,19 +682,49 @@ void* guac_rdp_client_thread(void* data) { guac_rdp_client* rdp_client = (guac_rdp_client*) client->data; guac_rdp_settings* settings = rdp_client->settings; - /* If Wake-on-LAN is enabled, try to wake. */ + /* If Wake-on-LAN is enabled, attempt to wake. */ if (settings->wol_send_packet) { - guac_client_log(client, GUAC_LOG_DEBUG, "Sending Wake-on-LAN packet, " - "and pausing for %d seconds.", settings->wol_wait_time); - - /* Send the Wake-on-LAN request. */ - if (guac_wol_wake(settings->wol_mac_addr, settings->wol_broadcast_addr, - settings->wol_udp_port)) + + /** + * If wait time is set, send the wake packet and try to connect to the + * server, failing if the server does not respond. + */ + if (settings->wol_wait_time > 0) { + guac_client_log(client, GUAC_LOG_DEBUG, "Sending Wake-on-LAN packet, " + "and pausing for %d seconds.", settings->wol_wait_time); + + /* char representation of a port should be, at most, 5 digits plus terminator. */ + char* str_port = guac_mem_alloc(6); + if (guac_itoa(str_port, settings->port) < 1) { + guac_client_log(client, GUAC_LOG_ERROR, "Failed to convert port to integer for WOL function."); + guac_mem_free(str_port); + return NULL; + } + + /* Send the Wake-on-LAN request and wait until the server is responsive. */ + if (guac_wol_wake_and_wait(settings->wol_mac_addr, + settings->wol_broadcast_addr, + settings->wol_udp_port, + settings->wol_wait_time, + GUAC_WOL_DEFAULT_CONNECT_RETRIES, + settings->hostname, + (const char *) str_port)) { + guac_client_log(client, GUAC_LOG_ERROR, "Failed to send WOL packet, or server failed to wake up."); + guac_mem_free(str_port); + return NULL; + } + + guac_mem_free(str_port); + + } + + /* Just send the packet and continue the connection, or return if failed. */ + else if(guac_wol_wake(settings->wol_mac_addr, + settings->wol_broadcast_addr, + settings->wol_udp_port)) { + guac_client_log(client, GUAC_LOG_ERROR, "Failed to send WOL packet."); return NULL; - - /* If wait time is specified, sleep for that amount of time. */ - if (settings->wol_wait_time > 0) - guac_timestamp_msleep(settings->wol_wait_time * 1000); + } } /* If audio enabled, choose an encoder */ diff --git a/src/protocols/ssh/ssh.c b/src/protocols/ssh/ssh.c index 1e18fed4ca..f1eb195798 100644 --- a/src/protocols/ssh/ssh.c +++ b/src/protocols/ssh/ssh.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -233,17 +234,35 @@ void* ssh_client_thread(void* data) { /* If Wake-on-LAN is enabled, attempt to wake. */ if (settings->wol_send_packet) { - guac_client_log(client, GUAC_LOG_DEBUG, "Sending Wake-on-LAN packet, " - "and pausing for %d seconds.", settings->wol_wait_time); - /* Send the Wake-on-LAN request. */ - if (guac_wol_wake(settings->wol_mac_addr, settings->wol_broadcast_addr, - settings->wol_udp_port)) - return NULL; + /** + * If wait time is set, send the wake packet and try to connect to the + * server, failing if the server does not respond. + */ + if (settings->wol_wait_time > 0) { + guac_client_log(client, GUAC_LOG_DEBUG, "Sending Wake-on-LAN packet, " + "and pausing for %d seconds.", settings->wol_wait_time); + + /* Send the Wake-on-LAN request and wait until the server is responsive. */ + if (guac_wol_wake_and_wait(settings->wol_mac_addr, + settings->wol_broadcast_addr, + settings->wol_udp_port, + settings->wol_wait_time, + GUAC_WOL_DEFAULT_CONNECT_RETRIES, + settings->hostname, + settings->port)) { + guac_client_log(client, GUAC_LOG_ERROR, "Failed to send WOL packet or connect to remote server."); + return NULL; + } + } - /* If wait time is specified, sleep for that amount of time. */ - if (settings->wol_wait_time > 0) - guac_timestamp_msleep(settings->wol_wait_time * 1000); + /* Just send the packet and continue the connection, or return if failed. */ + else if(guac_wol_wake(settings->wol_mac_addr, + settings->wol_broadcast_addr, + settings->wol_udp_port)) { + guac_client_log(client, GUAC_LOG_ERROR, "Failed to send WOL packet."); + return NULL; + } } /* Init SSH base libraries */ diff --git a/src/protocols/telnet/telnet.c b/src/protocols/telnet/telnet.c index 57e2d8be6b..77412961b7 100644 --- a/src/protocols/telnet/telnet.c +++ b/src/protocols/telnet/telnet.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -494,17 +495,35 @@ void* guac_telnet_client_thread(void* data) { /* If Wake-on-LAN is enabled, attempt to wake. */ if (settings->wol_send_packet) { - guac_client_log(client, GUAC_LOG_DEBUG, "Sending Wake-on-LAN packet, " - "and pausing for %d seconds.", settings->wol_wait_time); - /* Send the Wake-on-LAN request. */ - if (guac_wol_wake(settings->wol_mac_addr, settings->wol_broadcast_addr, - settings->wol_udp_port)) - return NULL; + /** + * If wait time is set, send the wake packet and try to connect to the + * server, failing if the server does not respond. + */ + if (settings->wol_wait_time > 0) { + guac_client_log(client, GUAC_LOG_DEBUG, "Sending Wake-on-LAN packet, " + "and pausing for %d seconds.", settings->wol_wait_time); + + /* Send the Wake-on-LAN request and wait until the server is responsive. */ + if (guac_wol_wake_and_wait(settings->wol_mac_addr, + settings->wol_broadcast_addr, + settings->wol_udp_port, + settings->wol_wait_time, + GUAC_WOL_DEFAULT_CONNECT_RETRIES, + settings->hostname, + settings->port)) { + guac_client_log(client, GUAC_LOG_ERROR, "Failed to send WOL packet or connect to remote server."); + return NULL; + } + } - /* If wait time is specified, sleep for that amount of time. */ - if (settings->wol_wait_time > 0) - guac_timestamp_msleep(settings->wol_wait_time * 1000); + /* Just send the packet and continue the connection, or return if failed. */ + else if(guac_wol_wake(settings->wol_mac_addr, + settings->wol_broadcast_addr, + settings->wol_udp_port)) { + guac_client_log(client, GUAC_LOG_ERROR, "Failed to send WOL packet."); + return NULL; + } } /* Set up screen recording, if requested */ diff --git a/src/protocols/vnc/vnc.c b/src/protocols/vnc/vnc.c index 3b94d5e759..04eb88feda 100644 --- a/src/protocols/vnc/vnc.c +++ b/src/protocols/vnc/vnc.c @@ -42,10 +42,13 @@ #endif #include +#include #include #include #include +#include #include +#include #include #include #include @@ -274,17 +277,47 @@ void* guac_vnc_client_thread(void* data) { /* If Wake-on-LAN is enabled, attempt to wake. */ if (settings->wol_send_packet) { - guac_client_log(client, GUAC_LOG_DEBUG, "Sending Wake-on-LAN packet, " - "and pausing for %d seconds.", settings->wol_wait_time); - - /* Send the Wake-on-LAN request. */ - if (guac_wol_wake(settings->wol_mac_addr, settings->wol_broadcast_addr, - settings->wol_udp_port)) + + /** + * If wait time is set, send the wake packet and try to connect to the + * server, failing if the server does not respond. + */ + if (settings->wol_wait_time > 0) { + guac_client_log(client, GUAC_LOG_DEBUG, "Sending Wake-on-LAN packet, " + "and pausing for %d seconds.", settings->wol_wait_time); + + /* char representation of a port should be, at most, 5 characters plus terminator. */ + char* str_port = guac_mem_alloc(6); + if (guac_itoa(str_port, settings->port) < 1) { + guac_client_log(client, GUAC_LOG_ERROR, "Failed to convert port to integer for WOL function."); + guac_mem_free(str_port); + return NULL; + } + + /* Send the Wake-on-LAN request and wait until the server is responsive. */ + if (guac_wol_wake_and_wait(settings->wol_mac_addr, + settings->wol_broadcast_addr, + settings->wol_udp_port, + settings->wol_wait_time, + GUAC_WOL_DEFAULT_CONNECT_RETRIES, + settings->hostname, + (const char *) str_port)) { + guac_client_log(client, GUAC_LOG_ERROR, "Failed to send WOL packet or connect to remote system."); + guac_mem_free(str_port); + return NULL; + } + + guac_mem_free(str_port); + + } + + /* Just send the packet and continue the connection, or return if failed. */ + else if(guac_wol_wake(settings->wol_mac_addr, + settings->wol_broadcast_addr, + settings->wol_udp_port)) { + guac_client_log(client, GUAC_LOG_ERROR, "Failed to send WOL packet."); return NULL; - - /* If wait time is specified, sleep for that amount of time. */ - if (settings->wol_wait_time > 0) - guac_timestamp_msleep(settings->wol_wait_time * 1000); + } } /* Configure clipboard encoding */