forked from rtrlib/bird-rtrlib-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.c
215 lines (210 loc) · 5.82 KB
/
cli.c
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
/*
* This file is part of BIRD-RTRlib-CLI.
*
* BIRD-RTRlib-CLI is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* BIRD-RTRlib-CLI 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 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with BIRD-RTRlib-CLI; see the file COPYING.
*
* written by Mehmet Ceyran, in cooperation with:
* CST group, Freie Universitaet Berlin
* Website: https://github.com/rtrlib/bird-rtrlib-cli
*/
#include <argp.h>
#include <stddef.h>
#include <string.h>
#include "config.h"
#define ARGKEY_BIRD_ROA_TABLE 't'
#define ARGKEY_BIRD_SOCKET 'b'
#define ARGKEY_RTR_ADDRESS 'r'
#define ARGKEY_RTR_SOURCE_ADDRESS 0x100
#define ARGKEY_RTRSSH_ENABLE 's'
#define ARGKEY_RTRSSH_HOSTKEY 0x101
#define ARGKEY_RTRSSH_PRIVKEY 0x102
#define ARGKEY_RTRSSH_USERNAME 0x103
#define ARGKEY_IP_VERSION 'v'
#define ARGKEY_QUIET 'q'
#define ARGKEY_DAEMON 'd'
#define ARGKEY_PIDFILE 'p'
// Parser function for argp_parse().
static error_t argp_parser(int key, char *arg, struct argp_state *state)
{
// Shortcut to config object passed to argp_parse().
struct config *config = state->input;
// Process command line argument.
switch (key) {
case ARGKEY_BIRD_ROA_TABLE:
config->bird_roa_table = arg;
break;
case ARGKEY_BIRD_SOCKET:
// Process BIRD socket path.
config->bird_socket_path = arg;
break;
case ARGKEY_RTR_ADDRESS:
config->rtr_host = strtok(arg, ":");
config->rtr_port = strtok(0, ":");
break;
case ARGKEY_RTR_SOURCE_ADDRESS:
config->rtr_bind_addr = arg;
break;
case ARGKEY_RTRSSH_ENABLE:
config->rtr_connection_type = ssh;
break;
case ARGKEY_RTRSSH_HOSTKEY:
config->rtr_ssh_hostkey_file = arg;
break;
case ARGKEY_RTRSSH_PRIVKEY:
config->rtr_ssh_privkey_file = arg;
break;
case ARGKEY_RTRSSH_USERNAME:
config->rtr_ssh_username = arg;
break;
case ARGKEY_IP_VERSION:
config->ip_version = arg;
break;
case ARGKEY_QUIET:
config->quiet = true;
break;
case ARGKEY_DAEMON:
config->daemon = true;
break;
case ARGKEY_PIDFILE:
config->pidfile = arg;
break;
default:
// Process unknown argument.
return ARGP_ERR_UNKNOWN;
}
// Return success.
return 0;
}
// Parses the specified command line arguments into the program config.
int parse_cli(int argc, char **argv, struct config *config)
{
// Command line options definition.
const struct argp_option argp_options[] = {
{
"bird-socket",
ARGKEY_BIRD_SOCKET,
"<BIRD_SOCKET_PATH>",
0,
"Path to the BIRD control socket.",
0
},
{
"bird-roa-table",
ARGKEY_BIRD_ROA_TABLE,
"<BIRD_ROA_TABLE>",
0,
"(optional) Name of the BIRD ROA table for RPKI ROA imports.",
0
},
{
"rtr-address",
ARGKEY_RTR_ADDRESS,
"<RTR_HOST>:<RTR_PORT>",
0,
"Address of the RTR server.",
1
},
{
"rtr-source-address",
ARGKEY_RTR_SOURCE_ADDRESS,
"<RTR_SOURCE_ADDRESS>",
0,
"(optional) Source address of the connection to the RTR server.",
1
},
{
"ssh",
ARGKEY_RTRSSH_ENABLE,
0,
0,
"Use an SSH connection instead of plain TCP.",
1
},
{
"rtr-ssh-hostkey",
ARGKEY_RTRSSH_HOSTKEY,
"<RTR_SSH_HOSTKEY_FILE>",
0,
"(optional) Path to a file containing the SSH host key of the RTR "
"server. Uses the default known_hosts file if not specified.",
2
},
{
"rtr-ssh-username",
ARGKEY_RTRSSH_USERNAME,
"<RTR_SSH_USERNAME>",
0,
"Name of the user to be authenticated with the RTR server. "
"Mandatory for SSH connections.",
2
},
{
"rtr-ssh-privkey",
ARGKEY_RTRSSH_PRIVKEY,
"<RTR_SSH_PRIVKEY_FILE>",
0,
"(optional) Path to a file containing the private key of the user "
"to be authenticated with the RTR server if an SSH connection is "
"used. Uses the user's default identity file if not specified.",
2
},
{
"version",
ARGKEY_IP_VERSION,
"<4|6>",
0,
"(Optional) Indicate if the bird daemon wants IPv4 or IPv6 only updates.",
2
},
{
"quiet",
ARGKEY_QUIET,
0,
0,
"(Optional) Report only errors.",
1
},
{
"daemon",
ARGKEY_DAEMON,
0,
0,
"(Optional) run as daemon, detach from terminal.",
1
},
{
"pidfile",
ARGKEY_PIDFILE,
"<PIDFILE>",
0,
"(Optional) Name of the pidfile",
2
},
{0}
};
// argp structure to be passed to argp_parse().
const struct argp argp = {
argp_options,
&argp_parser,
NULL,
"RTRLIB <-> BIRD interface",
NULL,
NULL,
NULL
};
// Parse command line. Exits on errors.
argp_parse(&argp, argc, argv, 0, NULL, config);
// Return success.
return 1;
}