-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrtmon.c
More file actions
166 lines (134 loc) · 3.34 KB
/
rtmon.c
File metadata and controls
166 lines (134 loc) · 3.34 KB
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
/*
* dvpn, a multipoint vpn implementation
* Copyright (C) 2016 Lennert Buytenhek
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version
* 2.1 as published by the Free Software Foundation.
*
* This library 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 Lesser General Public License version 2.1 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License version 2.1 along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <gnutls/abstract.h>
#include <gnutls/x509.h>
#include <iv.h>
#include <iv_signal.h>
#include "conf.h"
#include "dgp_connect.h"
#include "loc_rib.h"
#include "loc_rib_print.h"
#include "rt_builder.h"
#include "x509.h"
static uint8_t myid[NODE_ID_LEN];
static struct loc_rib loc_rib;
static struct rt_builder rb;
static struct dgp_connect dc;
static struct iv_signal sigint;
static struct iv_signal sigusr1;
static void print_addr(FILE *fp, uint8_t *addr)
{
if (addr != NULL) {
char caddr[64];
inet_ntop(AF_INET6, addr, caddr, sizeof(caddr));
fputs(caddr, fp);
} else {
fprintf(fp, "<direct>");
}
}
static void rt_add(void *_dummy, uint8_t *dest, uint8_t *nh)
{
printf("+ ");
print_addr(stdout, dest);
printf(" via ");
print_addr(stdout, nh);
printf("\n");
fflush(stdout);
}
static void rt_mod(void *_dummy, uint8_t *dest, uint8_t *oldnh, uint8_t *newnh)
{
printf("| ");
print_addr(stdout, dest);
printf(" via ");
print_addr(stdout, oldnh);
printf(" to ");
print_addr(stdout, newnh);
printf("\n");
fflush(stdout);
}
static void rt_del(void *_dummy, uint8_t *dest, uint8_t *nh)
{
printf("- ");
print_addr(stdout, dest);
printf(" via ");
print_addr(stdout, nh);
printf("\n");
fflush(stdout);
}
static void got_sigint(void *_dummy)
{
fprintf(stderr, "SIGINT received, shutting down\n");
rt_builder_deinit(&rb);
dgp_connect_stop(&dc);
iv_signal_unregister(&sigint);
iv_signal_unregister(&sigusr1);
}
static void got_sigusr1(void *_dummy)
{
loc_rib_print(stderr, &loc_rib);
}
int rtmon(const char *config)
{
struct conf *conf;
gnutls_pubkey_t pubkey;
conf = parse_config(config);
if (conf == NULL)
return 1;
gnutls_global_init();
if (read_pubkey(&pubkey, conf->public_key) < 0)
return 1;
free_config(conf);
get_pubkey_id(myid, pubkey);
gnutls_pubkey_deinit(pubkey);
gnutls_global_deinit();
iv_init();
loc_rib.myid = NULL;
loc_rib_init(&loc_rib);
rb.rib = &loc_rib;
rb.myid = myid;
rb.cookie = NULL;
rb.rt_add = rt_add;
rb.rt_mod = rt_mod;
rb.rt_del = rt_del;
rt_builder_init(&rb);
dc.myid = NULL;
dc.remoteid = myid;
dc.ifindex = 0;
dc.loc_rib = &loc_rib;
dgp_connect_start(&dc);
IV_SIGNAL_INIT(&sigint);
sigint.signum = SIGINT;
sigint.flags = 0;
sigint.cookie = NULL;
sigint.handler = got_sigint;
iv_signal_register(&sigint);
IV_SIGNAL_INIT(&sigusr1);
sigusr1.signum = SIGUSR1;
sigusr1.flags = 0;
sigusr1.cookie = NULL;
sigusr1.handler = got_sigusr1;
iv_signal_register(&sigusr1);
iv_main();
loc_rib_deinit(&loc_rib);
iv_deinit();
return 0;
}