blob: 7977a6abc114b422ff24de38bfcb8b691b8e8eb5 [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 Hofmeyre9920f22017-07-10 15:07:22 +020027
28void *ctx;
29
30#define buf_len 4096
31
32#if 0
33static struct msgb *from_hex(const char *hex)
34{
35 struct msgb *msg = msgb_alloc(buf_len, "mgcpgw_test_from_hex");
36 unsigned int l = osmo_hexparse(hex, msg->data, buf_len);
37 msg->l2h = msgb_put(msg, l);
38 return msg;
39}
40
41static struct msgb *mgcp_from_str(const char *head, const char *params)
42{
43 struct msgb *msg = msgb_alloc(buf_len, "mgcp_from_str");
44 unsigned int l;
45 char *data;
46 l = strlen(head);
47 msg->l2h = msgb_put(msg, l);
48 data = (char*)msgb_l2(msg);
49 strncpy(data, head, l);
50
51 data = (char*)msgb_put(msg, 1);
52 *data = '\n';
53
54 l = strlen(params);
55 data = (char*)msgb_put(msg, l);
56 strncpy(data, params, l);
57
58 return msg;
59}
60#endif
61
62static struct msgb *from_str(const char *str)
63{
64 struct msgb *msg = msgb_alloc(buf_len, "from_str");
65 unsigned int l = strlen(str);
66 char *data;
67 msg->l2h = msgb_put(msg, l);
68 data = (char*)msgb_l2(msg);
69 strncpy(data, str, l);
70 return msg;
71}
72
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020073static struct mgcp_client_conf conf;
74struct mgcp_client *mgcp = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +020075
76static void reply_to(mgcp_trans_id_t trans_id, int code, const char *comment,
77 int conn_id, const char *params)
78{
79 static char compose[4096 - 128];
80 int len;
81
82 len = snprintf(compose, sizeof(compose),
83 "%d %u %s\r\nI: %d\n\n%s",
84 code, trans_id, comment, conn_id, params);
85 OSMO_ASSERT(len < sizeof(compose));
86 OSMO_ASSERT(len > 0);
87
88 printf("composed response:\n-----\n%s\n-----\n",
89 compose);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020090 mgcp_client_rx(mgcp, from_str(compose));
Neels Hofmeyre9920f22017-07-10 15:07:22 +020091}
92
93void test_response_cb(struct mgcp_response *response, void *priv)
94{
95 OSMO_ASSERT(priv == mgcp);
96 mgcp_response_parse_params(response);
97
98 printf("response cb received:\n"
99 " head.response_code = %d\n"
100 " head.trans_id = %u\n"
101 " head.comment = %s\n"
102 " audio_port = %u\n",
103 response->head.response_code,
104 response->head.trans_id,
105 response->head.comment,
106 response->audio_port
107 );
108}
109
110mgcp_trans_id_t dummy_mgcp_send(struct msgb *msg)
111{
112 mgcp_trans_id_t trans_id;
113 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
114 char *end;
115
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200116 OSMO_ASSERT(mgcp_client_pending_add(mgcp, trans_id, test_response_cb, mgcp));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200117
118 end = (char*)msgb_put(msg, 1);
119 *end = '\0';
120 printf("composed:\n-----\n%s\n-----\n",
121 (char*)msgb_l2(msg));
122
123 talloc_free(msg);
124 return trans_id;
125}
126
127void test_crcx(void)
128{
129 struct msgb *msg;
130 mgcp_trans_id_t trans_id;
131
132 printf("\n===== %s =====\n", __func__);
133
134 if (mgcp)
135 talloc_free(mgcp);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200136 mgcp = mgcp_client_init(ctx, &conf);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200137
138 msg = mgcp_msg_crcx(mgcp, 23, 42, MGCP_CONN_LOOPBACK);
139 trans_id = dummy_mgcp_send(msg);
140
141 reply_to(trans_id, 200, "OK", 1,
142 "v=0\r\n"
143 "o=- 1 23 IN IP4 10.9.1.120\r\n"
144 "s=-\r\n"
145 "c=IN IP4 10.9.1.120\r\n"
146 "t=0 0\r\n"
147 "m=audio 16002 RTP/AVP 98\r\n"
148 "a=rtpmap:98 AMR/8000\r\n"
149 "a=ptime:20\r\n");
150}
151
Philipp Maier1dc6be62017-10-05 18:25:37 +0200152void test_mgcp_msg(void)
153{
154 struct msgb *msg;
155 char audio_ip_overflow[5000];
156
157 /* A message struct prefilled with some arbitary values */
158 struct mgcp_msg mgcp_msg = {
159 .audio_ip = "192.168.100.23",
160 .endpoint = "23@mgw",
161 .audio_port = 1234,
162 .call_id = 47,
163 .conn_id = 11,
164 .conn_mode = MGCP_CONN_RECV_SEND
165 };
166
167 if (mgcp)
168 talloc_free(mgcp);
169 mgcp = mgcp_client_init(ctx, &conf);
170
171 printf("\n");
172
173 printf("Generated CRCX message:\n");
174 mgcp_msg.verb = MGCP_VERB_CRCX;
175 mgcp_msg.presence =
176 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
177 MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE);
178 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
179 printf("%s\n", (char *)msg->data);
180
181 printf("Generated MDCX message:\n");
182 mgcp_msg.verb = MGCP_VERB_MDCX;
183 mgcp_msg.presence =
184 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
185 MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE |
186 MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT);
187 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
188 printf("%s\n", (char *)msg->data);
189
190 printf("Generated DLCX message:\n");
191 mgcp_msg.verb = MGCP_VERB_DLCX;
192 mgcp_msg.presence =
193 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
194 MGCP_MSG_PRESENCE_CONN_ID);
195 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
196 printf("%s\n", (char *)msg->data);
197
198 printf("Generated AUEP message:\n");
199 mgcp_msg.verb = MGCP_VERB_AUEP;
200 mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT);
201 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
202 printf("%s\n", msg->data);
203
204 printf("Generated RSIP message:\n");
205 mgcp_msg.verb = MGCP_VERB_RSIP;
206 mgcp_msg.presence = (MGCP_MSG_PRESENCE_ENDPOINT);
207 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
208 printf("%s\n", (char *)msg->data);
209
210 printf("Overfolow test:\n");
211 mgcp_msg.verb = MGCP_VERB_MDCX;
212 mgcp_msg.presence =
213 (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
214 MGCP_MSG_PRESENCE_CONN_ID | MGCP_MSG_PRESENCE_CONN_MODE |
215 MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT);
216 memset(audio_ip_overflow, 'X', sizeof(audio_ip_overflow));
217 audio_ip_overflow[sizeof(audio_ip_overflow) - 1] = '\0';
218 mgcp_msg.audio_ip = audio_ip_overflow;
219 msg = mgcp_msg_gen(mgcp, &mgcp_msg);
220 OSMO_ASSERT(msg == NULL);
221
222 printf("\n");
223 msgb_free(msg);
224}
225
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200226static const struct log_info_cat log_categories[] = {
227};
228
229const struct log_info log_info = {
230 .cat = log_categories,
231 .num_cat = ARRAY_SIZE(log_categories),
232};
233
234
235int main(int argc, char **argv)
236{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200237 ctx = talloc_named_const(NULL, 1, "mgcp_client_test");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200238 msgb_talloc_ctx_init(ctx, 0);
239 osmo_init_logging(&log_info);
Philipp Maier8348abb2017-10-25 12:14:13 +0200240 log_set_print_filename(osmo_stderr_target, 0);
241 log_set_print_timestamp(osmo_stderr_target, 0);
242 log_set_use_color(osmo_stderr_target, 0);
243 log_set_print_category(osmo_stderr_target, 1);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200244
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200245 mgcp_client_conf_init(&conf);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200246
247 test_crcx();
Philipp Maier1dc6be62017-10-05 18:25:37 +0200248 test_mgcp_msg();
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200249
250 printf("Done\n");
251 fprintf(stderr, "Done\n");
252 return EXIT_SUCCESS;
253}