blob: c5924b2be66ef1b90eb6fb5e927d9d2f64d6ca3c [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.
210 * \param[in] node CTRL node at whihc \a cmd is to be installed
211 * \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.
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100319 * The caller is responsible to talloc_free() the returned struct pointer. */
320struct ctrl_cmd *ctrl_cmd_parse2(void *ctx, struct msgb *msg)
321{
Daniel Willmann1264cb42010-10-21 15:00:36 +0200322 char *str, *tmp, *saveptr = NULL;
323 char *var, *val;
324 struct ctrl_cmd *cmd;
325
326 cmd = talloc_zero(ctx, struct ctrl_cmd);
327 if (!cmd) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200328 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200329 return NULL;
330 }
331
332 /* Make sure input is NULL terminated */
333 msgb_put_u8(msg, 0);
334 str = (char *) msg->l2h;
335
Harald Welteedf6fe72016-11-26 10:06:07 +0100336 OSMO_ASSERT(str);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200337 tmp = strtok_r(str, " ", &saveptr);
338 if (!tmp) {
339 cmd->type = CTRL_TYPE_ERROR;
340 cmd->id = "err";
341 cmd->reply = "Request malformed";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200342 LOGP(DLCTRL, LOGL_NOTICE, "Malformed request: \"%s\"\n", osmo_escape_str(str, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200343 goto err;
344 }
345
Max70c7d412017-02-24 13:59:14 +0100346 cmd->type = get_string_value(ctrl_type_vals, tmp);
Pau Espin Pedrol8aa85bd2017-06-23 12:43:31 +0200347 if ((int)cmd->type < 0 || cmd->type == CTRL_TYPE_UNKNOWN) {
Daniel Willmann1264cb42010-10-21 15:00:36 +0200348 cmd->type = CTRL_TYPE_ERROR;
349 cmd->id = "err";
350 cmd->reply = "Request type unknown";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200351 LOGP(DLCTRL, LOGL_NOTICE, "Request type unknown: \"%s\"\n", osmo_escape_str(str, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200352 goto err;
353 }
354
355 tmp = strtok_r(NULL, " ", &saveptr);
356
357 if (!tmp) {
358 cmd->type = CTRL_TYPE_ERROR;
359 cmd->id = "err";
360 cmd->reply = "Missing ID";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200361 LOGP(DLCTRL, LOGL_NOTICE, "Missing ID: \"%s\"\n", osmo_escape_str(str, -1));
362 goto err;
363 }
364
365 if (!id_str_valid(tmp)) {
Pau Espin Pedrol55088b72018-07-12 13:03:52 +0200366 LOGP(DLCTRL, LOGL_NOTICE, "Invalid %s message ID number: \"%s\"\n",
367 get_value_string(ctrl_type_vals, cmd->type), osmo_escape_str(tmp, -1));
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200368 cmd->type = CTRL_TYPE_ERROR;
369 cmd->id = "err";
370 cmd->reply = "Invalid message ID number";
Daniel Willmann1264cb42010-10-21 15:00:36 +0200371 goto err;
372 }
373 cmd->id = talloc_strdup(cmd, tmp);
374 if (!cmd->id)
375 goto oom;
376
377 switch (cmd->type) {
378 case CTRL_TYPE_GET:
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200379 var = strtok_r(NULL, " \n", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200380 if (!var) {
381 cmd->type = CTRL_TYPE_ERROR;
382 cmd->reply = "GET incomplete";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200383 LOGP(DLCTRL, LOGL_NOTICE, "GET Command incomplete: \"%s\"\n",
384 osmo_escape_str(str, -1));
385 goto err;
386 }
387 if (!osmo_separated_identifiers_valid(var, ".")) {
388 cmd->type = CTRL_TYPE_ERROR;
389 cmd->reply = "GET variable contains invalid characters";
390 LOGP(DLCTRL, LOGL_NOTICE, "GET variable contains invalid characters: \"%s\"\n",
391 osmo_escape_str(var, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200392 goto err;
393 }
394 cmd->variable = talloc_strdup(cmd, var);
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200395 var = strtok_r(NULL, "", &saveptr);
396 if (var) {
397 cmd->type = CTRL_TYPE_ERROR;
398 cmd->reply = "GET with trailing characters";
399 LOGP(DLCTRL, LOGL_NOTICE, "GET with trailing characters: \"%s\"\n",
400 osmo_escape_str(var, -1));
401 goto err;
402 }
Harald Welte7fd0c832014-08-20 19:58:13 +0200403 LOGP(DLCTRL, LOGL_DEBUG, "Command: GET %s\n", cmd->variable);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200404 break;
405 case CTRL_TYPE_SET:
406 var = strtok_r(NULL, " ", &saveptr);
Holger Hans Peter Freyther5ad742d2014-08-08 19:41:28 +0200407 val = strtok_r(NULL, "\n", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200408 if (!var || !val) {
409 cmd->type = CTRL_TYPE_ERROR;
410 cmd->reply = "SET incomplete";
Harald Welte7fd0c832014-08-20 19:58:13 +0200411 LOGP(DLCTRL, LOGL_NOTICE, "SET Command incomplete\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200412 goto err;
413 }
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200414 if (!osmo_separated_identifiers_valid(var, ".")) {
415 cmd->type = CTRL_TYPE_ERROR;
416 cmd->reply = "SET variable contains invalid characters";
417 LOGP(DLCTRL, LOGL_NOTICE, "SET variable contains invalid characters: \"%s\"\n",
418 osmo_escape_str(var, -1));
419 goto err;
420 }
Daniel Willmann1264cb42010-10-21 15:00:36 +0200421 cmd->variable = talloc_strdup(cmd, var);
422 cmd->value = talloc_strdup(cmd, val);
423 if (!cmd->variable || !cmd->value)
424 goto oom;
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200425
426 var = strtok_r(NULL, "", &saveptr);
427 if (var) {
428 cmd->type = CTRL_TYPE_ERROR;
429 cmd->reply = "SET with trailing characters";
430 LOGP(DLCTRL, LOGL_NOTICE, "SET with trailing characters: \"%s\"\n",
431 osmo_escape_str(var, -1));
432 goto err;
433 }
434
435 LOGP(DLCTRL, LOGL_DEBUG, "Command: SET %s = \"%s\"\n", cmd->variable,
436 osmo_escape_str(cmd->value, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200437 break;
Neels Hofmeyra5e21622017-12-16 01:18:53 +0100438#define REPLY_CASE(TYPE, NAME) \
439 case TYPE: \
440 var = strtok_r(NULL, " ", &saveptr); \
441 val = strtok_r(NULL, "", &saveptr); \
442 if (!var) { \
443 cmd->type = CTRL_TYPE_ERROR; \
444 cmd->reply = NAME " incomplete"; \
445 LOGP(DLCTRL, LOGL_NOTICE, NAME " incomplete\n"); \
446 goto err; \
447 } \
448 if (!osmo_separated_identifiers_valid(var, ".")) { \
449 cmd->type = CTRL_TYPE_ERROR; \
450 cmd->reply = NAME " variable contains invalid characters"; \
451 LOGP(DLCTRL, LOGL_NOTICE, NAME " variable contains invalid characters: \"%s\"\n", \
452 osmo_escape_str(var, -1)); \
453 goto err; \
454 } \
455 cmd->variable = talloc_strdup(cmd, var); \
456 cmd->reply = talloc_strdup(cmd, val); \
457 if (!cmd->variable || !cmd->reply) \
458 goto oom; \
459 LOGP(DLCTRL, LOGL_DEBUG, "Command: " NAME " %s: %s\n", cmd->variable, \
460 osmo_escape_str(cmd->reply, -1)); \
461 break
462 REPLY_CASE(CTRL_TYPE_GET_REPLY, "GET REPLY");
463 REPLY_CASE(CTRL_TYPE_SET_REPLY, "SET REPLY");
464 REPLY_CASE(CTRL_TYPE_TRAP, "TRAP");
465#undef REPLY_CASE
Daniel Willmann1264cb42010-10-21 15:00:36 +0200466 case CTRL_TYPE_ERROR:
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200467 var = strtok_r(NULL, "", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200468 if (!var) {
469 cmd->reply = "";
470 goto err;
471 }
472 cmd->reply = talloc_strdup(cmd, var);
473 if (!cmd->reply)
474 goto oom;
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200475 LOGP(DLCTRL, LOGL_DEBUG, "Command: ERROR \"%s\"\n",
476 osmo_escape_str(cmd->reply, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200477 break;
478 case CTRL_TYPE_UNKNOWN:
479 default:
480 cmd->type = CTRL_TYPE_ERROR;
481 cmd->reply = "Unknown type";
482 goto err;
483 }
484
485 return cmd;
486oom:
487 cmd->type = CTRL_TYPE_ERROR;
488 cmd->id = "err";
489 cmd->reply = "OOM";
490err:
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100491 return cmd;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200492}
493
Harald Welteb4186822018-05-26 17:25:11 +0200494/*! Encode a given CTRL command from its parsed form into a message buffer.
495 * \param[in] cmd decoded/parsed form of to-be-encoded command
496 * \returns callee-allocated message buffer containing the encoded \a cmd; NULL on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200497struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
498{
499 struct msgb *msg;
Max70c7d412017-02-24 13:59:14 +0100500 const char *type;
501 char *tmp;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200502
503 if (!cmd->id)
504 return NULL;
505
506 msg = msgb_alloc_headroom(4096, 128, "ctrl command make");
507 if (!msg)
508 return NULL;
509
Max70c7d412017-02-24 13:59:14 +0100510 type = get_value_string(ctrl_type_vals, cmd->type);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200511
512 switch (cmd->type) {
513 case CTRL_TYPE_GET:
514 if (!cmd->variable)
515 goto err;
516
517 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable);
518 if (!tmp) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200519 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200520 goto err;
521 }
522
523 msg->l2h = msgb_put(msg, strlen(tmp));
524 memcpy(msg->l2h, tmp, strlen(tmp));
525 talloc_free(tmp);
526 break;
527 case CTRL_TYPE_SET:
528 if (!cmd->variable || !cmd->value)
529 goto err;
530
531 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
532 cmd->value);
533 if (!tmp) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200534 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200535 goto err;
536 }
537
538 msg->l2h = msgb_put(msg, strlen(tmp));
539 memcpy(msg->l2h, tmp, strlen(tmp));
540 talloc_free(tmp);
541 break;
542 case CTRL_TYPE_GET_REPLY:
543 case CTRL_TYPE_SET_REPLY:
544 case CTRL_TYPE_TRAP:
545 if (!cmd->variable || !cmd->reply)
546 goto err;
547
548 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
549 cmd->reply);
550 if (!tmp) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200551 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200552 goto err;
553 }
554
555 msg->l2h = msgb_put(msg, strlen(tmp));
556 memcpy(msg->l2h, tmp, strlen(tmp));
557 talloc_free(tmp);
558 break;
559 case CTRL_TYPE_ERROR:
560 if (!cmd->reply)
561 goto err;
562
563 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id,
564 cmd->reply);
565 if (!tmp) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200566 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200567 goto err;
568 }
569
570 msg->l2h = msgb_put(msg, strlen(tmp));
571 memcpy(msg->l2h, tmp, strlen(tmp));
572 talloc_free(tmp);
573 break;
574 default:
Harald Welte7fd0c832014-08-20 19:58:13 +0200575 LOGP(DLCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200576 goto err;
577 break;
578 }
579
580 return msg;
581
582err:
583 msgb_free(msg);
584 return NULL;
585}
Harald Welte39c9e7b2014-08-22 00:28:51 +0200586
Harald Welteb4186822018-05-26 17:25:11 +0200587/*! Build a deferred control command state and keep it the per-connection list of deferred commands.
588 * This function is typically called by a ctrl command handler that wishes to defer returning a
589 * response. The reutnred state can later be used to check if the deferred command is still alive,
590 * and to respond to the specific command. This only works to defer the response to GET and SET.
591 * \param[in] ctx talloc context from whihc to allocate the ctrl_cmd_def
592 * \param[in] cmd the control command whose response is deferred
593 * \param[in] data opaque, user-defined pointer
594 * \param[in] secs number of seconds until the command times out
595 * \returns callee-allocated ctrl_cmd_def */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200596struct ctrl_cmd_def *
597ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs)
598{
599 struct ctrl_cmd_def *cd;
600
601 if (!cmd->ccon)
602 return NULL;
603
604 cd = talloc_zero(ctx, struct ctrl_cmd_def);
605
Neels Hofmeyrcdbc9af2018-04-03 16:51:49 +0200606 cmd->defer = cd;
Harald Welte39c9e7b2014-08-22 00:28:51 +0200607 cd->cmd = cmd;
608 cd->data = data;
609
610 /* add to per-connection list of deferred commands */
611 llist_add(&cd->list, &cmd->ccon->def_cmds);
612
613 return cd;
614}
615
Harald Welteb4186822018-05-26 17:25:11 +0200616/*! Determine if the given deferred control command is still alive or a zombie.
617 * \param[in] cd deferred ctrl command state
618 * \returns 0 is \a cd is still alive; 1 if it's a zombie */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200619int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd)
620{
621 /* luckily we're still alive */
622 if (cd->cmd)
623 return 0;
624
625 /* if we are a zombie, make sure we really die */
626 llist_del(&cd->list);
627 talloc_free(cd);
628
629 return 1;
630}
631
Harald Welteb4186822018-05-26 17:25:11 +0200632/*! Send the response to a deferred ctrl command.
633 * The command can only be a resply to a SET or a GET operation.
634 * \param[in] cd deferred ctrl command state
635 * \returns 0 if command sent successfully; negative on error */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200636int ctrl_cmd_def_send(struct ctrl_cmd_def *cd)
637{
638 struct ctrl_cmd *cmd = cd->cmd;
639
640 int rc;
641
642 /* Deferred commands can only be responses to GET/SET or ERROR, but
643 * never TRAP or anything else */
644 switch (cmd->type) {
645 case CTRL_TYPE_GET:
646 cmd->type = CTRL_TYPE_GET_REPLY;
647 break;
648 case CTRL_TYPE_SET:
649 cmd->type = CTRL_TYPE_SET_REPLY;
650 break;
651 default:
652 cmd->type = CTRL_TYPE_ERROR;
653 }
654
655 rc = ctrl_cmd_send(&cmd->ccon->write_queue, cmd);
656
657 talloc_free(cmd);
658 llist_del(&cd->list);
659 talloc_free(cd);
660
661 return rc;
662}