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

summaryrefslogtreecommitdiff
path: root/acccre-server.c
blob: 310317a1b72807a9cc07c2d090472babd1a6cea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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