Skip to content

Commit

Permalink
GUACAMOLE-600: Refactor guac_socket_tcp_connect() to guac_tcp_connect…
Browse files Browse the repository at this point in the history
…(), as it does not involve guac_socket.
  • Loading branch information
mike-jumper committed Aug 28, 2024
1 parent 68f1181 commit 5ec496f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/common-ssh/ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include <guacamole/client.h>
#include <guacamole/fips.h>
#include <guacamole/mem.h>
#include <guacamole/socket-tcp.h>
#include <guacamole/string.h>
#include <guacamole/tcp.h>
#include <libssh2.h>

#ifdef LIBSSH2_USES_GCRYPT
Expand Down Expand Up @@ -417,7 +417,7 @@ guac_common_ssh_session* guac_common_ssh_create_session(guac_client* client,
int timeout, int keepalive, const char* host_key,
guac_ssh_credential_handler* credential_handler) {

int fd = guac_socket_tcp_connect(hostname, port, timeout);
int fd = guac_tcp_connect(hostname, port, timeout);
if (fd < 0) {
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
"Failed to open TCP connection to %s on %s.", hostname, port);
Expand Down
4 changes: 2 additions & 2 deletions src/libguac/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ libguacinc_HEADERS = \
guacamole/socket-constants.h \
guacamole/socket.h \
guacamole/socket-fntypes.h \
guacamole/socket-tcp.h \
guacamole/socket-types.h \
guacamole/stream.h \
guacamole/stream-types.h \
guacamole/string.h \
guacamole/tcp.h \
guacamole/timestamp.h \
guacamole/timestamp-types.h \
guacamole/unicode.h \
Expand Down Expand Up @@ -129,9 +129,9 @@ libguac_la_SOURCES = \
socket-broadcast.c \
socket-fd.c \
socket-nest.c \
socket-tcp.c \
socket-tee.c \
string.c \
tcp.c \
timestamp.c \
unicode.c \
user.c \
Expand Down
22 changes: 15 additions & 7 deletions src/libguac/guacamole/socket-tcp.h → src/libguac/guacamole/tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@
* under the License.
*/

#ifndef __GUAC_SOCKET_TCP_H
#define __GUAC_SOCKET_TCP_H
#ifndef GUAC_TCP_H
#define GUAC_TCP_H

/**
* Provides convenience functions for establishing low-level TCP connections.
*
* @file tcp.h
*/

#include "config.h"

#include <stddef.h>

/**
* Given a hostname or IP address and port, attempt to connect to that
* system, returning an open socket if the connection succeeds, or a negative
* value if it fails. If it fails the errno variable will be set.
* Given a hostname or IP address and port, attempt to connect to that system,
* returning the file descriptor of an open socket if the connection succeeds,
* or a negative value if it fails. The returned file descriptor must
* eventually be freed with a call to close(). If this function fails,
* guac_error will be set appropriately.
*
* @param hostname
* The hostname or IP address to which to attempt connections.
Expand All @@ -42,6 +50,6 @@
* A valid socket if the connection succeeds, or a negative integer if it
* fails.
*/
int guac_socket_tcp_connect(const char* hostname, const char* port, const int timeout);
int guac_tcp_connect(const char* hostname, const char* port, const int timeout);

#endif // __GUAC_SOCKET_TCP_H
#endif // GUAC_TCP_H
7 changes: 4 additions & 3 deletions src/libguac/socket-tcp.c → src/libguac/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@

#include "config.h"
#include "guacamole/error.h"
#include "guacamole/socket.h"
#include "guacamole/tcp.h"

#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <unistd.h>

int guac_socket_tcp_connect(const char* hostname, const char* port, const int timeout) {
int guac_tcp_connect(const char* hostname, const char* port, const int timeout) {

int retval;

Expand Down Expand Up @@ -117,4 +118,4 @@ int guac_socket_tcp_connect(const char* hostname, const char* port, const int ti
/* Return the fd, or the error message if the socket connection failed. */
return fd;

}
}
6 changes: 3 additions & 3 deletions src/libguac/wol.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "config.h"

#include "guacamole/error.h"
#include "guacamole/socket-tcp.h"
#include "guacamole/tcp.h"
#include "guacamole/timestamp.h"
#include "guacamole/wol.h"

Expand Down Expand Up @@ -204,7 +204,7 @@ int guac_wol_wake_and_wait(const char* mac_addr, const char* broadcast_addr,
const char* hostname, const char* port, const int timeout) {

/* Attempt to connect, first. */
int sockfd = guac_socket_tcp_connect(hostname, port, timeout);
int sockfd = guac_tcp_connect(hostname, port, timeout);

/* If connection succeeds, no need to wake the system. */
if (sockfd > 0) {
Expand All @@ -225,7 +225,7 @@ int guac_wol_wake_and_wait(const char* mac_addr, const char* broadcast_addr,
/* Try to connect on the specified TCP port and hostname or IP. */
for (int i = 0; i < retries; i++) {

sockfd = guac_socket_tcp_connect(hostname, port, timeout);
sockfd = guac_tcp_connect(hostname, port, timeout);

/* Connection succeeded - close socket and exit. */
if (sockfd > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/protocols/telnet/telnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <guacamole/mem.h>
#include <guacamole/protocol.h>
#include <guacamole/recording.h>
#include <guacamole/socket-tcp.h>
#include <guacamole/tcp.h>
#include <guacamole/timestamp.h>
#include <guacamole/wol-constants.h>
#include <guacamole/wol.h>
Expand Down Expand Up @@ -386,7 +386,7 @@ static telnet_t* __guac_telnet_create_session(guac_client* client) {
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
guac_telnet_settings* settings = telnet_client->settings;

int fd = guac_socket_tcp_connect(settings->hostname, settings->port, settings->timeout);
int fd = guac_tcp_connect(settings->hostname, settings->port, settings->timeout);

/* Open telnet session */
telnet_t* telnet = telnet_init(__telnet_options, __guac_telnet_event_handler, 0, client);
Expand Down

0 comments on commit 5ec496f

Please sign in to comment.