blob: ef2fca84f9114a7f702959f6ab7ac7d6083b91c2 [file] [log] [blame]
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001/*
2 * (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
3 * All Rights Reserved
4 *
5 * Author: Neels Hofmeyr
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <string.h>
22
23#include <osmocom/core/msgb.h>
24#include <osmocom/core/application.h>
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020025#include <osmocom/mgcp_client/mgcp_client.h>
26#include <osmocom/mgcp_client/mgcp_client_internal.h>
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +010027#include <errno.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020028
29void *ctx;
30
31#define buf_len 4096
32
33#if 0
34static struct msgb *from_hex(const char *hex)
35{
36 struct msgb *msg = msgb_alloc(buf_len, "mgcpgw_test_from_hex");
37 unsigned int l = osmo_hexparse(hex, msg->data, buf_len);
38 msg->l2h = msgb_put(msg, l);
39 return msg;
40}
41
42static struct msgb *mgcp_from_str(const char *head, const char *params)
43{
44 struct msgb *msg = msgb_alloc(buf_len, "mgcp_from_str");
45 unsigned int l;
46 char *data;
47 l = strlen(head);
48 msg->l2h = msgb_put(msg, l);
49 data = (char*)msgb_l2(msg);
Philipp Maierf8bfbe82017-11-23 19:32:31 +010050 osmo_strlcpy(data, head, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +020051
52 data = (char*)msgb_put(msg, 1);
53 *data = '\n';
54
55 l = strlen(params);
56 data = (char*)msgb_put(msg, l);
Philipp Maierf8bfbe82017-11-23 19:32:31 +010057 osmo_strlcpy(data, params, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +020058
59 return msg;
60}
61#endif
62
63static struct msgb *from_str(const char *str)
64{
65 struct msgb *msg = msgb_alloc(buf_len, "from_str");
66 unsigned int l = strlen(str);
67 char *data;
68 msg->l2h = msgb_put(msg, l);
69 data = (char*)msgb_l2(msg);
Philipp Maierf8bfbe82017-11-23 19:32:31 +010070 osmo_strlcpy(data, str, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +020071 return msg;
72}
73
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020074static struct mgcp_client_conf conf;
75struct mgcp_client *mgcp = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +020076
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +010077static int reply_to(mgcp_trans_id_t trans_id, int code, const char *comment,
Neels Hofmeyre9920f22017-07-10 15:07:22 +020078 int conn_id, const char *params)
79{
80 static char compose[4096 - 128];
81 int len;
82
83 len = snprintf(compose, sizeof(compose),
84 "%d %u %s\r\nI: %d\n\n%s",
85 code, trans_id, comment, conn_id, params);
86 OSMO_ASSERT(len < sizeof(compose));
87 OSMO_ASSERT(len > 0);
88
89 printf("composed response:\n-----\n%s\n-----\n",
90 compose);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +010091 return mgcp_client_rx(mgcp, from_str(compose));
Neels Hofmeyre9920f22017-07-10 15:07:22 +020092}
93
94void test_response_cb(struct mgcp_response *response, void *priv)
95{
96 OSMO_ASSERT(priv == mgcp);
97 mgcp_response_parse_params(response);
98
99 printf("response cb received:\n"
100 " head.response_code = %d\n"
101 " head.trans_id = %u\n"
102 " head.comment = %s\n"
Philipp Maier06da85e2017-10-05 18:49:24 +0200103 " audio_port = %u\n"
104 " audio_ip = %s\n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200105 response->head.response_code,
106 response->head.trans_id,
107 response->head.comment,
Philipp Maier06da85e2017-10-05 18:49:24 +0200108 response->audio_port,
109 response->audio_ip
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200110 );
111}
112
113mgcp_trans_id_t dummy_mgcp_send(struct msgb *msg)
114{
115 mgcp_trans_id_t trans_id;
116 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
117 char *end;
118
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200119 OSMO_ASSERT(mgcp_client_pending_add(mgcp, trans_id, test_response_cb, mgcp));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200120
121 end = (char*)msgb_put(msg, 1);
122 *end = '\0';
123 printf("composed:\n-----\n%s\n-----\n",
124 (char*)msgb_l2(msg));
125
126 talloc_free(msg);
127 return trans_id;
128}
129
130void test_crcx(void)
131{
132 struct msgb *msg;
133 mgcp_trans_id_t trans_id;
134
135 printf("\n===== %s =====\n", __func__);
136
137 if (mgcp)
138 talloc_free(mgcp);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200139 mgcp = mgcp_client_init(ctx, &conf);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200140
141 msg = mgcp_msg_crcx(mgcp, 23, 42, MGCP_CONN_LOOPBACK);
142 trans_id = dummy_mgcp_send(msg);
143
144 reply_to(trans_id, 200, "OK", 1,
145 "v=0\r\n"
146 "o=- 1 23 IN IP4 10.9.1.120\r\n"
147 "s=-\r\n"
148 "c=IN IP4 10.9.1.120\r\n"
149 "t=0 0\r\n"
150 "m=audio 16002 RTP/AVP 98\r\n"
151 "a=rtpmap:98 AMR/8000\r\n"
152 "a=ptime:20\r\n");
153}
154
Philipp Maier1dc6be62017-10-05 18:25:37 +0200155void test_mgcp_msg(void)
156{
157 struct msgb *msg;
158 char audio_ip_overflow[5000];
159
160 /* A message struct prefilled with some arbitary values */
161 struct mgcp_msg mgcp_msg = {
162 .audio_ip = "192.168.100.23",
163 .endpoint = "23@mgw",
164 .audio_port = 1234,
165 .call_id = 47,
Philipp Maier01d24a32017-11-21 17:26:09 +0100166 .conn_id = "11",
Philipp Maier1dc6be62017-10-05 18:25:37 +0200167 .conn_mode = MGCP_CONN_RECV_SEND
168 };
169
170 if (mgcp)
171 talloc_free(mgcp);
172 mgcp = mgcp_client_init(ctx, &conf);
173
174 printf("\n");
175
176 printf("Generated CRCX message:\n");
177 mgcp_msg.verb = MGCP_VERB_CRCX;
178 mgcp_msg.presence =
179 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
180 MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE);
181 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
182 printf("%s\n", (char *)msg->data);
183
184 printf("Generated MDCX message:\n");
185 mgcp_msg.verb = MGCP_VERB_MDCX;
186 mgcp_msg.presence =
187 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
188 MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE |
189 MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT);
190 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
191 printf("%s\n", (char *)msg->data);
192
193 printf("Generated DLCX message:\n");
194 mgcp_msg.verb = MGCP_VERB_DLCX;
195 mgcp_msg.presence =
196 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
197 MGCP_MSG_PRESENCE_CONN_ID);
198 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
199 printf("%s\n", (char *)msg->data);
200
201 printf("Generated AUEP message:\n");
202 mgcp_msg.verb = MGCP_VERB_AUEP;
203 mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT);
204 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
205 printf("%s\n", msg->data);
206
207 printf("Generated RSIP message:\n");
208 mgcp_msg.verb = MGCP_VERB_RSIP;
209 mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT);
210 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
211 printf("%s\n", (char *)msg->data);
212
213 printf("Overfolow test:\n");
214 mgcp_msg.verb = MGCP_VERB_MDCX;
215 mgcp_msg.presence =
216 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
217 MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE |
218 MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT);
219 memset(audio_ip_overflow, 'X', sizeof(audio_ip_overflow));
220 audio_ip_overflow[sizeof(audio_ip_overflow) - 1] = '\0';
221 mgcp_msg.audio_ip = audio_ip_overflow;
222 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
223 OSMO_ASSERT(msg == NULL);
224
225 printf("\n");
226 msgb_free(msg);
227}
228
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100229void test_mgcp_client_cancel()
230{
231 mgcp_trans_id_t trans_id;
232 struct msgb *msg;
233 struct mgcp_msg mgcp_msg = {
234 .verb = MGCP_VERB_CRCX,
235 .audio_ip = "192.168.100.23",
236 .endpoint = "23@mgw",
237 .audio_port = 1234,
238 .call_id = 47,
Philipp Maier922ae9a2017-12-04 15:53:37 +0100239 .conn_id = "11",
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100240 .conn_mode = MGCP_CONN_RECV_SEND,
241 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID
242 | MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE),
243 };
244
245 printf("\n%s():\n", __func__);
246 fprintf(stderr, "\n%s():\n", __func__);
247
248 if (mgcp)
249 talloc_free(mgcp);
250 mgcp = mgcp_client_init(ctx, &conf);
251
252 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
253 trans_id = mgcp_msg_trans_id(msg);
254 fprintf(stderr, "- composed msg with trans_id=%u\n", trans_id);
255
256 fprintf(stderr, "- not in queue yet, cannot cancel yet\n");
257 OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == -ENOENT);
258
259 fprintf(stderr, "- enqueue\n");
260 dummy_mgcp_send(msg);
261
262 fprintf(stderr, "- cancel succeeds\n");
263 OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == 0);
264
265 fprintf(stderr, "- late response gets discarded\n");
266 OSMO_ASSERT(reply_to(trans_id, 200, "OK", 1, "v=0\r\n") == -ENOENT);
267
268 fprintf(stderr, "- canceling again does nothing\n");
269 OSMO_ASSERT(mgcp_client_cancel(mgcp, trans_id) == -ENOENT);
270
271 fprintf(stderr, "%s() done\n", __func__);
272}
273
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200274static const struct log_info_cat log_categories[] = {
275};
276
277const struct log_info log_info = {
278 .cat = log_categories,
279 .num_cat = ARRAY_SIZE(log_categories),
280};
281
282
283int main(int argc, char **argv)
284{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200285 ctx = talloc_named_const(NULL, 1, "mgcp_client_test");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200286 msgb_talloc_ctx_init(ctx, 0);
287 osmo_init_logging(&log_info);
Philipp Maier8348abb2017-10-25 12:14:13 +0200288 log_set_print_filename(osmo_stderr_target, 0);
289 log_set_print_timestamp(osmo_stderr_target, 0);
290 log_set_use_color(osmo_stderr_target, 0);
291 log_set_print_category(osmo_stderr_target, 1);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200292
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100293 log_set_category_filter(osmo_stderr_target, DLMGCP, 1, LOGL_DEBUG);
294
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200295 mgcp_client_conf_init(&conf);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200296
297 test_crcx();
Philipp Maier1dc6be62017-10-05 18:25:37 +0200298 test_mgcp_msg();
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100299 test_mgcp_client_cancel();
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200300
301 printf("Done\n");
302 fprintf(stderr, "Done\n");
303 return EXIT_SUCCESS;
304}