summaryrefslogtreecommitdiffstats
path: root/tools/rtrclient.c
blob: 6282fac728930fa548e5ec7c874eebee8940b5eb (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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
/*
 * This file is part of RTRlib.
 *
 * This file is subject to the terms and conditions of the MIT license.
 * See the file LICENSE in the top level directory for more details.
 *
 * Website: http://rtrlib.realmv6.org/
 */

#include "templates.h"

#include "rtrlib/rtrlib.h"

#include "third-party/mustach.h"
#include "third-party/tommyds/tommyarray.h"
#include "third-party/tommyds/tommyhashlin.h"

#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#ifdef RTRLIB_HAVE_LIBSSH
// constants for utf8 validation
static const uint8_t UTF8_ONE_BYTE_PREFIX = 0b00000000;
static const uint8_t UTF8_ONE_BYTE_MASK = 0b10000000;
static const uint8_t UTF8_TWO_BYTE_PREFIX = 0b11000000;
static const uint8_t UTF8_TWO_BYTE_MASK = 0b11100000;
static const uint8_t UTF8_THREE_BYTE_PREFIX = 0b11100000;
static const uint8_t UTF8_THREE_BYTE_MASK = 0b11110000;
static const uint8_t UTF8_FOUR_BYTE_PREFIX = 0b11110000;
static const uint8_t UTF8_FOUR_BYTE_MASK = 0b11111000;
static const uint8_t UTF8_SUBSEQUENT_BYTE_PREFIX = 0b10000000;
static const uint8_t UTF8_SUBSEQUENT_BYTE_MASK = 0b11000000;
#endif

__attribute__((format(printf, 1, 2), noreturn)) static void print_error_exit(const char *fmt, ...);

static bool is_readable_file(const char *str);

enum socket_type {
	SOCKET_TYPE_TCP,
#ifdef RTRLIB_HAVE_LIBSSH
	SOCKET_TYPE_SSH,
#endif
};

struct socket_config {
	struct rtr_socket socket;
	struct tr_socket tr_socket;
	enum socket_type type;
	bool print_pfx_updates;
	bool print_spki_updates;
	char *bindaddr;
	char *host;
	char *port;
#ifdef RTRLIB_HAVE_LIBSSH
	char *ssh_username;
	char *ssh_private_key;
	char *ssh_host_key;
	char *ssh_password;
	bool force_password;
	bool force_key;
#endif
};

/* activate update callback */
bool activate_pfx_update_cb = false;
bool activate_spki_update_cb = false;

/* print all updates regardless of socket configuration */
bool print_all_pfx_updates = false;
bool print_all_spki_updates = false;

bool print_status_updates = false;

bool export_pfx = false;
char *export_file_path = NULL;
const char *template_name = NULL;

struct socket_config **socket_config = NULL;
size_t socket_count = 0;

pthread_mutex_t stdout_mutex = PTHREAD_MUTEX_INITIALIZER;

/**
 * @brief Check if argument is NULL and exit if it is
 */
static void *check_alloc_ret(void *ptr)
{
	if (ptr)
		return ptr;

	fprintf(stderr, "Memory allocation error\n");
	exit(-1);
}

/**
 * @brief Return value checking malloc wrapper. Exits program on error
 */
static inline void *checked_malloc(size_t size)
{
	return check_alloc_ret(malloc(size));
}

/**
 * @brief Return value checking realloc wrapper. Exits program on error.
 */
static inline void *checked_realloc(void *ptr, size_t size)
{
	return check_alloc_ret(realloc(ptr, size));
}

static struct socket_config *extend_socket_config()
{
	socket_config = checked_realloc(socket_config, sizeof(struct socket_config *) * (socket_count + 1));
	struct socket_config *config = socket_config[socket_count] = checked_malloc(sizeof(struct socket_config));

	memset(config, 0, sizeof(*config));
	++socket_count;

	return config;
}

static const char *get_template(const char *name)
{
	const struct pfx_output_template *current = templates;

	while (current->name) {
		if (strcmp(name, current->name) == 0)
			return current->template;

		++current;
	}

	if (is_readable_file(name)) {
		FILE *template_file = fopen(name, "r");

		if (fseek(template_file, 0, SEEK_END) == -1)
			goto read_error;

		long file_size = ftell(template_file);

		if (file_size == -1)
			goto read_error;

		rewind(template_file);

		char *template = checked_malloc(file_size);

		size_t bytes_read = fread(template, 1, file_size, template_file);

		if (bytes_read != (unsigned long)file_size)
			goto read_error;

		fclose(template_file);
		return template;

read_error:
		fclose(template_file);
		print_error_exit("Could not read template");
	}

	print_error_exit("Template \"%s\" not found", name);
}

static void print_templates(void)
{
	const struct pfx_output_template *current = templates;

	while (current->name) {
		if (template_name && strcmp(current->name, template_name) == 0)
			printf("%s", current->template);
		else if (!template_name)
			printf("%s\n", current->name);

		++current;
	}
}

static bool is_numeric(const char *str)
{
	for (size_t i = 0; i < strlen(str); ++i) {
		if (!isdigit(str[i]))
			return false;
	}
	return true;
}

static bool is_valid_port_number(const char *str)
{
	if (!is_numeric(str))
		return false;

	int port = atoi(str);

	if (port < 1 || port > 65535)
		return false;

	return true;
}

static bool is_resolveable_host(const char *str)
{
	struct addrinfo hints;
	struct addrinfo *result = NULL;

	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = 0;
	hints.ai_protocol = 0;

	int ret = getaddrinfo(str, NULL, &hints, &result);

	freeaddrinfo(result);

	if (ret != 0)
		return false;
	return true;
}

static bool is_file(const char *str)
{
	struct stat stat_buf;

	if (stat(str, &stat_buf) == -1)
		return false;

	return S_ISREG(stat_buf.st_mode);
}

static bool is_readable_file(const char *str)
{
	if (access(str, R_OK) != 0)
		return false;

	return is_file(str);
}

#ifdef RTRLIB_HAVE_LIBSSH
static bool is_utf8(const char *str)
{
	size_t len = strlen(str);
	size_t i = 0;

	while (i < len) {
		// check if current byte is a single utf8 char
		if ((str[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_PREFIX) {
			i += 1;

		// check if current byte is the start of a two byte utf8 char and validate subsequent bytes
		} else if ((str[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_PREFIX && i + 1 < len &&
			   (str[i + 1] & UTF8_SUBSEQUENT_BYTE_MASK) == UTF8_SUBSEQUENT_BYTE_PREFIX) {
			i += 2;

		// check if current byte is the start of a three byte utf8 char and validate subsequent bytes
		} else if ((str[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_PREFIX && i + 2 < len &&
			   (str[i + 1] & UTF8_SUBSEQUENT_BYTE_MASK) == UTF8_SUBSEQUENT_BYTE_PREFIX &&
			   (str[i + 2] & UTF8_SUBSEQUENT_BYTE_MASK) == UTF8_SUBSEQUENT_BYTE_PREFIX) {
			i += 3;

		// check if current byte is the start of a four byte utf8 char and validate subsequent bytes
		} else if ((str[i] & UTF8_FOUR_BYTE_MASK) == UTF8_FOUR_BYTE_PREFIX && i + 3 < len &&
			   (str[i + 1] & UTF8_SUBSEQUENT_BYTE_MASK) == UTF8_SUBSEQUENT_BYTE_PREFIX &&
			   (str[i + 2] & UTF8_SUBSEQUENT_BYTE_MASK) == UTF8_SUBSEQUENT_BYTE_PREFIX &&
			   (str[i + 3] & UTF8_SUBSEQUENT_BYTE_MASK) == UTF8_SUBSEQUENT_BYTE_PREFIX) {
			i += 4;

		// if none of the conditions matched. The string contains at least one byte that is not valid utf8
		} else {
			return false;
		}
	}
	return true;
}
#endif

struct exporter_state {
	bool roa_section;
	tommy_count_t current_roa;
	tommy_array *roas;
};

struct pfx_export_cb_arg {
	tommy_array *array;
	tommy_hashlin *hashtable;
};

static void pfx_export_cb(const struct pfx_record *pfx_record, void *data);

static tommy_uint32_t hash_pfx_record(const struct pfx_record *record)
{
	struct pfx_record_packed {
		uint8_t ip[16];
		uint32_t asn;
		uint8_t min_len;
		uint8_t max_len;
	} __attribute__((packed));

	struct pfx_record_packed packed_record;

	memset(&packed_record, 0, sizeof(packed_record));

	if (record->prefix.ver == LRTR_IPV4)
		memcpy(&packed_record.ip, &record->prefix.u.addr4, sizeof(struct lrtr_ipv4_addr));
	else
		memcpy(&packed_record.ip, &record->prefix.u.addr6, sizeof(struct lrtr_ipv6_addr));

	packed_record.asn = record->asn;
	packed_record.min_len = record->min_len;
	packed_record.max_len = record->max_len;

	return tommy_hash_u32(0, &packed_record, sizeof(packed_record));
}

static int pfx_record_cmp(const void *data1, const void *data2)
{
	const struct pfx_record *arg1 = data1;
	const struct pfx_record *arg2 = data2;

	if ((arg1->asn == arg2->asn) && (arg1->max_len == arg2->max_len) && (arg1->min_len == arg2->min_len) &&
	    lrtr_ip_addr_equal(arg1->prefix, arg2->prefix))
		return 0;

	return 1;
}

static int template_enter(void *closure, const char *name)
{
	struct exporter_state *state = closure;

	if (strncmp("roas", name, strlen(name)) == 0) {
		state->roa_section = true;
		state->current_roa = 0;

		return 1;

	} else if (state->current_roa && strcmp("last", name) == 0 &&
		   state->current_roa == tommy_array_size(state->roas) - 1) {
		return 1;
	}

	return 0;
}

static int template_leave(void *closure)
{
	struct exporter_state *state = closure;

	state->roa_section = false;
	return 0;
}

static int template_next(void *closure)
{
	struct exporter_state *state = closure;

	if (++state->current_roa >= tommy_array_size(state->roas))
		return 0;

	return 1;
}

static int template_put(void *closure, const char *name, __attribute__((unused)) int escape, FILE *file)
{
	struct exporter_state *state = closure;

	size_t namelen = strlen(name);

	if (!state->roa_section || namelen == 0)
		return MUSTACH_ERROR_SYSTEM;

	struct pfx_record *pfx_record = tommy_array_get(state->roas, state->current_roa);

	if (strncmp("prefix", name, namelen) == 0) {
		char prefix_str[INET6_ADDRSTRLEN];

		lrtr_ip_addr_to_str(&pfx_record->prefix, prefix_str, sizeof(prefix_str));
		fputs(prefix_str, file);

	} else if (strncmp("length", name, namelen) == 0) {
		fprintf(file, "%d", pfx_record->min_len);

	} else if (strncmp("maxlen", name, namelen) == 0) {
		fprintf(file, "%d", pfx_record->max_len);

	} else if (strncmp("origin", name, namelen) == 0) {
		fprintf(file, "%d", pfx_record->asn);
	}

	return MUSTACH_OK;
}

struct mustach_itf template_itf = {.start = NULL,
				   .put = template_put,
				   .enter = template_enter,
				   .next = template_next,
				   .leave = template_leave};

static void print_usage(char **argv)
{
	printf("Usage:\n");
	printf(" %s [-hpkels] [-o file] [-t template] <socket>...\n", argv[0]);
	printf("\nSocket:\n");
	printf(" tcp [-hpkb bindaddr] <host> <port>\n");
#ifdef RTRLIB_HAVE_LIBSSH
	printf(" ssh [-hpkb bindaddr] <host> <port> <username> (<private_key> | <password>) [<host_key>]\n");
#endif
	printf("\nOptions:\n");
	printf("-b  bindaddr Hostnamne or IP address to connect from\n\n");

#ifdef RTRLIB_HAVE_LIBSSH
	printf("-w  force ssh authentication information to be interpreted as a password\n");
	printf("-r  force ssh authentication information to be interpreted as a private key\n\n");
#endif

	printf("-k  Print information about SPKI updates.\n");
	printf("-p  Print information about PFX updates.\n");
	printf("-s  Print information about connection status updates.\n\n");

	printf("-e  export pfx table and exit\n");
	printf("-o  output file for export\n");
	printf("-t  template used for export\n");
	printf("-l  list available templates\n\n");

	printf("-h  Print this help message.\n");

	printf("\nExamples:\n");
	printf(" %s tcp rpki-validator.realmv6.org 8283\n", argv[0]);
	printf(" %s tcp -k -p rpki-validator.realmv6.org 8283\n", argv[0]);
	printf(" %s tcp -k rpki-validator.realmv6.org 8283 tcp -s example.com 323\n", argv[0]);
	printf(" %s -kp tcp rpki-validator.realmv6.org 8283 tcp example.com 323\n", argv[0]);
#ifdef RTRLIB_HAVE_LIBSSH
	printf(" %s ssh rpki-validator.realmv6.org 22 rtr-ssh", argv[0]);
	printf(" ~/.ssh/id_rsa ~/.ssh/known_hosts\n");
	printf(" %s ssh -k -p rpki-validator.realmv6.org 22 rtr-ssh ~/.ssh/id_rsa ~/.ssh/known_hosts\n", argv[0]);
	printf(" %s ssh -k -p rpki-validator.realmv6.org 22 rtr-ssh ~/.ssh/id_rsa ~/.ssh/known_hosts", argv[0]);
	printf(" ssh -k -p example.com 22 rtr-ssh ~/.ssh/id_rsa_example\n");
	printf(" %s ssh -k -p rpki-validator.realmv6.org 22 rtr-ssh ~/.ssh/id_rsa ~/.ssh/known_hosts", argv[0]);
	printf(" tcp -k -p example.com 323\n");
#endif

	printf(" %s -e tcp rpki-validator.realmv6.org 8283\n", argv[0]);
	printf(" %s -e -t csv -o roa.csv tcp rpki-validator.realmv6.org 8283\n", argv[0]);
}

static void status_fp(const struct rtr_mgr_group *group __attribute__((unused)), enum rtr_mgr_status mgr_status,
		      const struct rtr_socket *rtr_sock, void *data __attribute__((unused)))
{
	if (print_status_updates) {
		pthread_mutex_lock(&stdout_mutex);
		printf("RTR-Socket changed connection status to: %s, Mgr Status: %s\n",
		       rtr_state_to_str(rtr_sock->state), rtr_mgr_status_to_str(mgr_status));
		pthread_mutex_unlock(&stdout_mutex);
	}
}

static void update_cb(struct pfx_table *p __attribute__((unused)), const struct pfx_record rec, const bool added)
{
	char ip[INET6_ADDRSTRLEN];

	const struct socket_config *config = (const struct socket_config *)rec.socket;

	if (!print_all_pfx_updates && !config->print_pfx_updates)
		return;

	pthread_mutex_lock(&stdout_mutex);
	if (added)
		printf("+ ");
	else
		printf("- ");
	lrtr_ip_addr_to_str(&rec.prefix, ip, sizeof(ip));
	if (socket_count > 1)
		printf("%s:%s %-40s   %3u - %3u   %10u\n", config->host, config->port, ip, rec.min_len, rec.max_len,
		       rec.asn);
	else
		printf("%-40s   %3u - %3u   %10u\n", ip, rec.min_len, rec.max_len, rec.asn);

	pthread_mutex_unlock(&stdout_mutex);
}

static void update_spki(struct spki_table *s __attribute__((unused)), const struct spki_record record, const bool added)
{
	const struct socket_config *config = (const struct socket_config *)record.socket;

	if (!print_all_spki_updates && !config->print_spki_updates)
		return;

	pthread_mutex_lock(&stdout_mutex);

	char c;

	if (added)
		c = '+';
	else
		c = '-';

	printf("%c ", c);
	printf("HOST:  %s:%s\n", config->host, config->port);
	printf("ASN:  %u\n  ", record.asn);

	int i;
	int size = sizeof(record.ski);

	printf("SKI:  ");
	for (i = 0; i < size; i++) {
		printf("%02x", record.ski[i]);
		if (i < size - 1)
			printf(":");
	}
	printf("\n  ");

	i = 0;
	size = sizeof(record.spki);
	printf("SPKI: ");
	for (i = 0; i < size; i++) {
		if ((i % 40 == 0) && (i != 0))
			printf("\n	");

		printf("%02x", record.spki[i]);
		if (i < size - 1)
			printf(":");
	}
	printf("\n");

	pthread_mutex_unlock(&stdout_mutex);
}

static void parse_global_opts(int argc, char **argv)
{
	int opt;

	bool print_template = false;

	while ((opt = getopt(argc, argv, "+kphelo:t:s")) != -1) {
		switch (opt) {
		case 'k':
			activate_spki_update_cb = true;
			print_all_spki_updates = true;
			break;

		case 'p':
			activate_pfx_update_cb = true;
			print_all_pfx_updates = true;
			break;

		case 'e':
			export_pfx = true;
			break;

		case 'o':
			if (export_file_path)
				print_error_exit("output file can not be specified more than once");

			export_file_path = optarg;
			break;

		case 't':
			template_name = optarg;
			break;

		case 'l':
			print_template = true;
			break;

		case 's':
			print_status_updates = true;
			break;

		default:
			print_usage(argv);
			exit(EXIT_FAILURE);
		}
	}

	if (print_template) {
		print_templates();
		exit(EXIT_SUCCESS);
	}

	if ((export_file_path || template_name) && !export_pfx)
		print_error_exit("Specifying -o or -t without -e does not make sense");
}

static void parse_socket_opts(int argc, char **argv, struct socket_config *config)
{
	int opt;

	while ((opt = getopt(argc, argv, "+kphwrb:")) != -1) {
		switch (opt) {
		case 'k':
			activate_spki_update_cb = true;
			config->print_spki_updates = true;
			break;

		case 'p':
			activate_pfx_update_cb = true;
			config->print_pfx_updates = true;
			break;

		case 'b':
			config->bindaddr = optarg;
			break;

#ifdef RTRLIB_HAVE_LIBSSH
		case 'w':
			if (config->force_key)
				print_error_exit("-w and -r are mutually exclusive");

			config->force_password = true;
			break;

		case 'r':
			if (config->force_password)
				print_error_exit("-w and -r are mutually exclusive");

			config->force_key = true;
			break;
#endif

		default:
			print_usage(argv);
			exit(EXIT_FAILURE);
		}
	}
}

static void print_error_exit(const char *fmt, ...)
{
	va_list argptr;

	va_start(argptr, fmt);

	fprintf(stderr, "error: ");

	vfprintf(stderr, fmt, argptr);

	fprintf(stderr, "\n");

	va_end(argptr);

	exit(EXIT_FAILURE);
}

static int parse_cli(int argc, char **argv)
{
	enum cli_parse_state {
		CLI_PARSE_STATE_BEGIN,
		CLI_PARSE_STATE_GLOBAL_OPTS,
		CLI_PARSE_STATE_SOCKET_BEGIN,
		CLI_PARSE_STATE_SOCKET_TCP_OPTIONS,
		CLI_PARSE_STATE_SOCKET_TCP_HOST,
		CLI_PARSE_STATE_SOCKET_TCP_PORT,
#ifdef RTRLIB_HAVE_LIBSSH
		CLI_PARSE_STATE_SOCKET_SSH_OPTIONS,
		CLI_PARSE_STATE_SOCKET_SSH_HOST,
		CLI_PARSE_STATE_SOCKET_SSH_PORT,
		CLI_PARSE_STATE_SOCKET_SSH_USERNAME,
		CLI_PARSE_STATE_SOCKET_SSH_PRIVATE_KEY_PASSWORD,
		CLI_PARSE_STATE_SOCKET_SSH_HOST_KEY,
#endif
		CLI_PARSE_STATE_END,
	};

	enum cli_parse_state state = CLI_PARSE_STATE_BEGIN;

	struct socket_config *current_config;

	while (true) {
		switch (state) {
		case CLI_PARSE_STATE_BEGIN:
			optind = 1;
			state = CLI_PARSE_STATE_GLOBAL_OPTS;
			break;

		case CLI_PARSE_STATE_GLOBAL_OPTS:
			parse_global_opts(argc, argv);

			state = CLI_PARSE_STATE_SOCKET_BEGIN;
			break;

		case CLI_PARSE_STATE_SOCKET_BEGIN:
			current_config = extend_socket_config();

			if (strncasecmp(argv[optind], "tcp", strlen(argv[optind])) == 0) {
				state = CLI_PARSE_STATE_SOCKET_TCP_OPTIONS;
				current_config->type = SOCKET_TYPE_TCP;

				/* Check if enough arguments are available
				 * to configure this socket.
				 * This does not account for options.
				 */
				if ((argc - optind) < 2)
					print_error_exit("Not enough arguments for tcp socket");

			} else if (strncasecmp(argv[optind], "ssh", strlen(argv[optind])) == 0) {
#ifdef RTRLIB_HAVE_LIBSSH
				state = CLI_PARSE_STATE_SOCKET_SSH_OPTIONS;
				current_config->type = SOCKET_TYPE_SSH;

				/* Check if enough arguments are available
				 * to configure this socket.
				 * This does not account for options.
				 */
				if ((argc - optind) < 4)
					print_error_exit("Not enough arguments for ssh socket");
#else
				print_error_exit("ssh support disabled at compile time\n");
#endif

			} else {
				print_error_exit("\"%s\" is not a valid socket type\n", argv[optind]);
				break;
			}

			++optind;
			break;

		case CLI_PARSE_STATE_SOCKET_TCP_OPTIONS:
			parse_socket_opts(argc, argv, current_config);

			/* Check again if enough arguments are available,
			 * accounting for options
			 */
			if ((argc - optind) < 2)
				print_error_exit("Not enough arguments for tcp socket");

			state = CLI_PARSE_STATE_SOCKET_TCP_HOST;
			break;

		case CLI_PARSE_STATE_SOCKET_TCP_HOST:
			if (!is_resolveable_host(argv[optind]))
				print_error_exit("cannot resolve \"%s\"\n", argv[optind]);

			current_config->host = argv[optind++];

			state = CLI_PARSE_STATE_SOCKET_TCP_PORT;
			break;

		case CLI_PARSE_STATE_SOCKET_TCP_PORT:
			if (!is_valid_port_number(argv[optind]))
				print_error_exit("\"%s\" is not a valid port number\n", argv[optind]);

			current_config->port = argv[optind++];

			state = CLI_PARSE_STATE_SOCKET_BEGIN;
			break;

#ifdef RTRLIB_HAVE_LIBSSH
		case CLI_PARSE_STATE_SOCKET_SSH_OPTIONS:
			parse_socket_opts(argc, argv, current_config);

			/* Check again if enough arguments are available,
			 * accounting for options
			 */
			if ((argc - optind) < 4)
				print_error_exit("Not enough arguments for ssh socket");

			state = CLI_PARSE_STATE_SOCKET_SSH_HOST;
			break;

		case CLI_PARSE_STATE_SOCKET_SSH_HOST:
			if (!is_resolveable_host(argv[optind]))
				print_error_exit("cannot resolve \"%s\"\n", argv[optind]);

			current_config->host = argv[optind++];

			state = CLI_PARSE_STATE_SOCKET_SSH_PORT;
			break;

		case CLI_PARSE_STATE_SOCKET_SSH_PORT:
			if (!is_valid_port_number(argv[optind]))
				print_error_exit("\"%s\" is not a valid port number\n", argv[optind]);

			current_config->port = argv[optind++];

			state = CLI_PARSE_STATE_SOCKET_SSH_USERNAME;
			break;

		case CLI_PARSE_STATE_SOCKET_SSH_USERNAME:
			current_config->ssh_username = argv[optind++];

			state = CLI_PARSE_STATE_SOCKET_SSH_PRIVATE_KEY_PASSWORD;
			break;

		case CLI_PARSE_STATE_SOCKET_SSH_PRIVATE_KEY_PASSWORD:
			if (current_config->force_key && !is_readable_file(argv[optind])) {
				print_error_exit("\"%s\" is not a readable file\n", argv[optind]);

			} else if (!current_config->force_password && !current_config->force_key &&
				   is_readable_file(argv[optind])) {
				current_config->ssh_private_key = argv[optind];

			} else if (!current_config->force_password && is_utf8(argv[optind])) {
				fprintf(stderr, "\"%s\" does not seem to be a file. Trying password authentication.\n",
					argv[optind]);
				fprintf(stderr, "Use -r to force key authentication or -w to silence this warning");
				current_config->ssh_password = argv[optind];

			} else if (current_config->force_password && !is_utf8(argv[optind])) {
				print_error_exit("\"%s\" is not a valid utf8 string", argv[optind]);

			} else if (current_config->force_password && !current_config->force_key &&
				   is_utf8(argv[optind])) {
				current_config->ssh_password = argv[optind];

			} else {
				print_error_exit("\"%s\" is neither a readable file nor a valid utf8 string",
						 argv[optind]);
			}

			++optind;
			state = CLI_PARSE_STATE_SOCKET_SSH_HOST_KEY;
			break;

		case CLI_PARSE_STATE_SOCKET_SSH_HOST_KEY:
			if (strncasecmp(argv[optind], "tcp", strlen(argv[optind])) == 0 ||
			    strncasecmp(argv[optind], "ssh", strlen(argv[optind])) == 0) {
				state = CLI_PARSE_STATE_SOCKET_BEGIN;
				break;
			}

			if (!is_readable_file(argv[optind]))
				print_error_exit("\"%s\" is not a readable file\n", argv[optind]);

			current_config->ssh_host_key = argv[optind++];

			state = CLI_PARSE_STATE_SOCKET_BEGIN;
			break;
#endif

		case CLI_PARSE_STATE_END:
			return 0;
		}

		if (optind >= argc)
			state = CLI_PARSE_STATE_END;
	}
}

static void init_sockets(void)
{
	for (size_t i = 0; i < socket_count; ++i) {
		struct socket_config *config = socket_config[i];
		struct tr_tcp_config tcp_config = {};
#ifdef RTRLIB_HAVE_LIBSSH
		struct tr_ssh_config ssh_config = {};
#endif

		switch (config->type) {
		case SOCKET_TYPE_TCP:
			tcp_config.host = config->host;
			tcp_config.port = config->port;
			tcp_config.bindaddr = config->bindaddr;

			tr_tcp_init(&tcp_config, &config->tr_socket);
			config->socket.tr_socket = &config->tr_socket;
			break;

#ifdef RTRLIB_HAVE_LIBSSH
		case SOCKET_TYPE_SSH:
			ssh_config.host = config->host;
			ssh_config.port = atoi(config->port);
			ssh_config.bindaddr = config->bindaddr;
			ssh_config.username = config->ssh_username;
			ssh_config.client_privkey_path = config->ssh_private_key;
			ssh_config.server_hostkey_path = config->ssh_host_key;
			ssh_config.password = config->ssh_password;

			tr_ssh_init(&ssh_config, &config->tr_socket);
			config->socket.tr_socket = &config->tr_socket;
			break;
#endif
		}
	}
}

int main(int argc, char **argv)
{
	parse_cli(argc, argv);

	if (socket_count == 0) {
		print_usage(argv);
		exit(EXIT_FAILURE);
	}

	init_sockets();

	struct rtr_mgr_config *conf;
	struct rtr_mgr_group groups[1];

	groups[0].sockets_len = socket_count;
	groups[0].sockets = (struct rtr_socket **)socket_config;
	groups[0].preference = 1;

	spki_update_fp spki_update_fp = activate_spki_update_cb ? update_spki : NULL;
	pfx_update_fp pfx_update_fp = activate_pfx_update_cb ? update_cb : NULL;

	int ret = rtr_mgr_init(&conf, groups, 1, 30, 600, 600, pfx_update_fp, spki_update_fp, status_fp, NULL);

	if (ret == RTR_ERROR)
		fprintf(stderr, "Error in rtr_mgr_init!\n");
	else if (ret == RTR_INVALID_PARAM)
		fprintf(stderr, "Invalid params passed to rtr_mgr_init\n");

	if (!conf)
		return EXIT_FAILURE;

	if (!export_pfx && activate_pfx_update_cb && socket_count > 1)
		printf("%-40s %-40s   %3s   %3s   %3s\n", "host", "Prefix", "Prefix Length", "", "ASN");
	else if (!export_pfx && activate_pfx_update_cb)
		printf("%-40s   %3s   %3s   %3s\n", "Prefix", "Prefix Length", "", "ASN");

	if (export_pfx) {
		const char *template;

		if (template_name)
			template = get_template(template_name);
		else
			template = templates->template;
		FILE *export_file = stdout;

		if (export_file_path) {
			export_file = fopen(export_file_path, "w");
			if (!export_file) {
				char *errormsg = strerror(errno);

				print_error_exit("\"%s\" could not be opened for writing: %s", optarg, errormsg);
			}
		}

		rtr_mgr_start(conf);

		while (!rtr_mgr_conf_in_sync(conf))
			sleep(1);

		printf("Sync done\n");
		tommy_array prefixes;
		tommy_hashlin prefix_hash;
		struct pfx_export_cb_arg arg = {
			.array = &prefixes,
			.hashtable = &prefix_hash,
		};

		tommy_array_init(&prefixes);
		tommy_hashlin_init(&prefix_hash);

		pfx_table_for_each_ipv4_record(conf->pfx_table, pfx_export_cb, &arg);
		pfx_table_for_each_ipv6_record(conf->pfx_table, pfx_export_cb, &arg);

		struct exporter_state state = {
			.roa_section = false,
			.current_roa = 0,
			.roas = &prefixes,
		};

		fmustach(template, &template_itf, &state, export_file);

		tommy_hashlin_foreach(&prefix_hash, free);
		tommy_hashlin_done(&prefix_hash);
		tommy_array_done(&prefixes);

		if (export_file != stdout && fclose(export_file) == EOF) {
			char *errormsg = strerror(errno);

			print_error_exit("\"Error during write into output file: %s\"", errormsg);
		}

	} else {
		rtr_mgr_start(conf);
		pause();
	}

	rtr_mgr_stop(conf);
	rtr_mgr_free(conf);
	free(groups[0].sockets);

	return EXIT_SUCCESS;
}

struct pfx_record_entry {
	struct pfx_record record;
	tommy_node hash_node;
};

static void pfx_export_cb(const struct pfx_record *pfx_record, void *data)
{
	struct pfx_export_cb_arg *arg = data;
	tommy_array *roa_array = arg->array;
	tommy_hashlin *roa_hashtable = arg->hashtable;

	tommy_hash_t record_hash = hash_pfx_record(pfx_record);

	if (tommy_hashlin_search(roa_hashtable, &pfx_record_cmp, pfx_record, record_hash) != 0)
		return;

	struct pfx_record_entry *pfx_record_entry = malloc(sizeof(struct pfx_record_entry));

	memcpy(&pfx_record_entry->record, pfx_record, sizeof(struct pfx_record));

	tommy_hashlin_insert(roa_hashtable, &pfx_record_entry->hash_node, pfx_record_entry, record_hash);
	tommy_array_insert(roa_array, pfx_record_entry);
}