blob: b069ca46935e1d27464fe05fc5e961fb657022ce [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 *
Daniel Willmann1264cb42010-10-21 15:00:36 +020023 */
24
25#include <ctype.h>
26#include <errno.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <time.h>
31#include <unistd.h>
32
Harald Welte1238cc62014-08-20 19:50:04 +020033#include <osmocom/ctrl/control_cmd.h>
Daniel Willmann1264cb42010-10-21 15:00:36 +020034
35#include <osmocom/core/msgb.h>
36#include <osmocom/core/talloc.h>
Max70c7d412017-02-24 13:59:14 +010037#include <osmocom/core/utils.h>
Daniel Willmann1264cb42010-10-21 15:00:36 +020038#include <osmocom/vty/command.h>
39#include <osmocom/vty/vector.h>
40
41extern vector ctrl_node_vec;
42
Max70c7d412017-02-24 13:59:14 +010043const struct value_string ctrl_type_vals[] = {
Neels Hofmeyr7c1ec8c2017-03-02 14:32:52 +010044 { CTRL_TYPE_UNKNOWN, "(unknown)" },
Max70c7d412017-02-24 13:59:14 +010045 { CTRL_TYPE_GET, "GET" },
46 { CTRL_TYPE_SET, "SET" },
47 { CTRL_TYPE_GET_REPLY, "GET_REPLY" },
48 { CTRL_TYPE_SET_REPLY, "SET_REPLY" },
49 { CTRL_TYPE_TRAP, "TRAP" },
50 { CTRL_TYPE_ERROR, "ERROR" },
Neels Hofmeyr7c1ec8c2017-03-02 14:32:52 +010051 { 0, NULL }
Daniel Willmann1264cb42010-10-21 15:00:36 +020052};
53
Daniel Willmann1264cb42010-10-21 15:00:36 +020054/* Functions from libosmocom */
55extern vector cmd_make_descvec(const char *string, const char *descstr);
56
57/* Get the ctrl_cmd_element that matches this command */
58static struct ctrl_cmd_element *ctrl_cmd_get_element_match(vector vline, vector node)
59{
60 int index, j;
61 const char *desc;
62 struct ctrl_cmd_element *cmd_el;
63 struct ctrl_cmd_struct *cmd_desc;
64 char *str;
65
66 for (index = 0; index < vector_active(node); index++) {
67 if ((cmd_el = vector_slot(node, index))) {
68 cmd_desc = &cmd_el->strcmd;
69 if (cmd_desc->nr_commands > vector_active(vline))
70 continue;
Holger Hans Peter Freyther5fb265e2015-04-05 14:36:31 +020071 for (j =0; j < vector_active(vline) && j < cmd_desc->nr_commands; j++) {
Daniel Willmann1264cb42010-10-21 15:00:36 +020072 str = vector_slot(vline, j);
73 desc = cmd_desc->command[j];
74 if (desc[0] == '*')
75 return cmd_el; /* Partial match */
76 if (strcmp(desc, str) != 0)
77 break;
78 }
79 /* We went through all the elements and all matched */
80 if (j == cmd_desc->nr_commands)
81 return cmd_el;
82 }
83 }
84
85 return NULL;
86}
87
Harald Welteb4186822018-05-26 17:25:11 +020088/*! Execute a given received command
89 * \param[in] vline vector representing the available/registered commands
90 * \param[inout] command parsed received command to be executed
91 * \param[in] node CTRL interface node
92 * \param[in] data opaque data passed to verify(), get() and set() call-backs
93 * \returns CTRL_CMD_HANDLED or CTRL_CMD_REPLY; CTRL_CMD_ERROR on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +020094int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data)
95{
96 int ret = CTRL_CMD_ERROR;
97 struct ctrl_cmd_element *cmd_el;
98
99 if ((command->type != CTRL_TYPE_GET) && (command->type != CTRL_TYPE_SET)) {
100 command->reply = "Trying to execute something not GET or SET";
101 goto out;
102 }
103 if ((command->type == CTRL_TYPE_SET) && (!command->value)) {
104 command->reply = "SET without a value";
105 goto out;
106 }
107
108 if (!vline)
109 goto out;
110
111 cmd_el = ctrl_cmd_get_element_match(vline, node);
112
113 if (!cmd_el) {
114 command->reply = "Command not found";
115 goto out;
116 }
117
118 if (command->type == CTRL_TYPE_SET) {
119 if (!cmd_el->set) {
120 command->reply = "SET not implemented";
121 goto out;
122 }
123 if (cmd_el->verify) {
124 if ((ret = cmd_el->verify(command, command->value, data))) {
125 ret = CTRL_CMD_ERROR;
Daniel Willmann9225ab12011-07-28 21:38:51 +0200126 /* If verify() set an appropriate error message, don't change it. */
127 if (!command->reply)
128 command->reply = "Value failed verification.";
Daniel Willmann1264cb42010-10-21 15:00:36 +0200129 goto out;
130 }
Daniel Willmann1264cb42010-10-21 15:00:36 +0200131 }
132 ret = cmd_el->set(command, data);
133 goto out;
134 } else if (command->type == CTRL_TYPE_GET) {
135 if (!cmd_el->get) {
136 command->reply = "GET not implemented";
137 goto out;
138 }
139 ret = cmd_el->get(command, data);
140 goto out;
141 }
142out:
143 if (ret == CTRL_CMD_REPLY) {
144 if (command->type == CTRL_TYPE_SET) {
145 command->type = CTRL_TYPE_SET_REPLY;
146 } else if (command->type == CTRL_TYPE_GET) {
147 command->type = CTRL_TYPE_GET_REPLY;
148 }
149 } else if (ret == CTRL_CMD_ERROR) {
150 command->type = CTRL_TYPE_ERROR;
151 }
152 return ret;
153}
154
155static void add_word(struct ctrl_cmd_struct *cmd,
156 const char *start, const char *end)
157{
158 if (!cmd->command) {
159 cmd->command = talloc_zero_array(tall_vty_vec_ctx,
160 char*, 1);
161 cmd->nr_commands = 0;
162 } else {
163 cmd->command = talloc_realloc(tall_vty_vec_ctx,
164 cmd->command, char*,
165 cmd->nr_commands + 1);
166 }
167
168 cmd->command[cmd->nr_commands++] = talloc_strndup(cmd->command,
169 start, end - start);
170}
171
172static void create_cmd_struct(struct ctrl_cmd_struct *cmd, const char *name)
173{
174 const char *cur, *word;
175
176 for (cur = name, word = NULL; cur[0] != '\0'; ++cur) {
177 /* warn about optionals */
178 if (cur[0] == '(' || cur[0] == ')' || cur[0] == '|') {
Harald Welte7fd0c832014-08-20 19:58:13 +0200179 LOGP(DLCTRL, LOGL_ERROR,
Daniel Willmann1264cb42010-10-21 15:00:36 +0200180 "Optionals are not supported in '%s'\n", name);
181 goto failure;
182 }
183
184 if (isspace(cur[0])) {
185 if (word) {
186 add_word(cmd, word, cur);
187 word = NULL;
188 }
189 continue;
190 }
191
192 if (!word)
193 word = cur;
194 }
195
196 if (word)
197 add_word(cmd, word, cur);
198
199 return;
200failure:
201 cmd->nr_commands = 0;
202 talloc_free(cmd->command);
203}
204
Harald Welteb4186822018-05-26 17:25:11 +0200205/*! Install a given command definition at a given CTRL node.
Philipp Maierb6fd8ed2021-06-16 11:37:14 +0200206 * \param[in] node CTRL node at which \a cmd is to be installed
Harald Welteb4186822018-05-26 17:25:11 +0200207 * \param[in] cmd command definition to be installed
208 * \returns 0 on success; negative on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200209int ctrl_cmd_install(enum ctrl_node_type node, struct ctrl_cmd_element *cmd)
210{
211 vector cmds_vec;
212
213 cmds_vec = vector_lookup_ensure(ctrl_node_vec, node);
214
215 if (!cmds_vec) {
216 cmds_vec = vector_init(5);
217 if (!cmds_vec) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200218 LOGP(DLCTRL, LOGL_ERROR, "vector_init failed.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200219 return -ENOMEM;
220 }
221 vector_set_index(ctrl_node_vec, node, cmds_vec);
222 }
223
224 vector_set(cmds_vec, cmd);
225
226 create_cmd_struct(&cmd->strcmd, cmd->name);
227 return 0;
228}
229
Harald Welteb4186822018-05-26 17:25:11 +0200230/*! Allocate a control command of given \a type.
231 * \param[in] ctx talloc context from which to allocate
232 * \param[in] type command type to set after allocation
233 * \returns callee-allocated \ref ctrl_cmd. Caller must talloc_free() it. */
Holger Hans Peter Freythera6b34012011-08-22 23:44:32 +0200234struct ctrl_cmd *ctrl_cmd_create(void *ctx, enum ctrl_type type)
235{
236 struct ctrl_cmd *cmd;
237
238 cmd = talloc_zero(ctx, struct ctrl_cmd);
239 if (!cmd)
240 return NULL;
241
242 cmd->type = type;
243 return cmd;
244}
245
Harald Welteb4186822018-05-26 17:25:11 +0200246/*! Perform a deepl copy of the given \a cmd, allocating memory from \a ctx.
247 * \param[in] ctx talloc context from which to allocate
248 * \param[in cmd CTRL command to be copied
249 * \returns deep copy of \a cmd on success; NULL on error */
Daniel Willmann8b7a9622011-03-17 15:37:54 +0100250struct ctrl_cmd *ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd)
251{
252 struct ctrl_cmd *cmd2;
253
254 cmd2 = talloc_zero(ctx, struct ctrl_cmd);
255 if (!cmd2)
256 return NULL;
257
258 cmd2->type = cmd->type;
259 if (cmd->id) {
260 cmd2->id = talloc_strdup(cmd2, cmd->id);
261 if (!cmd2->id)
262 goto err;
263 }
264 if (cmd->variable) {
265 cmd2->variable = talloc_strdup(cmd2, cmd->variable);
266 if (!cmd2->variable)
267 goto err;
268 }
269 if (cmd->value) {
270 cmd2->value = talloc_strdup(cmd2, cmd->value);
271 if (!cmd2->value)
272 goto err;
273 }
274 if (cmd->reply) {
275 cmd2->reply = talloc_strdup(cmd2, cmd->reply);
276 if (!cmd2->reply)
277 goto err;
278 }
279
280 return cmd2;
281err:
282 talloc_free(cmd2);
283 return NULL;
284}
285
Harald Welteb4186822018-05-26 17:25:11 +0200286/*! Parse/Decode CTRL from \ref msgb into command struct.
287 * \param[in] ctx talloc context from which to allocate
288 * \param[in] msg message buffer containing command to be decoded
289 * \returns callee-allocated decoded CTRL command; NULL on allocation or other failure
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100290 * The caller is responsible to talloc_free() the returned struct pointer. */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200291struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
292{
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100293 struct ctrl_cmd *res = ctrl_cmd_parse2(ctx, msg);
294 if (res->type == CTRL_TYPE_ERROR) {
295 talloc_free(res);
296 return NULL;
297 }
298 return res;
299}
300
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200301static bool id_str_valid(const char *str)
302{
303 for (;*str;str++) {
304 if (!isdigit(*str))
305 return false;
306 }
307 return true;
308}
309
Harald Welteb4186822018-05-26 17:25:11 +0200310/*! Parse/Decode CTRL from \ref msgb into command struct.
311 * \param[in] ctx talloc context from which to allocate
312 * \param[in] msg message buffer containing command to be decoded
313 * \returns callee-allocated decoded CTRL command; NULL on allocation failure,
314 * ctrl->type == CTRL_TYPE_ERROR and an error message in ctrl->reply on any error.
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200315 * The caller is responsible to talloc_free() the returned struct pointer.
316 * If information of the origin of the ERROR cmd returned is required (received
317 * or local parsing failure), use \ref ctrl_cmd_parse3 instead. */
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100318struct ctrl_cmd *ctrl_cmd_parse2(void *ctx, struct msgb *msg)
319{
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200320 bool unused;
321 return ctrl_cmd_parse3(ctx, msg, &unused);
322}
323
324/*! Parse/Decode CTRL from \ref msgb into command struct.
325 * \param[in] ctx talloc context from which to allocate
326 * \param[in] msg message buffer containing command to be decoded
327 * \param[out] parse_failed Whether returned ERROR cmd was generatd locally
328 * (due to parse failure) or was received.
329 * \returns callee-allocated decoded CTRL command; NULL on allocation failure,
330 * ctrl->type == CTRL_TYPE_ERROR and an error message in ctrl->reply on any error.
331 * The caller is responsible to talloc_free() the returned struct pointer. */
332struct ctrl_cmd *ctrl_cmd_parse3(void *ctx, struct msgb *msg, bool *parse_failed)
333{
Daniel Willmann1264cb42010-10-21 15:00:36 +0200334 char *str, *tmp, *saveptr = NULL;
335 char *var, *val;
336 struct ctrl_cmd *cmd;
337
338 cmd = talloc_zero(ctx, struct ctrl_cmd);
339 if (!cmd) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200340 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate.\n");
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200341 *parse_failed = true;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200342 return NULL;
343 }
344
345 /* Make sure input is NULL terminated */
346 msgb_put_u8(msg, 0);
347 str = (char *) msg->l2h;
348
Harald Welteedf6fe72016-11-26 10:06:07 +0100349 OSMO_ASSERT(str);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200350 tmp = strtok_r(str, " ", &saveptr);
351 if (!tmp) {
352 cmd->type = CTRL_TYPE_ERROR;
353 cmd->id = "err";
354 cmd->reply = "Request malformed";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200355 LOGP(DLCTRL, LOGL_NOTICE, "Malformed request: \"%s\"\n", osmo_escape_str(str, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200356 goto err;
357 }
358
Max70c7d412017-02-24 13:59:14 +0100359 cmd->type = get_string_value(ctrl_type_vals, tmp);
Pau Espin Pedrol8aa85bd2017-06-23 12:43:31 +0200360 if ((int)cmd->type < 0 || cmd->type == CTRL_TYPE_UNKNOWN) {
Daniel Willmann1264cb42010-10-21 15:00:36 +0200361 cmd->type = CTRL_TYPE_ERROR;
362 cmd->id = "err";
363 cmd->reply = "Request type unknown";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200364 LOGP(DLCTRL, LOGL_NOTICE, "Request type unknown: \"%s\"\n", osmo_escape_str(str, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200365 goto err;
366 }
367
368 tmp = strtok_r(NULL, " ", &saveptr);
369
370 if (!tmp) {
371 cmd->type = CTRL_TYPE_ERROR;
372 cmd->id = "err";
373 cmd->reply = "Missing ID";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200374 LOGP(DLCTRL, LOGL_NOTICE, "Missing ID: \"%s\"\n", osmo_escape_str(str, -1));
375 goto err;
376 }
377
Pau Espin Pedrol40ad9132018-07-12 17:41:55 +0200378 if (!id_str_valid(tmp) &&
379 !(cmd->type == CTRL_TYPE_ERROR && strcmp(tmp, "err") == 0)) {
Pau Espin Pedrol55088b72018-07-12 13:03:52 +0200380 LOGP(DLCTRL, LOGL_NOTICE, "Invalid %s message ID number: \"%s\"\n",
381 get_value_string(ctrl_type_vals, cmd->type), osmo_escape_str(tmp, -1));
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200382 cmd->type = CTRL_TYPE_ERROR;
383 cmd->id = "err";
384 cmd->reply = "Invalid message ID number";
Daniel Willmann1264cb42010-10-21 15:00:36 +0200385 goto err;
386 }
387 cmd->id = talloc_strdup(cmd, tmp);
388 if (!cmd->id)
389 goto oom;
390
391 switch (cmd->type) {
392 case CTRL_TYPE_GET:
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200393 var = strtok_r(NULL, " \n", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200394 if (!var) {
395 cmd->type = CTRL_TYPE_ERROR;
396 cmd->reply = "GET incomplete";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200397 LOGP(DLCTRL, LOGL_NOTICE, "GET Command incomplete: \"%s\"\n",
398 osmo_escape_str(str, -1));
399 goto err;
400 }
401 if (!osmo_separated_identifiers_valid(var, ".")) {
402 cmd->type = CTRL_TYPE_ERROR;
403 cmd->reply = "GET variable contains invalid characters";
404 LOGP(DLCTRL, LOGL_NOTICE, "GET variable contains invalid characters: \"%s\"\n",
405 osmo_escape_str(var, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200406 goto err;
407 }
408 cmd->variable = talloc_strdup(cmd, var);
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200409 var = strtok_r(NULL, "", &saveptr);
410 if (var) {
411 cmd->type = CTRL_TYPE_ERROR;
412 cmd->reply = "GET with trailing characters";
413 LOGP(DLCTRL, LOGL_NOTICE, "GET with trailing characters: \"%s\"\n",
414 osmo_escape_str(var, -1));
415 goto err;
416 }
Harald Welte7fd0c832014-08-20 19:58:13 +0200417 LOGP(DLCTRL, LOGL_DEBUG, "Command: GET %s\n", cmd->variable);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200418 break;
419 case CTRL_TYPE_SET:
420 var = strtok_r(NULL, " ", &saveptr);
Holger Hans Peter Freyther5ad742d2014-08-08 19:41:28 +0200421 val = strtok_r(NULL, "\n", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200422 if (!var || !val) {
423 cmd->type = CTRL_TYPE_ERROR;
424 cmd->reply = "SET incomplete";
Harald Welte7fd0c832014-08-20 19:58:13 +0200425 LOGP(DLCTRL, LOGL_NOTICE, "SET Command incomplete\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200426 goto err;
427 }
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200428 if (!osmo_separated_identifiers_valid(var, ".")) {
429 cmd->type = CTRL_TYPE_ERROR;
430 cmd->reply = "SET variable contains invalid characters";
431 LOGP(DLCTRL, LOGL_NOTICE, "SET variable contains invalid characters: \"%s\"\n",
432 osmo_escape_str(var, -1));
433 goto err;
434 }
Daniel Willmann1264cb42010-10-21 15:00:36 +0200435 cmd->variable = talloc_strdup(cmd, var);
436 cmd->value = talloc_strdup(cmd, val);
437 if (!cmd->variable || !cmd->value)
438 goto oom;
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200439
440 var = strtok_r(NULL, "", &saveptr);
441 if (var) {
442 cmd->type = CTRL_TYPE_ERROR;
443 cmd->reply = "SET with trailing characters";
444 LOGP(DLCTRL, LOGL_NOTICE, "SET with trailing characters: \"%s\"\n",
445 osmo_escape_str(var, -1));
446 goto err;
447 }
448
449 LOGP(DLCTRL, LOGL_DEBUG, "Command: SET %s = \"%s\"\n", cmd->variable,
450 osmo_escape_str(cmd->value, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200451 break;
Neels Hofmeyra5e21622017-12-16 01:18:53 +0100452#define REPLY_CASE(TYPE, NAME) \
453 case TYPE: \
454 var = strtok_r(NULL, " ", &saveptr); \
455 val = strtok_r(NULL, "", &saveptr); \
456 if (!var) { \
457 cmd->type = CTRL_TYPE_ERROR; \
458 cmd->reply = NAME " incomplete"; \
459 LOGP(DLCTRL, LOGL_NOTICE, NAME " incomplete\n"); \
460 goto err; \
461 } \
462 if (!osmo_separated_identifiers_valid(var, ".")) { \
463 cmd->type = CTRL_TYPE_ERROR; \
464 cmd->reply = NAME " variable contains invalid characters"; \
465 LOGP(DLCTRL, LOGL_NOTICE, NAME " variable contains invalid characters: \"%s\"\n", \
466 osmo_escape_str(var, -1)); \
467 goto err; \
468 } \
469 cmd->variable = talloc_strdup(cmd, var); \
470 cmd->reply = talloc_strdup(cmd, val); \
471 if (!cmd->variable || !cmd->reply) \
472 goto oom; \
473 LOGP(DLCTRL, LOGL_DEBUG, "Command: " NAME " %s: %s\n", cmd->variable, \
474 osmo_escape_str(cmd->reply, -1)); \
475 break
476 REPLY_CASE(CTRL_TYPE_GET_REPLY, "GET REPLY");
477 REPLY_CASE(CTRL_TYPE_SET_REPLY, "SET REPLY");
478 REPLY_CASE(CTRL_TYPE_TRAP, "TRAP");
479#undef REPLY_CASE
Daniel Willmann1264cb42010-10-21 15:00:36 +0200480 case CTRL_TYPE_ERROR:
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200481 var = strtok_r(NULL, "", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200482 if (!var) {
483 cmd->reply = "";
484 goto err;
485 }
486 cmd->reply = talloc_strdup(cmd, var);
487 if (!cmd->reply)
488 goto oom;
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200489 LOGP(DLCTRL, LOGL_DEBUG, "Command: ERROR \"%s\"\n",
490 osmo_escape_str(cmd->reply, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200491 break;
492 case CTRL_TYPE_UNKNOWN:
493 default:
494 cmd->type = CTRL_TYPE_ERROR;
495 cmd->reply = "Unknown type";
496 goto err;
497 }
498
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200499 *parse_failed = false;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200500 return cmd;
501oom:
502 cmd->type = CTRL_TYPE_ERROR;
503 cmd->id = "err";
504 cmd->reply = "OOM";
505err:
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200506 *parse_failed = true;
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100507 return cmd;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200508}
509
Harald Welteb4186822018-05-26 17:25:11 +0200510/*! Encode a given CTRL command from its parsed form into a message buffer.
511 * \param[in] cmd decoded/parsed form of to-be-encoded command
512 * \returns callee-allocated message buffer containing the encoded \a cmd; NULL on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200513struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
514{
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200515 struct msgb *msg = NULL;
516 char *strbuf;
517 size_t len;
Max70c7d412017-02-24 13:59:14 +0100518 const char *type;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200519
520 if (!cmd->id)
521 return NULL;
522
Max70c7d412017-02-24 13:59:14 +0100523 type = get_value_string(ctrl_type_vals, cmd->type);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200524
525 switch (cmd->type) {
526 case CTRL_TYPE_GET:
527 if (!cmd->variable)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200528 return NULL;
529 strbuf = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200530 break;
531 case CTRL_TYPE_SET:
532 if (!cmd->variable || !cmd->value)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200533 return NULL;
534 strbuf = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id,
535 cmd->variable, cmd->value);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200536 break;
537 case CTRL_TYPE_GET_REPLY:
538 case CTRL_TYPE_SET_REPLY:
539 case CTRL_TYPE_TRAP:
540 if (!cmd->variable || !cmd->reply)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200541 return NULL;
542 strbuf = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id,
543 cmd->variable, cmd->reply);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200544 break;
545 case CTRL_TYPE_ERROR:
546 if (!cmd->reply)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200547 return NULL;
548 strbuf = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->reply);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200549 break;
550 default:
Harald Welte7fd0c832014-08-20 19:58:13 +0200551 LOGP(DLCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type);
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200552 return NULL;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200553 }
554
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200555 if (!strbuf) {
556 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
557 goto ret;
558 }
559 len = strlen(strbuf);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200560
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200561 msg = msgb_alloc_headroom(len + 128, 128, "ctrl ERROR command make");
562 if (!msg)
563 goto ret;
564 msg->l2h = msgb_put(msg, len);
565 memcpy(msg->l2h, strbuf, len);
566
567ret:
568 talloc_free(strbuf);
569 return msg;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200570}
Harald Welte39c9e7b2014-08-22 00:28:51 +0200571
Harald Welteb4186822018-05-26 17:25:11 +0200572/*! Build a deferred control command state and keep it the per-connection list of deferred commands.
573 * This function is typically called by a ctrl command handler that wishes to defer returning a
574 * response. The reutnred state can later be used to check if the deferred command is still alive,
575 * and to respond to the specific command. This only works to defer the response to GET and SET.
576 * \param[in] ctx talloc context from whihc to allocate the ctrl_cmd_def
577 * \param[in] cmd the control command whose response is deferred
578 * \param[in] data opaque, user-defined pointer
579 * \param[in] secs number of seconds until the command times out
580 * \returns callee-allocated ctrl_cmd_def */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200581struct ctrl_cmd_def *
582ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs)
583{
584 struct ctrl_cmd_def *cd;
585
586 if (!cmd->ccon)
587 return NULL;
588
589 cd = talloc_zero(ctx, struct ctrl_cmd_def);
590
Neels Hofmeyrcdbc9af2018-04-03 16:51:49 +0200591 cmd->defer = cd;
Harald Welte39c9e7b2014-08-22 00:28:51 +0200592 cd->cmd = cmd;
593 cd->data = data;
594
595 /* add to per-connection list of deferred commands */
596 llist_add(&cd->list, &cmd->ccon->def_cmds);
597
598 return cd;
599}
600
Harald Welteb4186822018-05-26 17:25:11 +0200601/*! Determine if the given deferred control command is still alive or a zombie.
602 * \param[in] cd deferred ctrl command state
603 * \returns 0 is \a cd is still alive; 1 if it's a zombie */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200604int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd)
605{
606 /* luckily we're still alive */
607 if (cd->cmd)
608 return 0;
609
610 /* if we are a zombie, make sure we really die */
611 llist_del(&cd->list);
612 talloc_free(cd);
613
614 return 1;
615}
616
Harald Welteb4186822018-05-26 17:25:11 +0200617/*! Send the response to a deferred ctrl command.
618 * The command can only be a resply to a SET or a GET operation.
619 * \param[in] cd deferred ctrl command state
620 * \returns 0 if command sent successfully; negative on error */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200621int ctrl_cmd_def_send(struct ctrl_cmd_def *cd)
622{
623 struct ctrl_cmd *cmd = cd->cmd;
624
625 int rc;
626
627 /* Deferred commands can only be responses to GET/SET or ERROR, but
628 * never TRAP or anything else */
629 switch (cmd->type) {
630 case CTRL_TYPE_GET:
631 cmd->type = CTRL_TYPE_GET_REPLY;
632 break;
633 case CTRL_TYPE_SET:
634 cmd->type = CTRL_TYPE_SET_REPLY;
635 break;
636 default:
637 cmd->type = CTRL_TYPE_ERROR;
638 }
639
640 rc = ctrl_cmd_send(&cmd->ccon->write_queue, cmd);
641
642 talloc_free(cmd);
643 llist_del(&cd->list);
644 talloc_free(cd);
645
646 return rc;
647}