blob: 1c57347b4856ffff25a5a4df08fba27f3398ab91 [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>
28#include <osmocom/mgcp/mgcp_ep.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020029
30#include <osmocom/core/application.h>
31#include <osmocom/core/talloc.h>
32#include <osmocom/core/utils.h>
33#include <string.h>
34#include <limits.h>
35#include <dlfcn.h>
36#include <time.h>
37#include <math.h>
38
39char *strline_r(char *str, char **saveptr);
40
41const char *strline_test_data =
42 "one CR\r"
43 "two CR\r"
44 "\r"
45 "one CRLF\r\n"
46 "two CRLF\r\n"
Philipp Maier87bd9be2017-08-22 16:35:41 +020047 "\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 +020048
49#define EXPECTED_NUMBER_OF_LINES 13
50
51static void test_strline(void)
52{
53 char *save = NULL;
54 char *line;
55 char buf[2048];
56 int counter = 0;
57
58 osmo_strlcpy(buf, strline_test_data, sizeof(buf));
59
Philipp Maier87bd9be2017-08-22 16:35:41 +020060 for (line = mgcp_strline(buf, &save); line;
61 line = mgcp_strline(NULL, &save)) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020062 printf("line: '%s'\n", line);
63 counter++;
64 }
65
66 OSMO_ASSERT(counter == EXPECTED_NUMBER_OF_LINES);
67}
68
69#define AUEP1 "AUEP 158663169 ds/e1-1/2@172.16.6.66 MGCP 1.0\r\n"
70#define AUEP1_RET "200 158663169 OK\r\n"
71#define AUEP2 "AUEP 18983213 ds/e1-2/1@172.16.6.66 MGCP 1.0\r\n"
72#define AUEP2_RET "500 18983213 FAIL\r\n"
73#define EMPTY "\r\n"
74#define EMPTY_RET NULL
75#define SHORT "CRCX \r\n"
76#define SHORT_RET "510 000000 FAIL\r\n"
77
78#define MDCX_WRONG_EP "MDCX 18983213 ds/e1-3/1@172.16.6.66 MGCP 1.0\r\n"
79#define MDCX_ERR_RET "510 18983213 FAIL\r\n"
80#define MDCX_UNALLOCATED "MDCX 18983214 ds/e1-1/2@172.16.6.66 MGCP 1.0\r\n"
81#define MDCX_RET "400 18983214 FAIL\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020082
Philipp Maier87bd9be2017-08-22 16:35:41 +020083#define MDCX3 \
84 "MDCX 18983215 1@mgw MGCP 1.0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010085 "I: %s\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020086
Philipp Maier87bd9be2017-08-22 16:35:41 +020087#define MDCX3_RET \
88 "200 18983215 OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010089 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020090 "\n" \
91 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010092 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020093 "s=-\r\n" \
94 "c=IN IP4 0.0.0.0\r\n" \
95 "t=0 0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +010096 "m=audio 16002 RTP/AVP 97\r\n" \
97 "a=rtpmap:97 GSM-EFR/8000\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +020098 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020099
Philipp Maier87bd9be2017-08-22 16:35:41 +0200100#define MDCX3A_RET \
101 "200 18983215 OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100102 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200103 "\n" \
104 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100105 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200106 "s=-\r\n" \
107 "c=IN IP4 0.0.0.0\r\n" \
108 "t=0 0\r\n" \
109 "m=audio 16002 RTP/AVP 97\r\n" \
110 "a=rtpmap:97 GSM-EFR/8000\r\n" \
111 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200112
Philipp Maier87bd9be2017-08-22 16:35:41 +0200113#define MDCX3_FMTP_RET \
114 "200 18983215 OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100115 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200116 "\n" \
117 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100118 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200119 "s=-\r\n" \
120 "c=IN IP4 0.0.0.0\r\n" \
121 "t=0 0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100122 "m=audio 16006 RTP/AVP 97\r\n" \
123 "a=rtpmap:97 GSM-EFR/8000\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200124 "a=fmtp:126 0/1/2\r\n" \
125 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200126
Philipp Maier87bd9be2017-08-22 16:35:41 +0200127#define MDCX4 \
128 "MDCX 18983216 1@mgw MGCP 1.0\r\n" \
129 "M: sendrecv\r" \
130 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100131 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200132 "L: p:20, a:AMR, nt:IN\r\n" \
133 "\n" \
134 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100135 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200136 "c=IN IP4 0.0.0.0\r\n" \
137 "t=0 0\r\n" \
138 "m=audio 4441 RTP/AVP 99\r\n" \
139 "a=rtpmap:99 AMR/8000\r\n" \
140 "a=ptime:40\r\n"
141
142#define MDCX4_RET(Ident) \
143 "200 " Ident " OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100144 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200145 "\n" \
146 "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 Maierffd75e42017-11-22 11:44:50 +0100157 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200158 "\n" \
159 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100160 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200161 "s=-\r\n" \
162 "c=IN IP4 0.0.0.0\r\n" \
163 "t=0 0\r\n" \
164 "m=audio 16002 RTP/AVP 96\r\n" \
165 "a=rtpmap:96 AMR\r\n" \
166 "a=ptime:40\r\n"
167
168#define MDCX4_PT1 \
169 "MDCX 18983217 1@mgw MGCP 1.0\r\n" \
170 "M: sendrecv\r" \
171 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100172 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200173 "L: p:20-40, a:AMR, nt:IN\r\n" \
174 "\n" \
175 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100176 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200177 "c=IN IP4 0.0.0.0\r\n" \
178 "t=0 0\r\n" \
179 "m=audio 4441 RTP/AVP 99\r\n" \
180 "a=rtpmap:99 AMR/8000\r\n" \
181 "a=ptime:40\r\n"
182
183#define MDCX4_PT2 \
184 "MDCX 18983218 1@mgw MGCP 1.0\r\n" \
185 "M: sendrecv\r" \
186 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100187 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200188 "L: p:20-20, a:AMR, nt:IN\r\n" \
189 "\n" \
190 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100191 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200192 "c=IN IP4 0.0.0.0\r\n" \
193 "t=0 0\r\n" \
194 "m=audio 4441 RTP/AVP 99\r\n" \
195 "a=rtpmap:99 AMR/8000\r\n" \
196 "a=ptime:40\r\n"
197
198#define MDCX4_PT3 \
199 "MDCX 18983219 1@mgw MGCP 1.0\r\n" \
200 "M: sendrecv\r" \
201 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100202 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200203 "L: a:AMR, nt:IN\r\n" \
204 "\n" \
205 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100206 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200207 "c=IN IP4 0.0.0.0\r\n" \
208 "t=0 0\r\n" \
209 "m=audio 4441 RTP/AVP 99\r\n" \
210 "a=rtpmap:99 AMR/8000\r\n" \
211 "a=ptime:40\r\n"
212
213#define MDCX4_SO \
214 "MDCX 18983220 1@mgw MGCP 1.0\r\n" \
215 "M: sendonly\r" \
216 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100217 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200218 "L: p:20, a:AMR, nt:IN\r\n" \
219 "\n" \
220 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100221 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200222 "c=IN IP4 0.0.0.0\r\n" \
223 "t=0 0\r\n" \
224 "m=audio 4441 RTP/AVP 99\r\n" \
225 "a=rtpmap:99 AMR/8000\r\n" \
226 "a=ptime:40\r\n"
227
228#define MDCX4_RO \
229 "MDCX 18983221 1@mgw MGCP 1.0\r\n" \
230 "M: recvonly\r" \
231 "C: 2\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100232 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200233 "L: p:20, a:AMR, nt:IN\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200234
235#define SHORT2 "CRCX 1"
236#define SHORT2_RET "510 000000 FAIL\r\n"
237#define SHORT3 "CRCX 1 1@mgw"
238#define SHORT4 "CRCX 1 1@mgw MGCP"
239#define SHORT5 "CRCX 1 1@mgw MGCP 1.0"
240
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241#define CRCX \
242 "CRCX 2 1@mgw MGCP 1.0\r\n" \
243 "M: recvonly\r\n" \
244 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200245 "L: p:20\r\n" \
246 "\r\n" \
247 "v=0\r\n" \
248 "c=IN IP4 123.12.12.123\r\n" \
249 "m=audio 5904 RTP/AVP 97\r\n" \
250 "a=rtpmap:97 GSM-EFR/8000\r\n" \
251 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200252
Philipp Maier87bd9be2017-08-22 16:35:41 +0200253#define CRCX_RET \
254 "200 2 OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100255 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200256 "\n" \
257 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100258 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200259 "s=-\r\n" \
260 "c=IN IP4 0.0.0.0\r\n" \
261 "t=0 0\r\n" \
262 "m=audio 16002 RTP/AVP 97\r\n" \
263 "a=rtpmap:97 GSM-EFR/8000\r\n" \
264 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200265
Philipp Maier87bd9be2017-08-22 16:35:41 +0200266#define CRCX_RET_NO_RTPMAP \
267 "200 2 OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100268 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200269 "\n" \
270 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100271 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200272 "s=-\r\n" \
273 "c=IN IP4 0.0.0.0\r\n" \
274 "t=0 0\r\n" \
275 "m=audio 16002 RTP/AVP 97\r\n" \
276 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200277
Philipp Maier87bd9be2017-08-22 16:35:41 +0200278#define CRCX_FMTP_RET \
279 "200 2 OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100280 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200281 "\n" \
282 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100283 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200284 "s=-\r\n" \
285 "c=IN IP4 0.0.0.0\r\n" \
286 "t=0 0\r\n" \
287 "m=audio 16006 RTP/AVP 97\r\n" \
288 "a=rtpmap:97 GSM-EFR/8000\r\n" \
289 "a=fmtp:126 0/1/2\r\n" \
290 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200291
Philipp Maier87bd9be2017-08-22 16:35:41 +0200292#define CRCX_ZYN \
293 "CRCX 2 1@mgw MGCP 1.0\r" \
294 "M: recvonly\r" \
295 "C: 2\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200296 "\n" \
297 "v=0\r" \
298 "c=IN IP4 123.12.12.123\r" \
299 "m=audio 5904 RTP/AVP 97\r" \
300 "a=rtpmap:97 GSM-EFR/8000\r"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200301
Philipp Maier87bd9be2017-08-22 16:35:41 +0200302#define CRCX_ZYN_RET \
303 "200 2 OK\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100304 "I: %s\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200305 "\n" \
306 "v=0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100307 "o=- %s 23 IN IP4 0.0.0.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200308 "s=-\r\n" \
309 "c=IN IP4 0.0.0.0\r\n" \
310 "t=0 0\r\n" \
311 "m=audio 16004 RTP/AVP 97\r\n" \
312 "a=rtpmap:97 GSM-EFR/8000\r\n" \
313 "a=ptime:20\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200314
Philipp Maier87bd9be2017-08-22 16:35:41 +0200315#define DLCX \
316 "DLCX 7 1@mgw MGCP 1.0\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100317 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200318 "C: 2\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200319
Philipp Maier87bd9be2017-08-22 16:35:41 +0200320#define DLCX_RET \
321 "250 7 OK\r\n" \
322 "P: PS=0, OS=0, PR=0, OR=0, PL=0, JI=0\r\n" \
323 "X-Osmo-CP: EC TI=0, TO=0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200324
Philipp Maier87bd9be2017-08-22 16:35:41 +0200325#define RQNT \
326 "RQNT 186908780 1@mgw MGCP 1.0\r\n" \
327 "X: B244F267488\r\n" \
328 "S: D/9\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200329
Philipp Maier87bd9be2017-08-22 16:35:41 +0200330#define RQNT2 \
331 "RQNT 186908781 1@mgw MGCP 1.0\r\n" \
332 "X: ADD4F26746F\r\n" \
333 "R: D/[0-9#*](N), G/ft, fxr/t38\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200334
335#define RQNT1_RET "200 186908780 OK\r\n"
336#define RQNT2_RET "200 186908781 OK\r\n"
337
Philipp Maier87bd9be2017-08-22 16:35:41 +0200338#define PTYPE_IGNORE 0 /* == default initializer */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200339#define PTYPE_NONE 128
340#define PTYPE_NYI PTYPE_NONE
341
Philipp Maier87bd9be2017-08-22 16:35:41 +0200342#define CRCX_MULT_1 \
343 "CRCX 2 1@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200344 "M: recvonly\r\n" \
345 "C: 2\r\n" \
346 "X\r\n" \
347 "L: p:20\r\n" \
348 "\r\n" \
349 "v=0\r\n" \
350 "c=IN IP4 123.12.12.123\r\n" \
351 "m=audio 5904 RTP/AVP 18 97\r\n" \
352 "a=rtpmap:18 G729/8000\r\n" \
353 "a=rtpmap:97 GSM-EFR/8000\r\n" \
354 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200355
Philipp Maier87bd9be2017-08-22 16:35:41 +0200356#define CRCX_MULT_2 \
357 "CRCX 2 2@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200358 "M: recvonly\r\n" \
359 "C: 2\r\n" \
360 "X\r\n" \
361 "L: p:20\r\n" \
362 "\r\n" \
363 "v=0\r\n" \
364 "c=IN IP4 123.12.12.123\r\n" \
365 "m=audio 5904 RTP/AVP 18 97 101\r\n" \
366 "a=rtpmap:18 G729/8000\r\n" \
367 "a=rtpmap:97 GSM-EFR/8000\r\n" \
368 "a=rtpmap:101 FOO/8000\r\n" \
369 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200370
Philipp Maier87bd9be2017-08-22 16:35:41 +0200371#define CRCX_MULT_3 \
372 "CRCX 2 3@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200373 "M: recvonly\r\n" \
374 "C: 2\r\n" \
375 "X\r\n" \
376 "L: p:20\r\n" \
377 "\r\n" \
378 "v=0\r\n" \
379 "c=IN IP4 123.12.12.123\r\n" \
380 "m=audio 5904 RTP/AVP\r\n" \
381 "a=rtpmap:18 G729/8000\r\n" \
382 "a=rtpmap:97 GSM-EFR/8000\r\n" \
383 "a=rtpmap:101 FOO/8000\r\n" \
384 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200385
Philipp Maier87bd9be2017-08-22 16:35:41 +0200386#define CRCX_MULT_4 \
387 "CRCX 2 4@mgw MGCP 1.0\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200388 "M: recvonly\r\n" \
389 "C: 2\r\n" \
390 "X\r\n" \
391 "L: p:20\r\n" \
392 "\r\n" \
393 "v=0\r\n" \
394 "c=IN IP4 123.12.12.123\r\n" \
395 "m=audio 5904 RTP/AVP 18\r\n" \
396 "a=rtpmap:18 G729/8000\r\n" \
397 "a=rtpmap:97 GSM-EFR/8000\r\n" \
398 "a=rtpmap:101 FOO/8000\r\n" \
399 "a=ptime:40\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200400
401#define CRCX_MULT_GSM_EXACT \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200402 "CRCX 259260421 5@mgw MGCP 1.0\r\n" \
403 "C: 1355c6041e\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200404 "L: p:20, a:GSM, nt:IN\r\n" \
405 "M: recvonly\r\n" \
406 "\r\n" \
407 "v=0\r\n" \
408 "o=- 1439038275 1439038275 IN IP4 192.168.181.247\r\n" \
409 "s=-\r\nc=IN IP4 192.168.181.247\r\n" \
410 "t=0 0\r\nm=audio 29084 RTP/AVP 255 0 8 3 18 4 96 97 101\r\n" \
411 "a=rtpmap:0 PCMU/8000\r\n" \
412 "a=rtpmap:8 PCMA/8000\r\n" \
413 "a=rtpmap:3 gsm/8000\r\n" \
414 "a=rtpmap:18 G729/8000\r\n" \
415 "a=fmtp:18 annexb=no\r\n" \
416 "a=rtpmap:4 G723/8000\r\n" \
417 "a=rtpmap:96 iLBC/8000\r\n" \
418 "a=fmtp:96 mode=20\r\n" \
419 "a=rtpmap:97 iLBC/8000\r\n" \
420 "a=fmtp:97 mode=30\r\n" \
421 "a=rtpmap:101 telephone-event/8000\r\n" \
422 "a=fmtp:101 0-15\r\n" \
423 "a=recvonly\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200424
Philipp Maier87bd9be2017-08-22 16:35:41 +0200425#define MDCX_NAT_DUMMY \
426 "MDCX 23 5@mgw MGCP 1.0\r\n" \
427 "C: 1355c6041e\r\n" \
Philipp Maierffd75e42017-11-22 11:44:50 +0100428 "I: %s\r\n" \
Philipp Maier87bd9be2017-08-22 16:35:41 +0200429 "\r\n" \
430 "c=IN IP4 8.8.8.8\r\n" \
431 "m=audio 16434 RTP/AVP 255\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200432
433struct mgcp_test {
434 const char *name;
435 const char *req;
436 const char *exp_resp;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200437 int ptype;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200438 const char *extra_fmtp;
439};
440
441static const struct mgcp_test tests[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200442 {"AUEP1", AUEP1, AUEP1_RET},
443 {"AUEP2", AUEP2, AUEP2_RET},
444 {"MDCX1", MDCX_WRONG_EP, MDCX_ERR_RET},
445 {"MDCX2", MDCX_UNALLOCATED, MDCX_RET},
446 {"CRCX", CRCX, CRCX_RET, 97},
447 {"MDCX3", MDCX3, MDCX3_RET, PTYPE_IGNORE},
448 {"MDCX4", MDCX4, MDCX4_RET("18983216"), 99},
449 {"MDCX4_PT1", MDCX4_PT1, MDCX4_RET("18983217"), 99},
450 {"MDCX4_PT2", MDCX4_PT2, MDCX4_RET("18983218"), 99},
451 {"MDCX4_PT3", MDCX4_PT3, MDCX4_RET("18983219"), 99},
452 {"MDCX4_SO", MDCX4_SO, MDCX4_RET("18983220"), 99},
453 {"MDCX4_RO", MDCX4_RO, MDCX4_RO_RET("18983221"), PTYPE_IGNORE},
454 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
455 {"CRCX_ZYN", CRCX_ZYN, CRCX_ZYN_RET, 97},
456 {"EMPTY", EMPTY, EMPTY_RET},
457 {"SHORT1", SHORT, SHORT_RET},
458 {"SHORT2", SHORT2, SHORT2_RET},
459 {"SHORT3", SHORT3, SHORT2_RET},
460 {"SHORT4", SHORT4, SHORT2_RET},
461 {"RQNT1", RQNT, RQNT1_RET},
462 {"RQNT2", RQNT2, RQNT2_RET},
463 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE},
464 {"CRCX", CRCX, CRCX_FMTP_RET, 97,.extra_fmtp = "a=fmtp:126 0/1/2"},
465 {"MDCX3", MDCX3, MDCX3_FMTP_RET, PTYPE_NONE,.extra_fmtp =
466 "a=fmtp:126 0/1/2"},
467 {"DLCX", DLCX, DLCX_RET, PTYPE_IGNORE,.extra_fmtp = "a=fmtp:126 0/1/2"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200468};
469
470static const struct mgcp_test retransmit[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200471 {"CRCX", CRCX, CRCX_RET},
472 {"RQNT1", RQNT, RQNT1_RET},
473 {"RQNT2", RQNT2, RQNT2_RET},
474 {"MDCX3", MDCX3, MDCX3A_RET},
475 {"DLCX", DLCX, DLCX_RET},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200476};
477
Philipp Maierffd75e42017-11-22 11:44:50 +0100478static struct msgb *create_msg(const char *str, const char *conn_id)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200479{
480 struct msgb *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100481 int len;
482
483 printf("creating message from statically defined input:\n");
484 printf("---------8<---------\n%s\n---------8<---------\n", str);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200485
486 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
Philipp Maierffd75e42017-11-22 11:44:50 +0100487 if (conn_id && strlen(conn_id))
488 len = sprintf((char *)msg->data, str, conn_id, conn_id);
489 else
490 len = sprintf((char *)msg->data, "%s", str);
491
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200492 msg->l2h = msgb_put(msg, len);
493 return msg;
494}
495
496static int last_endpoint = -1;
497
498static int mgcp_test_policy_cb(struct mgcp_trunk_config *cfg, int endpoint,
499 int state, const char *transactio_id)
500{
501 fprintf(stderr, "Policy CB got state %d on endpoint %d\n",
502 state, endpoint);
503 last_endpoint = endpoint;
504 return MGCP_POLICY_CONT;
505}
506
507#define MGCP_DUMMY_LOAD 0x23
508static int dummy_packets = 0;
509/* override and forward */
510ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200511 const struct sockaddr *dest_addr, socklen_t addrlen)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200512{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200513 uint32_t dest_host =
514 htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
515 int dest_port = htons(((struct sockaddr_in *)dest_addr)->sin_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200516
Philipp Maier87bd9be2017-08-22 16:35:41 +0200517 if (len == 1 && ((const char *)buf)[0] == MGCP_DUMMY_LOAD) {
518 fprintf(stderr,
519 "Dummy packet to 0x%08x:%d, msg length %zu\n%s\n\n",
520 dest_host, dest_port, len, osmo_hexdump(buf, len));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200521 dummy_packets += 1;
522 }
523
Philipp Maier3d9b6562017-10-13 18:33:44 +0200524 return len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200525}
526
527static int64_t force_monotonic_time_us = -1;
528/* override and forward */
529int clock_gettime(clockid_t clk_id, struct timespec *tp)
530{
531 typedef int (*clock_gettime_t)(clockid_t clk_id, struct timespec *tp);
532 static clock_gettime_t real_clock_gettime = NULL;
533
534 if (!real_clock_gettime)
535 real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
536
537 if (clk_id == CLOCK_MONOTONIC && force_monotonic_time_us >= 0) {
538 tp->tv_sec = force_monotonic_time_us / 1000000;
539 tp->tv_nsec = (force_monotonic_time_us % 1000000) * 1000;
540 return 0;
541 }
542
543 return real_clock_gettime(clk_id, tp);
544}
545
546#define CONN_UNMODIFIED (0x1000)
547
548static void test_values(void)
549{
550 /* Check that NONE disables all output */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200551 OSMO_ASSERT((MGCP_CONN_NONE & MGCP_CONN_RECV_SEND) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200552
553 /* Check that LOOPBACK enables all output */
554 OSMO_ASSERT((MGCP_CONN_LOOPBACK & MGCP_CONN_RECV_SEND) ==
Philipp Maier87bd9be2017-08-22 16:35:41 +0200555 MGCP_CONN_RECV_SEND);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200556}
557
Philipp Maierffd75e42017-11-22 11:44:50 +0100558/* Extract a connection ID from a response (CRCX) */
559static int get_conn_id_from_response(uint8_t *resp, char *conn_id,
560 unsigned int conn_id_len)
561{
562 char *conn_id_ptr;
563 int i;
564
565 conn_id_ptr = strstr((char *)resp, "I: ");
566 if (!conn_id_ptr)
567 return -EINVAL;
568
569 memset(conn_id, 0, conn_id_len);
570 memcpy(conn_id, conn_id_ptr + 3, 32);
571
572 for (i = 0; i < conn_id_len; i++) {
573 if (conn_id[i] == '\n' || conn_id[i] == '\r')
574 conn_id[i] = '\0';
575 }
576
577 /* A valid conn_id must at least contain one digit, and must
578 * not exceed a length of 32 digits */
579 OSMO_ASSERT(strlen(conn_id) <= 32);
580 OSMO_ASSERT(strlen(conn_id) > 0);
581
582 return 0;
583}
584
585/* Check response, automatically patch connection ID if needed */
586static int check_response(uint8_t *resp, const char *exp_resp)
587{
588 char exp_resp_patched[4096];
589 const char *exp_resp_ptr;
590 char conn_id[256];
591
592 printf("checking response:\n");
593
594 /* If the expected response is intened to be patched
595 * (%s placeholder inside) we will patch it with the
596 * connection identifier we just received from the
597 * real response. This is necessary because the CI
598 * is generated by the mgcp code on CRCX and we can
599 * not know it in advance */
600 if (strstr(exp_resp, "%s")) {
601 if (get_conn_id_from_response(resp, conn_id, sizeof(conn_id)) ==
602 0) {
603 sprintf(exp_resp_patched, exp_resp, conn_id, conn_id);
604 exp_resp_ptr = exp_resp_patched;
605 printf
606 ("using message with patched conn_id for comparison\n");
607 } else {
608 printf
609 ("patching conn_id failed, using message as statically defined for comparison\n");
610 exp_resp_ptr = exp_resp;
611 }
612 } else {
613 printf("using message as statically defined for comparison\n");
614 exp_resp_ptr = exp_resp;
615 }
616
617 if (strcmp((char *)resp, exp_resp_ptr) != 0) {
618 printf("Unexpected response, please check!\n");
619 printf
620 ("Got:\n---------8<---------\n%s\n---------8<---------\n\n",
621 resp);
622 printf
623 ("Expected:\n---------8<---------\n%s\n---------8<---------\n",
624 exp_resp_ptr);
625 return -EINVAL;
626 }
627
628 printf("Response matches our expectations.\n");
629 return 0;
630}
631
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200632static void test_messages(void)
633{
634 struct mgcp_config *cfg;
635 struct mgcp_endpoint *endp;
636 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200637 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100638 char last_conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200639
640 cfg = mgcp_config_alloc();
641
Philipp Maierfcd06552017-11-10 17:32:22 +0100642 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200643 mgcp_endpoints_allocate(&cfg->trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200644 cfg->policy_cb = mgcp_test_policy_cb;
645
Philipp Maierffd75e42017-11-22 11:44:50 +0100646 memset(last_conn_id, 0, sizeof(last_conn_id));
647
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200648 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
649
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200650 for (i = 0; i < ARRAY_SIZE(tests); i++) {
651 const struct mgcp_test *t = &tests[i];
652 struct msgb *inp;
653 struct msgb *msg;
654
Philipp Maierffd75e42017-11-22 11:44:50 +0100655 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200656 printf("Testing %s\n", t->name);
657
658 last_endpoint = -1;
659 dummy_packets = 0;
660
Philipp Maier87bd9be2017-08-22 16:35:41 +0200661 osmo_talloc_replace_string(cfg, &cfg->trunk.audio_fmtp_extra,
662 t->extra_fmtp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200663
Philipp Maierffd75e42017-11-22 11:44:50 +0100664 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200665 msg = mgcp_handle_message(cfg, inp);
666 msgb_free(inp);
667 if (!t->exp_resp) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200668 if (msg) {
669 printf("%s failed '%s'\n", t->name,
670 (char *)msg->data);
671 OSMO_ASSERT(false);
672 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100673 } else if (check_response(msg->data, t->exp_resp) != 0) {
674 printf("%s failed.\n", t->name);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200675 OSMO_ASSERT(false);
676 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100677
678 if (msg)
679 get_conn_id_from_response(msg->data, last_conn_id,
680 sizeof(last_conn_id));
681
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200682 msgb_free(msg);
683
684 if (dummy_packets)
685 printf("Dummy packets: %d\n", dummy_packets);
686
687 if (last_endpoint != -1) {
688 endp = &cfg->trunk.endpoints[last_endpoint];
689
Philipp Maier01d24a32017-11-21 17:26:09 +0100690 conn = mgcp_conn_get_rtp(endp, "1");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200691 if (conn) {
692 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200693
Philipp Maier87bd9be2017-08-22 16:35:41 +0200694 if (conn->end.packet_duration_ms != -1)
695 printf("Detected packet duration: %d\n",
696 conn->end.packet_duration_ms);
697 else
698 printf("Packet duration not set\n");
699 if (endp->local_options.pkt_period_min ||
700 endp->local_options.pkt_period_max)
701 printf
702 ("Requested packetetization period: "
703 "%d-%d\n",
704 endp->local_options.pkt_period_min,
705 endp->
706 local_options.pkt_period_max);
707 else
708 printf
709 ("Requested packetization period not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200710
Philipp Maier87bd9be2017-08-22 16:35:41 +0200711 if ((conn->conn->mode & CONN_UNMODIFIED) == 0) {
712 printf("Connection mode: %d:%s%s%s%s\n",
713 conn->conn->mode,
714 !conn->conn->mode ? " NONE" : "",
715 conn->conn->mode & MGCP_CONN_SEND_ONLY
716 ? " SEND" : "",
717 conn->conn->mode & MGCP_CONN_RECV_ONLY
718 ? " RECV" : "",
719 conn->conn->mode & MGCP_CONN_LOOPBACK
720 & ~MGCP_CONN_RECV_SEND
721 ? " LOOP" : "");
722 fprintf(stderr,
723 "RTP output %sabled, NET output %sabled\n",
724 conn->end.output_enabled
725 ? "en" : "dis",
726 conn->end.output_enabled
727 ? "en" : "dis");
728 } else
729 printf("Connection mode not set\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200730
Philipp Maier87bd9be2017-08-22 16:35:41 +0200731 OSMO_ASSERT(conn->end.output_enabled
732 == (conn->conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0));
733
734 conn->conn->mode |= CONN_UNMODIFIED;
735
736 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200737 endp->local_options.pkt_period_min = 0;
738 endp->local_options.pkt_period_max = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200739 }
740
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200741 /* Check detected payload type */
Philipp Maierffd75e42017-11-22 11:44:50 +0100742 if (conn && t->ptype != PTYPE_IGNORE) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200743 OSMO_ASSERT(last_endpoint != -1);
744 endp = &cfg->trunk.endpoints[last_endpoint];
745
746 fprintf(stderr, "endpoint %d: "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200747 "payload type %d (expected %d)\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200748 last_endpoint,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200749 conn->end.codec.payload_type, t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200750
Philipp Maier87bd9be2017-08-22 16:35:41 +0200751 if (t->ptype != PTYPE_IGNORE)
752 OSMO_ASSERT(conn->end.codec.payload_type ==
753 t->ptype);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200754
755 /* Reset them again for next test */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200756 conn->end.codec.payload_type = PTYPE_NONE;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200757 }
758 }
759
760 talloc_free(cfg);
761}
762
763static void test_retransmission(void)
764{
765 struct mgcp_config *cfg;
766 int i;
Philipp Maierffd75e42017-11-22 11:44:50 +0100767 char last_conn_id[256];
Philipp Maier23b8e292017-12-04 16:48:45 +0100768 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200769
770 cfg = mgcp_config_alloc();
771
Philipp Maierfcd06552017-11-10 17:32:22 +0100772 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200773 mgcp_endpoints_allocate(&cfg->trunk);
774
Philipp Maierffd75e42017-11-22 11:44:50 +0100775 memset(last_conn_id, 0, sizeof(last_conn_id));
776
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200777 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
778
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200779 for (i = 0; i < ARRAY_SIZE(retransmit); i++) {
780 const struct mgcp_test *t = &retransmit[i];
781 struct msgb *inp;
782 struct msgb *msg;
783
Philipp Maierffd75e42017-11-22 11:44:50 +0100784 printf("\n================================================\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200785 printf("Testing %s\n", t->name);
786
Philipp Maierffd75e42017-11-22 11:44:50 +0100787 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200788 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200789
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200790 msgb_free(inp);
Philipp Maier7cedfd72017-12-04 16:49:12 +0100791 if (msg && check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200792 printf("%s failed '%s'\n", t->name, (char *)msg->data);
793 OSMO_ASSERT(false);
794 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100795
Philipp Maier23b8e292017-12-04 16:48:45 +0100796 if (msg && strcmp(t->name, "CRCX") == 0) {
797 rc = get_conn_id_from_response(msg->data, last_conn_id,
798 sizeof(last_conn_id));
799 OSMO_ASSERT(rc == 0);
800 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100801
Philipp Maier7cedfd72017-12-04 16:49:12 +0100802 if (msg)
803 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200804
805 /* Retransmit... */
806 printf("Re-transmitting %s\n", t->name);
Philipp Maierffd75e42017-11-22 11:44:50 +0100807 inp = create_msg(t->req, last_conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200808 msg = mgcp_handle_message(cfg, inp);
809 msgb_free(inp);
Philipp Maierffd75e42017-11-22 11:44:50 +0100810 if (check_response(msg->data, t->exp_resp) != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200811 printf("%s failed '%s'\n", t->name, (char *)msg->data);
812 OSMO_ASSERT(false);
813 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200814 msgb_free(msg);
815 }
816
817 talloc_free(cfg);
818}
819
820static int rqnt_cb(struct mgcp_endpoint *endp, char _tone)
821{
822 ptrdiff_t tone = _tone;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200823 endp->cfg->data = (void *)tone;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200824 return 0;
825}
826
827static void test_rqnt_cb(void)
828{
829 struct mgcp_config *cfg;
830 struct msgb *inp, *msg;
Philipp Maierffd75e42017-11-22 11:44:50 +0100831 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200832
833 cfg = mgcp_config_alloc();
834 cfg->rqnt_cb = rqnt_cb;
835
Philipp Maierfcd06552017-11-10 17:32:22 +0100836 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200837 mgcp_endpoints_allocate(&cfg->trunk);
838
839 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
840
Philipp Maierffd75e42017-11-22 11:44:50 +0100841 inp = create_msg(CRCX, NULL);
842 msg = mgcp_handle_message(cfg, inp);
843 OSMO_ASSERT(msg);
844 OSMO_ASSERT(get_conn_id_from_response(msg->data, conn_id,
845 sizeof(conn_id)) == 0);
846 msgb_free(msg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200847 msgb_free(inp);
848
849 /* send the RQNT and check for the CB */
Philipp Maierffd75e42017-11-22 11:44:50 +0100850 inp = create_msg(RQNT, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200851 msg = mgcp_handle_message(cfg, inp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200852 if (strncmp((const char *)msg->l2h, "200", 3) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200853 printf("FAILED: message is not 200. '%s'\n", msg->l2h);
854 abort();
855 }
856
Philipp Maier87bd9be2017-08-22 16:35:41 +0200857 if (cfg->data != (void *)'9') {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200858 printf("FAILED: callback not called: %p\n", cfg->data);
859 abort();
860 }
861
862 msgb_free(msg);
863 msgb_free(inp);
864
Philipp Maierffd75e42017-11-22 11:44:50 +0100865 inp = create_msg(DLCX, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200866 msgb_free(mgcp_handle_message(cfg, inp));
867 msgb_free(inp);
868 talloc_free(cfg);
869}
870
871struct pl_test {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200872 int cycles;
873 uint16_t base_seq;
874 uint16_t max_seq;
875 uint32_t packets;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200876
Philipp Maier87bd9be2017-08-22 16:35:41 +0200877 uint32_t expected;
878 int loss;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200879};
880
881static const struct pl_test pl_test_dat[] = {
882 /* basic.. just one package */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200883 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = 1,.expected =
884 1,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200885 /* some packages and a bit of loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200886 {.cycles = 0,.base_seq = 0,.max_seq = 100,.packets = 100,.expected =
887 101,.loss = 1},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200888 /* wrap around */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200889 {.cycles = 1 << 16,.base_seq = 0xffff,.max_seq = 2,.packets =
890 4,.expected = 4,.loss = 0},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200891 /* min loss */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200892 {.cycles = 0,.base_seq = 0,.max_seq = 0,.packets = UINT_MAX,.expected =
893 1,.loss = INT_MIN},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200894 /* max loss, with wrap around on expected max */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200895 {.cycles = INT_MAX,.base_seq = 0,.max_seq = UINT16_MAX,.packets =
896 0,.expected = ((uint32_t) (INT_MAX) + UINT16_MAX + 1),.loss = INT_MAX},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200897};
898
899static void test_packet_loss_calc(void)
900{
901 int i;
902 printf("Testing packet loss calculation.\n");
903
904 for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
905 uint32_t expected;
906 int loss;
907 struct mgcp_rtp_state state;
908 struct mgcp_rtp_end rtp;
909 memset(&state, 0, sizeof(state));
910 memset(&rtp, 0, sizeof(rtp));
911
912 state.stats_initialized = 1;
913 state.stats_base_seq = pl_test_dat[i].base_seq;
914 state.stats_max_seq = pl_test_dat[i].max_seq;
915 state.stats_cycles = pl_test_dat[i].cycles;
916
Philipp Maier87bd9be2017-08-22 16:35:41 +0200917 rtp.packets_rx = pl_test_dat[i].packets;
918 calc_loss(&state, &rtp, &expected, &loss);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200919
Philipp Maier87bd9be2017-08-22 16:35:41 +0200920 if (loss != pl_test_dat[i].loss
921 || expected != pl_test_dat[i].expected) {
922 printf
923 ("FAIL: Wrong exp/loss at idx(%d) Loss(%d vs. %d) Exp(%u vs. %u)\n",
924 i, loss, pl_test_dat[i].loss, expected,
925 pl_test_dat[i].expected);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200926 }
927 }
928}
929
Philipp Maier87bd9be2017-08-22 16:35:41 +0200930int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
931 uint32_t *pr, uint32_t *_or, int *loss,
932 uint32_t *jitter)
933{
934 char *line, *save;
935 int rc;
936
937 /* initialize with bad values */
938 *ps = *os = *pr = *_or = *jitter = UINT_MAX;
939 *loss = INT_MAX;
940
941 line = strtok_r((char *)msg->l2h, "\r\n", &save);
942 if (!line)
943 return -1;
944
945 /* this can only parse the message that is created above... */
946 for_each_non_empty_line(line, save) {
947 switch (line[0]) {
948 case 'P':
949 rc = sscanf(line,
950 "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
951 ps, os, pr, _or, loss, jitter);
952 return rc == 6 ? 0 : -1;
953 }
954 }
955
956 return -1;
957}
958
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200959static void test_mgcp_stats(void)
960{
961 printf("Testing stat parsing\n");
962
963 uint32_t bps, bos, pr, _or, jitter;
964 struct msgb *msg;
965 int loss;
966 int rc;
967
Philipp Maierffd75e42017-11-22 11:44:50 +0100968 msg = create_msg(DLCX_RET, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200969 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
970 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200971 if (bps != 0 || bos != 0 || pr != 0 || _or != 0 || loss != 0
972 || jitter != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200973 printf("FAIL: Parsing failed1.\n");
974 msgb_free(msg);
975
Philipp Maier87bd9be2017-08-22 16:35:41 +0200976 msg =
977 create_msg
Philipp Maierffd75e42017-11-22 11:44:50 +0100978 ("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 +0200979 rc = mgcp_parse_stats(msg, &bps, &bos, &pr, &_or, &loss, &jitter);
980 printf("Parsing result: %d\n", rc);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200981 if (bps != 10 || bos != 20 || pr != 30 || _or != 40 || loss != -3
982 || jitter != 40)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200983 printf("FAIL: Parsing failed2.\n");
984 msgb_free(msg);
985}
986
987struct rtp_packet_info {
988 float txtime;
989 int len;
990 char *data;
991};
992
993struct rtp_packet_info test_rtp_packets1[] = {
994 /* RTP: SeqNo=0, TS=0 */
995 {0.000000, 20, "\x80\x62\x00\x00\x00\x00\x00\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200996 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200997 /* RTP: SeqNo=1, TS=160 */
998 {0.020000, 20, "\x80\x62\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +0200999 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001000 /* RTP: SeqNo=2, TS=320 */
1001 {0.040000, 20, "\x80\x62\x00\x02\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001002 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001003 /* Repeat RTP timestamp: */
1004 /* RTP: SeqNo=3, TS=320 */
1005 {0.060000, 20, "\x80\x62\x00\x03\x00\x00\x01\x40\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001006 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001007 /* RTP: SeqNo=4, TS=480 */
1008 {0.080000, 20, "\x80\x62\x00\x04\x00\x00\x01\xE0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001009 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001010 /* RTP: SeqNo=5, TS=640 */
1011 {0.100000, 20, "\x80\x62\x00\x05\x00\x00\x02\x80\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001012 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001013 /* Double skip RTP timestamp (delta = 2*160): */
1014 /* RTP: SeqNo=6, TS=960 */
1015 {0.120000, 20, "\x80\x62\x00\x06\x00\x00\x03\xC0\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001016 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001017 /* RTP: SeqNo=7, TS=1120 */
1018 {0.140000, 20, "\x80\x62\x00\x07\x00\x00\x04\x60\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001019 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001020 /* RTP: SeqNo=8, TS=1280 */
1021 {0.160000, 20, "\x80\x62\x00\x08\x00\x00\x05\x00\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001022 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001023 /* Non 20ms RTP timestamp (delta = 120): */
1024 /* RTP: SeqNo=9, TS=1400 */
1025 {0.180000, 20, "\x80\x62\x00\x09\x00\x00\x05\x78\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001026 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001027 /* RTP: SeqNo=10, TS=1560 */
1028 {0.200000, 20, "\x80\x62\x00\x0A\x00\x00\x06\x18\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001029 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001030 /* RTP: SeqNo=11, TS=1720 */
1031 {0.220000, 20, "\x80\x62\x00\x0B\x00\x00\x06\xB8\x11\x22\x33\x44"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001032 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001033 /* SSRC changed to 0x10203040, RTP timestamp jump */
1034 /* RTP: SeqNo=12, TS=34688 */
1035 {0.240000, 20, "\x80\x62\x00\x0C\x00\x00\x87\x80\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001036 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001037 /* RTP: SeqNo=13, TS=34848 */
1038 {0.260000, 20, "\x80\x62\x00\x0D\x00\x00\x88\x20\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001039 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001040 /* RTP: SeqNo=14, TS=35008 */
1041 {0.280000, 20, "\x80\x62\x00\x0E\x00\x00\x88\xC0\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001042 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001043 /* Non 20ms RTP timestamp (delta = 120): */
1044 /* RTP: SeqNo=15, TS=35128 */
1045 {0.300000, 20, "\x80\x62\x00\x0F\x00\x00\x89\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001046 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001047 /* RTP: SeqNo=16, TS=35288 */
1048 {0.320000, 20, "\x80\x62\x00\x10\x00\x00\x89\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001049 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001050 /* RTP: SeqNo=17, TS=35448 */
1051 {0.340000, 20, "\x80\x62\x00\x11\x00\x00\x8A\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001052 "\x01\x23\x45\x67\x8A\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001053 /* SeqNo increment by 2, RTP timestamp delta = 320: */
1054 /* RTP: SeqNo=19, TS=35768 */
1055 {0.360000, 20, "\x80\x62\x00\x13\x00\x00\x8B\xB8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001056 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001057 /* RTP: SeqNo=20, TS=35928 */
1058 {0.380000, 20, "\x80\x62\x00\x14\x00\x00\x8C\x58\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001059 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001060 /* RTP: SeqNo=21, TS=36088 */
1061 {0.380000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001062 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001063 /* Repeat last packet */
1064 /* RTP: SeqNo=21, TS=36088 */
1065 {0.400000, 20, "\x80\x62\x00\x15\x00\x00\x8C\xF8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001066 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001067 /* RTP: SeqNo=22, TS=36248 */
1068 {0.420000, 20, "\x80\x62\x00\x16\x00\x00\x8D\x98\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001069 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001070 /* RTP: SeqNo=23, TS=36408 */
1071 {0.440000, 20, "\x80\x62\x00\x17\x00\x00\x8E\x38\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001072 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001073 /* Don't increment SeqNo but increment timestamp by 160 */
1074 /* RTP: SeqNo=23, TS=36568 */
1075 {0.460000, 20, "\x80\x62\x00\x17\x00\x00\x8E\xD8\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001076 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001077 /* RTP: SeqNo=24, TS=36728 */
1078 {0.480000, 20, "\x80\x62\x00\x18\x00\x00\x8F\x78\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001079 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001080 /* RTP: SeqNo=25, TS=36888 */
1081 {0.500000, 20, "\x80\x62\x00\x19\x00\x00\x90\x18\x10\x20\x30\x40"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001082 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001083 /* SSRC changed to 0x50607080, RTP timestamp jump, Delay of 1.5s,
1084 * SeqNo jump */
1085 /* RTP: SeqNo=1000, TS=160000 */
1086 {2.000000, 20, "\x80\x62\x03\xE8\x00\x02\x71\x00\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001087 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001088 /* RTP: SeqNo=1001, TS=160160 */
1089 {2.020000, 20, "\x80\x62\x03\xE9\x00\x02\x71\xA0\x50\x60\x70\x80"
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=1002, TS=160320 */
1092 {2.040000, 20, "\x80\x62\x03\xEA\x00\x02\x72\x40\x50\x60\x70\x80"
Philipp Maier87bd9be2017-08-22 16:35:41 +02001093 "\x01\x23\x45\x67\x89\xAB\xCD\xEF"},
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001094};
1095
Philipp Maier87bd9be2017-08-22 16:35:41 +02001096void mgcp_patch_and_count(struct mgcp_endpoint *endp,
1097 struct mgcp_rtp_state *state,
1098 struct mgcp_rtp_end *rtp_end,
1099 struct sockaddr_in *addr, char *data, int len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001100
1101static void test_packet_error_detection(int patch_ssrc, int patch_ts)
1102{
1103 int i;
1104
1105 struct mgcp_trunk_config trunk;
1106 struct mgcp_endpoint endp;
1107 struct mgcp_rtp_state state;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001108 struct mgcp_rtp_end *rtp;
1109 struct sockaddr_in addr = { 0 };
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001110 char buffer[4096];
1111 uint32_t last_ssrc = 0;
1112 uint32_t last_timestamp = 0;
1113 uint32_t last_seqno = 0;
1114 int last_in_ts_err_cnt = 0;
1115 int last_out_ts_err_cnt = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001116 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001117 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001118
1119 printf("Testing packet error detection%s%s.\n",
1120 patch_ssrc ? ", patch SSRC" : "",
1121 patch_ts ? ", patch timestamps" : "");
1122
1123 memset(&trunk, 0, sizeof(trunk));
1124 memset(&endp, 0, sizeof(endp));
1125 memset(&state, 0, sizeof(state));
1126
Philipp Maier87bd9be2017-08-22 16:35:41 +02001127 endp.type = &ep_typeset.rtp;
1128
Philipp Maierfcd06552017-11-10 17:32:22 +01001129 trunk.vty_number_endpoints = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001130 trunk.endpoints = &endp;
1131 trunk.force_constant_ssrc = patch_ssrc;
1132 trunk.force_aligned_timing = patch_ts;
1133
1134 endp.tcfg = &trunk;
1135
Philipp Maier87bd9be2017-08-22 16:35:41 +02001136 INIT_LLIST_HEAD(&endp.conns);
Philipp Maierffd75e42017-11-22 11:44:50 +01001137 _conn = mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
1138 "test-connection");
1139 OSMO_ASSERT(_conn);
1140 conn = mgcp_conn_get_rtp(&endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001141 OSMO_ASSERT(conn);
1142
1143 rtp = &conn->end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001144
1145 rtp->codec.payload_type = 98;
1146
1147 for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
1148 struct rtp_packet_info *info = test_rtp_packets1 + i;
1149
1150 force_monotonic_time_us = round(1000000.0 * info->txtime);
1151
1152 OSMO_ASSERT(info->len <= sizeof(buffer));
1153 OSMO_ASSERT(info->len >= 0);
1154 memmove(buffer, info->data, info->len);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001155 mgcp_rtp_end_config(&endp, 1, rtp);
1156
1157 mgcp_patch_and_count(&endp, &state, rtp, &addr,
1158 buffer, info->len);
1159
1160 if (state.out_stream.ssrc != last_ssrc) {
1161 printf("Output SSRC changed to %08x\n",
1162 state.out_stream.ssrc);
1163 last_ssrc = state.out_stream.ssrc;
1164 }
1165
1166 printf("In TS: %d, dTS: %d, Seq: %d\n",
1167 state.in_stream.last_timestamp,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001168 state.in_stream.last_tsdelta, state.in_stream.last_seq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001169
1170 printf("Out TS change: %d, dTS: %d, Seq change: %d, "
1171 "TS Err change: in %+d, out %+d\n",
1172 state.out_stream.last_timestamp - last_timestamp,
1173 state.out_stream.last_tsdelta,
1174 state.out_stream.last_seq - last_seqno,
1175 state.in_stream.err_ts_counter - last_in_ts_err_cnt,
1176 state.out_stream.err_ts_counter - last_out_ts_err_cnt);
1177
1178 printf("Stats: Jitter = %u, Transit = %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001179 calc_jitter(&state), state.stats_transit);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001180
1181 last_in_ts_err_cnt = state.in_stream.err_ts_counter;
1182 last_out_ts_err_cnt = state.out_stream.err_ts_counter;
1183 last_timestamp = state.out_stream.last_timestamp;
1184 last_seqno = state.out_stream.last_seq;
1185 }
1186
1187 force_monotonic_time_us = -1;
Neels Hofmeyrd20910c2017-11-18 21:27:50 +01001188 mgcp_conn_free_all(&endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001189}
1190
1191static void test_multilple_codec(void)
1192{
1193 struct mgcp_config *cfg;
1194 struct mgcp_endpoint *endp;
1195 struct msgb *inp, *resp;
1196 struct in_addr addr;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001197 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001198 char conn_id[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001199
1200 printf("Testing multiple payload types\n");
1201
1202 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001203 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001204 mgcp_endpoints_allocate(&cfg->trunk);
1205 cfg->policy_cb = mgcp_test_policy_cb;
1206 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1207
1208 /* Allocate endpoint 1@mgw with two codecs */
1209 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001210 inp = create_msg(CRCX_MULT_1, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001211 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001212 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1213 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001214 msgb_free(inp);
1215 msgb_free(resp);
1216
1217 OSMO_ASSERT(last_endpoint == 1);
1218 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001219 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001220 OSMO_ASSERT(conn);
1221 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1222 OSMO_ASSERT(conn->end.alt_codec.payload_type == 97);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001223
1224 /* Allocate 2@mgw with three codecs, last one ignored */
1225 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001226 inp = create_msg(CRCX_MULT_2, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001227 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001228 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1229 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001230 msgb_free(inp);
1231 msgb_free(resp);
1232
1233 OSMO_ASSERT(last_endpoint == 2);
1234 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001235 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001236 OSMO_ASSERT(conn);
1237 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1238 OSMO_ASSERT(conn->end.alt_codec.payload_type == 97);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001239
1240 /* Allocate 3@mgw with no codecs, check for PT == -1 */
1241 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001242 inp = create_msg(CRCX_MULT_3, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001243 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001244 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1245 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001246 msgb_free(inp);
1247 msgb_free(resp);
1248
1249 OSMO_ASSERT(last_endpoint == 3);
1250 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001251 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001252 OSMO_ASSERT(conn);
1253 OSMO_ASSERT(conn->end.codec.payload_type == -1);
1254 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001255
1256 /* Allocate 4@mgw with a single codec */
1257 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001258 inp = create_msg(CRCX_MULT_4, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001259 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001260 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1261 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001262 msgb_free(inp);
1263 msgb_free(resp);
1264
1265 OSMO_ASSERT(last_endpoint == 4);
1266 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001267 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001268 OSMO_ASSERT(conn);
1269 OSMO_ASSERT(conn->end.codec.payload_type == 18);
1270 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001271
1272 /* Allocate 5@mgw at select GSM.. */
1273 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001274 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001275 talloc_free(cfg->trunk.audio_name);
1276 cfg->trunk.audio_name = "GSM/8000";
1277 cfg->trunk.no_audio_transcoding = 1;
1278 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001279 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1280 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001281 msgb_free(inp);
1282 msgb_free(resp);
1283
1284 OSMO_ASSERT(last_endpoint == 5);
1285 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001286 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001287 OSMO_ASSERT(conn);
1288 OSMO_ASSERT(conn->end.codec.payload_type == 3);
1289 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001290
Philipp Maierffd75e42017-11-22 11:44:50 +01001291 inp = create_msg(MDCX_NAT_DUMMY, conn_id);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001292 last_endpoint = -1;
1293 resp = mgcp_handle_message(cfg, inp);
1294 msgb_free(inp);
1295 msgb_free(resp);
1296 OSMO_ASSERT(last_endpoint == 5);
1297 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001298 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001299 OSMO_ASSERT(conn);
1300 OSMO_ASSERT(conn->end.codec.payload_type == 3);
1301 OSMO_ASSERT(conn->end.alt_codec.payload_type == -1);
1302 OSMO_ASSERT(conn->end.rtp_port == htons(16434));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001303 memset(&addr, 0, sizeof(addr));
1304 inet_aton("8.8.8.8", &addr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001305 OSMO_ASSERT(conn->end.addr.s_addr == addr.s_addr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001306
1307 /* Check what happens without that flag */
1308
Philipp Maier87bd9be2017-08-22 16:35:41 +02001309 /* Free the previous endpoint and the data and
1310 * check if the connection really vanished... */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001311 mgcp_release_endp(endp);
1312 talloc_free(endp->last_response);
1313 talloc_free(endp->last_trans);
1314 endp->last_response = endp->last_trans = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001315 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001316 OSMO_ASSERT(!conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001317
1318 last_endpoint = -1;
Philipp Maierffd75e42017-11-22 11:44:50 +01001319 inp = create_msg(CRCX_MULT_GSM_EXACT, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001320 cfg->trunk.no_audio_transcoding = 0;
1321 resp = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001322 OSMO_ASSERT(get_conn_id_from_response(resp->data, conn_id,
1323 sizeof(conn_id)) == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001324 msgb_free(inp);
1325 msgb_free(resp);
1326
1327 OSMO_ASSERT(last_endpoint == 5);
1328 endp = &cfg->trunk.endpoints[last_endpoint];
Philipp Maierffd75e42017-11-22 11:44:50 +01001329 conn = mgcp_conn_get_rtp(endp, conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001330 OSMO_ASSERT(conn);
1331 OSMO_ASSERT(conn->end.codec.payload_type == 255);
1332 OSMO_ASSERT(conn->end.alt_codec.payload_type == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001333
1334 talloc_free(cfg);
1335}
1336
1337static void test_no_cycle(void)
1338{
1339 struct mgcp_config *cfg;
1340 struct mgcp_endpoint *endp;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001341 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +01001342 struct mgcp_conn *_conn = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001343
1344 printf("Testing no sequence flow on initial packet\n");
1345
1346 cfg = mgcp_config_alloc();
Philipp Maierfcd06552017-11-10 17:32:22 +01001347 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001348 mgcp_endpoints_allocate(&cfg->trunk);
1349
1350 endp = &cfg->trunk.endpoints[1];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001351
Philipp Maierffd75e42017-11-22 11:44:50 +01001352 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP,
1353 "test-connection");
1354 OSMO_ASSERT(_conn);
1355 conn = mgcp_conn_get_rtp(endp, _conn->id);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001356 OSMO_ASSERT(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001357
Philipp Maier87bd9be2017-08-22 16:35:41 +02001358 OSMO_ASSERT(conn->state.stats_initialized == 0);
1359
1360 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
1361 OSMO_ASSERT(conn->state.stats_initialized == 1);
1362 OSMO_ASSERT(conn->state.stats_cycles == 0);
1363 OSMO_ASSERT(conn->state.stats_max_seq == 0);
1364
1365 mgcp_rtp_annex_count(endp, &conn->state, 1, 0, 2342);
1366 OSMO_ASSERT(conn->state.stats_initialized == 1);
1367 OSMO_ASSERT(conn->state.stats_cycles == 0);
1368 OSMO_ASSERT(conn->state.stats_max_seq == 1);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001369
1370 /* now jump.. */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001371 mgcp_rtp_annex_count(endp, &conn->state, UINT16_MAX, 0, 2342);
1372 OSMO_ASSERT(conn->state.stats_initialized == 1);
1373 OSMO_ASSERT(conn->state.stats_cycles == 0);
1374 OSMO_ASSERT(conn->state.stats_max_seq == UINT16_MAX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001375
1376 /* and wrap */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001377 mgcp_rtp_annex_count(endp, &conn->state, 0, 0, 2342);
1378 OSMO_ASSERT(conn->state.stats_initialized == 1);
1379 OSMO_ASSERT(conn->state.stats_cycles == UINT16_MAX + 1);
1380 OSMO_ASSERT(conn->state.stats_max_seq == 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001381
Neels Hofmeyrb597b4f2017-11-18 21:28:35 +01001382 mgcp_release_endp(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001383 talloc_free(cfg);
1384}
1385
1386static void test_no_name(void)
1387{
1388 struct mgcp_config *cfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001389 struct msgb *inp, *msg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001390
1391 printf("Testing no rtpmap name\n");
1392 cfg = mgcp_config_alloc();
1393
Philipp Maierfcd06552017-11-10 17:32:22 +01001394 cfg->trunk.vty_number_endpoints = 64;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001395 cfg->trunk.audio_send_name = 0;
1396 mgcp_endpoints_allocate(&cfg->trunk);
1397
1398 cfg->policy_cb = mgcp_test_policy_cb;
1399
1400 mgcp_endpoints_allocate(mgcp_trunk_alloc(cfg, 1));
1401
Philipp Maierffd75e42017-11-22 11:44:50 +01001402 inp = create_msg(CRCX, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001403 msg = mgcp_handle_message(cfg, inp);
Philipp Maierffd75e42017-11-22 11:44:50 +01001404
1405 if (check_response(msg->data, CRCX_RET_NO_RTPMAP) != 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001406 printf("FAILED: there should not be a RTPMAP: %s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001407 (char *)msg->data);
1408 OSMO_ASSERT(false);
1409 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001410 msgb_free(inp);
1411 msgb_free(msg);
1412
1413 mgcp_release_endp(&cfg->trunk.endpoints[1]);
1414 talloc_free(cfg);
1415}
1416
1417static void test_osmux_cid(void)
1418{
1419 int id, i;
1420
1421 OSMO_ASSERT(osmux_used_cid() == 0);
1422 id = osmux_get_cid();
1423 OSMO_ASSERT(id == 0);
1424 OSMO_ASSERT(osmux_used_cid() == 1);
1425 osmux_put_cid(id);
1426 OSMO_ASSERT(osmux_used_cid() == 0);
1427
1428 for (i = 0; i < 256; ++i) {
1429 id = osmux_get_cid();
1430 OSMO_ASSERT(id == i);
1431 OSMO_ASSERT(osmux_used_cid() == i + 1);
1432 }
1433
1434 id = osmux_get_cid();
1435 OSMO_ASSERT(id == -1);
1436
1437 for (i = 0; i < 256; ++i)
1438 osmux_put_cid(i);
1439 OSMO_ASSERT(osmux_used_cid() == 0);
1440}
1441
1442static const struct log_info_cat log_categories[] = {
1443};
1444
1445const struct log_info log_info = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001446 .cat = log_categories,
1447 .num_cat = ARRAY_SIZE(log_categories),
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001448};
1449
1450int main(int argc, char **argv)
1451{
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001452 void *msgb_ctx = msgb_talloc_ctx_init(NULL, 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001453 osmo_init_logging(&log_info);
1454
1455 test_strline();
1456 test_values();
1457 test_messages();
1458 test_retransmission();
1459 test_packet_loss_calc();
1460 test_rqnt_cb();
1461 test_mgcp_stats();
1462 test_packet_error_detection(1, 0);
1463 test_packet_error_detection(0, 0);
1464 test_packet_error_detection(0, 1);
1465 test_packet_error_detection(1, 1);
1466 test_multilple_codec();
1467 test_no_cycle();
1468 test_no_name();
1469 test_osmux_cid();
1470
Neels Hofmeyr465446b2017-11-18 21:26:26 +01001471 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
1472 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
1473 talloc_free(msgb_ctx);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001474 printf("Done\n");
1475 return EXIT_SUCCESS;
1476}