Fix handling of errors with strtol(), factor out port parsing
[trust_router.git] / tr / trpc_main.c
1 /*
2  * Copyright (c) 2012, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <talloc.h>
38 #include <argp.h>
39 #include <unistd.h>
40
41 #include <gsscon.h>
42 #include <tr_debug.h>
43 #include <tr_trp.h>
44 #include <tr_inet_util.h>
45
46
47 /* command-line option setup */
48
49 /* argp global parameters */
50 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
51
52 /* doc strings */
53 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router TRP Client";
54 static const char arg_doc[]="<message> <server> [<port>]"; /* string describing arguments, if any */
55
56 /* define the options here. Fields are:
57  * { long-name, short-name, variable name, options, help description } */
58 static const struct argp_option cmdline_options[] = {
59   { "repeat", 'r', "N", OPTION_ARG_OPTIONAL, "Repeat message until terminated, or N times." },
60   {NULL}
61 };
62
63 /* structure for communicating with option parser */
64 struct cmdline_args {
65   char *msg;
66   char *server;
67   int port; /* optional */
68   int repeat; /* how many times to repeat, or -1 for infinite */
69 };
70
71 /* parser for individual options - fills in a struct cmdline_args */
72 static error_t parse_option(int key, char *arg, struct argp_state *state)
73 {
74   /* get a shorthand to the command line argument structure, part of state */
75   struct cmdline_args *arguments=state->input;
76
77   switch (key) {
78   case 'r':
79     if (arg==NULL)
80       arguments->repeat=-1;
81     else
82       arguments->repeat=strtol(arg, NULL, 10);
83     break;
84
85   case ARGP_KEY_ARG: /* handle argument (not option) */
86     switch (state->arg_num) {
87     case 0:
88       arguments->msg=arg;
89       break;
90
91     case 1:
92       arguments->server=arg;
93       break;
94
95     case 2:
96       arguments->port=tr_parse_port(arg); /* optional */
97       if (arguments->port < 0) {
98         switch(-(arguments->port)) {
99           case ERANGE:
100             printf("\nError parsing port (%s): port must be an integer in the range 1 - 65535\n\n", arg);
101             break;
102
103           default:
104             printf("\nError parsing port (%s): %s\n\n", arg, strerror(-arguments->port));
105             break;
106         }
107         argp_usage(state);
108       }
109       break;
110
111     default:
112       /* too many arguments */
113       argp_usage(state);
114     }
115     break;
116
117   case ARGP_KEY_END: /* no more arguments */
118     if (state->arg_num < 2) {
119       /* not enough arguments encountered */
120       argp_usage(state);
121     }
122     break;
123
124   default:
125     return ARGP_ERR_UNKNOWN;
126   }
127
128   return 0; /* success */
129 }
130
131 /* assemble the argp parser */
132 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
133
134 int main (int argc, 
135           char *argv[]) 
136 {
137   TALLOC_CTX *main_ctx=talloc_new(NULL);
138   TRPC_INSTANCE *trpc=NULL;
139   TRP_CONNECTION *conn=NULL;
140   struct cmdline_args opts;
141
142   /* parse the command line*/
143   /* set defaults */
144   opts.msg=NULL;
145   opts.server=NULL;
146   opts.port=TRP_PORT;
147   opts.repeat=1;
148
149   argp_parse(&argp, argc, argv, 0, 0, &opts);
150
151   /* Use standalone logging */
152   tr_log_open();
153
154   /* set logging levels */
155   talloc_set_log_stderr();
156   tr_log_threshold(LOG_CRIT);
157   tr_console_threshold(LOG_DEBUG);
158
159   printf("TRPC Client:\nServer = %s, port = %i\n", opts.server, opts.port);
160  
161   conn=trp_connection_new(trpc);
162   if (conn==NULL) {
163     printf("Could not allocate TRP_CONNECTION.\n");
164     return 1;
165   }
166   trpc = trpc_new(main_ctx);
167   trpc_set_server(trpc, opts.server);
168   trpc_set_port(trpc, opts.port);
169   trpc_set_conn(trpc, conn);
170   /* Set-up TRP connection */
171   if (TRP_SUCCESS != trpc_connect(trpc)) {
172     /* Handle error */
173     printf("Error in trpc_connect.\n");
174     return 1;
175   }
176
177   /* Send a TRP message */
178   while ((opts.repeat==-1) || (opts.repeat-->0)) {
179     if (TRP_SUCCESS != trpc_send_msg(trpc, opts.msg)) {
180       /* Handle error */
181       printf("Error in trpc_send_request.");
182       return 1;
183     }
184     usleep(1000000);
185   }
186     
187   /* Clean-up the TRP client instance, and exit */
188   trpc_free(trpc);
189
190   return 0;
191 }
192