Max | 5d42b8e | 2019-02-07 17:28:01 +0100 | [diff] [blame] | 1 | /* 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. |
Max | 5d42b8e | 2019-02-07 17:28:01 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <stdbool.h> |
| 21 | #include <string.h> |
| 22 | #include <talloc.h> |
| 23 | |
| 24 | #include <osmocom/core/utils.h> |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 25 | #include <osmocom/netif/stream.h> |
Max | 5d42b8e | 2019-02-07 17:28:01 +0100 | [diff] [blame] | 26 | |
| 27 | #include "client.h" |
| 28 | |
| 29 | bool match_config(const struct host_cfg *cfg, const char *match, enum match_kind k) |
| 30 | { |
| 31 | bool m_name = (strcmp(match, cfg->name) == 0), |
| 32 | m_host = (strcmp(match, cfg->remote_host) == 0); |
| 33 | |
| 34 | switch (k) { |
| 35 | case MATCH_NAME: |
| 36 | return m_name; |
| 37 | case MATCH_HOST: |
| 38 | return m_host; |
| 39 | case MATCH_EITHER: |
| 40 | return m_name | m_host; |
| 41 | case MATCH_BOTH: |
| 42 | return m_name & m_host; |
| 43 | default: |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | struct host_cfg *host_cfg_alloc(void *ctx, const char *name, const char *host, uint16_t port) |
| 51 | { |
| 52 | struct host_cfg *cfg = talloc_zero(ctx, struct host_cfg); |
| 53 | if (!cfg) |
| 54 | return NULL; |
| 55 | |
| 56 | cfg->name = talloc_strdup(cfg, name); |
| 57 | cfg->remote_host = talloc_strdup(cfg, host); |
| 58 | cfg->remote_port = port; |
| 59 | |
| 60 | return cfg; |
| 61 | } |
| 62 | |
| 63 | char *make_authority(void *ctx, const struct host_cfg *cfg) |
| 64 | { |
| 65 | if (!cfg->remote_host) |
| 66 | return NULL; |
| 67 | |
| 68 | return talloc_asprintf(ctx, "%s:%u", cfg->remote_host, cfg->remote_port); |
| 69 | } |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 70 | |
| 71 | struct osmo_stream_cli *make_tcp_client(struct host_cfg *cfg) |
| 72 | { |
| 73 | struct osmo_stream_cli *cl = osmo_stream_cli_create(cfg); |
| 74 | if (cl) { |
| 75 | osmo_stream_cli_set_addr(cl, cfg->remote_host); |
| 76 | osmo_stream_cli_set_port(cl, cfg->remote_port); |
| 77 | } |
| 78 | |
| 79 | return cl; |
| 80 | } |
| 81 | |
| 82 | void update_name(struct host_cfg *cfg, const char *new_name) |
| 83 | { |
| 84 | osmo_talloc_replace_string(cfg, (char **)&cfg->name, new_name); |
| 85 | } |
| 86 | |
| 87 | void update_host(struct host_cfg *cfg, const char *new_host) |
| 88 | { |
| 89 | osmo_talloc_replace_string(cfg, (char **)&cfg->remote_host, new_host); |
| 90 | } |