Skip to content

Commit

Permalink
Move get_peer_addr to socket_common.c for reuse.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakio815 committed Jan 31, 2025
1 parent 9358e76 commit 9f79d09
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
8 changes: 8 additions & 0 deletions network/api/socket_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ ssize_t peek_from_socket(int socket, unsigned char* result);
* @return True if closed, false if still connected.
*/
bool check_socket_closed(int socket);
/**
* Get the connected peer name from the connected socket.
* Set it to the server_ip_addr. Also, set server_hostname if LOG_LEVEL is higher than LOG_LEVEL_DEBUG.
*
* @param priv The socket_priv struct.
* @return 0 for success, -1 for failure.
*/
int get_peer_address(socket_priv_t* priv);

/**
* Write the specified number of bytes to the specified socket from the
Expand Down
23 changes: 0 additions & 23 deletions network/impl/src/lf_socket_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,6 @@ static socket_priv_t* get_socket_priv_t(netdrv_t drv) {
return (socket_priv_t*)drv;
}

static int get_peer_address(netdrv_t drv) {
socket_priv_t* priv = get_socket_priv_t(drv);
struct sockaddr_in peer_addr;
socklen_t addr_len = sizeof(peer_addr);
if (getpeername(priv->socket_descriptor, (struct sockaddr*)&peer_addr, &addr_len) != 0) {
lf_print_error("Failed to get peer address.");
return -1;
}
priv->server_ip_addr = peer_addr.sin_addr;

#if LOG_LEVEL >= LOG_LEVEL_DEBUG
// Create the human readable format and copy that into
// the .server_hostname field of the federate.
char str[INET_ADDRSTRLEN + 1];
inet_ntop(AF_INET, &priv->server_ip_addr, str, INET_ADDRSTRLEN);
strncpy(priv->server_hostname, str, INET_ADDRSTRLEN - 1); // Copy up to INET_ADDRSTRLEN - 1 characters
priv->server_hostname[INET_ADDRSTRLEN - 1] = '\0'; // Null-terminate explicitly

LF_PRINT_DEBUG("Got address %s", priv->server_hostname);
#endif
return 0;
}

netdrv_t initialize_netdrv() {
// Initialize priv.
socket_priv_t* priv = malloc(sizeof(socket_priv_t));
Expand Down
23 changes: 23 additions & 0 deletions network/impl/src/socket_common.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <unistd.h> // Defines read(), write(), and close()
#include <netinet/in.h> // IPPROTO_TCP, IPPROTO_UDP
#include <netinet/tcp.h> // TCP_NODELAY
#include <arpa/inet.h> // inet_ntop
#include <errno.h>
#include <stdio.h>
#include <sys/time.h>
Expand Down Expand Up @@ -180,6 +181,28 @@ bool check_socket_closed(int socket) {
}
}

int get_peer_address(socket_priv_t* priv) {
struct sockaddr_in peer_addr;
socklen_t addr_len = sizeof(peer_addr);
if (getpeername(priv->socket_descriptor, (struct sockaddr*)&peer_addr, &addr_len) != 0) {
lf_print_error("Failed to get peer address.");
return -1;
}
priv->server_ip_addr = peer_addr.sin_addr;

#if LOG_LEVEL >= LOG_LEVEL_DEBUG
// Create the human readable format and copy that into
// the .server_hostname field of the federate.
char str[INET_ADDRSTRLEN + 1];
inet_ntop(AF_INET, &priv->server_ip_addr, str, INET_ADDRSTRLEN);
strncpy(priv->server_hostname, str, INET_ADDRSTRLEN - 1); // Copy up to INET_ADDRSTRLEN - 1 characters
priv->server_hostname[INET_ADDRSTRLEN - 1] = '\0'; // Null-terminate explicitly

LF_PRINT_DEBUG("Got address %s", priv->server_hostname);
#endif
return 0;
}

int accept_socket(int socket, int rti_socket) {
struct sockaddr client_fd;
// Wait for an incoming connection request.
Expand Down

0 comments on commit 9f79d09

Please sign in to comment.