Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/acccre-server.c
diff options
context:
space:
mode:
Diffstat (limited to 'acccre-server.c')
-rw-r--r--acccre-server.c132
1 files changed, 132 insertions, 0 deletions
diff --git a/acccre-server.c b/acccre-server.c
new file mode 100644
index 0000000..310317a
--- /dev/null
+++ b/acccre-server.c
@@ -0,0 +1,132 @@
+#ifdef __unix__
+#include "acccre-server.h"
+
+#include <memory.h>
+#include <netdb.h>
+/*#ifdef INET*/
+ #include <netinet/in.h>
+/*#endif*/
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+/*#ifdef UNIX*/
+ #include <sys/un.h>
+/*#endif*/
+#include <unistd.h>
+
+volatile sig_atomic_t sig_flag = 0;
+
+/* TODO: call write() once; check for its errors */
+static void sig_handler(int signo) {
+ char *sig_name = strsignal(signo);
+ char *sig_msg = " signal is caught\n";
+ write(STDOUT_FILENO, sig_name, strlen(sig_name));
+ write(STDOUT_FILENO, sig_msg, strlen(sig_msg));
+ sig_flag = 1;
+}
+
+void *process(void *arg) {
+ int client_fd = *((int *)arg);
+ char *buf = malloc(sizeof(acccre_size_t));
+
+ /* TODO: make a protocol */
+ while (sig_flag == 0) {
+ if (recv(client_fd, buf, sizeof(acccre_size_t), 0) > 0) {
+ /* Now, parse the request... */
+ acccre_size_t msg_len = *buf;
+ printf("size of message is %d\n", (acccre_size_t)msg_len);
+
+ buf = realloc(buf, msg_len);
+ recv(client_fd, buf, msg_len, 0);
+ char *msg = buf;
+ printf("%s\n", msg);
+
+ /* ...
+ char *response = (char *)malloc(BUFFER_SIZE * sizeof(char));
+ snprintf(response, BUFFER_SIZE,
+ "");
+ size_t response_len;*/
+ /* ... */
+ }
+ }
+
+ close(client_fd);
+ free(arg);
+ free(buf);
+
+ return 0;
+}
+
+int main (int argc, char *argv[]) {
+ struct sigaction sa = {
+ sig_handler, /* sa_handler */
+ 0, /* sa_mask */
+ 0 /* sa_flags */
+ };
+ for (int i = 0; sigs[i] != SIGXFSZ; i++) {
+ if (sigaction(sigs[i], &sa, NULL) != 0) {
+ perror("sigaction()");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ int server_fd;
+ if ((server_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+ perror("socket()");
+ exit(EXIT_FAILURE);
+ }
+
+ struct sockaddr_un sun = {
+ AF_UNIX, /* sun_family */
+ DEFAULT_PATH /* sun_path */
+ };
+ if (argc > 1) {
+ /* Taken from https://pubs.opengroup.org/onlinepubs/9799919799/ */
+ if (sizeof(argv[1]) >= sizeof(((struct sockaddr_un *)0)->sun_path)) {
+ strcpy(sun.sun_path, argv[1]);
+ }
+ else {
+ fprintf(stderr, "Path to the socket is longer than %zu.\n", sizeof(((struct sockaddr_un *)0)->sun_path));
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ if (bind(server_fd, (struct sockaddr *)&sun, sizeof(sun)) != 0) {
+ perror("bind()");
+ exit(EXIT_FAILURE);
+ }
+
+ if (listen(server_fd, 5) != 0) {
+ perror("listen()");
+ exit(EXIT_FAILURE);
+ }
+
+ puts("Ready!");
+
+ while (sig_flag == 0) {
+ struct sockaddr_un client_addr;
+ socklen_t client_addr_len = sizeof(client_addr);
+
+ int *client_fd = malloc(sizeof(int));
+
+ *client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len);
+ if (*client_fd == -1) {
+ perror("accept()");
+ free(client_fd);
+ continue;
+ }
+
+ pthread_t thread_id; /* TODO */
+ pthread_create(&thread_id, NULL, process, (void *)client_fd);
+ pthread_detach(thread_id);
+ }
+
+ close(server_fd);
+ unlink(sun.sun_path);
+ return 0;
+}
+#endif