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.
39 #include <event2/event.h>
44 #include <tid_internal.h>
47 #include <tr_config.h>
49 #include <tr_cfgwatch.h>
52 /***** command-line option handling / setup *****/
54 /* Strip trailing / from a path name.*/
55 static void remove_trailing_slash(char *s) {
64 /* argp global parameters */
65 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
68 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router";
69 static const char arg_doc[]=""; /* string describing arguments, if any */
71 /* define the options here. Fields are:
72 * { long-name, short-name, variable name, options, help description } */
73 static const struct argp_option cmdline_options[] = {
74 { "config-dir", 'c', "DIR", 0, "Specify configuration file location (default is current directory)"},
78 /* structure for communicating with option parser */
83 /* parser for individual options - fills in a struct cmdline_args */
84 static error_t parse_option(int key, char *arg, struct argp_state *state)
86 /* get a shorthand to the command line argument structure, part of state */
87 struct cmdline_args *arguments=state->input;
92 /* somehow we got called without an argument */
93 return ARGP_ERR_UNKNOWN;
95 arguments->config_dir=arg;
99 return ARGP_ERR_UNKNOWN;
102 return 0; /* success */
105 /* assemble the argp parser */
106 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
109 int main (int argc, char *argv[])
111 TALLOC_CTX *main_ctx=talloc_new(NULL);
113 TR_INSTANCE *tr = NULL;
114 struct cmdline_args opts;
115 struct event_base *ev_base;
116 struct tr_socket_event tids_ev, trps_ev;
117 struct event *cfgwatch_ev;
119 /* Use standalone logging */
122 /***** parse command-line arguments *****/
126 /* parse the command line*/
127 argp_parse(&argp, argc, argv, 0, 0, &opts);
129 /* process options */
130 remove_trailing_slash(opts.config_dir);
132 /***** create a Trust Router instance *****/
133 if (NULL == (tr = tr_create(main_ctx))) {
134 tr_crit("Unable to create Trust Router instance, exiting.");
138 /***** process configuration *****/
139 tr->cfgwatch=tr_cfgwatch_create(tr);
140 if (tr->cfgwatch == NULL) {
141 tr_crit("Unable to create configuration watcher object, exiting.");
144 tr->cfgwatch->config_dir=opts.config_dir;
145 tr->cfgwatch->cfg_mgr=tr->cfg_mgr;
146 if (0 != tr_read_and_apply_config(tr->cfgwatch)) {
147 tr_crit("Error reading configuration, exiting.");
151 /***** initialize the trust path query server instance *****/
152 if (0 == (tr->tids = tids_create (tr))) {
153 tr_crit("Error initializing Trust Path Query Server instance.");
157 /***** initialize the trust router protocol server instance *****/
158 if (0 == (tr->trps = trps_create (tr))) {
159 tr_crit("Error initializing Trust Router Protocol Server instance.");
163 /***** Set up the event loop *****/
164 ev_base=tr_event_loop_init(); /* Set up the event loop */
166 tr_crit("Error initializing event loop.");
170 /* install configuration file watching events */
171 tr->cfgwatch->poll_interval=(struct timeval) {1,0}; /* set poll interval in {sec, usec} */
172 tr->cfgwatch->settling_time=(struct timeval) {5,0}; /* delay for changes to settle before updating */
173 /* TODO: pull these settings out of the configuration files */
175 /* already set config_dir, fstat_list and n_files earlier */
176 if (0 != tr_cfgwatch_event_init(ev_base, tr->cfgwatch, &cfgwatch_ev)) {
177 tr_crit("Error initializing configuration file watcher.");
181 /*tr_status_event_init();*/ /* install status reporting events */
183 /* install TID server events */
184 if (0 != tr_tids_event_init(ev_base,
188 tr_crit("Error initializing Trust Path Query Server instance.");
192 /* install TRP handler events */
193 if (0 != tr_trps_event_init(ev_base,
197 tr_crit("Error initializing Trust Path Query Server instance.");
201 tr_event_loop_run(ev_base); /* does not return until we are done */
203 /* TODO: ensure talloc is properly used so this actually works */
204 tr_destroy(tr); /* thanks to talloc, should destroy everything */
206 talloc_free(main_ctx);