aboutsummaryrefslogtreecommitdiff
path: root/btasktd.c
blob: 82092b2705d9f9ee2ef420f1b5ddce76e71ad029 (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
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
/*-
 * Copyright (c) 2026, Mr. Goblin
 *
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include "cliflags.h"
#include "codes.h"

#define BTASKTD_HELP_STR \
	"Copyright (c) 2026, Mr. Goblin\n" \
	"btasktd - task tracking TCP-server\n" \
	"Usage: btasktd [-Vh] [-p port] [-u users]\n" \
	"For more info read the program's man page\n"

#define BTASKTD_VERSION_STR \
	"0.0.0"

#ifndef BTASKTD_QUEUE_LEN
#	define BTASKTD_QUEUE_LEN 16
#endif

#ifndef BTASKTD_CLIENT_BUF_SIZE
#	define BTASKTD_CLIENT_BUF_SIZE 4096
#endif

#ifndef BTASKTD_MAX_RESPONSE_BUF_SIZE
#	define BTASKTD_MAX_RESPONSE_BUF_SIZE 8192
#endif

#ifndef BTASKTD_RESPONSE_BUF_SIZE
#	define BTASKTD_RESPONSE_BUF_SIZE \
		((BTASKTD_MAX_RESPONSE_BUF_SIZE) - 256)
#endif


struct user {
	int fd;
	char buf[BTASKTD_CLIENT_BUF_SIZE];
	int bi; /* buf index */
};

/* BODY SHOULD BE EMPTY OR END WITH CRLF (\r\n, 13 10) */
struct response {
	int code;
	const char *body; /* for empty use NULL or "" */
};

struct btasktd {
	int fd;
	int users_cnt;
	struct user *users;
};

static void
fprint_help(FILE *f)
{
	fprintf(f, "%s", BTASKTD_HELP_STR);
}

static void
set_nonblock(int fd)
{
	int fflags = fcntl(fd, F_GETFL);
	fcntl(fd, F_SETFL, fflags|O_NONBLOCK);
}

static int
find_free_user(const struct user *arr, int count)
{
	int i;
	for (i = 0; i < count; i++) {
		if (-1 == arr[i].fd)
			return i;
	}
	return -1;
}

static void
reset_user_buf(struct user *pu)
{
	pu->bi = 0;
	memset(pu->buf, '\0', sizeof(pu->buf));
}

static void
reset_user(struct user *pu)
{
	pu->fd = -1;
	reset_user_buf(pu);
}

static void
send_to_client(int fd, const char *s)
{
	int len = strlen(s);
	int code = send(fd, s, len, 0);
	if (-1 == code)
		perror("send");
}

static void
send_response(int fd, struct response r)
{
	char buf[BTASKTD_MAX_RESPONSE_BUF_SIZE] = {0};
	if (NULL == r.body)
		r.body = "";
	snprintf(buf, sizeof(buf), "%s%s.\r\n", code_to_str(r.code),
		r.body);
	send_to_client(fd, buf);
}

static void
send_response_code(int fd, int code)
{
	struct response r = { CODE_200_OK, NULL };
	r.code = code;
	send_response(fd, r);
}

static void
handle_accept(struct btasktd *this)
{
	int idx;
	struct sockaddr_in user_addr = {0};
	socklen_t addrlen = sizeof(user_addr);
	int fd = accept(this->fd, (struct sockaddr*)&user_addr, &addrlen);
	if (-1 == fd) {
		perror("accept");
		return;
	}
	set_nonblock(fd);
	idx = find_free_user(this->users, this->users_cnt);
	if (-1 == idx) {
		send_response_code(fd, CODE_503_SERVICE_UNAVAILABLE);
		close(fd);
	}
	this->users[idx].fd = fd;
	this->users[idx].bi = 0;
}

static void
handle_timeout(struct btasktd *this)
{
}

static void
handle_buf(struct btasktd *this, int id, int should_send_response)
{
	struct response resp;
	struct user *pu = &(this->users[id]);
	char buf[BTASKTD_RESPONSE_BUF_SIZE] = {0};
	/* dummy yet */
	resp.code = CODE_200_OK;
	resp.body = "";
	if (should_send_response) {
		snprintf(buf, sizeof(buf), "%s", pu->buf);
#if 1
		send_to_client(pu->fd, buf);
#else
		resp.body = buf;
		send_response(pu->fd, resp);
#endif
	}
	reset_user_buf(pu);
}

static void
handle_user(struct btasktd *this, int id)
{
	int valread;
	struct user *pu = &(this->users[id]);
	unsigned sz = sizeof(pu->buf) - (pu->bi) - 1;
	valread = recv(pu->fd, pu->buf + pu->bi, sz, 0);
	if (0 == valread) {
		if (pu->bi > 0) {
			handle_buf(this, id, 0);
		}
		close(pu->fd);
		reset_user(pu);
		return;
	} else if (-1 == valread) {
		perror("recv");
		close(pu->fd);
		reset_user(pu);
		return;
	}
	handle_buf(this, id, 1);
}

