Use TR_MSG instead of encoded strings in GSS request handler interface
[trust_router.git] / mon / mons.c
index 29f405b..d2a5c8a 100644 (file)
@@ -42,6 +42,9 @@
 #include <mon_internal.h>
 #include <tr_socket.h>
 #include <sys/wait.h>
+#include <tr_gss.h>
+
+#include "mons_handlers.h"
 
 /**
  * Allocate a new MONS_INSTANCE
@@ -54,7 +57,10 @@ MONS_INSTANCE *mons_new(TALLOC_CTX *mem_ctx)
   MONS_INSTANCE *mons = talloc(mem_ctx, MONS_INSTANCE);
 
   if (mons) {
+    mons->hostname = NULL;
     mons->port = 0;
+    mons->tids = NULL;
+    mons->trps = NULL;
     mons->req_handler = NULL;
     mons->auth_handler = NULL;
     mons->cookie = NULL;
@@ -68,6 +74,39 @@ MONS_INSTANCE *mons_new(TALLOC_CTX *mem_ctx)
 }
 
 /**
+ * Callback to process a request and produce a response
+ *
+ * @param req_str JSON-encoded request
+ * @param data pointer to a MONS_INSTANCE
+ * @return pointer to the response string or null to send no response
+ */
+static TR_MSG *mons_req_cb(TALLOC_CTX *mem_ctx, TR_MSG *req_msg, void *data)
+{
+  TALLOC_CTX *tmp_ctx = talloc_new(NULL);
+  //MONS_INSTANCE *mons = talloc_get_type_abort(data, MONS_INSTANCE);
+  MON_REQ *req = NULL;
+  TR_MSG *resp_msg = NULL; /* This is the response value */
+
+  /* Validate inputs */
+  if (req_msg == NULL)
+    goto cleanup;
+
+  req = tr_msg_get_mon_req(req_msg);
+  if (req == NULL) {
+    /* this is an internal error */
+    tr_err("mons_req_cb: Received incorrect message type (was %d, expected %d)",
+           tr_msg_get_msg_type(req_msg),
+           MON_REQUEST);
+    /* TODO send an error response */
+    goto cleanup;
+  }
+
+cleanup:
+  talloc_free(tmp_ctx);
+  return resp_msg;
+}
+
+/**
  * Create a listener for monitoring requests
  *
  * Accept connections with mons_accept()
@@ -82,13 +121,8 @@ MONS_INSTANCE *mons_new(TALLOC_CTX *mem_ctx)
  * @param max_fd
  * @return
  */
-int mons_get_listener(MONS_INSTANCE *mons,
-                      MONS_REQ_FUNC *req_handler,
-                      MONS_AUTH_FUNC *auth_handler,
-                      unsigned int port,
-                      void *cookie,
-                      int *fd_out,
-                      size_t max_fd)
+int mons_get_listener(MONS_INSTANCE *mons, MONS_REQ_FUNC *req_handler, MONS_AUTH_FUNC *auth_handler, const char *hostname,
+                      unsigned int port, void *cookie, int *fd_out, size_t max_fd)
 {
   size_t n_fd=0;
   size_t ii=0;
@@ -119,6 +153,7 @@ int mons_get_listener(MONS_INSTANCE *mons,
     /* store the caller's request handler & cookie */
     mons->req_handler = req_handler;
     mons->auth_handler = auth_handler;
+    mons->hostname = hostname;
     mons->cookie = cookie;
   }
 
@@ -149,7 +184,11 @@ int mons_accept(MONS_INSTANCE *mons, int listen)
 
   if (pid == 0) {
     close(listen);
-    mons_handle_connection(mons, conn);
+    tr_gss_handle_connection(conn,
+                             "trustmonitor", mons->hostname, /* acceptor name */
+                             mons->auth_handler, mons->cookie, /* auth callback and cookie */
+                             mons_req_cb, mons /* req callback and cookie */
+    );
     close(conn);
     exit(0); /* exit to kill forked child process */
   } else {