2 * Copyright (c) 2012, 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.
38 #include <trust_router/tr_dh.h>
39 #include <trust_router/tid.h>
45 TIDC_INSTANCE *tidc_create ()
47 TIDC_INSTANCE *tidc = NULL;
49 if (tidc = malloc(sizeof(TIDC_INSTANCE)))
50 memset(tidc, 0, sizeof(TIDC_INSTANCE));
57 void tidc_destroy (TIDC_INSTANCE *tidc)
63 int tidc_open_connection (TIDC_INSTANCE *tidc,
71 fprintf(stderr, "tidc_open_connection: Opening GSS connection to %s:%u.", server, port);
72 err = gsscon_connect(server, port, "trustidentity", &conn, gssctx);
80 int tidc_send_request (TIDC_INSTANCE *tidc,
86 TIDC_RESP_FUNC *resp_handler,
90 TID_REQ *tid_req = NULL;
92 /* Create and populate a TID req structure */
93 if (!(tid_req = malloc(sizeof(TID_REQ))))
96 memset(tid_req, 0, sizeof(TID_REQ));
99 tid_req->gssctx = gssctx;
101 if ((NULL == (tid_req->rp_realm = tr_new_name(rp_realm))) ||
102 (NULL == (tid_req->realm = tr_new_name(realm))) ||
103 (NULL == (tid_req->comm = tr_new_name(comm)))) {
104 fprintf (stderr, "tidc_send_request: Error duplicating names.\n");
108 tid_req->tidc_dh = tidc->client_dh;
110 return (tidc_fwd_request(tidc, tid_req, resp_handler, cookie));
113 int tidc_fwd_request (TIDC_INSTANCE *tidc,
115 TIDC_RESP_FUNC *resp_handler,
119 char *req_buf = NULL;
120 char *resp_buf = NULL;
121 size_t resp_buflen = 0;
123 TR_MSG *resp_msg = NULL;
126 /* Create and populate a TID msg structure */
127 if (!(msg = malloc(sizeof(TR_MSG))))
130 msg->msg_type = TID_REQUEST;
131 msg->tid_req = tid_req;
133 /* store the response function and cookie */
134 // tid_req->resp_func = resp_handler;
135 // tid_req->cookie = cookie;
138 /* Encode the request into a json string */
139 if (!(req_buf = tr_msg_encode(msg))) {
140 fprintf(stderr, "tidc_fwd_request: Error encoding TID request.\n");
144 fprintf (stderr, "tidc_fwd_request: Sending TID request:\n");
145 fprintf (stderr, "%s\n", req_buf);
147 /* Send the request over the connection */
148 if (err = gsscon_write_encrypted_token (tid_req->conn, tid_req->gssctx, req_buf,
150 fprintf(stderr, "tidc_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(tid_req->conn, tid_req->gssctx, &resp_buf, &resp_buflen)) {
164 fprintf(stdout, "tidc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
165 fprintf(stdout, "%s\n", resp_buf);
167 if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
168 fprintf(stderr, "tidc_fwd_request: Error decoding response.\n");
172 /* TBD -- Check if this is actually a valid response */
173 if (!resp_msg->tid_resp) {
174 fprintf(stderr, "tidc_fwd_request: Error, no response in the response!\n");
179 /* Call the caller's response function */
180 (*resp_handler)(tidc, tid_req, resp_msg->tid_resp, cookie);
182 fprintf(stderr, "tidc_fwd_request: NULL response function.\n");
193 /* TBD -- free the decoded response */