Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame^] | 1 | #include <osmocom/core/msgb.h> |
| 2 | #include <osmocom/core/logging.h> |
| 3 | #include <osmocom/core/application.h> |
| 4 | #include <osmocom/gsm/gsup.h> |
| 5 | |
| 6 | #include "db.h" |
| 7 | #include "logging.h" |
| 8 | #include "gsup_server.h" |
| 9 | #include "rand.h" |
| 10 | |
| 11 | static struct db_context *g_dbc; |
| 12 | |
| 13 | /* process an incoming SAI request */ |
| 14 | static int rx_send_auth_info(struct osmo_gsup_conn *conn, |
| 15 | const struct osmo_gsup_message *gsup) |
| 16 | { |
| 17 | struct osmo_gsup_message gsup_out; |
| 18 | struct msgb *msg_out; |
| 19 | int rc; |
| 20 | |
| 21 | /* initialize return message structure */ |
| 22 | memset(&gsup_out, 0, sizeof(gsup_out)); |
| 23 | gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT; |
| 24 | memcpy(&gsup_out.imsi, &gsup->imsi, sizeof(gsup_out.imsi)); |
| 25 | |
| 26 | rc = db_get_auc(g_dbc, gsup->imsi, gsup_out.auth_vectors, |
| 27 | ARRAY_SIZE(gsup_out.auth_vectors), |
| 28 | NULL /* gsup->rand_auts */, gsup->auts); |
| 29 | if (rc <= 0) { |
| 30 | gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR; |
| 31 | } |
| 32 | |
| 33 | msg_out = msgb_alloc(1024, "GSUP response"); |
| 34 | osmo_gsup_encode(msg_out, &gsup_out); |
| 35 | return osmo_gsup_conn_send(conn, msg_out); |
| 36 | } |
| 37 | |
| 38 | static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg) |
| 39 | { |
| 40 | static struct osmo_gsup_message gsup; |
| 41 | int rc; |
| 42 | |
| 43 | rc = osmo_gsup_decode(msgb_l3(msg), msgb_l3len(msg), &gsup); |
| 44 | if (rc < 0) { |
| 45 | LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc); |
| 46 | return rc; |
| 47 | } |
| 48 | |
| 49 | switch (gsup.message_type) { |
| 50 | /* requests sent to us */ |
| 51 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST: |
| 52 | rx_send_auth_info(conn, &gsup); |
| 53 | break; |
| 54 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST: |
| 55 | break; |
| 56 | /* responses to requests sent by us */ |
| 57 | case OSMO_GSUP_MSGT_INSERT_DATA_ERROR: |
| 58 | break; |
| 59 | case OSMO_GSUP_MSGT_INSERT_DATA_RESULT: |
| 60 | break; |
| 61 | default: |
| 62 | LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %u\n", |
| 63 | gsup.message_type); |
| 64 | break; |
| 65 | } |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | int main(int argc, char **argv) |
| 71 | { |
| 72 | struct osmo_gsup_server *gs; |
| 73 | int rc; |
| 74 | |
| 75 | rc = osmo_init_logging(&hlr_log_info); |
| 76 | if (rc < 0) { |
| 77 | fprintf(stderr, "Error initializing logging\n"); |
| 78 | exit(1); |
| 79 | } |
| 80 | LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n"); |
| 81 | |
| 82 | rc = rand_init(); |
| 83 | if (rc < 0) { |
| 84 | LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n"); |
| 85 | exit(1); |
| 86 | } |
| 87 | |
| 88 | g_dbc = db_open(NULL, "hlr.db"); |
| 89 | if (!g_dbc) { |
| 90 | LOGP(DMAIN, LOGL_FATAL, "Error opening database\n"); |
| 91 | exit(1); |
| 92 | } |
| 93 | |
| 94 | gs = osmo_gsup_server_create(NULL, NULL, 2222, read_cb); |
| 95 | if (!gs) { |
| 96 | LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n"); |
| 97 | exit(1); |
| 98 | } |
| 99 | |
| 100 | while (1) { |
| 101 | osmo_select_main(0); |
| 102 | } |
| 103 | |
| 104 | db_close(g_dbc); |
| 105 | |
| 106 | log_fini(); |
| 107 | |
| 108 | exit(0); |
| 109 | } |