blob: f09ef6dc38494c8decfe749c7d31a5dd3c54e076 [file] [log] [blame]
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02001/*!
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 Cejka94da2c52013-01-08 18:20:30 +01005 * \author Tomas Cejka <cejkat@cesnet.cz>
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02006 * \date 2012
Tomas Cejka94da2c52013-01-08 18:20:30 +01007 * \date 2013
Radek Krejci8fd1f5e2012-07-24 17:33:36 +02008 */
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 Kupka1e3e4c82012-09-04 09:32:15 +020046#define _GNU_SOURCE
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020047#include <unistd.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
Tomas Cejkab272bf12012-09-05 16:49:12 +020051#include <err.h>
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020052#include <errno.h>
53#include <sys/types.h>
54#include <sys/socket.h>
55#include <sys/un.h>
56#include <json/json.h>
David Kupka1e3e4c82012-09-04 09:32:15 +020057#include <ctype.h>
Tomas Cejka94da2c52013-01-08 18:20:30 +010058#include "message_type.h"
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020059
Tomas Cejka689a1042013-01-16 15:08:25 +010060static const char rcsid[] __attribute__((used)) ="$Id: "__FILE__": "RCSID" $";
61
Radek Krejci6cb08982012-07-25 18:01:06 +020062#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
David Kupka1e3e4c82012-09-04 09:32:15 +020063#define BUFFER_SIZE 40960
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020064
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020065void print_help(char* progname)
66{
67 printf("Usage: %s <command>\n", progname);
68 printf("Available commands:\n");
69 printf("\tconnect\n");
70 printf("\tdisconnect\n");
71 printf("\tcopy-config\n");
72 printf("\tdelete-config\n");
73 printf("\tedit-config\n");
74 printf("\tget\n");
75 printf("\tget-config\n");
76 printf("\tkill-session\n");
77 printf("\tlock\n");
78 printf("\tunlock\n");
Radek Krejci9e04c7b2012-07-26 15:54:25 +020079 printf("\tinfo\n");
Radek Krejci80c10d92012-07-30 08:38:50 +020080 printf("\tgeneric\n");
Tomas Cejka94da2c52013-01-08 18:20:30 +010081 printf("\tgetschema\n");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020082}
83
Tomas Cejkab272bf12012-09-05 16:49:12 +020084/**
85 * \brief Get multiline input text.
86 *
87 * Print given prompt and read text ending with CTRL+D.
88 * Output string is terminated by 0. Ending '\n' is removed.
89 *
90 * On error, err is called!
91 *
92 * \param[out] output - pointer to memory where string is stored
93 * \param[out] size - size of string return by getdelim()
94 * \param[in] prompt - text printed as a prompt
95 */
96void readmultiline(char **output, size_t *size, const char *prompt)
97{
98 printf(prompt);
99 if (getdelim (output, size, 'D' - 0x40, stdin) == -1) {
100 if (errno) {
101 err(errno, "Cannot read input.");
102 }
103 *output = (char *) malloc(sizeof(char));
104 **output = 0;
105 return;
106 }
107 (*output)[(*size)-1] = 0; /* input text end "sanitation" */
108 (*output)[(strlen(*output))-1] = 0; /* input text end "sanitation" */
109}
110
111/**
112 * \brief Get input text.
113 *
114 * Print given prompt and read one line of text.
115 * Output string is terminated by 0. Ending '\n' is removed.
116 *
117 * On error, err is called!
118 *
119 * \param[out] output - pointer to memory where string is stored
120 * \param[out] size - size of string return by getline()
121 * \param[in] prompt - text printed as a prompt
122 */
123void readline(char **output, size_t *size, const char *prompt)
124{
125 printf(prompt);
126 if (getline (output, size, stdin) == -1) {
127 if (errno) {
128 err(errno, "Cannot read input.");
129 }
130 }
131 (*output)[(*size)-1] = 0; /* input text end "sanitation" */
132 (*output)[(strlen(*output))-1] = 0; /* input text end "sanitation" */
133}
134
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200135int main (int argc, char* argv[])
136{
Kupka David00b9c5c2012-09-05 09:45:50 +0200137 json_object* msg = NULL, *reply = NULL, *capabilities = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200138 const char* msg_text;
139 int sock;
140 struct sockaddr_un addr;
141 size_t len;
David Kupka1e3e4c82012-09-04 09:32:15 +0200142 char *buffer;
143 char* line = NULL, *chunked_msg_text;
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200144 int i, alen;
David Kupka1e3e4c82012-09-04 09:32:15 +0200145 int buffer_size, buffer_len, ret, chunk_len;
146 char c, chunk_len_str[12];
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200147
148 if (argc != 2) {
149 print_help(argv[0]);
150 return (2);
151 }
152
153 /* connect to the daemon */
154 sock = socket(PF_UNIX, SOCK_STREAM, 0);
155 if (sock == -1) {
156 fprintf(stderr, "Creating socket failed (%s)", strerror(errno));
157 return (EXIT_FAILURE);
158 }
159 addr.sun_family = AF_UNIX;
160 strncpy(addr.sun_path, SOCKET_FILENAME, sizeof(addr.sun_path));
161 len = strlen(addr.sun_path) + sizeof(addr.sun_family);
162 if (connect(sock, (struct sockaddr *) &addr, len) == -1) {
163 fprintf(stderr, "Connecting to mod_netconf (%s) failed (%s)", SOCKET_FILENAME, strerror(errno));
164 close(sock);
165 return (EXIT_FAILURE);
166 }
167
168 line = malloc(sizeof(char) * BUFFER_SIZE);
169
170 if (strcmp(argv[1], "connect") == 0) {
Radek Krejciaa422df2012-07-25 18:04:59 +0200171 /*
172 * create NETCONF session
173 */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200174 msg = json_object_new_object();
175 json_object_object_add(msg, "type", json_object_new_int(MSG_CONNECT));
Tomas Cejka42434462012-09-05 18:11:43 +0200176 readline(&line, &len, "Hostname: ");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200177 json_object_object_add(msg, "host", json_object_new_string(line));
Tomas Cejka42434462012-09-05 18:11:43 +0200178 readline(&line, &len, "Port: ");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200179 json_object_object_add(msg, "port", json_object_new_string(line));
Tomas Cejka42434462012-09-05 18:11:43 +0200180 readline(&line, &len, "Username: ");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200181 json_object_object_add(msg, "user", json_object_new_string(line));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200182 system("stty -echo");
Tomas Cejka42434462012-09-05 18:11:43 +0200183 readline(&line, &len, "Password: ");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200184 system("stty echo");
185 printf("\n");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200186 json_object_object_add(msg, "pass", json_object_new_string(line));
Tomas Cejka42434462012-09-05 18:11:43 +0200187
Tomas Cejkab272bf12012-09-05 16:49:12 +0200188 /* clean read password - it is needless because we have a copy in json... :-( */
189 memset(line, 'X', len);
190 free(line);
191 line = NULL;
192
193 printf("Supported capabilities\n");
194 capabilities = json_object_new_array();
195 json_object_object_add(msg, "capabilities", capabilities);
196 while (1) {
197 readline(&line, &len, "Next capability (empty for end): ");
198 if (strlen(line) == 0) {
199 break;
200 }
201 json_object_array_add (capabilities, json_object_new_string(line));
202 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200203 } else if (strcmp(argv[1], "disconnect") == 0) {
Radek Krejciaa422df2012-07-25 18:04:59 +0200204 /*
205 * Close NETCONF session
206 */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200207 msg = json_object_new_object();
208 json_object_object_add(msg, "type", json_object_new_int(MSG_DISCONNECT));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200209 readline(&line, &len, "Session: ");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200210 json_object_object_add(msg, "session", json_object_new_string(line));
211 } else if (strcmp(argv[1], "copy-config") == 0) {
Radek Krejciaa422df2012-07-25 18:04:59 +0200212 /*
213 * NETCONF <copy-config>
214 */
Radek Krejci035bf4e2012-07-25 10:59:09 +0200215 msg = json_object_new_object();
216 json_object_object_add(msg, "type", json_object_new_int(MSG_COPYCONFIG));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200217 readline(&line, &len, "Session: ");
Radek Krejci035bf4e2012-07-25 10:59:09 +0200218 json_object_object_add(msg, "session", json_object_new_string(line));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200219 readline(&line, &len, "Source (running|startup|candidate): ");
Radek Krejciae021c12012-07-25 18:03:52 +0200220 if (strlen(line) > 0) {
221 json_object_object_add(msg, "source", json_object_new_string(line));
222 } else {
Tomas Cejkab272bf12012-09-05 16:49:12 +0200223 readmultiline(&line, &len, "Configuration data (ending with CTRL+D): ");
Radek Krejciae021c12012-07-25 18:03:52 +0200224 json_object_object_add(msg, "config", json_object_new_string(line));
225 }
Tomas Cejkab272bf12012-09-05 16:49:12 +0200226 readline(&line, &len, "Target (running|startup|candidate): ");
Radek Krejci035bf4e2012-07-25 10:59:09 +0200227 json_object_object_add(msg, "target", json_object_new_string(line));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200228 } else if (strcmp(argv[1], "delete-config") == 0) {
Radek Krejciaa422df2012-07-25 18:04:59 +0200229 /*
230 * NETCONF <delete-config>
231 */
232 msg = json_object_new_object();
233 json_object_object_add(msg, "type", json_object_new_int(MSG_DELETECONFIG));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200234 readline(&line, &len, "Session: ");
Radek Krejciaa422df2012-07-25 18:04:59 +0200235 json_object_object_add(msg, "session", json_object_new_string(line));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200236 readline(&line, &len, "Target (running|startup|candidate): ");
Radek Krejciaa422df2012-07-25 18:04:59 +0200237 json_object_object_add(msg, "target", json_object_new_string(line));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200238 } else if (strcmp(argv[1], "edit-config") == 0) {
Radek Krejciaa422df2012-07-25 18:04:59 +0200239 /*
240 * NETCONF <edit-config>
241 */
242 msg = json_object_new_object();
243 json_object_object_add(msg, "type", json_object_new_int(MSG_EDITCONFIG));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200244 readline(&line, &len, "Session: ");
Radek Krejciaa422df2012-07-25 18:04:59 +0200245 json_object_object_add(msg, "session", json_object_new_string(line));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200246 readline(&line, &len, "Target (running|startup|candidate): ");
Radek Krejciaa422df2012-07-25 18:04:59 +0200247 json_object_object_add(msg, "target", json_object_new_string(line));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200248 readline(&line, &len, "Default operation (merge|replace|none): ");
Radek Krejciaa422df2012-07-25 18:04:59 +0200249 if (strlen(line) > 0) {
250 json_object_object_add(msg, "default-operation", json_object_new_string(line));
251 }
Tomas Cejkab272bf12012-09-05 16:49:12 +0200252 readline(&line, &len, "Error option (stop-on-error|continue-on-error|rollback-on-error): ");
Radek Krejciaa422df2012-07-25 18:04:59 +0200253 if (strlen(line) > 0) {
254 json_object_object_add(msg, "error-option", json_object_new_string(line));
255 }
Tomas Cejkab272bf12012-09-05 16:49:12 +0200256 readmultiline(&line, &len, "Configuration data (ending with CTRL+D): ");
Radek Krejcib1e08f42012-07-26 10:54:28 +0200257 json_object_object_add(msg, "config", json_object_new_string(line));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200258 } else if (strcmp(argv[1], "get") == 0) {
Radek Krejciaa422df2012-07-25 18:04:59 +0200259 /*
260 * NETCONF <get>
261 */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200262 msg = json_object_new_object();
263 json_object_object_add(msg, "type", json_object_new_int(MSG_GET));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200264 readline(&line, &len, "Session: ");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200265 json_object_object_add(msg, "session", json_object_new_string(line));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200266 readmultiline(&line, &len, "Filter (ending with CTRL+D): ");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200267 if (strlen(line) > 0) {
268 json_object_object_add(msg, "filter", json_object_new_string(line));
269 }
270 } else if (strcmp(argv[1], "get-config") == 0) {
Radek Krejciaa422df2012-07-25 18:04:59 +0200271 /*
272 * NETCONF <get-config>
273 */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200274 msg = json_object_new_object();
275 json_object_object_add(msg, "type", json_object_new_int(MSG_GETCONFIG));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200276 readline(&line, &len, "Session: ");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200277 json_object_object_add(msg, "session", json_object_new_string(line));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200278 readline(&line, &len, "Source (running|startup|candidate): ");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200279 json_object_object_add(msg, "source", json_object_new_string(line));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200280 readmultiline(&line, &len, "Filter (ending with CTRL+D): ");
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200281 if (strlen(line) > 0) {
282 json_object_object_add(msg, "filter", json_object_new_string(line));
283 }
284 } else if (strcmp(argv[1], "kill-session") == 0) {
Radek Krejciaa422df2012-07-25 18:04:59 +0200285 /*
286 * NETCONF <kill-session>
287 */
288 msg = json_object_new_object();
289 json_object_object_add(msg, "type", json_object_new_int(MSG_KILL));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200290 readline(&line, &len, "Session: ");
Radek Krejciaa422df2012-07-25 18:04:59 +0200291 json_object_object_add(msg, "session", json_object_new_string(line));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200292 readline(&line, &len, "Kill session with ID: ");
Radek Krejciaa422df2012-07-25 18:04:59 +0200293 json_object_object_add(msg, "session-id", json_object_new_string(line));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200294 } else if (strcmp(argv[1], "lock") == 0) {
Radek Krejciaa422df2012-07-25 18:04:59 +0200295 /*
296 * NETCONF <lock>
297 */
298 msg = json_object_new_object();
299 json_object_object_add(msg, "type", json_object_new_int(MSG_LOCK));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200300 readline(&line, &len, "Session: ");
Radek Krejciaa422df2012-07-25 18:04:59 +0200301 json_object_object_add(msg, "session", json_object_new_string(line));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200302 readline(&line, &len, "Target (running|startup|candidate): ");
Radek Krejciaa422df2012-07-25 18:04:59 +0200303 json_object_object_add(msg, "target", json_object_new_string(line));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200304 } else if (strcmp(argv[1], "unlock") == 0) {
Radek Krejciaa422df2012-07-25 18:04:59 +0200305 /*
306 * NETCONF <unlock>
307 */
308 msg = json_object_new_object();
309 json_object_object_add(msg, "type", json_object_new_int(MSG_UNLOCK));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200310 readline(&line, &len,"Session: ");
Radek Krejciaa422df2012-07-25 18:04:59 +0200311 json_object_object_add(msg, "session", json_object_new_string(line));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200312 readline(&line, &len, "Target (running|startup|candidate): ");
Radek Krejciaa422df2012-07-25 18:04:59 +0200313 json_object_object_add(msg, "target", json_object_new_string(line));
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200314 } else if (strcmp(argv[1], "info") == 0) {
315 /*
316 * Get information about NETCONF session
317 */
318 msg = json_object_new_object();
319 json_object_object_add(msg, "type", json_object_new_int(MSG_INFO));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200320 readline(&line, &len, "Session: ");
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200321 json_object_object_add(msg, "session", json_object_new_string(line));
Radek Krejci80c10d92012-07-30 08:38:50 +0200322 } else if (strcmp(argv[1], "generic") == 0) {
323 /*
324 * Generic NETCONF request
325 */
326 msg = json_object_new_object();
327 json_object_object_add(msg, "type", json_object_new_int(MSG_GENERIC));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200328 readline(&line, &len, "Session: ");
Radek Krejci80c10d92012-07-30 08:38:50 +0200329 json_object_object_add(msg, "session", json_object_new_string(line));
Tomas Cejkab272bf12012-09-05 16:49:12 +0200330 readmultiline(&line, &len, "NETCONF <rpc> content (ending with CTRL+D): ");
Radek Krejci80c10d92012-07-30 08:38:50 +0200331 json_object_object_add(msg, "content", json_object_new_string(line));
Tomas Cejka94da2c52013-01-08 18:20:30 +0100332 } else if (strcmp(argv[1], "getschema") == 0) {
333 /*
334 * Get information about NETCONF session
335 */
336 msg = json_object_new_object();
337 json_object_object_add(msg, "type", json_object_new_int(MSG_GETSCHEMA));
338 readline(&line, &len, "Session: ");
339 json_object_object_add(msg, "session", json_object_new_string(line));
340 readline(&line, &len, "Identificator: ");
341 json_object_object_add(msg, "identifier", json_object_new_string(line));
342 readline(&line, &len, "Format [YIN]: ");
343 json_object_object_add(msg, "format", json_object_new_string(line));
344 readline(&line, &len, "Version: ");
345 json_object_object_add(msg, "version", json_object_new_string(line));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200346 } else {
Radek Krejciaa422df2012-07-25 18:04:59 +0200347 /*
348 * Unknown request
349 */
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200350 fprintf(stderr, "Unknown command %s\n", argv[1]);
351 close(sock);
352 return (EXIT_FAILURE);
353 }
354
355 /* send the message */
356 if (msg != NULL) {
357 msg_text = json_object_to_json_string(msg);
David Kupka1e3e4c82012-09-04 09:32:15 +0200358 asprintf (&chunked_msg_text, "\n#%d\n%s\n##\n", (int)strlen(msg_text), msg_text);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200359
Radek Krejci035bf4e2012-07-25 10:59:09 +0200360 if (json_object_object_get(msg, "pass") == NULL) {
361 /* print message only if it does not contain password */
362 printf("Sending: %s\n", msg_text);
363 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200364 send(sock, chunked_msg_text, strlen(chunked_msg_text) + 1, 0);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200365
366 json_object_put(msg);
David Kupka1e3e4c82012-09-04 09:32:15 +0200367 free (chunked_msg_text);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200368 } else {
369 close(sock);
370 return (EXIT_FAILURE);
371 }
372
David Kupka1e3e4c82012-09-04 09:32:15 +0200373 /* read json in chunked framing */
374 buffer_size = 0;
375 buffer_len = 0;
376 buffer = NULL;
377 while (1) {
378 /* read chunk length */
379 if ((ret = recv (sock, &c, 1, 0)) != 1 || c != '\n') {
380 free (buffer);
381 buffer = NULL;
382 break;
383 }
384 if ((ret = recv (sock, &c, 1, 0)) != 1 || c != '#') {
385 free (buffer);
386 buffer = NULL;
387 break;
388 }
389 i=0;
390 memset (chunk_len_str, 0, 12);
391 while ((ret = recv (sock, &c, 1, 0) == 1 && (isdigit(c) || c == '#'))) {
392 if (i==0 && c == '#') {
393 if (recv (sock, &c, 1, 0) != 1 || c != '\n') {
394 /* end but invalid */
395 free (buffer);
396 buffer = NULL;
397 }
398 /* end of message, double-loop break */
399 goto msg_complete;
400 }
401 chunk_len_str[i++] = c;
402 }
403 if (c != '\n') {
404 free (buffer);
405 buffer = NULL;
406 break;
407 }
408 if ((chunk_len = atoi (chunk_len_str)) == 0) {
409 free (buffer);
410 buffer = NULL;
411 break;
412 }
413 buffer_size += chunk_len+1;
414 buffer = realloc (buffer, sizeof(char)*buffer_size);
415 if ((ret = recv (sock, buffer+buffer_len, chunk_len, 0)) == -1 || ret != chunk_len) {
416 free (buffer);
417 buffer = NULL;
418 break;
419 }
420 buffer_len += ret;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200421 }
David Kupka1e3e4c82012-09-04 09:32:15 +0200422msg_complete:
423
424 if (buffer != NULL) {
425 reply = json_tokener_parse(buffer);
426 free (buffer);
427 } else {
428 reply = NULL;
429 }
430
Radek Krejci035bf4e2012-07-25 10:59:09 +0200431 printf("Received:\n");
432 if (reply == NULL) {
433 printf("(null)\n");
434 } else {
435 json_object_object_foreach(reply, key, value) {
436 printf("Key: %s, Value: ", key);
437 switch (json_object_get_type(value)) {
438 case json_type_string:
439 printf("%s\n", json_object_get_string(value));
440 break;
441 case json_type_int:
442 printf("%d\n", json_object_get_int(value));
443 break;
Radek Krejci9e04c7b2012-07-26 15:54:25 +0200444 case json_type_array:
445 printf("\n");
446 alen = json_object_array_length(value);
447 for (i = 0; i < alen; i++) {
448 printf("\t(%d) %s\n", i, json_object_get_string(json_object_array_get_idx(value, i)));
449 }
450 break;
Radek Krejci035bf4e2012-07-25 10:59:09 +0200451 default:
452 printf("\n");
453 break;
454 }
455 }
456 json_object_put(reply);
457 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200458 close(sock);
459 free(line);
460
461 return (EXIT_SUCCESS);
462}