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 #define TALLOC_DEBUG_ENABLE 1
54 /***** command-line option handling / setup *****/
56 /* Strip trailing / from a path name.*/
57 static void remove_trailing_slash(char *s) {
66 /* argp global parameters */
67 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
70 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router";
71 static const char arg_doc[]=""; /* string describing arguments, if any */
73 /* define the options here. Fields are:
74 * { long-name, short-name, variable name, options, help description } */
75 static const struct argp_option cmdline_options[] = {
76 { "config-dir", 'c', "DIR", 0, "Specify configuration file location (default is current directory)"},
80 /* structure for communicating with option parser */
85 /* parser for individual options - fills in a struct cmdline_args */
86 static error_t parse_option(int key, char *arg, struct argp_state *state)
88 /* get a shorthand to the command line argument structure, part of state */
89 struct cmdline_args *arguments=state->input;
94 /* somehow we got called without an argument */
95 return ARGP_ERR_UNKNOWN;
97 arguments->config_dir=arg;
101 return ARGP_ERR_UNKNOWN;
104 return 0; /* success */
107 /* assemble the argp parser */
108 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
111 /***** talloc error handling *****/
112 /* called when talloc tries to abort */
113 static void tr_abort(const char *reason)
115 tr_crit("tr_abort: Critical error, talloc aborted. Reason: %s", reason);
119 #if TALLOC_DEBUG_ENABLE
120 static void tr_talloc_log(const char *msg)
122 tr_debug("talloc: %s", msg);
124 #endif /* TALLOC_DEBUG_ENABLE */
127 static void debug_ping(evutil_socket_t fd, short what, void *arg)
129 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
130 TRPS_INSTANCE *trps=talloc_get_type_abort(arg, TRPS_INSTANCE);
135 tr_debug("debug_ping entered, trps=%p, trps->trpc=%p", trps, trps->trpc);
136 if (trps->trpc==NULL)
137 tr_trpc_initiate(trps, trps->hostname, trps->port);
139 /* create a TRP route request msg */
140 req=trp_req_new(tmp_ctx);
141 tr_msg_set_trp_req(&msg, req);
142 encoded=tr_msg_encode(&msg);
144 tr_err("debug_ping: error encoding TRP message.");
146 tr_debug("debug_ping: sending message");
147 trps_send_msg(trps, NULL, encoded);
148 tr_msg_free_encoded(encoded);
152 int main(int argc, char *argv[])
154 TALLOC_CTX *main_ctx=NULL;
156 TR_INSTANCE *tr = NULL;
157 struct cmdline_args opts;
158 struct event_base *ev_base;
159 struct tr_socket_event tids_ev;
160 TR_TRPS_EVENTS *trps_ev;
161 struct event *cfgwatch_ev;
162 struct event *debug_ping_ev;
163 struct timeval debug_ping_interval={1, 0};
165 /* we're going to be multithreaded, so disable null context tracking */
166 talloc_set_abort_fn(tr_abort);
167 talloc_disable_null_tracking();
168 #if TALLOC_DEBUG_ENABLE
169 talloc_set_log_fn(tr_talloc_log);
170 #endif /* TALLOC_DEBUG_ENABLE */
171 main_ctx=talloc_new(NULL);
173 /* Use standalone logging */
176 /***** parse command-line arguments *****/
180 /* parse the command line*/
181 argp_parse(&argp, argc, argv, 0, 0, &opts);
183 /* process options */
184 remove_trailing_slash(opts.config_dir);
186 /***** create a Trust Router instance *****/
187 if (NULL == (tr = tr_create(main_ctx))) {
188 tr_crit("Unable to create Trust Router instance, exiting.");
192 /***** process configuration *****/
193 tr->cfgwatch=tr_cfgwatch_create(tr);
194 if (tr->cfgwatch == NULL) {
195 tr_crit("Unable to create configuration watcher object, exiting.");
198 tr->cfgwatch->config_dir=opts.config_dir;
199 tr->cfgwatch->cfg_mgr=tr->cfg_mgr;
200 if (0 != tr_read_and_apply_config(tr->cfgwatch)) {
201 tr_crit("Error reading configuration, exiting.");
205 /***** initialize the trust path query server instance *****/
206 if (NULL == (tr->tids = tids_create (tr))) {
207 tr_crit("Error initializing Trust Path Query Server instance.");
211 /***** initialize the trust router protocol server instance *****/
212 if (NULL == (tr->trps = trps_new(tr))) {
213 tr_crit("Error initializing Trust Router Protocol Server instance.");
217 /***** Set up the event loop *****/
218 ev_base=tr_event_loop_init(); /* Set up the event loop */
220 tr_crit("Error initializing event loop.");
224 /* install configuration file watching events */
225 tr->cfgwatch->poll_interval=(struct timeval) {1,0}; /* set poll interval in {sec, usec} */
226 tr->cfgwatch->settling_time=(struct timeval) {5,0}; /* delay for changes to settle before updating */
227 /* TODO: pull these settings out of the configuration files */
229 /* already set config_dir, fstat_list and n_files earlier */
230 if (0 != tr_cfgwatch_event_init(ev_base, tr->cfgwatch, &cfgwatch_ev)) {
231 tr_crit("Error initializing configuration file watcher.");
235 /*tr_status_event_init();*/ /* install status reporting events */
237 /* install TID server events */
238 if (0 != tr_tids_event_init(ev_base,
242 tr_crit("Error initializing Trust Path Query Server instance.");
246 /* install TRP handler events */
247 trps_ev=tr_trps_events_new(main_ctx);
248 if (0 != tr_trps_event_init(ev_base,
252 tr_crit("Error initializing Trust Path Query Server instance.");
256 /* for debugging, send a message to peers on a timer */
257 debug_ping_ev=evtimer_new(ev_base, debug_ping, (void *)(tr->trps));
258 evtimer_add(debug_ping_ev, &debug_ping_interval);
260 tr_event_loop_run(ev_base); /* does not return until we are done */
262 /* TODO: ensure talloc is properly used so this actually works */
263 tr_destroy(tr); /* thanks to talloc, should destroy everything */
265 talloc_free(main_ctx);