spg_auth.c 11.9 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
/*
	Command authoriser

4
5
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2008, 2009, 2013
	Copyright Stichting C.A. Muller Radioastronomiestation, 2008, 2009, 2013
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU 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 General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <signal.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <inttypes.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "setpoint.h"
#include "tcp_listen.h"
#include "block_setpoint_generator.h"
42
43
#include "status_server.h"
#include "command_server.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
44
45

#include "dt_port_numbers.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
46
#include "dt_host.h"
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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


struct spg_client {
	struct spg_client *next;
	int fd;
	char cmd[12];
	size_t cmdlen;
	int pending;
	char buffer[100];
	int buflen;
	int state;
	struct spg *spg;
};

#define CLIENT_ST_SPG 0
#define CLIENT_ST_ID 1
#define CLIENT_ST_CMD 3

struct spg {
	struct setpoint_command *spg_cmd;
	char *name;
	char *unit;
	struct spg_client *clients;
};

struct spg spgs[] = {
	{ NULL, "Azimuth_Setpoint",           "rad", NULL },
	{ NULL, "Elevation_Setpoint",         "rad", NULL },
	{ NULL, "Elevation_Torsion_Setpoint", "rad", NULL },
	{ NULL, NULL, NULL, NULL },
};

struct spg_client *newclients = NULL;

typedef union {
	float f;
	uint32_t u;
} float32_t;



char *command_host = "localhost";
int future_hold = 3;
char *command_id = "console/spg_auth";

char *logfile = "spg_auth.log";

float az_offset = 0.0;
float el_offset = 0.0;

char current_auth[100] = { "none" };


int fd_listen;
int fd_console;
int fd_offset;

time_t lastcmd;

static int  fd_log = -1;

void log_command(struct spg *spg, void *cmd, size_t len)
{
	time_t now;
	uint32_t nown;
	int r;
	char endtok = 0xff;
	
	now = time(NULL);
	nown = htonl(now);

	if (fd_log < 0) {
		fd_log = open(logfile, 
		    O_WRONLY | O_APPEND | O_CREAT,
		    S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
		if (fd_log < 0)
			return;
	}

	r = write(fd_log, &nown, sizeof(nown));
	if (r <= 0)
		goto err;
	r = write(fd_log, cmd, len);
	if (r <= 0)
		goto err;
	r = write(fd_log, spg->name, strlen(spg->name)+1);
	if (r <= 0)
		goto err;
	r = write(fd_log, current_auth, strlen(current_auth)+1);
	if (r <= 0)
		goto err;
	r = write(fd_log, &endtok, 1);
	if (r <= 0)
		goto err;

	return;

err:
	close(fd_log);
	fd_log = -1;
	return;
}

void new_connection(void)
{
	int fd;
	struct spg_client *new_client;
	int i;
	
	fd = tcp_accept(fd_listen);
	if (fd < 0)
		return;
	ioctl(fd, FIONBIO, &(int){ 1 });
	
	new_client = malloc(sizeof(struct spg_client));
	if (!new_client) {
		close(fd);
		return;
	}
	
	new_client->fd = fd;
	new_client->cmdlen = 0;
	new_client->pending = 0;
	new_client->buflen = 0;
	new_client->state = CLIENT_ST_SPG;
	
	for (i = 0; spgs[i].name; i++) {
		write(fd, spgs[i].name, strlen(spgs[i].name) + 1);
		write(fd, spgs[i].unit, strlen(spgs[i].unit) + 1);
	}
	write(fd, (char [1]){ 0 }, 1);
	
	new_client->next = newclients;
	newclients = new_client;

	printf("New client\n");
}


186
int handle_offsets(char *command)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
187
{
188
189
	sscanf(command, "%f %f", &az_offset, &el_offset);
	printf("New offset: %e %e\n", az_offset, el_offset);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
190

191
	return 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
}

void try_command(struct spg_client *client)
{
	uint32_t type;
	float32_t pos;
	int skip;
	
	if (strcmp(client->buffer, current_auth)) {
		client->pending = 0;
		client->cmdlen = 0;
		return;
	}

	type = ntohl(*(uint32_t *)client->cmd);
	if (type == BLOCK_SPG_SETPOINT_TIME) {
		uint32_t end_time;
	
		/* Check if setpoint-time command is not to far in the future */
		end_time = ntohl(*(uint32_t *)&client->cmd[8]);
		if  (end_time > time(NULL) + future_hold)
			return;
		
		lastcmd = end_time;
	} else {
		lastcmd = time(NULL);
	}

	skip = 0;
	if (!strcmp(client->spg->name, "Azimuth_Setpoint")) {
		switch(type) {
			case BLOCK_SPG_SETPOINT:
			case BLOCK_SPG_SETPOINT_TIME:
				pos.u = ntohl(*(uint32_t *)&client->cmd[4]);
				pos.f += az_offset;
				*(uint32_t *)&client->cmd[4] = htonl(pos.u);
		}
	}
	if (!strcmp(client->spg->name, "Elevation_Setpoint")) {
		switch(type) {
			case BLOCK_SPG_SETPOINT:
			case BLOCK_SPG_SETPOINT_TIME:
				pos.u = ntohl(*(uint32_t *)&client->cmd[4]);
				pos.f += el_offset;
				*(uint32_t *)&client->cmd[4] = htonl(pos.u);
		}
	}
	if (!skip) {
		setpoint_command_setpoint_any(client->spg->spg_cmd, client->cmd, client->cmdlen);
		log_command(client->spg, client->cmd, client->cmdlen);
	}
	client->cmdlen = 0;
	client->pending = 0;
	printf("Done command\n");
}

