blob: 8be4e7aeab0f849da24b66f3d06756841773d72c [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;
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
88static 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
96static 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 */
105int 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
Neels Hofmeyr59504dc2017-01-13 03:10:54 +0100111 osmo_strlcpy(gsup.imsi, io->imsi, sizeof(gsup.imsi));
Harald Welte4f8e34b2016-05-06 23:27:38 +0200112 gsup.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
113
114 osmo_gsup_encode(msg, &gsup);
115
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100116 return gsup_client_send(g_gc, msg);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200117}
118
119/* allocate + generate + send Send-Auth-Info */
120int 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
Neels Hofmeyr59504dc2017-01-13 03:10:54 +0100126 osmo_strlcpy(gsup.imsi, io->imsi, sizeof(gsup.imsi));
Harald Welte4f8e34b2016-05-06 23:27:38 +0200127 gsup.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
128
129 osmo_gsup_encode(msg, &gsup);
130
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100131 return gsup_client_send(g_gc, msg);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200132}
133
134int 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
Neels Hofmeyr59504dc2017-01-13 03:10:54 +0100139 osmo_strlcpy(gsup.imsi, io->imsi, sizeof(gsup.imsi));
Harald Welte4f8e34b2016-05-06 23:27:38 +0200140 gsup.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
141
142 osmo_gsup_encode(msg, &gsup);
143
144 imsi_op_release(io);
145
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100146 return gsup_client_send(g_gc, msg);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200147}
148
149/* receive an incoming GSUP message */
150static 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
182static 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 Hofmeyr814fef02016-12-08 21:19:57 +0100199static int gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg)
Harald Welte4f8e34b2016-05-06 23:27:38 +0200200{
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
237static 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
250static void sig_cb(int sig)
251{
252 switch (sig) {
253 case SIGINT:
254 print_report();
255 exit(0);
256 break;
257 }
258}
259
260void *tall_bsc_ctx = NULL;
261
262/* default categories */
Neels Hofmeyref022782016-12-08 21:28:29 +0100263static struct log_info_cat default_categories[] = {
Harald Welte4f8e34b2016-05-06 23:27:38 +0200264};
265
Neels Hofmeyref022782016-12-08 21:28:29 +0100266static const struct log_info gsup_test_client_log_info = {
267 .cat = default_categories,
268 .num_cat = ARRAY_SIZE(default_categories),
Harald Welte4f8e34b2016-05-06 23:27:38 +0200269};
270
271int 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 Hofmeyref022782016-12-08 21:28:29 +0100277 osmo_init_logging(&gsup_test_client_log_info);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200278
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100279 g_gc = gsup_client_create(server_host, server_port, gsupc_read_cb,
280 NULL);
Harald Welte4f8e34b2016-05-06 23:27:38 +0200281
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}