This repository has been archived on 2025-07-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
ossp/firewall-server/server.c
AKP ab1c764902 Do not omit argument name
This caused compilation to fail on the VM.
2023-11-12 01:25:55 +00:00

57 lines
1.2 KiB
C

#include "server_includes/sockets.c"
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#define ADDR "::"
int sd;
int run_server(const char **port_str) {
if (open_socket(ADDR, *port_str, &sd) != 0) {
return 1;
}
if (accept_loop(sd) != 0) {
return 1;
}
return 0;
}
void sigint_handler(int sig) {
fprintf(stderr, "Gracefully shutting down...\n");
if (shutdown(sd, 2) != 0) {
perror("warning: global shutdown connection failed");
}
if (close(sd) != 0) {
perror("warning: global close connection failed");
}
}
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s PORT\n", argv[0]);
return 1;
}
char *port_str = argv[1];
pthread_t thread_id;
pthread_create(&thread_id, NULL, (void *)&run_server, &port_str);
fprintf(stderr, "Alive on %s:%s\n", ADDR, port_str);
signal(SIGINT, &sigint_handler);
int socket_res, status;
if ((status = pthread_join(thread_id, (void *)&socket_res)) != 0) {
fprintf(stderr, "join worker thread: %s\n", strerror(status));
return 1;
}
teardown_frames();
return socket_res;
}