Merge branch 'milestone/monitoring' into jennifer/request_id
[trust_router.git] / tid / example / tids_main.c
index 9d4c879..1ecc494 100644 (file)
@@ -41,6 +41,7 @@
 #include <poll.h>
 
 #include <tr_debug.h>
+#include <tr_util.h>
 #include <tid_internal.h>
 #include <trust_router/tr_constraint.h>
 #include <trust_router/tr_dh.h>
@@ -147,6 +148,7 @@ static int handle_authorizations(TID_REQ *req, const unsigned char *dh_hash,
       if (SQLITE_DONE != sqlite3_result)
        tr_crit("sqlite3: failed to write to database");
       sqlite3_reset(authorization_insert);
+      sqlite3_clear_bindings(authorization_insert);
     }
   return 0;
 }
@@ -160,14 +162,11 @@ static int tids_req_handler (TIDS_INSTANCE *tids,
   unsigned char *s_keybuf = NULL;
   int s_keylen = 0;
   char key_id[12];
-  unsigned char *pub_digest;
+  unsigned char *pub_digest=NULL;
   size_t pub_digest_len;
   
 
   tr_debug("tids_req_handler: Request received! target_realm = %s, community = %s", req->realm->buf, req->comm->buf);
-  if (tids)
-    tids->req_count++;
-
   if (!(resp) || !resp) {
     tr_debug("tids_req_handler: No response structure.");
     return -1;
@@ -175,13 +174,12 @@ static int tids_req_handler (TIDS_INSTANCE *tids,
 
 
   /* Allocate a new server block */
-  if (NULL == (resp->servers = talloc_zero(resp, TID_SRVR_BLK))){
-    tr_crit("tids_req_handler(): malloc failed.");
+  tid_srvr_blk_add(resp->servers, tid_srvr_blk_new(resp));
+  if (NULL==resp->servers) {
+    tr_crit("tids_req_handler(): unable to allocate server block.");
     return -1;
   }
 
-  resp->num_servers = 1;
-
   /* TBD -- Set up the server IP Address */
 
   if (!(req) || !(req->tidc_dh)) {
@@ -203,7 +201,7 @@ static int tids_req_handler (TIDS_INSTANCE *tids,
     return -1;
   }
 
-  resp->servers->aaa_server_addr=tids->ipaddr;
+  resp->servers->aaa_server_addr=talloc_strdup(resp->servers, tids->ipaddr);
 
   /* Set the key name */
   if (-1 == create_key_id(key_id, sizeof(key_id)))
@@ -226,7 +224,8 @@ static int tids_req_handler (TIDS_INSTANCE *tids,
   }
   if (0 != handle_authorizations(req, pub_digest, pub_digest_len))
     return -1;
-  resp->servers->path = req->path;
+  tid_srvr_blk_set_path(resp->servers, (TID_PATH *)(req->path));
+
   if (req->expiration_interval < 1)
     req->expiration_interval = 1;
   g_get_current_time(&resp->servers->key_expiration);
@@ -235,14 +234,16 @@ static int tids_req_handler (TIDS_INSTANCE *tids,
   if (NULL != insert_stmt) {
     int sqlite3_result;
     gchar *expiration_str = g_time_val_to_iso8601(&resp->servers->key_expiration);
-        sqlite3_bind_text(insert_stmt, 1, key_id, -1, SQLITE_TRANSIENT);
+    sqlite3_bind_text(insert_stmt, 1, key_id, -1, SQLITE_TRANSIENT);
     sqlite3_bind_blob(insert_stmt, 2, s_keybuf, s_keylen, SQLITE_TRANSIENT);
     sqlite3_bind_blob(insert_stmt, 3, pub_digest, pub_digest_len, SQLITE_TRANSIENT);
-        sqlite3_bind_text(insert_stmt, 4, expiration_str, -1, SQLITE_TRANSIENT);
+    sqlite3_bind_text(insert_stmt, 4, expiration_str, -1, SQLITE_TRANSIENT);
+    g_free(expiration_str); /* bind_text already made its own copy */
     sqlite3_result = sqlite3_step(insert_stmt);
     if (SQLITE_DONE != sqlite3_result)
       tr_crit("sqlite3: failed to write to database");
     sqlite3_reset(insert_stmt);
+    sqlite3_clear_bindings(insert_stmt);
   }
   
   /* Print out the key. */
@@ -252,6 +253,12 @@ static int tids_req_handler (TIDS_INSTANCE *tids,
   // }
   // fprintf(stderr, "\n");
 
+  if (s_keybuf!=NULL)
+    free(s_keybuf);
+
+  if (pub_digest!=NULL)
+    talloc_free(pub_digest);
+  
   return s_keylen;
 }
 
@@ -268,18 +275,24 @@ static int auth_handler(gss_name_t gss_name, TR_NAME *client,
   return result;
 }
 
+static void print_version_info(void)
+{
+  printf("Moonshot TID Server %s\n\n", PACKAGE_VERSION);
+}
+
 /* command-line option setup */
 
 /* argp global parameters */
 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
 
 /* doc strings */
-static const char doc[]=PACKAGE_NAME " - TID Server";
+static const char doc[]=PACKAGE_NAME " - Moonshot TID Server " PACKAGE_VERSION;
 static const char arg_doc[]="<ip-address> <gss-name> <hostname> <database-name>"; /* string describing arguments, if any */
 
 /* define the options here. Fields are:
  * { long-name, short-name, variable name, options, help description } */
 static const struct argp_option cmdline_options[] = {
+  { "version", 'v', NULL, 0, "Print version information and exit"},
   { NULL }
 };
 
@@ -329,6 +342,10 @@ static error_t parse_option(int key, char *arg, struct argp_state *state)
     }
     break;
 
+  case 'v':
+    print_version_info();
+    exit(0);
+
   default:
     return ARGP_ERR_UNKNOWN;
   }
@@ -344,16 +361,13 @@ int main (int argc,
 {
   TIDS_INSTANCE *tids;
   TR_NAME *gssname = NULL;
-  struct cmdline_args opts={NULL};
-#define MAX_SOCKETS 10
-  int tids_socket[MAX_SOCKETS];
-  size_t n_sockets;
-  struct pollfd poll_fds[MAX_SOCKETS];
-  size_t ii=0;
+  struct cmdline_args opts={0};
 
   /* parse the command line*/
   argp_parse(&argp, argc, argv, 0, 0, &opts);
 
+  print_version_info();
+
   talloc_set_log_stderr();
 
   /* Use standalone logging */
@@ -375,37 +389,13 @@ int main (int argc,
                     -1, &authorization_insert, NULL);
 
   /* Create a TID server instance */
-  if (NULL == (tids = tids_create(NULL))) {
+  if (NULL == (tids = tids_create())) {
     tr_crit("Unable to create TIDS instance, exiting.");
     return 1;
   }
 
   tids->ipaddr = opts.ip_address;
-
-  /* get listener for tids port */
-  n_sockets = tids_get_listener(tids, &tids_req_handler, auth_handler, opts.hostname, TID_PORT, gssname,
-                                tids_socket, MAX_SOCKETS);
-
-  for (ii=0; ii<n_sockets; ii++) {
-    poll_fds[ii].fd=tids_socket[ii];
-    poll_fds[ii].events=POLLIN; /* poll on ready for reading */
-    poll_fds[ii].revents=0;
-  }
-
-  /* main event loop */
-  while (1) {
-    /* wait up to 100 ms for an event, then handle any idle work */
-    if(poll(poll_fds, n_sockets, 100) > 0) {
-      for (ii=0; ii<n_sockets; ii++) {
-        if (poll_fds[ii].revents & POLLIN) {
-          if (0 != tids_accept(tids, tids_socket[ii])) {
-            tr_err("Error handling tids request.");
-          }
-        }
-      }
-    }
-    /* idle loop stuff here */
-  }
+  (void) tids_start(tids, &tids_req_handler, auth_handler, opts.hostname, TID_PORT, gssname);
 
   /* Clean-up the TID server instance */
   tids_destroy(tids);