2 * Copyright (c) 2012, 2015, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <event2/event.h>
43 #include <tid_internal.h>
44 #include <mon_internal.h>
48 #include <tr_config.h>
50 #include <tr_cfgwatch.h>
54 #define TALLOC_DEBUG_ENABLE 1
56 /***** command-line option handling / setup *****/
58 static void print_version_info(void)
60 printf("Moonshot Trust Router %s\n\n", PACKAGE_VERSION);
63 /* Strip trailing / from a path name.*/
64 static void remove_trailing_slash(char *s) {
73 /* argp global parameters */
74 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
77 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router " PACKAGE_VERSION;
78 static const char arg_doc[]=""; /* string describing arguments, if any */
80 /* define the options here. Fields are:
81 * { long-name, short-name, variable name, options, help description } */
82 static const struct argp_option cmdline_options[] = {
83 { "config-dir", 'c', "DIR", 0, "Specify configuration file location (default is current directory)"},
84 { "config-validate", 'C', NULL, 0, "Validate configuration files and exit"},
85 { "version", 1, NULL, 0, "Print version information and exit"},
89 /* structure for communicating with option parser */
91 int version_requested;
92 int validate_config_and_exit;
96 /* parser for individual options - fills in a struct cmdline_args */
97 static error_t parse_option(int key, char *arg, struct argp_state *state)
99 /* get a shorthand to the command line argument structure, part of state */
100 struct cmdline_args *arguments=state->input;
105 /* somehow we got called without an argument */
106 return ARGP_ERR_UNKNOWN;
108 arguments->config_dir=arg;
112 arguments->version_requested=1;
116 arguments->validate_config_and_exit=1;
120 return ARGP_ERR_UNKNOWN;
123 return 0; /* success */
126 /* assemble the argp parser */
127 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
130 /***** talloc error handling *****/
131 /* called when talloc tries to abort */
132 static void tr_abort(const char *reason)
134 tr_crit("tr_abort: Critical error, talloc aborted. Reason: %s", reason);
138 #if TALLOC_DEBUG_ENABLE
139 static void tr_talloc_log(const char *msg)
141 tr_debug("talloc: %s", msg);
143 #endif /* TALLOC_DEBUG_ENABLE */
145 static void configure_signals(void)
149 sigemptyset(&signals);
150 sigaddset(&signals, SIGPIPE);
151 pthread_sigmask(SIG_BLOCK, &signals, NULL);
154 /* Monitoring handlers */
155 static MON_RC tr_handle_version(void *cookie, json_t **result_ptr)
157 *result_ptr = json_string(PACKAGE_VERSION);
158 return (*result_ptr == NULL) ? MON_NOMEM : MON_SUCCESS;
161 static MON_RC tr_handle_uptime(void *cookie, json_t **result_ptr)
163 time_t *start_time = cookie;
164 *result_ptr = json_integer(time(NULL) - (*start_time));
165 return (*result_ptr == NULL) ? MON_NOMEM : MON_SUCCESS;
168 static MON_RC tr_handle_show_rp_clients(void *cookie, json_t **response_ptr)
170 TR_CFG_MGR *cfg_mgr = talloc_get_type_abort(cookie, TR_CFG_MGR);
172 *response_ptr = tr_rp_clients_to_json(cfg_mgr->active->rp_clients);
173 return (*response_ptr == NULL) ? MON_NOMEM : MON_SUCCESS;
176 static MON_RC tr_handle_show_cfg_serial(void *cookie, json_t **response_ptr)
178 TR_CFG_MGR *cfg_mgr = talloc_get_type_abort(cookie, TR_CFG_MGR);
180 *response_ptr = tr_cfg_files_to_json_array(cfg_mgr->active);
181 return (*response_ptr == NULL) ? MON_NOMEM : MON_SUCCESS;
186 int main(int argc, char *argv[])
188 TALLOC_CTX *main_ctx=NULL;
190 TR_INSTANCE *tr = NULL;
191 struct cmdline_args opts;
192 struct event_base *ev_base;
193 struct tr_socket_event tids_ev = {0};
194 struct event *tids_sweep_ev;
195 struct tr_socket_event mon_ev = {0};
196 struct event *cfgwatch_ev;
198 time_t start_time = time(NULL); /* TODO move this? */
202 /* we're going to be multithreaded, so disable null context tracking */
203 talloc_set_abort_fn(tr_abort);
204 talloc_disable_null_tracking();
205 #if TALLOC_DEBUG_ENABLE
206 talloc_set_log_fn(tr_talloc_log);
207 #endif /* TALLOC_DEBUG_ENABLE */
208 main_ctx=talloc_new(NULL);
210 /* Use standalone logging */
213 /***** parse command-line arguments *****/
215 opts.version_requested=0;
216 opts.validate_config_and_exit=0;
219 /* parse the command line*/
220 argp_parse(&argp, argc, argv, 0, 0, &opts);
222 /* process options */
223 remove_trailing_slash(opts.config_dir);
226 /***** Print version info *****/
227 print_version_info();
228 if (opts.version_requested)
229 return 0; /* requested that we print version and exit */
231 /***** create a Trust Router instance *****/
232 if (NULL == (tr = tr_create(main_ctx))) {
233 tr_crit("Unable to create Trust Router instance, exiting.");
237 /***** initialize the trust path query server instance *****/
238 if (NULL == (tr->tids = tids_new(tr))) {
239 tr_crit("Error initializing Trust Path Query Server instance.");
243 /***** initialize the trust router protocol server instance *****/
244 if (NULL == (tr->trps = trps_new(tr))) {
245 tr_crit("Error initializing Trust Router Protocol Server instance.");
249 /***** initialize the monitoring interface instance *****/
250 if (NULL == (tr->mons = mons_new(tr))) {
251 tr_crit("Error initializing monitoring interface instance.");
254 /* Monitor our tids/trps instances */
255 tr->mons->tids = tr->tids;
256 tr->mons->trps = tr->trps;
258 /* Register monitoring handlers */
259 mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_VERSION, tr_handle_version, NULL);
260 mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_CONFIG_FILES, tr_handle_show_cfg_serial, tr->cfg_mgr);
261 mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_UPTIME, tr_handle_uptime, &start_time);
262 mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_RP_CLIENTS, tr_handle_show_rp_clients, tr->cfg_mgr);
263 tr_tid_register_mons_handlers(tr->tids, tr->mons);
264 tr_trp_register_mons_handlers(tr->trps, tr->mons);
266 /***** process configuration *****/
267 tr->cfgwatch=tr_cfgwatch_create(tr);
268 if (tr->cfgwatch == NULL) {
269 tr_crit("Unable to create configuration watcher object, exiting.");
272 tr->cfgwatch->config_dir=opts.config_dir;
273 tr->cfgwatch->cfg_mgr=tr->cfg_mgr;
274 tr->cfgwatch->update_cb=tr_config_changed; /* handle configuration changes */
275 tr->cfgwatch->update_cookie=(void *)tr;
276 if (0 != tr_read_and_apply_config(tr->cfgwatch)) {
277 tr_crit("Error reading configuration, exiting.");
281 /***** Exit here if we are just validating our configuration *****/
282 if (opts.validate_config_and_exit) {
283 printf("Valid configuration found in %s.\n", opts.config_dir);
286 /***** Set up the event loop *****/
287 ev_base=tr_event_loop_init(); /* Set up the event loop */
289 tr_crit("Error initializing event loop.");
293 /* already set config_dir, fstat_list and n_files earlier */
294 if (0 != tr_cfgwatch_event_init(ev_base, tr->cfgwatch, &cfgwatch_ev)) {
295 tr_crit("Error initializing configuration file watcher.");
299 /* install monitoring interface events */
300 tr_debug("Initializing monitoring interface events.");
301 if (0 != tr_mons_event_init(ev_base, tr->mons, tr->cfg_mgr, &mon_ev)) {
302 tr_crit("Error initializing monitoring interface.");
306 /* install TID server events */
307 tr_debug("Initializing TID server events.");
308 if (0 != tr_tids_event_init(ev_base, tr->tids, tr->cfg_mgr, tr->trps, &tids_ev, &tids_sweep_ev)) {
309 tr_crit("Error initializing Trust Path Query Server instance.");
313 /* install TRP handler events */
314 tr_debug("Initializing Dynamic Trust Router Protocol events.");
315 if (TRP_SUCCESS != tr_trps_event_init(ev_base, tr)) {
316 tr_crit("Error initializing Trust Path Query Server instance.");
320 tr_debug("Starting event loop.");
321 tr_event_loop_run(ev_base); /* does not return until we are done */
323 tr_destroy(tr); /* thanks to talloc, should destroy everything */
325 talloc_free(main_ctx);