Refactor tidc/monc to better share code
[trust_router.git] / tr / trmon_main.c
1 /*
2  * Copyright (c) 2012-2018, 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 <stdlib.h>
36 #include <stdio.h>
37 #include <talloc.h>
38 #include <argp.h>
39 #include <unistd.h>
40
41 #include <mon_internal.h>
42 #include <gsscon.h>
43 #include <tr_debug.h>
44 #include <trust_router/tr_dh.h>
45
46
47 /* command-line option setup */
48
49 /* argp global parameters */
50 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
51
52 /* doc strings */
53 static const char doc[]=PACKAGE_NAME " - TR Monitoring Client";
54 static const char arg_doc[]="<message> <server> [<port>]"; /* string describing arguments, if any */
55
56 /* define the options here. Fields are:
57  * { long-name, short-name, variable name, options, help description } */
58 static const struct argp_option cmdline_options[] = {
59   { "repeat", 'r', "N", OPTION_ARG_OPTIONAL, "Repeat message until terminated, or N times." },
60   {NULL}
61 };
62
63 /* structure for communicating with option parser */
64 struct cmdline_args {
65   char *msg;
66   char *server;
67   unsigned int port; /* optional */
68   int repeat; /* how many times to repeat, or -1 for infinite */
69 };
70
71 /* parser for individual options - fills in a struct cmdline_args */
72 static error_t parse_option(int key, char *arg, struct argp_state *state)
73 {
74   long tmp_l = 0;
75
76   /* get a shorthand to the command line argument structure, part of state */
77   struct cmdline_args *arguments=state->input;
78
79   switch (key) {
80     case 'r':
81       if (arg==NULL)
82         arguments->repeat=-1;
83       else
84         tmp_l = strtol(arg, NULL, 10);
85       if ((errno == 0) && (tmp_l > 0) && (tmp_l < INT_MAX))
86         arguments->repeat = (int) tmp_l;
87       else
88         argp_usage(state);
89       break;
90
91     case ARGP_KEY_ARG: /* handle argument (not option) */
92       switch (state->arg_num) {
93         case 0:
94           arguments->msg=arg;
95           break;
96
97         case 1:
98           arguments->server=arg;
99           break;
100
101         case 2:
102           tmp_l = strtol(arg, NULL, 10);
103           if (errno || (tmp_l < 0) || (tmp_l > 65535)) /* max valid port */
104             argp_usage(state);
105
106           arguments->port=(unsigned int) tmp_l;
107           break;
108
109         default:
110           /* too many arguments */
111           argp_usage(state);
112       }
113       break;
114
115     case ARGP_KEY_END: /* no more arguments */
116       if (state->arg_num < 2) {
117         /* not enough arguments encountered */
118         argp_usage(state);
119       }
120       break;
121
122     default:
123       return ARGP_ERR_UNKNOWN;
124   }
125
126   return 0; /* success */
127 }
128
129
130 /* assemble the argp parser */
131 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
132
133 int main(int argc, char *argv[])
134 {
135   TALLOC_CTX *main_ctx=talloc_new(NULL);
136   MONC_INSTANCE *monc = NULL;
137   MON_REQ *req = NULL;
138   MON_RESP *resp = NULL;
139
140   struct cmdline_args opts;
141   int retval=1; /* exit with an error status unless this gets set to zero */
142
143   /* parse the command line*/
144   /* set defaults */
145   opts.msg=NULL;
146   opts.server=NULL;
147   opts.port=TRP_PORT;
148   opts.repeat=1;
149
150   argp_parse(&argp, argc, argv, 0, 0, &opts);
151
152   /* Use standalone logging */
153   tr_log_open();
154
155   /* set logging levels */
156   talloc_set_log_stderr();
157   tr_log_threshold(LOG_CRIT);
158   tr_console_threshold(LOG_DEBUG);
159
160   printf("TR Monitor:\nServer = %s, port = %i\n", opts.server, opts.port);
161
162   /* Create a MON client instance & the client DH */
163   monc = monc_new(main_ctx);
164   if (monc == NULL) {
165     printf("Error allocating client instance.\n");
166     goto cleanup;
167   }
168
169
170   /* fill in the DH parameters */
171   monc_set_dh(monc, tr_create_dh_params(NULL, 0));
172   if (monc_get_dh(monc) == NULL) {
173     printf("Error creating client DH params.\n");
174     goto cleanup;
175   }
176
177   /* Set-up MON connection */
178   if (0 != monc_open_connection(monc, opts.server, opts.port)) {
179     /* Handle error */
180     printf("Error opening connection to %s:%d.\n", opts.server, opts.port);
181     goto cleanup;
182   };
183
184   req = mon_req_new(main_ctx, MON_CMD_SHOW);
185
186   /* Send a MON request and get the response */
187   resp = monc_send_request(main_ctx, monc, req);
188
189   if (resp == NULL) {
190     /* Handle error */
191     printf("Error executing monitoring request.\n");
192     goto cleanup;
193   }
194
195   /* success */
196   retval = 0;
197
198   /* Clean-up the MON client instance, and exit */
199 cleanup:
200   talloc_free(main_ctx);
201   return retval;
202 }
203