blob: a701da1a347a4989285dd6fbf78ebffcdaa06ddb [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>
5 * \date 2012
6 */
7/*
8 * Copyright (C) 2012 CESNET
9 *
10 * LICENSE TERMS
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
21 * 3. Neither the name of the Company nor the names of its contributors
22 * may be used to endorse or promote products derived from this
23 * software without specific prior written permission.
24 *
25 * ALTERNATIVELY, provided that this notice is retained in full, this
26 * product may be distributed under the terms of the GNU General Public
27 * License (GPL) version 2 or later, in which case the provisions
28 * of the GPL apply INSTEAD OF those given above.
29 *
30 * This software is provided ``as is'', and any express or implied
31 * warranties, including, but not limited to, the implied warranties of
32 * merchantability and fitness for a particular purpose are disclaimed.
33 * In no event shall the company or contributors be liable for any
34 * direct, indirect, incidental, special, exemplary, or consequential
35 * damages (including, but not limited to, procurement of substitute
36 * goods or services; loss of use, data, or profits; or business
37 * interruption) however caused and on any theory of liability, whether
38 * in contract, strict liability, or tort (including negligence or
39 * otherwise) arising in any way out of the use of this software, even
40 * if advised of the possibility of such damage.
41 *
42 */
43
44#include <unistd.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <errno.h>
49#include <sys/types.h>
50#include <sys/socket.h>
51#include <sys/un.h>
52#include <json/json.h>
53
Radek Krejci6cb08982012-07-25 18:01:06 +020054#define SOCKET_FILENAME "/tmp/mod_netconf.sock"
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020055#define BUFFER_SIZE 4096
56
57typedef enum MSG_TYPE {
58 REPLY_OK,
59 REPLY_DATA,
60 REPLY_ERROR,
61 MSG_CONNECT,
62 MSG_DISCONNECT,
63 MSG_GET,
64 MSG_GETCONFIG,
65 MSG_EDITCONFIG,
66 MSG_COPYCONFIG,
67 MSG_DELETECONFIG,
68 MSG_LOCK,
69 MSG_UNLOCK,
70 MSG_KILL
71} MSG_TYPE;
72
73void print_help(char* progname)
74{
75 printf("Usage: %s <command>\n", progname);
76 printf("Available commands:\n");
77 printf("\tconnect\n");
78 printf("\tdisconnect\n");
79 printf("\tcopy-config\n");
80 printf("\tdelete-config\n");
81 printf("\tedit-config\n");
82 printf("\tget\n");
83 printf("\tget-config\n");
84 printf("\tkill-session\n");
85 printf("\tlock\n");
86 printf("\tunlock\n");
87}
88
89int main (int argc, char* argv[])
90{
Radek Krejci035bf4e2012-07-25 10:59:09 +020091 json_object* msg = NULL, *reply = NULL;
Radek Krejci8fd1f5e2012-07-24 17:33:36 +020092 const char* msg_text;
93 int sock;
94 struct sockaddr_un addr;
95 size_t len;
96 char buffer[BUFFER_SIZE];
97 char* line = NULL;
98
99 if (argc != 2) {
100 print_help(argv[0]);
101 return (2);
102 }
103
104 /* connect to the daemon */
105 sock = socket(PF_UNIX, SOCK_STREAM, 0);
106 if (sock == -1) {
107 fprintf(stderr, "Creating socket failed (%s)", strerror(errno));
108 return (EXIT_FAILURE);
109 }
110 addr.sun_family = AF_UNIX;
111 strncpy(addr.sun_path, SOCKET_FILENAME, sizeof(addr.sun_path));
112 len = strlen(addr.sun_path) + sizeof(addr.sun_family);
113 if (connect(sock, (struct sockaddr *) &addr, len) == -1) {
114 fprintf(stderr, "Connecting to mod_netconf (%s) failed (%s)", SOCKET_FILENAME, strerror(errno));
115 close(sock);
116 return (EXIT_FAILURE);
117 }
118
119 line = malloc(sizeof(char) * BUFFER_SIZE);
120
121 if (strcmp(argv[1], "connect") == 0) {
122 msg = json_object_new_object();
123 json_object_object_add(msg, "type", json_object_new_int(MSG_CONNECT));
124 printf("Hostname: ");
125 getline (&line, &len, stdin);
126 line[(strlen(line)-1)] = 0;
127 json_object_object_add(msg, "host", json_object_new_string(line));
128 printf("Port: ");
129 getline (&line, &len, stdin);
130 line[(strlen(line)-1)] = 0;
131 json_object_object_add(msg, "port", json_object_new_string(line));
132 printf("Username: ");
133 getline (&line, &len, stdin);
134 line[(strlen(line)-1)] = 0;
135 json_object_object_add(msg, "user", json_object_new_string(line));
136 printf("Password: ");
137 system("stty -echo");
138 getline (&line, &len, stdin);
139 system("stty echo");
140 printf("\n");
141 line[(strlen(line)-1)] = 0;
142 json_object_object_add(msg, "pass", json_object_new_string(line));
143 } else if (strcmp(argv[1], "disconnect") == 0) {
144 msg = json_object_new_object();
145 json_object_object_add(msg, "type", json_object_new_int(MSG_DISCONNECT));
146 printf("Session: ");
147 getline (&line, &len, stdin);
148 line[(strlen(line)-1)] = 0;
149 json_object_object_add(msg, "session", json_object_new_string(line));
150 } else if (strcmp(argv[1], "copy-config") == 0) {
Radek Krejci035bf4e2012-07-25 10:59:09 +0200151 msg = json_object_new_object();
152 json_object_object_add(msg, "type", json_object_new_int(MSG_COPYCONFIG));
153 printf("Session: ");
154 getline (&line, &len, stdin);
155 line[(strlen(line)-1)] = 0;
156 json_object_object_add(msg, "session", json_object_new_string(line));
157 printf("Source (running|startup|candidate): ");
158 getline (&line, &len, stdin);
159 line[(strlen(line)-1)] = 0;
160 json_object_object_add(msg, "source", json_object_new_string(line));
161 printf("Target (running|startup|candidate): ");
162 getline (&line, &len, stdin);
163 line[(strlen(line)-1)] = 0;
164 json_object_object_add(msg, "target", json_object_new_string(line));
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200165 } else if (strcmp(argv[1], "delete-config") == 0) {
166 } else if (strcmp(argv[1], "edit-config") == 0) {
167 } else if (strcmp(argv[1], "get") == 0) {
168 msg = json_object_new_object();
169 json_object_object_add(msg, "type", json_object_new_int(MSG_GET));
170 printf("Session: ");
171 getline (&line, &len, stdin);
172 line[(strlen(line)-1)] = 0;
173 json_object_object_add(msg, "session", json_object_new_string(line));
174 printf("Filter: ");
175 getline(&line, &len, stdin);
176 line[(strlen(line)-1)] = 0;
177 if (strlen(line) > 0) {
178 json_object_object_add(msg, "filter", json_object_new_string(line));
179 }
180 } else if (strcmp(argv[1], "get-config") == 0) {
181 msg = json_object_new_object();
182 json_object_object_add(msg, "type", json_object_new_int(MSG_GETCONFIG));
183 printf("Session: ");
184 getline (&line, &len, stdin);
185 line[(strlen(line)-1)] = 0;
186 json_object_object_add(msg, "session", json_object_new_string(line));
187 printf("Source (running|startup|candidate): ");
188 getline (&line, &len, stdin);
189 line[(strlen(line)-1)] = 0;
190 json_object_object_add(msg, "source", json_object_new_string(line));
191 printf("Filter: ");
192 getline(&line, &len, stdin);
193 line[(strlen(line)-1)] = 0;
194 if (strlen(line) > 0) {
195 json_object_object_add(msg, "filter", json_object_new_string(line));
196 }
197 } else if (strcmp(argv[1], "kill-session") == 0) {
198 } else if (strcmp(argv[1], "lock") == 0) {
199 } else if (strcmp(argv[1], "unlock") == 0) {
200 } else {
201 fprintf(stderr, "Unknown command %s\n", argv[1]);
202 close(sock);
203 return (EXIT_FAILURE);
204 }
205
206 /* send the message */
207 if (msg != NULL) {
208 msg_text = json_object_to_json_string(msg);
209
Radek Krejci035bf4e2012-07-25 10:59:09 +0200210 if (json_object_object_get(msg, "pass") == NULL) {
211 /* print message only if it does not contain password */
212 printf("Sending: %s\n", msg_text);
213 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200214 send(sock, msg_text, strlen(msg_text) + 1, 0);
215
216 json_object_put(msg);
217 } else {
218 close(sock);
219 return (EXIT_FAILURE);
220 }
221
222 len = recv(sock, buffer, BUFFER_SIZE, 0);
Radek Krejci035bf4e2012-07-25 10:59:09 +0200223 if (len > 0) {
224 reply = json_tokener_parse(buffer);
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200225 }
Radek Krejci035bf4e2012-07-25 10:59:09 +0200226 printf("Received:\n");
227 if (reply == NULL) {
228 printf("(null)\n");
229 } else {
230 json_object_object_foreach(reply, key, value) {
231 printf("Key: %s, Value: ", key);
232 switch (json_object_get_type(value)) {
233 case json_type_string:
234 printf("%s\n", json_object_get_string(value));
235 break;
236 case json_type_int:
237 printf("%d\n", json_object_get_int(value));
238 break;
239 default:
240 printf("\n");
241 break;
242 }
243 }
244 json_object_put(reply);
245 }
Radek Krejci8fd1f5e2012-07-24 17:33:36 +0200246 close(sock);
247 free(line);
248
249 return (EXIT_SUCCESS);
250}