blob: e67df67ec2a28f11da0dca8eec3ba284f87c25a6 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file control_cmd.c
2 * SNMP-like status interface. */
3/*
Daniel Willmann1264cb42010-10-21 15:00:36 +02004 * (C) 2010-2011 by Daniel Willmann <daniel@totalueberwachung.de>
5 * (C) 2010-2011 by On-Waves
Harald Weltee08da972017-11-13 01:00:26 +09006 * (C) 2014 by Harald Welte <laforge@gnumonks.org>
7 * (C) 2017 by sysmocom - s.f.m.c. GmbH
Daniel Willmann1264cb42010-10-21 15:00:36 +02008 *
9 * All Rights Reserved
10 *
Harald Weltee08da972017-11-13 01:00:26 +090011 * SPDX-License-Identifier: GPL-2.0+
12 *
Daniel Willmann1264cb42010-10-21 15:00:36 +020013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 */
28
29#include <ctype.h>
30#include <errno.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <time.h>
35#include <unistd.h>
36
Harald Welte1238cc62014-08-20 19:50:04 +020037#include <osmocom/ctrl/control_cmd.h>
Daniel Willmann1264cb42010-10-21 15:00:36 +020038
39#include <osmocom/core/msgb.h>
40#include <osmocom/core/talloc.h>
Max70c7d412017-02-24 13:59:14 +010041#include <osmocom/core/utils.h>
Daniel Willmann1264cb42010-10-21 15:00:36 +020042#include <osmocom/vty/command.h>
43#include <osmocom/vty/vector.h>
44
45extern vector ctrl_node_vec;
46
Max70c7d412017-02-24 13:59:14 +010047const struct value_string ctrl_type_vals[] = {
Neels Hofmeyr7c1ec8c2017-03-02 14:32:52 +010048 { CTRL_TYPE_UNKNOWN, "(unknown)" },
Max70c7d412017-02-24 13:59:14 +010049 { CTRL_TYPE_GET, "GET" },
50 { CTRL_TYPE_SET, "SET" },
51 { CTRL_TYPE_GET_REPLY, "GET_REPLY" },
52 { CTRL_TYPE_SET_REPLY, "SET_REPLY" },
53 { CTRL_TYPE_TRAP, "TRAP" },
54 { CTRL_TYPE_ERROR, "ERROR" },
Neels Hofmeyr7c1ec8c2017-03-02 14:32:52 +010055 { 0, NULL }
Daniel Willmann1264cb42010-10-21 15:00:36 +020056};
57
Daniel Willmann1264cb42010-10-21 15:00:36 +020058/* Functions from libosmocom */
59extern vector cmd_make_descvec(const char *string, const char *descstr);
60
61/* Get the ctrl_cmd_element that matches this command */
62static struct ctrl_cmd_element *ctrl_cmd_get_element_match(vector vline, vector node)
63{
64 int index, j;
65 const char *desc;
66 struct ctrl_cmd_element *cmd_el;
67 struct ctrl_cmd_struct *cmd_desc;
68 char *str;
69
70 for (index = 0; index < vector_active(node); index++) {
71 if ((cmd_el = vector_slot(node, index))) {
72 cmd_desc = &cmd_el->strcmd;
73 if (cmd_desc->nr_commands > vector_active(vline))
74 continue;
Holger Hans Peter Freyther5fb265e2015-04-05 14:36:31 +020075 for (j =0; j < vector_active(vline) && j < cmd_desc->nr_commands; j++) {
Daniel Willmann1264cb42010-10-21 15:00:36 +020076 str = vector_slot(vline, j);
77 desc = cmd_desc->command[j];
78 if (desc[0] == '*')
79 return cmd_el; /* Partial match */
80 if (strcmp(desc, str) != 0)
81 break;
82 }
83 /* We went through all the elements and all matched */
84 if (j == cmd_desc->nr_commands)
85 return cmd_el;
86 }
87 }
88
89 return NULL;
90}
91
Harald Welteb4186822018-05-26 17:25:11 +020092/*! Execute a given received command
93 * \param[in] vline vector representing the available/registered commands
94 * \param[inout] command parsed received command to be executed
95 * \param[in] node CTRL interface node
96 * \param[in] data opaque data passed to verify(), get() and set() call-backs
97 * \returns CTRL_CMD_HANDLED or CTRL_CMD_REPLY; CTRL_CMD_ERROR on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +020098int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data)
99{
100 int ret = CTRL_CMD_ERROR;
101 struct ctrl_cmd_element *cmd_el;
102
103 if ((command->type != CTRL_TYPE_GET) && (command->type != CTRL_TYPE_SET)) {
104 command->reply = "Trying to execute something not GET or SET";
105 goto out;
106 }
107 if ((command->type == CTRL_TYPE_SET) && (!command->value)) {
108 command->reply = "SET without a value";
109 goto out;
110 }
111
112 if (!vline)
113 goto out;
114
115 cmd_el = ctrl_cmd_get_element_match(vline, node);
116
117 if (!cmd_el) {
118 command->reply = "Command not found";
119 goto out;
120 }
121
122 if (command->type == CTRL_TYPE_SET) {
123 if (!cmd_el->set) {
124 command->reply = "SET not implemented";
125 goto out;
126 }
127 if (cmd_el->verify) {
128 if ((ret = cmd_el->verify(command, command->value, data))) {
129 ret = CTRL_CMD_ERROR;
Daniel Willmann9225ab12011-07-28 21:38:51 +0200130 /* If verify() set an appropriate error message, don't change it. */
131 if (!command->reply)
132 command->reply = "Value failed verification.";
Daniel Willmann1264cb42010-10-21 15:00:36 +0200133 goto out;
134 }
Daniel Willmann1264cb42010-10-21 15:00:36 +0200135 }
136 ret = cmd_el->set(command, data);
137 goto out;
138 } else if (command->type == CTRL_TYPE_GET) {
139 if (!cmd_el->get) {
140 command->reply = "GET not implemented";
141 goto out;
142 }
143 ret = cmd_el->get(command, data);
144 goto out;
145 }
146out:
147 if (ret == CTRL_CMD_REPLY) {
148 if (command->type == CTRL_TYPE_SET) {
149 command->type = CTRL_TYPE_SET_REPLY;
150 } else if (command->type == CTRL_TYPE_GET) {
151 command->type = CTRL_TYPE_GET_REPLY;
152 }
153 } else if (ret == CTRL_CMD_ERROR) {
154 command->type = CTRL_TYPE_ERROR;
155 }
156 return ret;
157}
158
159static void add_word(struct ctrl_cmd_struct *cmd,
160 const char *start, const char *end)
161{
162 if (!cmd->command) {
163 cmd->command = talloc_zero_array(tall_vty_vec_ctx,
164 char*, 1);
165 cmd->nr_commands = 0;
166 } else {
167 cmd->command = talloc_realloc(tall_vty_vec_ctx,
168 cmd->command, char*,
169 cmd->nr_commands + 1);
170 }
171
172 cmd->command[cmd->nr_commands++] = talloc_strndup(cmd->command,
173 start, end - start);
174}
175
176static void create_cmd_struct(struct ctrl_cmd_struct *cmd, const char *name)
177{
178 const char *cur, *word;
179
180 for (cur = name, word = NULL; cur[0] != '\0'; ++cur) {
181 /* warn about optionals */
182 if (cur[0] == '(' || cur[0] == ')' || cur[0] == '|') {
Harald Welte7fd0c832014-08-20 19:58:13 +0200183 LOGP(DLCTRL, LOGL_ERROR,
Daniel Willmann1264cb42010-10-21 15:00:36 +0200184 "Optionals are not supported in '%s'\n", name);
185 goto failure;
186 }
187
188 if (isspace(cur[0])) {
189 if (word) {
190 add_word(cmd, word, cur);
191 word = NULL;
192 }
193 continue;
194 }
195
196 if (!word)
197 word = cur;
198 }
199
200 if (word)
201 add_word(cmd, word, cur);
202
203 return;
204failure:
205 cmd->nr_commands = 0;
206 talloc_free(cmd->command);
207}
208
Harald Welteb4186822018-05-26 17:25:11 +0200209/*! Install a given command definition at a given CTRL node.
Philipp Maierb6fd8ed2021-06-16 11:37:14 +0200210 * \param[in] node CTRL node at which \a cmd is to be installed
Harald Welteb4186822018-05-26 17:25:11 +0200211 * \param[in] cmd command definition to be installed
212 * \returns 0 on success; negative on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200213int ctrl_cmd_install(enum ctrl_node_type node, struct ctrl_cmd_element *cmd)
214{
215 vector cmds_vec;
216
217 cmds_vec = vector_lookup_ensure(ctrl_node_vec, node);
218
219 if (!cmds_vec) {
220 cmds_vec = vector_init(5);
221 if (!cmds_vec) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200222 LOGP(DLCTRL, LOGL_ERROR, "vector_init failed.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200223 return -ENOMEM;
224 }
225 vector_set_index(ctrl_node_vec, node, cmds_vec);
226 }
227
228 vector_set(cmds_vec, cmd);
229
230 create_cmd_struct(&cmd->strcmd, cmd->name);
231 return 0;
232}
233
Harald Welteb4186822018-05-26 17:25:11 +0200234/*! Allocate a control command of given \a type.
235 * \param[in] ctx talloc context from which to allocate
236 * \param[in] type command type to set after allocation
237 * \returns callee-allocated \ref ctrl_cmd. Caller must talloc_free() it. */
Holger Hans Peter Freythera6b34012011-08-22 23:44:32 +0200238struct ctrl_cmd *ctrl_cmd_create(void *ctx, enum ctrl_type type)
239{
240 struct ctrl_cmd *cmd;
241
242 cmd = talloc_zero(ctx, struct ctrl_cmd);
243 if (!cmd)
244 return NULL;
245
246 cmd->type = type;
247 return cmd;
248}
249
Harald Welteb4186822018-05-26 17:25:11 +0200250/*! Perform a deepl copy of the given \a cmd, allocating memory from \a ctx.
251 * \param[in] ctx talloc context from which to allocate
252 * \param[in cmd CTRL command to be copied
253 * \returns deep copy of \a cmd on success; NULL on error */
Daniel Willmann8b7a9622011-03-17 15:37:54 +0100254struct ctrl_cmd *ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd)
255{
256 struct ctrl_cmd *cmd2;
257
258 cmd2 = talloc_zero(ctx, struct ctrl_cmd);
259 if (!cmd2)
260 return NULL;
261
262 cmd2->type = cmd->type;
263 if (cmd->id) {
264 cmd2->id = talloc_strdup(cmd2, cmd->id);
265 if (!cmd2->id)
266 goto err;
267 }
268 if (cmd->variable) {
269 cmd2->variable = talloc_strdup(cmd2, cmd->variable);
270 if (!cmd2->variable)
271 goto err;
272 }
273 if (cmd->value) {
274 cmd2->value = talloc_strdup(cmd2, cmd->value);
275 if (!cmd2->value)
276 goto err;
277 }
278 if (cmd->reply) {
279 cmd2->reply = talloc_strdup(cmd2, cmd->reply);
280 if (!cmd2->reply)
281 goto err;
282 }
283
284 return cmd2;
285err:
286 talloc_free(cmd2);
287 return NULL;
288}
289
Harald Welteb4186822018-05-26 17:25:11 +0200290/*! Parse/Decode CTRL from \ref msgb into command struct.
291 * \param[in] ctx talloc context from which to allocate
292 * \param[in] msg message buffer containing command to be decoded
293 * \returns callee-allocated decoded CTRL command; NULL on allocation or other failure
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100294 * The caller is responsible to talloc_free() the returned struct pointer. */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200295struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
296{
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100297 struct ctrl_cmd *res = ctrl_cmd_parse2(ctx, msg);
298 if (res->type == CTRL_TYPE_ERROR) {
299 talloc_free(res);
300 return NULL;
301 }
302 return res;
303}
304
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200305static bool id_str_valid(const char *str)
306{
307 for (;*str;str++) {
308 if (!isdigit(*str))
309 return false;
310 }
311 return true;
312}
313
Harald Welteb4186822018-05-26 17:25:11 +0200314/*! Parse/Decode CTRL from \ref msgb into command struct.
315 * \param[in] ctx talloc context from which to allocate
316 * \param[in] msg message buffer containing command to be decoded
317 * \returns callee-allocated decoded CTRL command; NULL on allocation failure,
318 * ctrl->type == CTRL_TYPE_ERROR and an error message in ctrl->reply on any error.
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200319 * The caller is responsible to talloc_free() the returned struct pointer.
320 * If information of the origin of the ERROR cmd returned is required (received
321 * or local parsing failure), use \ref ctrl_cmd_parse3 instead. */
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100322struct ctrl_cmd *ctrl_cmd_parse2(void *ctx, struct msgb *msg)
323{
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200324 bool unused;
325 return ctrl_cmd_parse3(ctx, msg, &unused);
326}
327
328/*! Parse/Decode CTRL from \ref msgb into command struct.
329 * \param[in] ctx talloc context from which to allocate
330 * \param[in] msg message buffer containing command to be decoded
331 * \param[out] parse_failed Whether returned ERROR cmd was generatd locally
332 * (due to parse failure) or was received.
333 * \returns callee-allocated decoded CTRL command; NULL on allocation failure,
334 * ctrl->type == CTRL_TYPE_ERROR and an error message in ctrl->reply on any error.
335 * The caller is responsible to talloc_free() the returned struct pointer. */
336struct ctrl_cmd *ctrl_cmd_parse3(void *ctx, struct msgb *msg, bool *parse_failed)
337{
Daniel Willmann1264cb42010-10-21 15:00:36 +0200338 char *str, *tmp, *saveptr = NULL;
339 char *var, *val;
340 struct ctrl_cmd *cmd;
341
342 cmd = talloc_zero(ctx, struct ctrl_cmd);
343 if (!cmd) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200344 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate.\n");
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200345 *parse_failed = true;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200346 return NULL;
347 }
348
349 /* Make sure input is NULL terminated */
350 msgb_put_u8(msg, 0);
351 str = (char *) msg->l2h;
352
Harald Welteedf6fe72016-11-26 10:06:07 +0100353 OSMO_ASSERT(str);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200354 tmp = strtok_r(str, " ", &saveptr);
355 if (!tmp) {
356 cmd->type = CTRL_TYPE_ERROR;
357 cmd->id = "err";
358 cmd->reply = "Request malformed";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200359 LOGP(DLCTRL, LOGL_NOTICE, "Malformed request: \"%s\"\n", osmo_escape_str(str, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200360 goto err;
361 }
362
Max70c7d412017-02-24 13:59:14 +0100363 cmd->type = get_string_value(ctrl_type_vals, tmp);
Pau Espin Pedrol8aa85bd2017-06-23 12:43:31 +0200364 if ((int)cmd->type < 0 || cmd->type == CTRL_TYPE_UNKNOWN) {
Daniel Willmann1264cb42010-10-21 15:00:36 +0200365 cmd->type = CTRL_TYPE_ERROR;
366 cmd->id = "err";
367 cmd->reply = "Request type unknown";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200368 LOGP(DLCTRL, LOGL_NOTICE, "Request type unknown: \"%s\"\n", osmo_escape_str(str, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200369 goto err;
370 }
371
372 tmp = strtok_r(NULL, " ", &saveptr);
373
374 if (!tmp) {
375 cmd->type = CTRL_TYPE_ERROR;
376 cmd->id = "err";
377 cmd->reply = "Missing ID";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200378 LOGP(DLCTRL, LOGL_NOTICE, "Missing ID: \"%s\"\n", osmo_escape_str(str, -1));
379 goto err;
380 }
381
Pau Espin Pedrol40ad9132018-07-12 17:41:55 +0200382 if (!id_str_valid(tmp) &&
383 !(cmd->type == CTRL_TYPE_ERROR && strcmp(tmp, "err") == 0)) {
Pau Espin Pedrol55088b72018-07-12 13:03:52 +0200384 LOGP(DLCTRL, LOGL_NOTICE, "Invalid %s message ID number: \"%s\"\n",
385 get_value_string(ctrl_type_vals, cmd->type), osmo_escape_str(tmp, -1));
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200386 cmd->type = CTRL_TYPE_ERROR;
387 cmd->id = "err";
388 cmd->reply = "Invalid message ID number";
Daniel Willmann1264cb42010-10-21 15:00:36 +0200389 goto err;
390 }
391 cmd->id = talloc_strdup(cmd, tmp);
392 if (!cmd->id)
393 goto oom;
394
395 switch (cmd->type) {
396 case CTRL_TYPE_GET:
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200397 var = strtok_r(NULL, " \n", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200398 if (!var) {
399 cmd->type = CTRL_TYPE_ERROR;
400 cmd->reply = "GET incomplete";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200401 LOGP(DLCTRL, LOGL_NOTICE, "GET Command incomplete: \"%s\"\n",
402 osmo_escape_str(str, -1));
403 goto err;
404 }
405 if (!osmo_separated_identifiers_valid(var, ".")) {
406 cmd->type = CTRL_TYPE_ERROR;
407 cmd->reply = "GET variable contains invalid characters";
408 LOGP(DLCTRL, LOGL_NOTICE, "GET variable contains invalid characters: \"%s\"\n",
409 osmo_escape_str(var, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200410 goto err;
411 }
412 cmd->variable = talloc_strdup(cmd, var);
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200413 var = strtok_r(NULL, "", &saveptr);
414 if (var) {
415 cmd->type = CTRL_TYPE_ERROR;
416 cmd->reply = "GET with trailing characters";
417 LOGP(DLCTRL, LOGL_NOTICE, "GET with trailing characters: \"%s\"\n",
418 osmo_escape_str(var, -1));
419 goto err;
420 }
Harald Welte7fd0c832014-08-20 19:58:13 +0200421 LOGP(DLCTRL, LOGL_DEBUG, "Command: GET %s\n", cmd->variable);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200422 break;
423 case CTRL_TYPE_SET:
424 var = strtok_r(NULL, " ", &saveptr);
Holger Hans Peter Freyther5ad742d2014-08-08 19:41:28 +0200425 val = strtok_r(NULL, "\n", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200426 if (!var || !val) {
427 cmd->type = CTRL_TYPE_ERROR;
428 cmd->reply = "SET incomplete";
Harald Welte7fd0c832014-08-20 19:58:13 +0200429 LOGP(DLCTRL, LOGL_NOTICE, "SET Command incomplete\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200430 goto err;
431 }
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200432 if (!osmo_separated_identifiers_valid(var, ".")) {
433 cmd->type = CTRL_TYPE_ERROR;
434 cmd->reply = "SET variable contains invalid characters";
435 LOGP(DLCTRL, LOGL_NOTICE, "SET variable contains invalid characters: \"%s\"\n",
436 osmo_escape_str(var, -1));
437 goto err;
438 }
Daniel Willmann1264cb42010-10-21 15:00:36 +0200439 cmd->variable = talloc_strdup(cmd, var);
440 cmd->value = talloc_strdup(cmd, val);
441 if (!cmd->variable || !cmd->value)
442 goto oom;
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200443
444 var = strtok_r(NULL, "", &saveptr);
445 if (var) {
446 cmd->type = CTRL_TYPE_ERROR;
447 cmd->reply = "SET with trailing characters";
448 LOGP(DLCTRL, LOGL_NOTICE, "SET with trailing characters: \"%s\"\n",
449 osmo_escape_str(var, -1));
450 goto err;
451 }
452
453 LOGP(DLCTRL, LOGL_DEBUG, "Command: SET %s = \"%s\"\n", cmd->variable,
454 osmo_escape_str(cmd->value, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200455 break;
Neels Hofmeyra5e21622017-12-16 01:18:53 +0100456#define REPLY_CASE(TYPE, NAME) \
457 case TYPE: \
458 var = strtok_r(NULL, " ", &saveptr); \
459 val = strtok_r(NULL, "", &saveptr); \
460 if (!var) { \
461 cmd->type = CTRL_TYPE_ERROR; \
462 cmd->reply = NAME " incomplete"; \
463 LOGP(DLCTRL, LOGL_NOTICE, NAME " incomplete\n"); \
464 goto err; \
465 } \
466 if (!osmo_separated_identifiers_valid(var, ".")) { \
467 cmd->type = CTRL_TYPE_ERROR; \
468 cmd->reply = NAME " variable contains invalid characters"; \
469 LOGP(DLCTRL, LOGL_NOTICE, NAME " variable contains invalid characters: \"%s\"\n", \
470 osmo_escape_str(var, -1)); \
471 goto err; \
472 } \
473 cmd->variable = talloc_strdup(cmd, var); \
474 cmd->reply = talloc_strdup(cmd, val); \
475 if (!cmd->variable || !cmd->reply) \
476 goto oom; \
477 LOGP(DLCTRL, LOGL_DEBUG, "Command: " NAME " %s: %s\n", cmd->variable, \
478 osmo_escape_str(cmd->reply, -1)); \
479 break
480 REPLY_CASE(CTRL_TYPE_GET_REPLY, "GET REPLY");
481 REPLY_CASE(CTRL_TYPE_SET_REPLY, "SET REPLY");
482 REPLY_CASE(CTRL_TYPE_TRAP, "TRAP");
483#undef REPLY_CASE
Daniel Willmann1264cb42010-10-21 15:00:36 +0200484 case CTRL_TYPE_ERROR:
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200485 var = strtok_r(NULL, "", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200486 if (!var) {
487 cmd->reply = "";
488 goto err;
489 }
490 cmd->reply = talloc_strdup(cmd, var);
491 if (!cmd->reply)
492 goto oom;
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200493 LOGP(DLCTRL, LOGL_DEBUG, "Command: ERROR \"%s\"\n",
494 osmo_escape_str(cmd->reply, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200495 break;
496 case CTRL_TYPE_UNKNOWN:
497 default:
498 cmd->type = CTRL_TYPE_ERROR;
499 cmd->reply = "Unknown type";
500 goto err;
501 }
502
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200503 *parse_failed = false;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200504 return cmd;
505oom:
506 cmd->type = CTRL_TYPE_ERROR;
507 cmd->id = "err";
508 cmd->reply = "OOM";
509err:
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200510 *parse_failed = true;
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100511 return cmd;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200512}
513
Harald Welteb4186822018-05-26 17:25:11 +0200514/*! Encode a given CTRL command from its parsed form into a message buffer.
515 * \param[in] cmd decoded/parsed form of to-be-encoded command
516 * \returns callee-allocated message buffer containing the encoded \a cmd; NULL on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200517struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
518{
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200519 struct msgb *msg = NULL;
520 char *strbuf;
521 size_t len;
Max70c7d412017-02-24 13:59:14 +0100522 const char *type;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200523
524 if (!cmd->id)
525 return NULL;
526
Max70c7d412017-02-24 13:59:14 +0100527 type = get_value_string(ctrl_type_vals, cmd->type);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200528
529 switch (cmd->type) {
530 case CTRL_TYPE_GET:
531 if (!cmd->variable)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200532 return NULL;
533 strbuf = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200534 break;
535 case CTRL_TYPE_SET:
536 if (!cmd->variable || !cmd->value)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200537 return NULL;
538 strbuf = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id,
539 cmd->variable, cmd->value);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200540 break;
541 case CTRL_TYPE_GET_REPLY:
542 case CTRL_TYPE_SET_REPLY:
543 case CTRL_TYPE_TRAP:
544 if (!cmd->variable || !cmd->reply)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200545 return NULL;
546 strbuf = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id,
547 cmd->variable, cmd->reply);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200548 break;
549 case CTRL_TYPE_ERROR:
550 if (!cmd->reply)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200551 return NULL;
552 strbuf = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->reply);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200553 break;
554 default:
Harald Welte7fd0c832014-08-20 19:58:13 +0200555 LOGP(DLCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type);
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200556 return NULL;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200557 }
558
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200559 if (!strbuf) {
560 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
561 goto ret;
562 }
563 len = strlen(strbuf);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200564
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200565 msg = msgb_alloc_headroom(len + 128, 128, "ctrl ERROR command make");
566 if (!msg)
567 goto ret;
568 msg->l2h = msgb_put(msg, len);
569 memcpy(msg->l2h, strbuf, len);
570
571ret:
572 talloc_free(strbuf);
573 return msg;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200574}
Harald Welte39c9e7b2014-08-22 00:28:51 +0200575
Harald Welteb4186822018-05-26 17:25:11 +0200576/*! Build a deferred control command state and keep it the per-connection list of deferred commands.
577 * This function is typically called by a ctrl command handler that wishes to defer returning a
578 * response. The reutnred state can later be used to check if the deferred command is still alive,
579 * and to respond to the specific command. This only works to defer the response to GET and SET.
580 * \param[in] ctx talloc context from whihc to allocate the ctrl_cmd_def
581 * \param[in] cmd the control command whose response is deferred
582 * \param[in] data opaque, user-defined pointer
583 * \param[in] secs number of seconds until the command times out
584 * \returns callee-allocated ctrl_cmd_def */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200585struct ctrl_cmd_def *
586ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs)
587{
588 struct ctrl_cmd_def *cd;
589
590 if (!cmd->ccon)
591 return NULL;
592
593 cd = talloc_zero(ctx, struct ctrl_cmd_def);
594
Neels Hofmeyrcdbc9af2018-04-03 16:51:49 +0200595 cmd->defer = cd;
Harald Welte39c9e7b2014-08-22 00:28:51 +0200596 cd->cmd = cmd;
597 cd->data = data;
598
599 /* add to per-connection list of deferred commands */
600 llist_add(&cd->list, &cmd->ccon->def_cmds);
601
602 return cd;
603}
604
Harald Welteb4186822018-05-26 17:25:11 +0200605/*! Determine if the given deferred control command is still alive or a zombie.
606 * \param[in] cd deferred ctrl command state
607 * \returns 0 is \a cd is still alive; 1 if it's a zombie */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200608int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd)
609{
610 /* luckily we're still alive */
611 if (cd->cmd)
612 return 0;
613
614 /* if we are a zombie, make sure we really die */
615 llist_del(&cd->list);
616 talloc_free(cd);
617
618 return 1;
619}
620
Harald Welteb4186822018-05-26 17:25:11 +0200621/*! Send the response to a deferred ctrl command.
622 * The command can only be a resply to a SET or a GET operation.
623 * \param[in] cd deferred ctrl command state
624 * \returns 0 if command sent successfully; negative on error */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200625int ctrl_cmd_def_send(struct ctrl_cmd_def *cd)
626{
627 struct ctrl_cmd *cmd = cd->cmd;
628
629 int rc;
630
631 /* Deferred commands can only be responses to GET/SET or ERROR, but
632 * never TRAP or anything else */
633 switch (cmd->type) {
634 case CTRL_TYPE_GET:
635 cmd->type = CTRL_TYPE_GET_REPLY;
636 break;
637 case CTRL_TYPE_SET:
638 cmd->type = CTRL_TYPE_SET_REPLY;
639 break;
640 default:
641 cmd->type = CTRL_TYPE_ERROR;
642 }
643
644 rc = ctrl_cmd_send(&cmd->ccon->write_queue, cmd);
645
646 talloc_free(cmd);
647 llist_del(&cd->list);
648 talloc_free(cd);
649
650 return rc;
651}