blob: db205510295d151c88e8deded68906a54a917a7c [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>
Harald Welte319a77e2024-03-02 18:48:39 +010034#include <osmocom/ctrl/control_if.h>
Daniel Willmann1264cb42010-10-21 15:00:36 +020035
36#include <osmocom/core/msgb.h>
37#include <osmocom/core/talloc.h>
Max70c7d412017-02-24 13:59:14 +010038#include <osmocom/core/utils.h>
Daniel Willmann1264cb42010-10-21 15:00:36 +020039#include <osmocom/vty/command.h>
40#include <osmocom/vty/vector.h>
41
42extern vector ctrl_node_vec;
43
Max70c7d412017-02-24 13:59:14 +010044const struct value_string ctrl_type_vals[] = {
Neels Hofmeyr7c1ec8c2017-03-02 14:32:52 +010045 { CTRL_TYPE_UNKNOWN, "(unknown)" },
Max70c7d412017-02-24 13:59:14 +010046 { CTRL_TYPE_GET, "GET" },
47 { CTRL_TYPE_SET, "SET" },
48 { CTRL_TYPE_GET_REPLY, "GET_REPLY" },
49 { CTRL_TYPE_SET_REPLY, "SET_REPLY" },
50 { CTRL_TYPE_TRAP, "TRAP" },
51 { CTRL_TYPE_ERROR, "ERROR" },
Neels Hofmeyr7c1ec8c2017-03-02 14:32:52 +010052 { 0, NULL }
Daniel Willmann1264cb42010-10-21 15:00:36 +020053};
54
Daniel Willmann1264cb42010-10-21 15:00:36 +020055/* Functions from libosmocom */
56extern vector cmd_make_descvec(const char *string, const char *descstr);
57
58/* Get the ctrl_cmd_element that matches this command */
59static struct ctrl_cmd_element *ctrl_cmd_get_element_match(vector vline, vector node)
60{
61 int index, j;
62 const char *desc;
63 struct ctrl_cmd_element *cmd_el;
64 struct ctrl_cmd_struct *cmd_desc;
65 char *str;
66
67 for (index = 0; index < vector_active(node); index++) {
68 if ((cmd_el = vector_slot(node, index))) {
69 cmd_desc = &cmd_el->strcmd;
70 if (cmd_desc->nr_commands > vector_active(vline))
71 continue;
Holger Hans Peter Freyther5fb265e2015-04-05 14:36:31 +020072 for (j =0; j < vector_active(vline) && j < cmd_desc->nr_commands; j++) {
Daniel Willmann1264cb42010-10-21 15:00:36 +020073 str = vector_slot(vline, j);
74 desc = cmd_desc->command[j];
75 if (desc[0] == '*')
76 return cmd_el; /* Partial match */
77 if (strcmp(desc, str) != 0)
78 break;
79 }
80 /* We went through all the elements and all matched */
81 if (j == cmd_desc->nr_commands)
82 return cmd_el;
83 }
84 }
85
86 return NULL;
87}
88
Harald Welteb4186822018-05-26 17:25:11 +020089/*! Execute a given received command
90 * \param[in] vline vector representing the available/registered commands
91 * \param[inout] command parsed received command to be executed
92 * \param[in] node CTRL interface node
93 * \param[in] data opaque data passed to verify(), get() and set() call-backs
94 * \returns CTRL_CMD_HANDLED or CTRL_CMD_REPLY; CTRL_CMD_ERROR on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +020095int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data)
96{
97 int ret = CTRL_CMD_ERROR;
98 struct ctrl_cmd_element *cmd_el;
99
100 if ((command->type != CTRL_TYPE_GET) && (command->type != CTRL_TYPE_SET)) {
101 command->reply = "Trying to execute something not GET or SET";
102 goto out;
103 }
104 if ((command->type == CTRL_TYPE_SET) && (!command->value)) {
105 command->reply = "SET without a value";
106 goto out;
107 }
108
109 if (!vline)
110 goto out;
111
112 cmd_el = ctrl_cmd_get_element_match(vline, node);
113
114 if (!cmd_el) {
115 command->reply = "Command not found";
116 goto out;
117 }
118
119 if (command->type == CTRL_TYPE_SET) {
120 if (!cmd_el->set) {
121 command->reply = "SET not implemented";
122 goto out;
123 }
124 if (cmd_el->verify) {
125 if ((ret = cmd_el->verify(command, command->value, data))) {
126 ret = CTRL_CMD_ERROR;
Daniel Willmann9225ab12011-07-28 21:38:51 +0200127 /* If verify() set an appropriate error message, don't change it. */
128 if (!command->reply)
129 command->reply = "Value failed verification.";
Daniel Willmann1264cb42010-10-21 15:00:36 +0200130 goto out;
131 }
Daniel Willmann1264cb42010-10-21 15:00:36 +0200132 }
133 ret = cmd_el->set(command, data);
134 goto out;
135 } else if (command->type == CTRL_TYPE_GET) {
136 if (!cmd_el->get) {
137 command->reply = "GET not implemented";
138 goto out;
139 }
140 ret = cmd_el->get(command, data);
141 goto out;
142 }
143out:
144 if (ret == CTRL_CMD_REPLY) {
145 if (command->type == CTRL_TYPE_SET) {
146 command->type = CTRL_TYPE_SET_REPLY;
147 } else if (command->type == CTRL_TYPE_GET) {
148 command->type = CTRL_TYPE_GET_REPLY;
149 }
150 } else if (ret == CTRL_CMD_ERROR) {
151 command->type = CTRL_TYPE_ERROR;
152 }
153 return ret;
154}
155
156static void add_word(struct ctrl_cmd_struct *cmd,
157 const char *start, const char *end)
158{
159 if (!cmd->command) {
160 cmd->command = talloc_zero_array(tall_vty_vec_ctx,
161 char*, 1);
162 cmd->nr_commands = 0;
163 } else {
164 cmd->command = talloc_realloc(tall_vty_vec_ctx,
165 cmd->command, char*,
166 cmd->nr_commands + 1);
167 }
168
169 cmd->command[cmd->nr_commands++] = talloc_strndup(cmd->command,
170 start, end - start);
171}
172
173static void create_cmd_struct(struct ctrl_cmd_struct *cmd, const char *name)
174{
175 const char *cur, *word;
176
177 for (cur = name, word = NULL; cur[0] != '\0'; ++cur) {
178 /* warn about optionals */
179 if (cur[0] == '(' || cur[0] == ')' || cur[0] == '|') {
Harald Welte7fd0c832014-08-20 19:58:13 +0200180 LOGP(DLCTRL, LOGL_ERROR,
Daniel Willmann1264cb42010-10-21 15:00:36 +0200181 "Optionals are not supported in '%s'\n", name);
182 goto failure;
183 }
184
185 if (isspace(cur[0])) {
186 if (word) {
187 add_word(cmd, word, cur);
188 word = NULL;
189 }
190 continue;
191 }
192
193 if (!word)
194 word = cur;
195 }
196
197 if (word)
198 add_word(cmd, word, cur);
199
200 return;
201failure:
202 cmd->nr_commands = 0;
203 talloc_free(cmd->command);
204}
205
Harald Welteb4186822018-05-26 17:25:11 +0200206/*! Install a given command definition at a given CTRL node.
Philipp Maierb6fd8ed2021-06-16 11:37:14 +0200207 * \param[in] node CTRL node at which \a cmd is to be installed
Harald Welteb4186822018-05-26 17:25:11 +0200208 * \param[in] cmd command definition to be installed
209 * \returns 0 on success; negative on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200210int ctrl_cmd_install(enum ctrl_node_type node, struct ctrl_cmd_element *cmd)
211{
212 vector cmds_vec;
213
Pau Espin Pedrole709bd42023-01-05 17:10:35 +0100214 /* If this assert triggers, it means the program forgot to initialize
215 * the CTRL interface first by calling ctrl_handle_alloc(2)() directly
216 * or indirectly through ctrl_interface_setup_dynip(2)()
217 */
218 if (!ctrl_node_vec) {
219 LOGP(DLCTRL, LOGL_ERROR,
220 "ctrl_handle must be initialized prior to installing cmds.\n");
221 return -ENODEV;
222 }
223
Daniel Willmann1264cb42010-10-21 15:00:36 +0200224 cmds_vec = vector_lookup_ensure(ctrl_node_vec, node);
225
226 if (!cmds_vec) {
227 cmds_vec = vector_init(5);
228 if (!cmds_vec) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200229 LOGP(DLCTRL, LOGL_ERROR, "vector_init failed.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200230 return -ENOMEM;
231 }
232 vector_set_index(ctrl_node_vec, node, cmds_vec);
233 }
234
235 vector_set(cmds_vec, cmd);
236
237 create_cmd_struct(&cmd->strcmd, cmd->name);
238 return 0;
239}
240
Harald Welteb4186822018-05-26 17:25:11 +0200241/*! Allocate a control command of given \a type.
242 * \param[in] ctx talloc context from which to allocate
243 * \param[in] type command type to set after allocation
244 * \returns callee-allocated \ref ctrl_cmd. Caller must talloc_free() it. */
Holger Hans Peter Freythera6b34012011-08-22 23:44:32 +0200245struct ctrl_cmd *ctrl_cmd_create(void *ctx, enum ctrl_type type)
246{
247 struct ctrl_cmd *cmd;
248
249 cmd = talloc_zero(ctx, struct ctrl_cmd);
250 if (!cmd)
251 return NULL;
252
253 cmd->type = type;
254 return cmd;
255}
256
Harald Welteb4186822018-05-26 17:25:11 +0200257/*! Perform a deepl copy of the given \a cmd, allocating memory from \a ctx.
258 * \param[in] ctx talloc context from which to allocate
259 * \param[in cmd CTRL command to be copied
260 * \returns deep copy of \a cmd on success; NULL on error */
Daniel Willmann8b7a9622011-03-17 15:37:54 +0100261struct ctrl_cmd *ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd)
262{
263 struct ctrl_cmd *cmd2;
264
265 cmd2 = talloc_zero(ctx, struct ctrl_cmd);
266 if (!cmd2)
267 return NULL;
268
269 cmd2->type = cmd->type;
270 if (cmd->id) {
271 cmd2->id = talloc_strdup(cmd2, cmd->id);
272 if (!cmd2->id)
273 goto err;
274 }
275 if (cmd->variable) {
276 cmd2->variable = talloc_strdup(cmd2, cmd->variable);
277 if (!cmd2->variable)
278 goto err;
279 }
280 if (cmd->value) {
281 cmd2->value = talloc_strdup(cmd2, cmd->value);
282 if (!cmd2->value)
283 goto err;
284 }
285 if (cmd->reply) {
286 cmd2->reply = talloc_strdup(cmd2, cmd->reply);
287 if (!cmd2->reply)
288 goto err;
289 }
290
291 return cmd2;
292err:
293 talloc_free(cmd2);
294 return NULL;
295}
296
Harald Welteb4186822018-05-26 17:25:11 +0200297/*! Parse/Decode CTRL from \ref msgb into command struct.
298 * \param[in] ctx talloc context from which to allocate
299 * \param[in] msg message buffer containing command to be decoded
300 * \returns callee-allocated decoded CTRL command; NULL on allocation or other failure
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100301 * The caller is responsible to talloc_free() the returned struct pointer. */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200302struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
303{
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100304 struct ctrl_cmd *res = ctrl_cmd_parse2(ctx, msg);
305 if (res->type == CTRL_TYPE_ERROR) {
306 talloc_free(res);
307 return NULL;
308 }
309 return res;
310}
311
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200312static bool id_str_valid(const char *str)
313{
314 for (;*str;str++) {
315 if (!isdigit(*str))
316 return false;
317 }
318 return true;
319}
320
Harald Welteb4186822018-05-26 17:25:11 +0200321/*! Parse/Decode CTRL from \ref msgb into command struct.
322 * \param[in] ctx talloc context from which to allocate
323 * \param[in] msg message buffer containing command to be decoded
324 * \returns callee-allocated decoded CTRL command; NULL on allocation failure,
325 * ctrl->type == CTRL_TYPE_ERROR and an error message in ctrl->reply on any error.
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200326 * The caller is responsible to talloc_free() the returned struct pointer.
327 * If information of the origin of the ERROR cmd returned is required (received
328 * or local parsing failure), use \ref ctrl_cmd_parse3 instead. */
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100329struct ctrl_cmd *ctrl_cmd_parse2(void *ctx, struct msgb *msg)
330{
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200331 bool unused;
332 return ctrl_cmd_parse3(ctx, msg, &unused);
333}
334
335/*! Parse/Decode CTRL from \ref msgb into command struct.
336 * \param[in] ctx talloc context from which to allocate
337 * \param[in] msg message buffer containing command to be decoded
338 * \param[out] parse_failed Whether returned ERROR cmd was generatd locally
339 * (due to parse failure) or was received.
340 * \returns callee-allocated decoded CTRL command; NULL on allocation failure,
341 * ctrl->type == CTRL_TYPE_ERROR and an error message in ctrl->reply on any error.
342 * The caller is responsible to talloc_free() the returned struct pointer. */
343struct ctrl_cmd *ctrl_cmd_parse3(void *ctx, struct msgb *msg, bool *parse_failed)
344{
Daniel Willmann1264cb42010-10-21 15:00:36 +0200345 char *str, *tmp, *saveptr = NULL;
346 char *var, *val;
347 struct ctrl_cmd *cmd;
348
349 cmd = talloc_zero(ctx, struct ctrl_cmd);
350 if (!cmd) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200351 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate.\n");
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200352 *parse_failed = true;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200353 return NULL;
354 }
355
356 /* Make sure input is NULL terminated */
357 msgb_put_u8(msg, 0);
358 str = (char *) msg->l2h;
359
Harald Welteedf6fe72016-11-26 10:06:07 +0100360 OSMO_ASSERT(str);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200361 tmp = strtok_r(str, " ", &saveptr);
362 if (!tmp) {
363 cmd->type = CTRL_TYPE_ERROR;
364 cmd->id = "err";
365 cmd->reply = "Request malformed";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200366 LOGP(DLCTRL, LOGL_NOTICE, "Malformed request: \"%s\"\n", osmo_escape_str(str, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200367 goto err;
368 }
369
Max70c7d412017-02-24 13:59:14 +0100370 cmd->type = get_string_value(ctrl_type_vals, tmp);
Pau Espin Pedrol8aa85bd2017-06-23 12:43:31 +0200371 if ((int)cmd->type < 0 || cmd->type == CTRL_TYPE_UNKNOWN) {
Daniel Willmann1264cb42010-10-21 15:00:36 +0200372 cmd->type = CTRL_TYPE_ERROR;
373 cmd->id = "err";
374 cmd->reply = "Request type unknown";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200375 LOGP(DLCTRL, LOGL_NOTICE, "Request type unknown: \"%s\"\n", osmo_escape_str(str, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200376 goto err;
377 }
378
379 tmp = strtok_r(NULL, " ", &saveptr);
380
381 if (!tmp) {
382 cmd->type = CTRL_TYPE_ERROR;
383 cmd->id = "err";
384 cmd->reply = "Missing ID";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200385 LOGP(DLCTRL, LOGL_NOTICE, "Missing ID: \"%s\"\n", osmo_escape_str(str, -1));
386 goto err;
387 }
388
Pau Espin Pedrol40ad9132018-07-12 17:41:55 +0200389 if (!id_str_valid(tmp) &&
390 !(cmd->type == CTRL_TYPE_ERROR && strcmp(tmp, "err") == 0)) {
Pau Espin Pedrol55088b72018-07-12 13:03:52 +0200391 LOGP(DLCTRL, LOGL_NOTICE, "Invalid %s message ID number: \"%s\"\n",
392 get_value_string(ctrl_type_vals, cmd->type), osmo_escape_str(tmp, -1));
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200393 cmd->type = CTRL_TYPE_ERROR;
394 cmd->id = "err";
395 cmd->reply = "Invalid message ID number";
Daniel Willmann1264cb42010-10-21 15:00:36 +0200396 goto err;
397 }
398 cmd->id = talloc_strdup(cmd, tmp);
399 if (!cmd->id)
400 goto oom;
401
402 switch (cmd->type) {
403 case CTRL_TYPE_GET:
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200404 var = strtok_r(NULL, " \n", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200405 if (!var) {
406 cmd->type = CTRL_TYPE_ERROR;
407 cmd->reply = "GET incomplete";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200408 LOGP(DLCTRL, LOGL_NOTICE, "GET Command incomplete: \"%s\"\n",
409 osmo_escape_str(str, -1));
410 goto err;
411 }
412 if (!osmo_separated_identifiers_valid(var, ".")) {
413 cmd->type = CTRL_TYPE_ERROR;
414 cmd->reply = "GET variable contains invalid characters";
415 LOGP(DLCTRL, LOGL_NOTICE, "GET variable contains invalid characters: \"%s\"\n",
416 osmo_escape_str(var, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200417 goto err;
418 }
419 cmd->variable = talloc_strdup(cmd, var);
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200420 var = strtok_r(NULL, "", &saveptr);
421 if (var) {
422 cmd->type = CTRL_TYPE_ERROR;
423 cmd->reply = "GET with trailing characters";
424 LOGP(DLCTRL, LOGL_NOTICE, "GET with trailing characters: \"%s\"\n",
425 osmo_escape_str(var, -1));
426 goto err;
427 }
Harald Welte7fd0c832014-08-20 19:58:13 +0200428 LOGP(DLCTRL, LOGL_DEBUG, "Command: GET %s\n", cmd->variable);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200429 break;
430 case CTRL_TYPE_SET:
431 var = strtok_r(NULL, " ", &saveptr);
Holger Hans Peter Freyther5ad742d2014-08-08 19:41:28 +0200432 val = strtok_r(NULL, "\n", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200433 if (!var || !val) {
434 cmd->type = CTRL_TYPE_ERROR;
435 cmd->reply = "SET incomplete";
Harald Welte7fd0c832014-08-20 19:58:13 +0200436 LOGP(DLCTRL, LOGL_NOTICE, "SET Command incomplete\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200437 goto err;
438 }
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200439 if (!osmo_separated_identifiers_valid(var, ".")) {
440 cmd->type = CTRL_TYPE_ERROR;
441 cmd->reply = "SET variable contains invalid characters";
442 LOGP(DLCTRL, LOGL_NOTICE, "SET variable contains invalid characters: \"%s\"\n",
443 osmo_escape_str(var, -1));
444 goto err;
445 }
Daniel Willmann1264cb42010-10-21 15:00:36 +0200446 cmd->variable = talloc_strdup(cmd, var);
447 cmd->value = talloc_strdup(cmd, val);
448 if (!cmd->variable || !cmd->value)
449 goto oom;
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200450
451 var = strtok_r(NULL, "", &saveptr);
452 if (var) {
453 cmd->type = CTRL_TYPE_ERROR;
454 cmd->reply = "SET with trailing characters";
455 LOGP(DLCTRL, LOGL_NOTICE, "SET with trailing characters: \"%s\"\n",
456 osmo_escape_str(var, -1));
457 goto err;
458 }
459
460 LOGP(DLCTRL, LOGL_DEBUG, "Command: SET %s = \"%s\"\n", cmd->variable,
461 osmo_escape_str(cmd->value, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200462 break;
Neels Hofmeyra5e21622017-12-16 01:18:53 +0100463#define REPLY_CASE(TYPE, NAME) \
464 case TYPE: \
465 var = strtok_r(NULL, " ", &saveptr); \
466 val = strtok_r(NULL, "", &saveptr); \
467 if (!var) { \
468 cmd->type = CTRL_TYPE_ERROR; \
469 cmd->reply = NAME " incomplete"; \
470 LOGP(DLCTRL, LOGL_NOTICE, NAME " incomplete\n"); \
471 goto err; \
472 } \
473 if (!osmo_separated_identifiers_valid(var, ".")) { \
474 cmd->type = CTRL_TYPE_ERROR; \
475 cmd->reply = NAME " variable contains invalid characters"; \
476 LOGP(DLCTRL, LOGL_NOTICE, NAME " variable contains invalid characters: \"%s\"\n", \
477 osmo_escape_str(var, -1)); \
478 goto err; \
479 } \
480 cmd->variable = talloc_strdup(cmd, var); \
481 cmd->reply = talloc_strdup(cmd, val); \
482 if (!cmd->variable || !cmd->reply) \
483 goto oom; \
484 LOGP(DLCTRL, LOGL_DEBUG, "Command: " NAME " %s: %s\n", cmd->variable, \
485 osmo_escape_str(cmd->reply, -1)); \
486 break
487 REPLY_CASE(CTRL_TYPE_GET_REPLY, "GET REPLY");
488 REPLY_CASE(CTRL_TYPE_SET_REPLY, "SET REPLY");
489 REPLY_CASE(CTRL_TYPE_TRAP, "TRAP");
490#undef REPLY_CASE
Daniel Willmann1264cb42010-10-21 15:00:36 +0200491 case CTRL_TYPE_ERROR:
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200492 var = strtok_r(NULL, "", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200493 if (!var) {
494 cmd->reply = "";
495 goto err;
496 }
497 cmd->reply = talloc_strdup(cmd, var);
498 if (!cmd->reply)
499 goto oom;
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200500 LOGP(DLCTRL, LOGL_DEBUG, "Command: ERROR \"%s\"\n",
501 osmo_escape_str(cmd->reply, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200502 break;
503 case CTRL_TYPE_UNKNOWN:
504 default:
505 cmd->type = CTRL_TYPE_ERROR;
506 cmd->reply = "Unknown type";
507 goto err;
508 }
509
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200510 *parse_failed = false;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200511 return cmd;
512oom:
513 cmd->type = CTRL_TYPE_ERROR;
514 cmd->id = "err";
515 cmd->reply = "OOM";
516err:
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200517 *parse_failed = true;
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100518 return cmd;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200519}
520
Harald Welteb4186822018-05-26 17:25:11 +0200521/*! Encode a given CTRL command from its parsed form into a message buffer.
522 * \param[in] cmd decoded/parsed form of to-be-encoded command
523 * \returns callee-allocated message buffer containing the encoded \a cmd; NULL on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200524struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
525{
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200526 struct msgb *msg = NULL;
527 char *strbuf;
528 size_t len;
Max70c7d412017-02-24 13:59:14 +0100529 const char *type;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200530
531 if (!cmd->id)
532 return NULL;
533
Max70c7d412017-02-24 13:59:14 +0100534 type = get_value_string(ctrl_type_vals, cmd->type);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200535
536 switch (cmd->type) {
537 case CTRL_TYPE_GET:
538 if (!cmd->variable)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200539 return NULL;
540 strbuf = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200541 break;
542 case CTRL_TYPE_SET:
543 if (!cmd->variable || !cmd->value)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200544 return NULL;
545 strbuf = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id,
546 cmd->variable, cmd->value);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200547 break;
548 case CTRL_TYPE_GET_REPLY:
549 case CTRL_TYPE_SET_REPLY:
550 case CTRL_TYPE_TRAP:
551 if (!cmd->variable || !cmd->reply)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200552 return NULL;
553 strbuf = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id,
554 cmd->variable, cmd->reply);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200555 break;
556 case CTRL_TYPE_ERROR:
557 if (!cmd->reply)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200558 return NULL;
559 strbuf = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->reply);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200560 break;
561 default:
Harald Welte7fd0c832014-08-20 19:58:13 +0200562 LOGP(DLCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type);
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200563 return NULL;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200564 }
565
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200566 if (!strbuf) {
567 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
568 goto ret;
569 }
570 len = strlen(strbuf);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200571
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200572 msg = msgb_alloc_headroom(len + 128, 128, "ctrl ERROR command make");
573 if (!msg)
574 goto ret;
575 msg->l2h = msgb_put(msg, len);
576 memcpy(msg->l2h, strbuf, len);
577
578ret:
579 talloc_free(strbuf);
580 return msg;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200581}
Harald Welte39c9e7b2014-08-22 00:28:51 +0200582
Harald Welteb4186822018-05-26 17:25:11 +0200583/*! Build a deferred control command state and keep it the per-connection list of deferred commands.
584 * This function is typically called by a ctrl command handler that wishes to defer returning a
585 * response. The reutnred state can later be used to check if the deferred command is still alive,
586 * and to respond to the specific command. This only works to defer the response to GET and SET.
587 * \param[in] ctx talloc context from whihc to allocate the ctrl_cmd_def
588 * \param[in] cmd the control command whose response is deferred
589 * \param[in] data opaque, user-defined pointer
590 * \param[in] secs number of seconds until the command times out
591 * \returns callee-allocated ctrl_cmd_def */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200592struct ctrl_cmd_def *
593ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs)
594{
595 struct ctrl_cmd_def *cd;
596
597 if (!cmd->ccon)
598 return NULL;
599
600 cd = talloc_zero(ctx, struct ctrl_cmd_def);
601
Neels Hofmeyrcdbc9af2018-04-03 16:51:49 +0200602 cmd->defer = cd;
Harald Welte39c9e7b2014-08-22 00:28:51 +0200603 cd->cmd = cmd;
604 cd->data = data;
605
606 /* add to per-connection list of deferred commands */
607 llist_add(&cd->list, &cmd->ccon->def_cmds);
608
609 return cd;
610}
611
Harald Welteb4186822018-05-26 17:25:11 +0200612/*! Determine if the given deferred control command is still alive or a zombie.
613 * \param[in] cd deferred ctrl command state
614 * \returns 0 is \a cd is still alive; 1 if it's a zombie */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200615int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd)
616{
617 /* luckily we're still alive */
618 if (cd->cmd)
619 return 0;
620
621 /* if we are a zombie, make sure we really die */
622 llist_del(&cd->list);
623 talloc_free(cd);
624
625 return 1;
626}
627
Harald Welteb4186822018-05-26 17:25:11 +0200628/*! Send the response to a deferred ctrl command.
629 * The command can only be a resply to a SET or a GET operation.
630 * \param[in] cd deferred ctrl command state
631 * \returns 0 if command sent successfully; negative on error */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200632int ctrl_cmd_def_send(struct ctrl_cmd_def *cd)
633{
634 struct ctrl_cmd *cmd = cd->cmd;
635
636 int rc;
637
638 /* Deferred commands can only be responses to GET/SET or ERROR, but
639 * never TRAP or anything else */
640 switch (cmd->type) {
641 case CTRL_TYPE_GET:
642 cmd->type = CTRL_TYPE_GET_REPLY;
643 break;
644 case CTRL_TYPE_SET:
645 cmd->type = CTRL_TYPE_SET_REPLY;
646 break;
647 default:
648 cmd->type = CTRL_TYPE_ERROR;
649 }
650
Harald Welte319a77e2024-03-02 18:48:39 +0100651 rc = ctrl_cmd_send2(cmd->ccon, cmd);
Harald Welte39c9e7b2014-08-22 00:28:51 +0200652
653 talloc_free(cmd);
654 llist_del(&cd->list);
655 talloc_free(cd);
656
657 return rc;
658}