blob: 4e4ef782d12d0920fe33650abf64377e3ae42902 [file] [log] [blame]
Harald Welte7d29d592018-07-28 22:00:08 +02001/* osmo-demo-euse: An External USSD Entity (EUSE) for demo purpose */
2
3/* (C) 2018 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21/*
22 * This program illustrates how to implement an external USSD application using
23 * the existing osmocom libraries, particularly libosmocore, libosmogsm and libosmo-gsup-client.
24 *
25 * It will receive any MS-originated USSD message that is routed to it via the HLR, and
26 * simply respond it quoted in the following string: 'You sent "foobar"' (assuming the original
27 * message was 'foobar').
28 */
29
30#include <string.h>
31#include <stdio.h>
32#include <errno.h>
33#include <signal.h>
34
35#include <osmocom/core/msgb.h>
36#include <osmocom/core/select.h>
37#include <osmocom/core/application.h>
38#include <osmocom/core/utils.h>
39#include <osmocom/core/logging.h>
40
41#include <osmocom/gsm/gsup.h>
42#include <osmocom/gsm/gsm0480.h>
43#include <osmocom/gsm/protocol/gsm_04_80.h>
44
45#include <osmocom/gsupclient/gsup_client.h>
46
47#include "logging.h"
48
49static struct osmo_gsup_client *g_gc;
50
51/*! send a SS/USSD response to a given imsi/session.
52 * \param[in] gsupc GSUP client connection through which to send
53 * \param[in] imsi IMSI of the subscriber
54 * \param[in] session_id Unique identifier of SS session for which this response is
55 * \param[in] gsup_msg_type GSUP message type (OSMO_GSUP_MSGT_PROC_SS_{REQUEST,RESULT,ERROR})
56 * \param[in] final Is this the final result (true=END) or an intermediate result (false=CONTINUE)
57 * \param[in] msg Optional binary/BER encoded SS date (for FACILITY IE). Can be NULL. Freed in
58 * this function call.
59 */
60static int euse_tx_ss(struct osmo_gsup_client *gsupc, const char *imsi, uint32_t session_id,
61 enum osmo_gsup_message_type gsup_msg_type, bool final, struct msgb *ss_msg)
62{
63 struct osmo_gsup_message resp = {0};
64 struct msgb *resp_msg;
65
66 switch (gsup_msg_type) {
67 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
68 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
69 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
70 break;
71 default:
72 msgb_free(ss_msg);
73 return -EINVAL;
74 }
75
76 resp.message_type = gsup_msg_type;
77 OSMO_STRLCPY_ARRAY(resp.imsi, imsi);
78 if (final)
79 resp.session_state = OSMO_GSUP_SESSION_STATE_END;
80 else
81 resp.session_state = OSMO_GSUP_SESSION_STATE_CONTINUE;
82 resp.session_id = session_id;
83 if (ss_msg) {
84 resp.ss_info = msgb_data(ss_msg);
85 resp.ss_info_len = msgb_length(ss_msg);
86 }
87
88 resp_msg = gsm0480_msgb_alloc_name(__func__);
89 OSMO_ASSERT(resp_msg);
90 osmo_gsup_encode(resp_msg, &resp);
91 msgb_free(ss_msg);
92 return osmo_gsup_client_send(gsupc, resp_msg);
93}
94
95/*! send a SS/USSD reject to a given IMSI/session.
96 * \param[in] gsupc GSUP client connection through which to send
97 * \param[in] imsi IMSI of the subscriber
98 * \param[in] session_id Unique identifier of SS session for which this response is
99 * \param[in] invoke_id InvokeID of the request
100 * \param[in] problem_tag Problem code tag (table 3.13)
101 * \param[in] problem_code Problem code (table 3.14-3.17)
102 */
103static int euse_tx_ussd_reject(struct osmo_gsup_client *gsupc, const char *imsi, uint32_t session_id,
104 int invoke_id, uint8_t problem_tag, uint8_t problem_code)
105{
106 struct msgb *msg = gsm0480_gen_reject(invoke_id, problem_tag, problem_code);
107 LOGP(DMAIN, LOGL_NOTICE, "Tx %s/0x%08x: Reject(%d, 0x%02x, 0x%02x)\n", imsi, session_id,
108 invoke_id, problem_tag, problem_code);
109 OSMO_ASSERT(msg);
110 return euse_tx_ss(gsupc, imsi, session_id, OSMO_GSUP_MSGT_PROC_SS_RESULT, true, msg);
111}
112
113/*! send a SS/USSD response in 7-bit GSM default alphabet o a given imsi/session.
114 * \param[in] gsupc GSUP client connection through which to send
115 * \param[in] imsi IMSI of the subscriber
116 * \param[in] session_id Unique identifier of SS session for which this response is
117 * \param[in] final Is this the final result (true=END) or an intermediate result
118 * (false=CONTINUE)
119 * \param[in] invoke_id InvokeID of the request
120 */
121static int euse_tx_ussd_resp_7bit(struct osmo_gsup_client *gsupc, const char *imsi, uint32_t session_id,
122 bool final, uint8_t invoke_id, const char *text)
123{
124 struct msgb *ss_msg;
125
126 /* encode response; remove L3 header */
127 ss_msg = gsm0480_gen_ussd_resp_7bit(invoke_id, text);
128 LOGP(DMAIN, LOGL_DEBUG, "Tx %s/0x%08x: USSD Result(%d, %s, '%s')\n", imsi, session_id,
129 invoke_id, final ? "END" : "CONTINUE", text);
130 OSMO_ASSERT(ss_msg);
131 return euse_tx_ss(gsupc, imsi, session_id, OSMO_GSUP_MSGT_PROC_SS_RESULT, final, ss_msg);
132}
133
134static int euse_rx_proc_ss_req(struct osmo_gsup_client *gsupc, const struct osmo_gsup_message *gsup)
135{
136 char buf[GSM0480_USSD_7BIT_STRING_LEN+1];
137 struct ss_request req = {0};
138
139 if (gsup->ss_info && gsup->ss_info_len) {
140 if (gsm0480_parse_facility_ie(gsup->ss_info, gsup->ss_info_len, &req)) {
141 return euse_tx_ussd_reject(gsupc, gsup->imsi, gsup->session_id, -1,
142 GSM_0480_PROBLEM_CODE_TAG_GENERAL,
143 GSM_0480_GEN_PROB_CODE_BAD_STRUCTURE);
144 }
145 }
146
147 LOGP(DMAIN, LOGL_INFO, "Rx %s/0x%08x: USSD SessionState=%s, OpCode=%s, '%s'\n", gsup->imsi,
148 gsup->session_id, osmo_gsup_session_state_name(gsup->session_state),
149 gsm0480_op_code_name(req.opcode), req.ussd_text);
150
151 /* we only handle single-request-response USSD in this demo */
152 if (gsup->session_state != OSMO_GSUP_SESSION_STATE_BEGIN) {
153 return euse_tx_ussd_reject(gsupc, gsup->imsi, gsup->session_id, req.invoke_id,
154 GSM_0480_PROBLEM_CODE_TAG_GENERAL,
155 GSM_0480_GEN_PROB_CODE_UNRECOGNISED);
156 }
157
158 snprintf(buf, sizeof(buf), "You sent \"%s\"", req.ussd_text);
159 return euse_tx_ussd_resp_7bit(gsupc, gsup->imsi, gsup->session_id, true, req.invoke_id, buf);
160}
161
162static int gsupc_read_cb(struct osmo_gsup_client *gsupc, struct msgb *msg)
163{
164 struct osmo_gsup_message gsup_msg = {0};
165 int rc;
166
167 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup_msg);
168 if (rc < 0) {
169 LOGP(DMAIN, LOGL_ERROR, "Error decoding GSUP: %s\n", msgb_hexdump(msg));
170 return rc;
171 }
172 DEBUGP(DMAIN, "Rx GSUP %s: %s\n", osmo_gsup_message_type_name(gsup_msg.message_type),
173 msgb_hexdump(msg));
174
175 switch (gsup_msg.message_type) {
176 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
177 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
178 euse_rx_proc_ss_req(gsupc, &gsup_msg);
179 break;
180 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
181 break;
182 default:
183 LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n",
184 osmo_gsup_message_type_name(gsup_msg.message_type));
185 break;
186 }
187
188 msgb_free(msg);
189 return 0;
190}
191
192
193static struct log_info_cat default_categories[] = {
194 [DMAIN] = {
195 .name = "DMAIN",
196 .description = "Main Program",
197 .enabled = 1, .loglevel = LOGL_DEBUG,
198 },
199};
200
201static const struct log_info gsup_log_info = {
202 .cat = default_categories,
203 .num_cat = ARRAY_SIZE(default_categories),
204};
205
206static void print_usage(void)
207{
208 printf("Usage: osmo-euse-demo [hlr-ip [hlr-gsup-port]]\n");
209}
210
211int main(int argc, char **argv)
212{
213 char *server_host = "127.0.0.1";
214 uint16_t server_port = OSMO_GSUP_PORT;
215 void *ctx = talloc_named_const(NULL, 0, "demo-euse");
216
217 osmo_init_logging2(ctx, &gsup_log_info);
218
219 printf("argc=%d\n", argc);
220
221 if (argc > 1) {
222 if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
223 print_usage();
224 exit(0);
225 } else
226 server_host = argv[1];
227 }
228 if (argc > 2)
229 server_port = atoi(argv[2]);
230
231 g_gc = osmo_gsup_client_create(ctx, "EUSE-foobar", server_host, server_port, gsupc_read_cb, NULL);
232
233 while (1) {
234 osmo_select_main(0);
235 }
236
237 exit(0);
238}
239