507bf34716057b1e06e3c531b6607af93d065db1
[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   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   /* get a shorthand to the command line argument structure, part of state */
75   struct cmdline_args *arguments=state->input;
76
77   switch (key) {
78   case 'r':
79     if (arg==NULL)
80       arguments->repeat=-1;
81     else
82       arguments->repeat=strtol(arg, NULL, 10);
83     break;
84
85   case ARGP_KEY_ARG: /* handle argument (not option) */
86     switch (state->arg_num) {
87     case 0:
88       arguments->msg=arg;
89       break;
90
91     case 1:
92       arguments->server=arg;
93       break;
94
95     case 2:
96       arguments->port=strtol(arg, NULL, 10); /* optional */
97       break;
98
99     default:
100       /* too many arguments */
101       argp_usage(state);
102     }
103     break;
104
105   case ARGP_KEY_END: /* no more arguments */
106     if (state->arg_num < 2) {
107       /* not enough arguments encountered */
108       argp_usage(state);
109     }
110     break;
111
112   default:
113     return ARGP_ERR_UNKNOWN;
114   }
115
116   return 0; /* success */
117 }
118
119
120 /* assemble the argp parser */
121 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
122
123 int main (int argc, 
124           char *argv[]) 
125 {
126   TALLOC_CTX *main_ctx=talloc_new(NULL);
127   MONC_INSTANCE *monc=NULL;
128   struct cmdline_args opts;
129   int rc;
130   int conn;
131   gss_ctx_id_t gssctx;
132
133   /* parse the command line*/
134   /* set defaults */
135   opts.msg=NULL;
136   opts.server=NULL;
137   opts.port=TRP_PORT;
138   opts.repeat=1;
139
140   argp_parse(&argp, argc, argv, 0, 0, &opts);
141
142   /* Use standalone logging */
143   tr_log_open();
144
145   /* set logging levels */
146   talloc_set_log_stderr();
147   tr_log_threshold(LOG_CRIT);
148   tr_console_threshold(LOG_DEBUG);
149
150   printf("TR Monitor:\nServer = %s, port = %i\n", opts.server, opts.port);
151
152   /* Create a MON client instance & the client DH */
153   monc = monc_create();
154   if (NULL == (monc->client_dh = tr_create_dh_params(main_ctx, 0))) {
155     printf("Error creating client DH params.\n");
156     return 1;
157   }
158
159   /* Set-up MON connection */
160   if (-1 == (conn = monc_open_connection(monc, opts.server, opts.port, &gssctx))) {
161     /* Handle error */
162     printf("Error in monc_open_connection.\n");
163     return 1;
164   };
165
166   /* Send a MON request */
167   if (0 > (rc = monc_send_request(monc, conn, gssctx, NULL, NULL))) {
168     /* Handle error */
169     printf("Error in monc_send_request, rc = %d.\n", rc);
170     return 1;
171   }
172
173   /* Clean-up the MON client instance, and exit */
174   monc_destroy(monc);
175   talloc_free(main_ctx);
176   return 0;
177 }
178