diff options
Diffstat (limited to 'src/ctrl/control_cmd.c')
-rw-r--r-- | src/ctrl/control_cmd.c | 106 |
1 files changed, 41 insertions, 65 deletions
diff --git a/src/ctrl/control_cmd.c b/src/ctrl/control_cmd.c index 33496bd8..db205510 100644 --- a/src/ctrl/control_cmd.c +++ b/src/ctrl/control_cmd.c @@ -20,10 +20,6 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * */ #include <ctype.h> @@ -35,6 +31,7 @@ #include <unistd.h> #include <osmocom/ctrl/control_cmd.h> +#include <osmocom/ctrl/control_if.h> #include <osmocom/core/msgb.h> #include <osmocom/core/talloc.h> @@ -207,13 +204,23 @@ failure: } /*! Install a given command definition at a given CTRL node. - * \param[in] node CTRL node at whihc \a cmd is to be installed + * \param[in] node CTRL node at which \a cmd is to be installed * \param[in] cmd command definition to be installed * \returns 0 on success; negative on error */ int ctrl_cmd_install(enum ctrl_node_type node, struct ctrl_cmd_element *cmd) { vector cmds_vec; + /* If this assert triggers, it means the program forgot to initialize + * the CTRL interface first by calling ctrl_handle_alloc(2)() directly + * or indirectly through ctrl_interface_setup_dynip(2)() + */ + if (!ctrl_node_vec) { + LOGP(DLCTRL, LOGL_ERROR, + "ctrl_handle must be initialized prior to installing cmds.\n"); + return -ENODEV; + } + cmds_vec = vector_lookup_ensure(ctrl_node_vec, node); if (!cmds_vec) { @@ -516,92 +523,61 @@ err: * \returns callee-allocated message buffer containing the encoded \a cmd; NULL on error */ struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd) { - struct msgb *msg; + struct msgb *msg = NULL; + char *strbuf; + size_t len; const char *type; - char *tmp; if (!cmd->id) return NULL; - msg = msgb_alloc_headroom(4096, 128, "ctrl command make"); - if (!msg) - return NULL; - type = get_value_string(ctrl_type_vals, cmd->type); switch (cmd->type) { case CTRL_TYPE_GET: if (!cmd->variable) - goto err; - - tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable); - if (!tmp) { - LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n"); - goto err; - } - - msg->l2h = msgb_put(msg, strlen(tmp)); - memcpy(msg->l2h, tmp, strlen(tmp)); - talloc_free(tmp); + return NULL; + strbuf = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable); break; case CTRL_TYPE_SET: if (!cmd->variable || !cmd->value) - goto err; - - tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable, - cmd->value); - if (!tmp) { - LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n"); - goto err; - } - - msg->l2h = msgb_put(msg, strlen(tmp)); - memcpy(msg->l2h, tmp, strlen(tmp)); - talloc_free(tmp); + return NULL; + strbuf = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, + cmd->variable, cmd->value); break; case CTRL_TYPE_GET_REPLY: case CTRL_TYPE_SET_REPLY: case CTRL_TYPE_TRAP: if (!cmd->variable || !cmd->reply) - goto err; - - tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable, - cmd->reply); - if (!tmp) { - LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n"); - goto err; - } - - msg->l2h = msgb_put(msg, strlen(tmp)); - memcpy(msg->l2h, tmp, strlen(tmp)); - talloc_free(tmp); + return NULL; + strbuf = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, + cmd->variable, cmd->reply); break; case CTRL_TYPE_ERROR: if (!cmd->reply) - goto err; - - tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, - cmd->reply); - if (!tmp) { - LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n"); - goto err; - } - - msg->l2h = msgb_put(msg, strlen(tmp)); - memcpy(msg->l2h, tmp, strlen(tmp)); - talloc_free(tmp); + return NULL; + strbuf = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->reply); break; default: LOGP(DLCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type); - goto err; - break; + return NULL; } - return msg; + if (!strbuf) { + LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n"); + goto ret; + } + len = strlen(strbuf); -err: - msgb_free(msg); - return NULL; + msg = msgb_alloc_headroom(len + 128, 128, "ctrl ERROR command make"); + if (!msg) + goto ret; + msg->l2h = msgb_put(msg, len); + memcpy(msg->l2h, strbuf, len); + +ret: + talloc_free(strbuf); + return msg; } /*! Build a deferred control command state and keep it the per-connection list of deferred commands. @@ -672,7 +648,7 @@ int ctrl_cmd_def_send(struct ctrl_cmd_def *cd) cmd->type = CTRL_TYPE_ERROR; } - rc = ctrl_cmd_send(&cmd->ccon->write_queue, cmd); + rc = ctrl_cmd_send2(cmd->ccon, cmd); talloc_free(cmd); llist_del(&cd->list); |