2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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 struct option long_options
[] = {
60 {"verbose", optional_argument
, 0, 'v'},
61 {"help", no_argument
, 0, 'h'},
64 {"bootstrap-ca-cert", required_argument
, 0, OPT_BOOTSTRAP_CA_CERT
},
65 STREAM_SSL_LONG_OPTIONS
69 char *short_options
= long_options_to_short_options(long_options
);
72 int c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
82 vlog_set_verbosity(optarg
);
85 DAEMON_OPTION_HANDLERS
88 STREAM_SSL_OPTION_HANDLERS
90 case OPT_BOOTSTRAP_CA_CERT
:
91 stream_ssl_set_ca_cert_file(optarg
, true);
108 printf("%s: JSON-RPC test utility\n"
109 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
110 " listen LOCAL listen for connections on LOCAL\n"
111 " request REMOTE METHOD PARAMS send request, print reply\n"
112 " notify REMOTE METHOD PARAMS send notification and exit\n",
113 program_name
, program_name
);
114 stream_usage("JSON-RPC", true, true, true);
117 printf("\nOther options:\n"
118 " -h, --help display this help message\n");
122 /* Command helper functions. */
125 parse_json(const char *s
)
127 struct json
*json
= json_from_string(s
);
128 if (json
->type
== JSON_STRING
) {
129 ovs_fatal(0, "\"%s\": %s", s
, json
->u
.string
);
135 print_and_free_json(struct json
*json
)
137 char *string
= json_to_string(json
, JSSF_SORT
);
143 /* Command implementations. */
146 handle_rpc(struct jsonrpc
*rpc
, struct jsonrpc_msg
*msg
, bool *done
)
148 struct jsonrpc_msg
*reply
= NULL
;
149 if (msg
->type
== JSONRPC_REQUEST
) {
150 if (!strcmp(msg
->method
, "echo")) {
151 reply
= jsonrpc_create_reply(json_clone(msg
->params
), msg
->id
);
153 struct json
*error
= json_object_create();
154 json_object_put_string(error
, "error", "unknown method");
155 reply
= jsonrpc_create_error(error
, msg
->id
);
156 ovs_error(0, "unknown request %s", msg
->method
);
159 } else if (msg
->type
== JSONRPC_NOTIFY
) {
160 if (!strcmp(msg
->method
, "shutdown")) {
163 jsonrpc_error(rpc
, ENOTTY
);
164 ovs_error(0, "unknown notification %s", msg
->method
);
167 jsonrpc_error(rpc
, EPROTO
);
168 ovs_error(0, "unsolicited JSON-RPC reply or error");
172 jsonrpc_send(rpc
, reply
);
177 do_listen(int argc OVS_UNUSED
, char *argv
[])
179 struct pstream
*pstream
;
180 struct jsonrpc
**rpcs
;
181 size_t n_rpcs
, allocated_rpcs
;
185 die_if_already_running();
187 error
= jsonrpc_pstream_open(argv
[1], &pstream
);
189 ovs_fatal(error
, "could not listen on \"%s\"", argv
[1]);
195 n_rpcs
= allocated_rpcs
= 0;
198 struct stream
*stream
;
201 /* Accept new connections. */
202 error
= pstream_accept(pstream
, &stream
);
204 if (n_rpcs
>= allocated_rpcs
) {
205 rpcs
= x2nrealloc(rpcs
, &allocated_rpcs
, sizeof *rpcs
);
207 rpcs
[n_rpcs
++] = jsonrpc_open(stream
);
208 } else if (error
!= EAGAIN
) {
209 ovs_fatal(error
, "pstream_accept failed");
212 /* Service existing connections. */
213 for (i
= 0; i
< n_rpcs
; ) {
214 struct jsonrpc
*rpc
= rpcs
[i
];
215 struct jsonrpc_msg
*msg
;
218 if (!jsonrpc_get_backlog(rpc
)) {
219 error
= jsonrpc_recv(rpc
, &msg
);
221 handle_rpc(rpc
, msg
, &done
);
222 jsonrpc_msg_destroy(msg
);
226 error
= jsonrpc_get_status(rpc
);
229 ovs_error(error
, "connection closed");
230 memmove(&rpcs
[i
], &rpcs
[i
+ 1],
231 (n_rpcs
- i
- 1) * sizeof *rpcs
);
238 /* Wait for something to do. */
239 if (done
&& !n_rpcs
) {
242 pstream_wait(pstream
);
243 for (i
= 0; i
< n_rpcs
; i
++) {
244 struct jsonrpc
*rpc
= rpcs
[i
];
247 if (!jsonrpc_get_backlog(rpc
)) {
248 jsonrpc_recv_wait(rpc
);
254 pstream_close(pstream
);
258 do_request(int argc OVS_UNUSED
, char *argv
[])
260 struct jsonrpc_msg
*msg
;
263 struct stream
*stream
;
269 params
= parse_json(argv
[3]);
270 msg
= jsonrpc_create_request(method
, params
, NULL
);
271 string
= jsonrpc_msg_is_valid(msg
);
273 ovs_fatal(0, "not a valid JSON-RPC request: %s", string
);
276 error
= stream_open_block(jsonrpc_stream_open(argv
[1], &stream
), &stream
);
278 ovs_fatal(error
, "could not open \"%s\"", argv
[1]);
280 rpc
= jsonrpc_open(stream
);
282 error
= jsonrpc_send(rpc
, msg
);
284 ovs_fatal(error
, "could not send request");
287 error
= jsonrpc_recv_block(rpc
, &msg
);
289 ovs_fatal(error
, "error waiting for reply");
291 print_and_free_json(jsonrpc_msg_to_json(msg
));
297 do_notify(int argc OVS_UNUSED
, char *argv
[])
299 struct jsonrpc_msg
*msg
;
302 struct stream
*stream
;
308 params
= parse_json(argv
[3]);
309 msg
= jsonrpc_create_notify(method
, params
);
310 string
= jsonrpc_msg_is_valid(msg
);
312 ovs_fatal(0, "not a JSON RPC-valid notification: %s", string
);
315 error
= stream_open_block(jsonrpc_stream_open(argv
[1], &stream
), &stream
);
317 ovs_fatal(error
, "could not open \"%s\"", argv
[1]);
319 rpc
= jsonrpc_open(stream
);
321 error
= jsonrpc_send_block(rpc
, msg
);
323 ovs_fatal(error
, "could not send notification");
329 do_help(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
334 static struct command all_commands
[] = {
335 { "listen", 1, 1, do_listen
},
336 { "request", 3, 3, do_request
},
337 { "notify", 3, 3, do_notify
},
338 { "help", 0, INT_MAX
, do_help
},
339 { NULL
, 0, 0, NULL
},