2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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"
37 static struct command all_commands
[];
39 static void usage(void) NO_RETURN
;
40 static void parse_options(int argc
, char *argv
[]);
43 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
, all_commands
);
53 parse_options(int argc
, char *argv
[])
56 OPT_BOOTSTRAP_CA_CERT
= UCHAR_MAX
+ 1,
59 static const struct option long_options
[] = {
60 {"verbose", optional_argument
, NULL
, 'v'},
61 {"help", no_argument
, NULL
, 'h'},
63 {"bootstrap-ca-cert", required_argument
, NULL
, OPT_BOOTSTRAP_CA_CERT
},
64 STREAM_SSL_LONG_OPTIONS
,
67 char *short_options
= long_options_to_short_options(long_options
);
70 int c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
80 vlog_set_verbosity(optarg
);
83 DAEMON_OPTION_HANDLERS
85 STREAM_SSL_OPTION_HANDLERS
87 case OPT_BOOTSTRAP_CA_CERT
:
88 stream_ssl_set_ca_cert_file(optarg
, true);
104 printf("%s: JSON-RPC test utility\n"
105 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
106 " listen LOCAL listen for connections on LOCAL\n"
107 " request REMOTE METHOD PARAMS send request, print reply\n"
108 " notify REMOTE METHOD PARAMS send notification and exit\n",
109 program_name
, program_name
);
110 stream_usage("JSON-RPC", true, true, true);
113 printf("\nOther options:\n"
114 " -h, --help display this help message\n");
118 /* Command helper functions. */
121 parse_json(const char *s
)
123 struct json
*json
= json_from_string(s
);
124 if (json
->type
== JSON_STRING
) {
125 ovs_fatal(0, "\"%s\": %s", s
, json
->u
.string
);
131 print_and_free_json(struct json
*json
)
133 char *string
= json_to_string(json
, JSSF_SORT
);
139 /* Command implementations. */
142 handle_rpc(struct jsonrpc
*rpc
, struct jsonrpc_msg
*msg
, bool *done
)
144 if (msg
->type
== JSONRPC_REQUEST
) {
145 struct jsonrpc_msg
*reply
= NULL
;
146 if (!strcmp(msg
->method
, "echo")) {
147 reply
= jsonrpc_create_reply(json_clone(msg
->params
), msg
->id
);
149 struct json
*error
= json_object_create();
150 json_object_put_string(error
, "error", "unknown method");
151 reply
= jsonrpc_create_error(error
, msg
->id
);
152 ovs_error(0, "unknown request %s", msg
->method
);
154 jsonrpc_send(rpc
, reply
);
156 } else if (msg
->type
== JSONRPC_NOTIFY
) {
157 if (!strcmp(msg
->method
, "shutdown")) {
161 ovs_error(0, "unknown notification %s", msg
->method
);
165 ovs_error(0, "unsolicited JSON-RPC reply or error");
171 do_listen(int argc OVS_UNUSED
, char *argv
[])
173 struct pstream
*pstream
;
174 struct jsonrpc
**rpcs
;
175 size_t n_rpcs
, allocated_rpcs
;
179 error
= jsonrpc_pstream_open(argv
[1], &pstream
, DSCP_DEFAULT
);
181 ovs_fatal(error
, "could not listen on \"%s\"", argv
[1]);
187 n_rpcs
= allocated_rpcs
= 0;
190 struct stream
*stream
;
193 /* Accept new connections. */
194 error
= pstream_accept(pstream
, &stream
);
196 if (n_rpcs
>= allocated_rpcs
) {
197 rpcs
= x2nrealloc(rpcs
, &allocated_rpcs
, sizeof *rpcs
);
199 rpcs
[n_rpcs
++] = jsonrpc_open(stream
);
200 } else if (error
!= EAGAIN
) {
201 ovs_fatal(error
, "pstream_accept failed");
204 /* Service existing connections. */
205 for (i
= 0; i
< n_rpcs
; ) {
206 struct jsonrpc
*rpc
= rpcs
[i
];
207 struct jsonrpc_msg
*msg
;
210 if (!jsonrpc_get_backlog(rpc
)) {
211 error
= jsonrpc_recv(rpc
, &msg
);
213 error
= handle_rpc(rpc
, msg
, &done
);
214 jsonrpc_msg_destroy(msg
);
215 } else if (error
== EAGAIN
) {
221 error
= jsonrpc_get_status(rpc
);
225 ovs_error(error
, "connection closed");
226 memmove(&rpcs
[i
], &rpcs
[i
+ 1],
227 (n_rpcs
- i
- 1) * sizeof *rpcs
);
234 /* Wait for something to do. */
235 if (done
&& !n_rpcs
) {
238 pstream_wait(pstream
);
239 for (i
= 0; i
< n_rpcs
; i
++) {
240 struct jsonrpc
*rpc
= rpcs
[i
];
243 if (!jsonrpc_get_backlog(rpc
)) {
244 jsonrpc_recv_wait(rpc
);
250 pstream_close(pstream
);
254 do_request(int argc OVS_UNUSED
, char *argv
[])
256 struct jsonrpc_msg
*msg
;
259 struct stream
*stream
;
265 params
= parse_json(argv
[3]);
266 msg
= jsonrpc_create_request(method
, params
, NULL
);
267 string
= jsonrpc_msg_is_valid(msg
);
269 ovs_fatal(0, "not a valid JSON-RPC request: %s", string
);
272 error
= stream_open_block(jsonrpc_stream_open(argv
[1], &stream
,
273 DSCP_DEFAULT
), &stream
);
275 ovs_fatal(error
, "could not open \"%s\"", argv
[1]);
277 rpc
= jsonrpc_open(stream
);
279 error
= jsonrpc_send(rpc
, msg
);
281 ovs_fatal(error
, "could not send request");
284 error
= jsonrpc_recv_block(rpc
, &msg
);
286 ovs_fatal(error
, "error waiting for reply");
288 print_and_free_json(jsonrpc_msg_to_json(msg
));
294 do_notify(int argc OVS_UNUSED
, char *argv
[])
296 struct jsonrpc_msg
*msg
;
299 struct stream
*stream
;
305 params
= parse_json(argv
[3]);
306 msg
= jsonrpc_create_notify(method
, params
);
307 string
= jsonrpc_msg_is_valid(msg
);
309 ovs_fatal(0, "not a JSON RPC-valid notification: %s", string
);
312 error
= stream_open_block(jsonrpc_stream_open(argv
[1], &stream
,
313 DSCP_DEFAULT
), &stream
);
315 ovs_fatal(error
, "could not open \"%s\"", argv
[1]);
317 rpc
= jsonrpc_open(stream
);
319 error
= jsonrpc_send_block(rpc
, msg
);
321 ovs_fatal(error
, "could not send notification");
327 do_help(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
332 static struct command all_commands
[] = {
333 { "listen", 1, 1, do_listen
},
334 { "request", 3, 3, do_request
},
335 { "notify", 3, 3, do_notify
},
336 { "help", 0, INT_MAX
, do_help
},
337 { NULL
, 0, 0, NULL
},