From 4462f8c30f744406dd7609eebf7ca1878003e14d Mon Sep 17 00:00:00 2001 From: Daniel Willmann Date: Thu, 21 Oct 2010 15:00:36 +0200 Subject: Add libctrl, an SNMP-like control interface In contrast to the VTY interface the control interface is meant to be used by programs. This patch adds basic support, no commands are defined. --- openbsc/configure.ac | 1 + openbsc/include/openbsc/control_cmd.h | 81 +++++++ openbsc/src/Makefile.am | 2 +- openbsc/src/libctrl/Makefile.am | 7 + openbsc/src/libctrl/control_cmd.c | 443 ++++++++++++++++++++++++++++++++++ openbsc/src/libctrl/control_if.c | 369 ++++++++++++++++++++++++++++ 6 files changed, 902 insertions(+), 1 deletion(-) create mode 100644 openbsc/include/openbsc/control_cmd.h create mode 100644 openbsc/src/libctrl/Makefile.am create mode 100644 openbsc/src/libctrl/control_cmd.c create mode 100644 openbsc/src/libctrl/control_if.c diff --git a/openbsc/configure.ac b/openbsc/configure.ac index 5ed53035b..89cfcac66 100644 --- a/openbsc/configure.ac +++ b/openbsc/configure.ac @@ -92,6 +92,7 @@ AC_OUTPUT( src/libtrau/Makefile src/libabis/Makefile src/libbsc/Makefile + src/libctrl/Makefile src/libmsc/Makefile src/libmgcp/Makefile src/libcommon/Makefile diff --git a/openbsc/include/openbsc/control_cmd.h b/openbsc/include/openbsc/control_cmd.h new file mode 100644 index 000000000..8d543ee84 --- /dev/null +++ b/openbsc/include/openbsc/control_cmd.h @@ -0,0 +1,81 @@ +#ifndef _CONTROL_CMD_H +#define _CONTROL_CMD_H + +#include +#include + +#include + +#define CTRL_CMD_ERROR -1 +#define CTRL_CMD_HANDLED 0 +#define CTRL_CMD_REPLY 1 + +enum ctrl_node_type { + CTRL_NODE_ROOT, /* Root elements */ + CTRL_NODE_NET, /* Network specific (net.) */ + CTRL_NODE_BTS, /* BTS specific (net.btsN.) */ + CTRL_NODE_TRX, /* TRX specific (net.btsN.trxM.) */ + CTRL_NODE_TS, /* TS specific (net.btsN.trxM.tsI.) */ + _LAST_CTRL_NODE +}; + +enum ctrl_type { + CTRL_TYPE_UNKNOWN, + CTRL_TYPE_GET, + CTRL_TYPE_SET, + CTRL_TYPE_GET_REPLY, + CTRL_TYPE_SET_REPLY, + CTRL_TYPE_TRAP, + CTRL_TYPE_ERROR +}; + +struct ctrl_connection { + struct llist_head list_entry; + + /* The queue for sending data back */ + struct osmo_wqueue write_queue; + + /* Callback if the connection was closed */ + void (*closed_cb)(struct ctrl_connection *conn); + + /* Pending commands for this connection */ + struct llist_head cmds; +}; + +struct ctrl_cmd { + struct ctrl_connection *ccon; + enum ctrl_type type; + char *id; + void *node; + char *variable; + char *value; + char *reply; +}; + +struct ctrl_cmd_struct { + int nr_commands; + char **command; +}; + +struct ctrl_cmd_element { + const char *name; + const char *param; + struct ctrl_cmd_struct strcmd; + int (*set)(struct ctrl_cmd *cmd, void *data); + int (*get)(struct ctrl_cmd *cmd, void *data); + int (*verify)(struct ctrl_cmd *cmd, const char *value, void *data); +}; + +struct ctrl_cmd_map { + char *cmd; + enum ctrl_type type; +}; + +int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data); +int ctrl_cmd_install(enum ctrl_node_type node, struct ctrl_cmd_element *cmd); +int ctrl_cmd_handle(struct ctrl_cmd *cmd, void *data); +int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd); +struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg); +struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd); + +#endif /* _CONTROL_CMD_H */ diff --git a/openbsc/src/Makefile.am b/openbsc/src/Makefile.am index df7b936e7..c7de811f3 100644 --- a/openbsc/src/Makefile.am +++ b/openbsc/src/Makefile.am @@ -2,7 +2,7 @@ INCLUDES = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir) AM_CFLAGS=-Wall $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS) $(LIBOSMOGSM_CFLAGS) $(LIBOSMOVTY_CFLAGS) $(COVERAGE_CFLAGS) AM_LDFLAGS = $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS) $(COVERAGE_LDFLAGS) -SUBDIRS = libcommon libabis libmgcp libbsc libmsc libtrau osmo-nitb osmo-bsc_mgcp utils ipaccess libgb gprs +SUBDIRS = libcommon libabis libmgcp libbsc libmsc libtrau libctrl osmo-nitb osmo-bsc_mgcp utils ipaccess libgb gprs # Conditional modules if BUILD_NAT diff --git a/openbsc/src/libctrl/Makefile.am b/openbsc/src/libctrl/Makefile.am new file mode 100644 index 000000000..286929b89 --- /dev/null +++ b/openbsc/src/libctrl/Makefile.am @@ -0,0 +1,7 @@ +INCLUDES = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir) +AM_CFLAGS=-Wall $(LIBOSMOCORE_CFLAGS) $(LIBOSMOVTY_CFLAGS) $(COVERAGE_CFLAGS) +AM_LDFLAGS = $(LIBOSMOCORE_LIBS) $(COVERAGE_LDFLAGS) + +noinst_LIBRARIES = libctrl.a + +libctrl_a_SOURCES = control_if.c control_cmd.c diff --git a/openbsc/src/libctrl/control_cmd.c b/openbsc/src/libctrl/control_cmd.c new file mode 100644 index 000000000..06af3b320 --- /dev/null +++ b/openbsc/src/libctrl/control_cmd.c @@ -0,0 +1,443 @@ +/* SNMP-like status interface + * + * (C) 2010-2011 by Daniel Willmann + * (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. + * + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include +#include + +extern vector ctrl_node_vec; + +static struct ctrl_cmd_map ccm[] = { + {"GET", CTRL_TYPE_GET}, + {"SET", CTRL_TYPE_SET}, + {"GET_REPLY", CTRL_TYPE_GET_REPLY}, + {"SET_REPLY", CTRL_TYPE_SET_REPLY}, + {"TRAP", CTRL_TYPE_TRAP}, + {"ERROR", CTRL_TYPE_ERROR}, + {NULL} +}; + +int ctrl_cmd_str2type(char *s) +{ + int i; + for (i=0; ccm[i].cmd != NULL; i++) { + if (strcasecmp(s, ccm[i].cmd) == 0) + return ccm[i].type; + } + return CTRL_TYPE_UNKNOWN; +} + +char *ctrl_cmd_type2str(int type) +{ + int i; + for (i=0; ccm[i].cmd != NULL; i++) { + if (ccm[i].type == type) + return ccm[i].cmd; + } + return NULL; +} + +/* Functions from libosmocom */ +extern vector cmd_make_descvec(const char *string, const char *descstr); + +/* Get the ctrl_cmd_element that matches this command */ +static struct ctrl_cmd_element *ctrl_cmd_get_element_match(vector vline, vector node) +{ + int index, j; + const char *desc; + struct ctrl_cmd_element *cmd_el; + struct ctrl_cmd_struct *cmd_desc; + char *str; + + for (index = 0; index < vector_active(node); index++) { + if ((cmd_el = vector_slot(node, index))) { + cmd_desc = &cmd_el->strcmd; + if (cmd_desc->nr_commands > vector_active(vline)) + continue; + for (j =0; j < vector_active(vline); j++) { + str = vector_slot(vline, j); + desc = cmd_desc->command[j]; + if (desc[0] == '*') + return cmd_el; /* Partial match */ + if (strcmp(desc, str) != 0) + break; + } + /* We went through all the elements and all matched */ + if (j == cmd_desc->nr_commands) + return cmd_el; + } + } + + return NULL; +} + +int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data) +{ + int ret = CTRL_CMD_ERROR; + struct ctrl_cmd_element *cmd_el; + + if ((command->type != CTRL_TYPE_GET) && (command->type != CTRL_TYPE_SET)) { + command->reply = "Trying to execute something not GET or SET"; + goto out; + } + if ((command->type == CTRL_TYPE_SET) && (!command->value)) { + command->reply = "SET without a value"; + goto out; + } + + if (!vline) + goto out; + + cmd_el = ctrl_cmd_get_element_match(vline, node); + + if (!cmd_el) { + command->reply = "Command not found"; + goto out; + } + + if (command->type == CTRL_TYPE_SET) { + if (!cmd_el->set) { + command->reply = "SET not implemented"; + goto out; + } + if (cmd_el->verify) { + if ((ret = cmd_el->verify(command, command->value, data))) { + ret = CTRL_CMD_ERROR; + command->reply = "Value failed verification."; + goto out; + } + } else if (cmd_el->param) { + LOGP(DINP, LOGL_NOTICE, "Parameter verification unimplemented, continuing without\n"); + } + ret = cmd_el->set(command, data); + goto out; + } else if (command->type == CTRL_TYPE_GET) { + if (!cmd_el->get) { + command->reply = "GET not implemented"; + goto out; + } + ret = cmd_el->get(command, data); + goto out; + } +out: + if (ret == CTRL_CMD_REPLY) { + if (command->type == CTRL_TYPE_SET) { + command->type = CTRL_TYPE_SET_REPLY; + } else if (command->type == CTRL_TYPE_GET) { + command->type = CTRL_TYPE_GET_REPLY; + } + } else if (ret == CTRL_CMD_ERROR) { + command->type = CTRL_TYPE_ERROR; + } + return ret; +} + +static void add_word(struct ctrl_cmd_struct *cmd, + const char *start, const char *end) +{ + if (!cmd->command) { + cmd->command = talloc_zero_array(tall_vty_vec_ctx, + char*, 1); + cmd->nr_commands = 0; + } else { + cmd->command = talloc_realloc(tall_vty_vec_ctx, + cmd->command, char*, + cmd->nr_commands + 1); + } + + cmd->command[cmd->nr_commands++] = talloc_strndup(cmd->command, + start, end - start); +} + +static void create_cmd_struct(struct ctrl_cmd_struct *cmd, const char *name) +{ + const char *cur, *word; + + for (cur = name, word = NULL; cur[0] != '\0'; ++cur) { + /* warn about optionals */ + if (cur[0] == '(' || cur[0] == ')' || cur[0] == '|') { + LOGP(DINP, LOGL_ERROR, + "Optionals are not supported in '%s'\n", name); + goto failure; + } + + if (isspace(cur[0])) { + if (word) { + add_word(cmd, word, cur); + word = NULL; + } + continue; + } + + if (!word) + word = cur; + } + + if (word) + add_word(cmd, word, cur); + + return; +failure: + cmd->nr_commands = 0; + talloc_free(cmd->command); +} + +int ctrl_cmd_install(enum ctrl_node_type node, struct ctrl_cmd_element *cmd) +{ + vector cmds_vec; + + cmds_vec = vector_lookup_ensure(ctrl_node_vec, node); + + if (!cmds_vec) { + cmds_vec = vector_init(5); + if (!cmds_vec) { + LOGP(DINP, LOGL_ERROR, "vector_init failed.\n"); + return -ENOMEM; + } + vector_set_index(ctrl_node_vec, node, cmds_vec); + } + + vector_set(cmds_vec, cmd); + + create_cmd_struct(&cmd->strcmd, cmd->name); + return 0; +} + +struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg) +{ + char *str, *tmp, *saveptr = NULL; + char *var, *val; + struct ctrl_cmd *cmd; + + cmd = talloc_zero(ctx, struct ctrl_cmd); + if (!cmd) { + LOGP(DINP, LOGL_ERROR, "Failed to allocate.\n"); + return NULL; + } + + /* Make sure input is NULL terminated */ + msgb_put_u8(msg, 0); + str = (char *) msg->l2h; + + tmp = strtok_r(str, " ", &saveptr); + if (!tmp) { + cmd->type = CTRL_TYPE_ERROR; + cmd->id = "err"; + cmd->reply = "Request malformed"; + goto err; + } + + cmd->type = ctrl_cmd_str2type(tmp); + if (cmd->type == CTRL_TYPE_UNKNOWN) { + cmd->type = CTRL_TYPE_ERROR; + cmd->id = "err"; + cmd->reply = "Request type unknown"; + goto err; + } + + tmp = strtok_r(NULL, " ", &saveptr); + + if (!tmp) { + cmd->type = CTRL_TYPE_ERROR; + cmd->id = "err"; + cmd->reply = "Missing ID"; + goto err; + } + cmd->id = talloc_strdup(cmd, tmp); + if (!cmd->id) + goto oom; + + switch (cmd->type) { + case CTRL_TYPE_GET: + var = strtok_r(NULL, " ", &saveptr); + if (!var) { + cmd->type = CTRL_TYPE_ERROR; + cmd->reply = "GET incomplete"; + LOGP(DINP, LOGL_NOTICE, "GET Command incomplete\n"); + goto err; + } + cmd->variable = talloc_strdup(cmd, var); + LOGP(DINP, LOGL_DEBUG, "Command: GET %s\n", cmd->variable); + break; + case CTRL_TYPE_SET: + var = strtok_r(NULL, " ", &saveptr); + val = strtok_r(NULL, " ", &saveptr); + if (!var || !val) { + cmd->type = CTRL_TYPE_ERROR; + cmd->reply = "SET incomplete"; + LOGP(DINP, LOGL_NOTICE, "SET Command incomplete\n"); + goto err; + } + cmd->variable = talloc_strdup(cmd, var); + cmd->value = talloc_strdup(cmd, val); + if (!cmd->variable || !cmd->value) + goto oom; + LOGP(DINP, LOGL_DEBUG, "Command: SET %s = %s\n", cmd->variable, cmd->value); + break; + case CTRL_TYPE_GET_REPLY: + case CTRL_TYPE_SET_REPLY: + case CTRL_TYPE_TRAP: + var = strtok_r(NULL, " ", &saveptr); + val = strtok_r(NULL, " ", &saveptr); + if (!var || !val) { + cmd->type = CTRL_TYPE_ERROR; + cmd->reply = "Trap/Reply incomplete"; + LOGP(DINP, LOGL_NOTICE, "Trap/Reply incomplete\n"); + goto err; + } + cmd->variable = talloc_strdup(cmd, var); + cmd->reply = talloc_strdup(cmd, val); + if (!cmd->variable || !cmd->reply) + goto oom; + LOGP(DINP, LOGL_DEBUG, "Command: TRAP/REPLY %s: %s\n", cmd->variable, cmd->reply); + break; + case CTRL_TYPE_ERROR: + var = strtok_r(NULL, "\0", &saveptr); + if (!var) { + cmd->reply = ""; + goto err; + } + cmd->reply = talloc_strdup(cmd, var); + if (!cmd->reply) + goto oom; + LOGP(DINP, LOGL_DEBUG, "Command: ERROR %s\n", cmd->reply); + break; + case CTRL_TYPE_UNKNOWN: + default: + cmd->type = CTRL_TYPE_ERROR; + cmd->reply = "Unknown type"; + goto err; + } + + return cmd; +oom: + cmd->type = CTRL_TYPE_ERROR; + cmd->id = "err"; + cmd->reply = "OOM"; +err: + talloc_free(cmd); + return NULL; +} + +struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd) +{ + struct msgb *msg; + char *type, *tmp; + + if (!cmd->id) + return NULL; + + msg = msgb_alloc_headroom(4096, 128, "ctrl command make"); + if (!msg) + return NULL; + + type = ctrl_cmd_type2str(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(DINP, 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); + 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(DINP, 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); + 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(DINP, 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); + 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(DINP, 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); + break; + default: + LOGP(DINP, LOGL_NOTICE, "Unknown command type %i\n", cmd->type); + goto err; + break; + } + + return msg; + +err: + msgb_free(msg); + return NULL; +} diff --git a/openbsc/src/libctrl/control_if.c b/openbsc/src/libctrl/control_if.c new file mode 100644 index 000000000..a607b10bc --- /dev/null +++ b/openbsc/src/libctrl/control_if.c @@ -0,0 +1,369 @@ +/* SNMP-like status interface + * + * (C) 2010-2011 by Daniel Willmann + * (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. + * + */ + +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include + +#include +#include + +struct ctrl_handle { + struct osmo_fd listen_fd; + struct gsm_network *gsmnet; +}; + +vector ctrl_node_vec; + +int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd) +{ + int ret; + struct msgb *msg; + + msg = ctrl_cmd_make(cmd); + if (!msg) { + LOGP(DINP, LOGL_ERROR, "Could not generate msg\n"); + return -1; + } + + ipaccess_prepend_header_ext(msg, IPAC_PROTO_EXT_CTRL); + ipaccess_prepend_header(msg, IPAC_PROTO_OSMO); + + ret = osmo_wqueue_enqueue(queue, msg); + if (ret != 0) { + LOGP(DINP, LOGL_ERROR, "Failed to enqueue the command.\n"); + msgb_free(msg); + } + return ret; +} + +int ctrl_cmd_handle(struct ctrl_cmd *cmd, void *data) +{ + char *token, *request; + int num, i, j, ret, node; + struct gsm_network *gsmnet = data; + + struct gsm_network *net = NULL; + struct gsm_bts *bts = NULL; + struct gsm_bts_trx *trx = NULL; + struct gsm_bts_trx_ts *ts = NULL; + vector vline, cmdvec, cmds_vec; + + ret = CTRL_CMD_ERROR; + cmd->reply = "Someone forgot to fill in the reply."; + cmd->node = NULL; + node = CTRL_NODE_ROOT; + + request = talloc_strdup(tall_bsc_ctx, cmd->variable); + if (!request) + goto err; + + for (i=0;inode = net; + node = CTRL_NODE_NET; + } else if (!strncmp(token, "bts", 3)) { + if (!net) + break; + num = atoi(&token[3]); + bts = gsm_bts_num(net, num); + if (!bts) + break; + cmd->node = bts; + node = CTRL_NODE_BTS; + } else if (!strncmp(token, "trx", 3)) { + if (!bts) + break; + num = atoi(&token[3]); + trx = gsm_bts_trx_num(bts, num); + if (!trx) + break; + cmd->node = trx; + node = CTRL_NODE_TRX; + } else if (!strncmp(token, "ts", 2)) { + if (!trx) + break; + num = atoi(&token[2]); + if ((num >= 0) && (num < TRX_NR_TS)) + ts = &trx->ts[num]; + if (!ts) + break; + cmd->node = ts; + node = CTRL_NODE_TS; + } else { + /* If we're here the rest must be the command */ + cmdvec = vector_init(vector_active(vline)-i); + for (j=i; jreply = "Command not found"; + vector_free(cmdvec); + break; + } + + ret = ctrl_cmd_exec(cmdvec, cmd, cmds_vec, data); + + vector_free(cmdvec); + break; + } + } + + cmd_free_strvec(vline); + +err: + if (ret == CTRL_CMD_ERROR) + cmd->type = CTRL_TYPE_ERROR; + return ret; +} + +static void control_close_conn(struct ctrl_connection *ccon) +{ + close(ccon->write_queue.bfd.fd); + osmo_fd_unregister(&ccon->write_queue.bfd); + if (ccon->closed_cb) + ccon->closed_cb(ccon); + talloc_free(ccon); +} + +static int handle_control_read(struct osmo_fd * bfd) +{ + int ret = -1, error; + struct osmo_wqueue *queue; + struct ctrl_connection *ccon; + struct ipaccess_head *iph; + struct ipaccess_head_ext *iph_ext; + struct msgb *msg; + 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); + + msg = ipaccess_read_msg(bfd, &error); + + if (!msg) { + if (error == 0) + LOGP(DINP, LOGL_INFO, "The control connection was closed\n"); + else + LOGP(DINP, LOGL_ERROR, "Failed to parse ip access message: %d\n", error); + + goto err; + } + + if (msg->len < sizeof(*iph) + sizeof(*iph_ext)) { + LOGP(DINP, LOGL_ERROR, "The message is too short.\n"); + goto err; + } + + iph = (struct ipaccess_head *) msg->data; + if (iph->proto != IPAC_PROTO_OSMO) { + LOGP(DINP, LOGL_ERROR, "Protocol mismatch. We got 0x%x\n", iph->proto); + goto err; + } + + iph_ext = (struct ipaccess_head_ext *) iph->data; + if (iph_ext->proto != IPAC_PROTO_EXT_CTRL) { + LOGP(DINP, LOGL_ERROR, "Extended protocol mismatch. We got 0x%x\n", iph_ext->proto); + goto err; + } + + msg->l2h = iph_ext->data; + + cmd = ctrl_cmd_parse(ccon, msg); + + if (cmd) { + cmd->ccon = ccon; + if (ctrl_cmd_handle(cmd, ctrl->gsmnet) != CTRL_CMD_HANDLED) { + ctrl_cmd_send(queue, cmd); + talloc_free(cmd); + } + } else { + cmd = talloc_zero(ccon, struct ctrl_cmd); + if (!cmd) + goto err; + LOGP(DINP, LOGL_ERROR, "Command parser error.\n"); + 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) + LOGP(DINP, LOGL_ERROR, "Failed to write message to the control connection.\n"); + + return rc; +} + +static struct ctrl_connection *ctrl_connection_alloc(void *ctx) +{ + 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); + return ccon; +} + +static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what) +{ + int ret, fd, on; + 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; + } + LOGP(DINP, LOGL_INFO, "accept()ed new control connection from %s\n", + inet_ntoa(sa.sin_addr)); + + on = 1; + ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); + if (ret != 0) { + LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); + close(fd); + return ret; + } + ccon = ctrl_connection_alloc(listen_bfd->data); + if (!ccon) { + LOGP(DINP, LOGL_ERROR, "Failed to allocate.\n"); + close(fd); + return -1; + } + + ccon->write_queue.bfd.data = listen_bfd->data; + ccon->write_queue.bfd.fd = fd; + ccon->write_queue.bfd.when = BSC_FD_READ; + ccon->write_queue.read_cb = handle_control_read; + ccon->write_queue.write_cb = control_write_cb; + + ret = osmo_fd_register(&ccon->write_queue.bfd); + if (ret < 0) { + LOGP(DINP, LOGL_ERROR, "Could not register FD.\n"); + close(ccon->write_queue.bfd.fd); + talloc_free(ccon); + } + + return ret; +} + +int controlif_setup(struct gsm_network *gsmnet, uint16_t port) +{ + int ret; + struct ctrl_handle *ctrl; + + ctrl = talloc_zero(tall_bsc_ctx, struct ctrl_handle); + if (!ctrl) + return -ENOMEM; + + ctrl->gsmnet = gsmnet; + + ctrl_node_vec = vector_init(5); + if (!ctrl_node_vec) + return -ENOMEM; + + /* Listen for control connections */ + ret = make_sock(&ctrl->listen_fd, IPPROTO_TCP, 0, port, + 0, listen_fd_cb, ctrl); + if (ret < 0) { + talloc_free(ctrl); + return ret; + } + + return ret; +} -- cgit v1.2.3