waffle-mole/main.c

165 lines
4.1 KiB
C

/*
* Copyright (C) 2022 Camden Dixie O'Brien
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#define SBUF_SIZE 1024
static bool exit_requested = false;
void handle_exit_signal(int signum)
{
(void)signum;
exit_requested = true;
}
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
int res = EXIT_SUCCESS;
/*
* Register signal handler for SIGTERM and SIGINT.
*
* The behaviour of signal() is not entirely consistent across
* different implementations, so this should ideally be rewritten
* to use signaction() instead at some point.
*/
if (signal(SIGTERM, handle_exit_signal) == SIG_ERR) {
fprintf(stderr, "Failed to register SIGTERM signal handler\n");
return EXIT_FAILURE;
}
if (signal(SIGINT, handle_exit_signal) == SIG_ERR) {
fprintf(stderr, "Failed to register SIGINT signal handler\n");
return EXIT_FAILURE;
}
/*
* Initialise socket.
*
* Currently only supporting IPv6, and hard-coding address and
* port. Should eventully get address and port from arguments, and
* support IPv4 as well.
*/
int sfd = socket(AF_INET6, SOCK_STREAM, 0);
if (sfd == -1) {
fprintf(stderr, "Failed to open socket\n");
return EXIT_FAILURE;
}
const struct sockaddr_in6 haddr = {
.sin6_family = AF_INET6,
.sin6_port = htons(7070),
.sin6_addr = IN6ADDR_LOOPBACK_INIT,
};
if (bind(sfd, (const struct sockaddr *)&haddr, sizeof(haddr)) == -1) {
fprintf(stderr, "Error binding socket to address\n");
res = EXIT_FAILURE;
goto close_server_socket;
}
if (listen(sfd, 8) == -1) {
fprintf(stderr, "Error attempting to listen on socket\n");
res = EXIT_FAILURE;
goto close_server_socket;
}
struct sockaddr_in6 paddr;
socklen_t paddr_size = sizeof(paddr);
int cfd;
char sbuf[SBUF_SIZE];
ssize_t n, slen;
while (!exit_requested) {
/*
* Accept incoming connection.
*
* If this is interrupted we go to the next loop iteration
* rather than retrying directly in case the received signal
* was requesting that the server exits.
*
* System calls after this point should be retried before
* checking if we need to exit, as we don't want to leave the
* client hanging.
*/
cfd = accept(sfd, (struct sockaddr *)&paddr, &paddr_size);
if (cfd == -1) {
if (errno != EINTR)
fprintf(stderr, "Error accepting connection\n");
continue;
}
/*
* Read selector from client socket.
*
* For now, assuming that the selector is less than RDBUF
* bytes long.
*/
do {
errno = 0;
n = read(cfd, sbuf, SBUF_SIZE);
} while (errno == EINTR);
if (n == -1) {
fprintf(stderr, "Error reading selector from client\n");
goto close_client_socket;
}
/*
* Locate the end of the selector.
*
* The end of the selector string is indicated with CRLF. Most
* of the time this will be at the end of the received data,
* so might as well start from there.
*/
slen = n;
for (unsigned i = n - 2; i < n; --i) {
if (sbuf[i] == 0x0d && sbuf[i + 1] == 0x0a)
slen = i;
}
if (slen == n) {
fprintf(stderr, "Received invalid selector (no CRLF)\n");
goto close_client_socket;
}
/*
* Write an empty response to the client.
*/
do {
errno = 0;
n = write(cfd, ".\r\n", 3);
} while (errno == EINTR);
if (n == -1) {
fprintf(stderr, "Error sending response to client\n");
goto close_client_socket;
}
close_client_socket:
close(cfd);
}
close_server_socket:
close(sfd);
return res;
}