FreeCalypso > hg > osmo-playpen
annotate ctrl-client/client.c @ 22:7bfe3e21eaae default tip
top Makefile: add ctrl-client
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 17 Dec 2023 08:53:35 +0000 |
parents | 80849380395d |
children |
rev | line source |
---|---|
21
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* Generic client structure and related helpers */ |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 /* (C) 2018 by Harald Welte <laforge@gnumonks.org> |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * (C) 2019 by sysmocom - s.f.m.c. GmbH. |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 * All Rights Reserved. |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 * |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 * SPDX-License-Identifier: GPL-2.0+ |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 * |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 * This program is free software; you can redistribute it and/or modify |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 * it under the terms of the GNU General Public License as published by |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 * the Free Software Foundation; either version 2 of the License, or |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 * (at your option) any later version. |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 * |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 * This program is distributed in the hope that it will be useful, |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 * GNU General Public License for more details. |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 */ |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 #include <stdbool.h> |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 #include <string.h> |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 #include <talloc.h> |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 #include <osmocom/core/utils.h> |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 #include <osmocom/netif/stream.h> |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 #include "client.h" |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 bool match_config(const struct host_cfg *cfg, const char *match, enum match_kind k) |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 { |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 bool m_name = (strcmp(match, cfg->name) == 0), |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 m_host = (strcmp(match, cfg->remote_host) == 0); |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 switch (k) { |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 case MATCH_NAME: |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 return m_name; |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 case MATCH_HOST: |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 return m_host; |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 case MATCH_EITHER: |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 return m_name | m_host; |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 case MATCH_BOTH: |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 return m_name & m_host; |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 default: |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 return false; |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 } |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 return false; |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 struct host_cfg *host_cfg_alloc(void *ctx, const char *name, const char *host, uint16_t port) |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 { |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 struct host_cfg *cfg = talloc_zero(ctx, struct host_cfg); |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 if (!cfg) |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 return NULL; |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 cfg->name = talloc_strdup(cfg, name); |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 cfg->remote_host = talloc_strdup(cfg, host); |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 cfg->remote_port = port; |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 return cfg; |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 } |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 char *make_authority(void *ctx, const struct host_cfg *cfg) |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 { |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 if (!cfg->remote_host) |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 return NULL; |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 return talloc_asprintf(ctx, "%s:%u", cfg->remote_host, cfg->remote_port); |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 } |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 struct osmo_stream_cli *make_tcp_client(struct host_cfg *cfg) |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 { |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 struct osmo_stream_cli *cl = osmo_stream_cli_create(cfg); |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 if (cl) { |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 osmo_stream_cli_set_addr(cl, cfg->remote_host); |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 osmo_stream_cli_set_port(cl, cfg->remote_port); |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 } |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 return cl; |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 } |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 void update_name(struct host_cfg *cfg, const char *new_name) |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 { |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 osmo_talloc_replace_string(cfg, (char **)&cfg->name, new_name); |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 } |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 void update_host(struct host_cfg *cfg, const char *new_host) |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 { |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 osmo_talloc_replace_string(cfg, (char **)&cfg->remote_host, new_host); |
80849380395d
osmo-ctrl-client compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 } |