Checkpoint commit: refactoring the request code in TIDS for better reuse
[trust_router.git] / tr / tr_main.c
1 /*
2  * Copyright (c) 2012, 2015, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <argp.h>
38 #include <event2/event.h>
39 #include <talloc.h>
40 #include <signal.h>
41
42 #include <tid_internal.h>
43 #include <tr_tid.h>
44 #include <tr_trp.h>
45 #include <tr_config.h>
46 #include <tr_event.h>
47 #include <tr_cfgwatch.h>
48 #include <tr.h>
49 #include <tr_debug.h>
50
51 #define TALLOC_DEBUG_ENABLE 1
52
53 /***** command-line option handling / setup *****/
54
55 static void print_version_info(void)
56 {
57   printf("Moonshot Trust Router %s\n\n", PACKAGE_VERSION);
58 }
59
60 /* Strip trailing / from a path name.*/
61 static void remove_trailing_slash(char *s) {
62   size_t n;
63
64   n=strlen(s);
65   if(s[n-1]=='/') {
66     s[n-1]='\0';
67   }
68 }
69
70 /* argp global parameters */
71 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
72
73 /* doc strings */
74 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router " PACKAGE_VERSION;
75 static const char arg_doc[]=""; /* string describing arguments, if any */
76
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)"},
81     { "version", 'v', NULL, 0, "Print version information and exit"},
82     { NULL }
83 };
84
85 /* structure for communicating with option parser */
86 struct cmdline_args {
87     int version_requested;
88     char *config_dir;
89 };
90
91 /* parser for individual options - fills in a struct cmdline_args */
92 static error_t parse_option(int key, char *arg, struct argp_state *state)
93 {
94   /* get a shorthand to the command line argument structure, part of state */
95   struct cmdline_args *arguments=state->input;
96
97   switch (key) {
98     case 'c':
99       if (arg == NULL) {
100         /* somehow we got called without an argument */
101         return ARGP_ERR_UNKNOWN;
102       }
103       arguments->config_dir=arg;
104       break;
105
106     case 'v':
107       arguments->version_requested=1;
108       break;
109
110     default:
111       return ARGP_ERR_UNKNOWN;
112   }
113
114   return 0; /* success */
115 }
116
117 /* assemble the argp parser */
118 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
119
120
121 /***** talloc error handling *****/
122 /* called when talloc tries to abort */
123 static void tr_abort(const char *reason)
124 {
125   tr_crit("tr_abort: Critical error, talloc aborted. Reason: %s", reason);
126   abort();
127 }
128
129 #if TALLOC_DEBUG_ENABLE
130 static void tr_talloc_log(const char *msg)
131 {
132   tr_debug("talloc: %s", msg);
133 }
134 #endif /* TALLOC_DEBUG_ENABLE */
135
136 static void configure_signals(void)
137 {
138   sigset_t signals;
139   /* ignore SIGPIPE */
140   sigemptyset(&signals);
141   sigaddset(&signals, SIGPIPE);
142   pthread_sigmask(SIG_BLOCK, &signals, NULL);
143 }
144
145 int main(int argc, char *argv[])
146 {
147   TALLOC_CTX *main_ctx=NULL;
148
149   TR_INSTANCE *tr = NULL;
150   struct cmdline_args opts;
151   struct event_base *ev_base;
152   struct tr_socket_event tids_ev = {0};
153 //  struct tr_socket_event mon_ev = {0};
154   struct event *cfgwatch_ev;
155
156   configure_signals();
157
158   /* we're going to be multithreaded, so disable null context tracking */
159   talloc_set_abort_fn(tr_abort);
160   talloc_disable_null_tracking();
161 #if TALLOC_DEBUG_ENABLE
162   talloc_set_log_fn(tr_talloc_log);
163 #endif /* TALLOC_DEBUG_ENABLE */
164   main_ctx=talloc_new(NULL);
165
166   /* Use standalone logging */
167   tr_log_open();
168
169   /***** parse command-line arguments *****/
170   /* set defaults */
171   opts.version_requested=0;
172   opts.config_dir=".";
173
174   /* parse the command line*/
175   argp_parse(&argp, argc, argv, 0, 0, &opts);
176
177   /* process options */
178   remove_trailing_slash(opts.config_dir);
179
180
181   /***** Print version info *****/
182   print_version_info();
183   if (opts.version_requested)
184     return 0; /* requested that we print version and exit */
185
186   /***** create a Trust Router instance *****/
187   if (NULL == (tr = tr_create(main_ctx))) {
188     tr_crit("Unable to create Trust Router instance, exiting.");
189     return 1;
190   }
191
192   /***** initialize the trust path query server instance *****/
193   if (NULL == (tr->tids = tids_new(tr))) {
194     tr_crit("Error initializing Trust Path Query Server instance.");
195     return 1;
196   }
197
198   /***** initialize the trust router protocol server instance *****/
199   if (NULL == (tr->trps = trps_new(tr))) {
200     tr_crit("Error initializing Trust Router Protocol Server instance.");
201     return 1;
202   }
203
204 //  /***** initialize the monitoring interface instance *****/
205 //  if (NULL == (tr->mons = mons_new(tr))) {
206 //    tr_crit("Error initializing monitoring interface instance.");
207 //    return 1;
208 //  }
209
210   /***** process configuration *****/
211   tr->cfgwatch=tr_cfgwatch_create(tr);
212   if (tr->cfgwatch == NULL) {
213     tr_crit("Unable to create configuration watcher object, exiting.");
214     return 1;
215   }
216   tr->cfgwatch->config_dir=opts.config_dir;
217   tr->cfgwatch->cfg_mgr=tr->cfg_mgr;
218   tr->cfgwatch->update_cb=tr_config_changed; /* handle configuration changes */
219   tr->cfgwatch->update_cookie=(void *)tr;
220   if (0 != tr_read_and_apply_config(tr->cfgwatch)) {
221     tr_crit("Error reading configuration, exiting.");
222     return 1;
223   }
224
225   /***** Set up the event loop *****/
226   ev_base=tr_event_loop_init(); /* Set up the event loop */
227   if (ev_base==NULL) {
228     tr_crit("Error initializing event loop.");
229     return 1;
230   }
231
232   /* already set config_dir, fstat_list and n_files earlier */
233   if (0 != tr_cfgwatch_event_init(ev_base, tr->cfgwatch, &cfgwatch_ev)) {
234     tr_crit("Error initializing configuration file watcher.");
235     return 1;
236   }
237
238 //  /* install monitoring interface events */
239 //  tr_debug("Initializing monitoring interface events.");
240 //  if (0 != tr_mon_event_init(tr->mons, ev_base, &mon_ev)) {
241 //    tr_crit("Error initializing monitoring interface.");
242 //    return 1;
243 //  }
244
245   /* install TID server events */
246   tr_debug("Initializing TID server events.");
247   if (0 != tr_tids_event_init(ev_base,
248                               tr->tids,
249                               tr->cfg_mgr,
250                               tr->trps,
251                              &tids_ev)) {
252     tr_crit("Error initializing Trust Path Query Server instance.");
253     return 1;
254   }
255
256   /* install TRP handler events */
257   tr_debug("Initializing Dynamic Trust Router Protocol events.");
258   if (TRP_SUCCESS != tr_trps_event_init(ev_base, tr)) {
259     tr_crit("Error initializing Trust Path Query Server instance.");
260     return 1;
261   }
262
263   tr_debug("Starting event loop.");
264   tr_event_loop_run(ev_base); /* does not return until we are done */
265
266   tr_destroy(tr); /* thanks to talloc, should destroy everything */
267
268   talloc_free(main_ctx);
269   return 0;
270 }