blob: 14ff906575bda1e05e865369c423bae8b11c2a40 [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
Pau Espin Pedrol40ad9132018-07-12 17:41:55 +0200365 if (!id_str_valid(tmp) &&
366 !(cmd->type == CTRL_TYPE_ERROR && strcmp(tmp, "err") == 0)) {
Pau Espin Pedrol55088b72018-07-12 13:03:52 +0200367 LOGP(DLCTRL, LOGL_NOTICE, "Invalid %s message ID number: \"%s\"\n",
368 get_value_string(ctrl_type_vals, cmd->type), osmo_escape_str(tmp, -1));
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200369 cmd->type = CTRL_TYPE_ERROR;
370 cmd->id = "err";
371 cmd->reply = "Invalid message ID number";
Daniel Willmann1264cb42010-10-21 15:00:36 +0200372 goto err;
373 }
374 cmd->id = talloc_strdup(cmd, tmp);
375 if (!cmd->id)
376 goto oom;
377
378 switch (cmd->type) {
379 case CTRL_TYPE_GET:
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200380 var = strtok_r(NULL, " \n", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200381 if (!var) {
382 cmd->type = CTRL_TYPE_ERROR;
383 cmd->reply = "GET incomplete";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200384 LOGP(DLCTRL, LOGL_NOTICE, "GET Command incomplete: \"%s\"\n",
385 osmo_escape_str(str, -1));
386 goto err;
387 }
388 if (!osmo_separated_identifiers_valid(var, ".")) {
389 cmd->type = CTRL_TYPE_ERROR;
390 cmd->reply = "GET variable contains invalid characters";
391 LOGP(DLCTRL, LOGL_NOTICE, "GET variable contains invalid characters: \"%s\"\n",
392 osmo_escape_str(var, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200393 goto err;
394 }
395 cmd->variable = talloc_strdup(cmd, var);
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200396 var = strtok_r(NULL, "", &saveptr);
397 if (var) {
398 cmd->type = CTRL_TYPE_ERROR;
399 cmd->reply = "GET with trailing characters";
400 LOGP(DLCTRL, LOGL_NOTICE, "GET with trailing characters: \"%s\"\n",
401 osmo_escape_str(var, -1));
402 goto err;
403 }
Harald Welte7fd0c832014-08-20 19:58:13 +0200404 LOGP(DLCTRL, LOGL_DEBUG, "Command: GET %s\n", cmd->variable);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200405 break;
406 case CTRL_TYPE_SET:
407 var = strtok_r(NULL, " ", &saveptr);
Holger Hans Peter Freyther5ad742d2014-08-08 19:41:28 +0200408 val = strtok_r(NULL, "\n", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200409 if (!var || !val) {
410 cmd->type = CTRL_TYPE_ERROR;
411 cmd->reply = "SET incomplete";
Harald Welte7fd0c832014-08-20 19:58:13 +0200412 LOGP(DLCTRL, LOGL_NOTICE, "SET Command incomplete\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200413 goto err;
414 }
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200415 if (!osmo_separated_identifiers_valid(var, ".")) {
416 cmd->type = CTRL_TYPE_ERROR;
417 cmd->reply = "SET variable contains invalid characters";
418 LOGP(DLCTRL, LOGL_NOTICE, "SET variable contains invalid characters: \"%s\"\n",
419 osmo_escape_str(var, -1));
420 goto err;
421 }
Daniel Willmann1264cb42010-10-21 15:00:36 +0200422 cmd->variable = talloc_strdup(cmd, var);
423 cmd->value = talloc_strdup(cmd, val);
424 if (!cmd->variable || !cmd->value)
425 goto oom;
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200426
427 var = strtok_r(NULL, "", &saveptr);
428 if (var) {
429 cmd->type = CTRL_TYPE_ERROR;
430 cmd->reply = "SET with trailing characters";
431 LOGP(DLCTRL, LOGL_NOTICE, "SET with trailing characters: \"%s\"\n",
432 osmo_escape_str(var, -1));
433 goto err;
434 }
435
436 LOGP(DLCTRL, LOGL_DEBUG, "Command: SET %s = \"%s\"\n", cmd->variable,
437 osmo_escape_str(cmd->value, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200438 break;
Neels Hofmeyra5e21622017-12-16 01:18:53 +0100439#define REPLY_CASE(TYPE, NAME) \
440 case TYPE: \
441 var = strtok_r(NULL, " ", &saveptr); \
442 val = strtok_r(NULL, "", &saveptr); \
443 if (!var) { \
444 cmd->type = CTRL_TYPE_ERROR; \
445 cmd->reply = NAME " incomplete"; \
446 LOGP(DLCTRL, LOGL_NOTICE, NAME " incomplete\n"); \
447 goto err; \
448 } \
449 if (!osmo_separated_identifiers_valid(var, ".")) { \
450 cmd->type = CTRL_TYPE_ERROR; \
451 cmd->reply = NAME " variable contains invalid characters"; \
452 LOGP(DLCTRL, LOGL_NOTICE, NAME " variable contains invalid characters: \"%s\"\n", \
453 osmo_escape_str(var, -1)); \
454 goto err; \
455 } \
456 cmd->variable = talloc_strdup(cmd, var); \
457 cmd->reply = talloc_strdup(cmd, val); \
458 if (!cmd->variable || !cmd->reply) \
459 goto oom; \
460 LOGP(DLCTRL, LOGL_DEBUG, "Command: " NAME " %s: %s\n", cmd->variable, \
461 osmo_escape_str(cmd->reply, -1)); \
462 break
463 REPLY_CASE(CTRL_TYPE_GET_REPLY, "GET REPLY");
464 REPLY_CASE(CTRL_TYPE_SET_REPLY, "SET REPLY");
465 REPLY_CASE(CTRL_TYPE_TRAP, "TRAP");
466#undef REPLY_CASE
Daniel Willmann1264cb42010-10-21 15:00:36 +0200467 case CTRL_TYPE_ERROR:
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200468 var = strtok_r(NULL, "", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200469 if (!var) {
470 cmd->reply = "";
471 goto err;
472 }
473 cmd->reply = talloc_strdup(cmd, var);
474 if (!cmd->reply)
475 goto oom;
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200476 LOGP(DLCTRL, LOGL_DEBUG, "Command: ERROR \"%s\"\n",
477 osmo_escape_str(cmd->reply, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200478 break;
479 case CTRL_TYPE_UNKNOWN:
480 default:
481 cmd->type = CTRL_TYPE_ERROR;
482 cmd->reply = "Unknown type";
483 goto err;
484 }
485
486 return cmd;
487oom:
488 cmd->type = CTRL_TYPE_ERROR;
489 cmd->id = "err";
490 cmd->reply = "OOM";
491err:
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100492 return cmd;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200493}
494
Harald Welteb4186822018-05-26 17:25:11 +0200495/*! Encode a given CTRL command from its parsed form into a message buffer.
496 * \param[in] cmd decoded/parsed form of to-be-encoded command
497 * \returns callee-allocated message buffer containing the encoded \a cmd; NULL on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200498struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
499{
500 struct msgb *msg;
Max70c7d412017-02-24 13:59:14 +0100501 const char *type;
502 char *tmp;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200503
504 if (!cmd->id)
505 return NULL;
506
507 msg = msgb_alloc_headroom(4096, 128, "ctrl command make");
508 if (!msg)
509 return NULL;
510
Max70c7d412017-02-24 13:59:14 +0100511 type = get_value_string(ctrl_type_vals, cmd->type);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200512
513 switch (cmd->type) {
514 case CTRL_TYPE_GET:
515 if (!cmd->variable)
516 goto err;
517
518 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable);
519 if (!tmp) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200520 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200521 goto err;
522 }
523
524 msg->l2h = msgb_put(msg, strlen(tmp));
525 memcpy(msg->l2h, tmp, strlen(tmp));
526 talloc_free(tmp);
527 break;
528 case CTRL_TYPE_SET:
529 if (!cmd->variable || !cmd->value)
530 goto err;
531
532 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
533 cmd->value);
534 if (!tmp) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200535 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200536 goto err;
537 }
538
539 msg->l2h = msgb_put(msg, strlen(tmp));
540 memcpy(msg->l2h, tmp, strlen(tmp));
541 talloc_free(tmp);
542 break;
543 case CTRL_TYPE_GET_REPLY:
544 case CTRL_TYPE_SET_REPLY:
545 case CTRL_TYPE_TRAP:
546 if (!cmd->variable || !cmd->reply)
547 goto err;
548
549 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
550 cmd->reply);
551 if (!tmp) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200552 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200553 goto err;
554 }
555
556 msg->l2h = msgb_put(msg, strlen(tmp));
557 memcpy(msg->l2h, tmp, strlen(tmp));
558 talloc_free(tmp);
559 break;
560 case CTRL_TYPE_ERROR:
561 if (!cmd->reply)
562 goto err;
563
564 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id,
565 cmd->reply);
566 if (!tmp) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200567 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200568 goto err;
569 }
570
571 msg->l2h = msgb_put(msg, strlen(tmp));
572 memcpy(msg->l2h, tmp, strlen(tmp));
573 talloc_free(tmp);
574 break;
575 default:
Harald Welte7fd0c832014-08-20 19:58:13 +0200576 LOGP(DLCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200577 goto err;
578 break;
579 }
580
581 return msg;
582
583err:
584 msgb_free(msg);
585 return NULL;
586}
Harald Welte39c9e7b2014-08-22 00:28:51 +0200587
Harald Welteb4186822018-05-26 17:25:11 +0200588/*! Build a deferred control command state and keep it the per-connection list of deferred commands.
589 * This function is typically called by a ctrl command handler that wishes to defer returning a
590 * response. The reutnred state can later be used to check if the deferred command is still alive,
591 * and to respond to the specific command. This only works to defer the response to GET and SET.
592 * \param[in] ctx talloc context from whihc to allocate the ctrl_cmd_def
593 * \param[in] cmd the control command whose response is deferred
594 * \param[in] data opaque, user-defined pointer
595 * \param[in] secs number of seconds until the command times out
596 * \returns callee-allocated ctrl_cmd_def */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200597struct ctrl_cmd_def *
598ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs)
599{
600 struct ctrl_cmd_def *cd;
601
602 if (!cmd->ccon)
603 return NULL;
604
605 cd = talloc_zero(ctx, struct ctrl_cmd_def);
606
Neels Hofmeyrcdbc9af2018-04-03 16:51:49 +0200607 cmd->defer = cd;
Harald Welte39c9e7b2014-08-22 00:28:51 +0200608 cd->cmd = cmd;
609 cd->data = data;
610
611 /* add to per-connection list of deferred commands */
612 llist_add(&cd->list, &cmd->ccon->def_cmds);
613
614 return cd;
615}
616
Harald Welteb4186822018-05-26 17:25:11 +0200617/*! Determine if the given deferred control command is still alive or a zombie.
618 * \param[in] cd deferred ctrl command state
619 * \returns 0 is \a cd is still alive; 1 if it's a zombie */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200620int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd)
621{
622 /* luckily we're still alive */
623 if (cd->cmd)
624 return 0;
625
626 /* if we are a zombie, make sure we really die */
627 llist_del(&cd->list);
628 talloc_free(cd);
629
630 return 1;
631}
632
Harald Welteb4186822018-05-26 17:25:11 +0200633/*! Send the response to a deferred ctrl command.
634 * The command can only be a resply to a SET or a GET operation.
635 * \param[in] cd deferred ctrl command state
636 * \returns 0 if command sent successfully; negative on error */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200637int ctrl_cmd_def_send(struct ctrl_cmd_def *cd)
638{
639 struct ctrl_cmd *cmd = cd->cmd;
640
641 int rc;
642
643 /* Deferred commands can only be responses to GET/SET or ERROR, but
644 * never TRAP or anything else */
645 switch (cmd->type) {
646 case CTRL_TYPE_GET:
647 cmd->type = CTRL_TYPE_GET_REPLY;
648 break;
649 case CTRL_TYPE_SET:
650 cmd->type = CTRL_TYPE_SET_REPLY;
651 break;
652 default:
653 cmd->type = CTRL_TYPE_ERROR;
654 }
655
656 rc = ctrl_cmd_send(&cmd->ccon->write_queue, cmd);
657
658 talloc_free(cmd);
659 llist_del(&cd->list);
660 talloc_free(cd);
661
662 return rc;
663}