Refactor tidc/monc to better share code
[trust_router.git] / mon / monc.c
index 3e567ce..426b447 100644 (file)
 #include <tr_debug.h>
 
 
-static int monc_destructor(void *obj)
+MONC_INSTANCE *monc_new(TALLOC_CTX *mem_ctx)
 {
-  MONC_INSTANCE *monc=talloc_get_type_abort(obj, MONC_INSTANCE);
-  if (NULL!=monc) {
-    if (NULL!=monc->client_dh)
-      tr_destroy_dh_params(monc->client_dh);
-  }
-  return 0;
-}
-
-/* creates struct in talloc null context */
-MONC_INSTANCE *monc_create(void)
-{
-  MONC_INSTANCE *monc=talloc(NULL, MONC_INSTANCE);
+  MONC_INSTANCE *monc=talloc(mem_ctx, MONC_INSTANCE);
   if (monc!=NULL) {
-    monc->client_dh=NULL;
-    talloc_set_destructor((void *)monc, monc_destructor);
+    monc->gssc = tr_gssc_instance_new(monc);
+    if (monc->gssc == NULL) {
+      talloc_free(monc);
+      return NULL;
+    }
+
+    monc->gssc->service_name = "trustmonitor";
   }
   return monc;
 }
 
-void monc_destroy(MONC_INSTANCE *monc)
+void monc_free(MONC_INSTANCE *monc)
 {
   talloc_free(monc);
 }
 
-int monc_open_connection (MONC_INSTANCE *monc,
-                          const char *server,
-                          unsigned int port,
-                          gss_ctx_id_t *gssctx)
+int monc_open_connection(MONC_INSTANCE *monc,
+                         const char *server,
+                         unsigned int port)
 {
-  int err = 0;
-  int conn = -1;
-
-  tr_debug("monc_open_connection: opening monc connection to %s:%d", server, port);
-  err = gsscon_connect(server, port, "trustmonitor", &conn, gssctx);
-
-  if (!err)
-    return conn;
-  else
-    return -1;
+  return tr_gssc_open_connection(monc->gssc, server, port);
 }
 
-int monc_send_request (MONC_INSTANCE *monc,
-                       int conn,
-                       gss_ctx_id_t gssctx,
-                       MONC_RESP_FUNC *resp_handler,
-                       void *cookie)
+MON_RESP *monc_send_request(TALLOC_CTX *mem_ctx, MONC_INSTANCE *monc, MON_REQ *req)
 {
-  MON_REQ *mon_req = NULL;
-  int rc;
-
-  /* Create and populate a MON req structure */
-  if (!(mon_req = mon_req_new(NULL, MON_CMD_SHOW))) // TODO accept command as a parameter
-    goto error;
-
-  rc = monc_fwd_request(monc, conn, gssctx, mon_req, resp_handler, cookie);
-  goto cleanup;
-error:
-  rc = -1;
-cleanup:
-  mon_req_free(mon_req);
-  return rc;
-}
-
-int monc_fwd_request(MONC_INSTANCE *monc,
-                     int conn,
-                     gss_ctx_id_t gssctx,
-                     MON_REQ *mon_req,
-                     MONC_RESP_FUNC *resp_handler,
-                     void *cookie)
-{
-  char *req_buf = NULL;
-  char *resp_buf = NULL;
-  size_t resp_buflen = 0;
+  TALLOC_CTX *tmp_ctx = talloc_new(NULL);
   TR_MSG *msg = NULL;
   TR_MSG *resp_msg = NULL;
-  int err;
-  int rc = 0;
+  MON_RESP *resp = NULL;
 
-  /* Create and populate a MON msg structure */
-  if (!(msg = talloc_zero(mon_req, TR_MSG)))
-    goto error;
+  /* Create and populate a msg structure */
+  if (!(msg = talloc_zero(tmp_ctx, TR_MSG)))
+    goto cleanup;
 
   msg->msg_type = MON_REQUEST;
-  tr_msg_set_mon_req(msg, mon_req);
-
-  /* store the response function and cookie */
-  // mon_req->resp_func = resp_handler;
-  // mon_req->cookie = cookie;
-
-
-  /* Encode the request into a json string */
-  if (!(req_buf = tr_msg_encode(NULL, msg))) {
-    tr_err("monc_fwd_request: Error encoding MON request.\n");
-    goto error;
-  }
+  tr_msg_set_mon_req(msg, req);
 
-  tr_debug( "monc_fwd_request: Sending MON request:\n");
-  tr_debug( "%s\n", req_buf);
+  resp_msg = tr_gssc_exchange_msgs(tmp_ctx, monc->gssc, msg);
+  if (resp_msg == NULL)
+    goto cleanup;
 
-  /* Send the request over the connection */
-  err = gsscon_write_encrypted_token (conn, gssctx, req_buf, strlen(req_buf));
-  if (err) {
-    tr_err( "monc_fwd_request: Error sending request over connection.\n");
-    goto error;
-  }
+  resp = tr_msg_get_mon_resp(resp_msg);
 
-  /* TBD -- queue request on instance, read resps in separate thread */
+  /* if we got a response, steal it from resp_msg's context so we can return it */
+  if (resp)
+    talloc_steal(mem_ctx, resp);
 
-  /* Read the response from the connection */
-  /* TBD -- timeout? */
-  if (err = gsscon_read_encrypted_token(conn, gssctx, &resp_buf, &resp_buflen)) {
-    if (resp_buf)
-      free(resp_buf);
-    goto error;
-  }
-
-  tr_debug( "monc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
-  tr_debug( "%s\n", resp_buf);
-
-//  if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
-//    tr_err( "monc_fwd_request: Error decoding response.\n");
-//    goto error;
-//  }
-//
-//  /* TBD -- Check if this is actually a valid response */
-//  if (MON_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
-//    tr_err( "monc_fwd_request: Error, no response in the response!\n");
-//    goto error;
-//  }
-//
-//  if (resp_handler) {
-//    /* Call the caller's response function. It must copy any data it needs before returning. */
-//    tr_debug("monc_fwd_request: calling response callback function.");
-//    (*resp_handler)(monc, mon_req, tr_msg_get_resp(resp_msg), cookie);
-//  }
-
-  goto cleanup;
-
-error:
-  rc = -1;
 cleanup:
-  if (msg)
-    talloc_free(msg);
-  if (req_buf)
-    free(req_buf);
-  if (resp_buf)
-    free(resp_buf);
-  if (resp_msg)
-    tr_msg_free_decoded(resp_msg);
-  return rc;
+  talloc_free(tmp_ctx);
+  return resp;
 }
 
-
-DH * monc_get_dh(MONC_INSTANCE *inst)
+DH *monc_get_dh(MONC_INSTANCE *inst)
 {
-  return inst->client_dh;
+  return tr_gssc_get_dh(inst->gssc);
 }
 
 DH *monc_set_dh(MONC_INSTANCE *inst, DH *dh)
 {
-  inst->client_dh = dh;
-  return dh;
+  return tr_gssc_set_dh(inst->gssc, dh);
 }