Roman Janota | 907cf93 | 2022-06-03 12:33:34 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file client.c |
| 3 | * @author Roman Janota <xjanot04@fit.vutbr.cz> |
| 4 | * @brief libnetconf2 client example |
| 5 | * |
| 6 | * @copyright |
| 7 | * Copyright (c) 2022 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #include "example.h" |
| 17 | |
| 18 | #include <getopt.h> |
| 19 | #include <stdint.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <libyang/libyang.h> |
| 26 | |
| 27 | #include "log.h" |
| 28 | #include "messages_client.h" |
| 29 | #include "netconf.h" |
| 30 | #include "session_client.h" |
| 31 | #include "session_client_ch.h" |
| 32 | |
| 33 | static void |
| 34 | help_print() |
| 35 | { |
| 36 | printf("Example usage:\n" |
| 37 | " client -u ./unix_socket get\n" |
| 38 | "\n" |
| 39 | " Available options:\n" |
| 40 | " -h, --help\t \tPrint usage help.\n" |
| 41 | " -u, --unix\t<path>\tConnect to a UNIX socket at the place specified by <path>.\n" |
| 42 | " -s, --ssh\t\tConnect to a SSH server.\n\n" |
| 43 | " Available RPCs:\n" |
| 44 | " get [xpath-filter]\t\t\t\t\t send a <get> RPC with optional XPath filter\n" |
| 45 | " get-config [datastore] [xpath-filter]\t\t send a <get-config> RPC with optional XPath filter and datastore, the default datastore is \"running\" \n\n"); |
| 46 | } |
| 47 | |
| 48 | static enum NC_DATASTORE_TYPE |
| 49 | string2datastore(const char *str) |
| 50 | { |
| 51 | if (!str) { |
| 52 | return NC_DATASTORE_RUNNING; |
| 53 | } |
| 54 | |
| 55 | if (!strcmp(str, "candidate")) { |
| 56 | return NC_DATASTORE_CANDIDATE; |
| 57 | } else if (!strcmp(str, "running")) { |
| 58 | return NC_DATASTORE_RUNNING; |
| 59 | } else if (!strcmp(str, "startup")) { |
| 60 | return NC_DATASTORE_STARTUP; |
| 61 | } else { |
| 62 | return 0; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static int |
| 67 | send_rpc(struct nc_session *session, NC_RPC_TYPE rpc_type, const char *param1, const char *param2) |
| 68 | { |
| 69 | enum NC_DATASTORE_TYPE datastore; |
| 70 | int r = 0, rc = 0; |
| 71 | uint64_t msg_id = 0; |
| 72 | struct lyd_node *envp = NULL, *op = NULL; |
| 73 | struct nc_rpc *rpc = NULL; |
| 74 | |
| 75 | /* decide which type of RPC to send */ |
| 76 | switch (rpc_type) { |
| 77 | case NC_RPC_GET: |
| 78 | /* create get RPC with an optional filter */ |
| 79 | rpc = nc_rpc_get(param1, NC_WD_UNKNOWN, NC_PARAMTYPE_CONST); |
| 80 | break; |
| 81 | |
| 82 | case NC_RPC_GETCONFIG: |
| 83 | /* create get-config RPC with a source datastore and an optional filter */ |
| 84 | datastore = string2datastore(param1); |
| 85 | if (!datastore) { |
| 86 | ERR_MSG_CLEANUP("Invalid name of a datastore. Use candidate, running, startup or neither.\n"); |
| 87 | } |
| 88 | rpc = nc_rpc_getconfig(datastore, param2, NC_WD_UNKNOWN, NC_PARAMTYPE_CONST); |
| 89 | break; |
| 90 | |
| 91 | default: |
| 92 | break; |
| 93 | } |
| 94 | if (!rpc) { |
| 95 | ERR_MSG_CLEANUP("Error while creating a RPC\n"); |
| 96 | } |
| 97 | |
| 98 | /* send the RPC on the session and remember NETCONF message ID */ |
| 99 | r = nc_send_rpc(session, rpc, 100, &msg_id); |
| 100 | if (r != NC_MSG_RPC) { |
| 101 | ERR_MSG_CLEANUP("Couldn't send a RPC\n"); |
| 102 | } |
| 103 | |
| 104 | /* receive the server's reply with the expected message ID |
| 105 | * as separate rpc-reply NETCONF envelopes and the parsed YANG output itself, if any */ |
| 106 | r = nc_recv_reply(session, rpc, msg_id, 100, &envp, &op); |
| 107 | if (r != NC_MSG_REPLY) { |
| 108 | ERR_MSG_CLEANUP("Couldn't receive a reply from the server\n"); |
| 109 | } |
| 110 | |
| 111 | /* print the whole reply */ |
| 112 | if (!op) { |
| 113 | r = lyd_print_file(stdout, envp, LYD_XML, 0); |
| 114 | } else { |
| 115 | r = lyd_print_file(stdout, op, LYD_XML, 0); |
| 116 | if (r) { |
| 117 | ERR_MSG_CLEANUP("Couldn't print the RPC to stdout\n"); |
| 118 | } |
| 119 | r = lyd_print_file(stdout, envp, LYD_XML, 0); |
| 120 | } |
| 121 | if (r) { |
| 122 | ERR_MSG_CLEANUP("Couldn't print the RPC to stdout\n"); |
| 123 | } |
| 124 | |
| 125 | cleanup: |
| 126 | lyd_free_all(envp); |
| 127 | lyd_free_all(op); |
| 128 | nc_rpc_free(rpc); |
| 129 | return rc; |
| 130 | } |
| 131 | |
| 132 | int |
| 133 | main(int argc, char **argv) |
| 134 | { |
| 135 | int rc = 0, opt, connection_type = NONE; |
| 136 | struct nc_session *session = NULL; |
| 137 | const char *unix_socket_path = NULL, *rpc_parameter_1 = NULL, *rpc_parameter_2 = NULL; |
| 138 | |
| 139 | struct option options[] = { |
| 140 | {"help", no_argument, NULL, 'h'}, |
| 141 | {"unix", required_argument, NULL, 'u'}, |
| 142 | {"ssh", no_argument, NULL, 's'}, |
| 143 | {"debug", no_argument, NULL, 'd'}, |
| 144 | {NULL, 0, NULL, 0} |
| 145 | }; |
| 146 | |
| 147 | if (argc == 1) { |
| 148 | help_print(); |
| 149 | goto cleanup; |
| 150 | } |
| 151 | |
| 152 | nc_client_init(); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame^] | 153 | |
Roman Janota | 907cf93 | 2022-06-03 12:33:34 +0200 | [diff] [blame] | 154 | /* set the path to search for schemas */ |
| 155 | nc_client_set_schema_searchpath(MODULES_DIR); |
| 156 | |
| 157 | opterr = 0; |
| 158 | |
| 159 | while ((opt = getopt_long(argc, argv, "hu:sd", options, NULL)) != -1) { |
| 160 | switch (opt) { |
| 161 | case 'h': |
| 162 | help_print(); |
| 163 | goto cleanup; |
| 164 | |
| 165 | case 'u': |
| 166 | unix_socket_path = optarg; |
| 167 | connection_type = UNIX; |
| 168 | break; |
| 169 | |
| 170 | case 's': |
| 171 | connection_type = SSH; |
| 172 | /* set the client SSH username to always be used when connecting to the server */ |
| 173 | if (nc_client_ssh_set_username(SSH_USERNAME)) { |
| 174 | ERR_MSG_CLEANUP("Couldn't set the SSH username\n"); |
| 175 | } |
| 176 | break; |
| 177 | |
| 178 | case 'd': |
| 179 | nc_verbosity(NC_VERB_DEBUG); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame^] | 180 | nc_libssh_thread_verbosity(2); |
Roman Janota | 907cf93 | 2022-06-03 12:33:34 +0200 | [diff] [blame] | 181 | break; |
| 182 | |
| 183 | default: |
| 184 | ERR_MSG_CLEANUP("Invalid option or missing argument\n"); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (optind == argc) { |
| 189 | ERR_MSG_CLEANUP("Expected the name of RPC after options\n"); |
| 190 | } |
| 191 | |
| 192 | /* connect to the server using the specified transport protocol */ |
| 193 | switch (connection_type) { |
| 194 | case UNIX: |
| 195 | session = nc_connect_unix(unix_socket_path, NULL); |
| 196 | break; |
| 197 | |
| 198 | case SSH: |
| 199 | session = nc_connect_ssh(SSH_ADDRESS, SSH_PORT, NULL); |
| 200 | break; |
| 201 | |
| 202 | case NONE: |
| 203 | ERR_MSG_CLEANUP("Expected connection type (either SSH or UNIX)!\n"); |
| 204 | } |
| 205 | if (!session) { |
| 206 | ERR_MSG_CLEANUP("Couldn't connect to the server\n"); |
| 207 | } |
| 208 | |
| 209 | /* sending a get RPC */ |
| 210 | if (!strcmp(argv[optind], "get")) { |
| 211 | if (optind + 1 < argc) { |
| 212 | /* use the specified XPath filter */ |
| 213 | rpc_parameter_1 = argv[optind + 1]; |
| 214 | } |
| 215 | if (send_rpc(session, NC_RPC_GET, rpc_parameter_1, rpc_parameter_2)) { |
| 216 | rc = 1; |
| 217 | goto cleanup; |
| 218 | } |
| 219 | /* sending a get-config RPC */ |
| 220 | } else if (!strcmp(argv[optind], "get-config")) { |
| 221 | /* use the specified datastore and optional XPath filter */ |
| 222 | if (optind + 2 < argc) { |
| 223 | rpc_parameter_1 = argv[optind + 1]; |
| 224 | rpc_parameter_2 = argv[optind + 2]; |
| 225 | } else if (optind + 1 < argc) { |
| 226 | rpc_parameter_1 = argv[optind + 1]; |
| 227 | } |
| 228 | if (send_rpc(session, NC_RPC_GETCONFIG, rpc_parameter_1, rpc_parameter_2)) { |
| 229 | rc = 1; |
| 230 | goto cleanup; |
| 231 | } |
| 232 | } else { |
| 233 | ERR_MSG_CLEANUP("Invalid name of a RPC\n"); |
| 234 | } |
| 235 | |
| 236 | cleanup: |
| 237 | nc_session_free(session, NULL); |
| 238 | nc_client_destroy(); |
| 239 | return rc; |
| 240 | } |