2 * Copyright (c) 2012, 2014-2018, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
39 #include <trust_router/tr_dh.h>
40 #include <mon_internal.h>
46 static int monc_destructor(void *obj)
48 MONC_INSTANCE *monc=talloc_get_type_abort(obj, MONC_INSTANCE);
50 if (NULL!=monc->client_dh)
51 tr_destroy_dh_params(monc->client_dh);
56 /* creates struct in talloc null context */
57 MONC_INSTANCE *monc_create(void)
59 MONC_INSTANCE *monc=talloc(NULL, MONC_INSTANCE);
62 talloc_set_destructor((void *)monc, monc_destructor);
67 void monc_destroy(MONC_INSTANCE *monc)
72 int monc_open_connection (MONC_INSTANCE *monc,
80 tr_debug("monc_open_connection: opening monc connection to %s:%d", server, port);
81 err = gsscon_connect(server, port, "trustmonitor", &conn, gssctx);
89 int monc_send_request (MONC_INSTANCE *monc,
92 MONC_RESP_FUNC *resp_handler,
95 MON_REQ *mon_req = NULL;
98 /* Create and populate a MON req structure */
99 if (!(mon_req = mon_req_new(NULL, MON_CMD_SHOW))) // TODO accept command as a parameter
102 rc = monc_fwd_request(monc, conn, gssctx, mon_req, resp_handler, cookie);
107 mon_req_free(mon_req);
111 int monc_fwd_request(MONC_INSTANCE *monc,
115 MONC_RESP_FUNC *resp_handler,
118 char *req_buf = NULL;
119 char *resp_buf = NULL;
120 size_t resp_buflen = 0;
122 TR_MSG *resp_msg = NULL;
126 /* Create and populate a MON msg structure */
127 if (!(msg = talloc_zero(mon_req, TR_MSG)))
130 msg->msg_type = MON_REQUEST;
131 tr_msg_set_mon_req(msg, mon_req);
133 /* store the response function and cookie */
134 // mon_req->resp_func = resp_handler;
135 // mon_req->cookie = cookie;
138 /* Encode the request into a json string */
139 if (!(req_buf = tr_msg_encode(NULL, msg))) {
140 tr_err("monc_fwd_request: Error encoding MON request.\n");
144 tr_debug( "monc_fwd_request: Sending MON request:\n");
145 tr_debug( "%s\n", req_buf);
147 /* Send the request over the connection */
148 err = gsscon_write_encrypted_token (conn, gssctx, req_buf, strlen(req_buf));
150 tr_err( "monc_fwd_request: Error sending request over connection.\n");
154 /* TBD -- queue request on instance, read resps in separate thread */
156 /* Read the response from the connection */
157 /* TBD -- timeout? */
158 if (err = gsscon_read_encrypted_token(conn, gssctx, &resp_buf, &resp_buflen)) {
164 tr_debug( "monc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
165 tr_debug( "%s\n", resp_buf);
167 // if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
168 // tr_err( "monc_fwd_request: Error decoding response.\n");
172 // /* TBD -- Check if this is actually a valid response */
173 // if (MON_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
174 // tr_err( "monc_fwd_request: Error, no response in the response!\n");
178 // if (resp_handler) {
179 // /* Call the caller's response function. It must copy any data it needs before returning. */
180 // tr_debug("monc_fwd_request: calling response callback function.");
181 // (*resp_handler)(monc, mon_req, tr_msg_get_resp(resp_msg), cookie);
196 tr_msg_free_decoded(resp_msg);
201 DH * monc_get_dh(MONC_INSTANCE *inst)
203 return inst->client_dh;
206 DH *monc_set_dh(MONC_INSTANCE *inst, DH *dh)
208 inst->client_dh = dh;