mirror of https://gerrit.osmocom.org/osmo-sysmon
Introduce generic host config and related helpers
Add generic host config struct and related helpers for TCP-based probes and use them for ctrl probe. This will be used in follow-up patch for OpenVPN probe as well. Change-Id: Ie321655a92cdbefbfaa056ac0d583397c83beccb
This commit is contained in:
parent
9ce5bf90bb
commit
5d42b8ec98
|
@ -21,7 +21,7 @@ bin_PROGRAMS = \
|
||||||
$(NULL)
|
$(NULL)
|
||||||
|
|
||||||
noinst_LTLIBRARIES = libintern.la
|
noinst_LTLIBRARIES = libintern.la
|
||||||
libintern_la_SOURCES = simple_ctrl.c
|
libintern_la_SOURCES = simple_ctrl.c client.c
|
||||||
libintern_la_LIBADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS)
|
libintern_la_LIBADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS)
|
||||||
|
|
||||||
osmo_sysmon_CFLAGS = $(LIBMNL_CFLAGS) $(LIBOSMOVTY_CFLAGS) $(LIBOPING_CFLAGS) $(AM_CFLAGS)
|
osmo_sysmon_CFLAGS = $(LIBMNL_CFLAGS) $(LIBOSMOVTY_CFLAGS) $(LIBOPING_CFLAGS) $(AM_CFLAGS)
|
||||||
|
@ -44,6 +44,7 @@ osmo_sysmon_SOURCES = \
|
||||||
|
|
||||||
noinst_HEADERS = \
|
noinst_HEADERS = \
|
||||||
osysmon.h \
|
osysmon.h \
|
||||||
|
client.h \
|
||||||
simple_ctrl.h \
|
simple_ctrl.h \
|
||||||
value_node.h \
|
value_node.h \
|
||||||
$(NULL)
|
$(NULL)
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
/* Generic client structure and related helpers */
|
||||||
|
|
||||||
|
/* (C) 2018 by Harald Welte <laforge@gnumonks.org>
|
||||||
|
* (C) 2019 by sysmocom - s.f.m.c. GmbH.
|
||||||
|
* All Rights Reserved.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-2.0+
|
||||||
|
*
|
||||||
|
* 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 <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <talloc.h>
|
||||||
|
|
||||||
|
#include <osmocom/core/utils.h>
|
||||||
|
|
||||||
|
#include "client.h"
|
||||||
|
|
||||||
|
bool match_config(const struct host_cfg *cfg, const char *match, enum match_kind k)
|
||||||
|
{
|
||||||
|
bool m_name = (strcmp(match, cfg->name) == 0),
|
||||||
|
m_host = (strcmp(match, cfg->remote_host) == 0);
|
||||||
|
|
||||||
|
switch (k) {
|
||||||
|
case MATCH_NAME:
|
||||||
|
return m_name;
|
||||||
|
case MATCH_HOST:
|
||||||
|
return m_host;
|
||||||
|
case MATCH_EITHER:
|
||||||
|
return m_name | m_host;
|
||||||
|
case MATCH_BOTH:
|
||||||
|
return m_name & m_host;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct host_cfg *host_cfg_alloc(void *ctx, const char *name, const char *host, uint16_t port)
|
||||||
|
{
|
||||||
|
struct host_cfg *cfg = talloc_zero(ctx, struct host_cfg);
|
||||||
|
if (!cfg)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
cfg->name = talloc_strdup(cfg, name);
|
||||||
|
cfg->remote_host = talloc_strdup(cfg, host);
|
||||||
|
cfg->remote_port = port;
|
||||||
|
|
||||||
|
return cfg;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *make_authority(void *ctx, const struct host_cfg *cfg)
|
||||||
|
{
|
||||||
|
if (!cfg->remote_host)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return talloc_asprintf(ctx, "%s:%u", cfg->remote_host, cfg->remote_port);
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
enum match_kind {
|
||||||
|
MATCH_NAME,
|
||||||
|
MATCH_HOST,
|
||||||
|
MATCH_BOTH,
|
||||||
|
MATCH_EITHER,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* a client config */
|
||||||
|
struct host_cfg {
|
||||||
|
/* name of this client */
|
||||||
|
const char *name;
|
||||||
|
/* remote host/IP */
|
||||||
|
const char *remote_host;
|
||||||
|
/* remote port */
|
||||||
|
uint16_t remote_port;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct host_cfg *host_cfg_alloc(void *ctx, const char *name, const char *host, uint16_t port);
|
||||||
|
bool match_config(const struct host_cfg *cfg, const char *match, enum match_kind k);
|
||||||
|
char *make_authority(void *ctx, const struct host_cfg *cfg);
|
|
@ -26,6 +26,7 @@
|
||||||
#include <osmocom/vty/vty.h>
|
#include <osmocom/vty/vty.h>
|
||||||
#include <osmocom/vty/command.h>
|
#include <osmocom/vty/command.h>
|
||||||
|
|
||||||
|
#include "client.h"
|
||||||
#include "osysmon.h"
|
#include "osysmon.h"
|
||||||
#include "simple_ctrl.h"
|
#include "simple_ctrl.h"
|
||||||
#include "value_node.h"
|
#include "value_node.h"
|
||||||
|
@ -38,7 +39,7 @@
|
||||||
struct ctrl_client {
|
struct ctrl_client {
|
||||||
/* links to osysmon.ctrl_clients */
|
/* links to osysmon.ctrl_clients */
|
||||||
struct llist_head list;
|
struct llist_head list;
|
||||||
struct ctrl_cfg cfg;
|
struct host_cfg *cfg;
|
||||||
struct simple_ctrl_handle *sch;
|
struct simple_ctrl_handle *sch;
|
||||||
/* list of ctrl_client_get_var objects */
|
/* list of ctrl_client_get_var objects */
|
||||||
struct llist_head get_vars;
|
struct llist_head get_vars;
|
||||||
|
@ -62,7 +63,7 @@ static struct ctrl_client *ctrl_client_find(struct osysmon_state *os, const char
|
||||||
{
|
{
|
||||||
struct ctrl_client *cc;
|
struct ctrl_client *cc;
|
||||||
llist_for_each_entry(cc, &os->ctrl_clients, list) {
|
llist_for_each_entry(cc, &os->ctrl_clients, list) {
|
||||||
if (!strcmp(name, cc->cfg.name))
|
if (match_config(cc->cfg, name, MATCH_NAME))
|
||||||
return cc;
|
return cc;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -79,9 +80,13 @@ static struct ctrl_client *ctrl_client_create(struct osysmon_state *os, const ch
|
||||||
cc = talloc_zero(os, struct ctrl_client);
|
cc = talloc_zero(os, struct ctrl_client);
|
||||||
if (!cc)
|
if (!cc)
|
||||||
return NULL;
|
return NULL;
|
||||||
cc->cfg.name = talloc_strdup(cc, name);
|
|
||||||
cc->cfg.remote_host = talloc_strdup(cc, host);
|
cc->cfg = host_cfg_alloc(cc, name, host, port);
|
||||||
cc->cfg.remote_port = port;
|
if (!cc->cfg) {
|
||||||
|
talloc_free(cc);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
INIT_LLIST_HEAD(&cc->get_vars);
|
INIT_LLIST_HEAD(&cc->get_vars);
|
||||||
llist_add_tail(&cc->list, &os->ctrl_clients);
|
llist_add_tail(&cc->list, &os->ctrl_clients);
|
||||||
/* FIXME */
|
/* FIXME */
|
||||||
|
@ -156,10 +161,10 @@ DEFUN(cfg_ctrl_client, cfg_ctrl_client_cmd,
|
||||||
struct ctrl_client *cc;
|
struct ctrl_client *cc;
|
||||||
cc = ctrl_client_find(g_oss, argv[0]);
|
cc = ctrl_client_find(g_oss, argv[0]);
|
||||||
if (cc) {
|
if (cc) {
|
||||||
if ((strcmp(cc->cfg.remote_host, argv[1])) ||
|
if ((strcmp(cc->cfg->remote_host, argv[1])) ||
|
||||||
(cc->cfg.remote_port != atoi(argv[2]))) {
|
(cc->cfg->remote_port != atoi(argv[2]))) {
|
||||||
vty_out(vty, "Client %s has different IP/port, please remove it first%s",
|
vty_out(vty, "Client %s has different IP/port, please remove it first%s",
|
||||||
cc->cfg.name, VTY_NEWLINE);
|
cc->cfg->name, VTY_NEWLINE);
|
||||||
return CMD_WARNING;
|
return CMD_WARNING;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
|
@ -215,8 +220,8 @@ DEFUN(cfg_ctrlc_no_get_var, cfg_ctrlc_no_get_var_cmd,
|
||||||
static void write_one_ctrl_client(struct vty *vty, struct ctrl_client *cc)
|
static void write_one_ctrl_client(struct vty *vty, struct ctrl_client *cc)
|
||||||
{
|
{
|
||||||
struct ctrl_client_get_var *ccgv;
|
struct ctrl_client_get_var *ccgv;
|
||||||
vty_out(vty, "ctrl-client %s %s %u%s", cc->cfg.name,
|
vty_out(vty, "ctrl-client %s %s %u%s", cc->cfg->name,
|
||||||
cc->cfg.remote_host, cc->cfg.remote_port, VTY_NEWLINE);
|
cc->cfg->remote_host, cc->cfg->remote_port, VTY_NEWLINE);
|
||||||
llist_for_each_entry(ccgv, &cc->get_vars, list) {
|
llist_for_each_entry(ccgv, &cc->get_vars, list) {
|
||||||
vty_out(vty, " get-variable %s%s", ccgv->cfg.name, VTY_NEWLINE);
|
vty_out(vty, " get-variable %s%s", ccgv->cfg.name, VTY_NEWLINE);
|
||||||
if (ccgv->cfg.display_name)
|
if (ccgv->cfg.display_name)
|
||||||
|
@ -259,11 +264,11 @@ int osysmon_ctrl_init()
|
||||||
static int ctrl_client_poll(struct ctrl_client *cc, struct value_node *parent)
|
static int ctrl_client_poll(struct ctrl_client *cc, struct value_node *parent)
|
||||||
{
|
{
|
||||||
struct ctrl_client_get_var *ccgv;
|
struct ctrl_client_get_var *ccgv;
|
||||||
struct value_node *vn_clnt = value_node_add(parent, cc->cfg.name, NULL);
|
struct value_node *vn_clnt = value_node_add(parent, cc->cfg->name, NULL);
|
||||||
|
|
||||||
/* attempt to re-connect */
|
/* attempt to re-connect */
|
||||||
if (!cc->sch)
|
if (!cc->sch)
|
||||||
cc->sch = simple_ctrl_open(cc, cc->cfg.remote_host, cc->cfg.remote_port, 1000);
|
cc->sch = simple_ctrl_open(cc, cc->cfg->remote_host, cc->cfg->remote_port, 1000);
|
||||||
/* abort, if that failed */
|
/* abort, if that failed */
|
||||||
if (!cc->sch) {
|
if (!cc->sch) {
|
||||||
return -1;
|
return -1;
|
||||||
|
|
|
@ -35,10 +35,11 @@
|
||||||
#include <osmocom/gsm/ipa.h>
|
#include <osmocom/gsm/ipa.h>
|
||||||
#include <osmocom/gsm/protocol/ipaccess.h>
|
#include <osmocom/gsm/protocol/ipaccess.h>
|
||||||
|
|
||||||
|
#include "client.h"
|
||||||
#include "simple_ctrl.h"
|
#include "simple_ctrl.h"
|
||||||
|
|
||||||
#define CTRL_ERR(cfg, fmt, args...) \
|
#define CTRL_ERR(sch, fmt, args...) \
|
||||||
fprintf(stderr, "CTRL %s:%u error: " fmt, cfg.remote_host, cfg.remote_port, ##args)
|
fprintf(stderr, "CTRL %s error: " fmt, make_authority(sch, &sch->cfg), ##args)
|
||||||
|
|
||||||
/***********************************************************************
|
/***********************************************************************
|
||||||
* blocking I/O with timeout helpers
|
* blocking I/O with timeout helpers
|
||||||
|
@ -101,7 +102,7 @@ struct simple_ctrl_handle {
|
||||||
int fd;
|
int fd;
|
||||||
uint32_t next_id;
|
uint32_t next_id;
|
||||||
uint32_t tout_msec;
|
uint32_t tout_msec;
|
||||||
struct ctrl_cfg cfg;
|
struct host_cfg cfg;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct simple_ctrl_handle *simple_ctrl_open(void *ctx, const char *host, uint16_t dport,
|
struct simple_ctrl_handle *simple_ctrl_open(void *ctx, const char *host, uint16_t dport,
|
||||||
|
@ -122,7 +123,7 @@ struct simple_ctrl_handle *simple_ctrl_open(void *ctx, const char *host, uint16_
|
||||||
fd = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, host, dport,
|
fd = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, host, dport,
|
||||||
OSMO_SOCK_F_CONNECT | OSMO_SOCK_F_NONBLOCK);
|
OSMO_SOCK_F_CONNECT | OSMO_SOCK_F_NONBLOCK);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
CTRL_ERR(sch->cfg, "connecting socket: %s\n", strerror(errno));
|
CTRL_ERR(sch, "connecting socket: %s\n", strerror(errno));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,17 +132,17 @@ struct simple_ctrl_handle *simple_ctrl_open(void *ctx, const char *host, uint16_
|
||||||
FD_SET(fd, &writeset);
|
FD_SET(fd, &writeset);
|
||||||
rc = select(fd+1, NULL, &writeset, NULL, timeval_from_msec(tout_msec));
|
rc = select(fd+1, NULL, &writeset, NULL, timeval_from_msec(tout_msec));
|
||||||
if (rc == 0) {
|
if (rc == 0) {
|
||||||
CTRL_ERR(sch->cfg, "timeout during connect\n");
|
CTRL_ERR(sch, "timeout during connect\n");
|
||||||
goto out_close;
|
goto out_close;
|
||||||
}
|
}
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
CTRL_ERR(sch->cfg, "error connecting socket: %s\n", strerror(errno));
|
CTRL_ERR(sch, "error connecting socket: %s\n", strerror(errno));
|
||||||
goto out_close;
|
goto out_close;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set FD blocking again */
|
/* set FD blocking again */
|
||||||
if (ioctl(fd, FIONBIO, (unsigned char *)&off) < 0) {
|
if (ioctl(fd, FIONBIO, (unsigned char *)&off) < 0) {
|
||||||
CTRL_ERR(sch->cfg, "cannot set socket blocking: %s\n", strerror(errno));
|
CTRL_ERR(sch, "cannot set socket blocking: %s\n", strerror(errno));
|
||||||
goto out_close;
|
goto out_close;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,10 +174,10 @@ static struct msgb *simple_ipa_receive(struct simple_ctrl_handle *sch)
|
||||||
|
|
||||||
rc = read_timeout(sch->fd, (uint8_t *) &hh, sizeof(hh), sch->tout_msec);
|
rc = read_timeout(sch->fd, (uint8_t *) &hh, sizeof(hh), sch->tout_msec);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
CTRL_ERR(sch->cfg, "read(): %d\n", rc);
|
CTRL_ERR(sch, "read(): %d\n", rc);
|
||||||
return NULL;
|
return NULL;
|
||||||
} else if (rc < sizeof(hh)) {
|
} else if (rc < sizeof(hh)) {
|
||||||
CTRL_ERR(sch->cfg, "short read (header)\n");
|
CTRL_ERR(sch, "short read (header)\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
len = ntohs(hh.len);
|
len = ntohs(hh.len);
|
||||||
|
@ -190,7 +191,7 @@ static struct msgb *simple_ipa_receive(struct simple_ctrl_handle *sch)
|
||||||
resp->l2h = resp->tail;
|
resp->l2h = resp->tail;
|
||||||
rc = read(sch->fd, resp->l2h, len);
|
rc = read(sch->fd, resp->l2h, len);
|
||||||
if (rc < len) {
|
if (rc < len) {
|
||||||
CTRL_ERR(sch->cfg, "short read (payload)\n");
|
CTRL_ERR(sch, "short read (payload)\n");
|
||||||
msgb_free(resp);
|
msgb_free(resp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -222,7 +223,7 @@ struct msgb *simple_ctrl_receive(struct simple_ctrl_handle *sch)
|
||||||
*tmp = '\0';
|
*tmp = '\0';
|
||||||
return resp;
|
return resp;
|
||||||
} else {
|
} else {
|
||||||
CTRL_ERR(sch->cfg, "unknown IPA message %s\n", msgb_hexdump(resp));
|
CTRL_ERR(sch, "unknown IPA message %s\n", msgb_hexdump(resp));
|
||||||
msgb_free(resp);
|
msgb_free(resp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -237,10 +238,10 @@ static int simple_ctrl_send(struct simple_ctrl_handle *sch, struct msgb *msg)
|
||||||
|
|
||||||
rc = write_timeout(sch->fd, msg->data, msg->len, sch->tout_msec);
|
rc = write_timeout(sch->fd, msg->data, msg->len, sch->tout_msec);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
CTRL_ERR(sch->cfg, "write(): %d\n", rc);
|
CTRL_ERR(sch, "write(): %d\n", rc);
|
||||||
return rc;
|
return rc;
|
||||||
} else if (rc < msg->len) {
|
} else if (rc < msg->len) {
|
||||||
CTRL_ERR(sch->cfg, "short write\n");
|
CTRL_ERR(sch, "short write\n");
|
||||||
msgb_free(msg);
|
msgb_free(msg);
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
|
@ -291,7 +292,7 @@ char *simple_ctrl_get(struct simple_ctrl_handle *sch, const char *var)
|
||||||
free(rx_var);
|
free(rx_var);
|
||||||
free(rx_val);
|
free(rx_val);
|
||||||
} else {
|
} else {
|
||||||
CTRL_ERR(sch->cfg, "GET(%s) results in '%s'\n", var, (char *)msgb_l2(resp));
|
CTRL_ERR(sch, "GET(%s) results in '%s'\n", var, (char *)msgb_l2(resp));
|
||||||
}
|
}
|
||||||
|
|
||||||
msgb_free(resp);
|
msgb_free(resp);
|
||||||
|
@ -329,7 +330,7 @@ int simple_ctrl_set(struct simple_ctrl_handle *sch, const char *var, const char
|
||||||
free(rx_var);
|
free(rx_var);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
CTRL_ERR(sch->cfg, "SET(%s=%s) results in '%s'\n", var, val, (char *) msgb_l2(resp));
|
CTRL_ERR(sch, "SET(%s=%s) results in '%s'\n", var, val, (char *) msgb_l2(resp));
|
||||||
}
|
}
|
||||||
|
|
||||||
msgb_free(resp);
|
msgb_free(resp);
|
||||||
|
|
|
@ -2,16 +2,6 @@
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
/* a CTRL client config */
|
|
||||||
struct ctrl_cfg {
|
|
||||||
/* name of this CTRL client */
|
|
||||||
const char *name;
|
|
||||||
/* remote host/IP */
|
|
||||||
const char *remote_host;
|
|
||||||
/* remote CTRL port */
|
|
||||||
uint16_t remote_port;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct simple_ctrl_handle;
|
struct simple_ctrl_handle;
|
||||||
|
|
||||||
struct simple_ctrl_handle *simple_ctrl_open(void *ctx, const char *host, uint16_t dport,
|
struct simple_ctrl_handle *simple_ctrl_open(void *ctx, const char *host, uint16_t dport,
|
||||||
|
|
Loading…
Reference in New Issue