view smsc-daemon/main.c @ 6:18b7f75fed9b

smsc-sendmt/smsc_addr.c: temporary code stashaway
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 25 Aug 2023 14:28:54 -0800
parents cef4677a4cf8
children 22ddb71f6883
line wrap: on
line source

/*
 * 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-euse-demo
 * program from OsmoHLR package.
 *
 * proto-smsc-daemon author: Mychaela N. Falconia <falcon@freecalypso.org>,
 * no copyright.
 *
 * osmo-euse-demo 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);
}