Add TRP handling events, plus change to cfg layout.
[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 <jansson.h>
38 #include <argp.h>
39 #include <event2/event.h>
40 #include <talloc.h>
41 #include <sys/time.h>
42
43 #include <tr.h>
44 #include <tid_internal.h>
45 #include <tr_tid.h>
46 #include <tr_trp.h>
47 #include <tr_config.h>
48 #include <tr_event.h>
49 #include <tr_cfgwatch.h>
50 #include <tr_debug.h>
51
52 /***** command-line option handling / setup *****/
53
54 /* Strip trailing / from a path name.*/
55 static void remove_trailing_slash(char *s) {
56   size_t n;
57
58   n=strlen(s);
59   if(s[n-1]=='/') {
60     s[n-1]='\0';
61   }
62 }
63
64 /* argp global parameters */
65 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
66
67 /* doc strings */
68 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router";
69 static const char arg_doc[]=""; /* string describing arguments, if any */
70
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)"},
75     { NULL }
76 };
77
78 /* structure for communicating with option parser */
79 struct cmdline_args {
80   char *config_dir;
81 };
82
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)
85 {
86   /* get a shorthand to the command line argument structure, part of state */
87   struct cmdline_args *arguments=state->input;
88
89   switch (key) {
90   case 'c':
91     if (arg == NULL) {
92       /* somehow we got called without an argument */
93       return ARGP_ERR_UNKNOWN;
94     }
95     arguments->config_dir=arg;
96     break;
97
98   default:
99     return ARGP_ERR_UNKNOWN;
100   }
101
102   return 0; /* success */
103 }
104
105 /* assemble the argp parser */
106 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
107
108
109 int main (int argc, char *argv[])
110 {
111   TALLOC_CTX *main_ctx=talloc_new(NULL);
112
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;
118
119   /* Use standalone logging */
120   tr_log_open();
121
122   /***** parse command-line arguments *****/
123   /* set defaults */
124   opts.config_dir=".";
125
126   /* parse the command line*/
127   argp_parse(&argp, argc, argv, 0, 0, &opts);
128
129   /* process options */
130   remove_trailing_slash(opts.config_dir);
131
132   /***** create a Trust Router instance *****/
133   if (NULL == (tr = tr_create(main_ctx))) {
134     tr_crit("Unable to create Trust Router instance, exiting.");
135     return 1;
136   }
137
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.");
142     return 1;
143   }
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.");
148     return 1;
149   }
150
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.");
154     return 1;
155   }
156
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.");
160     return 1;
161   }
162
163   /***** Set up the event loop *****/
164   ev_base=tr_event_loop_init(); /* Set up the event loop */
165   if (ev_base==NULL) {
166     tr_crit("Error initializing event loop.");
167     return 1;
168   }
169
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 */
174
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.");
178     return 1;
179   }
180
181   /*tr_status_event_init();*/ /* install status reporting events */
182
183   /* install TID server events */
184   if (0 != tr_tids_event_init(ev_base,
185                               tr->tids,
186                               tr->cfg_mgr,
187                              &tids_ev)) {
188     tr_crit("Error initializing Trust Path Query Server instance.");
189     return 1;
190   }
191
192   /* install TRP handler events */
193   if (0 != tr_trps_event_init(ev_base,
194                               tr->trps,
195                               tr->cfg_mgr,
196                              &trps_ev)) {
197     tr_crit("Error initializing Trust Path Query Server instance.");
198     return 1;
199   }
200
201   tr_event_loop_run(ev_base); /* does not return until we are done */
202
203   /* TODO: ensure talloc is properly used so this actually works */
204   tr_destroy(tr); /* thanks to talloc, should destroy everything */
205
206   talloc_free(main_ctx);
207   return 0;
208 }