2017-06-20 02:35:06 +00:00
|
|
|
/*! \file control_if.c
|
|
|
|
* SNMP-like status interface. */
|
|
|
|
/*
|
2010-10-21 13:00:36 +00:00
|
|
|
* (C) 2010-2011 by Daniel Willmann <daniel@totalueberwachung.de>
|
|
|
|
* (C) 2010-2011 by On-Waves
|
|
|
|
*
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-03-18 20:52:38 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2010-10-21 13:00:36 +00:00
|
|
|
#include <errno.h>
|
2011-07-19 10:12:10 +00:00
|
|
|
#include <inttypes.h>
|
2010-10-21 13:00:36 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2014-08-21 14:31:52 +00:00
|
|
|
#include <netinet/in.h>
|
2014-12-04 13:15:36 +00:00
|
|
|
#ifdef HAVE_NETINET_TCP_H
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#endif
|
2010-10-21 13:00:36 +00:00
|
|
|
|
2014-12-04 13:15:36 +00:00
|
|
|
#include <fcntl.h>
|
2010-10-21 13:00:36 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2014-08-20 17:50:04 +00:00
|
|
|
#include <osmocom/ctrl/control_cmd.h>
|
|
|
|
#include <osmocom/ctrl/control_if.h>
|
2010-10-21 13:00:36 +00:00
|
|
|
|
|
|
|
#include <osmocom/core/msgb.h>
|
2011-04-21 09:43:55 +00:00
|
|
|
#include <osmocom/core/rate_ctr.h>
|
2010-10-21 13:00:36 +00:00
|
|
|
#include <osmocom/core/select.h>
|
2017-10-15 17:46:19 +00:00
|
|
|
#include <osmocom/core/counter.h>
|
2010-10-21 13:00:36 +00:00
|
|
|
#include <osmocom/core/talloc.h>
|
2014-08-20 17:50:04 +00:00
|
|
|
#include <osmocom/core/socket.h>
|
2010-10-21 13:00:36 +00:00
|
|
|
|
2014-08-20 17:50:04 +00:00
|
|
|
#include <osmocom/gsm/protocol/ipaccess.h>
|
2014-08-20 20:51:25 +00:00
|
|
|
#include <osmocom/gsm/ipa.h>
|
2010-10-21 13:00:36 +00:00
|
|
|
|
|
|
|
#include <osmocom/vty/command.h>
|
|
|
|
#include <osmocom/vty/vector.h>
|
|
|
|
|
2017-04-16 17:08:49 +00:00
|
|
|
extern int osmo_fsm_ctrl_cmds_install(void);
|
|
|
|
|
2010-10-21 13:00:36 +00:00
|
|
|
vector ctrl_node_vec;
|
|
|
|
|
2017-04-16 14:57:40 +00:00
|
|
|
/* global list of control interface lookup helpers */
|
|
|
|
struct lookup_helper {
|
|
|
|
struct llist_head list;
|
|
|
|
ctrl_cmd_lookup lookup;
|
|
|
|
};
|
|
|
|
static LLIST_HEAD(ctrl_lookup_helpers);
|
|
|
|
|
2014-08-21 13:13:08 +00:00
|
|
|
int ctrl_parse_get_num(vector vline, int i, long *num)
|
|
|
|
{
|
|
|
|
char *token, *tmp;
|
|
|
|
|
|
|
|
if (i >= vector_active(vline))
|
|
|
|
return 0;
|
|
|
|
token = vector_slot(vline, i);
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if (token[0] == '\0')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*num = strtol(token, &tmp, 10);
|
|
|
|
if (tmp[0] != '\0' || errno != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-05-06 14:38:11 +00:00
|
|
|
/* Send command to all */
|
|
|
|
int ctrl_cmd_send_to_all(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd)
|
|
|
|
{
|
|
|
|
struct ctrl_connection *ccon;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
llist_for_each_entry(ccon, &ctrl->ccon_list, list_entry) {
|
|
|
|
if (ccon == cmd->ccon)
|
|
|
|
continue;
|
|
|
|
if (ctrl_cmd_send(&ccon->write_queue, cmd))
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-10-21 13:00:36 +00:00
|
|
|
int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct msgb *msg;
|
|
|
|
|
|
|
|
msg = ctrl_cmd_make(cmd);
|
|
|
|
if (!msg) {
|
2014-08-20 17:58:13 +00:00
|
|
|
LOGP(DLCTRL, LOGL_ERROR, "Could not generate msg\n");
|
2010-10-21 13:00:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-08-20 20:51:25 +00:00
|
|
|
ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_CTRL);
|
|
|
|
ipa_prepend_header(msg, IPAC_PROTO_OSMO);
|
2010-10-21 13:00:36 +00:00
|
|
|
|
|
|
|
ret = osmo_wqueue_enqueue(queue, msg);
|
|
|
|
if (ret != 0) {
|
2014-08-20 17:58:13 +00:00
|
|
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to enqueue the command.\n");
|
2010-10-21 13:00:36 +00:00
|
|
|
msgb_free(msg);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Send TRAP over given Control Interface
|
2016-08-04 09:07:08 +00:00
|
|
|
* \param[in] ctrl Control Interface over which TRAP will be sent
|
|
|
|
* \param[in] name Name of the TRAP variable
|
|
|
|
* \param[in] value Value of the TRAP variable
|
|
|
|
* \return Negative value on error, result of ctrl_cmd_send_to_all() otherwise
|
|
|
|
*/
|
|
|
|
int ctrl_cmd_send_trap(struct ctrl_handle *ctrl, const char *name, char *value)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
struct ctrl_cmd *cmd = ctrl_cmd_create(NULL, CTRL_TYPE_TRAP);
|
|
|
|
if (!cmd)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
cmd->id = "0"; /* It's a TRAP! */
|
2017-02-08 15:46:53 +00:00
|
|
|
cmd->variable = (char *) name;
|
2016-08-04 09:07:08 +00:00
|
|
|
cmd->reply = value;
|
|
|
|
r = ctrl_cmd_send_to_all(ctrl, cmd);
|
|
|
|
talloc_free(cmd);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-07-22 16:08:31 +00:00
|
|
|
struct ctrl_cmd *ctrl_cmd_trap(struct ctrl_cmd *cmd)
|
|
|
|
{
|
|
|
|
struct ctrl_cmd *trap;
|
|
|
|
|
2014-08-20 17:55:17 +00:00
|
|
|
trap = ctrl_cmd_cpy(cmd, cmd);
|
2011-07-22 16:08:31 +00:00
|
|
|
if (!trap)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
trap->ccon = cmd->ccon;
|
|
|
|
trap->type = CTRL_TYPE_TRAP;
|
|
|
|
return trap;
|
|
|
|
}
|
|
|
|
|
2010-10-21 13:00:36 +00:00
|
|
|
static void control_close_conn(struct ctrl_connection *ccon)
|
|
|
|
{
|
2014-08-21 22:28:51 +00:00
|
|
|
struct ctrl_cmd_def *cd, *cd2;
|
|
|
|
|
2013-03-16 09:13:04 +00:00
|
|
|
osmo_wqueue_clear(&ccon->write_queue);
|
2010-10-21 13:00:36 +00:00
|
|
|
close(ccon->write_queue.bfd.fd);
|
|
|
|
osmo_fd_unregister(&ccon->write_queue.bfd);
|
2011-05-06 14:37:25 +00:00
|
|
|
llist_del(&ccon->list_entry);
|
2010-10-21 13:00:36 +00:00
|
|
|
if (ccon->closed_cb)
|
|
|
|
ccon->closed_cb(ccon);
|
2014-03-31 11:42:11 +00:00
|
|
|
msgb_free(ccon->pending_msg);
|
2014-08-21 22:28:51 +00:00
|
|
|
|
|
|
|
/* clean up deferred commands */
|
|
|
|
llist_for_each_entry_safe(cd, cd2, &ccon->def_cmds, list) {
|
|
|
|
/* delete from list of def_cmds for this ccon */
|
|
|
|
llist_del(&cd->list);
|
|
|
|
/* not strictly needed as this is a slave to the ccon which we
|
|
|
|
* are about to free anyway */
|
|
|
|
talloc_free(cd->cmd);
|
|
|
|
/* set the CMD to null, this is the indication to the user that
|
|
|
|
* the connection for this command has gone */
|
|
|
|
cd->cmd = NULL;
|
|
|
|
}
|
|
|
|
|
2010-10-21 13:00:36 +00:00
|
|
|
talloc_free(ccon);
|
|
|
|
}
|
|
|
|
|
2014-08-21 13:13:08 +00:00
|
|
|
int ctrl_cmd_handle(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
char *request;
|
|
|
|
int i, j, ret, node;
|
2017-05-02 14:24:12 +00:00
|
|
|
bool break_cycle = false;
|
2014-08-21 13:13:08 +00:00
|
|
|
vector vline, cmdvec, cmds_vec;
|
|
|
|
|
2017-02-24 13:48:45 +00:00
|
|
|
if (cmd->type == CTRL_TYPE_SET_REPLY ||
|
|
|
|
cmd->type == CTRL_TYPE_GET_REPLY) {
|
|
|
|
if (strncmp(cmd->reply, "OK", 2) == 0) {
|
|
|
|
LOGP(DLCTRL, LOGL_DEBUG, "%s <%s> for %s is OK\n",
|
|
|
|
get_value_string(ctrl_type_vals, cmd->type),
|
|
|
|
cmd->id, cmd->variable);
|
|
|
|
return CTRL_CMD_HANDLED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd->type == CTRL_TYPE_ERROR) {
|
|
|
|
LOGP(DLCTRL, LOGL_ERROR, "%s <%s> for %s is %s\n",
|
|
|
|
get_value_string(ctrl_type_vals, cmd->type),
|
|
|
|
cmd->id, cmd->variable, cmd->reply);
|
|
|
|
return CTRL_CMD_HANDLED;
|
|
|
|
}
|
|
|
|
|
2014-08-21 13:13:08 +00:00
|
|
|
ret = CTRL_CMD_ERROR;
|
|
|
|
cmd->reply = NULL;
|
|
|
|
node = CTRL_NODE_ROOT;
|
|
|
|
cmd->node = data;
|
|
|
|
|
|
|
|
request = talloc_strdup(cmd, cmd->variable);
|
|
|
|
if (!request)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
for (i=0;i<strlen(request);i++) {
|
|
|
|
if (request[i] == '.')
|
|
|
|
request[i] = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
vline = cmd_make_strvec(request);
|
|
|
|
talloc_free(request);
|
|
|
|
if (!vline) {
|
|
|
|
cmd->reply = "cmd_make_strvec failed.";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0;i<vector_active(vline);i++) {
|
2017-04-16 14:57:40 +00:00
|
|
|
struct lookup_helper *lh;
|
2014-08-21 13:13:08 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (ctrl->lookup)
|
|
|
|
rc = ctrl->lookup(data, vline, &node, &cmd->node, &i);
|
|
|
|
else
|
|
|
|
rc = 0;
|
|
|
|
|
2017-04-16 14:57:40 +00:00
|
|
|
if (!rc) {
|
|
|
|
llist_for_each_entry(lh, &ctrl_lookup_helpers, list) {
|
|
|
|
rc = lh->lookup(data, vline, &node, &cmd->node, &i);
|
|
|
|
if (rc)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:24:12 +00:00
|
|
|
switch (rc) {
|
|
|
|
case 1: /* do nothing */
|
|
|
|
break;
|
|
|
|
case -ENODEV:
|
|
|
|
cmd_free_strvec(vline);
|
|
|
|
cmd->type = CTRL_TYPE_ERROR;
|
|
|
|
cmd->reply = "Error while resolving object";
|
|
|
|
return ret;
|
|
|
|
case -ERANGE:
|
|
|
|
cmd_free_strvec(vline);
|
|
|
|
cmd->type = CTRL_TYPE_ERROR;
|
|
|
|
cmd->reply = "Error while parsing the index.";
|
|
|
|
return ret;
|
|
|
|
default: /* If we're here the rest must be the command */
|
2014-08-21 13:13:08 +00:00
|
|
|
cmdvec = vector_init(vector_active(vline)-i);
|
|
|
|
for (j=i; j<vector_active(vline); j++) {
|
|
|
|
vector_set(cmdvec, vector_slot(vline, j));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the command vector of the right node */
|
|
|
|
cmds_vec = vector_lookup(ctrl_node_vec, node);
|
|
|
|
|
|
|
|
if (!cmds_vec) {
|
|
|
|
cmd->reply = "Command not found.";
|
|
|
|
vector_free(cmdvec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ctrl_cmd_exec(cmdvec, cmd, cmds_vec, data);
|
|
|
|
vector_free(cmdvec);
|
2017-05-02 14:24:12 +00:00
|
|
|
break_cycle = true;
|
2014-08-21 13:13:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:24:12 +00:00
|
|
|
if (break_cycle)
|
|
|
|
break;
|
|
|
|
|
2014-08-21 13:13:08 +00:00
|
|
|
if (i+1 == vector_active(vline))
|
|
|
|
cmd->reply = "Command not present.";
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd_free_strvec(vline);
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (!cmd->reply) {
|
|
|
|
if (ret == CTRL_CMD_ERROR) {
|
2017-03-02 21:13:48 +00:00
|
|
|
cmd->reply = "An error has occurred.";
|
2014-08-21 13:13:08 +00:00
|
|
|
LOGP(DLCTRL, LOGL_NOTICE,
|
|
|
|
"%s: cmd->reply has not been set (ERROR).\n",
|
|
|
|
cmd->variable);
|
|
|
|
} else if (ret == CTRL_CMD_REPLY) {
|
|
|
|
LOGP(DLCTRL, LOGL_NOTICE,
|
|
|
|
"%s: cmd->reply has not been set (type = %d).\n",
|
|
|
|
cmd->variable, cmd->type);
|
|
|
|
cmd->reply = "";
|
|
|
|
} else {
|
|
|
|
cmd->reply = "Command has been handled.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == CTRL_CMD_ERROR)
|
|
|
|
cmd->type = CTRL_TYPE_ERROR;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-21 13:00:36 +00:00
|
|
|
static int handle_control_read(struct osmo_fd * bfd)
|
|
|
|
{
|
src: port openBSC over libosmo-abis
This is a big patch that ports openBSC over libosmo-abis.
Sorry, the changes that are included here are all dependent
of libosmo-abis, splitting them into smaller pieces would
leave the repository in some intermediate state, which is
not desired.
The main changes are:
- The directory libabis/ has been removed as it now lives in
libosmo-abis.
- new configuration file format for nanoBTS and HSL femto, we
need to define the virtual e1_line and attach it to the OML
link.
- all the existing BTS drivers (nanoBTS, hsl femto, Nokia site,
BS11 and rbs2000) now use the new libosmo-abis framework.
- use r232 input driver available in libosmo-abis for bs11_config.
- use ipa_msg_recv instead of old ipaccess_read_msg function.
- delete definition of gsm_e1_subslot and input_signal_data.
These structures now lives in libosmo-abis.
Most of this patch are deletions of libabis/ which has been
moved to libosmo-abis.
This patch also modifies openBSC to use all the new definitions
available in libosmocore and libosmo-abis. In order to do that,
we have replaced the following:
- DINP, DMI, DMIB and DMUX by their respective DL* correspondences.
- SS_GLOBAL by SS_L_GLOBAL
- SS_INPUT by SS_L_INPUT
- S_GLOBAL_SHUTDOWN by S_L_GLOBAL_SHUTDOWN
- SS_INPUT by SS_L_INPUT
- S_INP_* by S_L_INP_* sub-signals
- E1INP_NODE by L_E1INP_NODE vty node
This patch has been tested with:
- one nanoBTS
- the HSL femto with the examples available under libosmo-abis
- BS11 with both dahdi and misdn drivers.
2011-08-17 20:44:07 +00:00
|
|
|
int ret = -1;
|
2010-10-21 13:00:36 +00:00
|
|
|
struct osmo_wqueue *queue;
|
|
|
|
struct ctrl_connection *ccon;
|
|
|
|
struct ipaccess_head *iph;
|
|
|
|
struct ipaccess_head_ext *iph_ext;
|
2011-10-11 22:29:30 +00:00
|
|
|
struct msgb *msg = NULL;
|
2010-10-21 13:00:36 +00:00
|
|
|
struct ctrl_cmd *cmd;
|
|
|
|
struct ctrl_handle *ctrl = bfd->data;
|
|
|
|
|
|
|
|
queue = container_of(bfd, struct osmo_wqueue, bfd);
|
|
|
|
ccon = container_of(queue, struct ctrl_connection, write_queue);
|
|
|
|
|
2014-03-31 11:42:11 +00:00
|
|
|
ret = ipa_msg_recv_buffered(bfd->fd, &msg, &ccon->pending_msg);
|
src: port openBSC over libosmo-abis
This is a big patch that ports openBSC over libosmo-abis.
Sorry, the changes that are included here are all dependent
of libosmo-abis, splitting them into smaller pieces would
leave the repository in some intermediate state, which is
not desired.
The main changes are:
- The directory libabis/ has been removed as it now lives in
libosmo-abis.
- new configuration file format for nanoBTS and HSL femto, we
need to define the virtual e1_line and attach it to the OML
link.
- all the existing BTS drivers (nanoBTS, hsl femto, Nokia site,
BS11 and rbs2000) now use the new libosmo-abis framework.
- use r232 input driver available in libosmo-abis for bs11_config.
- use ipa_msg_recv instead of old ipaccess_read_msg function.
- delete definition of gsm_e1_subslot and input_signal_data.
These structures now lives in libosmo-abis.
Most of this patch are deletions of libabis/ which has been
moved to libosmo-abis.
This patch also modifies openBSC to use all the new definitions
available in libosmocore and libosmo-abis. In order to do that,
we have replaced the following:
- DINP, DMI, DMIB and DMUX by their respective DL* correspondences.
- SS_GLOBAL by SS_L_GLOBAL
- SS_INPUT by SS_L_INPUT
- S_GLOBAL_SHUTDOWN by S_L_GLOBAL_SHUTDOWN
- SS_INPUT by SS_L_INPUT
- S_INP_* by S_L_INP_* sub-signals
- E1INP_NODE by L_E1INP_NODE vty node
This patch has been tested with:
- one nanoBTS
- the HSL femto with the examples available under libosmo-abis
- BS11 with both dahdi and misdn drivers.
2011-08-17 20:44:07 +00:00
|
|
|
if (ret <= 0) {
|
2014-03-31 11:42:11 +00:00
|
|
|
if (ret == -EAGAIN)
|
|
|
|
return 0;
|
src: port openBSC over libosmo-abis
This is a big patch that ports openBSC over libosmo-abis.
Sorry, the changes that are included here are all dependent
of libosmo-abis, splitting them into smaller pieces would
leave the repository in some intermediate state, which is
not desired.
The main changes are:
- The directory libabis/ has been removed as it now lives in
libosmo-abis.
- new configuration file format for nanoBTS and HSL femto, we
need to define the virtual e1_line and attach it to the OML
link.
- all the existing BTS drivers (nanoBTS, hsl femto, Nokia site,
BS11 and rbs2000) now use the new libosmo-abis framework.
- use r232 input driver available in libosmo-abis for bs11_config.
- use ipa_msg_recv instead of old ipaccess_read_msg function.
- delete definition of gsm_e1_subslot and input_signal_data.
These structures now lives in libosmo-abis.
Most of this patch are deletions of libabis/ which has been
moved to libosmo-abis.
This patch also modifies openBSC to use all the new definitions
available in libosmocore and libosmo-abis. In order to do that,
we have replaced the following:
- DINP, DMI, DMIB and DMUX by their respective DL* correspondences.
- SS_GLOBAL by SS_L_GLOBAL
- SS_INPUT by SS_L_INPUT
- S_GLOBAL_SHUTDOWN by S_L_GLOBAL_SHUTDOWN
- SS_INPUT by SS_L_INPUT
- S_INP_* by S_L_INP_* sub-signals
- E1INP_NODE by L_E1INP_NODE vty node
This patch has been tested with:
- one nanoBTS
- the HSL femto with the examples available under libosmo-abis
- BS11 with both dahdi and misdn drivers.
2011-08-17 20:44:07 +00:00
|
|
|
if (ret == 0)
|
2014-08-20 17:58:13 +00:00
|
|
|
LOGP(DLCTRL, LOGL_INFO, "The control connection was closed\n");
|
2010-10-21 13:00:36 +00:00
|
|
|
else
|
2014-08-20 17:58:13 +00:00
|
|
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to parse ip access message: %d\n", ret);
|
2010-10-21 13:00:36 +00:00
|
|
|
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->len < sizeof(*iph) + sizeof(*iph_ext)) {
|
2014-08-20 17:58:13 +00:00
|
|
|
LOGP(DLCTRL, LOGL_ERROR, "The message is too short.\n");
|
2010-10-21 13:00:36 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
iph = (struct ipaccess_head *) msg->data;
|
|
|
|
if (iph->proto != IPAC_PROTO_OSMO) {
|
2014-08-20 17:58:13 +00:00
|
|
|
LOGP(DLCTRL, LOGL_ERROR, "Protocol mismatch. We got 0x%x\n", iph->proto);
|
2010-10-21 13:00:36 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
iph_ext = (struct ipaccess_head_ext *) iph->data;
|
|
|
|
if (iph_ext->proto != IPAC_PROTO_EXT_CTRL) {
|
2014-08-20 17:58:13 +00:00
|
|
|
LOGP(DLCTRL, LOGL_ERROR, "Extended protocol mismatch. We got 0x%x\n", iph_ext->proto);
|
2010-10-21 13:00:36 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->l2h = iph_ext->data;
|
|
|
|
|
|
|
|
cmd = ctrl_cmd_parse(ccon, msg);
|
|
|
|
|
|
|
|
if (cmd) {
|
|
|
|
cmd->ccon = ccon;
|
2014-08-21 13:13:08 +00:00
|
|
|
if (ctrl_cmd_handle(ctrl, cmd, ctrl->data) != CTRL_CMD_HANDLED) {
|
2010-10-21 13:00:36 +00:00
|
|
|
ctrl_cmd_send(queue, cmd);
|
|
|
|
talloc_free(cmd);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cmd = talloc_zero(ccon, struct ctrl_cmd);
|
|
|
|
if (!cmd)
|
|
|
|
goto err;
|
2014-08-20 17:58:13 +00:00
|
|
|
LOGP(DLCTRL, LOGL_ERROR, "Command parser error.\n");
|
2010-10-21 13:00:36 +00:00
|
|
|
cmd->type = CTRL_TYPE_ERROR;
|
|
|
|
cmd->id = "err";
|
|
|
|
cmd->reply = "Command parser error.";
|
|
|
|
ctrl_cmd_send(queue, cmd);
|
|
|
|
talloc_free(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
msgb_free(msg);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
control_close_conn(ccon);
|
|
|
|
msgb_free(msg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int control_write_cb(struct osmo_fd *bfd, struct msgb *msg)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = write(bfd->fd, msg->data, msg->len);
|
|
|
|
if (rc != msg->len)
|
2014-08-20 17:58:13 +00:00
|
|
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to write message to the control connection.\n");
|
2010-10-21 13:00:36 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Allocate CTRL connection
|
2017-02-20 10:53:11 +00:00
|
|
|
* \param[in] ctx Context from which talloc should allocate it
|
|
|
|
* \param[in] data caller's private data parameter which should assigned to
|
|
|
|
write queue's file descriptor data parameter.
|
|
|
|
* \return Allocated CTRL connection structure or NULL in case of errors
|
|
|
|
*/
|
|
|
|
struct ctrl_connection *osmo_ctrl_conn_alloc(void *ctx, void *data)
|
2010-10-21 13:00:36 +00:00
|
|
|
{
|
|
|
|
struct ctrl_connection *ccon = talloc_zero(ctx, struct ctrl_connection);
|
|
|
|
if (!ccon)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
osmo_wqueue_init(&ccon->write_queue, 100);
|
|
|
|
/* Error handling here? */
|
|
|
|
|
|
|
|
INIT_LLIST_HEAD(&ccon->cmds);
|
2014-08-21 22:28:51 +00:00
|
|
|
INIT_LLIST_HEAD(&ccon->def_cmds);
|
|
|
|
|
2017-02-20 10:53:11 +00:00
|
|
|
ccon->write_queue.bfd.data = data;
|
|
|
|
ccon->write_queue.write_cb = control_write_cb;
|
2017-02-24 12:03:32 +00:00
|
|
|
ccon->write_queue.read_cb = handle_control_read;
|
2017-02-20 10:53:11 +00:00
|
|
|
|
2010-10-21 13:00:36 +00:00
|
|
|
return ccon;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
|
|
|
|
{
|
|
|
|
int ret, fd, on;
|
2011-05-06 14:37:25 +00:00
|
|
|
struct ctrl_handle *ctrl;
|
2010-10-21 13:00:36 +00:00
|
|
|
struct ctrl_connection *ccon;
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
socklen_t sa_len = sizeof(sa);
|
|
|
|
|
|
|
|
|
|
|
|
if (!(what & BSC_FD_READ))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("accept");
|
|
|
|
return fd;
|
|
|
|
}
|
2014-08-20 17:58:13 +00:00
|
|
|
LOGP(DLCTRL, LOGL_INFO, "accept()ed new control connection from %s\n",
|
2010-10-21 13:00:36 +00:00
|
|
|
inet_ntoa(sa.sin_addr));
|
|
|
|
|
2014-12-04 13:15:36 +00:00
|
|
|
#ifdef TCP_NODELAY
|
2010-10-21 13:00:36 +00:00
|
|
|
on = 1;
|
|
|
|
ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
|
|
|
|
if (ret != 0) {
|
2014-08-20 17:58:13 +00:00
|
|
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
|
2010-10-21 13:00:36 +00:00
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-12-04 13:15:36 +00:00
|
|
|
#endif
|
2017-02-20 10:53:11 +00:00
|
|
|
ctrl = listen_bfd->data;
|
|
|
|
ccon = osmo_ctrl_conn_alloc(listen_bfd->data, ctrl);
|
2010-10-21 13:00:36 +00:00
|
|
|
if (!ccon) {
|
2014-08-20 17:58:13 +00:00
|
|
|
LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate.\n");
|
2010-10-21 13:00:36 +00:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ccon->write_queue.bfd.fd = fd;
|
|
|
|
ccon->write_queue.bfd.when = BSC_FD_READ;
|
|
|
|
|
|
|
|
ret = osmo_fd_register(&ccon->write_queue.bfd);
|
|
|
|
if (ret < 0) {
|
2014-08-20 17:58:13 +00:00
|
|
|
LOGP(DLCTRL, LOGL_ERROR, "Could not register FD.\n");
|
2010-10-21 13:00:36 +00:00
|
|
|
close(ccon->write_queue.bfd.fd);
|
|
|
|
talloc_free(ccon);
|
|
|
|
}
|
|
|
|
|
2011-05-06 14:37:25 +00:00
|
|
|
llist_add(&ccon->list_entry, &ctrl->ccon_list);
|
|
|
|
|
2010-10-21 13:00:36 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-21 09:43:55 +00:00
|
|
|
static uint64_t get_rate_ctr_value(const struct rate_ctr *ctr, int intv)
|
|
|
|
{
|
|
|
|
if (intv >= RATE_CTR_INTV_NUM)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Absolute value */
|
|
|
|
if (intv == -1) {
|
|
|
|
return ctr->current;
|
|
|
|
} else {
|
|
|
|
return ctr->intv[intv].rate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 17:55:17 +00:00
|
|
|
static char *get_all_rate_ctr_in_group(void *ctx, const struct rate_ctr_group *ctrg, int intv)
|
2011-04-21 09:43:55 +00:00
|
|
|
{
|
|
|
|
int i;
|
2014-08-20 17:55:17 +00:00
|
|
|
char *counters = talloc_strdup(ctx, "");
|
2011-04-21 09:43:55 +00:00
|
|
|
if (!counters)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i=0;i<ctrg->desc->num_ctr;i++) {
|
2011-07-19 10:12:10 +00:00
|
|
|
counters = talloc_asprintf_append(counters, "\n%s.%u.%s %"PRIu64,
|
2011-04-21 09:43:55 +00:00
|
|
|
ctrg->desc->group_name_prefix, ctrg->idx,
|
|
|
|
ctrg->desc->ctr_desc[i].name,
|
|
|
|
get_rate_ctr_value(&ctrg->ctr[i], intv));
|
|
|
|
if (!counters)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return counters;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_rate_ctr_group_idx(const struct rate_ctr_group *ctrg, int intv, struct ctrl_cmd *cmd)
|
|
|
|
{
|
|
|
|
char *counters;
|
|
|
|
|
2014-08-20 17:55:17 +00:00
|
|
|
counters = get_all_rate_ctr_in_group(cmd, ctrg, intv);
|
2011-04-21 09:43:55 +00:00
|
|
|
if (!counters)
|
|
|
|
goto oom;
|
|
|
|
|
|
|
|
cmd->reply = talloc_asprintf(cmd, "All counters in %s.%u%s",
|
|
|
|
ctrg->desc->group_name_prefix, ctrg->idx, counters);
|
|
|
|
talloc_free(counters);
|
|
|
|
if (!cmd->reply)
|
|
|
|
goto oom;
|
|
|
|
|
|
|
|
return CTRL_CMD_REPLY;
|
|
|
|
oom:
|
|
|
|
cmd->reply = "OOM.";
|
|
|
|
return CTRL_CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rate_ctr */
|
|
|
|
CTRL_CMD_DEFINE(rate_ctr, "rate_ctr *");
|
2011-08-05 09:48:18 +00:00
|
|
|
static int get_rate_ctr(struct ctrl_cmd *cmd, void *data)
|
2011-04-21 09:43:55 +00:00
|
|
|
{
|
|
|
|
int intv;
|
|
|
|
unsigned int idx;
|
2016-09-19 12:15:03 +00:00
|
|
|
char *ctr_group, *ctr_idx, *tmp, *dup, *saveptr, *interval;
|
2011-04-21 09:43:55 +00:00
|
|
|
struct rate_ctr_group *ctrg;
|
|
|
|
const struct rate_ctr *ctr;
|
|
|
|
|
|
|
|
dup = talloc_strdup(cmd, cmd->variable);
|
|
|
|
if (!dup)
|
|
|
|
goto oom;
|
|
|
|
|
|
|
|
/* Skip over possible prefixes (net.) */
|
|
|
|
tmp = strstr(dup, "rate_ctr");
|
|
|
|
if (!tmp) {
|
|
|
|
talloc_free(dup);
|
|
|
|
cmd->reply = "rate_ctr not a token in rate_ctr command!";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
strtok_r(tmp, ".", &saveptr);
|
|
|
|
interval = strtok_r(NULL, ".", &saveptr);
|
|
|
|
if (!interval) {
|
|
|
|
talloc_free(dup);
|
|
|
|
cmd->reply = "Missing interval.";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(interval, "abs")) {
|
|
|
|
intv = -1;
|
|
|
|
} else if (!strcmp(interval, "per_sec")) {
|
|
|
|
intv = RATE_CTR_INTV_SEC;
|
|
|
|
} else if (!strcmp(interval, "per_min")) {
|
|
|
|
intv = RATE_CTR_INTV_MIN;
|
|
|
|
} else if (!strcmp(interval, "per_hour")) {
|
|
|
|
intv = RATE_CTR_INTV_HOUR;
|
|
|
|
} else if (!strcmp(interval, "per_day")) {
|
|
|
|
intv = RATE_CTR_INTV_DAY;
|
|
|
|
} else {
|
|
|
|
talloc_free(dup);
|
|
|
|
cmd->reply = "Wrong interval.";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctr_group = strtok_r(NULL, ".", &saveptr);
|
2016-06-14 16:17:06 +00:00
|
|
|
ctr_idx = strtok_r(NULL, ".", &saveptr);
|
|
|
|
if (!ctr_group || !ctr_idx) {
|
2011-04-21 09:43:55 +00:00
|
|
|
talloc_free(dup);
|
2016-06-14 16:17:06 +00:00
|
|
|
cmd->reply = "Counter group must be of name.index form e. g. "
|
|
|
|
"e1inp.0";
|
2011-04-21 09:43:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = atoi(ctr_idx);
|
|
|
|
|
|
|
|
ctrg = rate_ctr_get_group_by_name_idx(ctr_group, idx);
|
|
|
|
if (!ctrg) {
|
|
|
|
talloc_free(dup);
|
2016-06-14 16:17:06 +00:00
|
|
|
cmd->reply = "Counter group with given name and index not found";
|
2011-04-21 09:43:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-06-14 16:17:06 +00:00
|
|
|
if (!strlen(saveptr)) {
|
2011-04-21 09:43:55 +00:00
|
|
|
talloc_free(dup);
|
|
|
|
return get_rate_ctr_group_idx(ctrg, intv, cmd);
|
|
|
|
}
|
|
|
|
|
2016-06-14 16:17:06 +00:00
|
|
|
ctr = rate_ctr_get_by_name(ctrg, saveptr);
|
2011-04-21 09:43:55 +00:00
|
|
|
if (!ctr) {
|
|
|
|
cmd->reply = "Counter name not found.";
|
|
|
|
talloc_free(dup);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
talloc_free(dup);
|
|
|
|
|
2012-03-16 11:18:39 +00:00
|
|
|
cmd->reply = talloc_asprintf(cmd, "%"PRIu64, get_rate_ctr_value(ctr, intv));
|
2011-04-21 09:43:55 +00:00
|
|
|
if (!cmd->reply)
|
|
|
|
goto oom;
|
|
|
|
|
|
|
|
return CTRL_CMD_REPLY;
|
|
|
|
oom:
|
|
|
|
cmd->reply = "OOM";
|
|
|
|
err:
|
|
|
|
return CTRL_CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-08-05 09:48:18 +00:00
|
|
|
static int set_rate_ctr(struct ctrl_cmd *cmd, void *data)
|
2011-04-21 09:43:55 +00:00
|
|
|
{
|
|
|
|
cmd->reply = "Can't set rate counter.";
|
|
|
|
|
|
|
|
return CTRL_CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-08-05 09:48:18 +00:00
|
|
|
static int verify_rate_ctr(struct ctrl_cmd *cmd, const char *value, void *data)
|
2011-04-21 09:43:55 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* counter */
|
|
|
|
CTRL_CMD_DEFINE(counter, "counter *");
|
2011-08-05 09:48:18 +00:00
|
|
|
static int get_counter(struct ctrl_cmd *cmd, void *data)
|
2011-04-21 09:43:55 +00:00
|
|
|
{
|
|
|
|
char *ctr_name, *tmp, *dup, *saveptr;
|
|
|
|
struct osmo_counter *counter;
|
|
|
|
|
|
|
|
cmd->reply = "OOM";
|
|
|
|
dup = talloc_strdup(cmd, cmd->variable);
|
|
|
|
if (!dup)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
|
|
|
|
tmp = strstr(dup, "counter");
|
|
|
|
if (!tmp) {
|
|
|
|
talloc_free(dup);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
strtok_r(tmp, ".", &saveptr);
|
|
|
|
ctr_name = strtok_r(NULL, "\0", &saveptr);
|
|
|
|
|
|
|
|
if (!ctr_name)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
counter = osmo_counter_get_by_name(ctr_name);
|
|
|
|
if (!counter) {
|
|
|
|
cmd->reply = "Counter name not found.";
|
|
|
|
talloc_free(dup);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
talloc_free(dup);
|
|
|
|
|
|
|
|
cmd->reply = talloc_asprintf(cmd, "%lu", counter->value);
|
|
|
|
if (!cmd->reply) {
|
|
|
|
cmd->reply = "OOM";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CTRL_CMD_REPLY;
|
|
|
|
err:
|
|
|
|
return CTRL_CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-08-05 09:48:18 +00:00
|
|
|
static int set_counter(struct ctrl_cmd *cmd, void *data)
|
2011-04-21 09:43:55 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
cmd->reply = "Can't set counter.";
|
|
|
|
|
|
|
|
return CTRL_CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-08-05 09:48:18 +00:00
|
|
|
static int verify_counter(struct ctrl_cmd *cmd, const char *value, void *data)
|
2011-04-21 09:43:55 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-21 13:20:00 +00:00
|
|
|
struct ctrl_handle *ctrl_interface_setup(void *data, uint16_t port,
|
|
|
|
ctrl_cmd_lookup lookup)
|
2016-02-23 23:09:40 +00:00
|
|
|
{
|
|
|
|
return ctrl_interface_setup_dynip(data, "127.0.0.1", port, lookup);
|
|
|
|
}
|
|
|
|
|
2017-04-16 17:08:49 +00:00
|
|
|
static int ctrl_initialized = 0;
|
|
|
|
|
|
|
|
/* global ctrl initialization */
|
|
|
|
static int ctrl_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (ctrl_initialized)
|
|
|
|
return 0;
|
|
|
|
|
2017-04-16 17:49:24 +00:00
|
|
|
ctrl_node_vec = vector_init(_LAST_CTRL_NODE);
|
2017-04-16 17:08:49 +00:00
|
|
|
if (!ctrl_node_vec)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_rate_ctr);
|
|
|
|
if (ret)
|
|
|
|
goto err_vec;
|
|
|
|
ret = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_counter);
|
|
|
|
if (ret)
|
|
|
|
goto err_vec;
|
|
|
|
|
2017-04-16 15:26:30 +00:00
|
|
|
ret = osmo_fsm_ctrl_cmds_install();
|
|
|
|
if (ret)
|
|
|
|
goto err_vec;
|
|
|
|
|
2017-04-16 17:08:49 +00:00
|
|
|
ctrl_initialized = 1;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_vec:
|
|
|
|
vector_free(ctrl_node_vec);
|
|
|
|
ctrl_node_vec = NULL;
|
|
|
|
err:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Allocate a CTRL interface handle
|
2017-04-16 17:08:49 +00:00
|
|
|
* \param[in] ctx Tallo callocation context to be used
|
|
|
|
* \param[in] data Pointer which will be made available to each
|
|
|
|
set_..() get_..() verify_..() control command function
|
|
|
|
* \param[in] lookup Lookup function pointer, can be NULL
|
|
|
|
* \returns ctrl_handle pointer or NULL in case of errors
|
|
|
|
*/
|
|
|
|
struct ctrl_handle *ctrl_handle_alloc(void *ctx, void *data, ctrl_cmd_lookup lookup)
|
|
|
|
{
|
|
|
|
struct ctrl_handle *ctrl;
|
|
|
|
|
|
|
|
ctrl_init();
|
|
|
|
|
|
|
|
ctrl = talloc_zero(ctx, struct ctrl_handle);
|
|
|
|
if (!ctrl)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
INIT_LLIST_HEAD(&ctrl->ccon_list);
|
|
|
|
|
|
|
|
ctrl->data = data;
|
|
|
|
ctrl->lookup = lookup;
|
|
|
|
|
|
|
|
return ctrl;
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Setup CTRL interface on a given address
|
2017-02-16 16:44:09 +00:00
|
|
|
* \param[in] data Pointer which will be made available to each
|
|
|
|
set_..() get_..() verify_..() control command function
|
|
|
|
* \param[in] bind_addr Address on which CTRL socket shall listen
|
|
|
|
* \param[in] port Port on which CTRL socket shall listen
|
|
|
|
* \param[in] lookup Lookup function pointer, can be NULL
|
|
|
|
* \returns ctrl_handle pointer or NULL in case of errors
|
|
|
|
*/
|
2016-02-23 23:09:40 +00:00
|
|
|
struct ctrl_handle *ctrl_interface_setup_dynip(void *data,
|
|
|
|
const char *bind_addr,
|
|
|
|
uint16_t port,
|
|
|
|
ctrl_cmd_lookup lookup)
|
2010-10-21 13:00:36 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct ctrl_handle *ctrl;
|
|
|
|
|
2017-04-16 17:08:49 +00:00
|
|
|
ctrl = ctrl_handle_alloc(data, data, lookup);
|
2010-10-21 13:00:36 +00:00
|
|
|
if (!ctrl)
|
2011-07-22 15:55:42 +00:00
|
|
|
return NULL;
|
2010-10-21 13:00:36 +00:00
|
|
|
|
|
|
|
/* Listen for control connections */
|
2014-08-20 17:50:04 +00:00
|
|
|
ctrl->listen_fd.cb = listen_fd_cb;
|
|
|
|
ctrl->listen_fd.data = ctrl;
|
|
|
|
ret = osmo_sock_init_ofd(&ctrl->listen_fd, AF_INET, SOCK_STREAM, IPPROTO_TCP,
|
2016-02-23 23:09:40 +00:00
|
|
|
bind_addr, port, OSMO_SOCK_F_BIND);
|
2017-04-16 17:08:49 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
talloc_free(ctrl);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-21 09:43:55 +00:00
|
|
|
|
2016-09-19 12:17:30 +00:00
|
|
|
LOGP(DLCTRL, LOGL_NOTICE, "CTRL at %s %u\n", bind_addr, port);
|
2011-07-22 15:55:42 +00:00
|
|
|
return ctrl;
|
2010-10-21 13:00:36 +00:00
|
|
|
}
|
2017-04-16 14:57:40 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Install a lookup helper function for control nodes
|
2017-04-16 14:57:40 +00:00
|
|
|
* This function is used by e.g. library code to install lookup helpers
|
|
|
|
* for additional nodes in the control interface.
|
|
|
|
* \param[in] lookup The lookup helper function
|
|
|
|
* \retuns - on success; negative on error.
|
|
|
|
*/
|
|
|
|
int ctrl_lookup_register(ctrl_cmd_lookup lookup)
|
|
|
|
{
|
|
|
|
struct lookup_helper *lh;
|
|
|
|
|
|
|
|
/* avoid double registration */
|
|
|
|
llist_for_each_entry(lh, &ctrl_lookup_helpers, list) {
|
|
|
|
if (lh->lookup == lookup)
|
|
|
|
return -EEXIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
lh = talloc_zero(NULL, struct lookup_helper);
|
|
|
|
if (!lh)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
lh->lookup = lookup;
|
|
|
|
llist_add_tail(&lh->list, &ctrl_lookup_helpers);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-16 17:17:10 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Helper for "local execution" of a CTRL command from a string
|
2017-04-16 17:17:10 +00:00
|
|
|
* The function will parse + execute the given control command string
|
|
|
|
* and return a corresponding ctrl_cmd. Caller is responsible to
|
|
|
|
* talloc_free() the return value.
|
|
|
|
* \param[in] Control Interface Command String
|
|
|
|
* \returns parsed command, including reply; NULL on error */
|
|
|
|
struct ctrl_cmd *ctrl_cmd_exec_from_string(struct ctrl_handle *ch, const char *cmdstr)
|
|
|
|
{
|
|
|
|
struct msgb *msg = msgb_alloc(1024, "ctrl-cmd");
|
|
|
|
struct ctrl_cmd *cmd;
|
|
|
|
|
|
|
|
if (!msg)
|
|
|
|
return NULL;
|
|
|
|
msg->l2h = msg->data;
|
|
|
|
osmo_strlcpy((char *)msg->data, cmdstr, msgb_tailroom(msg));
|
|
|
|
msgb_put(msg, strlen(cmdstr));
|
|
|
|
|
|
|
|
cmd = ctrl_cmd_parse(ch, msg);
|
|
|
|
msgb_free(msg);
|
|
|
|
if (!cmd)
|
|
|
|
return NULL;
|
|
|
|
if (ctrl_cmd_handle(ch, cmd, NULL) < 0) {
|
|
|
|
talloc_free(cmd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return cmd;
|
|
|
|
}
|