blob: b0362ad2b7233ddcf05fbf62da3d0816f514effe [file] [log] [blame]
Harald Welteec6915a2018-07-23 14:25:33 +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>
10#include <osmocom/core/utils.h>
11#include <osmocom/core/logging.h>
12#include <osmocom/gsm/gsup.h>
13
14#include <osmocom/gsupclient/gsup_client.h>
15
Harald Welte953d27c2018-07-23 11:11:22 +020016static struct osmo_gsup_client *g_gc;
Harald Welteec6915a2018-07-23 14:25:33 +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);
78 OSMO_STRLCPY_ARRAY(io->imsi, imsi);
79 io->type = type;
80 osmo_timer_setup(&io->timer, imsi_op_timer_cb, io);
81 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 */
104static int 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 int rc;
110
111 OSMO_STRLCPY_ARRAY(gsup.imsi, io->imsi);
112 gsup.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
113
114 rc = osmo_gsup_encode(msg, &gsup);
115 if (rc < 0) {
116 printf("%s: encoding failure (%s)\n", imsi, strerror(-rc));
117 return rc;
118 }
119
Harald Welte953d27c2018-07-23 11:11:22 +0200120 return osmo_gsup_client_send(g_gc, msg);
Harald Welteec6915a2018-07-23 14:25:33 +0200121}
122
123/* allocate + generate + send Send-Auth-Info */
124static int req_loc_upd(const char *imsi)
125{
126 struct imsi_op *io = imsi_op_alloc(g_gc, imsi, IMSI_OP_LU);
127 struct osmo_gsup_message gsup = {0};
128 struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__);
129 int rc;
130
131 OSMO_STRLCPY_ARRAY(gsup.imsi, io->imsi);
132 gsup.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
133
134 rc = osmo_gsup_encode(msg, &gsup);
135 if (rc < 0) {
136 printf("%s: encoding failure (%s)\n", imsi, strerror(-rc));
137 return rc;
138 }
139
Harald Welte953d27c2018-07-23 11:11:22 +0200140 return osmo_gsup_client_send(g_gc, msg);
Harald Welteec6915a2018-07-23 14:25:33 +0200141}
142
143static int resp_isd(struct imsi_op *io)
144{
145 struct osmo_gsup_message gsup = {0};
146 struct msgb *msg = msgb_alloc_headroom(1200, 200, __func__);
147 int rc;
148
149 OSMO_STRLCPY_ARRAY(gsup.imsi, io->imsi);
150 gsup.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
151
152 rc = osmo_gsup_encode(msg, &gsup);
153 if (rc < 0) {
154 printf("%s: encoding failure (%s)\n", io->imsi, strerror(-rc));
155 return rc;
156 }
157
158 imsi_op_release(io);
159
Harald Welte953d27c2018-07-23 11:11:22 +0200160 return osmo_gsup_client_send(g_gc, msg);
Harald Welteec6915a2018-07-23 14:25:33 +0200161}
162
163/* receive an incoming GSUP message */
164static void imsi_op_rx_gsup(struct imsi_op *io, const struct osmo_gsup_message *gsup)
165{
166 int is_error = 0, rc;
167
168 if (OSMO_GSUP_IS_MSGT_ERROR(gsup->message_type)) {
169 imsi_op_stats[io->type].num_rx_error++;
170 is_error = 1;
171 } else
172 imsi_op_stats[io->type].num_rx_success++;
173
174 switch (io->type) {
175 case IMSI_OP_SAI:
176 printf("%s; SAI Response%s\n", io->imsi, is_error ? ": ERROR" : "");
177 /* now that we have auth tuples, request LU */
178 rc = req_loc_upd(io->imsi);
179 if (rc < 0)
180 printf("Failed to request Location Update for %s\n", io->imsi);
181 imsi_op_release(io);
182 break;
183 case IMSI_OP_LU:
184 printf("%s; LU Response%s\n", io->imsi, is_error ? ": ERROR" : "");
185 imsi_op_release(io);
186 break;
187 case IMSI_OP_ISD:
188 printf("%s; ISD Request%s\n", io->imsi, is_error ? ": ERROR" : "");
189 rc = resp_isd(io);
190 if (rc < 0)
191 printf("Failed to insert subscriber data for %s\n", io->imsi);
192 break;
193 default:
194 printf("%s: Unknown\n", io->imsi);
195 imsi_op_release(io);
196 break;
197 }
198}
199
200static int op_type_by_gsup_msgt(enum osmo_gsup_message_type msg_type)
201{
202 switch (msg_type) {
203 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
204 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
205 return IMSI_OP_SAI;
206 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
207 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
208 return IMSI_OP_LU;
209 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
210 return IMSI_OP_ISD;
211 default:
212 printf("Unknown GSUP msg_type %u\n", msg_type);
213 return -1;
214 }
215}
216
Harald Welte953d27c2018-07-23 11:11:22 +0200217static int gsupc_read_cb(struct osmo_gsup_client *gsupc, struct msgb *msg)
Harald Welteec6915a2018-07-23 14:25:33 +0200218{
219 struct osmo_gsup_message gsup_msg = {0};
220 struct imsi_op *io = NULL;
221 int rc;
222
223 DEBUGP(DLGSUP, "Rx GSUP %s\n", msgb_hexdump(msg));
224
225 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup_msg);
226 if (rc < 0)
227 return rc;
228
229 if (!gsup_msg.imsi[0])
230 return -1;
231
232 rc = op_type_by_gsup_msgt(gsup_msg.message_type);
233 if (rc < 0)
234 return rc;
235
236 switch (rc) {
237 case IMSI_OP_SAI:
238 case IMSI_OP_LU:
239 io = imsi_op_find(gsup_msg.imsi, rc);
240 break;
241 case IMSI_OP_ISD:
242 /* ISD is an inbound transaction */
243 io = imsi_op_alloc(g_gc, gsup_msg.imsi, IMSI_OP_ISD);
244 break;
245 }
246 if (!io)
247 return -1;
248
249 imsi_op_rx_gsup(io, &gsup_msg);
250 msgb_free(msg);
251
252 return 0;
253}
254
255static void print_report(void)
256{
257 unsigned int i;
258
259 for (i = 0; i < ARRAY_SIZE(imsi_op_stats); i++) {
260 struct imsi_op_stats *st = &imsi_op_stats[i];
261 const char *name = get_value_string(imsi_op_names, i);
262 printf("%s: %u alloc, %u released, %u success, %u error , %u tout\n",
263 name, st->num_alloc, st->num_released, st->num_rx_success,
264 st->num_rx_error, st->num_timeout);
265 }
266}
267
268static void sig_cb(int sig)
269{
270 switch (sig) {
271 case SIGINT:
272 print_report();
273 exit(0);
274 break;
275 }
276}
277
278/* default categories */
279static struct log_info_cat default_categories[] = {
280};
281
282static const struct log_info gsup_test_client_log_info = {
283 .cat = default_categories,
284 .num_cat = ARRAY_SIZE(default_categories),
285};
286
287int main(int argc, char **argv)
288{
289 unsigned long long i;
290 char *server_host = "127.0.0.1";
291 uint16_t server_port = OSMO_GSUP_PORT;
292 void *ctx = talloc_named_const(NULL, 0, "gsup_test_client");
293
294 osmo_init_logging2(ctx, &gsup_test_client_log_info);
295
Harald Welte953d27c2018-07-23 11:11:22 +0200296 g_gc = osmo_gsup_client_create(ctx, "GSUPTEST", server_host, server_port,
297 gsupc_read_cb, NULL);
Harald Welteec6915a2018-07-23 14:25:33 +0200298
299
300 signal(SIGINT, sig_cb);
301
302 for (i = 0; i < 10000; i++) {
303 unsigned long long imsi = 901790000000000 + i;
304 char imsi_buf[17] = { 0 };
305 int rc;
306
307 snprintf(imsi_buf, sizeof(imsi_buf), "%015llu", imsi);
308 rc = req_auth_info(imsi_buf);
309 if (rc < 0)
310 printf("Failed to request Auth Info for %s\n", imsi_buf);
311
312 osmo_select_main(0);
313 }
314
315 while (1) {
316 osmo_select_main(0);
317 }
318
319 print_report();
320 exit(0);
321}