about summary refs log tree commit diff homepage
path: root/server.c
blob: ee19ec81b9ab441d8a63ea19f9bbbf3631507c17 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/* Copyright (C) 2017, 2021  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sysexits.h>
#include <time.h>
#include <unistd.h>

#ifdef __FreeBSD__
#include <sys/capsicum.h>
#endif

#include "torus.h"

static struct Tile *tiles;

static void tilesMap(const char *path) {
	int fd = open(path, O_CREAT | O_RDWR, 0644);
	if (fd < 0) err(EX_CANTCREAT, "%s", path);

	int error = ftruncate(fd, TilesSize);
	if (error) err(EX_IOERR, "%s", path);

	tiles = mmap(NULL, TilesSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (tiles == MAP_FAILED) err(EX_OSERR, "mmap");
	close(fd);

#ifdef MADV_NOCORE
	error = madvise(tiles, TilesSize, MADV_NOCORE);
	if (error) err(EX_OSERR, "madvise");
#endif
}

static struct Tile *tileGet(uint32_t tileX, uint32_t tileY) {
	struct Tile *tile = &tiles[tileY * TileRows + tileX];
	if (!tile->createTime) {
		memset(tile->cells, ' ', CellsSize);
		memset(tile->colors, ColorWhite, CellsSize);
		tile->createTime = time(NULL);
	}
	return tile;
}

static const struct Tile *tileAccess(uint32_t tileX, uint32_t tileY) {
	struct Tile *tile = tileGet(tileX, tileY);
	tile->accessTime = time(NULL);
	tile->accessCount++;
	return tile;
}

static struct Tile *tileModify(uint32_t tileX, uint32_t tileY) {
	struct Tile *tile = tileGet(tileX, tileY);
	tile->modifyTime = time(NULL);
	tile->modifyCount++;
	return tile;
}

enum { ClientsCap = 256 };
static struct Client {
	int fd;
	uint32_t tileX;
	uint32_t tileY;
	uint8_t cellX;
	uint8_t cellY;
} clients[ClientsCap];
static size_t clientsLen;

static struct Client *clientAdd(int fd) {
	if (clientsLen == ClientsCap) return NULL;
	struct Client *client = &clients[clientsLen++];
	client->fd = fd;
	client->tileX = TileInitX;
	client->tileY = TileInitY;
	client->cellX = CellInitX;
	client->cellY = CellInitY;
	return client;
}

static int
clientSend(const struct Client *client, const struct ServerMessage *msg) {
	ssize_t len = send(client->fd, msg, sizeof(*msg), 0);
	if (len < 0) return -1;
	if (msg->type == ServerTile) {
		const struct Tile *tile = tileAccess(client->tileX, client->tileY);
		len = send(client->fd, tile, sizeof(*tile), 0);
		if (len < 0) return -1;
	}
	return 0;
}

static void
clientCast(const struct Client *source, const struct ServerMessage *msg) {
	for (size_t i = 0; i < clientsLen; ++i) {
		if (&clients[i] == source) continue;
		if (clients[i].tileX != source->tileX) continue;
		if (clients[i].tileY != source->tileY) continue;
		clientSend(&clients[i], msg);
	}
}

static void clientRemove(size_t i) {
	struct Client client = clients[i];
	clients[i] = clients[--clientsLen];
	close(client.fd);
	struct ServerMessage msg = {
		.type = ServerCursor,
		.cursor = {
			.oldCellX = client.cellX, .oldCellY = client.cellY,
			.newCellX = CursorNone, .newCellY = CursorNone,
		},
	};
	clientCast(&client, &msg);
}

static int clientCursors(const struct Client *client) {
	struct ServerMessage msg = {
		.type = ServerCursor,
		.cursor.oldCellX = CursorNone,
		.cursor.oldCellY = CursorNone,
	};
	for (size_t i = 0; i < clientsLen; ++i) {
		if (&clients[i] == client) continue;
		if (clients[i].tileX != client->tileX) continue;
		if (clients[i].tileY != client->tileY) continue;
		msg.cursor.newCellX = clients[i].cellX;
		msg.cursor.newCellY = clients[i].cellY;
		if (clientSend(client, &msg) < 0) return -1;
	}
	return 0;
}

static int clientUpdate(struct Client *new, const struct Client *old) {
	struct ServerMessage msg = {
		.type = ServerMove,
		.move.cellX = new->cellX,
		.move.cellY = new->cellY,
	};
	if (clientSend(new, &msg) < 0) return -1;

	if (new->tileX != old->tileX || new->tileY != old->tileY) {
		msg.type = ServerTile;
		if (clientSend(new, &msg) < 0) return -1;
		if (clientCursors(new) < 0) return -1;

		msg = (struct ServerMessage) {
			.type = ServerCursor,
			.cursor = {
				.oldCellX = old->cellX, .oldCellY = old->cellY,
				.newCellX = CursorNone, .newCellY = CursorNone,
			},
		};
		clientCast(old, &msg);

		msg = (struct ServerMessage) {
			.type = ServerCursor,
			.cursor = {
				.oldCellX = CursorNone, .oldCellY = CursorNone,
				.newCellX = new->cellX, .newCellY = new->cellY,
			},
		};
		clientCast(new, &msg);

	} else {
		msg = (struct ServerMessage) {
			.type = ServerCursor,
			.cursor = {
				.oldCellX = old->cellX, .oldCellY = old->cellY,
				.newCellX = new->cellX, .newCellY = new->cellY,
			},
		};
		clientCast(new, &msg);
	}
	return 0;
}

static int clientMove(struct Client *client, int8_t dx, int8_t dy) {
	struct Client old = *client;

	if (dx > CellCols - client->cellX) dx = CellCols - client->cellX;
	if (dx < -client->cellX - 1)       dx = -client->cellX - 1;
	if (dy > CellRows - client->cellY) dy = CellRows - client->cellY;
	if (dy < -client->cellY - 1)       dy = -client->cellY - 1;

	client->cellX += dx;
	client->cellY += dy;

	if (client->cellX == CellCols) {
		client->tileX++;
		client->cellX = 0;
	}
	if (client->cellX == UINT8_MAX) {
		client->tileX--;
		client->cellX = CellCols - 1;
	}
	if (client->cellY == CellRows) {
		client->tileY++;
		client->cellY = 0;
	}
	if (client->cellY == UINT8_MAX) {
		client->tileY--;
		client->cellY = CellRows - 1;
	}

	if (client->tileX == TileCols)   client->tileX = 0;
	if (client->tileX == UINT32_MAX) client->tileX = TileCols - 1;
	if (client->tileY == TileRows)   client->tileY = 0;
	if (client->tileY == UINT32_MAX) client->tileY = TileRows - 1;

	assert(client->cellX < CellCols);
	assert(client->cellY < CellRows);
	assert(client->tileX < TileCols);
	assert(client->tileY < TileRows);

	return clientUpdate(client, &old);
}

static int clientFlip(struct Client *client) {
	struct Client old = *client;
	client->tileX = (client->tileX + TileCols / 2) % TileCols;
	client->tileY = (client->tileY + TileRows / 2) % TileRows;
	return clientUpdate(client, &old);
}

static int clientPut(const struct Client *client, uint8_t color, uint8_t cell) {
	struct Tile *tile = tileModify(client->tileX, client->tileY);
	tile->colors[client->cellY][client->cellX] = color;
	tile->cells[client->cellY][client->cellX] = cell;
	struct ServerMessage msg = {
		.type = ServerPut,
		.put = {
			.cellX = client->cellX,
			.cellY = client->cellY,
			.color = color,
			.cell = cell,
		},
	};
	int error = clientSend(client, &msg);
	clientCast(client, &msg);
	return error;
}

static int clientMap(const struct Client *client) {
	int32_t mapY = (int32_t)client->tileY - MapRows / 2;
	int32_t mapX = (int32_t)client->tileX - MapCols / 2;

	time_t now = time(NULL);
	struct Map map = {
		.now = now,
		.min = {
			.createTime = now,
			.modifyTime = now,
			.accessTime = now,
			.modifyCount = UINT32_MAX,
			.accessCount = UINT32_MAX,
		},
	};

	for (int32_t y = 0; y < MapRows; ++y) {
		for (int32_t x = 0; x < MapCols; ++x) {
			uint32_t tileY = ((mapY + y) % TileRows + TileRows) % TileRows;
			uint32_t tileX = ((mapX + x) % TileCols + TileCols) % TileCols;
			struct Meta meta = tileMeta(&tiles[tileY * TileRows + tileX]);

			if (meta.createTime > 1) {
				if (meta.createTime < map.min.createTime) {
					map.min.createTime = meta.createTime;
				}
				if (meta.createTime > map.max.createTime) {
					map.max.createTime = meta.createTime;
				}
			}
			if (meta.modifyTime) {
				if (meta.modifyTime < map.min.modifyTime) {
					map.min.modifyTime = meta.modifyTime;
				}
				if (meta.modifyTime > map.max.modifyTime) {
					map.max.modifyTime = meta.modifyTime;
				}
			}
			if (meta.accessTime) {
				if (meta.accessTime < map.min.accessTime) {
					map.min.accessTime = meta.accessTime;
				}
				if (meta.accessTime > map.max.accessTime) {
					map.max.accessTime = meta.accessTime;
				}
			}
			if (meta.modifyCount < map.min.modifyCount) {
				map.min.modifyCount = meta.modifyCount;
			}
			if (meta.modifyCount > map.max.modifyCount) {
				map.max.modifyCount = meta.modifyCount;
			}
			if (meta.accessCount < map.min.accessCount) {
				map.min.accessCount = meta.accessCount;
			}
			if (meta.accessCount > map.max.accessCount) {
				map.max.accessCount = meta.accessCount;
			}

			map.meta[y][x] = meta;
		}
	}

	struct ServerMessage msg = { .type = ServerMap };
	if (clientSend(client, &msg) < 0) return -1;
	if (send(client->fd, &map, sizeof(map), 0) < 0) return -1;
	return 0;
}

static int clientTele(struct Client *client, uint8_t port) {
	if (port >= ARRAY_LEN(Ports)) return -1;
	struct Client old = *client;
	client->tileX = Ports[port].tileX;
	client->tileY = Ports[port].tileY;
	client->cellX = CellInitX;
	client->cellY = CellInitY;
	return clientUpdate(client, &old);
}

int main(int argc, char *argv[]) {
	int error;

	const char *dataPath = DefaultDataPath;
	const char *sockPath = DefaultSockPath;
	const char *pidPath = NULL;

	for (int opt; 0 < (opt = getopt(argc, argv, "d:p:s:"));) {
		switch (opt) {
			break; case 'd': dataPath = optarg;
			break; case 'p': pidPath = optarg;
			break; case 's': sockPath = optarg;
			break; default:  return EX_USAGE;
		}
	}

	int pidFile = -1;
	if (pidPath) {
		pidFile = open(pidPath, O_WRONLY | O_CREAT | O_CLOEXEC, 0600);
		if (pidFile < 0) err(EX_CANTCREAT, "%s", pidPath);

		error = flock(pidFile, LOCK_EX | LOCK_NB);
		if (error && errno != EWOULDBLOCK) err(EX_IOERR, "%s", pidPath);
		if (error) errx(EX_CANTCREAT, "%s: file is locked", pidPath);

		error = ftruncate(pidFile, 0);
		if (error) err(EX_IOERR, "%s", pidPath);
	}

	tilesMap(dataPath);

	int server = socket(PF_UNIX, SOCK_STREAM, 0);
	if (server < 0) err(EX_OSERR, "socket");

	error = unlink(sockPath);
	if (error && errno != ENOENT) err(EX_CANTCREAT, "%s", sockPath);

	struct sockaddr_un addr = { .sun_family = AF_UNIX };
	snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", sockPath);
	error = bind(server, (struct sockaddr *)&addr, SUN_LEN(&addr));
	if (error) err(EX_CANTCREAT, "%s", sockPath);

	if (pidPath) {
		error = daemon(0, 0);
		if (error) err(EX_OSERR, "daemon");
		dprintf(pidFile, "%ju", (uintmax_t)getpid());
	}

#ifdef __OpenBSD__
	error = pledge("stdio unix", NULL);
	if (error) err(EX_OSERR, "pledge");
#endif

#ifdef __FreeBSD__
	error = cap_enter();
	if (error) err(EX_OSERR, "cap_enter");

	cap_rights_t rights;
	cap_rights_init(
		&rights,
		CAP_LISTEN, CAP_ACCEPT, CAP_EVENT,
		CAP_READ, CAP_WRITE, CAP_SETSOCKOPT
	);
	error = cap_rights_limit(server, &rights);
	if (error) err(EX_OSERR, "cap_rights_limit");
#endif

	error = listen(server, -1);
	if (error) err(EX_OSERR, "listen");

	signal(SIGPIPE, SIG_IGN);
	struct pollfd fds[1 + ClientsCap] = {
		{ .fd = server, .events = POLLIN },
	};
	for (;;) {
		for (size_t i = 0; i < clientsLen; ++i) {
			fds[1 + i].fd = clients[i].fd;
			fds[1 + i].events = POLLIN;
		}
		int nfds = poll(fds, 1 + clientsLen, -1);
		if (nfds < 0 && errno != EINTR) err(EX_IOERR, "poll");
		if (nfds < 0) continue;

		for (size_t i = clientsLen - 1; i < clientsLen; --i) {
			if (!fds[1 + i].revents) continue;
			if (fds[1 + i].revents & (POLLHUP | POLLERR)) {
				clientRemove(i);
				continue;
			}

			struct ClientMessage msg;
			ssize_t len = recv(clients[i].fd, &msg, sizeof(msg), 0);
			if (len != sizeof(msg)) {
				clientRemove(i);
				continue;
			}

			switch (msg.type) {
				break; case ClientMove: {
					error = clientMove(&clients[i], msg.move.dx, msg.move.dy);
				}
				break; case ClientFlip: {
					error = clientFlip(&clients[i]);
				}
				break; case ClientPut: {
					error = clientPut(&clients[i], msg.put.color, msg.put.cell);
				}
				break; case ClientMap: {
					error = clientMap(&clients[i]);
				}
				break; case ClientTele: {
					error = clientTele(&clients[i], msg.port);
				}
				break; default: error = -1;
			}
			if (error) clientRemove(i);
		}

		if (fds[0].revents) {
			int fd = accept(server, NULL, NULL);
			if (fd < 0) err(EX_IOERR, "accept");
			fcntl(fd, F_SETFL, O_NONBLOCK);

			int size = 2 * sizeof(struct Tile);
			error = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
			if (error) err(EX_IOERR, "setsockopt");

			struct Client *client = clientAdd(fd);
			if (!client) {
				close(fd);
				continue;
			}

			struct ServerMessage msg = { .type = ServerTile };
			error = 0
				|| clientSend(client, &msg)
				|| clientMove(client, 0, 0)
				|| clientCursors(client);
			if (error) clientRemove(clientsLen - 1);
		}
	}
}