Further work on tids and monitoring, tids appears to work again
[trust_router.git] / include / mon_internal.h
1 /*
2  * Copyright (c) 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
36 #ifndef TRUST_ROUTER_MON_REQ_H
37 #define TRUST_ROUTER_MON_REQ_H
38
39 #include <talloc.h>
40 #include <stdint.h>
41 #include <jansson.h>
42 #include <gmodule.h>
43 #include <trp_internal.h>
44 #include <tr_gss_names.h>
45 #include <tr_name_internal.h>
46 #include <gssapi.h>
47
48 /* Typedefs */
49 typedef struct mon_req MON_REQ;
50 typedef struct mon_resp MON_RESP;
51
52 typedef enum mon_cmd MON_CMD;
53 typedef enum mon_resp_code MON_RESP_CODE;
54
55 typedef struct mon_opt MON_OPT;
56 typedef enum mon_opt_type MON_OPT_TYPE;
57
58 typedef enum mon_rc MON_RC;
59
60 typedef struct mons_instance MONS_INSTANCE;
61
62 typedef int (MONS_REQ_FUNC)(MONS_INSTANCE *, MON_REQ *, MON_RESP *, void *);
63 typedef int (MONS_AUTH_FUNC)(gss_name_t client_name, TR_NAME *display_name, void *cookie);
64
65 /* Struct and enum definitions */
66 enum mon_rc {
67   MON_SUCCESS=0,
68   MON_ERROR, /* generic error */
69   MON_BADARG, /* problem with the arguments */
70   MON_NOMEM, /* out of memory */
71   MON_NOPARSE, /* parsing failed */
72 };
73
74 enum mon_cmd {
75   MON_CMD_UNKNOWN=0,
76   MON_CMD_RECONFIGURE,
77   MON_CMD_SHOW
78 };
79
80 /* These should be explicitly numbered because they form part of the public API */
81 enum mon_resp_code {
82   MON_RESP_SUCCESS=0,
83   MON_RESP_ERROR=1, // generic error
84 };
85
86 enum mon_opt_type {
87   OPT_TYPE_UNKNOWN=0,
88
89   // System information
90   OPT_TYPE_SHOW_VERSION,
91   OPT_TYPE_SHOW_SERIAL,
92
93   // System statistics
94   OPT_TYPE_SHOW_UPTIME,
95   OPT_TYPE_SHOW_TID_REQ_COUNT,
96   OPT_TYPE_SHOW_TID_REQ_PENDING,
97
98   // Dynamic trust router state
99   OPT_TYPE_SHOW_ROUTES,
100   OPT_TYPE_SHOW_COMMUNITIES
101 };
102
103 struct mon_opt {
104   MON_OPT_TYPE type;
105 };
106
107 struct mon_req {
108   MON_CMD command;
109   GArray *options;
110 };
111
112 struct mon_resp {
113   MON_REQ *req; // request this responds to
114   MON_RESP_CODE code;
115   TR_NAME *message;
116   json_t *payload;
117 };
118
119 /* Monitoring server instance */
120 struct mons_instance {
121   const char *hostname;
122   unsigned int port;
123   TR_GSS_NAMES *authorized_gss_names;
124   TIDS_INSTANCE *tids;
125   TRPS_INSTANCE *trps;
126   MONS_REQ_FUNC *req_handler;
127   MONS_AUTH_FUNC *auth_handler;
128   void *cookie;
129 };
130
131 /* Prototypes */
132 /* tr_mon.c */
133 const char *mon_cmd_to_string(MON_CMD cmd);
134 MON_CMD mon_cmd_from_string(const char *s);
135 const char *mon_opt_type_to_string(MON_OPT_TYPE opt_type);
136 MON_OPT_TYPE mon_opt_type_from_string(const char *s);
137
138 /* mon_req.c */
139 MON_REQ *mon_req_new(TALLOC_CTX *mem_ctx, MON_CMD cmd);
140 void mon_req_free(MON_REQ *req);
141 MON_RC mon_req_add_option(MON_REQ *req, MON_OPT_TYPE opt_type);
142 size_t mon_req_opt_count(MON_REQ *req);
143 MON_OPT *mon_req_opt_index(MON_REQ *req, size_t index);
144
145 /* mon_req_encode.c */
146 json_t *mon_req_encode(MON_REQ *req);
147
148 /* mon_req_decode.c */
149 MON_REQ *mon_req_decode(TALLOC_CTX *mem_ctx, const char *req_json);
150
151 /* mon_resp.c */
152 MON_RESP *mon_resp_new(TALLOC_CTX *mem_ctx,
153                           MON_REQ *req,
154                           MON_RESP_CODE code,
155                           const char *msg,
156                           json_t *payload);
157 void mon_resp_free(MON_RESP *resp);
158
159 /* mon_resp_encode.c */
160 json_t *mon_resp_encode(MON_RESP *resp);
161
162 /* mons.c */
163 MONS_INSTANCE *mons_new(TALLOC_CTX *mem_ctx);
164 int mons_get_listener(MONS_INSTANCE *mons, MONS_REQ_FUNC *req_handler, MONS_AUTH_FUNC *auth_handler, const char *hostname,
165                       unsigned int port, void *cookie, int *fd_out, size_t max_fd);
166 int mons_accept(MONS_INSTANCE *mons, int listen);
167
168 #endif //TRUST_ROUTER_MON_REQ_H