blob: 6b37fc654392b8cca77e1410ffbf01066361f687 [file] [log] [blame]
Max5d42b8e2019-02-07 17:28:01 +01001/* Generic client structure and related helpers */
2
3/* (C) 2018 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2019 by sysmocom - s.f.m.c. GmbH.
5 * All Rights Reserved.
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 * MA 02110-1301, USA.
23 */
24
25#include <stdbool.h>
26#include <string.h>
27#include <talloc.h>
28
29#include <osmocom/core/utils.h>
30
31#include "client.h"
32
33bool match_config(const struct host_cfg *cfg, const char *match, enum match_kind k)
34{
35 bool m_name = (strcmp(match, cfg->name) == 0),
36 m_host = (strcmp(match, cfg->remote_host) == 0);
37
38 switch (k) {
39 case MATCH_NAME:
40 return m_name;
41 case MATCH_HOST:
42 return m_host;
43 case MATCH_EITHER:
44 return m_name | m_host;
45 case MATCH_BOTH:
46 return m_name & m_host;
47 default:
48 return false;
49 }
50
51 return false;
52}
53
54struct host_cfg *host_cfg_alloc(void *ctx, const char *name, const char *host, uint16_t port)
55{
56 struct host_cfg *cfg = talloc_zero(ctx, struct host_cfg);
57 if (!cfg)
58 return NULL;
59
60 cfg->name = talloc_strdup(cfg, name);
61 cfg->remote_host = talloc_strdup(cfg, host);
62 cfg->remote_port = port;
63
64 return cfg;
65}
66
67char *make_authority(void *ctx, const struct host_cfg *cfg)
68{
69 if (!cfg->remote_host)
70 return NULL;
71
72 return talloc_asprintf(ctx, "%s:%u", cfg->remote_host, cfg->remote_port);
73}