Use json_is_true() in place of json_boolean_value() for compatibility
[trust_router.git] / tr / trpc_main.c
index 088a2d6..db29417 100644 (file)
 #include <stdio.h>
 #include <talloc.h>
 #include <argp.h>
+#include <unistd.h>
 
 #include <gsscon.h>
 #include <tr_debug.h>
 #include <tr_trp.h>
-#include <trust_router/tr_dh.h>
+#include <tr_inet_util.h>
 
-#if 0
-static void tidc_resp_handler (TIDC_INSTANCE * tidc, 
-                       TID_REQ *req,
-                       TID_RESP *resp, 
-                       void *cookie) 
-{
-  int c_keylen = 0;
-  unsigned char *c_keybuf = NULL;
-  int i;
-
-  printf ("Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf);
-
-  /* Generate the client key -- TBD, handle more than one server */
-  if (TID_SUCCESS != resp->result) {
-    fprintf(stderr, "tidc_resp_handler: Response is an error.\n");
-    return;
-  }
-
-  if (!resp->servers) {
-    fprintf(stderr, "tidc_resp_handler: Response does not contain server info.\n");
-    return;
-  }
-  
-  if (0 > (c_keylen = tr_compute_dh_key(&c_keybuf, 
-                                     resp->servers->aaa_server_dh->pub_key, 
-                                     req->tidc_dh))) {
-    
-    printf("tidc_resp_handler: Error computing client key.\n");
-    return;
-  }
-  
-  /* Print out the client key. */
-  printf("Client Key Generated (len = %d):\n", c_keylen);
-  for (i = 0; i < c_keylen; i++) {
-    printf("%.2x", c_keybuf[i]); 
-  }
-  printf("\n");
-
-  return;
-}
-#endif
 
 /* command-line option setup */
 
@@ -90,13 +50,14 @@ static void tidc_resp_handler (TIDC_INSTANCE * tidc,
 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
 
 /* doc strings */
-static const char doc[]=PACKAGE_NAME " - TRP Client";
+static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router TRP Client";
 static const char arg_doc[]="<message> <server> [<port>]"; /* 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[] = {
-  { NULL }
+  { "repeat", 'r', "N", OPTION_ARG_OPTIONAL, "Repeat message until terminated, or N times." },
+  {NULL}
 };
 
 /* structure for communicating with option parser */
@@ -104,6 +65,7 @@ struct cmdline_args {
   char *msg;
   char *server;
   int port; /* optional */
+  int repeat; /* how many times to repeat, or -1 for infinite */
 };
 
 /* parser for individual options - fills in a struct cmdline_args */
@@ -113,6 +75,13 @@ static error_t parse_option(int key, char *arg, struct argp_state *state)
   struct cmdline_args *arguments=state->input;
 
   switch (key) {
+  case 'r':
+    if (arg==NULL)
+      arguments->repeat=-1;
+    else
+      arguments->repeat=strtol(arg, NULL, 10);
+    break;
+
   case ARGP_KEY_ARG: /* handle argument (not option) */
     switch (state->arg_num) {
     case 0:
@@ -124,7 +93,19 @@ static error_t parse_option(int key, char *arg, struct argp_state *state)
       break;
 
     case 2:
-      arguments->port=strtol(arg, NULL, 10); /* optional */
+      arguments->port=tr_parse_port(arg); /* optional */
+      if (arguments->port < 0) {
+        switch(-(arguments->port)) {
+          case ERANGE:
+            printf("\nError parsing port (%s): port must be an integer in the range 1 - 65535\n\n", arg);
+            break;
+
+          default:
+            printf("\nError parsing port (%s): %s\n\n", arg, strerror(-arguments->port));
+            break;
+        }
+        argp_usage(state);
+      }
       break;
 
     default:
@@ -155,9 +136,7 @@ int main (int argc,
 {
   TALLOC_CTX *main_ctx=talloc_new(NULL);
   TRPC_INSTANCE *trpc=NULL;
-  int conn = 0;
-  int rc;
-  gss_ctx_id_t gssctx;
+  TRP_CONNECTION *conn=NULL;
   struct cmdline_args opts;
 
   /* parse the command line*/
@@ -165,9 +144,9 @@ int main (int argc,
   opts.msg=NULL;
   opts.server=NULL;
   opts.port=TRP_PORT;
+  opts.repeat=1;
 
   argp_parse(&argp, argc, argv, 0, 0, &opts);
-  /* TBD -- validity checking, dealing with quotes, etc. */
 
   /* Use standalone logging */
   tr_log_open();
@@ -179,29 +158,34 @@ int main (int argc,
 
   printf("TRPC Client:\nServer = %s, port = %i\n", opts.server, opts.port);
  
-  /* Create a TRP client instance & the client DH */
-  trpc = trpc_create(main_ctx);
-  if (NULL == (trpc->client_dh = tr_create_dh_params(NULL, 0))) {
-    printf("Error creating client DH params.\n");
+  conn=trp_connection_new(trpc);
+  if (conn==NULL) {
+    printf("Could not allocate TRP_CONNECTION.\n");
     return 1;
   }
-
+  trpc = trpc_new(main_ctx);
+  trpc_set_server(trpc, opts.server);
+  trpc_set_port(trpc, opts.port);
+  trpc_set_conn(trpc, conn);
   /* Set-up TRP connection */
-  if (-1 == (conn = trpc_open_connection(trpc, opts.server, opts.port, &gssctx))) {
+  if (TRP_SUCCESS != trpc_connect(trpc)) {
     /* Handle error */
-    printf("Error in trpc_open_connection.\n");
+    printf("Error in trpc_connect.\n");
     return 1;
-  };
+  }
 
   /* Send a TRP message */
-  if (0 > (rc = trpc_send_msg(trpc, conn, gssctx, opts.msg, NULL, NULL))) {
-    /* Handle error */
-    printf("Error in trpc_send_request, rc = %d.\n", rc);
-    return 1;
+  while ((opts.repeat==-1) || (opts.repeat-->0)) {
+    if (TRP_SUCCESS != trpc_send_msg(trpc, opts.msg)) {
+      /* Handle error */
+      printf("Error in trpc_send_request.");
+      return 1;
+    }
+    usleep(1000000);
   }
     
   /* Clean-up the TRP client instance, and exit */
-  trpc_destroy(trpc);
+  trpc_free(trpc);
 
   return 0;
 }