29 #include <arpa/inet.h>
35 #define MAX_READ_ATTEMPTS 100
41 log(LOG_READS,
"Attempting to read payload of size %d", (
int)size - (
int)rtn);
42 int new_rtn = recv(fd, buff + rtn, size - rtn, 0);
43 if (new_rtn < 0 && errno != EAGAIN) {
53 }
while ((errno == EAGAIN || rtn > 0) && (
int)rtn < (
int)size);
55 log(LOG_CONNECTIONS,
"fd %d has been closed by peer", fd);
59 log_error(
"Could not read full runtime payload from socket %d. "
60 "Requested: %d, received: %d", fd, (
int)size, (
int)rtn);
68 while (size < data_len) {
69 ssize_t rtn = write(fd, data + size, data_len - size);
71 log_error(
"Error sending buffer to endpoint with socket %d", fd);
73 }
else if (rtn < data_len) {
74 log_warn(
"Full buffer not sent to endpoint!");
83 int sock = socket(AF_INET, SOCK_STREAM, 0);
91 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
92 &val,
sizeof(val)) < 0 ) {
96 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
97 &val,
sizeof(val)) < 0) {
100 char ip[INET_ADDRSTRLEN];
101 inet_ntop(AF_INET, &addr->sin_addr, ip, INET_ADDRSTRLEN);
102 int port = ntohs(addr->sin_port);
103 log(LOG_CONNECTIONS,
"Attepting to connect to socket at %s:%d", ip, port);
104 if (connect(sock, (
struct sockaddr*)addr,
sizeof(*addr)) < 0) {
105 log_perror(
"Failed to connect to socket at %s:%d", ip, port);
110 log(LOG_CONNECTIONS,
"Connected socket to %s:%d", ip, port);
115 int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
121 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &val,
sizeof(val)) < 0) {
122 log_perror(
"Error setting SO_REUSEPORT on socket");
125 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val,
sizeof(val)) < 0) {
126 log_perror(
"Error setting SO_REUSEADDR on socket");
130 struct sockaddr_in addr;
131 addr.sin_family = AF_INET;
132 addr.sin_port = htons(port);
133 addr.sin_addr.s_addr = htonl(INADDR_ANY);
135 if (bind(sock, (
struct sockaddr*)&addr,
sizeof(addr)) == -1) {
136 log_perror(
"Failed to bind to port %d", port);
150 log_error(
"Could not start listening socket due to failed bind");
154 int rtn = listen(sock,
BACKLOG);
156 log_perror(
"Error starting listening socket");
159 log(LOG_COMMUNICATION,
"Started listening socket on fd %d", sock);
int init_connected_socket(struct sockaddr_in *addr)
Initializes a socket that is connected to a given address.
Interface for general-purpose socket communication.
int read_payload(int fd, size_t size, void *buff)
Reads a buffer of a given size from a file descriptor.
#define log_perror(fmt,...)
Logging of status messages to the terminal.
#define log_error(fmt,...)
#define MAX_READ_ATTEMPTS
The maximum number of times that a call to read() can be attempted for a single buffer before giving ...
#define BACKLOG
The backlog size for listening sockets.
int init_listening_socket(int port)
Initializes a socket which is bound to and listening on the given port.
#define log(level, fmt,...)
Log at a custom level.
ssize_t send_to_endpoint(int fd, void *data, size_t data_len)
Writes a buffer of a given size to a file descriptor.
int init_bound_socket(int port)
Initializes a socket which is bound to a given port (and any local IP address).
#define log_warn(fmt,...)