blob: 2a93d7dfbf0889ac8a7711c10be33d8bedaac7bd [file] [log] [blame]
Harald Welteaabae9e2016-04-28 12:48:14 +02001#include <signal.h>
2
Harald Weltee72cf552016-04-28 07:18:49 +02003#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
13static struct db_context *g_dbc;
14
15/* process an incoming SAI request */
16static 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
40static 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 }
68 return 0;
69}
70
Harald Welteaabae9e2016-04-28 12:48:14 +020071static struct osmo_gsup_server *gs;
72
73static void signal_hdlr(int signal)
74{
75 switch (signal) {
76 case SIGINT:
77 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to SIGINT\n");
78 osmo_gsup_server_destroy(gs);
79 db_close(g_dbc);
80 log_fini();
81 exit(0);
82 break;
83 case SIGUSR1:
84 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
85 talloc_report_full(NULL, stderr);
86 break;
87 }
88}
Harald Weltee72cf552016-04-28 07:18:49 +020089
90int main(int argc, char **argv)
91{
Harald Weltee72cf552016-04-28 07:18:49 +020092 int rc;
93
Harald Welteaabae9e2016-04-28 12:48:14 +020094 talloc_enable_leak_report_full();
95
Harald Weltee72cf552016-04-28 07:18:49 +020096 rc = osmo_init_logging(&hlr_log_info);
97 if (rc < 0) {
98 fprintf(stderr, "Error initializing logging\n");
99 exit(1);
100 }
101 LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
102
103 rc = rand_init();
104 if (rc < 0) {
105 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
106 exit(1);
107 }
108
109 g_dbc = db_open(NULL, "hlr.db");
110 if (!g_dbc) {
111 LOGP(DMAIN, LOGL_FATAL, "Error opening database\n");
112 exit(1);
113 }
114
115 gs = osmo_gsup_server_create(NULL, NULL, 2222, read_cb);
116 if (!gs) {
117 LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
118 exit(1);
119 }
120
Harald Welteaabae9e2016-04-28 12:48:14 +0200121 osmo_init_ignore_signals();
122 signal(SIGINT, &signal_hdlr);
123 signal(SIGUSR1, &signal_hdlr);
124
125 //osmo_daemonize();
126
Harald Weltee72cf552016-04-28 07:18:49 +0200127 while (1) {
128 osmo_select_main(0);
129 }
130
131 db_close(g_dbc);
132
133 log_fini();
134
135 exit(0);
136}