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