2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
27 #include "command-line.h"
30 #include "poll-loop.h"
31 #include "stream-ssl.h"
38 static void usage(void) NO_RETURN;
39 static void parse_options(int argc, char *argv[]);
40 static struct command *get_all_commands(void);
43 test_jsonrpc_main(int argc, char *argv[])
45 proctitle_init(argc, argv);
46 set_program_name(argv[0]);
47 parse_options(argc, argv);
48 run_command(argc - optind, argv + optind, get_all_commands());
52 parse_options(int argc, char *argv[])
55 OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1,
58 static const struct option long_options[] = {
59 {"verbose", optional_argument, NULL, 'v'},
60 {"help", no_argument, NULL, 'h'},
62 {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
63 STREAM_SSL_LONG_OPTIONS,
66 char *short_options = long_options_to_short_options(long_options);
69 int c = getopt_long(argc, argv, short_options, long_options, NULL);
79 vlog_set_verbosity(optarg);
82 DAEMON_OPTION_HANDLERS
84 STREAM_SSL_OPTION_HANDLERS
86 case OPT_BOOTSTRAP_CA_CERT:
87 stream_ssl_set_ca_cert_file(optarg, true);
103 printf("%s: JSON-RPC test utility\n"
104 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
105 " listen LOCAL listen for connections on LOCAL\n"
106 " request REMOTE METHOD PARAMS send request, print reply\n"
107 " notify REMOTE METHOD PARAMS send notification and exit\n",
108 program_name, program_name);
109 stream_usage("JSON-RPC", true, true, true);
112 printf("\nOther options:\n"
113 " -h, --help display this help message\n");
117 /* Command helper functions. */
120 parse_json(const char *s)
122 struct json *json = json_from_string(s);
123 if (json->type == JSON_STRING) {
124 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
130 print_and_free_json(struct json *json)
132 char *string = json_to_string(json, JSSF_SORT);
138 /* Command implementations. */
141 handle_rpc(struct jsonrpc *rpc, struct jsonrpc_msg *msg, bool *done)
143 if (msg->type == JSONRPC_REQUEST) {
144 struct jsonrpc_msg *reply = NULL;
145 if (!strcmp(msg->method, "echo")) {
146 reply = jsonrpc_create_reply(json_clone(msg->params), msg->id);
148 struct json *error = json_object_create();
149 json_object_put_string(error, "error", "unknown method");
150 reply = jsonrpc_create_error(error, msg->id);
151 ovs_error(0, "unknown request %s", msg->method);
153 jsonrpc_send(rpc, reply);
155 } else if (msg->type == JSONRPC_NOTIFY) {
156 if (!strcmp(msg->method, "shutdown")) {
160 ovs_error(0, "unknown notification %s", msg->method);
164 ovs_error(0, "unsolicited JSON-RPC reply or error");
170 do_listen(int argc OVS_UNUSED, char *argv[])
172 struct pstream *pstream;
173 struct jsonrpc **rpcs;
174 size_t n_rpcs, allocated_rpcs;
178 error = jsonrpc_pstream_open(argv[1], &pstream, DSCP_DEFAULT);
180 ovs_fatal(error, "could not listen on \"%s\"", argv[1]);
186 n_rpcs = allocated_rpcs = 0;
189 struct stream *stream;
192 /* Accept new connections. */
193 error = pstream_accept(pstream, &stream);
195 if (n_rpcs >= allocated_rpcs) {
196 rpcs = x2nrealloc(rpcs, &allocated_rpcs, sizeof *rpcs);
198 rpcs[n_rpcs++] = jsonrpc_open(stream);
199 } else if (error != EAGAIN) {
200 ovs_fatal(error, "pstream_accept failed");
203 /* Service existing connections. */
204 for (i = 0; i < n_rpcs; ) {
205 struct jsonrpc *rpc = rpcs[i];
206 struct jsonrpc_msg *msg;
209 if (!jsonrpc_get_backlog(rpc)) {
210 error = jsonrpc_recv(rpc, &msg);
212 error = handle_rpc(rpc, msg, &done);
213 jsonrpc_msg_destroy(msg);
214 } else if (error == EAGAIN) {
220 error = jsonrpc_get_status(rpc);
224 ovs_error(error, "connection closed");
225 memmove(&rpcs[i], &rpcs[i + 1],
226 (n_rpcs - i - 1) * sizeof *rpcs);
233 /* Wait for something to do. */
234 if (done && !n_rpcs) {
237 pstream_wait(pstream);
238 for (i = 0; i < n_rpcs; i++) {
239 struct jsonrpc *rpc = rpcs[i];
242 if (!jsonrpc_get_backlog(rpc)) {
243 jsonrpc_recv_wait(rpc);
249 pstream_close(pstream);
253 do_request(int argc OVS_UNUSED, char *argv[])
255 struct jsonrpc_msg *msg;
258 struct stream *stream;
264 params = parse_json(argv[3]);
265 msg = jsonrpc_create_request(method, params, NULL);
266 string = jsonrpc_msg_is_valid(msg);
268 ovs_fatal(0, "not a valid JSON-RPC request: %s", string);
271 error = stream_open_block(jsonrpc_stream_open(argv[1], &stream,
272 DSCP_DEFAULT), &stream);
274 ovs_fatal(error, "could not open \"%s\"", argv[1]);
276 rpc = jsonrpc_open(stream);
278 error = jsonrpc_send(rpc, msg);
280 ovs_fatal(error, "could not send request");
283 error = jsonrpc_recv_block(rpc, &msg);
285 ovs_fatal(error, "error waiting for reply");
287 print_and_free_json(jsonrpc_msg_to_json(msg));
293 do_notify(int argc OVS_UNUSED, char *argv[])
295 struct jsonrpc_msg *msg;
298 struct stream *stream;
304 params = parse_json(argv[3]);
305 msg = jsonrpc_create_notify(method, params);
306 string = jsonrpc_msg_is_valid(msg);
308 ovs_fatal(0, "not a JSON RPC-valid notification: %s", string);
311 error = stream_open_block(jsonrpc_stream_open(argv[1], &stream,
312 DSCP_DEFAULT), &stream);
314 ovs_fatal(error, "could not open \"%s\"", argv[1]);
316 rpc = jsonrpc_open(stream);
318 error = jsonrpc_send_block(rpc, msg);
320 ovs_fatal(error, "could not send notification");
326 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
331 static struct command all_commands[] = {
332 { "listen", 1, 1, do_listen },
333 { "request", 3, 3, do_request },
334 { "notify", 3, 3, do_notify },
335 { "help", 0, INT_MAX, do_help },
336 { NULL, 0, 0, NULL },
339 static struct command *
340 get_all_commands(void)
345 OVSTEST_REGISTER("test-jsonrpc", test_jsonrpc_main);