-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
59 lines (48 loc) · 1.24 KB
/
main.c
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
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "cache.h"
#include "proxy-utils.h"
#include "sockets-handler.h"
static void interrupt_handler(int signal) {
sockets_destroy();
cache_free();
printf("Server closed.\n");
exit(0);
}
int main(int argc, char* argv[]) {
struct sockaddr_in addr;
int server_socket;
int result;
int port;
if (argc != 2) {
fprintf(stderr, "Usage: %s <listen-port>\n", argv[0]);
return -1;
}
port = atoi(argv[1]);
fprintf(stderr, "Binding server socket listener to %d...\n", port);
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Cannot create socket");
return -1;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(server_socket, (struct sockaddr*)&addr, sizeof(addr))) {
perror("Cannot bind server socket");
return -1;
}
fprintf(stderr, "Server socket bound.\n");
result = cache_init();
if (result) {
proxy_error(result, "Cannot init cache");
return -1;
}
signal(SIGPIPE, SIG_IGN);
signal(SIGINT, &interrupt_handler);
return sockets_poll_loop(server_socket);
}