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>
46 #include <tr_config.h>
48 #include <tr_cfgwatch.h>
51 /***** command-line option handling / setup *****/
53 /* Strip trailing / from a path name.*/
54 static void remove_trailing_slash(char *s) {
63 /* argp global parameters */
64 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
67 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router";
68 static const char arg_doc[]=""; /* string describing arguments, if any */
70 /* define the options here. Fields are:
71 * { long-name, short-name, variable name, options, help description } */
72 static const struct argp_option cmdline_options[] = {
73 { "config-dir", 'c', "DIR", 0, "Specify configuration file location (default is current directory)"},
77 /* structure for communicating with option parser */
82 /* parser for individual options - fills in a struct cmdline_args */
83 static error_t parse_option(int key, char *arg, struct argp_state *state)
85 /* get a shorthand to the command line argument structure, part of state */
86 struct cmdline_args *arguments=state->input;
91 /* somehow we got called without an argument */
92 return ARGP_ERR_UNKNOWN;
94 arguments->config_dir=arg;
98 return ARGP_ERR_UNKNOWN;
101 return 0; /* success */
104 /* assemble the argp parser */
105 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
108 int main (int argc, char *argv[])
110 TALLOC_CTX *main_ctx=talloc_new(NULL);
112 TR_INSTANCE *tr = NULL;
113 struct cmdline_args opts;
114 struct event_base *ev_base;
115 struct tr_socket_event tids_ev;
116 struct event *cfgwatch_ev;
117 TR_CFGWATCH *cfgwatch; /* config file watcher status */
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 /* Get a configuration status object */
133 cfgwatch=tr_cfgwatch_create(main_ctx);
134 if (cfgwatch == NULL) {
135 tr_crit("Unable to create configuration watcher object, exiting.");
139 /***** create a Trust Router instance *****/
140 if (NULL == (tr = tr_create())) {
141 tr_crit("Unable to create Trust Router instance, exiting.");
145 /***** process configuration *****/
146 cfgwatch->config_dir=opts.config_dir;
147 cfgwatch->ctx=main_ctx;
149 if (0 != tr_read_and_apply_config(cfgwatch)) {
150 tr_crit("Error reading configuration, exiting.");
154 /***** initialize the trust path query server instance *****/
155 if (0 == (tr->tids = tids_create ())) {
156 tr_crit("Error initializing Trust Path Query Server instance.");
160 /***** Set up the event loop *****/
161 ev_base=tr_event_loop_init(); /* Set up the event loop */
163 tr_crit("Error initializing event loop.");
167 /* install configuration file watching events */
168 cfgwatch->poll_interval=(struct timeval) {1,0}; /* set poll interval in {sec, usec} */
169 cfgwatch->settling_time=(struct timeval) {5,0}; /* delay for changes to settle before updating */
171 /* already set config_dir, fstat_list and n_files earlier */
172 if (0 != tr_cfgwatch_event_init(ev_base, cfgwatch, &cfgwatch_ev)) {
173 tr_crit("Error initializing configuration file watcher.");
177 /*tr_status_event_init();*/ /* install status reporting events */
179 /* install TID server events */
180 if (0 != tr_tids_event_init(ev_base, tr, &tids_ev)) {
181 tr_crit("Error initializing Trust Path Query Server instance.");
185 /*tr_trp_event_init();*/ /* install TRP handler events */
187 fflush(stdout); fflush(stderr);
188 tr_event_loop_run(ev_base); /* does not return until we are done */
190 /* TODO: update the cleanup code */
191 tids_destroy(tr->tids);
194 talloc_free(main_ctx);