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