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>
45 #include <tid_internal.h>
48 #include <tr_config.h>
50 #include <tr_cfgwatch.h>
54 #define TALLOC_DEBUG_ENABLE 1
55 #define DEBUG_HARDCODED_PEER_TABLE 1
56 #define DEBUG_PING_SELF 0
58 /***** command-line option handling / setup *****/
60 /* Strip trailing / from a path name.*/
61 static void remove_trailing_slash(char *s) {
70 /* argp global parameters */
71 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
74 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router";
75 static const char arg_doc[]=""; /* string describing arguments, if any */
77 /* define the options here. Fields are:
78 * { long-name, short-name, variable name, options, help description } */
79 static const struct argp_option cmdline_options[] = {
80 { "config-dir", 'c', "DIR", 0, "Specify configuration file location (default is current directory)"},
84 /* structure for communicating with option parser */
89 /* parser for individual options - fills in a struct cmdline_args */
90 static error_t parse_option(int key, char *arg, struct argp_state *state)
92 /* get a shorthand to the command line argument structure, part of state */
93 struct cmdline_args *arguments=state->input;
98 /* somehow we got called without an argument */
99 return ARGP_ERR_UNKNOWN;
101 arguments->config_dir=arg;
105 return ARGP_ERR_UNKNOWN;
108 return 0; /* success */
111 /* assemble the argp parser */
112 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
115 /***** talloc error handling *****/
116 /* called when talloc tries to abort */
117 static void tr_abort(const char *reason)
119 tr_crit("tr_abort: Critical error, talloc aborted. Reason: %s", reason);
123 #if TALLOC_DEBUG_ENABLE
124 static void tr_talloc_log(const char *msg)
126 tr_debug("talloc: %s", msg);
128 #endif /* TALLOC_DEBUG_ENABLE */
137 static void debug_ping(evutil_socket_t fd, short what, void *arg)
139 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
140 struct thingy *thingy=(struct thingy *)arg;
141 TRPS_INSTANCE *trps=thingy->trps;
145 struct timeval interval={1, 0};
149 tr_debug("debug_ping entered");
150 if (trps->trpc==NULL)
151 tr_trpc_initiate(trps, trps->hostname, trps->port);
153 /* create a TRP route request msg */
154 req=trp_req_new(tmp_ctx);
155 name=tr_new_name("community");
156 trp_req_set_comm(req, name);
157 name=tr_new_name("realm");
158 trp_req_set_realm(req, name);
159 tr_msg_set_trp_req(&msg, req);
160 encoded=tr_msg_encode(&msg);
162 tr_err("debug_ping: error encoding TRP message.");
164 tr_debug("debug_ping: sending message");
165 trps_send_msg(trps, NULL, encoded);
166 tr_msg_free_encoded(encoded);
169 evtimer_add(thingy->ev, &interval);
171 #endif /* DEBUG_PING_SELF */
173 static void configure_signals(void)
177 sigemptyset(&signals);
178 sigaddset(&signals, SIGPIPE);
179 pthread_sigmask(SIG_BLOCK, &signals, NULL);
182 int main(int argc, char *argv[])
184 TALLOC_CTX *main_ctx=NULL;
186 TR_INSTANCE *tr = NULL;
187 struct cmdline_args opts;
188 struct event_base *ev_base;
189 struct tr_socket_event tids_ev;
190 struct event *cfgwatch_ev;
193 struct event *debug_ping_ev;
194 struct timeval notime={0, 0};
195 struct thingy thingy={NULL};
196 #endif /* DEBUG_PING_SELF */
200 /* we're going to be multithreaded, so disable null context tracking */
201 talloc_set_abort_fn(tr_abort);
202 talloc_disable_null_tracking();
203 #if TALLOC_DEBUG_ENABLE
204 talloc_set_log_fn(tr_talloc_log);
205 #endif /* TALLOC_DEBUG_ENABLE */
206 main_ctx=talloc_new(NULL);
208 /* Use standalone logging */
211 /***** parse command-line arguments *****/
215 /* parse the command line*/
216 argp_parse(&argp, argc, argv, 0, 0, &opts);
218 /* process options */
219 remove_trailing_slash(opts.config_dir);
221 /***** create a Trust Router instance *****/
222 if (NULL == (tr = tr_create(main_ctx))) {
223 tr_crit("Unable to create Trust Router instance, exiting.");
227 /***** initialize the trust path query server instance *****/
228 if (NULL == (tr->tids = tids_create (tr))) {
229 tr_crit("Error initializing Trust Path Query Server instance.");
233 /***** initialize the trust router protocol server instance *****/
234 if (NULL == (tr->trps = trps_new(tr))) {
235 tr_crit("Error initializing Trust Router Protocol Server instance.");
239 /***** process configuration *****/
240 tr->cfgwatch=tr_cfgwatch_create(tr);
241 if (tr->cfgwatch == NULL) {
242 tr_crit("Unable to create configuration watcher object, exiting.");
245 tr->cfgwatch->config_dir=opts.config_dir;
246 tr->cfgwatch->cfg_mgr=tr->cfg_mgr;
247 tr->cfgwatch->update_cb=tr_config_changed; /* handle configuration changes */
248 tr->cfgwatch->update_cookie=(void *)tr;
249 if (0 != tr_read_and_apply_config(tr->cfgwatch)) {
250 tr_crit("Error reading configuration, exiting.");
254 /***** Set up the event loop *****/
255 ev_base=tr_event_loop_init(); /* Set up the event loop */
257 tr_crit("Error initializing event loop.");
261 /* already set config_dir, fstat_list and n_files earlier */
262 if (0 != tr_cfgwatch_event_init(ev_base, tr->cfgwatch, &cfgwatch_ev)) {
263 tr_crit("Error initializing configuration file watcher.");
267 /*tr_status_event_init();*/ /* install status reporting events */
269 /* install TID server events */
270 if (0 != tr_tids_event_init(ev_base,
274 tr_crit("Error initializing Trust Path Query Server instance.");
278 /* install TRP handler events */
279 if (TRP_SUCCESS != tr_trps_event_init(ev_base, tr)) {
280 tr_crit("Error initializing Trust Path Query Server instance.");
284 #if DEBUG_HARDCODED_PEER_TABLE
286 TRP_PEER *hc_peer=NULL;
289 hc_peer=trp_peer_new(main_ctx); /* will later be stolen by ptable context */
291 tr_crit("Unable to allocate new peer. Aborting.");
294 trp_peer_set_server(hc_peer, "epsilon.vmnet");
295 trp_peer_set_gssname(hc_peer, tr_new_name("tr-epsilon-vmnet@apc.painless-security.com"));
296 trp_peer_set_conn_status_cb(hc_peer, tr_peer_status_change, (void *)(tr->trps));
297 switch (tr->trps->port) {
299 trp_peer_set_port(hc_peer, 10001);
302 trp_peer_set_port(hc_peer, 10000);
305 tr_crit("Cannot use hardcoded peer table with port other than 10000 or 10001.");
308 if (TRP_SUCCESS != trps_add_peer(tr->trps, hc_peer)) {
309 tr_crit("Unable to add peer.");
313 hc_peer=trp_peer_new(main_ctx); /* will later be stolen by ptable context */
315 tr_crit("Unable to allocate new peer. Aborting.");
318 trp_peer_set_server(hc_peer, "epsilon.vmnet");
319 trp_peer_set_gssname(hc_peer, tr_new_name("trpc@apc.painless-security.com"));
320 trp_peer_set_port(hc_peer, 10002); /* not really used */
321 if (TRP_SUCCESS != trps_add_peer(tr->trps, hc_peer)) {
322 tr_crit("Unable to add peer.");
326 s=trp_ptable_to_str(main_ctx, tr->trps->ptable, NULL, NULL);
327 tr_debug("Peer Table:\n%s\n", s);
330 #endif /* DEBUG_HARDCODED_PEER_TABLE */
333 /* for debugging, send a message to peers on a timer */
334 debug_ping_ev=evtimer_new(ev_base, debug_ping, (void *)&thingy);
335 thingy.trps=tr->trps;
336 thingy.ev=debug_ping_ev;
337 evtimer_add(debug_ping_ev, ¬ime);
338 #endif /* DEBUG_PING_SELF */
340 tr_event_loop_run(ev_base); /* does not return until we are done */
342 /* TODO: ensure talloc is properly used so this actually works */
343 tr_destroy(tr); /* thanks to talloc, should destroy everything */
345 talloc_free(main_ctx);