annotate ctrl-client/simple_ctrl.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 2230a763713f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /* Simple, blocking client API against the Osmocom CTRL interface */
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 /* (C) 2018 by Harald Welte <laforge@gnumonks.org>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * All Rights Reserved.
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 *
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * SPDX-License-Identifier: GPL-2.0+
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 *
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 * (at your option) any later version.
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 *
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 * GNU General Public License for more details.
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 */
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 #include <unistd.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 #include <stdint.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 #include <talloc.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 #include <string.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 #include <errno.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 #include <sys/ioctl.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 #include <netinet/in.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 #include <osmocom/core/msgb.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 #include <osmocom/core/socket.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 #include <osmocom/gsm/ipa.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 #include <osmocom/gsm/protocol/ipaccess.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 #include "client.h"
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 #include "simple_ctrl.h"
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 #define CTRL_ERR(sch, fmt, args...) \
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 fprintf(stderr, "CTRL %s error: " fmt, make_authority(sch, &sch->cfg), ##args)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 /***********************************************************************
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 * blocking I/O with timeout helpers
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 ***********************************************************************/
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 static struct timeval *timeval_from_msec(uint32_t tout_msec)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 static struct timeval tout;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 if (tout_msec == 0)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 return NULL;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 tout.tv_sec = tout_msec/1000;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 tout.tv_usec = (tout_msec%1000)*1000;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 return &tout;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 static ssize_t read_timeout(int fd, void *buf, size_t count, uint32_t tout_msec)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 fd_set readset;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 int rc;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 FD_ZERO(&readset);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 FD_SET(fd, &readset);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 rc = select(fd+1, &readset, NULL, NULL, timeval_from_msec(tout_msec));
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 if (rc < 0)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 return rc;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 if (FD_ISSET(fd, &readset))
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 return read(fd, buf, count);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 return -ETIMEDOUT;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 static ssize_t write_timeout(int fd, const void *buf, size_t count, uint32_t tout_msec)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 fd_set writeset;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 int rc;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 FD_ZERO(&writeset);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 FD_SET(fd, &writeset);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 rc = select(fd+1, NULL, &writeset, NULL, timeval_from_msec(tout_msec));
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 if (rc < 0)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 return rc;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 if (FD_ISSET(fd, &writeset))
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 return write(fd, buf, count);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 return -ETIMEDOUT;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 /***********************************************************************
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 * actual CTRL client API
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 ***********************************************************************/
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 struct simple_ctrl_handle {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 int fd;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 uint32_t next_id;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 uint32_t tout_msec;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 struct host_cfg cfg;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 };
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 struct simple_ctrl_handle *simple_ctrl_open(void *ctx, const char *host, uint16_t dport,
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 uint32_t tout_msec)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 struct simple_ctrl_handle *sch = talloc_zero(ctx, struct simple_ctrl_handle);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 fd_set writeset;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 int off = 0;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 int rc, fd;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 if (!sch)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 return NULL;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 sch->cfg.name = talloc_strdup(sch, "simple-ctrl");
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 sch->cfg.remote_host = talloc_strdup(sch, host);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 sch->cfg.remote_port = dport;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 fd = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, host, dport,
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 OSMO_SOCK_F_CONNECT | OSMO_SOCK_F_NONBLOCK);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 if (fd < 0) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 CTRL_ERR(sch, "connecting socket: %s\n", strerror(errno));
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 return NULL;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 /* wait until connect (or timeout) happens */
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 FD_ZERO(&writeset);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 FD_SET(fd, &writeset);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 rc = select(fd+1, NULL, &writeset, NULL, timeval_from_msec(tout_msec));
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 if (rc == 0) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 CTRL_ERR(sch, "timeout during connect\n");
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 goto out_close;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 if (rc < 0) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 CTRL_ERR(sch, "error connecting socket: %s\n", strerror(errno));
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 goto out_close;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 /* set FD blocking again */
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 if (ioctl(fd, FIONBIO, (unsigned char *)&off) < 0) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 CTRL_ERR(sch, "cannot set socket blocking: %s\n", strerror(errno));
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 goto out_close;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 sch->fd = fd;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 sch->tout_msec = tout_msec;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 return sch;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 out_close:
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 close(fd);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 return NULL;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 void simple_ctrl_set_timeout(struct simple_ctrl_handle *sch, uint32_t tout_msec)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
154 {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
155 sch->tout_msec = tout_msec;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
156 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
157
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
158 void simple_ctrl_close(struct simple_ctrl_handle *sch)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160 close(sch->fd);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
161 talloc_free(sch);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
163
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
164 static struct msgb *simple_ipa_receive(struct simple_ctrl_handle *sch)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
165 {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
166 struct ipaccess_head hh;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
167 struct msgb *resp;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
168 int rc, len;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
169
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
170 rc = read_timeout(sch->fd, (uint8_t *) &hh, sizeof(hh), sch->tout_msec);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 if (rc < 0) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172 CTRL_ERR(sch, "read(): %d\n", rc);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
173 return NULL;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
174 } else if (rc < sizeof(hh)) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
175 CTRL_ERR(sch, "short read (header)\n");
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
176 return NULL;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178 len = ntohs(hh.len);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 resp = msgb_alloc(len+sizeof(hh)+1, "CTRL Rx");
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
181 if (!resp)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 return NULL;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
183 resp->l1h = msgb_put(resp, sizeof(hh));
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 memcpy(resp->l1h, (uint8_t *) &hh, sizeof(hh));
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 resp->l2h = resp->tail;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
187 rc = read(sch->fd, resp->l2h, len);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 if (rc < len) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 CTRL_ERR(sch, "short read (payload)\n");
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 msgb_free(resp);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191 return NULL;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
192 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
193 msgb_put(resp, rc);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
194
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
195 return resp;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
196 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
197
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
198 struct msgb *simple_ctrl_receive(struct simple_ctrl_handle *sch)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
199 {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
200 struct msgb *resp;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
201 struct ipaccess_head *ih;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
202 struct ipaccess_head_ext *ihe;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
203 unsigned char *tmp;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
204
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
205 /* loop until we've received a CTRL message */
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
206 while (true) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
207 resp = simple_ipa_receive(sch);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
208 if (!resp)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
209 return NULL;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
210
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
211 ih = (struct ipaccess_head *) resp->l1h;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
212 if (ih->proto == IPAC_PROTO_OSMO)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
213 resp->l2h = resp->l2h+1;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
214 ihe = (struct ipaccess_head_ext*) (resp->l1h + sizeof(*ih));
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
215 if (ih->proto == IPAC_PROTO_OSMO && ihe->proto == IPAC_PROTO_EXT_CTRL) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
216 /* Ensure data is NULL terminated */
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
217 tmp = msgb_put(resp, 1);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
218 *tmp = '\0';
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
219 return resp;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
220 } else {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
221 CTRL_ERR(sch, "unknown IPA message %s\n", msgb_hexdump(resp));
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
222 msgb_free(resp);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
223 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
224 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
225 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
226
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
227 static int simple_ctrl_send(struct simple_ctrl_handle *sch, struct msgb *msg)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
228 {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
229 int rc;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
230
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
231 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_CTRL);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
232 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
233
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
234 rc = write_timeout(sch->fd, msg->data, msg->len, sch->tout_msec);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
235 if (rc < 0) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
236 CTRL_ERR(sch, "write(): %d\n", rc);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
237 return rc;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
238 } else if (rc < msg->len) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
239 CTRL_ERR(sch, "short write\n");
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
240 msgb_free(msg);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
241 return -1;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
242 } else {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
243 msgb_free(msg);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
244 return 0;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
245 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
246 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
247
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
248 static struct msgb *simple_ctrl_xceive(struct simple_ctrl_handle *sch, struct msgb *msg)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
249 {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
250 int rc;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
251
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
252 rc = simple_ctrl_send(sch, msg);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
253 if (rc < 0)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
254 return NULL;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
255
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
256 /* FIXME: ignore any TRAP */
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
257 return simple_ctrl_receive(sch);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
258 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
259
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
260 char *simple_ctrl_get(struct simple_ctrl_handle *sch, const char *var)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
261 {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
262 struct msgb *msg = msgb_alloc_headroom(512+8, 8, "CTRL GET");
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
263 struct msgb *resp;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
264 unsigned int rx_id;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
265 char *rx_var, *rx_val;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
266 int rc;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
267
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
268 if (!msg)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
269 return NULL;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
270
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
271 rc = msgb_printf(msg, "GET %u %s", sch->next_id++, var);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
272 if (rc < 0) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
273 msgb_free(msg);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
274 return NULL;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
275 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
276 resp = simple_ctrl_xceive(sch, msg);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
277 if (!resp)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
278 return NULL;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
279
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
280 rc = sscanf(msgb_l2(resp), "GET_REPLY %u %ms %ms", &rx_id, &rx_var, &rx_val);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
281 if ((rc == 2) || (rc == 3)) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
282 /* If body is empty return an empty string */
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
283 if (rc == 2)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
284 rx_val = strdup("");
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
285
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
286 if (rx_id == sch->next_id-1 && !strcmp(var, rx_var)) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
287 free(rx_var);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
288 msgb_free(resp);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
289 return rx_val;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
290 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
291 free(rx_var);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
292 free(rx_val);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
293 } else {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
294 CTRL_ERR(sch, "GET(%s) results in '%s'\n", var, (char *)msgb_l2(resp));
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
295 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
296
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
297 msgb_free(resp);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
298 return NULL;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
299 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
300
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
301 int simple_ctrl_set(struct simple_ctrl_handle *sch, const char *var, const char *val)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
302 {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
303 struct msgb *msg = msgb_alloc_headroom(512+8, 8, "CTRL SET");
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
304 struct msgb *resp;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
305 unsigned int rx_id;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
306 char *rx_var, *rx_val;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
307 int rc;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
308
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
309 if (!msg)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
310 return -1;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
311
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
312 rc = msgb_printf(msg, "SET %u %s %s", sch->next_id++, var, val);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
313 if (rc < 0) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
314 msgb_free(msg);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
315 return -1;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
316 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
317 resp = simple_ctrl_xceive(sch, msg);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
318 if (!resp)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
319 return -1;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
320
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
321 if (sscanf(msgb_l2(resp), "SET_REPLY %u %ms %ms", &rx_id, &rx_var, &rx_val) == 3) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
322 if (rx_id == sch->next_id-1 && !strcmp(var, rx_var) && !strcmp(val, rx_val)) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
323 free(rx_val);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
324 free(rx_var);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
325 msgb_free(resp);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
326 return 0;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
327 } else {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
328 free(rx_val);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
329 free(rx_var);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
330 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
331 } else {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
332 CTRL_ERR(sch, "SET(%s=%s) results in '%s'\n", var, val, (char *) msgb_l2(resp));
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
333 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
334
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
335 msgb_free(resp);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
336 return -1;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
337 }