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 | |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 60 | #define SOCKET_FILENAME "/var/run/netopeerguid.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 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 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"); |
| 77 | printf("\tinfo\n"); |
| 78 | printf("\tgeneric\n"); |
| 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 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 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" */ |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 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 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 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" */ |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 133 | int main (int argc, char* argv[]) |
| 134 | { |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 135 | json_object* msg = NULL, *reply = NULL, *obj, *obj2; |
| 136 | const char* msg_text; |
| 137 | int sock; |
| 138 | struct sockaddr_un addr; |
| 139 | size_t len; |
| 140 | char *buffer; |
| 141 | char* line = NULL, *chunked_msg_text; |
| 142 | int i; |
| 143 | int buffer_size, buffer_len, ret, chunk_len; |
| 144 | char c, chunk_len_str[12]; |
| 145 | unsigned int session_key; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 146 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 147 | if (argc != 2) { |
| 148 | print_help(argv[0]); |
| 149 | return (2); |
| 150 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 151 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 152 | /* connect to the daemon */ |
| 153 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 154 | if (sock == -1) { |
| 155 | fprintf(stderr, "Creating socket failed (%s)\n", strerror(errno)); |
| 156 | return (EXIT_FAILURE); |
| 157 | } |
| 158 | addr.sun_family = AF_UNIX; |
| 159 | strncpy(addr.sun_path, SOCKET_FILENAME, sizeof(addr.sun_path)); |
| 160 | len = strlen(addr.sun_path) + sizeof(addr.sun_family); |
| 161 | if (connect(sock, (struct sockaddr *) &addr, len) == -1) { |
Michal Vasko | 7f46ede | 2016-02-24 10:45:55 +0100 | [diff] [blame^] | 162 | fprintf(stderr, "Connecting to netopeerguid (%s) failed (%s)\n", SOCKET_FILENAME, strerror(errno)); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 163 | close(sock); |
| 164 | return (EXIT_FAILURE); |
| 165 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 166 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 167 | line = malloc(sizeof(char) * BUFFER_SIZE); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 168 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 169 | if (strcmp(argv[1], "connect") == 0) { |
| 170 | /* |
| 171 | * create NETCONF session |
| 172 | */ |
| 173 | msg = json_object_new_object(); |
| 174 | json_object_object_add(msg, "type", json_object_new_int(MSG_CONNECT)); |
| 175 | readline(&line, &len, "Hostname: "); |
| 176 | json_object_object_add(msg, "host", json_object_new_string(line)); |
| 177 | readline(&line, &len, "Port: "); |
| 178 | json_object_object_add(msg, "port", json_object_new_string(line)); |
| 179 | readline(&line, &len, "Username: "); |
| 180 | json_object_object_add(msg, "user", json_object_new_string(line)); |
| 181 | system("stty -echo"); |
| 182 | readline(&line, &len, "Password: "); |
| 183 | system("stty echo"); |
| 184 | printf("\n"); |
| 185 | json_object_object_add(msg, "pass", json_object_new_string(line)); |
Tomas Cejka | 4243446 | 2012-09-05 18:11:43 +0200 | [diff] [blame] | 186 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 187 | /* clean read password - it is needless because we have a copy in json... :-( */ |
| 188 | memset(line, 'X', len); |
| 189 | free(line); |
| 190 | line = NULL; |
| 191 | } else if (strcmp(argv[1], "disconnect") == 0) { |
| 192 | /* |
| 193 | * Close NETCONF session |
| 194 | */ |
| 195 | msg = json_object_new_object(); |
| 196 | json_object_object_add(msg, "type", json_object_new_int(MSG_DISCONNECT)); |
| 197 | readline(&line, &len, "Session: "); |
| 198 | obj = json_object_new_array(); |
| 199 | session_key = atoi(line); |
| 200 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 201 | json_object_object_add(msg, "sessions", obj); |
| 202 | } else if (strcmp(argv[1], "copy-config") == 0) { |
| 203 | /* |
| 204 | * NETCONF <copy-config> |
| 205 | */ |
| 206 | msg = json_object_new_object(); |
| 207 | json_object_object_add(msg, "type", json_object_new_int(MSG_COPYCONFIG)); |
| 208 | readline(&line, &len, "Session: "); |
| 209 | obj = json_object_new_array(); |
| 210 | session_key = atoi(line); |
| 211 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 212 | json_object_object_add(msg, "sessions", obj); |
| 213 | readline(&line, &len, "Source (running|startup|candidate): "); |
| 214 | if (strlen(line) > 0) { |
| 215 | json_object_object_add(msg, "source", json_object_new_string(line)); |
| 216 | } else { |
| 217 | readmultiline(&line, &len, "Configuration data (ending with CTRL+D): "); |
Michal Vasko | 7f46ede | 2016-02-24 10:45:55 +0100 | [diff] [blame^] | 218 | obj = json_object_new_array(); |
| 219 | json_object_array_add(obj, json_object_new_string(line)); |
| 220 | json_object_object_add(msg, "configs", obj); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 221 | } |
| 222 | readline(&line, &len, "Target (running|startup|candidate): "); |
| 223 | json_object_object_add(msg, "target", json_object_new_string(line)); |
| 224 | } else if (strcmp(argv[1], "delete-config") == 0) { |
| 225 | /* |
| 226 | * NETCONF <delete-config> |
| 227 | */ |
| 228 | msg = json_object_new_object(); |
| 229 | json_object_object_add(msg, "type", json_object_new_int(MSG_DELETECONFIG)); |
| 230 | readline(&line, &len, "Session: "); |
| 231 | obj = json_object_new_array(); |
| 232 | session_key = atoi(line); |
| 233 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 234 | json_object_object_add(msg, "sessions", obj); |
| 235 | readline(&line, &len, "Target (running|startup|candidate): "); |
| 236 | json_object_object_add(msg, "target", json_object_new_string(line)); |
| 237 | } else if (strcmp(argv[1], "edit-config") == 0) { |
| 238 | /* |
| 239 | * NETCONF <edit-config> |
| 240 | */ |
| 241 | msg = json_object_new_object(); |
| 242 | json_object_object_add(msg, "type", json_object_new_int(MSG_EDITCONFIG)); |
| 243 | readline(&line, &len, "Session: "); |
| 244 | obj = json_object_new_array(); |
| 245 | session_key = atoi(line); |
| 246 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 247 | json_object_object_add(msg, "sessions", obj); |
| 248 | readline(&line, &len, "Target (running|startup|candidate): "); |
| 249 | json_object_object_add(msg, "target", json_object_new_string(line)); |
| 250 | readline(&line, &len, "Default operation (merge|replace|none): "); |
| 251 | if (strlen(line) > 0) { |
| 252 | json_object_object_add(msg, "default-operation", json_object_new_string(line)); |
| 253 | } |
| 254 | readline(&line, &len, "Error option (stop-on-error|continue-on-error|rollback-on-error): "); |
| 255 | if (strlen(line) > 0) { |
| 256 | json_object_object_add(msg, "error-option", json_object_new_string(line)); |
| 257 | } |
| 258 | readmultiline(&line, &len, "Configuration data (ending with CTRL+D): "); |
Michal Vasko | 7f46ede | 2016-02-24 10:45:55 +0100 | [diff] [blame^] | 259 | obj = json_object_new_array(); |
| 260 | json_object_array_add(obj, json_object_new_string(line)); |
| 261 | json_object_object_add(msg, "configs", obj); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 262 | } else if (strcmp(argv[1], "get") == 0) { |
| 263 | /* |
| 264 | * NETCONF <get> |
| 265 | */ |
| 266 | msg = json_object_new_object(); |
| 267 | json_object_object_add(msg, "type", json_object_new_int(MSG_GET)); |
| 268 | readline(&line, &len, "Session: "); |
| 269 | obj = json_object_new_array(); |
| 270 | session_key = atoi(line); |
| 271 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 272 | json_object_object_add(msg, "sessions", obj); |
| 273 | readmultiline(&line, &len, "Filter (ending with CTRL+D): "); |
| 274 | if (strlen(line) > 0) { |
| 275 | json_object_object_add(msg, "filter", json_object_new_string(line)); |
| 276 | } |
| 277 | do { |
| 278 | readline(&line, &len, "Strict (y/n): "); |
| 279 | } while (strcmp(line, "y") && strcmp(line, "n")); |
| 280 | json_object_object_add(msg, "strict", json_object_new_boolean(strcmp(line, "y") ? 0 : 1)); |
| 281 | } else if (strcmp(argv[1], "get-config") == 0) { |
| 282 | /* |
| 283 | * NETCONF <get-config> |
| 284 | */ |
| 285 | msg = json_object_new_object(); |
| 286 | json_object_object_add(msg, "type", json_object_new_int(MSG_GETCONFIG)); |
| 287 | readline(&line, &len, "Session: "); |
| 288 | obj = json_object_new_array(); |
| 289 | session_key = atoi(line); |
| 290 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 291 | json_object_object_add(msg, "sessions", obj); |
| 292 | readline(&line, &len, "Source (running|startup|candidate): "); |
| 293 | json_object_object_add(msg, "source", json_object_new_string(line)); |
| 294 | readmultiline(&line, &len, "Filter (ending with CTRL+D): "); |
| 295 | if (strlen(line) > 0) { |
| 296 | json_object_object_add(msg, "filter", json_object_new_string(line)); |
| 297 | } |
| 298 | do { |
| 299 | readline(&line, &len, "Strict (y/n): "); |
| 300 | } while (strcmp(line, "y") && strcmp(line, "n")); |
| 301 | json_object_object_add(msg, "strict", json_object_new_boolean(strcmp(line, "y") ? 0 : 1)); |
| 302 | } else if (strcmp(argv[1], "kill-session") == 0) { |
| 303 | /* |
| 304 | * NETCONF <kill-session> |
| 305 | */ |
| 306 | msg = json_object_new_object(); |
| 307 | json_object_object_add(msg, "type", json_object_new_int(MSG_KILL)); |
| 308 | readline(&line, &len, "Session: "); |
| 309 | obj = json_object_new_array(); |
| 310 | session_key = atoi(line); |
| 311 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 312 | json_object_object_add(msg, "sessions", obj); |
| 313 | readline(&line, &len, "Kill session with ID: "); |
| 314 | json_object_object_add(msg, "session-id", json_object_new_string(line)); |
| 315 | } else if (strcmp(argv[1], "lock") == 0) { |
| 316 | /* |
| 317 | * NETCONF <lock> |
| 318 | */ |
| 319 | msg = json_object_new_object(); |
| 320 | json_object_object_add(msg, "type", json_object_new_int(MSG_LOCK)); |
| 321 | readline(&line, &len, "Session: "); |
| 322 | obj = json_object_new_array(); |
| 323 | session_key = atoi(line); |
| 324 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 325 | json_object_object_add(msg, "sessions", obj); |
| 326 | readline(&line, &len, "Target (running|startup|candidate): "); |
| 327 | json_object_object_add(msg, "target", json_object_new_string(line)); |
| 328 | } else if (strcmp(argv[1], "unlock") == 0) { |
| 329 | /* |
| 330 | * NETCONF <unlock> |
| 331 | */ |
| 332 | msg = json_object_new_object(); |
| 333 | json_object_object_add(msg, "type", json_object_new_int(MSG_UNLOCK)); |
| 334 | readline(&line, &len,"Session: "); |
| 335 | obj = json_object_new_array(); |
| 336 | session_key = atoi(line); |
| 337 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 338 | json_object_object_add(msg, "sessions", obj); |
| 339 | readline(&line, &len, "Target (running|startup|candidate): "); |
| 340 | json_object_object_add(msg, "target", json_object_new_string(line)); |
| 341 | } else if (strcmp(argv[1], "info") == 0) { |
| 342 | /* |
| 343 | * Get information about NETCONF session |
| 344 | */ |
| 345 | msg = json_object_new_object(); |
| 346 | json_object_object_add(msg, "type", json_object_new_int(MSG_INFO)); |
| 347 | readline(&line, &len, "Session: "); |
| 348 | obj = json_object_new_array(); |
| 349 | session_key = atoi(line); |
| 350 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 351 | json_object_object_add(msg, "sessions", obj); |
| 352 | } else if (strcmp(argv[1], "generic") == 0) { |
| 353 | /* |
| 354 | * Generic NETCONF request |
| 355 | */ |
| 356 | msg = json_object_new_object(); |
| 357 | json_object_object_add(msg, "type", json_object_new_int(MSG_GENERIC)); |
| 358 | readline(&line, &len, "Session: "); |
| 359 | obj = json_object_new_array(); |
| 360 | session_key = atoi(line); |
| 361 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 362 | json_object_object_add(msg, "sessions", obj); |
| 363 | readmultiline(&line, &len, "NETCONF <rpc> content (ending with CTRL+D): "); |
Michal Vasko | 7f46ede | 2016-02-24 10:45:55 +0100 | [diff] [blame^] | 364 | obj = json_object_new_array(); |
| 365 | json_object_array_add(obj, json_object_new_string(line)); |
| 366 | json_object_object_add(msg, "contents", obj); |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 367 | } else if (strcmp(argv[1], "getschema") == 0) { |
| 368 | /* |
| 369 | * Get information about NETCONF session |
| 370 | */ |
| 371 | msg = json_object_new_object(); |
| 372 | json_object_object_add(msg, "type", json_object_new_int(MSG_GETSCHEMA)); |
| 373 | readline(&line, &len, "Session: "); |
| 374 | obj = json_object_new_array(); |
| 375 | session_key = atoi(line); |
| 376 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 377 | json_object_object_add(msg, "sessions", obj); |
| 378 | readline(&line, &len, "Identificator: "); |
| 379 | json_object_object_add(msg, "identifier", json_object_new_string(line)); |
| 380 | readline(&line, &len, "Format [YIN]: "); |
| 381 | json_object_object_add(msg, "format", json_object_new_string(line)); |
| 382 | readline(&line, &len, "Version: "); |
| 383 | json_object_object_add(msg, "version", json_object_new_string(line)); |
| 384 | } else if (strcmp(argv[1], "query") == 0) { |
| 385 | /* |
| 386 | * Query metadata about a node |
| 387 | */ |
| 388 | msg = json_object_new_object(); |
| 389 | json_object_object_add(msg, "type", json_object_new_int(SCH_QUERY)); |
| 390 | readline(&line, &len, "Session: "); |
| 391 | obj = json_object_new_array(); |
| 392 | session_key = atoi(line); |
| 393 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 394 | json_object_object_add(msg, "sessions", obj); |
| 395 | readline(&line, &len, "Filter: "); |
| 396 | obj = json_object_new_array(); |
| 397 | json_object_array_add(obj, json_object_new_string(line)); |
| 398 | json_object_object_add(msg, "filters", obj); |
| 399 | do { |
| 400 | readline(&line, &len, "Load children (y/n): "); |
| 401 | } while (strcmp(line, "y") && strcmp(line, "n")); |
| 402 | json_object_object_add(msg, "load_children", json_object_new_boolean(strcmp(line, "y") ? 0 : 1)); |
| 403 | } else if (strcmp(argv[1], "merge") == 0) { |
| 404 | /* |
| 405 | * Merge configuration data with metadata |
| 406 | */ |
| 407 | msg = json_object_new_object(); |
| 408 | json_object_object_add(msg, "type", json_object_new_int(SCH_QUERY)); |
| 409 | readline(&line, &len, "Session: "); |
| 410 | obj = json_object_new_array(); |
| 411 | session_key = atoi(line); |
| 412 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 413 | json_object_object_add(msg, "sessions", obj); |
| 414 | readmultiline(&line, &len, "Configuration data (ending with CTRL+D): "); |
| 415 | obj = json_object_new_array(); |
| 416 | json_object_array_add(obj, json_object_new_string(line)); |
| 417 | json_object_object_add(msg, "configurations", obj); |
| 418 | } else { |
| 419 | /* |
| 420 | * Unknown request |
| 421 | */ |
| 422 | fprintf(stderr, "Unknown command %s\n", argv[1]); |
| 423 | close(sock); |
| 424 | return (EXIT_FAILURE); |
| 425 | } |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 426 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 427 | /* send the message */ |
| 428 | if (msg != NULL) { |
| 429 | msg_text = json_object_to_json_string(msg); |
| 430 | 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] | 431 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 432 | if (json_object_object_get(msg, "pass") == NULL) { |
| 433 | /* print message only if it does not contain password */ |
| 434 | printf("Sending: %s\n", msg_text); |
| 435 | } |
| 436 | send(sock, chunked_msg_text, strlen(chunked_msg_text) + 1, 0); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 437 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 438 | json_object_put(msg); |
| 439 | free (chunked_msg_text); |
| 440 | } else { |
| 441 | close(sock); |
| 442 | return (EXIT_FAILURE); |
| 443 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 444 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 445 | /* read json in chunked framing */ |
| 446 | buffer_size = 0; |
| 447 | buffer_len = 0; |
| 448 | buffer = NULL; |
| 449 | while (1) { |
| 450 | /* read chunk length */ |
| 451 | if ((ret = recv (sock, &c, 1, 0)) != 1 || c != '\n') { |
| 452 | free (buffer); |
| 453 | buffer = NULL; |
| 454 | break; |
| 455 | } |
| 456 | if ((ret = recv (sock, &c, 1, 0)) != 1 || c != '#') { |
| 457 | free (buffer); |
| 458 | buffer = NULL; |
| 459 | break; |
| 460 | } |
| 461 | i=0; |
| 462 | memset (chunk_len_str, 0, 12); |
| 463 | while ((ret = recv (sock, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) { |
| 464 | if (i==0 && c == '#') { |
| 465 | if (recv (sock, &c, 1, 0) != 1 || c != '\n') { |
| 466 | /* end but invalid */ |
| 467 | free (buffer); |
| 468 | buffer = NULL; |
| 469 | } |
| 470 | /* end of message, double-loop break */ |
| 471 | goto msg_complete; |
| 472 | } |
| 473 | chunk_len_str[i++] = c; |
| 474 | } |
| 475 | if (c != '\n') { |
| 476 | free (buffer); |
| 477 | buffer = NULL; |
| 478 | break; |
| 479 | } |
| 480 | if ((chunk_len = atoi (chunk_len_str)) == 0) { |
| 481 | free (buffer); |
| 482 | buffer = NULL; |
| 483 | break; |
| 484 | } |
| 485 | buffer_size += chunk_len+1; |
| 486 | buffer = realloc (buffer, sizeof(char)*buffer_size); |
| 487 | if ((ret = recv (sock, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) { |
| 488 | free (buffer); |
| 489 | buffer = NULL; |
| 490 | break; |
| 491 | } |
| 492 | buffer_len += ret; |
| 493 | } |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 494 | msg_complete: |
| 495 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 496 | if (buffer != NULL) { |
| 497 | reply = json_tokener_parse(buffer); |
| 498 | free (buffer); |
| 499 | } else { |
| 500 | reply = NULL; |
| 501 | } |
| 502 | free(line); |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 503 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 504 | printf("Received:\n"); |
| 505 | if (reply == NULL) { |
| 506 | printf("(null)\n"); |
| 507 | } else { |
| 508 | asprintf(&line, "%d", session_key); |
| 509 | json_object_object_get_ex(reply, line, &obj); |
| 510 | free(line); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 511 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 512 | json_object_object_get_ex(obj, "type", &obj2); |
| 513 | switch (json_object_get_int(obj2)) { |
| 514 | case 0: |
| 515 | printf("OK\n"); |
| 516 | printf("%s\n", json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
| 517 | break; |
| 518 | case 1: |
| 519 | printf("DATA\n"); |
| 520 | json_object_object_get_ex(obj, "data", &obj2); |
| 521 | msg_text = json_object_get_string(obj2); |
| 522 | obj = json_tokener_parse(msg_text); |
| 523 | printf("%s\n", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
| 524 | json_object_put(obj); |
| 525 | break; |
| 526 | case 2: |
| 527 | printf("ERROR\n"); |
| 528 | printf("%s\n", json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
| 529 | break; |
| 530 | case 3: |
| 531 | printf("INFO\n"); |
| 532 | printf("%s\n", json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
| 533 | break; |
| 534 | default: |
| 535 | printf("(unknown)\n"); |
| 536 | printf("%s\n", json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
| 537 | break; |
| 538 | } |
| 539 | json_object_put(reply); |
| 540 | } |
| 541 | close(sock); |
| 542 | |
| 543 | return (EXIT_SUCCESS); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 544 | } |