Harald Welte | abc6505 | 2016-05-06 23:27:38 +0200 | [diff] [blame] | 1 | #include <string.h> |
| 2 | #include <stdio.h> |
| 3 | #include <errno.h> |
| 4 | #include <signal.h> |
| 5 | |
| 6 | #include <osmocom/core/linuxlist.h> |
| 7 | #include <osmocom/core/msgb.h> |
| 8 | #include <osmocom/core/select.h> |
| 9 | #include <osmocom/core/application.h> |
| 10 | #include <osmocom/gsm/gsup.h> |
| 11 | |
Neels Hofmeyr | db1d801 | 2016-12-08 21:22:58 +0100 | [diff] [blame] | 12 | #include <openbsc/gsup_client.h> |
Harald Welte | abc6505 | 2016-05-06 23:27:38 +0200 | [diff] [blame] | 13 | #include <openbsc/debug.h> |
| 14 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 15 | static struct gsup_client *g_gc; |
Harald Welte | abc6505 | 2016-05-06 23:27:38 +0200 | [diff] [blame] | 16 | |
| 17 | |
| 18 | /*********************************************************************** |
| 19 | * IMSI Operation |
| 20 | ***********************************************************************/ |
| 21 | static LLIST_HEAD(g_imsi_ops); |
| 22 | |
| 23 | struct imsi_op_stats { |
| 24 | uint32_t num_alloc; |
| 25 | uint32_t num_released; |
| 26 | uint32_t num_rx_success; |
| 27 | uint32_t num_rx_error; |
| 28 | uint32_t num_timeout; |
| 29 | }; |
| 30 | |
| 31 | enum imsi_op_type { |
| 32 | IMSI_OP_SAI, |
| 33 | IMSI_OP_LU, |
| 34 | IMSI_OP_ISD, |
| 35 | _NUM_IMSI_OP |
| 36 | }; |
| 37 | |
| 38 | static const struct value_string imsi_op_names[] = { |
| 39 | { IMSI_OP_SAI, "SAI" }, |
| 40 | { IMSI_OP_LU, "LU" }, |
| 41 | { IMSI_OP_ISD, "ISD" }, |
| 42 | { 0, NULL } |
| 43 | }; |
| 44 | |
| 45 | static struct imsi_op_stats imsi_op_stats[_NUM_IMSI_OP]; |
| 46 | |
| 47 | struct imsi_op { |
| 48 | struct llist_head list; |
| 49 | char imsi[17]; |
| 50 | enum imsi_op_type type; |
| 51 | struct osmo_timer_list timer; |
| 52 | }; |
| 53 | |
| 54 | static struct imsi_op *imsi_op_find(const char *imsi, |
| 55 | enum imsi_op_type type) |
| 56 | { |
| 57 | struct imsi_op *io; |
| 58 | |
| 59 | llist_for_each_entry(io, &g_imsi_ops, list) { |
| 60 | if (!strcmp(io->imsi, imsi) && io->type == type) |
| 61 | return io; |
| 62 | } |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | static void imsi_op_timer_cb(void *data); |
| 67 | |
| 68 | static struct imsi_op *imsi_op_alloc(void *ctx, const char *imsi, |
| 69 | enum imsi_op_type type) |
| 70 | { |
| 71 | struct imsi_op *io; |
| 72 | |
| 73 | if (imsi_op_find(imsi, type)) |
| 74 | return NULL; |
| 75 | |
| 76 | io = talloc_zero(ctx, struct imsi_op); |
| 77 | strncpy(io->imsi, imsi, sizeof(io->imsi)); |
| 78 | io->imsi[sizeof(io->imsi)-1] = '\0'; |
| 79 | io->type = type; |
| 80 | io->timer.cb = imsi_op_timer_cb; |
| 81 | io->timer.data = io; |
| 82 | llist_add(&io->list, &g_imsi_ops); |
| 83 | imsi_op_stats[type].num_alloc++; |
| 84 | |
| 85 | return io; |
| 86 | } |
| 87 | |
| 88 | static void imsi_op_release(struct imsi_op *io) |
| 89 | { |
| 90 | osmo_timer_del(&io->timer); |
| 91 | llist_del(&io->list); |
| 92 | imsi_op_stats[io->type].num_released++; |
| 93 | talloc_free(io); |
| 94 | } |
| 95 | |
| 96 | static void imsi_op_timer_cb(void *data) |
| 97 | { |
| 98 | struct imsi_op *io = data; |
| 99 | printf("%s: Timer expiration\n", io->imsi); |
| 100 | imsi_op_stats[io->type].num_timeout++; |
| 101 | imsi_op_release(io); |
| 102 | } |
| 103 | |
| 104 | /* allocate + generate + send Send-Auth-Info */ |
| 105 | int req_auth_info(const char *imsi) |
| 106 | { |
| 107 | struct imsi_op *io = imsi_op_alloc(g_gc, imsi, IMSI_OP_SAI); |
| 108 | struct osmo_gsup_message gsup = {0}; |
| 109 | struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__); |
| 110 | |
| 111 | strncpy(gsup.imsi, io->imsi, sizeof(gsup.imsi)); |
| 112 | gsup.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST; |
| 113 | |
| 114 | osmo_gsup_encode(msg, &gsup); |
| 115 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 116 | return gsup_client_send(g_gc, msg); |
Harald Welte | abc6505 | 2016-05-06 23:27:38 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* allocate + generate + send Send-Auth-Info */ |
| 120 | int req_loc_upd(const char *imsi) |
| 121 | { |
| 122 | struct imsi_op *io = imsi_op_alloc(g_gc, imsi, IMSI_OP_LU); |
| 123 | struct osmo_gsup_message gsup = {0}; |
| 124 | struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__); |
| 125 | |
| 126 | strncpy(gsup.imsi, io->imsi, sizeof(gsup.imsi)); |
| 127 | gsup.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST; |
| 128 | |
| 129 | osmo_gsup_encode(msg, &gsup); |
| 130 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 131 | return gsup_client_send(g_gc, msg); |
Harald Welte | abc6505 | 2016-05-06 23:27:38 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int resp_isd(struct imsi_op *io) |
| 135 | { |
| 136 | struct osmo_gsup_message gsup = {0}; |
| 137 | struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__); |
| 138 | |
| 139 | strncpy(gsup.imsi, io->imsi, sizeof(gsup.imsi)); |
| 140 | gsup.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT; |
| 141 | |
| 142 | osmo_gsup_encode(msg, &gsup); |
| 143 | |
| 144 | imsi_op_release(io); |
| 145 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 146 | return gsup_client_send(g_gc, msg); |
Harald Welte | abc6505 | 2016-05-06 23:27:38 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* receive an incoming GSUP message */ |
| 150 | static void imsi_op_rx_gsup(struct imsi_op *io, const struct osmo_gsup_message *gsup) |
| 151 | { |
| 152 | int is_error = 0; |
| 153 | |
| 154 | if (OSMO_GSUP_IS_MSGT_ERROR(gsup->message_type)) { |
| 155 | imsi_op_stats[io->type].num_rx_error++; |
| 156 | is_error = 1; |
| 157 | } else |
| 158 | imsi_op_stats[io->type].num_rx_success++; |
| 159 | |
| 160 | switch (io->type) { |
| 161 | case IMSI_OP_SAI: |
| 162 | printf("%s; SAI Response%s\n", io->imsi, is_error ? ": ERROR" : ""); |
| 163 | /* now that we have auth tuples, request LU */ |
| 164 | req_loc_upd(io->imsi); |
| 165 | imsi_op_release(io); |
| 166 | break; |
| 167 | case IMSI_OP_LU: |
| 168 | printf("%s; LU Response%s\n", io->imsi, is_error ? ": ERROR" : ""); |
| 169 | imsi_op_release(io); |
| 170 | break; |
| 171 | case IMSI_OP_ISD: |
| 172 | printf("%s; ISD Request%s\n", io->imsi, is_error ? ": ERROR" : ""); |
| 173 | resp_isd(io); |
| 174 | break; |
| 175 | default: |
| 176 | printf("%s: Unknown\n", io->imsi); |
| 177 | imsi_op_release(io); |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | static int op_type_by_gsup_msgt(enum osmo_gsup_message_type msg_type) |
| 183 | { |
| 184 | switch (msg_type) { |
| 185 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT: |
| 186 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR: |
| 187 | return IMSI_OP_SAI; |
| 188 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT: |
| 189 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR: |
| 190 | return IMSI_OP_LU; |
| 191 | case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST: |
| 192 | return IMSI_OP_ISD; |
| 193 | default: |
| 194 | printf("Unknown GSUP msg_type %u\n", msg_type); |
| 195 | return -1; |
| 196 | } |
| 197 | } |
| 198 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 199 | static int gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg) |
Harald Welte | abc6505 | 2016-05-06 23:27:38 +0200 | [diff] [blame] | 200 | { |
| 201 | struct osmo_gsup_message gsup_msg = {0}; |
| 202 | struct imsi_op *io; |
| 203 | int rc; |
| 204 | |
| 205 | DEBUGP(DGPRS, "Rx GSUP %s\n", osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); |
| 206 | |
| 207 | rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup_msg); |
| 208 | if (rc < 0) |
| 209 | return rc; |
| 210 | |
| 211 | if (!gsup_msg.imsi[0]) |
| 212 | return -1; |
| 213 | |
| 214 | rc = op_type_by_gsup_msgt(gsup_msg.message_type); |
| 215 | if (rc < 0) |
| 216 | return rc; |
| 217 | |
| 218 | switch (rc) { |
| 219 | case IMSI_OP_SAI: |
| 220 | case IMSI_OP_LU: |
| 221 | io = imsi_op_find(gsup_msg.imsi, rc); |
| 222 | if (!io) |
| 223 | return -1; |
| 224 | break; |
| 225 | case IMSI_OP_ISD: |
| 226 | /* ISD is an inbound transaction */ |
| 227 | io = imsi_op_alloc(g_gc, gsup_msg.imsi, IMSI_OP_ISD); |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | imsi_op_rx_gsup(io, &gsup_msg); |
| 232 | msgb_free(msg); |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static void print_report(void) |
| 238 | { |
| 239 | unsigned int i; |
| 240 | |
| 241 | for (i = 0; i < ARRAY_SIZE(imsi_op_stats); i++) { |
| 242 | struct imsi_op_stats *st = &imsi_op_stats[i]; |
| 243 | const char *name = get_value_string(imsi_op_names, i); |
| 244 | printf("%s: %u alloc, %u released, %u success, %u error , %u tout\n", |
| 245 | name, st->num_alloc, st->num_released, st->num_rx_success, |
| 246 | st->num_rx_error, st->num_timeout); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | static void sig_cb(int sig) |
| 251 | { |
| 252 | switch (sig) { |
| 253 | case SIGINT: |
| 254 | print_report(); |
| 255 | exit(0); |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void *tall_bsc_ctx = NULL; |
| 261 | |
| 262 | /* default categories */ |
Neels Hofmeyr | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 263 | static struct log_info_cat default_categories[] = { |
Harald Welte | abc6505 | 2016-05-06 23:27:38 +0200 | [diff] [blame] | 264 | }; |
| 265 | |
Neels Hofmeyr | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 266 | static const struct log_info gsup_test_client_log_info = { |
| 267 | .cat = default_categories, |
| 268 | .num_cat = ARRAY_SIZE(default_categories), |
Harald Welte | abc6505 | 2016-05-06 23:27:38 +0200 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | int main(int argc, char **argv) |
| 272 | { |
| 273 | unsigned long long i; |
| 274 | char *server_host = "127.0.0.1"; |
| 275 | uint16_t server_port = 2222; |
| 276 | |
Neels Hofmeyr | 7aef97a | 2016-12-08 21:28:29 +0100 | [diff] [blame] | 277 | osmo_init_logging(&gsup_test_client_log_info); |
Harald Welte | abc6505 | 2016-05-06 23:27:38 +0200 | [diff] [blame] | 278 | |
Neels Hofmeyr | 0e5c1e1 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 279 | g_gc = gsup_client_create(server_host, server_port, gsupc_read_cb, |
| 280 | NULL); |
Harald Welte | abc6505 | 2016-05-06 23:27:38 +0200 | [diff] [blame] | 281 | |
| 282 | |
| 283 | signal(SIGINT, sig_cb); |
| 284 | |
| 285 | for (i = 0; i < 10000; i++) { |
| 286 | unsigned long long imsi = 901790000000000 + i; |
| 287 | char imsi_buf[17]; |
| 288 | snprintf(imsi_buf, sizeof(imsi_buf), "%015llu", imsi); |
| 289 | req_auth_info(imsi_buf); |
| 290 | osmo_select_main(0); |
| 291 | } |
| 292 | |
| 293 | while (1) { |
| 294 | osmo_select_main(0); |
| 295 | } |
| 296 | |
| 297 | print_report(); |
| 298 | exit(0); |
| 299 | } |