aboutsummaryrefslogtreecommitdiff
path: root/server.c
blob: e702a7a6a25656fcb12fc8e65eb44a9eec72dfff (plain) (blame)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* The server recieves connections and passes files to the clients
 */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/wait.h>

#include <string.h>
#include <util.h>

struct se_vhost {
	char *username;
	char *path_root;
	char *path_error;
};

char* constructFilePath(const char* root, const char* file) {
	if (root == NULL || file == NULL) return NULL;

	int rootLen = strlen(root), fileLen = strlen(file);

	int rootEndOnSlash = root[rootLen - 1] == '/' || file[0] == '/';
	int fileEndOnSlash = file[fileLen - 1] == '/';

	int pathLen = rootLen + !rootEndOnSlash + fileLen + (fileEndOnSlash * 8) + 1;
	char* path = malloc(pathLen * sizeof(char));
	memset(path, 0, pathLen);
	strncpy(path, root, rootLen);
	if (!rootEndOnSlash) strcat(path, "/");
	strcat(path, file);
	if (fileEndOnSlash)  strcat(path, "index.md");

	return path;
}

void on_connection(const int fd_client, const struct se_vhost *vhosts, const int vhostsc) {
	printf("[%d] Connected successfully!\n", fd_client);

	char address[256];
	read(fd_client, address, 256);
	printf("[%d] Requested %s\n", fd_client, address);

	int usernameLen = strchr(address, '@') - address;

	const struct se_vhost *vhost = NULL;
	for (int i = 0; i < vhostsc; i++) {
		if (strncmp(vhosts[i].username, address, usernameLen) == 0) {
			vhost = vhosts + i;
			break;
		}
	}

	if (vhost == NULL) {
		fprintf(stderr, "[%d] Unknown username in address %s\n", fd_client, address);
		return;
	}

	char* filePath = constructFilePath(vhost->path_root, address + usernameLen + 1);

	int fd = open(filePath, O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "[%d] Error opening %s\n", fd_client, filePath);

		free(filePath);
		filePath = constructFilePath(vhost->path_root, vhost->path_error);
		fd = open(filePath, O_RDONLY);
		if (fd < 0) {
			fprintf(stderr, "[%d] Error opening %s\n", fd_client, filePath);
			free(filePath);
			return;
		}
	}

	printf("[%d] Serving %s\n", fd_client, filePath);
	free(filePath);

	char buff[256];
	memset(buff, 0, sizeof(buff));
	while (read(fd, buff, 256)) {
		write(fd_client, buff, strlen(buff));
		memset(buff, 0, sizeof(buff));
	}
	close(fd);

	printf("[%d] Served!\n", fd_client);
}

int main(int argc, char* argv[]) {
	/*
	 * Create socket for accepting connections
	 */
	int fd_socket;
	herr(fd_socket = socket(AF_INET, SOCK_STREAM, 0), "socket");

	struct sockaddr_in sa_socket = {
		.sin_family = AF_INET,
		.sin_port = inet_atop("8080"),
	};
	herr(inet_aton("127.0.0.1", &sa_socket.sin_addr.s_addr), "inet_aton");

	// Reuse address when in TIME_WAIT state, after listening socket was closed
	// https://stackoverflow.com/a/10651048/12036073
	// https://superuser.com/questions/173535/what-are-close-wait-and-time-wait-states#comment951880_173543
	int true = 1;
	herr(setsockopt(fd_socket, SOL_SOCKET, SO_REUSEADDR, &true, sizeof(int)), "setsockopt");

	herr(bind(fd_socket, (struct sockaddr*)&sa_socket, sizeof(struct sockaddr_in)), "bind");

	herr(listen(fd_socket, 50), "listen");

	/*
	 * Accept connection on the socket
	 */

	struct se_vhost vhosts[1] = {
	{
		.username = "hello",
		.path_root = ".",
		.path_error = NULL,
	} };

	struct sockaddr_in sa_client;
	socklen_t sa_client_size = sizeof(struct sockaddr_in);
	int fd_client;
	int count = 0;
	while (count < 2) {
		herr(fd_client = accept(fd_socket, (struct sockaddr*)&sa_client, &sa_client_size), "accept");
		count++;
		int fp = fork();

		if (fp == 0) {
			close(fd_socket);
			on_connection(fd_client, vhosts, 1);
			close(fd_client);
			return 0;
		}
		close(fd_client);
	}
	close(fd_socket);

	printf("Exiting");
	while(wait(NULL) > 0);
}