Add generic host config and related helpers

This will be used in follow-up patches as a generic way to store remote
host related configuration data by several TCP-based probes.

Change-Id: Ie321655a92cdbefbfaa056ac0d583397c83beccb
This commit is contained in:
Max 2019-01-29 15:53:11 +01:00
parent 8066a41e78
commit ab26c18fb5
5 changed files with 134 additions and 2 deletions

View File

@ -42,6 +42,7 @@ PKG_CHECK_MODULES(LIBOSMOCORE, libosmocore >= 0.11.0)
PKG_CHECK_MODULES(LIBOSMOVTY, libosmovty >= 0.11.0)
PKG_CHECK_MODULES(LIBOSMOCTRL, libosmoctrl >= 0.11.0)
PKG_CHECK_MODULES(LIBOSMOGSM, libosmogsm >= 0.11.0)
PKG_CHECK_MODULES(LIBOSMONETIF, libosmo-netif >= 0.4.0)
PKG_CHECK_MODULES(LIBMNL, libmnl)
dnl FIXME: bump to 1.10.0 once it's available on build slaves and remove workaround from osysmon_ping.c
PKG_CHECK_MODULES(LIBOPING, liboping >= 1.9.0)

View File

@ -24,6 +24,9 @@ verify_value_string_arrays_are_terminated.py $(find . -name "*.[hc]")
export PKG_CONFIG_PATH="$inst/lib/pkgconfig:$PKG_CONFIG_PATH"
export LD_LIBRARY_PATH="$inst/lib"
export PATH="$inst/bin:$PATH"
osmo-build-dep.sh libosmo-netif "" '--disable-doxygen'
set +x
echo

View File

@ -9,6 +9,7 @@ AM_CFLAGS = \
-Wall \
$(LIBOSMOCORE_CFLAGS) \
$(LIBOSMOGSM_CFLAGS) \
$(LIBOSMONETIF_CFLAGS) \
$(NULL)
AM_LDFLAGS = \
@ -21,8 +22,8 @@ bin_PROGRAMS = \
$(NULL)
noinst_LTLIBRARIES = libintern.la
libintern_la_SOURCES = simple_ctrl.c
libintern_la_LIBADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS)
libintern_la_SOURCES = simple_ctrl.c client.c
libintern_la_LIBADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS) $(LIBOSMONETIF_LIBS)
osmo_sysmon_CFLAGS = $(LIBMNL_CFLAGS) $(LIBOSMOVTY_CFLAGS) $(LIBOPING_CFLAGS) $(AM_CFLAGS)
@ -44,6 +45,7 @@ osmo_sysmon_SOURCES = \
noinst_HEADERS = \
osysmon.h \
client.h \
simple_ctrl.h \
value_node.h \
$(NULL)

96
src/client.c Normal file
View File

@ -0,0 +1,96 @@
/* 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 <osmocom/netif/stream.h>
#include "client.h"
#define MATCH(a, b) (strcmp(a, b) != 0) ? false : true
bool match_config(const struct host_cfg *cfg, const char *match, enum match_kind k)
{
bool m_name = MATCH(match, cfg->name), m_host = MATCH(match, cfg->remote_host);
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 *make_config(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;
}
void update_name(struct host_cfg *cfg, const char *new_name)
{
osmo_talloc_replace_string(cfg, (char **)&cfg->name, new_name);
}
void update_host(struct host_cfg *cfg, const char *new_host)
{
osmo_talloc_replace_string(cfg, (char **)&cfg->remote_host, new_host);
}
char *get_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);
}
struct osmo_stream_cli *make_tcp_client(struct host_cfg *cfg)
{
struct osmo_stream_cli *cl = osmo_stream_cli_create(cfg);
if (cl) {
osmo_stream_cli_set_addr(cl, cfg->remote_host);
osmo_stream_cli_set_port(cl, cfg->remote_port);
}
return cl;
}

30
src/client.h Normal file
View File

@ -0,0 +1,30 @@
#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 *make_config(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);
void update_name(struct host_cfg *cfg, const char *new_name);
void update_host(struct host_cfg *cfg, const char *new_host);
struct osmo_stream_cli *make_tcp_client(struct host_cfg *cfg);
char *get_authority(void *ctx, const struct host_cfg *cfg);