changeset 3:8680979baeb1

smsc-daemon: first version put together
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 17 Aug 2023 22:56:49 -0800
parents 2067c55e2c79
children 02112c3a04ae
files .hgignore smsc-daemon/Makefile smsc-daemon/gsup_rx.c smsc-daemon/logging.h smsc-daemon/main.c smsc-daemon/record_mo.c
diffstat 6 files changed, 360 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Tue Aug 01 17:23:44 2023 -0800
+++ b/.hgignore	Thu Aug 17 22:56:49 2023 -0800
@@ -3,3 +3,5 @@
 \.[oa]$
 
 ^euse-demo/osmo-euse-demo$
+
+^smsc-daemon/proto-smsc-daemon$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/smsc-daemon/Makefile	Thu Aug 17 22:56:49 2023 -0800
@@ -0,0 +1,14 @@
+CC=	gcc
+CFLAGS=	-O2 -pthread -I/opt/osmocni2/include -I/usr/include/samba-4.0
+PROG=	proto-smsc-daemon
+OBJS=	gsup_rx.o main.o record_mo.o
+OSMOLIB=-L/opt/osmocni2/lib -Wl,-rpath,/opt/osmocni2/lib -losmo-gsup-client \
+	-losmogsm -losmocore -ltalloc
+
+all:	${PROG}
+
+${PROG}:	${OBJS}
+	${CC} -o $@ ${OBJS} ${OSMOLIB}
+
+clean:
+	rm -f *.o ${PROG}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/smsc-daemon/gsup_rx.c	Thu Aug 17 22:56:49 2023 -0800
@@ -0,0 +1,118 @@
+/*
+ * This C module is part of proto-smsc-daemon concoction, a prototype/test
+ * implementation of a GSUP-based GSM SMSC.  It is based on the osmo-demo-euse
+ * program from OsmoHLR package.
+ *
+ * proto-smsc-daemon author: Mychaela N. Falconia <falcon@freecalypso.org>,
+ * no copyright.
+ *
+ * osmo-demo-euse author: Harald Welte <laforge@gnumonks.org>, (C) 2018, AGPL3+
+ */
+
+#include <ctype.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <signal.h>
+
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/application.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/logging.h>
+
+#include <osmocom/gsm/gsup.h>
+
+#include <osmocom/gsupclient/gsup_client.h>
+
+#include "logging.h"
+
+extern void record_mo_sm(struct osmo_gsup_message *gmsg);
+
+static void send_gsup_response(struct osmo_gsup_client *gsupc,
+				struct osmo_gsup_message *orig_msg)
+{
+	struct osmo_gsup_message resp = {0};
+	struct msgb *resp_msg;
+
+	resp.message_type = OSMO_GSUP_TO_MSGT_RESULT(orig_msg->message_type);
+	OSMO_STRLCPY_ARRAY(resp.imsi, orig_msg->imsi);
+	resp.message_class = OSMO_GSUP_MESSAGE_CLASS_SMS;
+	resp.sm_rp_mr = orig_msg->sm_rp_mr;
+	resp.destination_name = orig_msg->source_name;
+	resp.destination_name_len = orig_msg->source_name_len;
+
+	resp_msg = osmo_gsup_client_msgb_alloc();
+	OSMO_ASSERT(resp_msg);
+	osmo_gsup_encode(resp_msg, &resp);
+	osmo_gsup_client_send(gsupc, resp_msg);
+}
+
+static void handle_mo_forward_sm(struct osmo_gsup_client *gsupc,
+				 struct osmo_gsup_message *gmsg)
+{
+	if (!gmsg->sm_rp_mr) {
+		LOGP(DMAIN, LOGL_ERROR,
+		     "error in MO-FORWARD-SM: missing SM-RP-MR\n");
+		return;
+	}
+	if (!gmsg->sm_rp_da_type) {
+		LOGP(DMAIN, LOGL_ERROR,
+		     "error in MO-FORWARD-SM: missing SM-RP-DA\n");
+		return;
+	}
+	if (!gmsg->sm_rp_oa_type) {
+		LOGP(DMAIN, LOGL_ERROR,
+		     "error in MO-FORWARD-SM: missing SM-RP-OA\n");
+		return;
+	}
+	if (!gmsg->sm_rp_ui) {
+		LOGP(DMAIN, LOGL_ERROR,
+		     "error in MO-FORWARD-SM: missing SM-RP-UI\n");
+		return;
+	}
+	record_mo_sm(gmsg);
+	send_gsup_response(gsupc, gmsg);
+}
+
+static void handle_ready_for_sm(struct osmo_gsup_client *gsupc,
+				struct osmo_gsup_message *gmsg)
+{
+	if (!gmsg->sm_rp_mr) {
+		LOGP(DMAIN, LOGL_ERROR,
+		     "error in READY-FOR-SM: missing SM-RP-MR\n");
+		return;
+	}
+	send_gsup_response(gsupc, gmsg);
+}
+
+int gsupc_read_cb(struct osmo_gsup_client *gsupc, struct msgb *msg)
+{
+	struct osmo_gsup_message gsup_msg = {0};
+	int rc;
+
+	rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup_msg);
+	if (rc < 0) {
+		LOGP(DMAIN, LOGL_ERROR, "Error decoding GSUP: %s\n", msgb_hexdump(msg));
+		return rc;
+	}
+	DEBUGP(DMAIN, "Rx GSUP %s: %s\n", osmo_gsup_message_type_name(gsup_msg.message_type),
+		msgb_hexdump(msg));
+
+	switch (gsup_msg.message_type) {
+	case OSMO_GSUP_MSGT_MO_FORWARD_SM_REQUEST:
+		handle_mo_forward_sm(gsupc, &gsup_msg);
+		break;
+	case OSMO_GSUP_MSGT_READY_FOR_SM_REQUEST:
+		handle_ready_for_sm(gsupc, &gsup_msg);
+		break;
+	default:
+		LOGP(DMAIN, LOGL_ERROR, "Unhandled GSUP message type %s\n",
+			osmo_gsup_message_type_name(gsup_msg.message_type));
+		break;
+	}
+
+	msgb_free(msg);
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/smsc-daemon/logging.h	Thu Aug 17 22:56:49 2023 -0800
@@ -0,0 +1,4 @@
+/* logging categories */
+enum {
+	DMAIN,
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/smsc-daemon/main.c	Thu Aug 17 22:56:49 2023 -0800
@@ -0,0 +1,126 @@
+/*
+ * This C module is part of proto-smsc-daemon concoction, a prototype/test
+ * implementation of a GSUP-based GSM SMSC.  It is based on the osmo-demo-euse
+ * program from OsmoHLR package.
+ *
+ * proto-smsc-daemon author: Mychaela N. Falconia <falcon@freecalypso.org>,
+ * no copyright.
+ *
+ * osmo-demo-euse author: Harald Welte <laforge@gnumonks.org>, (C) 2018, AGPL3+
+ */
+
+#include <ctype.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <signal.h>
+
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/application.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/logging.h>
+
+#include <osmocom/gsm/gsup.h>
+
+#include <osmocom/gsupclient/gsup_client.h>
+
+#include "logging.h"
+
+extern int gsupc_read_cb(struct osmo_gsup_client *gsupc, struct msgb *msg);
+
+char smsc_addr_num[21];		/* maximum of 20 digits per GSM 04.11 */
+uint8_t smsc_addr_ton_npi;
+FILE *smsc_log_file;
+char smsc_gsup_name[26];
+struct osmo_gsup_client *g_gc;
+
+static struct log_info_cat default_categories[] = {
+	[DMAIN] = {
+		.name = "DMAIN",
+		.description = "Main Program",
+		.enabled = 1, .loglevel = LOGL_DEBUG,
+	},
+};
+
+static const struct log_info smsc_log_info = {
+	.cat = default_categories,
+	.num_cat = ARRAY_SIZE(default_categories),
+};
+
+static void parse_smsc_addr_arg(char *arg)
+{
+	char *cp, *dp, *endp;
+	unsigned ndig;
+
+	cp = arg;
+	dp = smsc_addr_num;
+	ndig = 0;
+	while (isdigit(*cp)) {
+		if (ndig >= 20) {
+			fprintf(stderr,
+	"error: SMSC address argument exceeds GSM 04.11 limit of 20 digits\n");
+			exit(1);
+		}
+		*dp++ = *cp++;
+		ndig++;
+	}
+	if (!ndig) {
+invalid:	fprintf(stderr,
+			"error: SMSC address argument \"%s\" is invalid\n",
+			arg);
+		exit(1);
+	}
+	if (!*cp) {
+		/* default to TON=1, NPI=1, i.e., a pretend Global Title */
+		smsc_addr_ton_npi = 0x91;
+		return;
+	}
+	if (*cp++ != ',')
+		goto invalid;
+	if (!*cp)
+		goto invalid;
+	smsc_addr_ton_npi = strtoul(cp, &endp, 0);
+	if (*endp)
+		goto invalid;
+}
+
+static void open_log_file(char *filename)
+{
+	smsc_log_file = fopen(filename, "a");
+	if (!smsc_log_file) {
+		perror(filename);
+		exit(1);
+	}
+}
+
+int main(int argc, char **argv)
+{
+	char *server_host = "127.0.0.1";
+	uint16_t server_port = OSMO_GSUP_PORT;
+	void *ctx = talloc_named_const(NULL, 0, "proto-smsc");
+
+	osmo_init_logging2(ctx, &smsc_log_info);
+
+	if (argc != 3) {
+		fprintf(stderr, "usage: %s smsc-addr logfile\n", argv[0]);
+		exit(1);
+	}
+	parse_smsc_addr_arg(argv[1]);
+	open_log_file(argv[2]);
+
+	sprintf(smsc_gsup_name, "SMSC-%s", smsc_addr_num);
+	g_gc = osmo_gsup_client_create(ctx, smsc_gsup_name, server_host,
+					server_port, gsupc_read_cb, NULL);
+	if (!g_gc) {
+		fprintf(stderr, "error: osmo_gsup_client_create() failed\n");
+		exit(1);
+	}
+
+	while (1) {
+		osmo_select_main(0);
+	}
+
+	exit(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/smsc-daemon/record_mo.c	Thu Aug 17 22:56:49 2023 -0800
@@ -0,0 +1,96 @@
+/*
+ * This C module is part of proto-smsc-daemon concoction, a prototype/test
+ * implementation of a GSUP-based GSM SMSC.  It is based on the osmo-demo-euse
+ * program from OsmoHLR package.
+ *
+ * proto-smsc-daemon author: Mychaela N. Falconia <falcon@freecalypso.org>,
+ * no copyright.
+ *
+ * osmo-demo-euse author: Harald Welte <laforge@gnumonks.org>, (C) 2018, AGPL3+
+ */
+
+#include <ctype.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <signal.h>
+#include <time.h>
+
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/application.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/logging.h>
+
+#include <osmocom/gsm/gsup.h>
+#include <osmocom/gsm/gsm48_ie.h>
+
+#include <osmocom/gsupclient/gsup_client.h>
+
+#include "logging.h"
+
+extern FILE *smsc_log_file;
+
+static void format_addr(char *ie_name, enum osmo_gsup_sms_sm_rp_oda_t type,
+			size_t len, const uint8_t *data)
+{
+	int skip_byte;
+	uint8_t lv_buf[255];
+	char decode_buf[520];
+
+	fprintf(smsc_log_file, "%s: ", ie_name);
+	switch (type) {
+	case OSMO_GSUP_SMS_SM_RP_ODA_IMSI:
+		fputs("IMSI ", smsc_log_file);
+		skip_byte = 0;
+		break;
+	case OSMO_GSUP_SMS_SM_RP_ODA_MSISDN:
+		fprintf(smsc_log_file, "MSISDN TON=%u NPI=%u ",
+			(data[0] & 0x70) >> 4, data[0] & 0x0F);
+		skip_byte = 1;
+		break;
+	case OSMO_GSUP_SMS_SM_RP_ODA_SMSC_ADDR:
+		fprintf(smsc_log_file, "SMSC TON=%u NPI=%u ",
+			(data[0] & 0x70) >> 4, data[0] & 0x0F);
+		skip_byte = 1;
+		break;
+	case OSMO_GSUP_SMS_SM_RP_ODA_NULL:
+		fputs("NULL\n", smsc_log_file);
+		return;
+	default:
+		fputs("Invalid!\n", smsc_log_file);
+		return;
+	}
+	OSMO_ASSERT(len >= 1 && len <= 254);
+	lv_buf[0] = len - skip_byte;
+	memcpy(lv_buf + 1, data + skip_byte, len - skip_byte);
+	gsm48_decode_bcd_number2(decode_buf, sizeof decode_buf,
+				 lv_buf, sizeof lv_buf, 0);
+	fprintf(smsc_log_file, "%s\n", decode_buf[0] ? decode_buf : "<empty>");
+}
+
+void record_mo_sm(struct osmo_gsup_message *gmsg)
+{
+	time_t curtime;
+	struct tm *tm;
+	const uint8_t *dp, *endp;
+
+	time(&curtime);
+	tm = gmtime(&curtime);
+	fprintf(smsc_log_file, "\n%d-%02d-%02dT%02d:%02d:%02dZ Rx MO SM\n",
+		tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
+		tm->tm_hour, tm->tm_min, tm->tm_sec);
+	fprintf(smsc_log_file, "IMSI: %s\n", gmsg->imsi);
+	fprintf(smsc_log_file, "SM-RP-MR: 0x%02X\n", gmsg->sm_rp_mr);
+	format_addr("SM-RP-DA", gmsg->sm_rp_da_type, gmsg->sm_rp_da_len,
+			gmsg->sm_rp_da);
+	format_addr("SM-RP-OA", gmsg->sm_rp_oa_type, gmsg->sm_rp_oa_len,
+			gmsg->sm_rp_oa);
+	fprintf(smsc_log_file, "SM-RP-UI: %u bytes\n", gmsg->sm_rp_ui_len);
+	dp = gmsg->sm_rp_ui;
+	endp = dp + gmsg->sm_rp_ui_len;
+	while (dp < endp)
+		fprintf(smsc_log_file, "%02X", *dp++);
+	putc('\n', smsc_log_file);
+}