Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * BSC NAT Message filtering |
| 3 | * |
| 4 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Hans Peter Freyther | df6143a | 2010-06-15 18:46:56 +0800 | [diff] [blame] | 5 | * (C) 2010 by On-Waves |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include <openbsc/debug.h> |
| 27 | #include <openbsc/gsm_data.h> |
| 28 | #include <openbsc/bsc_nat.h> |
| 29 | |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 30 | #include <osmocore/talloc.h> |
| 31 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | |
| 34 | /* test messages for ipa */ |
| 35 | static u_int8_t ipa_id[] = { |
| 36 | 0x00, 0x01, 0xfe, 0x06, |
| 37 | }; |
| 38 | |
| 39 | /* SCCP messages are below */ |
| 40 | static u_int8_t gsm_reset[] = { |
| 41 | 0x00, 0x12, 0xfd, |
| 42 | 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe, |
| 43 | 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04, |
| 44 | 0x01, 0x20, |
| 45 | }; |
| 46 | |
| 47 | static const u_int8_t gsm_reset_ack[] = { |
| 48 | 0x00, 0x13, 0xfd, |
| 49 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 50 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03, |
| 51 | 0x00, 0x01, 0x31, |
| 52 | }; |
| 53 | |
| 54 | static const u_int8_t gsm_paging[] = { |
| 55 | 0x00, 0x20, 0xfd, |
| 56 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 57 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x10, |
| 58 | 0x00, 0x0e, 0x52, 0x08, 0x08, 0x29, 0x47, 0x10, |
| 59 | 0x02, 0x01, 0x31, 0x97, 0x61, 0x1a, 0x01, 0x06, |
| 60 | }; |
| 61 | |
| 62 | /* BSC -> MSC connection open */ |
| 63 | static const u_int8_t bssmap_cr[] = { |
| 64 | 0x00, 0x2c, 0xfd, |
| 65 | 0x01, 0x01, 0x02, 0x03, 0x02, 0x02, 0x04, 0x02, |
| 66 | 0x42, 0xfe, 0x0f, 0x1f, 0x00, 0x1d, 0x57, 0x05, |
| 67 | 0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x12, 0xc3, |
| 68 | 0x50, 0x17, 0x10, 0x05, 0x24, 0x11, 0x03, 0x33, |
| 69 | 0x19, 0xa2, 0x08, 0x29, 0x47, 0x10, 0x02, 0x01, |
| 70 | 0x31, 0x97, 0x61, 0x00 |
| 71 | }; |
| 72 | |
| 73 | /* MSC -> BSC connection confirm */ |
| 74 | static const u_int8_t bssmap_cc[] = { |
| 75 | 0x00, 0x0a, 0xfd, |
| 76 | 0x02, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, |
| 77 | }; |
| 78 | |
| 79 | /* MSC -> BSC released */ |
| 80 | static const u_int8_t bssmap_released[] = { |
| 81 | 0x00, 0x0e, 0xfd, |
| 82 | 0x04, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03, 0x00, 0x01, 0x0f, |
| 83 | 0x02, 0x23, 0x42, 0x00, |
| 84 | }; |
| 85 | |
| 86 | /* BSC -> MSC released */ |
| 87 | static const u_int8_t bssmap_release_complete[] = { |
| 88 | 0x00, 0x07, 0xfd, |
| 89 | 0x05, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03 |
| 90 | }; |
| 91 | |
Holger Hans Peter Freyther | a128d91 | 2010-04-01 08:47:12 +0200 | [diff] [blame] | 92 | /* MGCP wrap... */ |
| 93 | static const u_int8_t mgcp_msg[] = { |
| 94 | 0x00, 0x03, 0xfc, |
| 95 | 0x20, 0x20, 0x20, |
| 96 | }; |
| 97 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 98 | struct filter_result { |
| 99 | const u_int8_t *data; |
| 100 | const u_int16_t length; |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 101 | const int dir; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 102 | const int result; |
| 103 | }; |
| 104 | |
| 105 | static const struct filter_result results[] = { |
| 106 | { |
| 107 | .data = ipa_id, |
| 108 | .length = ARRAY_SIZE(ipa_id), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 109 | .dir = DIR_MSC, |
| 110 | .result = 1, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 111 | }, |
| 112 | { |
| 113 | .data = gsm_reset, |
| 114 | .length = ARRAY_SIZE(gsm_reset), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 115 | .dir = DIR_MSC, |
| 116 | .result = 1, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 117 | }, |
| 118 | { |
| 119 | .data = gsm_reset_ack, |
| 120 | .length = ARRAY_SIZE(gsm_reset_ack), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 121 | .dir = DIR_BSC, |
| 122 | .result = 1, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 123 | }, |
| 124 | { |
| 125 | .data = gsm_paging, |
| 126 | .length = ARRAY_SIZE(gsm_paging), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 127 | .dir = DIR_BSC, |
| 128 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 129 | }, |
| 130 | { |
| 131 | .data = bssmap_cr, |
| 132 | .length = ARRAY_SIZE(bssmap_cr), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 133 | .dir = DIR_MSC, |
| 134 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 135 | }, |
| 136 | { |
| 137 | .data = bssmap_cc, |
| 138 | .length = ARRAY_SIZE(bssmap_cc), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 139 | .dir = DIR_BSC, |
| 140 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 141 | }, |
| 142 | { |
| 143 | .data = bssmap_released, |
| 144 | .length = ARRAY_SIZE(bssmap_released), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 145 | .dir = DIR_MSC, |
| 146 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 147 | }, |
| 148 | { |
| 149 | .data = bssmap_release_complete, |
| 150 | .length = ARRAY_SIZE(bssmap_release_complete), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 151 | .dir = DIR_BSC, |
| 152 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 153 | }, |
Holger Hans Peter Freyther | a128d91 | 2010-04-01 08:47:12 +0200 | [diff] [blame] | 154 | { |
| 155 | .data = mgcp_msg, |
| 156 | .length = ARRAY_SIZE(mgcp_msg), |
| 157 | .dir = DIR_MSC, |
| 158 | .result = 0, |
| 159 | }, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 160 | }; |
| 161 | |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 162 | static void test_filter(void) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 163 | { |
| 164 | int i; |
| 165 | |
| 166 | |
| 167 | /* start testinh with proper messages */ |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 168 | fprintf(stderr, "Testing BSS Filtering.\n"); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 169 | for (i = 0; i < ARRAY_SIZE(results); ++i) { |
| 170 | int result; |
| 171 | struct bsc_nat_parsed *parsed; |
| 172 | struct msgb *msg = msgb_alloc(4096, "test-message"); |
| 173 | |
| 174 | fprintf(stderr, "Going to test item: %d\n", i); |
| 175 | memcpy(msg->data, results[i].data, results[i].length); |
| 176 | msg->l2h = msgb_put(msg, results[i].length); |
| 177 | |
| 178 | parsed = bsc_nat_parse(msg); |
| 179 | if (!parsed) { |
| 180 | fprintf(stderr, "FAIL: Failed to parse the message\n"); |
| 181 | continue; |
| 182 | } |
| 183 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 184 | result = bsc_nat_filter_ipa(results[i].dir, msg, parsed); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 185 | if (result != results[i].result) { |
| 186 | fprintf(stderr, "FAIL: Not the expected result got: %d wanted: %d\n", |
| 187 | result, results[i].result); |
| 188 | } |
| 189 | |
| 190 | msgb_free(msg); |
| 191 | } |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 192 | } |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 193 | |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 194 | #include "bsc_data.c" |
| 195 | |
| 196 | static void copy_to_msg(struct msgb *msg, const u_int8_t *data, unsigned int length) |
| 197 | { |
| 198 | msgb_reset(msg); |
| 199 | msg->l2h = msgb_put(msg, length); |
| 200 | memcpy(msg->l2h, data, msgb_l2len(msg)); |
| 201 | } |
| 202 | |
| 203 | #define VERIFY(con_found, con, msg, ver, str) \ |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 204 | if (!con_found || con_found->bsc != con) { \ |
Holger Hans Peter Freyther | 72ba162 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 205 | fprintf(stderr, "Failed to find the con: %p\n", con_found); \ |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 206 | abort(); \ |
| 207 | } \ |
| 208 | if (memcmp(msg->data, ver, sizeof(ver)) != 0) { \ |
| 209 | fprintf(stderr, "Failed to patch the %s msg.\n", str); \ |
| 210 | abort(); \ |
| 211 | } |
| 212 | |
| 213 | /* test conn tracking once */ |
| 214 | static void test_contrack() |
| 215 | { |
| 216 | int rc; |
| 217 | struct bsc_nat *nat; |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 218 | struct bsc_connection *con; |
| 219 | struct sccp_connections *con_found; |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 220 | struct bsc_nat_parsed *parsed; |
| 221 | struct msgb *msg; |
| 222 | |
| 223 | fprintf(stderr, "Testing connection tracking.\n"); |
| 224 | nat = bsc_nat_alloc(); |
| 225 | con = bsc_connection_alloc(nat); |
| 226 | msg = msgb_alloc(4096, "test"); |
| 227 | |
| 228 | /* 1.) create a connection */ |
| 229 | copy_to_msg(msg, bsc_cr, sizeof(bsc_cr)); |
| 230 | parsed = bsc_nat_parse(msg); |
| 231 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
| 232 | if (con_found != NULL) { |
Holger Hans Peter Freyther | 72ba162 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 233 | fprintf(stderr, "Con should not exist %p\n", con_found); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 234 | abort(); |
| 235 | } |
| 236 | rc = create_sccp_src_ref(con, msg, parsed); |
| 237 | if (rc != 0) { |
| 238 | fprintf(stderr, "Failed to create a ref\n"); |
| 239 | abort(); |
| 240 | } |
| 241 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 242 | if (!con_found || con_found->bsc != con) { |
Holger Hans Peter Freyther | 72ba162 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 243 | fprintf(stderr, "Failed to find the con: %p\n", con_found); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 244 | abort(); |
| 245 | } |
| 246 | if (memcmp(msg->data, bsc_cr_patched, sizeof(bsc_cr_patched)) != 0) { |
| 247 | fprintf(stderr, "Failed to patch the BSC CR msg.\n"); |
| 248 | abort(); |
| 249 | } |
| 250 | talloc_free(parsed); |
| 251 | |
| 252 | /* 2.) get the cc */ |
| 253 | copy_to_msg(msg, msc_cc, sizeof(msc_cc)); |
| 254 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 255 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 256 | VERIFY(con_found, con, msg, msc_cc_patched, "MSC CC"); |
| 257 | if (update_sccp_src_ref(con_found, parsed) != 0) { |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 258 | fprintf(stderr, "Failed to update the SCCP con.\n"); |
| 259 | abort(); |
| 260 | } |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 261 | |
| 262 | /* 3.) send some data */ |
| 263 | copy_to_msg(msg, bsc_dtap, sizeof(bsc_dtap)); |
| 264 | parsed = bsc_nat_parse(msg); |
| 265 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
| 266 | VERIFY(con_found, con, msg, bsc_dtap_patched, "BSC DTAP"); |
| 267 | |
| 268 | /* 4.) receive some data */ |
| 269 | copy_to_msg(msg, msc_dtap, sizeof(msc_dtap)); |
| 270 | parsed = bsc_nat_parse(msg); |
| 271 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 272 | VERIFY(con_found, con, msg, msc_dtap_patched, "MSC DTAP"); |
| 273 | |
| 274 | /* 5.) close the connection */ |
| 275 | copy_to_msg(msg, msc_rlsd, sizeof(msc_rlsd)); |
| 276 | parsed = bsc_nat_parse(msg); |
| 277 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 278 | VERIFY(con_found, con, msg, msc_rlsd_patched, "MSC RLSD"); |
| 279 | |
| 280 | /* 6.) confirm the connection close */ |
| 281 | copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc)); |
| 282 | parsed = bsc_nat_parse(msg); |
| 283 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 284 | if (!con_found || con_found->bsc != con) { |
Holger Hans Peter Freyther | 72ba162 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 285 | fprintf(stderr, "Failed to find the con: %p\n", con_found); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 286 | abort(); |
| 287 | } |
| 288 | if (memcmp(msg->data, bsc_rlc_patched, sizeof(bsc_rlc_patched)) != 0) { |
| 289 | fprintf(stderr, "Failed to patch the BSC CR msg.\n"); |
| 290 | abort(); |
| 291 | } |
| 292 | remove_sccp_src_ref(con, msg, parsed); |
Holger Hans Peter Freyther | 9d51855 | 2010-04-05 21:44:51 +0200 | [diff] [blame] | 293 | talloc_free(parsed); |
| 294 | |
| 295 | copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc)); |
| 296 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 297 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
| 298 | |
| 299 | /* verify that it is gone */ |
| 300 | if (con_found != NULL) { |
Holger Hans Peter Freyther | 72ba162 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 301 | fprintf(stderr, "Con should be gone. %p\n", con_found); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 302 | abort(); |
| 303 | } |
| 304 | talloc_free(parsed); |
| 305 | |
| 306 | |
| 307 | talloc_free(nat); |
| 308 | msgb_free(msg); |
| 309 | } |
| 310 | |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 311 | static void test_paging(void) |
| 312 | { |
| 313 | struct bsc_nat *nat; |
| 314 | struct bsc_connection *con; |
| 315 | struct bsc_nat_parsed *parsed; |
| 316 | struct msgb *msg; |
| 317 | |
| 318 | fprintf(stderr, "Testing paging by lac.\n"); |
| 319 | |
| 320 | nat = bsc_nat_alloc(); |
| 321 | con = bsc_connection_alloc(nat); |
| 322 | con->lac = 23; |
| 323 | con->authenticated = 1; |
| 324 | llist_add(&con->list_entry, &nat->bsc_connections); |
| 325 | msg = msgb_alloc(4096, "test"); |
| 326 | |
| 327 | /* Test completely bad input */ |
| 328 | copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd)); |
| 329 | if (bsc_nat_find_bsc(nat, msg) != 0) { |
| 330 | fprintf(stderr, "Should have not found anything.\n"); |
| 331 | abort(); |
| 332 | } |
| 333 | |
| 334 | /* Test it by not finding it */ |
| 335 | copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd)); |
| 336 | parsed = bsc_nat_parse(msg); |
| 337 | if (bsc_nat_find_bsc(nat, msg) != 0) { |
| 338 | fprintf(stderr, "Should have not found aynthing.\n"); |
| 339 | abort(); |
| 340 | } |
| 341 | talloc_free(parsed); |
| 342 | |
| 343 | /* Test by finding it */ |
| 344 | con->lac = 8213; |
| 345 | copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd)); |
| 346 | parsed = bsc_nat_parse(msg); |
| 347 | if (bsc_nat_find_bsc(nat, msg) != con) { |
| 348 | fprintf(stderr, "Should have found it.\n"); |
| 349 | abort(); |
| 350 | } |
| 351 | talloc_free(parsed); |
| 352 | } |
| 353 | |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 354 | static void test_mgcp_ass_tracking(void) |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 355 | { |
| 356 | struct sccp_connections con; |
| 357 | struct bsc_nat_parsed *parsed; |
| 358 | struct msgb *msg; |
| 359 | |
| 360 | fprintf(stderr, "Testing MGCP.\n"); |
| 361 | memset(&con, 0, sizeof(con)); |
| 362 | |
| 363 | msg = msgb_alloc(4096, "foo"); |
| 364 | copy_to_msg(msg, ass_cmd, sizeof(ass_cmd)); |
| 365 | parsed = bsc_nat_parse(msg); |
| 366 | if (bsc_mgcp_assign(&con, msg) != 0) { |
| 367 | fprintf(stderr, "Failed to handle assignment.\n"); |
| 368 | abort(); |
| 369 | } |
| 370 | |
| 371 | if (con.msc_timeslot != 21) { |
| 372 | fprintf(stderr, "Timeslot should be 21.\n"); |
| 373 | abort(); |
| 374 | } |
| 375 | |
| 376 | if (con.bsc_timeslot != 21) { |
| 377 | fprintf(stderr, "Assigned timeslot should have been 21.\n"); |
| 378 | abort(); |
| 379 | } |
| 380 | talloc_free(parsed); |
| 381 | |
| 382 | bsc_mgcp_clear(&con); |
| 383 | if (con.bsc_timeslot != -1 || con.msc_timeslot != -1) { |
| 384 | fprintf(stderr, "Clearing should remove the mapping.\n"); |
| 385 | abort(); |
| 386 | } |
| 387 | } |
| 388 | |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 389 | /* test the code to find a given connection */ |
| 390 | static void test_mgcp_find(void) |
| 391 | { |
| 392 | struct bsc_nat *nat; |
| 393 | struct bsc_connection *con; |
| 394 | struct sccp_connections *sccp_con; |
| 395 | |
| 396 | fprintf(stderr, "Testing finding of a BSC Connection\n"); |
| 397 | |
| 398 | nat = bsc_nat_alloc(); |
| 399 | con = bsc_connection_alloc(nat); |
| 400 | llist_add(&con->list_entry, &nat->bsc_connections); |
| 401 | |
| 402 | sccp_con = talloc_zero(con, struct sccp_connections); |
| 403 | sccp_con->msc_timeslot = 12; |
| 404 | sccp_con->bsc_timeslot = 12; |
| 405 | sccp_con->bsc = con; |
| 406 | llist_add(&sccp_con->list_entry, &nat->sccp_connections); |
| 407 | |
| 408 | if (bsc_mgcp_find_con(nat, 11) != NULL) { |
| 409 | fprintf(stderr, "Found the wrong connection.\n"); |
| 410 | abort(); |
| 411 | } |
| 412 | |
| 413 | if (bsc_mgcp_find_con(nat, 12) != con) { |
| 414 | fprintf(stderr, "Didn't find the connection\n"); |
| 415 | abort(); |
| 416 | } |
| 417 | |
| 418 | sccp_con->msc_timeslot = 0; |
| 419 | sccp_con->bsc_timeslot = 0; |
| 420 | if (bsc_mgcp_find_con(nat, 1) != con) { |
| 421 | fprintf(stderr, "Didn't find the connection\n"); |
| 422 | abort(); |
| 423 | } |
| 424 | |
| 425 | /* free everything */ |
| 426 | talloc_free(nat); |
| 427 | } |
| 428 | |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 429 | static void test_mgcp_rewrite(void) |
| 430 | { |
| 431 | int i; |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 432 | struct msgb *output; |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 433 | fprintf(stderr, "Test rewriting MGCP messages.\n"); |
| 434 | |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 435 | for (i = 0; i < ARRAY_SIZE(mgcp_messages); ++i) { |
| 436 | const char *orig = mgcp_messages[i].orig; |
| 437 | const char *patc = mgcp_messages[i].patch; |
| 438 | const char *ip = mgcp_messages[i].ip; |
| 439 | const int port = mgcp_messages[i].port; |
| 440 | |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 441 | char *input = strdup(orig); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 442 | |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 443 | output = bsc_mgcp_rewrite(input, strlen(input), ip, port); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 444 | if (msgb_l2len(output) != strlen(patc)) { |
| 445 | fprintf(stderr, "Wrong sizes for test: %d %d != %d != %d\n", i, msgb_l2len(output), strlen(patc), strlen(orig)); |
| 446 | fprintf(stderr, "String '%s' vs '%s'\n", (const char *) output->l2h, patc); |
| 447 | abort(); |
| 448 | } |
| 449 | |
| 450 | if (memcmp(output->l2h, patc, msgb_l2len(output)) != 0) { |
| 451 | fprintf(stderr, "Broken on %d msg: '%s'\n", i, (const char *) output->l2h); |
| 452 | abort(); |
| 453 | } |
| 454 | |
| 455 | msgb_free(output); |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 456 | free(input); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 457 | } |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 460 | static void test_mgcp_parse(void) |
| 461 | { |
| 462 | int code, ci; |
| 463 | char transaction[60]; |
| 464 | |
| 465 | fprintf(stderr, "Test MGCP response parsing.\n"); |
| 466 | |
| 467 | if (bsc_mgcp_parse_response(crcx_resp, &code, transaction) != 0) { |
| 468 | fprintf(stderr, "Failed to parse CRCX resp.\n"); |
| 469 | abort(); |
| 470 | } |
| 471 | |
| 472 | if (code != 200) { |
| 473 | fprintf(stderr, "Failed to parse the CODE properly. Got: %d\n", code); |
| 474 | abort(); |
| 475 | } |
| 476 | |
| 477 | if (strcmp(transaction, "23265295") != 0) { |
| 478 | fprintf(stderr, "Failed to parse transaction id: '%s'\n", transaction); |
| 479 | abort(); |
| 480 | } |
| 481 | |
| 482 | ci = bsc_mgcp_extract_ci(crcx_resp); |
| 483 | if (ci != 1) { |
| 484 | fprintf(stderr, "Failed to parse the CI. Got: %d\n", ci); |
| 485 | abort(); |
| 486 | } |
| 487 | } |
| 488 | |
Holger Hans Peter Freyther | f05506e | 2010-04-05 09:10:30 +0200 | [diff] [blame] | 489 | static void test_mgcp_parse_port(void) |
| 490 | { |
| 491 | int port; |
| 492 | |
| 493 | fprintf(stderr, "Test MGCP port parsing.\n"); |
| 494 | |
| 495 | port = bsc_mgcp_extract_port(mdcx_resp); |
| 496 | if (port != 4002) { |
| 497 | fprintf(stderr, "Could not parse port. Got: %d\n", port); |
| 498 | abort(); |
| 499 | } |
| 500 | } |
| 501 | |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 502 | int main(int argc, char **argv) |
| 503 | { |
| 504 | struct debug_target *stderr_target; |
| 505 | |
| 506 | stderr_target = debug_target_create_stderr(); |
| 507 | debug_add_target(stderr_target); |
| 508 | debug_set_all_filter(stderr_target, 1); |
| 509 | |
| 510 | test_filter(); |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 511 | test_contrack(); |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 512 | test_paging(); |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 513 | test_mgcp_ass_tracking(); |
| 514 | test_mgcp_find(); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 515 | test_mgcp_rewrite(); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 516 | test_mgcp_parse(); |
Holger Hans Peter Freyther | f05506e | 2010-04-05 09:10:30 +0200 | [diff] [blame] | 517 | test_mgcp_parse_port(); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 518 | return 0; |
| 519 | } |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 520 | |
| 521 | void input_event() |
| 522 | {} |
| 523 | int nm_state_event() |
| 524 | { |
| 525 | return -1; |
| 526 | } |
| 527 | |
| 528 | int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id) |
| 529 | { |
| 530 | return -1; |
| 531 | } |