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) { |
| 162 | fprintf(stderr, "Connecting to mod_netconf (%s) failed (%s)\n", SOCKET_FILENAME, strerror(errno)); |
| 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): "); |
| 218 | json_object_object_add(msg, "config", json_object_new_string(line)); |
| 219 | } |
| 220 | readline(&line, &len, "Target (running|startup|candidate): "); |
| 221 | json_object_object_add(msg, "target", json_object_new_string(line)); |
| 222 | } else if (strcmp(argv[1], "delete-config") == 0) { |
| 223 | /* |
| 224 | * NETCONF <delete-config> |
| 225 | */ |
| 226 | msg = json_object_new_object(); |
| 227 | json_object_object_add(msg, "type", json_object_new_int(MSG_DELETECONFIG)); |
| 228 | readline(&line, &len, "Session: "); |
| 229 | obj = json_object_new_array(); |
| 230 | session_key = atoi(line); |
| 231 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 232 | json_object_object_add(msg, "sessions", obj); |
| 233 | readline(&line, &len, "Target (running|startup|candidate): "); |
| 234 | json_object_object_add(msg, "target", json_object_new_string(line)); |
| 235 | } else if (strcmp(argv[1], "edit-config") == 0) { |
| 236 | /* |
| 237 | * NETCONF <edit-config> |
| 238 | */ |
| 239 | msg = json_object_new_object(); |
| 240 | json_object_object_add(msg, "type", json_object_new_int(MSG_EDITCONFIG)); |
| 241 | readline(&line, &len, "Session: "); |
| 242 | obj = json_object_new_array(); |
| 243 | session_key = atoi(line); |
| 244 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 245 | json_object_object_add(msg, "sessions", obj); |
| 246 | readline(&line, &len, "Target (running|startup|candidate): "); |
| 247 | json_object_object_add(msg, "target", json_object_new_string(line)); |
| 248 | readline(&line, &len, "Default operation (merge|replace|none): "); |
| 249 | if (strlen(line) > 0) { |
| 250 | json_object_object_add(msg, "default-operation", json_object_new_string(line)); |
| 251 | } |
| 252 | readline(&line, &len, "Error option (stop-on-error|continue-on-error|rollback-on-error): "); |
| 253 | if (strlen(line) > 0) { |
| 254 | json_object_object_add(msg, "error-option", json_object_new_string(line)); |
| 255 | } |
| 256 | readmultiline(&line, &len, "Configuration data (ending with CTRL+D): "); |
| 257 | json_object_object_add(msg, "config", json_object_new_string(line)); |
| 258 | } else if (strcmp(argv[1], "get") == 0) { |
| 259 | /* |
| 260 | * NETCONF <get> |
| 261 | */ |
| 262 | msg = json_object_new_object(); |
| 263 | json_object_object_add(msg, "type", json_object_new_int(MSG_GET)); |
| 264 | readline(&line, &len, "Session: "); |
| 265 | obj = json_object_new_array(); |
| 266 | session_key = atoi(line); |
| 267 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 268 | json_object_object_add(msg, "sessions", obj); |
| 269 | readmultiline(&line, &len, "Filter (ending with CTRL+D): "); |
| 270 | if (strlen(line) > 0) { |
| 271 | json_object_object_add(msg, "filter", json_object_new_string(line)); |
| 272 | } |
| 273 | do { |
| 274 | readline(&line, &len, "Strict (y/n): "); |
| 275 | } while (strcmp(line, "y") && strcmp(line, "n")); |
| 276 | json_object_object_add(msg, "strict", json_object_new_boolean(strcmp(line, "y") ? 0 : 1)); |
| 277 | } else if (strcmp(argv[1], "get-config") == 0) { |
| 278 | /* |
| 279 | * NETCONF <get-config> |
| 280 | */ |
| 281 | msg = json_object_new_object(); |
| 282 | json_object_object_add(msg, "type", json_object_new_int(MSG_GETCONFIG)); |
| 283 | readline(&line, &len, "Session: "); |
| 284 | obj = json_object_new_array(); |
| 285 | session_key = atoi(line); |
| 286 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 287 | json_object_object_add(msg, "sessions", obj); |
| 288 | readline(&line, &len, "Source (running|startup|candidate): "); |
| 289 | json_object_object_add(msg, "source", json_object_new_string(line)); |
| 290 | readmultiline(&line, &len, "Filter (ending with CTRL+D): "); |
| 291 | if (strlen(line) > 0) { |
| 292 | json_object_object_add(msg, "filter", json_object_new_string(line)); |
| 293 | } |
| 294 | do { |
| 295 | readline(&line, &len, "Strict (y/n): "); |
| 296 | } while (strcmp(line, "y") && strcmp(line, "n")); |
| 297 | json_object_object_add(msg, "strict", json_object_new_boolean(strcmp(line, "y") ? 0 : 1)); |
| 298 | } else if (strcmp(argv[1], "kill-session") == 0) { |
| 299 | /* |
| 300 | * NETCONF <kill-session> |
| 301 | */ |
| 302 | msg = json_object_new_object(); |
| 303 | json_object_object_add(msg, "type", json_object_new_int(MSG_KILL)); |
| 304 | readline(&line, &len, "Session: "); |
| 305 | obj = json_object_new_array(); |
| 306 | session_key = atoi(line); |
| 307 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 308 | json_object_object_add(msg, "sessions", obj); |
| 309 | readline(&line, &len, "Kill session with ID: "); |
| 310 | json_object_object_add(msg, "session-id", json_object_new_string(line)); |
| 311 | } else if (strcmp(argv[1], "lock") == 0) { |
| 312 | /* |
| 313 | * NETCONF <lock> |
| 314 | */ |
| 315 | msg = json_object_new_object(); |
| 316 | json_object_object_add(msg, "type", json_object_new_int(MSG_LOCK)); |
| 317 | readline(&line, &len, "Session: "); |
| 318 | obj = json_object_new_array(); |
| 319 | session_key = atoi(line); |
| 320 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 321 | json_object_object_add(msg, "sessions", obj); |
| 322 | readline(&line, &len, "Target (running|startup|candidate): "); |
| 323 | json_object_object_add(msg, "target", json_object_new_string(line)); |
| 324 | } else if (strcmp(argv[1], "unlock") == 0) { |
| 325 | /* |
| 326 | * NETCONF <unlock> |
| 327 | */ |
| 328 | msg = json_object_new_object(); |
| 329 | json_object_object_add(msg, "type", json_object_new_int(MSG_UNLOCK)); |
| 330 | readline(&line, &len,"Session: "); |
| 331 | obj = json_object_new_array(); |
| 332 | session_key = atoi(line); |
| 333 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 334 | json_object_object_add(msg, "sessions", obj); |
| 335 | readline(&line, &len, "Target (running|startup|candidate): "); |
| 336 | json_object_object_add(msg, "target", json_object_new_string(line)); |
| 337 | } else if (strcmp(argv[1], "info") == 0) { |
| 338 | /* |
| 339 | * Get information about NETCONF session |
| 340 | */ |
| 341 | msg = json_object_new_object(); |
| 342 | json_object_object_add(msg, "type", json_object_new_int(MSG_INFO)); |
| 343 | readline(&line, &len, "Session: "); |
| 344 | obj = json_object_new_array(); |
| 345 | session_key = atoi(line); |
| 346 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 347 | json_object_object_add(msg, "sessions", obj); |
| 348 | } else if (strcmp(argv[1], "generic") == 0) { |
| 349 | /* |
| 350 | * Generic NETCONF request |
| 351 | */ |
| 352 | msg = json_object_new_object(); |
| 353 | json_object_object_add(msg, "type", json_object_new_int(MSG_GENERIC)); |
| 354 | readline(&line, &len, "Session: "); |
| 355 | obj = json_object_new_array(); |
| 356 | session_key = atoi(line); |
| 357 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 358 | json_object_object_add(msg, "sessions", obj); |
| 359 | readmultiline(&line, &len, "NETCONF <rpc> content (ending with CTRL+D): "); |
| 360 | json_object_object_add(msg, "content", json_object_new_string(line)); |
| 361 | } else if (strcmp(argv[1], "getschema") == 0) { |
| 362 | /* |
| 363 | * Get information about NETCONF session |
| 364 | */ |
| 365 | msg = json_object_new_object(); |
| 366 | json_object_object_add(msg, "type", json_object_new_int(MSG_GETSCHEMA)); |
| 367 | readline(&line, &len, "Session: "); |
| 368 | obj = json_object_new_array(); |
| 369 | session_key = atoi(line); |
| 370 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 371 | json_object_object_add(msg, "sessions", obj); |
| 372 | readline(&line, &len, "Identificator: "); |
| 373 | json_object_object_add(msg, "identifier", json_object_new_string(line)); |
| 374 | readline(&line, &len, "Format [YIN]: "); |
| 375 | json_object_object_add(msg, "format", json_object_new_string(line)); |
| 376 | readline(&line, &len, "Version: "); |
| 377 | json_object_object_add(msg, "version", json_object_new_string(line)); |
| 378 | } else if (strcmp(argv[1], "query") == 0) { |
| 379 | /* |
| 380 | * Query metadata about a node |
| 381 | */ |
| 382 | msg = json_object_new_object(); |
| 383 | json_object_object_add(msg, "type", json_object_new_int(SCH_QUERY)); |
| 384 | readline(&line, &len, "Session: "); |
| 385 | obj = json_object_new_array(); |
| 386 | session_key = atoi(line); |
| 387 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 388 | json_object_object_add(msg, "sessions", obj); |
| 389 | readline(&line, &len, "Filter: "); |
| 390 | obj = json_object_new_array(); |
| 391 | json_object_array_add(obj, json_object_new_string(line)); |
| 392 | json_object_object_add(msg, "filters", obj); |
| 393 | do { |
| 394 | readline(&line, &len, "Load children (y/n): "); |
| 395 | } while (strcmp(line, "y") && strcmp(line, "n")); |
| 396 | json_object_object_add(msg, "load_children", json_object_new_boolean(strcmp(line, "y") ? 0 : 1)); |
| 397 | } else if (strcmp(argv[1], "merge") == 0) { |
| 398 | /* |
| 399 | * Merge configuration data with metadata |
| 400 | */ |
| 401 | msg = json_object_new_object(); |
| 402 | json_object_object_add(msg, "type", json_object_new_int(SCH_QUERY)); |
| 403 | readline(&line, &len, "Session: "); |
| 404 | obj = json_object_new_array(); |
| 405 | session_key = atoi(line); |
| 406 | json_object_array_add(obj, json_object_new_int(session_key)); |
| 407 | json_object_object_add(msg, "sessions", obj); |
| 408 | readmultiline(&line, &len, "Configuration data (ending with CTRL+D): "); |
| 409 | obj = json_object_new_array(); |
| 410 | json_object_array_add(obj, json_object_new_string(line)); |
| 411 | json_object_object_add(msg, "configurations", obj); |
| 412 | } else { |
| 413 | /* |
| 414 | * Unknown request |
| 415 | */ |
| 416 | fprintf(stderr, "Unknown command %s\n", argv[1]); |
| 417 | close(sock); |
| 418 | return (EXIT_FAILURE); |
| 419 | } |
Tomas Cejka | b272bf1 | 2012-09-05 16:49:12 +0200 | [diff] [blame] | 420 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 421 | /* send the message */ |
| 422 | if (msg != NULL) { |
| 423 | msg_text = json_object_to_json_string(msg); |
| 424 | 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] | 425 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 426 | if (json_object_object_get(msg, "pass") == NULL) { |
| 427 | /* print message only if it does not contain password */ |
| 428 | printf("Sending: %s\n", msg_text); |
| 429 | } |
| 430 | send(sock, chunked_msg_text, strlen(chunked_msg_text) + 1, 0); |
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 | json_object_put(msg); |
| 433 | free (chunked_msg_text); |
| 434 | } else { |
| 435 | close(sock); |
| 436 | return (EXIT_FAILURE); |
| 437 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 438 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 439 | /* read json in chunked framing */ |
| 440 | buffer_size = 0; |
| 441 | buffer_len = 0; |
| 442 | buffer = NULL; |
| 443 | while (1) { |
| 444 | /* read chunk length */ |
| 445 | if ((ret = recv (sock, &c, 1, 0)) != 1 || c != '\n') { |
| 446 | free (buffer); |
| 447 | buffer = NULL; |
| 448 | break; |
| 449 | } |
| 450 | if ((ret = recv (sock, &c, 1, 0)) != 1 || c != '#') { |
| 451 | free (buffer); |
| 452 | buffer = NULL; |
| 453 | break; |
| 454 | } |
| 455 | i=0; |
| 456 | memset (chunk_len_str, 0, 12); |
| 457 | while ((ret = recv (sock, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) { |
| 458 | if (i==0 && c == '#') { |
| 459 | if (recv (sock, &c, 1, 0) != 1 || c != '\n') { |
| 460 | /* end but invalid */ |
| 461 | free (buffer); |
| 462 | buffer = NULL; |
| 463 | } |
| 464 | /* end of message, double-loop break */ |
| 465 | goto msg_complete; |
| 466 | } |
| 467 | chunk_len_str[i++] = c; |
| 468 | } |
| 469 | if (c != '\n') { |
| 470 | free (buffer); |
| 471 | buffer = NULL; |
| 472 | break; |
| 473 | } |
| 474 | if ((chunk_len = atoi (chunk_len_str)) == 0) { |
| 475 | free (buffer); |
| 476 | buffer = NULL; |
| 477 | break; |
| 478 | } |
| 479 | buffer_size += chunk_len+1; |
| 480 | buffer = realloc (buffer, sizeof(char)*buffer_size); |
| 481 | if ((ret = recv (sock, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) { |
| 482 | free (buffer); |
| 483 | buffer = NULL; |
| 484 | break; |
| 485 | } |
| 486 | buffer_len += ret; |
| 487 | } |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 488 | msg_complete: |
| 489 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 490 | if (buffer != NULL) { |
| 491 | reply = json_tokener_parse(buffer); |
| 492 | free (buffer); |
| 493 | } else { |
| 494 | reply = NULL; |
| 495 | } |
| 496 | free(line); |
David Kupka | 1e3e4c8 | 2012-09-04 09:32:15 +0200 | [diff] [blame] | 497 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 498 | printf("Received:\n"); |
| 499 | if (reply == NULL) { |
| 500 | printf("(null)\n"); |
| 501 | } else { |
| 502 | asprintf(&line, "%d", session_key); |
| 503 | json_object_object_get_ex(reply, line, &obj); |
| 504 | free(line); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 505 | |
Michal Vasko | da0ab5c | 2015-11-13 13:24:51 +0100 | [diff] [blame] | 506 | json_object_object_get_ex(obj, "type", &obj2); |
| 507 | switch (json_object_get_int(obj2)) { |
| 508 | case 0: |
| 509 | printf("OK\n"); |
| 510 | printf("%s\n", json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
| 511 | break; |
| 512 | case 1: |
| 513 | printf("DATA\n"); |
| 514 | json_object_object_get_ex(obj, "data", &obj2); |
| 515 | msg_text = json_object_get_string(obj2); |
| 516 | obj = json_tokener_parse(msg_text); |
| 517 | printf("%s\n", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
| 518 | json_object_put(obj); |
| 519 | break; |
| 520 | case 2: |
| 521 | printf("ERROR\n"); |
| 522 | printf("%s\n", json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
| 523 | break; |
| 524 | case 3: |
| 525 | printf("INFO\n"); |
| 526 | printf("%s\n", json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
| 527 | break; |
| 528 | default: |
| 529 | printf("(unknown)\n"); |
| 530 | printf("%s\n", json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
| 531 | break; |
| 532 | } |
| 533 | json_object_put(reply); |
| 534 | } |
| 535 | close(sock); |
| 536 | |
| 537 | return (EXIT_SUCCESS); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 538 | } |