blob: ebcad8efb4937f3d29c3bd91ddb083316054a649 [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/*
2 * (C) 2011-2012,2014 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2011-2012,2014 by On-Waves
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19#undef _GNU_SOURCE
20#define _GNU_SOURCE
21
Philipp Maier87bd9be2017-08-22 16:35:41 +020022#include <osmocom/mgcp/mgcp.h>
23#include <osmocom/mgcp/vty.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020024#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020025#include <osmocom/mgcp/mgcp_internal.h>
26#include <osmocom/mgcp/mgcp_stat.h>
27#include <osmocom/mgcp/mgcp_msg.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010028#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020029#include <osmocom/mgcp/mgcp_sdp.h>
30#include <osmocom/mgcp/mgcp_codec.h>
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +020031#include <osmocom/mgcp/mgcp_internal.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020032
33#include <osmocom/core/application.h>
34#include <osmocom/core/talloc.h>
35#include <osmocom/core/utils.h>
36#include <string.h>
37#include <limits.h>
38#include <dlfcn.h>
39#include <time.h>
40#include <math.h>
Neels Hofmeyrb861db92018-08-28 16:19:25 +020041#include <ctype.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020042
43char *strline_r(char *str, char **saveptr);
44
45const char *strline_test_data =
46 "one CR\r"
47 "two CR\r"
48 "\r"
49 "one CRLF\r\n"
50 "two CRLF\r\n"
Philipp Maier87bd9be2017-08-22 16:35:41 +020051 "\r\n" "one LF\n" "two LF\n" "\n" "mixed (4 lines)\r\r\n\n\r\n";
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020052
53#define EXPECTED_NUMBER_OF_LINES 13
54
55static void test_strline(void)
56{
57 char *save = NULL;
58 char *line;
59 char buf[2048];
60 int counter = 0;
61
62 osmo_strlcpy(buf, strline_test_data, sizeof(buf));
63
Philipp Maier87bd9be2017-08-22 16:35:41 +020064 for (line = mgcp_strline(buf, &save); line;
65 line = mgcp_strline(NULL, &save)) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020066 printf("line: '%s'\n", line);
67 counter++;
68 }
69
70 OSMO_ASSERT(counter == EXPECTED_NUMBER_OF_LINES);
71}
72
Philipp Maier12943ea2018-01-17 15:40:25 +010073#define AUEP1 "AUEP 158663169 ds/e1-1/2@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020074#define AUEP1_RET "200 158663169 OK\r\n"
Philipp Maier12943ea2018-01-17 15:40:25 +010075#define AUEP2 "AUEP 18983213 ds/e1-2/1@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020076#define AUEP2_RET "500 18983213 FAIL\r\n"
77#define EMPTY "\r\n"
78#define EMPTY_RET NULL
79#define SHORT "CRCX \r\n"
80#define SHORT_RET "510 000000 FAIL\r\n"
81
Philipp Maier12943ea2018-01-17 15:40:25 +010082#define MDCX_WRONG_EP "MDCX 18983213 ds/e1-3/1@mgw MGCP 1.0\r\n"
Harald Welteabbb6b92017-12-28 13:13:50 +010083#define MDCX_ERR_RET "500 18983213 FAIL\r\n"
Philipp Maier12943ea2018-01-17 15:40:25 +010084#define MDCX_UNALLOCATED "MDCX 18983214 ds/e1-1/2@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020085#define MDCX_RET "400 18983214 FAIL\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020086
Philipp Maier87bd9be2017-08-22 16:35:41 +020087#define MDCX3 \
88 "MDCX 18983215 1@mgw MGCP 1.0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010089 "I: %s\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020090
Philipp Maier87bd9be2017-08-22 16:35:41 +020091#define MDCX3_RET \
92 "200 18983215 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +010093 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020094 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010095 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020096 "s=-\r\n" \
97 "c=IN IP4 0.0.0.0\r\n" \
98 "t=0 0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010099 "m=audio 16002 RTP/AVP 97\r\n" \
100 "a=rtpmap:97 GSM-EFR/8000\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200101 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200102
Philipp Maier87bd9be2017-08-22 16:35:41 +0200103#define MDCX3A_RET \
104 "200 18983215 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100105 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200106 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100107 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200108 "s=-\r\n" \
109 "c=IN IP4 0.0.0.0\r\n" \
110 "t=0 0\r\n" \
111 "m=audio 16002 RTP/AVP 97\r\n" \
112 "a=rtpmap:97 GSM-EFR/8000\r\n" \
113 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200114
Philipp Maier87bd9be2017-08-22 16:35:41 +0200115#define MDCX3_FMTP_RET \
116 "200 18983215 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100117 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200118 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100119 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200120 "s=-\r\n" \
121 "c=IN IP4 0.0.0.0\r\n" \
122 "t=0 0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100123 "m=audio 16006 RTP/AVP 97\r\n" \
124 "a=rtpmap:97 GSM-EFR/8000\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200125 "a=fmtp:126 0/1/2\r\n" \
126 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200127
Philipp Maier87bd9be2017-08-22 16:35:41 +0200128#define MDCX4 \
129 "MDCX 18983216 1@mgw MGCP 1.0\r\n" \
130 "M: sendrecv\r" \
131 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100132 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200133 "L: p:20, a:AMR, nt:IN\r\n" \
134 "\n" \
135 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100136 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200137 "c=IN IP4 0.0.0.0\r\n" \
138 "t=0 0\r\n" \
139 "m=audio 4441 RTP/AVP 99\r\n" \
140 "a=rtpmap:99 AMR/8000\r\n" \
141 "a=ptime:40\r\n"
142
143#define MDCX4_RET(Ident) \
144 "200 " Ident " OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100145 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200146 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100147 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200148 "s=-\r\n" \
149 "c=IN IP4 0.0.0.0\r\n" \
150 "t=0 0\r\n" \
151 "m=audio 16002 RTP/AVP 99\r\n" \
152 "a=rtpmap:99 AMR/8000\r\n" \
153 "a=ptime:40\r\n"
154
155#define MDCX4_RO_RET(Ident) \
156 "200 " Ident " OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100157 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200158 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100159 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200160 "s=-\r\n" \
161 "c=IN IP4 0.0.0.0\r\n" \
162 "t=0 0\r\n" \
Philipp Maierbc0346e2018-06-07 09:52:16 +0200163 "m=audio 16002 RTP/AVP 112\r\n" \
164 "a=rtpmap:112 AMR\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200165 "a=ptime:40\r\n"
166
167#define MDCX4_PT1 \
168 "MDCX 18983217 1@mgw MGCP 1.0\r\n" \
169 "M: sendrecv\r" \
170 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100171 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200172 "L: p:20-40, a:AMR, nt:IN\r\n" \
173 "\n" \
174 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100175 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200176 "c=IN IP4 0.0.0.0\r\n" \
177 "t=0 0\r\n" \
178 "m=audio 4441 RTP/AVP 99\r\n" \
179 "a=rtpmap:99 AMR/8000\r\n" \
180 "a=ptime:40\r\n"
181
182#define MDCX4_PT2 \
183 "MDCX 18983218 1@mgw MGCP 1.0\r\n" \
184 "M: sendrecv\r" \
185 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100186 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200187 "L: p:20-20, a:AMR, nt:IN\r\n" \
188 "\n" \
189 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100190 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200191 "c=IN IP4 0.0.0.0\r\n" \
192 "t=0 0\r\n" \
193 "m=audio 4441 RTP/AVP 99\r\n" \
194 "a=rtpmap:99 AMR/8000\r\n" \
195 "a=ptime:40\r\n"
196
197#define MDCX4_PT3 \
198 "MDCX 18983219 1@mgw MGCP 1.0\r\n" \
199 "M: sendrecv\r" \
200 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100201 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200202 "L: a:AMR, nt:IN\r\n" \
203 "\n" \
204 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100205 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200206 "c=IN IP4 0.0.0.0\r\n" \
207 "t=0 0\r\n" \
208 "m=audio 4441 RTP/AVP 99\r\n" \
209 "a=rtpmap:99 AMR/8000\r\n" \
210 "a=ptime:40\r\n"
211
212#define MDCX4_SO \
213 "MDCX 18983220 1@mgw MGCP 1.0\r\n" \
214 "M: sendonly\r" \
215 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100216 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200217 "L: p:20, a:AMR, nt:IN\r\n" \
218 "\n" \
219 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100220 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200221 "c=IN IP4 0.0.0.0\r\n" \
222 "t=0 0\r\n" \
223 "m=audio 4441 RTP/AVP 99\r\n" \
224 "a=rtpmap:99 AMR/8000\r\n" \
225 "a=ptime:40\r\n"
226
227#define MDCX4_RO \
228 "MDCX 18983221 1@mgw MGCP 1.0\r\n" \
229 "M: recvonly\r" \
230 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100231 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200232 "L: p:20, a:AMR, nt:IN\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200233
Neels Hofmeyr5336f572018-09-03 22:05:48 +0200234#define MDCX_TOO_LONG_CI \
235 "MDCX 18983222 1@mgw MGCP 1.0\r\n" \
236 "I: 123456789012345678901234567890123\n"
237
238#define MDCX_TOO_LONG_CI_RET "510 18983222 FAIL\r\n"
239
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200240#define SHORT2 "CRCX 1"
241#define SHORT2_RET "510 000000 FAIL\r\n"
242#define SHORT3 "CRCX 1 1@mgw"
243#define SHORT4 "CRCX 1 1@mgw MGCP"
244#define SHORT5 "CRCX 1 1@mgw MGCP 1.0"
245
Philipp Maier87bd9be2017-08-22 16:35:41 +0200246#define CRCX \
247 "CRCX 2 1@mgw MGCP 1.0\r\n" \
248 "M: recvonly\r\n" \
249 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200250 "L: p:20\r\n" \
251 "\r\n" \
252 "v=0\r\n" \
253 "c=IN IP4 123.12.12.123\r\n" \
254 "m=audio 5904 RTP/AVP 97\r\n" \
255 "a=rtpmap:97 GSM-EFR/8000\r\n" \
256 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200257
Philipp Maier87bd9be2017-08-22 16:35:41 +0200258#define CRCX_RET \
259 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100260 "I: %s\r\n" \
261 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200262 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100263 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200264 "s=-\r\n" \
265 "c=IN IP4 0.0.0.0\r\n" \
266 "t=0 0\r\n" \
267 "m=audio 16002 RTP/AVP 97\r\n" \
268 "a=rtpmap:97 GSM-EFR/8000\r\n" \
269 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200270
Philipp Maier87bd9be2017-08-22 16:35:41 +0200271#define CRCX_RET_NO_RTPMAP \
272 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100273 "I: %s\r\n" \
274 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200275 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100276 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200277 "s=-\r\n" \
278 "c=IN IP4 0.0.0.0\r\n" \
279 "t=0 0\r\n" \
280 "m=audio 16002 RTP/AVP 97\r\n" \
281 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200282
Philipp Maier87bd9be2017-08-22 16:35:41 +0200283#define CRCX_FMTP_RET \
284 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100285 "I: %s\r\n" \
286 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200287 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100288 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200289 "s=-\r\n" \
290 "c=IN IP4 0.0.0.0\r\n" \
291 "t=0 0\r\n" \
292 "m=audio 16006 RTP/AVP 97\r\n" \
293 "a=rtpmap:97 GSM-EFR/8000\r\n" \
294 "a=fmtp:126 0/1/2\r\n" \
295 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200296
Philipp Maier87bd9be2017-08-22 16:35:41 +0200297#define CRCX_ZYN \
298 "CRCX 2 1@mgw MGCP 1.0\r" \
299 "M: recvonly\r" \
300 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200301 "\n" \
302 "v=0\r" \
303 "c=IN IP4 123.12.12.123\r" \
304 "m=audio 5904 RTP/AVP 97\r" \
305 "a=rtpmap:97 GSM-EFR/8000\r"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200306
Philipp Maier87bd9be2017-08-22 16:35:41 +0200307#define CRCX_ZYN_RET \
308 "200 2 OK\r\n" \
Philipp Maierc3cfae22018-01-22 12:03:03 +0100309 "I: %s\r\n" \
310 "\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200311 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100312 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200313 "s=-\r\n" \
314 "c=IN IP4 0.0.0.0\r\n" \
315 "t=0 0\r\n" \
316 "m=audio 16004 RTP/AVP 97\r\n" \
317 "a=rtpmap:97 GSM-EFR/8000\r\n" \
318 "a=ptime:20\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200319
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200320#define CRCX_X_OSMO_IGN \
321 "CRCX 2 1@mgw MGCP 1.0\r\n" \
322 "M: recvonly\r\n" \
323 "C: 2\r\n" \
324 "L: p:20\r\n" \
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200325 "X-Osmo-IGN: C foo\r\n" \
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200326 "\r\n" \
327 "v=0\r\n" \
328 "c=IN IP4 123.12.12.123\r\n" \
329 "m=audio 5904 RTP/AVP 97\r\n" \
330 "a=rtpmap:97 GSM-EFR/8000\r\n" \
331 "a=ptime:40\r\n"
332
333#define CRCX_X_OSMO_IGN_RET \
334 "200 2 OK\r\n" \
335 "I: %s\r\n" \
336 "\r\n" \
337 "v=0\r\n" \
338 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
339 "s=-\r\n" \
340 "c=IN IP4 0.0.0.0\r\n" \
341 "t=0 0\r\n" \
342 "m=audio 16010 RTP/AVP 97\r\n" \
343 "a=rtpmap:97 GSM-EFR/8000\r\n" \
344 "a=ptime:40\r\n"
345
Philipp Maier87bd9be2017-08-22 16:35:41 +0200346#define DLCX \
347 "DLCX 7 1@mgw MGCP 1.0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100348 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200349 "C: 2\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200350
Philipp Maier87bd9be2017-08-22 16:35:41 +0200351#define DLCX_RET \
352 "250 7 OK\r\n" \
Pau Espin Pedrol2da99a22018-02-20 13:11:17 +0100353 "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n"
354
355 #define DLCX_RET_OSMUX DLCX_RET \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200356 "X-Osmo-CP: EC TI=0, TO=0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200357
Philipp Maier87bd9be2017-08-22 16:35:41 +0200358#define RQNT \
359 "RQNT 186908780 1@mgw MGCP 1.0\r\n" \
360 "X: B244F267488\r\n" \
361 "S: D/9\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200362
Philipp Maier87bd9be2017-08-22 16:35:41 +0200363#define RQNT2 \
364 "RQNT 186908781 1@mgw MGCP 1.0\r\n" \
365 "X: ADD4F26746F\r\n" \
366 "R: D/[0-9#*](N), G/ft, fxr/t38\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200367
368#define RQNT1_RET "200 186908780 OK\r\n"
369#define RQNT2_RET "200 186908781 OK\r\n"
370
Philipp Maier87bd9be2017-08-22 16:35:41 +0200371#define PTYPE_IGNORE 0 /* == default initializer */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200372#define PTYPE_NONE 128
373#define PTYPE_NYI PTYPE_NONE
374
Philipp Maier87bd9be2017-08-22 16:35:41 +0200375#define CRCX_MULT_1 \
376 "CRCX 2 1@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200377 "M: recvonly\r\n" \
378 "C: 2\r\n" \
379 "X\r\n" \
380 "L: p:20\r\n" \
381 "\r\n" \
382 "v=0\r\n" \
383 "c=IN IP4 123.12.12.123\r\n" \
384 "m=audio 5904 RTP/AVP 18 97\r\n" \
385 "a=rtpmap:18 G729/8000\r\n" \
386 "a=rtpmap:97 GSM-EFR/8000\r\n" \
387 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200388
Philipp Maier87bd9be2017-08-22 16:35:41 +0200389#define CRCX_MULT_2 \
390 "CRCX 2 2@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200391 "M: recvonly\r\n" \
392 "C: 2\r\n" \
393 "X\r\n" \
394 "L: p:20\r\n" \
395 "\r\n" \
396 "v=0\r\n" \
397 "c=IN IP4 123.12.12.123\r\n" \
398 "m=audio 5904 RTP/AVP 18 97 101\r\n" \
399 "a=rtpmap:18 G729/8000\r\n" \
400 "a=rtpmap:97 GSM-EFR/8000\r\n" \
401 "a=rtpmap:101 FOO/8000\r\n" \
402 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200403
Philipp Maier87bd9be2017-08-22 16:35:41 +0200404#define CRCX_MULT_3 \
405 "CRCX 2 3@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200406 "M: recvonly\r\n" \
407 "C: 2\r\n" \
408 "X\r\n" \
409 "L: p:20\r\n" \
410 "\r\n" \
411 "v=0\r\n" \
412 "c=IN IP4 123.12.12.123\r\n" \
413 "m=audio 5904 RTP/AVP\r\n" \
414 "a=rtpmap:18 G729/8000\r\n" \
415 "a=rtpmap:97 GSM-EFR/8000\r\n" \
416 "a=rtpmap:101 FOO/8000\r\n" \
417 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200418
Philipp Maier87bd9be2017-08-22 16:35:41 +0200419#define CRCX_MULT_4 \
420 "CRCX 2 4@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200421 "M: recvonly\r\n" \
422 "C: 2\r\n" \
423 "X\r\n" \
424 "L: p:20\r\n" \
425 "\r\n" \
426 "v=0\r\n" \
427 "c=IN IP4 123.12.12.123\r\n" \
428 "m=audio 5904 RTP/AVP 18\r\n" \
429 "a=rtpmap:18 G729/8000\r\n" \
430 "a=rtpmap:97 GSM-EFR/8000\r\n" \
431 "a=rtpmap:101 FOO/8000\r\n" \
432 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200433
434#define CRCX_MULT_GSM_EXACT \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200435 "CRCX 259260421 5@mgw MGCP 1.0\r\n" \
436 "C: 1355c6041e\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200437 "L: p:20, a:GSM, nt:IN\r\n" \
438 "M: recvonly\r\n" \
439 "\r\n" \
440 "v=0\r\n" \
441 "o=- 1439038275 1439038275 IN IP4 192.168.181.247\r\n" \
442 "s=-\r\nc=IN IP4 192.168.181.247\r\n" \
Philipp Maierbc0346e2018-06-07 09:52:16 +0200443 "t=0 0\r\nm=audio 29084 RTP/AVP 0 8 3 18 4 96 97 101\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200444 "a=rtpmap:0 PCMU/8000\r\n" \
445 "a=rtpmap:8 PCMA/8000\r\n" \
446 "a=rtpmap:3 gsm/8000\r\n" \
447 "a=rtpmap:18 G729/8000\r\n" \
448 "a=fmtp:18 annexb=no\r\n" \
449 "a=rtpmap:4 G723/8000\r\n" \
450 "a=rtpmap:96 iLBC/8000\r\n" \
451 "a=fmtp:96 mode=20\r\n" \
452 "a=rtpmap:97 iLBC/8000\r\n" \
453 "a=fmtp:97 mode=30\r\n" \
454 "a=rtpmap:101 telephone-event/8000\r\n" \
455 "a=fmtp:101 0-15\r\n" \
456 "a=recvonly\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200457
Philipp Maier87bd9be2017-08-22 16:35:41 +0200458#define MDCX_NAT_DUMMY \
459 "MDCX 23 5@mgw MGCP 1.0\r\n" \
460 "C: 1355c6041e\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100461 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200462 "\r\n" \
463 "c=IN IP4 8.8.8.8\r\n" \
Philipp Maierbc0346e2018-06-07 09:52:16 +0200464 "m=audio 16434 RTP/AVP 3\r\n"
465
466#define CRCX_NO_LCO_NO_SDP \
467 "CRCX 2 6@mgw MGCP 1.0\r\n" \
468 "M: recvonly\r\n" \
469 "C: 2\r\n"
470
471#define CRCX_NO_LCO_NO_SDP_RET \
472 "200 2 OK\r\n" \
473 "I: %s\r\n" \
474 "\r\n" \
475 "v=0\r\n" \
476 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
477 "s=-\r\n" \
478 "c=IN IP4 0.0.0.0\r\n" \
479 "t=0 0\r\n" \
480 "m=audio 16008 RTP/AVP 0\r\n" \
481 "a=ptime:20\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200482
483struct mgcp_test {
484 const char *name;
485 const char *req;
486 const char *exp_resp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200487 int ptype;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200488 const char *extra_fmtp;
489};
490
491static const struct mgcp_test tests[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200492 {"AUEP1", AUEP1, AUEP1_RET},
493 {"AUEP2", AUEP2, AUEP2_RET},
494 {"MDCX1", MDCX_WRONG_EP, MDCX_ERR_RET},
495 {"MDCX2", MDCX_UNALLOCATED, MDCX_RET},
496 {"CRCX", CRCX, CRCX_RET, 97},
497 {"MDCX3", MDCX3, MDCX3_RET, PTYPE_IGNORE},
498 {"MDCX4", MDCX4, MDCX4_RET("18983216"), 99},
499 {"MDCX4_PT1", MDCX4_PT1, MDCX4_RET("18983217"), 99},
500 {"MDCX4_PT2", MDCX4_PT2, MDCX4_RET("18983218"), 99},
501 {"MDCX4_PT3", MDCX4_PT3, MDCX4_RET("18983219"), 99},
502 {"MDCX4_SO", MDCX4_SO, MDCX4_RET("18983220"), 99},
503 {"MDCX4_RO", MDCX4_RO, MDCX4_RO_RET("18983221"), PTYPE_IGNORE},
504 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
505 {"CRCX_ZYN", CRCX_ZYN, CRCX_ZYN_RET, 97},
506 {"EMPTY", EMPTY, EMPTY_RET},
507 {"SHORT1", SHORT, SHORT_RET},
508 {"SHORT2", SHORT2, SHORT2_RET},
509 {"SHORT3", SHORT3, SHORT2_RET},
510 {"SHORT4", SHORT4, SHORT2_RET},
511 {"RQNT1", RQNT, RQNT1_RET},
512 {"RQNT2", RQNT2, RQNT2_RET},
513 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
514 {"CRCX", CRCX, CRCX_FMTP_RET, 97,.extra_fmtp = "a=fmtp:126 0/1/2"},
515 {"MDCX3", MDCX3, MDCX3_FMTP_RET, PTYPE_NONE,.extra_fmtp =
516 "a=fmtp:126 0/1/2"},
517 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE,.extra_fmtp = "a=fmtp:126 0/1/2"},
Philipp Maierbc0346e2018-06-07 09:52:16 +0200518 {"CRCX", CRCX_NO_LCO_NO_SDP, CRCX_NO_LCO_NO_SDP_RET, 97},
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200519 {"CRCX", CRCX_X_OSMO_IGN, CRCX_X_OSMO_IGN_RET, 97},
Neels Hofmeyr5336f572018-09-03 22:05:48 +0200520 {"MDCX_TOO_LONG_CI", MDCX_TOO_LONG_CI, MDCX_TOO_LONG_CI_RET},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200521};
522
523static const struct mgcp_test retransmit[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200524 {"CRCX", CRCX, CRCX_RET},
525 {"RQNT1", RQNT, RQNT1_RET},
526 {"RQNT2", RQNT2, RQNT2_RET},
527 {"MDCX3", MDCX3, MDCX3A_RET},
528 {"DLCX", DLCX, DLCX_RET},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200529};
530
Philipp Maierffd75e42017-11-22 11:44:50 +0100531static struct msgb *create_msg(const char *str, const char *conn_id)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200532{
533 struct msgb *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100534 int len;
535
536 printf("creating message from statically defined input:\n");
537 printf("---------8<---------\n%s\n---------8<---------\n", str);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200538
539 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
Philipp Maierffd75e42017-11-22 11:44:50 +0100540 if (conn_id && strlen(conn_id))
541 len = sprintf((char *)msg->data, str, conn_id, conn_id);
542 else
543 len = sprintf((char *)msg->data, "%s", str);
544
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200545 msg->l2h = msgb_put(msg, len);
546 return msg;
547}
548
549static int last_endpoint = -1;
550
551static int mgcp_test_policy_cb(struct mgcp_trunk_config *cfg, int endpoint,
552 int state, const char *transactio_id)
553{
Pau Espin Pedrol9ecceb62018-10-16 15:35:58 +0200554 fprintf(stderr, "Policy CB got state %d on endpoint 0x%x\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200555 state, endpoint);
556 last_endpoint = endpoint;
557 return MGCP_POLICY_CONT;
558}
559
560#define MGCP_DUMMY_LOAD 0x23
561static int dummy_packets = 0;
562/* override and forward */
563ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200564 const struct sockaddr *dest_addr, socklen_t addrlen)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200565{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200566 uint32_t dest_host =
567 htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
568 int dest_port = htons(((struct sockaddr_in *)dest_addr)->sin_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200569
Philipp Maier87bd9be2017-08-22 16:35:41 +0200570 if (len == 1 && ((const char *)buf)[0] == MGCP_DUMMY_LOAD) {
571 fprintf(stderr,
572 "Dummy packet to 0x%08x:%d, msg length %zu\n%s\n\n",
573 dest_host, dest_port, len, osmo_hexdump(buf, len));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200574 dummy_packets += 1;
575 }
576
Philipp Maier3d9b6562017-10-13 18:33:44 +0200577 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200578}
579
580static int64_t force_monotonic_time_us = -1;
581/* override and forward */
582int clock_gettime(clockid_t clk_id, struct timespec *tp)
583{
584 typedef int (*clock_gettime_t)(clockid_t clk_id, struct timespec *tp);
585 static clock_gettime_t real_clock_gettime = NULL;
586
587 if (!real_clock_gettime)
588 real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
589
590 if (clk_id == CLOCK_MONOTONIC && force_monotonic_time_us >= 0) {
591 tp->tv_sec = force_monotonic_time_us / 1000000;
592 tp->tv_nsec = (force_monotonic_time_us % 1000000) * 1000;
593 return 0;
594 }
595
596 return real_clock_gettime(clk_id, tp);
597}
598
599#define CONN_UNMODIFIED (0x1000)
600
601static void test_values(void)
602{
603 /* Check that NONE disables all output */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200604 OSMO_ASSERT((MGCP_CONN_NONE & MGCP_CONN_RECV_SEND) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200605
606 /* Check that LOOPBACK enables all output */
607 OSMO_ASSERT((MGCP_CONN_LOOPBACK & MGCP_CONN_RECV_SEND) ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200608 MGCP_CONN_RECV_SEND);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200609}
610
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200611/* Extract a connection ID from a response and return in conn_id;
612 * if there is none, return -EINVAL and leave conn_id unchanged. */
Philipp Maierffd75e42017-11-22 11:44:50 +0100613static int get_conn_id_from_response(uint8_t *resp, char *conn_id,
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200614 size_t conn_id_buflen)
Philipp Maierffd75e42017-11-22 11:44:50 +0100615{
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200616 const char *conn_id_start;
617 const char *conn_id_end;
618 int conn_id_len;
Philipp Maierffd75e42017-11-22 11:44:50 +0100619
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200620 const char *header_I = "\r\nI: ";
621 const char *header_o = "\r\no=- ";
Philipp Maierffd75e42017-11-22 11:44:50 +0100622
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200623 /* Try to get the conn_id from the 'I:' or 'o=-' parameter */
624 if ((conn_id_start = strstr((char *)resp, header_I))) {
625 conn_id_start += strlen(header_I);
626 conn_id_end = strstr(conn_id_start, "\r\n");
627 } else if ((conn_id_start = strstr((char *)resp, header_o))) {
628 conn_id_start += strlen(header_o);
629 conn_id_end = strchr(conn_id_start, ' ');
630 } else
631 return -EINVAL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100632
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200633 if (conn_id_end)
634 conn_id_len = conn_id_end - conn_id_start;
635 else
636 conn_id_len = strlen(conn_id_start);
637 OSMO_ASSERT(conn_id_len <= conn_id_buflen - 1);
Philipp Maier55295f72018-01-15 14:00:28 +0100638
Neels Hofmeyre28b6732018-08-28 16:19:25 +0200639 /* A valid conn_id must at least contain one digit, and must
640 * not exceed a length of 32 digits */
641 OSMO_ASSERT(conn_id_len <= 32);
642 OSMO_ASSERT(conn_id_len > 0);
643
644 strncpy(conn_id, conn_id_start, conn_id_len);
645 conn_id[conn_id_len] = '\0';
646 return 0;
Philipp Maierffd75e42017-11-22 11:44:50 +0100647}
648
649/* Check response, automatically patch connection ID if needed */
650static int check_response(uint8_t *resp, const char *exp_resp)
651{
652 char exp_resp_patched[4096];
653 const char *exp_resp_ptr;
654 char conn_id[256];
655
656 printf("checking response:\n");
657
658 /* If the expected response is intened to be patched
659 * (%s placeholder inside) we will patch it with the
660 * connection identifier we just received from the
661 * real response. This is necessary because the CI
662 * is generated by the mgcp code on CRCX and we can
663 * not know it in advance */
664 if (strstr(exp_resp, "%s")) {
665 if (get_conn_id_from_response(resp, conn_id, sizeof(conn_id)) ==
666 0) {
667 sprintf(exp_resp_patched, exp_resp, conn_id, conn_id);
668 exp_resp_ptr = exp_resp_patched;
669 printf
670 ("using message with patched conn_id for comparison\n");
671 } else {
672 printf
673 ("patching conn_id failed, using message as statically defined for comparison\n");
674 exp_resp_ptr = exp_resp;
675 }
676 } else {
677 printf("using message as statically defined for comparison\n");
678 exp_resp_ptr = exp_resp;
679 }
680
681 if (strcmp((char *)resp, exp_resp_ptr) != 0) {
682 printf("Unexpected response, please check!\n");
683 printf
684 ("Got:\n---------8<---------\n%s\n---------8<---------\n\n",
685 resp);
686 printf
687 ("Expected:\n---------8<---------\n%s\n---------8<---------\n",
688 exp_resp_ptr);
689 return -EINVAL;
690 }
691
692 printf("Response matches our expectations.\n");
693 return 0;
694}
695
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200696static void test_messages(void)
697{
698 struct mgcp_config *cfg;
699 struct mgcp_endpoint *endp;
700 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200701 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100702 char last_conn_id[256];
Philipp Maier7df419b2017-12-04 17:11:42 +0100703 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200704
705 cfg = mgcp_config_alloc();
706
Philipp Maierfcd06552017-11-10 17:32:22 +0100707 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200708 mgcp_endpoints_allocate(&cfg->trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200709 cfg->policy_cb = mgcp_test_policy_cb;
710
Philipp Maierffd75e42017-11-22 11:44:50 +0100711 memset(last_conn_id, 0, sizeof(last_conn_id));
712
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200713 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
714
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200715 for (i = 0; i < ARRAY_SIZE(tests); i++) {
716 const struct mgcp_test *t = &tests[i];
717 struct msgb *inp;
718 struct msgb *msg;
719
Philipp Maierffd75e42017-11-22 11:44:50 +0100720 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200721 printf("Testing %s\n", t->name);
722
723 last_endpoint = -1;
724 dummy_packets = 0;
725
Philipp Maier87bd9be2017-08-22 16:35:41 +0200726 osmo_talloc_replace_string(cfg, &cfg->trunk.audio_fmtp_extra,
727 t->extra_fmtp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200728
Philipp Maierffd75e42017-11-22 11:44:50 +0100729 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200730 msg = mgcp_handle_message(cfg, inp);
731 msgb_free(inp);
732 if (!t->exp_resp) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200733 if (msg) {
734 printf("%s failed '%s'\n", t->name,
735 (char *)msg->data);
736 OSMO_ASSERT(false);
737 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100738 } else if (check_response(msg->data, t->exp_resp) != 0) {
739 printf("%s failed.\n", t->name);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200740 OSMO_ASSERT(false);
741 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100742
Philipp Maier7df419b2017-12-04 17:11:42 +0100743 if (msg) {
744 rc = get_conn_id_from_response(msg->data, last_conn_id,
745 sizeof(last_conn_id));
Neels Hofmeyr08e07042018-08-28 16:22:14 +0200746 if (rc == 0)
Philipp Maier7df419b2017-12-04 17:11:42 +0100747 printf("(response contains a connection id)\n");
748 else
749 printf("(response does not contain a connection id)\n");
750 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100751
Philipp Maiera330b862017-12-04 17:16:16 +0100752 if (msg)
753 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200754
755 if (dummy_packets)
756 printf("Dummy packets: %d\n", dummy_packets);
757
758 if (last_endpoint != -1) {
759 endp = &cfg->trunk.endpoints[last_endpoint];
760
Philipp Maier01d24a32017-11-21 17:26:09 +0100761 conn = mgcp_conn_get_rtp(endp, "1");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200762 if (conn) {
763 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200764
Philipp Maier87bd9be2017-08-22 16:35:41 +0200765 if (conn->end.packet_duration_ms != -1)
766 printf("Detected packet duration: %d\n",
767 conn->end.packet_duration_ms);
768 else
769 printf("Packet duration not set\n");
770 if (endp->local_options.pkt_period_min ||
771 endp->local_options.pkt_period_max)
772 printf
773 ("Requested packetetization period: "
774 "%d-%d\n",
775 endp->local_options.pkt_period_min,
776 endp->
777 local_options.pkt_period_max);
778 else
779 printf
780 ("Requested packetization period not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200781
Philipp Maier87bd9be2017-08-22 16:35:41 +0200782 if ((conn->conn->mode & CONN_UNMODIFIED) == 0) {
783 printf("Connection mode: %d:%s%s%s%s\n",
784 conn->conn->mode,
785 !conn->conn->mode ? " NONE" : "",
786 conn->conn->mode & MGCP_CONN_SEND_ONLY
787 ? " SEND" : "",
788 conn->conn->mode & MGCP_CONN_RECV_ONLY
789 ? " RECV" : "",
790 conn->conn->mode & MGCP_CONN_LOOPBACK
791 & ~MGCP_CONN_RECV_SEND
792 ? " LOOP" : "");
793 fprintf(stderr,
794 "RTP output %sabled, NET output %sabled\n",
795 conn->end.output_enabled
796 ? "en" : "dis",
797 conn->end.output_enabled
798 ? "en" : "dis");
799 } else
800 printf("Connection mode not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200801
Philipp Maier87bd9be2017-08-22 16:35:41 +0200802 OSMO_ASSERT(conn->end.output_enabled
803 == (conn->conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
804
805 conn->conn->mode |= CONN_UNMODIFIED;
806
807 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200808 endp->local_options.pkt_period_min = 0;
809 endp->local_options.pkt_period_max = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200810 }
811
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200812 /* Check detected payload type */
Philipp Maierffd75e42017-11-22 11:44:50 +0100813 if (conn && t->ptype != PTYPE_IGNORE) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200814 OSMO_ASSERT(last_endpoint != -1);
815 endp = &cfg->trunk.endpoints[last_endpoint];
816
Pau Espin Pedrol9ecceb62018-10-16 15:35:58 +0200817 fprintf(stderr, "endpoint 0x%x: "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200818 "payload type %d (expected %d)\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200819 last_endpoint,
Philipp Maierbc0346e2018-06-07 09:52:16 +0200820 conn->end.codec->payload_type, t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200821
Philipp Maier87bd9be2017-08-22 16:35:41 +0200822 if (t->ptype != PTYPE_IGNORE)
Philipp Maierbc0346e2018-06-07 09:52:16 +0200823 OSMO_ASSERT(conn->end.codec->payload_type ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200824 t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200825
826 /* Reset them again for next test */
Philipp Maierbc0346e2018-06-07 09:52:16 +0200827 conn->end.codec->payload_type = PTYPE_NONE;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200828 }
829 }
830
831 talloc_free(cfg);
832}
833
834static void test_retransmission(void)
835{
836 struct mgcp_config *cfg;
837 int i;
Philipp Maierffd75e42017-11-22 11:44:50 +0100838 char last_conn_id[256];
Philipp Maier23b8e292017-12-04 16:48:45 +0100839 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200840
841 cfg = mgcp_config_alloc();
842
Philipp Maierfcd06552017-11-10 17:32:22 +0100843 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200844 mgcp_endpoints_allocate(&cfg->trunk);
845
Philipp Maierffd75e42017-11-22 11:44:50 +0100846 memset(last_conn_id, 0, sizeof(last_conn_id));
847
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200848 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
849
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200850 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
851 const struct mgcp_test *t = &retransmit[i];
852 struct msgb *inp;
853 struct msgb *msg;
854
Philipp Maierffd75e42017-11-22 11:44:50 +0100855 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200856 printf("Testing %s\n", t->name);
857
Philipp Maierffd75e42017-11-22 11:44:50 +0100858 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200859 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200860
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200861 msgb_free(inp);
Philipp Maier7cedfd72017-12-04 16:49:12 +0100862 if (msg && check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200863 printf("%s failed '%s'\n", t->name, (char *)msg->data);
864 OSMO_ASSERT(false);
865 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100866
Philipp Maier23b8e292017-12-04 16:48:45 +0100867 if (msg && strcmp(t->name, "CRCX") == 0) {
868 rc = get_conn_id_from_response(msg->data, last_conn_id,
869 sizeof(last_conn_id));
870 OSMO_ASSERT(rc == 0);
871 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100872
Philipp Maier7cedfd72017-12-04 16:49:12 +0100873 if (msg)
874 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200875
876 /* Retransmit... */
877 printf("Re-transmitting %s\n", t->name);
Philipp Maierffd75e42017-11-22 11:44:50 +0100878 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200879 msg = mgcp_handle_message(cfg, inp);
880 msgb_free(inp);
Philipp Maierffd75e42017-11-22 11:44:50 +0100881 if (check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200882 printf("%s failed '%s'\n", t->name, (char *)msg->data);
883 OSMO_ASSERT(false);
884 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200885 msgb_free(msg);
886 }
887
888 talloc_free(cfg);
889}
890
891static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
892{
893 ptrdiff_t tone = _tone;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200894 endp->cfg->data = (void *)tone;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200895 return 0;
896}
897
898static void test_rqnt_cb(void)
899{
900 struct mgcp_config *cfg;
901 struct msgb *inp, *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100902 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200903
904 cfg = mgcp_config_alloc();
905 cfg->rqnt_cb = rqnt_cb;
906
Philipp Maierfcd06552017-11-10 17:32:22 +0100907 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200908 mgcp_endpoints_allocate(&cfg->trunk);
909
910 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
911
Philipp Maierffd75e42017-11-22 11:44:50 +0100912 inp = create_msg(CRCX, NULL);
913 msg = mgcp_handle_message(cfg, inp);
914 OSMO_ASSERT(msg);
915 OSMO_ASSERT(get_conn_id_from_response(msg->data, conn_id,
916 sizeof(conn_id)) == 0);
917 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200918 msgb_free(inp);
919
920 /* send the RQNT and check for the CB */
Philipp Maierffd75e42017-11-22 11:44:50 +0100921 inp = create_msg(RQNT, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200922 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200923 if (strncmp((const char *)msg->l2h, "200", 3) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200924 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
925 abort();
926 }
927
Philipp Maier87bd9be2017-08-22 16:35:41 +0200928 if (cfg->data != (void *)'9') {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200929 printf("FAILED: callback not called: %p\n", cfg->data);
930 abort();
931 }
932
933 msgb_free(msg);
934 msgb_free(inp);
935
Philipp Maierffd75e42017-11-22 11:44:50 +0100936 inp = create_msg(DLCX, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200937 msgb_free(mgcp_handle_message(cfg, inp));
938 msgb_free(inp);
939 talloc_free(cfg);
940}
941
942struct pl_test {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200943 int cycles;
944 uint16_t base_seq;
945 uint16_t max_seq;
946 uint32_t packets;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200947
Philipp Maier87bd9be2017-08-22 16:35:41 +0200948 uint32_t expected;
949 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200950};
951
952static const struct pl_test pl_test_dat[] = {
953 /* basic.. just one package */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200954 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = 1,.expected =
955 1,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200956 /* some packages and a bit of loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200957 {.cycles = 0,.base_seq = 0,.max_seq = 100,.packets = 100,.expected =
958 101,.loss = 1},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200959 /* wrap around */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200960 {.cycles = 1 << 16,.base_seq = 0xffff,.max_seq = 2,.packets =
961 4,.expected = 4,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200962 /* min loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200963 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = UINT_MAX,.expected =
964 1,.loss = INT_MIN},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200965 /* max loss, with wrap around on expected max */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200966 {.cycles = INT_MAX,.base_seq = 0,.max_seq = UINT16_MAX,.packets =
967 0,.expected = ((uint32_t) (INT_MAX) + UINT16_MAX + 1),.loss = INT_MAX},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200968};
969
970static void test_packet_loss_calc(void)
971{
972 int i;
Philipp Maiercede2a42018-07-03 14:14:21 +0200973 struct mgcp_endpoint endp;
974 struct mgcp_trunk_config trunk;
975
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200976 printf("Testing packet loss calculation.\n");
977
Philipp Maiercede2a42018-07-03 14:14:21 +0200978 memset(&endp, 0, sizeof(endp));
979 memset(&trunk, 0, sizeof(trunk));
980
981 endp.type = &ep_typeset.rtp;
982 trunk.vty_number_endpoints = 1;
983 trunk.endpoints = &endp;
984 endp.tcfg = &trunk;
985 INIT_LLIST_HEAD(&endp.conns);
986
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200987 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
988 uint32_t expected;
989 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200990
Philipp Maiercede2a42018-07-03 14:14:21 +0200991 struct mgcp_conn_rtp *conn = NULL;
992 struct mgcp_conn *_conn = NULL;
993 struct mgcp_rtp_state *state;
994 struct rate_ctr *packets_rx;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200995
Philipp Maiercede2a42018-07-03 14:14:21 +0200996 _conn =
997 mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
998 "test-connection");
999 conn = mgcp_conn_get_rtp(&endp, _conn->id);
1000 state = &conn->state;
1001 packets_rx = &conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR];
1002
1003 state->stats.initialized = 1;
1004 state->stats.base_seq = pl_test_dat[i].base_seq;
1005 state->stats.max_seq = pl_test_dat[i].max_seq;
1006 state->stats.cycles = pl_test_dat[i].cycles;
1007
1008 packets_rx->current = pl_test_dat[i].packets;
1009 calc_loss(conn, &expected, &loss);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001010
Philipp Maier87bd9be2017-08-22 16:35:41 +02001011 if (loss != pl_test_dat[i].loss
1012 || expected != pl_test_dat[i].expected) {
1013 printf
1014 ("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
1015 i, loss, pl_test_dat[i].loss, expected,
1016 pl_test_dat[i].expected);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001017 }
Philipp Maiercede2a42018-07-03 14:14:21 +02001018
1019 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001020 }
Philipp Maiercede2a42018-07-03 14:14:21 +02001021
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001022}
1023
Philipp Maier87bd9be2017-08-22 16:35:41 +02001024int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
1025 uint32_t *pr, uint32_t *_or, int *loss,
1026 uint32_t *jitter)
1027{
1028 char *line, *save;
1029 int rc;
1030
1031 /* initialize with bad values */
1032 *ps = *os = *pr = *_or = *jitter = UINT_MAX;
1033 *loss = INT_MAX;
1034
1035 line = strtok_r((char *)msg->l2h, "\r\n", &save);
1036 if (!line)
1037 return -1;
1038
1039 /* this can only parse the message that is created above... */
1040 for_each_non_empty_line(line, save) {
1041 switch (line[0]) {
1042 case 'P':
1043 rc = sscanf(line,
1044 "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
1045 ps, os, pr, _or, loss, jitter);
1046 return rc == 6 ? 0 : -1;
1047 }
1048 }
1049
1050 return -1;
1051}
1052
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001053static void test_mgcp_stats(void)
1054{
1055 printf("Testing stat parsing\n");
1056
1057 uint32_t bps, bos, pr, _or, jitter;
1058 struct msgb *msg;
1059 int loss;
1060 int rc;
1061
Philipp Maierffd75e42017-11-22 11:44:50 +01001062 msg = create_msg(DLCX_RET, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001063 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
1064 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001065 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0
1066 || jitter != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001067 printf("FAIL: Parsing failed1.\n");
1068 msgb_free(msg);
1069
Philipp Maier87bd9be2017-08-22 16:35:41 +02001070 msg =
1071 create_msg
Philipp Maierffd75e42017-11-22 11:44:50 +01001072 ("250 7 OK\r\nP: PS=10, OS=20, PR=30, OR=40, PL=-3, JI=40\r\n", NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001073 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
1074 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001075 if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3
1076 || jitter != 40)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001077 printf("FAIL: Parsing failed2.\n");
1078 msgb_free(msg);
1079}
1080
1081struct rtp_packet_info {
1082 float txtime;
1083 int len;
1084 char *data;
1085};
1086
1087struct rtp_packet_info test_rtp_packets1[] = {
1088 /* RTP: SeqNo=0, TS=0 */
1089 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001090 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001091 /* RTP: SeqNo=1, TS=160 */
1092 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001093 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001094 /* RTP: SeqNo=2, TS=320 */
1095 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001096 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001097 /* Repeat RTP timestamp: */
1098 /* RTP: SeqNo=3, TS=320 */
1099 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001100 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001101 /* RTP: SeqNo=4, TS=480 */
1102 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001103 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001104 /* RTP: SeqNo=5, TS=640 */
1105 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001106 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001107 /* Double skip RTP timestamp (delta = 2*160): */
1108 /* RTP: SeqNo=6, TS=960 */
1109 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001110 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001111 /* RTP: SeqNo=7, TS=1120 */
1112 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001113 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001114 /* RTP: SeqNo=8, TS=1280 */
1115 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001116 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001117 /* Non 20ms RTP timestamp (delta = 120): */
1118 /* RTP: SeqNo=9, TS=1400 */
1119 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001120 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001121 /* RTP: SeqNo=10, TS=1560 */
1122 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001123 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001124 /* RTP: SeqNo=11, TS=1720 */
1125 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001126 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001127 /* SSRC changed to 0x10203040, RTP timestamp jump */
1128 /* RTP: SeqNo=12, TS=34688 */
1129 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001130 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001131 /* RTP: SeqNo=13, TS=34848 */
1132 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001133 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001134 /* RTP: SeqNo=14, TS=35008 */
1135 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001136 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001137 /* Non 20ms RTP timestamp (delta = 120): */
1138 /* RTP: SeqNo=15, TS=35128 */
1139 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001140 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001141 /* RTP: SeqNo=16, TS=35288 */
1142 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001143 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001144 /* RTP: SeqNo=17, TS=35448 */
1145 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001146 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001147 /* SeqNo increment by 2, RTP timestamp delta = 320: */
1148 /* RTP: SeqNo=19, TS=35768 */
1149 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001150 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001151 /* RTP: SeqNo=20, TS=35928 */
1152 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001153 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001154 /* RTP: SeqNo=21, TS=36088 */
1155 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001156 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001157 /* Repeat last packet */
1158 /* RTP: SeqNo=21, TS=36088 */
1159 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001160 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001161 /* RTP: SeqNo=22, TS=36248 */
1162 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001163 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001164 /* RTP: SeqNo=23, TS=36408 */
1165 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001166 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001167 /* Don't increment SeqNo but increment timestamp by 160 */
1168 /* RTP: SeqNo=23, TS=36568 */
1169 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001170 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001171 /* RTP: SeqNo=24, TS=36728 */
1172 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001173 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001174 /* RTP: SeqNo=25, TS=36888 */
1175 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001176 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001177 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
1178 * SeqNo jump */
1179 /* RTP: SeqNo=1000, TS=160000 */
1180 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001181 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001182 /* RTP: SeqNo=1001, TS=160160 */
1183 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001184 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001185 /* RTP: SeqNo=1002, TS=160320 */
1186 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001187 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001188};
1189
Philipp Maier87bd9be2017-08-22 16:35:41 +02001190void mgcp_patch_and_count(struct mgcp_endpoint *endp,
1191 struct mgcp_rtp_state *state,
1192 struct mgcp_rtp_end *rtp_end,
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001193 struct sockaddr_in *addr, struct msgb *msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001194
1195static void test_packet_error_detection(int patch_ssrc, int patch_ts)
1196{
1197 int i;
1198
1199 struct mgcp_trunk_config trunk;
1200 struct mgcp_endpoint endp;
1201 struct mgcp_rtp_state state;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001202 struct mgcp_rtp_end *rtp;
1203 struct sockaddr_in addr = { 0 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001204 uint32_t last_ssrc = 0;
1205 uint32_t last_timestamp = 0;
1206 uint32_t last_seqno = 0;
Philipp Maier9e1d1642018-05-09 16:26:34 +02001207 uint64_t last_in_ts_err_cnt = 0;
1208 uint64_t last_out_ts_err_cnt = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001209 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001210 struct mgcp_conn *_conn = NULL;
Philipp Maier9e1d1642018-05-09 16:26:34 +02001211 struct rate_ctr test_ctr_in;
1212 struct rate_ctr test_ctr_out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001213
1214 printf("Testing packet error detection%s%s.\n",
1215 patch_ssrc ? ", patch SSRC" : "",
1216 patch_ts ? ", patch timestamps" : "");
1217
1218 memset(&trunk, 0, sizeof(trunk));
1219 memset(&endp, 0, sizeof(endp));
1220 memset(&state, 0, sizeof(state));
1221
Philipp Maier9e1d1642018-05-09 16:26:34 +02001222 memset(&test_ctr_in, 0, sizeof(test_ctr_in));
1223 memset(&test_ctr_out, 0, sizeof(test_ctr_out));
1224 state.in_stream.err_ts_ctr = &test_ctr_in;
1225 state.out_stream.err_ts_ctr = &test_ctr_out;
1226
Philipp Maier87bd9be2017-08-22 16:35:41 +02001227 endp.type = &ep_typeset.rtp;
1228
Philipp Maierfcd06552017-11-10 17:32:22 +01001229 trunk.vty_number_endpoints = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001230 trunk.endpoints = &endp;
1231 trunk.force_constant_ssrc = patch_ssrc;
1232 trunk.force_aligned_timing = patch_ts;
1233
1234 endp.tcfg = &trunk;
1235
Philipp Maier87bd9be2017-08-22 16:35:41 +02001236 INIT_LLIST_HEAD(&endp.conns);
Philipp Maierffd75e42017-11-22 11:44:50 +01001237 _conn = mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
1238 "test-connection");
1239 OSMO_ASSERT(_conn);
1240 conn = mgcp_conn_get_rtp(&endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001241 OSMO_ASSERT(conn);
1242
1243 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001244
Philipp Maierbc0346e2018-06-07 09:52:16 +02001245 OSMO_ASSERT(mgcp_codec_add(conn, PTYPE_UNDEFINED, "AMR/8000/1") == 0);
1246 rtp->codec = &rtp->codecs[0];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001247
1248 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
1249 struct rtp_packet_info *info = test_rtp_packets1 + i;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001250 struct msgb *msg = msgb_alloc(4096, __func__);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001251
1252 force_monotonic_time_us = round(1000000.0 * info->txtime);
1253
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001254 OSMO_ASSERT(info->len <= msgb_tailroom(msg));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001255 OSMO_ASSERT(info->len >= 0);
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001256 msg->l3h = msgb_put(msg, info->len);
1257 memcpy((char*)msgb_l3(msg), info->data, info->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001258 mgcp_rtp_end_config(&endp, 1, rtp);
1259
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001260 mgcp_patch_and_count(&endp, &state, rtp, &addr, msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001261
1262 if (state.out_stream.ssrc != last_ssrc) {
1263 printf("Output SSRC changed to %08x\n",
1264 state.out_stream.ssrc);
1265 last_ssrc = state.out_stream.ssrc;
1266 }
1267
1268 printf("In TS: %d, dTS: %d, Seq: %d\n",
1269 state.in_stream.last_timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001270 state.in_stream.last_tsdelta, state.in_stream.last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001271
1272 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
Philipp Maier9e1d1642018-05-09 16:26:34 +02001273 "TS Err change: in +%u, out +%u\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001274 state.out_stream.last_timestamp - last_timestamp,
1275 state.out_stream.last_tsdelta,
1276 state.out_stream.last_seq - last_seqno,
Philipp Maier9e1d1642018-05-09 16:26:34 +02001277 (unsigned int) (state.in_stream.err_ts_ctr->current - last_in_ts_err_cnt),
1278 (unsigned int) (state.out_stream.err_ts_ctr->current - last_out_ts_err_cnt));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001279
1280 printf("Stats: Jitter = %u, Transit = %d\n",
Harald Welte49e3d5a2017-12-25 09:47:57 +01001281 calc_jitter(&state), state.stats.transit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001282
Philipp Maier9e1d1642018-05-09 16:26:34 +02001283 last_in_ts_err_cnt = state.in_stream.err_ts_ctr->current;
1284 last_out_ts_err_cnt = state.out_stream.err_ts_ctr->current;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001285 last_timestamp = state.out_stream.last_timestamp;
1286 last_seqno = state.out_stream.last_seq;
Neels Hofmeyr3243c7c2018-09-30 05:01:20 +02001287
1288 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001289 }
1290
1291 force_monotonic_time_us = -1;
Neels Hofmeyrd20910c2017-11-18 21:27:50 +01001292 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001293}
1294
1295static void test_multilple_codec(void)
1296{
1297 struct mgcp_config *cfg;
1298 struct mgcp_endpoint *endp;
1299 struct msgb *inp, *resp;
1300 struct in_addr addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001301 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001302 char conn_id[256];
Philipp Maiera74c0ea2018-08-02 11:59:09 +02001303 int i;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001304
1305 printf("Testing multiple payload types\n");
1306
1307 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001308 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001309 mgcp_endpoints_allocate(&cfg->trunk);
1310 cfg->policy_cb = mgcp_test_policy_cb;
1311 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1312
1313 /* Allocate endpoint 1@mgw with two codecs */
1314 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001315 inp = create_msg(CRCX_MULT_1, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001316 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001317 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1318 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001319 msgb_free(inp);
1320 msgb_free(resp);
1321
1322 OSMO_ASSERT(last_endpoint == 1);
1323 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001324 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001325 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001326 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001327
1328 /* Allocate 2@mgw with three codecs, last one ignored */
1329 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001330 inp = create_msg(CRCX_MULT_2, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001331 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001332 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1333 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001334 msgb_free(inp);
1335 msgb_free(resp);
1336
1337 OSMO_ASSERT(last_endpoint == 2);
1338 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001339 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001340 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001341 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001342
Philipp Maierbc0346e2018-06-07 09:52:16 +02001343 /* Allocate 3@mgw with no codecs, check for PT == 0 */
1344 /* Note: It usually makes no sense to leave the payload type list
1345 * out. However RFC 2327 does not clearly forbid this case and
1346 * it makes and since we already decided in OS#2658 that a missing
1347 * LCO should pick a sane default codec, it makes sense to expect
1348 * the same behaviour if SDP lacks proper payload type information */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001349 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001350 inp = create_msg(CRCX_MULT_3, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001351 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001352 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1353 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001354 msgb_free(inp);
1355 msgb_free(resp);
1356
1357 OSMO_ASSERT(last_endpoint == 3);
1358 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001359 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001360 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001361 OSMO_ASSERT(conn->end.codec->payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001362
1363 /* Allocate 4@mgw with a single codec */
1364 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001365 inp = create_msg(CRCX_MULT_4, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001366 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001367 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1368 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001369 msgb_free(inp);
1370 msgb_free(resp);
1371
1372 OSMO_ASSERT(last_endpoint == 4);
1373 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001374 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001375 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001376 OSMO_ASSERT(conn->end.codec->payload_type == 18);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001377
1378 /* Allocate 5@mgw at select GSM.. */
1379 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001380 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001381 talloc_free(cfg->trunk.audio_name);
1382 cfg->trunk.audio_name = "GSM/8000";
1383 cfg->trunk.no_audio_transcoding = 1;
1384 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001385 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1386 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001387 msgb_free(inp);
1388 msgb_free(resp);
1389
1390 OSMO_ASSERT(last_endpoint == 5);
1391 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001392 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001393 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001394 OSMO_ASSERT(conn->end.codec->payload_type == 3);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001395
Philipp Maierffd75e42017-11-22 11:44:50 +01001396 inp = create_msg(MDCX_NAT_DUMMY, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001397 last_endpoint = -1;
1398 resp = mgcp_handle_message(cfg, inp);
1399 msgb_free(inp);
1400 msgb_free(resp);
1401 OSMO_ASSERT(last_endpoint == 5);
1402 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001403 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001404 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001405 OSMO_ASSERT(conn->end.codec->payload_type == 3);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001406 OSMO_ASSERT(conn->end.rtp_port == htons(16434));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001407 memset(&addr, 0, sizeof(addr));
1408 inet_aton("8.8.8.8", &addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001409 OSMO_ASSERT(conn->end.addr.s_addr == addr.s_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001410
1411 /* Check what happens without that flag */
1412
Philipp Maier87bd9be2017-08-22 16:35:41 +02001413 /* Free the previous endpoint and the data and
1414 * check if the connection really vanished... */
Philipp Maier1355d7e2018-02-01 14:30:06 +01001415 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001416 talloc_free(endp->last_response);
1417 talloc_free(endp->last_trans);
1418 endp->last_response = endp->last_trans = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001419 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001420 OSMO_ASSERT(!conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001421
1422 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001423 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001424 cfg->trunk.no_audio_transcoding = 0;
1425 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001426 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1427 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001428 msgb_free(inp);
1429 msgb_free(resp);
1430
1431 OSMO_ASSERT(last_endpoint == 5);
1432 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001433 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001434 OSMO_ASSERT(conn);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001435 OSMO_ASSERT(conn->end.codec->payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001436
Philipp Maiera74c0ea2018-08-02 11:59:09 +02001437 for (i = 1; i < cfg->trunk.number_endpoints; i++)
1438 mgcp_endp_release(&cfg->trunk.endpoints[i]);
1439
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001440 talloc_free(cfg);
1441}
1442
1443static void test_no_cycle(void)
1444{
1445 struct mgcp_config *cfg;
1446 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001447 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001448 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001449
1450 printf("Testing no sequence flow on initial packet\n");
1451
1452 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001453 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001454 mgcp_endpoints_allocate(&cfg->trunk);
1455
1456 endp = &cfg->trunk.endpoints[1];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001457
Philipp Maierffd75e42017-11-22 11:44:50 +01001458 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP,
1459 "test-connection");
1460 OSMO_ASSERT(_conn);
1461 conn = mgcp_conn_get_rtp(endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001462 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001463
Harald Welte49e3d5a2017-12-25 09:47:57 +01001464 OSMO_ASSERT(conn->state.stats.initialized == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001465
1466 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001467 OSMO_ASSERT(conn->state.stats.initialized == 1);
1468 OSMO_ASSERT(conn->state.stats.cycles == 0);
1469 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001470
1471 mgcp_rtp_annex_count(endp, &conn->state, 1, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001472 OSMO_ASSERT(conn->state.stats.initialized == 1);
1473 OSMO_ASSERT(conn->state.stats.cycles == 0);
1474 OSMO_ASSERT(conn->state.stats.max_seq == 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001475
1476 /* now jump.. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001477 mgcp_rtp_annex_count(endp, &conn->state, UINT16_MAX, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001478 OSMO_ASSERT(conn->state.stats.initialized == 1);
1479 OSMO_ASSERT(conn->state.stats.cycles == 0);
1480 OSMO_ASSERT(conn->state.stats.max_seq == UINT16_MAX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001481
1482 /* and wrap */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001483 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
Harald Welte49e3d5a2017-12-25 09:47:57 +01001484 OSMO_ASSERT(conn->state.stats.initialized == 1);
1485 OSMO_ASSERT(conn->state.stats.cycles == UINT16_MAX + 1);
1486 OSMO_ASSERT(conn->state.stats.max_seq == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001487
Philipp Maier1355d7e2018-02-01 14:30:06 +01001488 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001489 talloc_free(cfg);
1490}
1491
1492static void test_no_name(void)
1493{
1494 struct mgcp_config *cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001495 struct msgb *inp, *msg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001496
1497 printf("Testing no rtpmap name\n");
1498 cfg = mgcp_config_alloc();
1499
Philipp Maierfcd06552017-11-10 17:32:22 +01001500 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001501 cfg->trunk.audio_send_name = 0;
1502 mgcp_endpoints_allocate(&cfg->trunk);
1503
1504 cfg->policy_cb = mgcp_test_policy_cb;
1505
1506 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1507
Philipp Maierffd75e42017-11-22 11:44:50 +01001508 inp = create_msg(CRCX, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001509 msg = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001510
1511 if (check_response(msg->data, CRCX_RET_NO_RTPMAP) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001512 printf("FAILED: there should not be a RTPMAP: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001513 (char *)msg->data);
1514 OSMO_ASSERT(false);
1515 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001516 msgb_free(inp);
1517 msgb_free(msg);
1518
Philipp Maier1355d7e2018-02-01 14:30:06 +01001519 mgcp_endp_release(&cfg->trunk.endpoints[1]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001520 talloc_free(cfg);
1521}
1522
1523static void test_osmux_cid(void)
1524{
1525 int id, i;
1526
1527 OSMO_ASSERT(osmux_used_cid() == 0);
1528 id = osmux_get_cid();
1529 OSMO_ASSERT(id == 0);
1530 OSMO_ASSERT(osmux_used_cid() == 1);
1531 osmux_put_cid(id);
1532 OSMO_ASSERT(osmux_used_cid() == 0);
1533
1534 for (i = 0; i < 256; ++i) {
1535 id = osmux_get_cid();
1536 OSMO_ASSERT(id == i);
1537 OSMO_ASSERT(osmux_used_cid() == i + 1);
1538 }
1539
1540 id = osmux_get_cid();
1541 OSMO_ASSERT(id == -1);
1542
1543 for (i = 0; i < 256; ++i)
1544 osmux_put_cid(i);
1545 OSMO_ASSERT(osmux_used_cid() == 0);
1546}
1547
1548static const struct log_info_cat log_categories[] = {
1549};
1550
1551const struct log_info log_info = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001552 .cat = log_categories,
1553 .num_cat = ARRAY_SIZE(log_categories),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001554};
1555
Philipp Maier3d7b58d2018-06-06 09:35:31 +02001556static void test_get_lco_identifier(void)
1557{
1558 char *test;
1559 printf("Testing get_lco_identifier()\n");
1560
1561 /* Normal case at the beginning */
1562 test = "p:10, a:PCMU";
1563 printf("%s -> %s\n", test, get_lco_identifier(test));
1564
1565 test = "p:10, a:PCMU";
1566 printf("%s -> %s\n", test, get_lco_identifier(test));
1567
1568 /* Begin parsing in the middle of the value part of
1569 * the previous LCO option value */
1570 test = "XXXX, p:10, a:PCMU";
1571 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1572
1573 test = "XXXX,p:10,a:PCMU";
1574 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1575
1576 test = "10,a:PCMU";
1577 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1578
1579 test = "10, a:PCMU";
1580 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1581
1582 test = "10,a: PCMU";
1583 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1584
1585 test = "10 ,a: PCMU";
1586 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1587
1588 /* Begin parsing right at the end of the previous LCO
1589 * option value */
1590 test = ", a:PCMU";
1591 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1592
1593 test = " a:PCMU";
1594 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1595
1596 /* Empty string, result should be (null) */
1597 test = "";
1598 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1599
1600 /* Missing colons, result should be (null) */
1601 test = "p10, aPCMU";
1602 printf("%s -> %s\n", test, get_lco_identifier(test));
1603
1604 /* Colon separated from the identifier, result should be (null) */
1605 test = "10,a :PCMU";
1606 printf("'%s' -> '%s'\n", test, get_lco_identifier(test));
1607}
1608
1609static void test_check_local_cx_options(void *ctx)
1610{
1611 /* Legal cases */
1612 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU") == 0);
1613 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU") == 0);
1614 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, p:10, IN:10") == 0);
1615 OSMO_ASSERT(check_local_cx_options(ctx, "one:AAA, two:BB, tree:C") ==
1616 0);
1617 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU") == 0);
1618 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:G726-32") == 0);
1619 OSMO_ASSERT(check_local_cx_options(ctx, "p:10-20, b:64") == 0);
1620 OSMO_ASSERT(check_local_cx_options(ctx, "b:32-64, e:off") == 0);
1621 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU;G726-32") == 0);
1622
1623 /* Illegal spaces before and after colon */
1624 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, p :10") == -1);
1625 OSMO_ASSERT(check_local_cx_options(ctx, "a :PCMU, p:10") == -1);
1626 OSMO_ASSERT(check_local_cx_options(ctx, "p: 10, a:PCMU") == -1);
1627
1628 /* Check if multiple appearances of LCOs are rejected */
1629 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU, p:10") == -1);
1630 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:PCMU, a:PCMU, p:10") ==
1631 -1);
1632 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, p:10") == -1);
1633
1634 /* Check if empty LCO are rejected */
1635 OSMO_ASSERT(check_local_cx_options(ctx, "p: , a:PCMU") == -1);
1636 OSMO_ASSERT(check_local_cx_options(ctx, "p: , a: PCMU") == -1);
1637 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a: PCMU") == -1);
1638 OSMO_ASSERT(check_local_cx_options(ctx, "p:, a:PCMU") == -1);
1639 OSMO_ASSERT(check_local_cx_options(ctx, "p:10, a:") == -1);
1640
1641 /* Garbeled beginning and ends */
1642 OSMO_ASSERT(check_local_cx_options(ctx, ":10, a:10") == -1);
1643 OSMO_ASSERT(check_local_cx_options(ctx, "10, a:PCMU") == -1);
1644 OSMO_ASSERT(check_local_cx_options(ctx, ", a:PCMU") == -1);
1645 OSMO_ASSERT(check_local_cx_options(ctx, " a:PCMU") == -1);
1646 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU,") == -1);
1647 OSMO_ASSERT(check_local_cx_options(ctx, "a:PCMU, ") == -1);
1648
1649 /* Illegal strings */
1650 OSMO_ASSERT(check_local_cx_options(ctx, " ") == -1);
1651 OSMO_ASSERT(check_local_cx_options(ctx, "") == -1);
1652 OSMO_ASSERT(check_local_cx_options(ctx, "AAA") == -1);
1653 OSMO_ASSERT(check_local_cx_options(ctx, ":,") == -1);
1654 OSMO_ASSERT(check_local_cx_options(ctx, ",:") == -1);
1655 OSMO_ASSERT(check_local_cx_options(ctx, ",,,") == -1);
1656}
1657
Philipp Maier6931f9a2018-07-26 09:29:31 +02001658static void test_mgcp_codec_pt_translate_pars(struct mgcp_rtp_codec *c)
1659{
1660 c->rate = 8000;
1661 c->channels = 1;
1662 c->frame_duration_num = 23;
1663 c->frame_duration_den = 42;
1664}
1665
1666static void test_mgcp_codec_pt_translate(void)
1667{
1668 struct mgcp_conn_rtp conn_src;
1669 struct mgcp_conn_rtp conn_dst;
1670 int pt_dst;
1671
1672 /* Setup a realistic set of codec configurations on both
1673 * ends. AMR and HR will use different payload types. PCMU
1674 * must use 0 on both ends since this is not a dynamic payload
1675 * type */
1676 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[0]);
1677 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[0]);
1678 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[1]);
1679 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[1]);
1680 test_mgcp_codec_pt_translate_pars(&conn_src.end.codecs[2]);
1681 test_mgcp_codec_pt_translate_pars(&conn_dst.end.codecs[2]);
1682 conn_src.end.codecs[0].payload_type = 112;
1683 conn_dst.end.codecs[0].payload_type = 96;
1684 conn_src.end.codecs[1].payload_type = 0;
1685 conn_dst.end.codecs[1].payload_type = 0;
1686 conn_src.end.codecs[2].payload_type = 111;
1687 conn_dst.end.codecs[2].payload_type = 97;
1688 conn_src.end.codecs[0].audio_name = "AMR/8000/1";
1689 conn_dst.end.codecs[0].audio_name = "AMR/8000/1";
1690 conn_src.end.codecs[1].audio_name = "PCMU/8000/1";
1691 conn_dst.end.codecs[1].audio_name = "PCMU/8000/1";
1692 conn_src.end.codecs[2].audio_name = "GSM-HR-08/8000/1";
1693 conn_dst.end.codecs[2].audio_name = "GSM-HR-08/8000/1";
1694 conn_src.end.codecs[0].subtype_name = "AMR";
1695 conn_dst.end.codecs[0].subtype_name = "AMR";
1696 conn_src.end.codecs[1].subtype_name = "PCMU";
1697 conn_dst.end.codecs[1].subtype_name = "PCMU";
1698 conn_src.end.codecs[2].subtype_name = "GSM-HR-08";
1699 conn_dst.end.codecs[2].subtype_name = "GSM-HR-08";
1700 conn_src.end.codecs_assigned = 3;
1701 conn_dst.end.codecs_assigned = 3;
1702
1703 /* We expect the function to find the PT we must use when we send the
1704 * packet out to the destination. All we know is the context for both
1705 * connections and the payload type from the source packet */
1706 pt_dst =
1707 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1708 conn_src.end.codecs[0].payload_type);
1709 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[0].payload_type);
1710 pt_dst =
1711 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1712 conn_src.end.codecs[1].payload_type);
1713 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[1].payload_type);
1714 pt_dst =
1715 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1716 conn_src.end.codecs[2].payload_type);
1717 OSMO_ASSERT(pt_dst == conn_dst.end.codecs[2].payload_type);
1718
1719 /* Try some constellations that must fail */
1720 pt_dst = mgcp_codec_pt_translate(&conn_src, &conn_dst, 123);
1721 OSMO_ASSERT(pt_dst == -EINVAL);
1722 conn_src.end.codecs_assigned = 0;
1723 conn_dst.end.codecs_assigned = 3;
1724 pt_dst =
1725 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1726 conn_src.end.codecs[0].payload_type);
1727 OSMO_ASSERT(pt_dst == -EINVAL);
1728 pt_dst =
1729 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1730 conn_src.end.codecs[1].payload_type);
1731 OSMO_ASSERT(pt_dst == -EINVAL);
1732 pt_dst =
1733 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1734 conn_src.end.codecs[2].payload_type);
1735 OSMO_ASSERT(pt_dst == -EINVAL);
1736 conn_src.end.codecs_assigned = 3;
1737 conn_dst.end.codecs_assigned = 0;
1738 pt_dst =
1739 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1740 conn_src.end.codecs[0].payload_type);
1741 OSMO_ASSERT(pt_dst == -EINVAL);
1742 pt_dst =
1743 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1744 conn_src.end.codecs[1].payload_type);
1745 OSMO_ASSERT(pt_dst == -EINVAL);
1746 pt_dst =
1747 mgcp_codec_pt_translate(&conn_src, &conn_dst,
1748 conn_src.end.codecs[2].payload_type);
1749 OSMO_ASSERT(pt_dst == -EINVAL);
1750}
1751
Neels Hofmeyr65317262018-09-03 22:11:05 +02001752void test_conn_id_matching()
1753{
1754 struct mgcp_endpoint endp = {};
1755 struct mgcp_conn *conn;
1756 struct mgcp_conn *conn_match;
1757 int i;
1758 const char *conn_id_generated = "000023AB";
1759 const char *conn_id_request[] = {
Neels Hofmeyra77eade2018-08-29 02:30:39 +02001760 "23AB",
1761 "0023AB",
Neels Hofmeyr65317262018-09-03 22:11:05 +02001762 "000023AB",
Neels Hofmeyra77eade2018-08-29 02:30:39 +02001763 "00000023AB",
1764 "23ab",
1765 "0023ab",
Neels Hofmeyr65317262018-09-03 22:11:05 +02001766 "000023ab",
Neels Hofmeyra77eade2018-08-29 02:30:39 +02001767 "00000023ab",
Neels Hofmeyr65317262018-09-03 22:11:05 +02001768 };
1769
1770 printf("\nTesting %s\n", __func__);
1771
1772 INIT_LLIST_HEAD(&endp.conns);
1773
1774 conn = talloc_zero(NULL, struct mgcp_conn);
1775 OSMO_ASSERT(conn);
1776 osmo_strlcpy(conn->id, conn_id_generated, sizeof(conn->id));
1777 llist_add(&conn->entry, &endp.conns);
1778
1779 for (i = 0; i < ARRAY_SIZE(conn_id_request); i++) {
1780 const char *needle = conn_id_request[i];
1781 printf("needle='%s' ", needle);
1782 conn_match = mgcp_conn_get(&endp, needle);
1783 OSMO_ASSERT(conn_match);
1784 printf("found '%s'\n", conn_match->id);
1785 OSMO_ASSERT(conn_match == conn);
1786 }
1787
1788 llist_del(&conn->entry);
1789 talloc_free(conn);
1790}
1791
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001792int main(int argc, char **argv)
1793{
Neels Hofmeyr60f8e312018-03-30 23:01:07 +02001794 void *ctx = talloc_named_const(NULL, 0, "mgcp_test");
1795 void *msgb_ctx = msgb_talloc_ctx_init(ctx, 0);
1796 osmo_init_logging2(ctx, &log_info);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001797
1798 test_strline();
1799 test_values();
1800 test_messages();
1801 test_retransmission();
1802 test_packet_loss_calc();
1803 test_rqnt_cb();
1804 test_mgcp_stats();
1805 test_packet_error_detection(1, 0);
1806 test_packet_error_detection(0, 0);
1807 test_packet_error_detection(0, 1);
1808 test_packet_error_detection(1, 1);
1809 test_multilple_codec();
1810 test_no_cycle();
1811 test_no_name();
1812 test_osmux_cid();
Philipp Maier3d7b58d2018-06-06 09:35:31 +02001813 test_get_lco_identifier();
1814 test_check_local_cx_options(ctx);
Philipp Maier6931f9a2018-07-26 09:29:31 +02001815 test_mgcp_codec_pt_translate();
Neels Hofmeyr65317262018-09-03 22:11:05 +02001816 test_conn_id_matching();
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001817
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001818 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
1819 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
1820 talloc_free(msgb_ctx);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001821 printf("Done\n");
1822 return EXIT_SUCCESS;
1823}