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>
42 #include <tid_internal.h>
43 #include <mon_internal.h>
47 #include <tr_config.h>
49 #include <tr_cfgwatch.h>
53 #define TALLOC_DEBUG_ENABLE 1
55 /***** command-line option handling / setup *****/
57 static void print_version_info(void)
59 printf("Moonshot Trust Router %s\n\n", PACKAGE_VERSION);
62 /* Strip trailing / from a path name.*/
63 static void remove_trailing_slash(char *s) {
72 /* argp global parameters */
73 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
76 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router " PACKAGE_VERSION;
77 static const char arg_doc[]=""; /* string describing arguments, if any */
79 /* define the options here. Fields are:
80 * { long-name, short-name, variable name, options, help description } */
81 static const struct argp_option cmdline_options[] = {
82 { "config-dir", 'c', "DIR", 0, "Specify configuration file location (default is current directory)"},
83 { "version", 'v', NULL, 0, "Print version information and exit"},
87 /* structure for communicating with option parser */
89 int version_requested;
93 /* parser for individual options - fills in a struct cmdline_args */
94 static error_t parse_option(int key, char *arg, struct argp_state *state)
96 /* get a shorthand to the command line argument structure, part of state */
97 struct cmdline_args *arguments=state->input;
102 /* somehow we got called without an argument */
103 return ARGP_ERR_UNKNOWN;
105 arguments->config_dir=arg;
109 arguments->version_requested=1;
113 return ARGP_ERR_UNKNOWN;
116 return 0; /* success */
119 /* assemble the argp parser */
120 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
123 /***** talloc error handling *****/
124 /* called when talloc tries to abort */
125 static void tr_abort(const char *reason)
127 tr_crit("tr_abort: Critical error, talloc aborted. Reason: %s", reason);
131 #if TALLOC_DEBUG_ENABLE
132 static void tr_talloc_log(const char *msg)
134 tr_debug("talloc: %s", msg);
136 #endif /* TALLOC_DEBUG_ENABLE */
138 static void configure_signals(void)
142 sigemptyset(&signals);
143 sigaddset(&signals, SIGPIPE);
144 pthread_sigmask(SIG_BLOCK, &signals, NULL);
147 int main(int argc, char *argv[])
149 TALLOC_CTX *main_ctx=NULL;
151 TR_INSTANCE *tr = NULL;
152 struct cmdline_args opts;
153 struct event_base *ev_base;
154 struct tr_socket_event tids_ev = {0};
155 struct tr_socket_event mon_ev = {0};
156 struct event *cfgwatch_ev;
160 /* we're going to be multithreaded, so disable null context tracking */
161 talloc_set_abort_fn(tr_abort);
162 talloc_disable_null_tracking();
163 #if TALLOC_DEBUG_ENABLE
164 talloc_set_log_fn(tr_talloc_log);
165 #endif /* TALLOC_DEBUG_ENABLE */
166 main_ctx=talloc_new(NULL);
168 /* Use standalone logging */
171 /***** parse command-line arguments *****/
173 opts.version_requested=0;
176 /* parse the command line*/
177 argp_parse(&argp, argc, argv, 0, 0, &opts);
179 /* process options */
180 remove_trailing_slash(opts.config_dir);
183 /***** Print version info *****/
184 print_version_info();
185 if (opts.version_requested)
186 return 0; /* requested that we print version and exit */
188 /***** create a Trust Router instance *****/
189 if (NULL == (tr = tr_create(main_ctx))) {
190 tr_crit("Unable to create Trust Router instance, exiting.");
194 /***** initialize the trust path query server instance *****/
195 if (NULL == (tr->tids = tids_new(tr))) {
196 tr_crit("Error initializing Trust Path Query Server instance.");
200 /***** initialize the trust router protocol server instance *****/
201 if (NULL == (tr->trps = trps_new(tr))) {
202 tr_crit("Error initializing Trust Router Protocol Server instance.");
206 /***** initialize the monitoring interface instance *****/
207 if (NULL == (tr->mons = mons_new(tr))) {
208 tr_crit("Error initializing monitoring interface instance.");
211 /* Monitor our tids/trps instances */
212 tr->mons->tids = tr->tids;
213 tr->mons->trps = tr->trps;
215 /***** process configuration *****/
216 tr->cfgwatch=tr_cfgwatch_create(tr);
217 if (tr->cfgwatch == NULL) {
218 tr_crit("Unable to create configuration watcher object, exiting.");
221 tr->cfgwatch->config_dir=opts.config_dir;
222 tr->cfgwatch->cfg_mgr=tr->cfg_mgr;
223 tr->cfgwatch->update_cb=tr_config_changed; /* handle configuration changes */
224 tr->cfgwatch->update_cookie=(void *)tr;
225 if (0 != tr_read_and_apply_config(tr->cfgwatch)) {
226 tr_crit("Error reading configuration, exiting.");
230 /***** Set up the event loop *****/
231 ev_base=tr_event_loop_init(); /* Set up the event loop */
233 tr_crit("Error initializing event loop.");
237 /* already set config_dir, fstat_list and n_files earlier */
238 if (0 != tr_cfgwatch_event_init(ev_base, tr->cfgwatch, &cfgwatch_ev)) {
239 tr_crit("Error initializing configuration file watcher.");
243 /* install monitoring interface events */
244 tr_debug("Initializing monitoring interface events.");
245 if (0 != tr_mons_event_init(ev_base, tr->mons, tr->cfg_mgr, &mon_ev)) {
246 tr_crit("Error initializing monitoring interface.");
250 /* install TID server events */
251 tr_debug("Initializing TID server events.");
252 if (0 != tr_tids_event_init(ev_base,
257 tr_crit("Error initializing Trust Path Query Server instance.");
261 /* install TRP handler events */
262 tr_debug("Initializing Dynamic Trust Router Protocol events.");
263 if (TRP_SUCCESS != tr_trps_event_init(ev_base, tr)) {
264 tr_crit("Error initializing Trust Path Query Server instance.");
268 tr_debug("Starting event loop.");
269 tr_event_loop_run(ev_base); /* does not return until we are done */
271 tr_destroy(tr); /* thanks to talloc, should destroy everything */
273 talloc_free(main_ctx);