blob: dec19b9b88861f823d8634b3eeef4732e678ffd9 [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
Pau Espin Pedrole709bd42023-01-05 17:10:35 +0100213 /* If this assert triggers, it means the program forgot to initialize
214 * the CTRL interface first by calling ctrl_handle_alloc(2)() directly
215 * or indirectly through ctrl_interface_setup_dynip(2)()
216 */
217 if (!ctrl_node_vec) {
218 LOGP(DLCTRL, LOGL_ERROR,
219 "ctrl_handle must be initialized prior to installing cmds.\n");
220 return -ENODEV;
221 }
222
Daniel Willmann1264cb42010-10-21 15:00:36 +0200223 cmds_vec = vector_lookup_ensure(ctrl_node_vec, node);
224
225 if (!cmds_vec) {
226 cmds_vec = vector_init(5);
227 if (!cmds_vec) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200228 LOGP(DLCTRL, LOGL_ERROR, "vector_init failed.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200229 return -ENOMEM;
230 }
231 vector_set_index(ctrl_node_vec, node, cmds_vec);
232 }
233
234 vector_set(cmds_vec, cmd);
235
236 create_cmd_struct(&cmd->strcmd, cmd->name);
237 return 0;
238}
239
Harald Welteb4186822018-05-26 17:25:11 +0200240/*! Allocate a control command of given \a type.
241 * \param[in] ctx talloc context from which to allocate
242 * \param[in] type command type to set after allocation
243 * \returns callee-allocated \ref ctrl_cmd. Caller must talloc_free() it. */
Holger Hans Peter Freythera6b34012011-08-22 23:44:32 +0200244struct ctrl_cmd *ctrl_cmd_create(void *ctx, enum ctrl_type type)
245{
246 struct ctrl_cmd *cmd;
247
248 cmd = talloc_zero(ctx, struct ctrl_cmd);
249 if (!cmd)
250 return NULL;
251
252 cmd->type = type;
253 return cmd;
254}
255
Harald Welteb4186822018-05-26 17:25:11 +0200256/*! Perform a deepl copy of the given \a cmd, allocating memory from \a ctx.
257 * \param[in] ctx talloc context from which to allocate
258 * \param[in cmd CTRL command to be copied
259 * \returns deep copy of \a cmd on success; NULL on error */
Daniel Willmann8b7a9622011-03-17 15:37:54 +0100260struct ctrl_cmd *ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd)
261{
262 struct ctrl_cmd *cmd2;
263
264 cmd2 = talloc_zero(ctx, struct ctrl_cmd);
265 if (!cmd2)
266 return NULL;
267
268 cmd2->type = cmd->type;
269 if (cmd->id) {
270 cmd2->id = talloc_strdup(cmd2, cmd->id);
271 if (!cmd2->id)
272 goto err;
273 }
274 if (cmd->variable) {
275 cmd2->variable = talloc_strdup(cmd2, cmd->variable);
276 if (!cmd2->variable)
277 goto err;
278 }
279 if (cmd->value) {
280 cmd2->value = talloc_strdup(cmd2, cmd->value);
281 if (!cmd2->value)
282 goto err;
283 }
284 if (cmd->reply) {
285 cmd2->reply = talloc_strdup(cmd2, cmd->reply);
286 if (!cmd2->reply)
287 goto err;
288 }
289
290 return cmd2;
291err:
292 talloc_free(cmd2);
293 return NULL;
294}
295
Harald Welteb4186822018-05-26 17:25:11 +0200296/*! Parse/Decode CTRL from \ref msgb into command struct.
297 * \param[in] ctx talloc context from which to allocate
298 * \param[in] msg message buffer containing command to be decoded
299 * \returns callee-allocated decoded CTRL command; NULL on allocation or other failure
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100300 * The caller is responsible to talloc_free() the returned struct pointer. */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200301struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
302{
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100303 struct ctrl_cmd *res = ctrl_cmd_parse2(ctx, msg);
304 if (res->type == CTRL_TYPE_ERROR) {
305 talloc_free(res);
306 return NULL;
307 }
308 return res;
309}
310
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200311static bool id_str_valid(const char *str)
312{
313 for (;*str;str++) {
314 if (!isdigit(*str))
315 return false;
316 }
317 return true;
318}
319
Harald Welteb4186822018-05-26 17:25:11 +0200320/*! Parse/Decode CTRL from \ref msgb into command struct.
321 * \param[in] ctx talloc context from which to allocate
322 * \param[in] msg message buffer containing command to be decoded
323 * \returns callee-allocated decoded CTRL command; NULL on allocation failure,
324 * ctrl->type == CTRL_TYPE_ERROR and an error message in ctrl->reply on any error.
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200325 * The caller is responsible to talloc_free() the returned struct pointer.
326 * If information of the origin of the ERROR cmd returned is required (received
327 * or local parsing failure), use \ref ctrl_cmd_parse3 instead. */
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100328struct ctrl_cmd *ctrl_cmd_parse2(void *ctx, struct msgb *msg)
329{
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200330 bool unused;
331 return ctrl_cmd_parse3(ctx, msg, &unused);
332}
333
334/*! Parse/Decode CTRL from \ref msgb into command struct.
335 * \param[in] ctx talloc context from which to allocate
336 * \param[in] msg message buffer containing command to be decoded
337 * \param[out] parse_failed Whether returned ERROR cmd was generatd locally
338 * (due to parse failure) or was received.
339 * \returns callee-allocated decoded CTRL command; NULL on allocation failure,
340 * ctrl->type == CTRL_TYPE_ERROR and an error message in ctrl->reply on any error.
341 * The caller is responsible to talloc_free() the returned struct pointer. */
342struct ctrl_cmd *ctrl_cmd_parse3(void *ctx, struct msgb *msg, bool *parse_failed)
343{
Daniel Willmann1264cb42010-10-21 15:00:36 +0200344 char *str, *tmp, *saveptr = NULL;
345 char *var, *val;
346 struct ctrl_cmd *cmd;
347
348 cmd = talloc_zero(ctx, struct ctrl_cmd);
349 if (!cmd) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200350 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate.\n");
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200351 *parse_failed = true;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200352 return NULL;
353 }
354
355 /* Make sure input is NULL terminated */
356 msgb_put_u8(msg, 0);
357 str = (char *) msg->l2h;
358
Harald Welteedf6fe72016-11-26 10:06:07 +0100359 OSMO_ASSERT(str);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200360 tmp = strtok_r(str, " ", &saveptr);
361 if (!tmp) {
362 cmd->type = CTRL_TYPE_ERROR;
363 cmd->id = "err";
364 cmd->reply = "Request malformed";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200365 LOGP(DLCTRL, LOGL_NOTICE, "Malformed request: \"%s\"\n", osmo_escape_str(str, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200366 goto err;
367 }
368
Max70c7d412017-02-24 13:59:14 +0100369 cmd->type = get_string_value(ctrl_type_vals, tmp);
Pau Espin Pedrol8aa85bd2017-06-23 12:43:31 +0200370 if ((int)cmd->type < 0 || cmd->type == CTRL_TYPE_UNKNOWN) {
Daniel Willmann1264cb42010-10-21 15:00:36 +0200371 cmd->type = CTRL_TYPE_ERROR;
372 cmd->id = "err";
373 cmd->reply = "Request type unknown";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200374 LOGP(DLCTRL, LOGL_NOTICE, "Request type unknown: \"%s\"\n", osmo_escape_str(str, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200375 goto err;
376 }
377
378 tmp = strtok_r(NULL, " ", &saveptr);
379
380 if (!tmp) {
381 cmd->type = CTRL_TYPE_ERROR;
382 cmd->id = "err";
383 cmd->reply = "Missing ID";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200384 LOGP(DLCTRL, LOGL_NOTICE, "Missing ID: \"%s\"\n", osmo_escape_str(str, -1));
385 goto err;
386 }
387
Pau Espin Pedrol40ad9132018-07-12 17:41:55 +0200388 if (!id_str_valid(tmp) &&
389 !(cmd->type == CTRL_TYPE_ERROR && strcmp(tmp, "err") == 0)) {
Pau Espin Pedrol55088b72018-07-12 13:03:52 +0200390 LOGP(DLCTRL, LOGL_NOTICE, "Invalid %s message ID number: \"%s\"\n",
391 get_value_string(ctrl_type_vals, cmd->type), osmo_escape_str(tmp, -1));
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200392 cmd->type = CTRL_TYPE_ERROR;
393 cmd->id = "err";
394 cmd->reply = "Invalid message ID number";
Daniel Willmann1264cb42010-10-21 15:00:36 +0200395 goto err;
396 }
397 cmd->id = talloc_strdup(cmd, tmp);
398 if (!cmd->id)
399 goto oom;
400
401 switch (cmd->type) {
402 case CTRL_TYPE_GET:
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200403 var = strtok_r(NULL, " \n", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200404 if (!var) {
405 cmd->type = CTRL_TYPE_ERROR;
406 cmd->reply = "GET incomplete";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200407 LOGP(DLCTRL, LOGL_NOTICE, "GET Command incomplete: \"%s\"\n",
408 osmo_escape_str(str, -1));
409 goto err;
410 }
411 if (!osmo_separated_identifiers_valid(var, ".")) {
412 cmd->type = CTRL_TYPE_ERROR;
413 cmd->reply = "GET variable contains invalid characters";
414 LOGP(DLCTRL, LOGL_NOTICE, "GET variable contains invalid characters: \"%s\"\n",
415 osmo_escape_str(var, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200416 goto err;
417 }
418 cmd->variable = talloc_strdup(cmd, var);
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200419 var = strtok_r(NULL, "", &saveptr);
420 if (var) {
421 cmd->type = CTRL_TYPE_ERROR;
422 cmd->reply = "GET with trailing characters";
423 LOGP(DLCTRL, LOGL_NOTICE, "GET with trailing characters: \"%s\"\n",
424 osmo_escape_str(var, -1));
425 goto err;
426 }
Harald Welte7fd0c832014-08-20 19:58:13 +0200427 LOGP(DLCTRL, LOGL_DEBUG, "Command: GET %s\n", cmd->variable);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200428 break;
429 case CTRL_TYPE_SET:
430 var = strtok_r(NULL, " ", &saveptr);
Holger Hans Peter Freyther5ad742d2014-08-08 19:41:28 +0200431 val = strtok_r(NULL, "\n", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200432 if (!var || !val) {
433 cmd->type = CTRL_TYPE_ERROR;
434 cmd->reply = "SET incomplete";
Harald Welte7fd0c832014-08-20 19:58:13 +0200435 LOGP(DLCTRL, LOGL_NOTICE, "SET Command incomplete\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200436 goto err;
437 }
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200438 if (!osmo_separated_identifiers_valid(var, ".")) {
439 cmd->type = CTRL_TYPE_ERROR;
440 cmd->reply = "SET variable contains invalid characters";
441 LOGP(DLCTRL, LOGL_NOTICE, "SET variable contains invalid characters: \"%s\"\n",
442 osmo_escape_str(var, -1));
443 goto err;
444 }
Daniel Willmann1264cb42010-10-21 15:00:36 +0200445 cmd->variable = talloc_strdup(cmd, var);
446 cmd->value = talloc_strdup(cmd, val);
447 if (!cmd->variable || !cmd->value)
448 goto oom;
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200449
450 var = strtok_r(NULL, "", &saveptr);
451 if (var) {
452 cmd->type = CTRL_TYPE_ERROR;
453 cmd->reply = "SET with trailing characters";
454 LOGP(DLCTRL, LOGL_NOTICE, "SET with trailing characters: \"%s\"\n",
455 osmo_escape_str(var, -1));
456 goto err;
457 }
458
459 LOGP(DLCTRL, LOGL_DEBUG, "Command: SET %s = \"%s\"\n", cmd->variable,
460 osmo_escape_str(cmd->value, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200461 break;
Neels Hofmeyra5e21622017-12-16 01:18:53 +0100462#define REPLY_CASE(TYPE, NAME) \
463 case TYPE: \
464 var = strtok_r(NULL, " ", &saveptr); \
465 val = strtok_r(NULL, "", &saveptr); \
466 if (!var) { \
467 cmd->type = CTRL_TYPE_ERROR; \
468 cmd->reply = NAME " incomplete"; \
469 LOGP(DLCTRL, LOGL_NOTICE, NAME " incomplete\n"); \
470 goto err; \
471 } \
472 if (!osmo_separated_identifiers_valid(var, ".")) { \
473 cmd->type = CTRL_TYPE_ERROR; \
474 cmd->reply = NAME " variable contains invalid characters"; \
475 LOGP(DLCTRL, LOGL_NOTICE, NAME " variable contains invalid characters: \"%s\"\n", \
476 osmo_escape_str(var, -1)); \
477 goto err; \
478 } \
479 cmd->variable = talloc_strdup(cmd, var); \
480 cmd->reply = talloc_strdup(cmd, val); \
481 if (!cmd->variable || !cmd->reply) \
482 goto oom; \
483 LOGP(DLCTRL, LOGL_DEBUG, "Command: " NAME " %s: %s\n", cmd->variable, \
484 osmo_escape_str(cmd->reply, -1)); \
485 break
486 REPLY_CASE(CTRL_TYPE_GET_REPLY, "GET REPLY");
487 REPLY_CASE(CTRL_TYPE_SET_REPLY, "SET REPLY");
488 REPLY_CASE(CTRL_TYPE_TRAP, "TRAP");
489#undef REPLY_CASE
Daniel Willmann1264cb42010-10-21 15:00:36 +0200490 case CTRL_TYPE_ERROR:
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200491 var = strtok_r(NULL, "", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200492 if (!var) {
493 cmd->reply = "";
494 goto err;
495 }
496 cmd->reply = talloc_strdup(cmd, var);
497 if (!cmd->reply)
498 goto oom;
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200499 LOGP(DLCTRL, LOGL_DEBUG, "Command: ERROR \"%s\"\n",
500 osmo_escape_str(cmd->reply, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200501 break;
502 case CTRL_TYPE_UNKNOWN:
503 default:
504 cmd->type = CTRL_TYPE_ERROR;
505 cmd->reply = "Unknown type";
506 goto err;
507 }
508
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200509 *parse_failed = false;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200510 return cmd;
511oom:
512 cmd->type = CTRL_TYPE_ERROR;
513 cmd->id = "err";
514 cmd->reply = "OOM";
515err:
Pau Espin Pedrol239ed3b2018-07-12 17:51:16 +0200516 *parse_failed = true;
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100517 return cmd;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200518}
519
Harald Welteb4186822018-05-26 17:25:11 +0200520/*! Encode a given CTRL command from its parsed form into a message buffer.
521 * \param[in] cmd decoded/parsed form of to-be-encoded command
522 * \returns callee-allocated message buffer containing the encoded \a cmd; NULL on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200523struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
524{
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200525 struct msgb *msg = NULL;
526 char *strbuf;
527 size_t len;
Max70c7d412017-02-24 13:59:14 +0100528 const char *type;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200529
530 if (!cmd->id)
531 return NULL;
532
Max70c7d412017-02-24 13:59:14 +0100533 type = get_value_string(ctrl_type_vals, cmd->type);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200534
535 switch (cmd->type) {
536 case CTRL_TYPE_GET:
537 if (!cmd->variable)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200538 return NULL;
539 strbuf = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200540 break;
541 case CTRL_TYPE_SET:
542 if (!cmd->variable || !cmd->value)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200543 return NULL;
544 strbuf = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id,
545 cmd->variable, cmd->value);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200546 break;
547 case CTRL_TYPE_GET_REPLY:
548 case CTRL_TYPE_SET_REPLY:
549 case CTRL_TYPE_TRAP:
550 if (!cmd->variable || !cmd->reply)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200551 return NULL;
552 strbuf = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id,
553 cmd->variable, cmd->reply);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200554 break;
555 case CTRL_TYPE_ERROR:
556 if (!cmd->reply)
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200557 return NULL;
558 strbuf = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->reply);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200559 break;
560 default:
Harald Welte7fd0c832014-08-20 19:58:13 +0200561 LOGP(DLCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type);
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200562 return NULL;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200563 }
564
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200565 if (!strbuf) {
566 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
567 goto ret;
568 }
569 len = strlen(strbuf);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200570
Pau Espin Pedrolf5b8ed12021-06-10 13:15:20 +0200571 msg = msgb_alloc_headroom(len + 128, 128, "ctrl ERROR command make");
572 if (!msg)
573 goto ret;
574 msg->l2h = msgb_put(msg, len);
575 memcpy(msg->l2h, strbuf, len);
576
577ret:
578 talloc_free(strbuf);
579 return msg;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200580}
Harald Welte39c9e7b2014-08-22 00:28:51 +0200581
Harald Welteb4186822018-05-26 17:25:11 +0200582/*! Build a deferred control command state and keep it the per-connection list of deferred commands.
583 * This function is typically called by a ctrl command handler that wishes to defer returning a
584 * response. The reutnred state can later be used to check if the deferred command is still alive,
585 * and to respond to the specific command. This only works to defer the response to GET and SET.
586 * \param[in] ctx talloc context from whihc to allocate the ctrl_cmd_def
587 * \param[in] cmd the control command whose response is deferred
588 * \param[in] data opaque, user-defined pointer
589 * \param[in] secs number of seconds until the command times out
590 * \returns callee-allocated ctrl_cmd_def */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200591struct ctrl_cmd_def *
592ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs)
593{
594 struct ctrl_cmd_def *cd;
595
596 if (!cmd->ccon)
597 return NULL;
598
599 cd = talloc_zero(ctx, struct ctrl_cmd_def);
600
Neels Hofmeyrcdbc9af2018-04-03 16:51:49 +0200601 cmd->defer = cd;
Harald Welte39c9e7b2014-08-22 00:28:51 +0200602 cd->cmd = cmd;
603 cd->data = data;
604
605 /* add to per-connection list of deferred commands */
606 llist_add(&cd->list, &cmd->ccon->def_cmds);
607
608 return cd;
609}
610
Harald Welteb4186822018-05-26 17:25:11 +0200611/*! Determine if the given deferred control command is still alive or a zombie.
612 * \param[in] cd deferred ctrl command state
613 * \returns 0 is \a cd is still alive; 1 if it's a zombie */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200614int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd)
615{
616 /* luckily we're still alive */
617 if (cd->cmd)
618 return 0;
619
620 /* if we are a zombie, make sure we really die */
621 llist_del(&cd->list);
622 talloc_free(cd);
623
624 return 1;
625}
626
Harald Welteb4186822018-05-26 17:25:11 +0200627/*! Send the response to a deferred ctrl command.
628 * The command can only be a resply to a SET or a GET operation.
629 * \param[in] cd deferred ctrl command state
630 * \returns 0 if command sent successfully; negative on error */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200631int ctrl_cmd_def_send(struct ctrl_cmd_def *cd)
632{
633 struct ctrl_cmd *cmd = cd->cmd;
634
635 int rc;
636
637 /* Deferred commands can only be responses to GET/SET or ERROR, but
638 * never TRAP or anything else */
639 switch (cmd->type) {
640 case CTRL_TYPE_GET:
641 cmd->type = CTRL_TYPE_GET_REPLY;
642 break;
643 case CTRL_TYPE_SET:
644 cmd->type = CTRL_TYPE_SET_REPLY;
645 break;
646 default:
647 cmd->type = CTRL_TYPE_ERROR;
648 }
649
650 rc = ctrl_cmd_send(&cmd->ccon->write_queue, cmd);
651
652 talloc_free(cmd);
653 llist_del(&cd->list);
654 talloc_free(cd);
655
656 return rc;
657}