void handle_client(struct spg_client *client, struct spg_client **clientp)
{
	int ret, i;
	
	switch (client->state) {
		case CLIENT_ST_SPG:
			do {
				ret = read(client->fd, client->buffer + client->buflen, 1);
				if (ret == 1) {
					client->buflen++;
					if (client->buffer[client->buflen - 1] == '\n' ||
					    client->buffer[client->buflen - 1] == '\r' ||
					    client->buffer[client->buflen - 1] == 0 ) {
						client->buffer[client->buflen - 1] = 0;
						if (client->buflen > 1) {
							printf("Spg client for: %s\n",
							    client->buffer);
							for (i = 0; spgs[i].name; i++) {
								if (!strcmp(spgs[i].name, client->buffer)) {
									*clientp = client->next;
									client->next = spgs[i].clients;
									spgs[i].clients = client;
									client->spg = &spgs[i];
									client->state = CLIENT_ST_ID;
									client->buflen = 0;
									return;
								}
							}
							printf("Not found...\n");
							close(client->fd);
							client->fd = -1;
							return;
						}
					}
					if (client->buflen >= 100) {
						close(client->fd);
						printf("spg name to long\n");
						client->fd = -1;
						return;
					}
				} else {
					if (ret == 0 || errno != EAGAIN) {
						close(client->fd);
						printf("unknown spg client dropped connection\n");
						client->fd = -1;
					}
				}
			} while (ret == 1);
			break;
		case CLIENT_ST_ID:
			do {
				ret = read(client->fd, client->buffer + client->buflen, 1);
				if (ret == 1) {
					client->buflen++;
					if (client->buffer[client->buflen - 1] == '\n' ||
					    client->buffer[client->buflen - 1] == '\r' ||
					    client->buffer[client->buflen - 1] == 0 ) {
						client->buffer[client->buflen - 1] = 0;
						if (client->buflen > 1) {
							printf("Spg client id: %s\n",
							    client->buffer);
							client->state = CLIENT_ST_CMD;
							return;
						}
					}
					if (client->buflen >= 100) {
						close(client->fd);
						printf("id to long\n");
						client->fd = -1;
						return;
					}
				} else {
					if (ret == 0 || errno != EAGAIN) {
						close(client->fd);
						client->fd = -1;
						printf("Connection dropped before id known\n");
					}
				}
			} while (ret == 1);
			break;
		case CLIENT_ST_CMD:
			if (client->pending) {
				try_command(client);
				return;
			}

			do {
				ret = read(client->fd, client->cmd + client->cmdlen, 1);
				if (ret == 1) {
					client->cmdlen++;
					
					if (client->cmdlen == 8 &&
					    ntohl(*(uint32_t*)client->cmd) == BLOCK_SPG_SPEED) {
						client->pending = 1;
						try_command(client);
						return;
					}
					if (client->cmdlen == 8 &&
					    ntohl(*(uint32_t*)client->cmd) == BLOCK_SPG_SETPOINT) {
						client->pending = 1;
						try_command(client);
						return;
					}
					if (client->cmdlen == 12 &&
					    ntohl(*(uint32_t*)client->cmd) == BLOCK_SPG_SETPOINT_TIME) {
						client->pending = 1;
						try_command(client);
						return;
					}
					
					if (client->cmdlen >= 12) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
359
						printf("cmdlen %zd is to long\n", client->cmdlen);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
						close(client->fd);
						client->fd = -1;
						return;
					}
				} else {
					if (ret == 0 || errno != EAGAIN) {
						close(client->fd);
						client->fd = -1;
						printf("Command connection lost\n");
					}
				}
			} while (ret == 1);

			break;
	}
}


