Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1 | /*! |
| 2 | * \file test-client.c |
| 3 | * \brief Testing client sending JSON requsts to the mod_netconf socket |
| 4 | * \author Radek Krejci <rkrejci@cesnet.cz> |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 5 | * \author Tomas Cejka <cejkat@cesnet.cz> |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 6 | * \date 2012 |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 7 | * \date 2013 |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 8 | */ |
| 9 | /* |
| 10 | * Copyright (C) 2012 CESNET |
| 11 | * |
| 12 | * LICENSE TERMS |
| 13 | * |
| 14 | * Redistribution and use in source and binary forms, with or without |
| 15 | * modification, are permitted provided that the following conditions |
| 16 | * are met: |
| 17 | * 1. Redistributions of source code must retain the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer. |
| 19 | * 2. Redistributions in binary form must reproduce the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer in |
| 21 | * the documentation and/or other materials provided with the |
| 22 | * distribution. |
| 23 | * 3. Neither the name of the Company nor the names of its contributors |
| 24 | * may be used to endorse or promote products derived from this |
| 25 | * software without specific prior written permission. |
| 26 | * |
| 27 | * ALTERNATIVELY, provided that this notice is retained in full, this |
| 28 | * product may be distributed under the terms of the GNU General Public |
| 29 | * License (GPL) version 2 or later, in which case the provisions |
| 30 | * of the GPL apply INSTEAD OF those given above. |
| 31 | * |
| 32 | * This software is provided ``as is'', and any express or implied |
| 33 | * warranties, including, but not limited to, the implied warranties of |
| 34 | * merchantability and fitness for a particular purpose are disclaimed. |
| 35 | * In no event shall the company or contributors be liable for any |
| 36 | * direct, indirect, incidental, special, exemplary, or consequential |
| 37 | * damages (including, but not limited to, procurement of substitute |
| 38 | * goods or services; loss of use, data, or profits; or business |
| 39 | * interruption) however caused and on any theory of liability, whether |
| 40 | * in contract, strict liability, or tort (including negligence or |
| 41 | * otherwise) arising in any way out of the use of this software, even |
| 42 | * if advised of the possibility of such damage. |
| 43 | * |
| 44 | */ |
| 45 | |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 46 | #define _GNU_SOURCE |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 47 | #include <unistd.h> |
| 48 | #include <stdio.h> |
| 49 | #include <stdlib.h> |
| 50 | #include <string.h> |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 51 | #include <err.h> |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 52 | #include <errno.h> |
| 53 | #include <sys/types.h> |
| 54 | #include <sys/socket.h> |
| 55 | #include <sys/un.h> |
| 56 | #include <json/json.h> |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 57 | #include <ctype.h> |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 58 | #include "message_type.h" |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 59 | |
Radek Krejci | 6cb0898 | 2012-07-25 18:01:06 +0200 | [diff] [blame] | 60 | #define SOCKET_FILENAME "/tmp/mod_netconf.sock" |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 61 | #define BUFFER_SIZE 40960 |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 62 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 63 | void print_help(char* progname) |
| 64 | { |
| 65 | printf("Usage: %s <command>\n", progname); |
| 66 | printf("Available commands:\n"); |
| 67 | printf("\tconnect\n"); |
| 68 | printf("\tdisconnect\n"); |
| 69 | printf("\tcopy-config\n"); |
| 70 | printf("\tdelete-config\n"); |
| 71 | printf("\tedit-config\n"); |
| 72 | printf("\tget\n"); |
| 73 | printf("\tget-config\n"); |
| 74 | printf("\tkill-session\n"); |
| 75 | printf("\tlock\n"); |
| 76 | printf("\tunlock\n"); |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 77 | printf("\tinfo\n"); |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 78 | printf("\tgeneric\n"); |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 79 | printf("\tgetschema\n"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 82 | /** |
| 83 | * \brief Get multiline input text. |
| 84 | * |
| 85 | * Print given prompt and read text ending with CTRL+D. |
| 86 | * Output string is terminated by 0. Ending '\n' is removed. |
| 87 | * |
| 88 | * On error, err is called! |
| 89 | * |
| 90 | * \param[out] output - pointer to memory where string is stored |
| 91 | * \param[out] size - size of string return by getdelim() |
| 92 | * \param[in] prompt - text printed as a prompt |
| 93 | */ |
| 94 | void readmultiline(char **output, size_t *size, const char *prompt) |
| 95 | { |
| 96 | printf(prompt); |
| 97 | if (getdelim (output, size, 'D' - 0x40, stdin) == -1) { |
| 98 | if (errno) { |
| 99 | err(errno, "Cannot read input."); |
| 100 | } |
| 101 | *output = (char *) malloc(sizeof(char)); |
| 102 | **output = 0; |
| 103 | return; |
| 104 | } |
| 105 | (*output)[(*size)-1] = 0; /* input text end "sanitation" */ |
| 106 | (*output)[(strlen(*output))-1] = 0; /* input text end "sanitation" */ |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * \brief Get input text. |
| 111 | * |
| 112 | * Print given prompt and read one line of text. |
| 113 | * Output string is terminated by 0. Ending '\n' is removed. |
| 114 | * |
| 115 | * On error, err is called! |
| 116 | * |
| 117 | * \param[out] output - pointer to memory where string is stored |
| 118 | * \param[out] size - size of string return by getline() |
| 119 | * \param[in] prompt - text printed as a prompt |
| 120 | */ |
| 121 | void readline(char **output, size_t *size, const char *prompt) |
| 122 | { |
| 123 | printf(prompt); |
| 124 | if (getline (output, size, stdin) == -1) { |
| 125 | if (errno) { |
| 126 | err(errno, "Cannot read input."); |
| 127 | } |
| 128 | } |
| 129 | (*output)[(*size)-1] = 0; /* input text end "sanitation" */ |
| 130 | (*output)[(strlen(*output))-1] = 0; /* input text end "sanitation" */ |
| 131 | } |
| 132 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 133 | int main (int argc, char* argv[]) |
| 134 | { |
Kupka David | 00b9c5c | 2012-09-05 09:45:50 +0200 | [diff] [blame] | 135 | json_object* msg = NULL, *reply = NULL, *capabilities = NULL; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 136 | const char* msg_text; |
| 137 | int sock; |
| 138 | struct sockaddr_un addr; |
| 139 | size_t len; |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 140 | char *buffer; |
| 141 | char* line = NULL, *chunked_msg_text; |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 142 | int i, alen; |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 143 | int buffer_size, buffer_len, ret, chunk_len; |
| 144 | char c, chunk_len_str[12]; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 145 | |
| 146 | if (argc != 2) { |
| 147 | print_help(argv[0]); |
| 148 | return (2); |
| 149 | } |
| 150 | |
| 151 | /* connect to the daemon */ |
| 152 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 153 | if (sock == -1) { |
| 154 | fprintf(stderr, "Creating socket failed (%s)", strerror(errno)); |
| 155 | return (EXIT_FAILURE); |
| 156 | } |
| 157 | addr.sun_family = AF_UNIX; |
| 158 | strncpy(addr.sun_path, SOCKET_FILENAME, sizeof(addr.sun_path)); |
| 159 | len = strlen(addr.sun_path) + sizeof(addr.sun_family); |
| 160 | if (connect(sock, (struct sockaddr *) &addr, len) == -1) { |
| 161 | fprintf(stderr, "Connecting to mod_netconf (%s) failed (%s)", SOCKET_FILENAME, strerror(errno)); |
| 162 | close(sock); |
| 163 | return (EXIT_FAILURE); |
| 164 | } |
| 165 | |
| 166 | line = malloc(sizeof(char) * BUFFER_SIZE); |
| 167 | |
| 168 | if (strcmp(argv[1], "connect") == 0) { |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 169 | /* |
| 170 | * create NETCONF session |
| 171 | */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 172 | msg = json_object_new_object(); |
| 173 | json_object_object_add(msg, "type", json_object_new_int(MSG_CONNECT)); |
Tomas Cejka | 4243446 | 2012-09-05 18:11:43 +0200 | [diff] [blame] | 174 | readline(&line, &len, "Hostname: "); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 175 | json_object_object_add(msg, "host", json_object_new_string(line)); |
Tomas Cejka | 4243446 | 2012-09-05 18:11:43 +0200 | [diff] [blame] | 176 | readline(&line, &len, "Port: "); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 177 | json_object_object_add(msg, "port", json_object_new_string(line)); |
Tomas Cejka | 4243446 | 2012-09-05 18:11:43 +0200 | [diff] [blame] | 178 | readline(&line, &len, "Username: "); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 179 | json_object_object_add(msg, "user", json_object_new_string(line)); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 180 | system("stty -echo"); |
Tomas Cejka | 4243446 | 2012-09-05 18:11:43 +0200 | [diff] [blame] | 181 | readline(&line, &len, "Password: "); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 182 | system("stty echo"); |
| 183 | printf("\n"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 184 | json_object_object_add(msg, "pass", json_object_new_string(line)); |
Tomas Cejka | 4243446 | 2012-09-05 18:11:43 +0200 | [diff] [blame] | 185 | |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 186 | /* clean read password - it is needless because we have a copy in json... :-( */ |
| 187 | memset(line, 'X', len); |
| 188 | free(line); |
| 189 | line = NULL; |
| 190 | |
| 191 | printf("Supported capabilities\n"); |
| 192 | capabilities = json_object_new_array(); |
| 193 | json_object_object_add(msg, "capabilities", capabilities); |
| 194 | while (1) { |
| 195 | readline(&line, &len, "Next capability (empty for end): "); |
| 196 | if (strlen(line) == 0) { |
| 197 | break; |
| 198 | } |
| 199 | json_object_array_add (capabilities, json_object_new_string(line)); |
| 200 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 201 | } else if (strcmp(argv[1], "disconnect") == 0) { |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 202 | /* |
| 203 | * Close NETCONF session |
| 204 | */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 205 | msg = json_object_new_object(); |
| 206 | json_object_object_add(msg, "type", json_object_new_int(MSG_DISCONNECT)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 207 | readline(&line, &len, "Session: "); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 208 | json_object_object_add(msg, "session", json_object_new_string(line)); |
| 209 | } else if (strcmp(argv[1], "copy-config") == 0) { |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 210 | /* |
| 211 | * NETCONF <copy-config> |
| 212 | */ |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 213 | msg = json_object_new_object(); |
| 214 | json_object_object_add(msg, "type", json_object_new_int(MSG_COPYCONFIG)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 215 | readline(&line, &len, "Session: "); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 216 | json_object_object_add(msg, "session", json_object_new_string(line)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 217 | readline(&line, &len, "Source (running|startup|candidate): "); |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 218 | if (strlen(line) > 0) { |
| 219 | json_object_object_add(msg, "source", json_object_new_string(line)); |
| 220 | } else { |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 221 | readmultiline(&line, &len, "Configuration data (ending with CTRL+D): "); |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 222 | json_object_object_add(msg, "config", json_object_new_string(line)); |
| 223 | } |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 224 | readline(&line, &len, "Target (running|startup|candidate): "); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 225 | json_object_object_add(msg, "target", json_object_new_string(line)); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 226 | } else if (strcmp(argv[1], "delete-config") == 0) { |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 227 | /* |
| 228 | * NETCONF <delete-config> |
| 229 | */ |
| 230 | msg = json_object_new_object(); |
| 231 | json_object_object_add(msg, "type", json_object_new_int(MSG_DELETECONFIG)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 232 | readline(&line, &len, "Session: "); |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 233 | json_object_object_add(msg, "session", json_object_new_string(line)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 234 | readline(&line, &len, "Target (running|startup|candidate): "); |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 235 | json_object_object_add(msg, "target", json_object_new_string(line)); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 236 | } else if (strcmp(argv[1], "edit-config") == 0) { |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 237 | /* |
| 238 | * NETCONF <edit-config> |
| 239 | */ |
| 240 | msg = json_object_new_object(); |
| 241 | json_object_object_add(msg, "type", json_object_new_int(MSG_EDITCONFIG)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 242 | readline(&line, &len, "Session: "); |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 243 | json_object_object_add(msg, "session", json_object_new_string(line)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 244 | readline(&line, &len, "Target (running|startup|candidate): "); |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 245 | json_object_object_add(msg, "target", json_object_new_string(line)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 246 | readline(&line, &len, "Default operation (merge|replace|none): "); |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 247 | if (strlen(line) > 0) { |
| 248 | json_object_object_add(msg, "default-operation", json_object_new_string(line)); |
| 249 | } |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 250 | readline(&line, &len, "Error option (stop-on-error|continue-on-error|rollback-on-error): "); |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 251 | if (strlen(line) > 0) { |
| 252 | json_object_object_add(msg, "error-option", json_object_new_string(line)); |
| 253 | } |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 254 | readmultiline(&line, &len, "Configuration data (ending with CTRL+D): "); |
Radek Krejci | b1e08f4 | 2012-07-26 10:54:28 +0200 | [diff] [blame] | 255 | json_object_object_add(msg, "config", json_object_new_string(line)); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 256 | } else if (strcmp(argv[1], "get") == 0) { |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 257 | /* |
| 258 | * NETCONF <get> |
| 259 | */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 260 | msg = json_object_new_object(); |
| 261 | json_object_object_add(msg, "type", json_object_new_int(MSG_GET)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 262 | readline(&line, &len, "Session: "); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 263 | json_object_object_add(msg, "session", json_object_new_string(line)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 264 | readmultiline(&line, &len, "Filter (ending with CTRL+D): "); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 265 | if (strlen(line) > 0) { |
| 266 | json_object_object_add(msg, "filter", json_object_new_string(line)); |
| 267 | } |
| 268 | } else if (strcmp(argv[1], "get-config") == 0) { |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 269 | /* |
| 270 | * NETCONF <get-config> |
| 271 | */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 272 | msg = json_object_new_object(); |
| 273 | json_object_object_add(msg, "type", json_object_new_int(MSG_GETCONFIG)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 274 | readline(&line, &len, "Session: "); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 275 | json_object_object_add(msg, "session", json_object_new_string(line)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 276 | readline(&line, &len, "Source (running|startup|candidate): "); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 277 | json_object_object_add(msg, "source", json_object_new_string(line)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 278 | readmultiline(&line, &len, "Filter (ending with CTRL+D): "); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 279 | if (strlen(line) > 0) { |
| 280 | json_object_object_add(msg, "filter", json_object_new_string(line)); |
| 281 | } |
| 282 | } else if (strcmp(argv[1], "kill-session") == 0) { |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 283 | /* |
| 284 | * NETCONF <kill-session> |
| 285 | */ |
| 286 | msg = json_object_new_object(); |
| 287 | json_object_object_add(msg, "type", json_object_new_int(MSG_KILL)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 288 | readline(&line, &len, "Session: "); |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 289 | json_object_object_add(msg, "session", json_object_new_string(line)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 290 | readline(&line, &len, "Kill session with ID: "); |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 291 | json_object_object_add(msg, "session-id", json_object_new_string(line)); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 292 | } else if (strcmp(argv[1], "lock") == 0) { |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 293 | /* |
| 294 | * NETCONF <lock> |
| 295 | */ |
| 296 | msg = json_object_new_object(); |
| 297 | json_object_object_add(msg, "type", json_object_new_int(MSG_LOCK)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 298 | readline(&line, &len, "Session: "); |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 299 | json_object_object_add(msg, "session", json_object_new_string(line)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 300 | readline(&line, &len, "Target (running|startup|candidate): "); |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 301 | json_object_object_add(msg, "target", json_object_new_string(line)); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 302 | } else if (strcmp(argv[1], "unlock") == 0) { |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 303 | /* |
| 304 | * NETCONF <unlock> |
| 305 | */ |
| 306 | msg = json_object_new_object(); |
| 307 | json_object_object_add(msg, "type", json_object_new_int(MSG_UNLOCK)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 308 | readline(&line, &len,"Session: "); |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 309 | json_object_object_add(msg, "session", json_object_new_string(line)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 310 | readline(&line, &len, "Target (running|startup|candidate): "); |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 311 | json_object_object_add(msg, "target", json_object_new_string(line)); |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 312 | } else if (strcmp(argv[1], "info") == 0) { |
| 313 | /* |
| 314 | * Get information about NETCONF session |
| 315 | */ |
| 316 | msg = json_object_new_object(); |
| 317 | json_object_object_add(msg, "type", json_object_new_int(MSG_INFO)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 318 | readline(&line, &len, "Session: "); |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 319 | json_object_object_add(msg, "session", json_object_new_string(line)); |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 320 | } else if (strcmp(argv[1], "generic") == 0) { |
| 321 | /* |
| 322 | * Generic NETCONF request |
| 323 | */ |
| 324 | msg = json_object_new_object(); |
| 325 | json_object_object_add(msg, "type", json_object_new_int(MSG_GENERIC)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 326 | readline(&line, &len, "Session: "); |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 327 | json_object_object_add(msg, "session", json_object_new_string(line)); |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 328 | readmultiline(&line, &len, "NETCONF <rpc> content (ending with CTRL+D): "); |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame] | 329 | json_object_object_add(msg, "content", json_object_new_string(line)); |
Tomas Cejka | 94da2c5 | 2013-01-08 18:20:30 +0100 | [diff] [blame] | 330 | } else if (strcmp(argv[1], "getschema") == 0) { |
| 331 | /* |
| 332 | * Get information about NETCONF session |
| 333 | */ |
| 334 | msg = json_object_new_object(); |
| 335 | json_object_object_add(msg, "type", json_object_new_int(MSG_GETSCHEMA)); |
| 336 | readline(&line, &len, "Session: "); |
| 337 | json_object_object_add(msg, "session", json_object_new_string(line)); |
| 338 | readline(&line, &len, "Identificator: "); |
| 339 | json_object_object_add(msg, "identifier", json_object_new_string(line)); |
| 340 | readline(&line, &len, "Format [YIN]: "); |
| 341 | json_object_object_add(msg, "format", json_object_new_string(line)); |
| 342 | readline(&line, &len, "Version: "); |
| 343 | json_object_object_add(msg, "version", json_object_new_string(line)); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 344 | } else { |
Radek Krejci | aa422df | 2012-07-25 18:04:59 +0200 | [diff] [blame] | 345 | /* |
| 346 | * Unknown request |
| 347 | */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 348 | fprintf(stderr, "Unknown command %s\n", argv[1]); |
| 349 | close(sock); |
| 350 | return (EXIT_FAILURE); |
| 351 | } |
| 352 | |
| 353 | /* send the message */ |
| 354 | if (msg != NULL) { |
| 355 | msg_text = json_object_to_json_string(msg); |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 356 | asprintf (&chunked_msg_text, "\n#%d\n%s\n##\n", (int)strlen(msg_text), msg_text); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 357 | |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 358 | if (json_object_object_get(msg, "pass") == NULL) { |
| 359 | /* print message only if it does not contain password */ |
| 360 | printf("Sending: %s\n", msg_text); |
| 361 | } |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 362 | send(sock, chunked_msg_text, strlen(chunked_msg_text) + 1, 0); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 363 | |
| 364 | json_object_put(msg); |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 365 | free (chunked_msg_text); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 366 | } else { |
| 367 | close(sock); |
| 368 | return (EXIT_FAILURE); |
| 369 | } |
| 370 | |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 371 | /* read json in chunked framing */ |
| 372 | buffer_size = 0; |
| 373 | buffer_len = 0; |
| 374 | buffer = NULL; |
| 375 | while (1) { |
| 376 | /* read chunk length */ |
| 377 | if ((ret = recv (sock, &c, 1, 0)) != 1 || c != '\n') { |
| 378 | free (buffer); |
| 379 | buffer = NULL; |
| 380 | break; |
| 381 | } |
| 382 | if ((ret = recv (sock, &c, 1, 0)) != 1 || c != '#') { |
| 383 | free (buffer); |
| 384 | buffer = NULL; |
| 385 | break; |
| 386 | } |
| 387 | i=0; |
| 388 | memset (chunk_len_str, 0, 12); |
| 389 | while ((ret = recv (sock, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) { |
| 390 | if (i==0 && c == '#') { |
| 391 | if (recv (sock, &c, 1, 0) != 1 || c != '\n') { |
| 392 | /* end but invalid */ |
| 393 | free (buffer); |
| 394 | buffer = NULL; |
| 395 | } |
| 396 | /* end of message, double-loop break */ |
| 397 | goto msg_complete; |
| 398 | } |
| 399 | chunk_len_str[i++] = c; |
| 400 | } |
| 401 | if (c != '\n') { |
| 402 | free (buffer); |
| 403 | buffer = NULL; |
| 404 | break; |
| 405 | } |
| 406 | if ((chunk_len = atoi (chunk_len_str)) == 0) { |
| 407 | free (buffer); |
| 408 | buffer = NULL; |
| 409 | break; |
| 410 | } |
| 411 | buffer_size += chunk_len+1; |
| 412 | buffer = realloc (buffer, sizeof(char)*buffer_size); |
| 413 | if ((ret = recv (sock, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) { |
| 414 | free (buffer); |
| 415 | buffer = NULL; |
| 416 | break; |
| 417 | } |
| 418 | buffer_len += ret; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 419 | } |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 420 | msg_complete: |
| 421 | |
| 422 | if (buffer != NULL) { |
| 423 | reply = json_tokener_parse(buffer); |
| 424 | free (buffer); |
| 425 | } else { |
| 426 | reply = NULL; |
| 427 | } |
| 428 | |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 429 | printf("Received:\n"); |
| 430 | if (reply == NULL) { |
| 431 | printf("(null)\n"); |
| 432 | } else { |
| 433 | json_object_object_foreach(reply, key, value) { |
| 434 | printf("Key: %s, Value: ", key); |
| 435 | switch (json_object_get_type(value)) { |
| 436 | case json_type_string: |
| 437 | printf("%s\n", json_object_get_string(value)); |
| 438 | break; |
| 439 | case json_type_int: |
| 440 | printf("%d\n", json_object_get_int(value)); |
| 441 | break; |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 442 | case json_type_array: |
| 443 | printf("\n"); |
| 444 | alen = json_object_array_length(value); |
| 445 | for (i = 0; i < alen; i++) { |
| 446 | printf("\t(%d) %s\n", i, json_object_get_string(json_object_array_get_idx(value, i))); |
| 447 | } |
| 448 | break; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 449 | default: |
| 450 | printf("\n"); |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | json_object_put(reply); |
| 455 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 456 | close(sock); |
| 457 | free(line); |
| 458 | |
| 459 | return (EXIT_SUCCESS); |
| 460 | } |