static void
bye_all_clients(struct btasktd *this)
{
	int i = 0;
	for (i = 0; i < this->users_cnt; i++) {
		if (-1 == this->users[i].fd)
			continue;
		send_response_code(this->users[i].fd,
			CODE_503_SERVICE_UNAVAILABLE);
		close(this->users[i].fd);
		this->users[i].fd = -1;
	}
}

static volatile int last_sig = 0;

static void
handle_sigint(int sig)
{
	signal(sig, handle_sigint);
	fprintf(stderr, "Caught signal %d, shutting down...\n", sig);
	last_sig = SIGINT;
}

int
main(int argc, char **argv)
{
	struct btasktd server;
	int server_fd, opt = 1, res, port, passed_flags = 1, users_cnt;
	int max_fd;
	struct user *users = NULL;
	int i;
	struct sockaddr_in addr = {0};
	struct int_flag_val int_res;
	struct bool_flag_val bool_res;
	fd_set readfds;

	bool_res = extract_bool_flag('V', argc, argv);
	if (fvr_ok == bool_res.code && bool_res.data) {
		printf("btasktd version %s\n", BTASKTD_VERSION_STR);
		return 0;
	}

	bool_res = extract_bool_flag('h', argc, argv);
	if (fvr_ok == bool_res.code && bool_res.data) {
		fprint_help(stdout);
		return 0;
	}

	int_res = extract_int_flag('p', argc, argv);
	switch (int_res.code) {
	case fvr_not_found:
#		ifdef BTASKTD_DEFAULT_PORT
			port = BTASKTD_DEFAULT_PORT;
#		else
			fprintf(stderr, "No port specified!\n");
			passed_flags = 0;
#		endif
		break;
	case fvr_ok:
		port = int_res.data;
		if (port > 0)
			break;
		/* fallthrough */
	case fvr_invalid:
		fprintf(stderr, "Invalid port!\n");
		return 1;
		break;
	}

	int_res = extract_int_flag('u', argc, argv);
	switch (int_res.code) {
	case fvr_not_found:
#		ifdef BTASKTD_DEFAULT_USERS_CNT
			users_cnt = BTASKTD_DEFAULT_USERS_CNT;
#		else
			fprintf(stderr, "No users count specified!\n");
			passed_flags = 0;
#		endif
		break;
	case fvr_ok:
		users_cnt = int_res.data;
		if (users_cnt > 0)
			break;
		/* fallthrough */
	case fvr_invalid:
		fprintf(stderr, "Invalid users count!\n");
		return 1;
		break;
	}

	if (!passed_flags) {
		fprintf(stderr, "-\n");
		fprint_help(stderr);
		return 1;
	}

	users = (struct user*)calloc(users_cnt, sizeof(struct user));
	if (NULL == users) {
		fprintf(stderr, "No memory!\n");
		return 1;
	}
	for (i = 0; i < users_cnt; i++) {
		users[i].fd = -1;
	}

	server_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (server_fd < 0) {
		free(users);
		perror("socket");
		return 1;
	}
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.s_addr = htonl(INADDR_ANY);
	res = setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR,
		&opt, sizeof(opt));
	if (0 != res) {
		close(server_fd);
		free(users);
		perror("setsockopt");
		return 1;
	}
	res = bind(server_fd, (struct sockaddr*)&addr, sizeof(addr));
	if (0 != res) {
		close(server_fd);
		free(users);
		perror("bind");
		return 1;
	}
	res = listen(server_fd, BTASKTD_QUEUE_LEN);
	if (0 != res) {
		close(server_fd);
		free(users);
		perror("listen");
		return 1;
	}

	set_nonblock(server_fd);

	server.fd = server_fd;
	server.users_cnt = users_cnt;
	server.users = users;
	printf("btaskt server btasktd started on port %d\n", port);

	signal(SIGINT, handle_sigint);

	for (;;) {
		FD_ZERO(&readfds);
		FD_SET(server_fd, &readfds);
		max_fd = server_fd;
		for (i = 0; i < users_cnt; i++) {
			if (-1 == users[i].fd)
				continue;
			FD_SET(users[i].fd, &readfds);
			if (users[i].fd > max_fd)
				max_fd = users[i].fd;
		}
		res = select(max_fd+1, &readfds, NULL, NULL, NULL);
		if (-1 == res) {
			if (EINTR == errno) {
				/* signal handler called */
				if (SIGINT == last_sig) {
					bye_all_clients(&server);
					break;
				}
			} else {
				perror("select");
			}
			continue;
		} else if (0 == res) {
			handle_timeout(&server);
			continue;
		}
		if (FD_ISSET(server_fd, &readfds))
			handle_accept(&server);
		for (i = 0; i < users_cnt; i++) {
			if (-1 == users[i].fd)
				continue;
			if (FD_ISSET(users[i].fd, &readfds))
				handle_user(&server, i);
		}
	}

	printf("btaskt server btasktd stopped\n");
	close(server_fd);
	free(users);
	return 0;
}