378
int handle_con_client(char *command)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
379
{
380
381
	if (strcmp(current_auth, command)) {
		int i;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
382
						
383
384
385
386
		strcpy(current_auth, command);
		while (current_auth[strlen(current_auth)-1] == '\n' ||
		    current_auth[strlen(current_auth)-1] == '\r')
			current_auth[strlen(current_auth)-1] = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
387

388
389
		for (i = 0; spgs[i].name; i++) {
			setpoint_command_speed(spgs[i].spg_cmd, 0.0);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
390
391
		}

392
393
		printf("New authorised user: %s\n",
		    current_auth);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
394
	}
395
	return 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
396
397
}

398
void update_status(struct status_server *stat_srv)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
{
	static time_t last = 0;
	time_t now;
	char status[100];
	
	now = time(NULL);
	if (now == last)
		return;
	
	last = now;
	
	snprintf(status, 99, "%s %e %e\n", current_auth, az_offset, el_offset);
	status[99] = 0;
	
413
	status_server_send(stat_srv, status);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
414
415
416
417
418
}

int main (int argc, char **argv)
{
	int i;
419
420
421
	struct status_server *stat_srv;
	struct command_server *offset_srv;
	struct command_server *console_srv;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
422
423
424

	signal(SIGPIPE, SIG_IGN);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
425
426
	command_host = dt_host_controller();

Jeroen Vreeken's avatar
Jeroen Vreeken committed
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
	if (argc > 1) {
		logfile = argv[2];
	}

	for (i = 0; spgs[i].name; i++) {
		spgs[i].spg_cmd = setpoint_command_init(
		    command_host, CTRL_COMMAND_PORT, spgs[i].name, command_id);
	}

	fd_listen = tcp_listen(CONSOLE_COMMAND_PORT, 0, 100);
	if (fd_listen < 0) {
		printf("Could not open listen port for commands\n");
		fd_listen = 0;
	}
	ioctl(fd_listen, FIONBIO, &(int){ 1 });

443
444
	console_srv = command_server_create(CONSOLE_ACTIVE_PORT, 0, 100);
	if (!console_srv) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
445
446
		printf("Could not open listen port for consoles\n");
	}
447
448
	stat_srv = status_server_create(CONSOLE_STATUS_PORT, 0, 100);
	if (!stat_srv) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
449
450
451
		printf("Could not open listen port for status\n");
	}
	
452
453
	offset_srv = command_server_create(CONSOLE_OFFSET_PORT, 0, 100);
	if (!offset_srv) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
454
455
		printf("Could not open listen port for offset\n");
	}
456
457
458

	command_server_handler_raw_set(offset_srv, handle_offsets);
	command_server_handler_raw_set(console_srv, handle_con_client);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
	
	while (1) {
		fd_set fd_rd;
		struct timeval timeout;
		int high;
		struct spg_client *client, **clientp;
		int i;
		
		timeout.tv_usec = 0;
		timeout.tv_sec = 1;
		
		FD_ZERO(&fd_rd);
		high = fd_listen + 1;
		FD_SET(fd_listen, &fd_rd);
473
474
475
476

		status_server_fdset_add(stat_srv, &fd_rd, &high);
		command_server_fdset_add(offset_srv, &fd_rd, &high);
		command_server_fdset_add(console_srv, &fd_rd, &high);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
		
		for (i = 0; spgs[i].name; i++) {
			for (client = spgs[i].clients; client; client = client->next) {
				if (client->pending)
					continue;
				FD_SET(client->fd, &fd_rd);
				if (client->fd >= high)
					high = client->fd +1;
			}
		}
		for (client = newclients; client; client = client->next) {
			FD_SET(client->fd, &fd_rd);
			if (client->fd >= high)
				high = client->fd + 1;
		}
492

Jeroen Vreeken's avatar
Jeroen Vreeken committed
493
494
495
496
497
		select(high, &fd_rd, NULL, NULL, &timeout);

		if (FD_ISSET(fd_listen, &fd_rd)) {
			new_connection();
		}
498
499
500
501
502

		status_server_fdset_handle(stat_srv, &fd_rd);
		command_server_fdset_handle(offset_srv, &fd_rd);
		command_server_fdset_handle(console_srv, &fd_rd);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
		for (i = 0; spgs[i].name; i++) {
			for (client = spgs[i].clients; client; client = client->next) {
				handle_client(client, NULL);
			}
			for (client = spgs[i].clients, clientp = &spgs[i].clients; client; client = *clientp) {
				if (client->fd < 0) {
					*clientp = client->next;
					free(client);
					printf("Removed spg client\n");
				} else {
					clientp = &client->next;
				}
			}
		}
		for (clientp = &newclients; *clientp; clientp = (*clientp ? &(*clientp)->next : clientp)) {
			handle_client(*clientp, clientp);
		}
		for (client = newclients, clientp = &newclients; client; client = *clientp) {
			if (client->fd < 0) {
				*clientp = client->next;
				printf("Removed unknown spg client\n");
				free(client);
			} else {
				clientp = &client->next;
			}
		}
529
530
		
		update_status(stat_srv);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
531
532
	}
}