comparison smsc-daemon/main.c @ 3:8680979baeb1

smsc-daemon: first version put together
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 17 Aug 2023 22:56:49 -0800
parents
children cef4677a4cf8
comparison
equal deleted inserted replaced
2:2067c55e2c79 3:8680979baeb1
1 /*
2 * This C module is part of proto-smsc-daemon concoction, a prototype/test
3 * implementation of a GSUP-based GSM SMSC. It is based on the osmo-demo-euse
4 * program from OsmoHLR package.
5 *
6 * proto-smsc-daemon author: Mychaela N. Falconia <falcon@freecalypso.org>,
7 * no copyright.
8 *
9 * osmo-demo-euse author: Harald Welte <laforge@gnumonks.org>, (C) 2018, AGPL3+
10 */
11
12 #include <ctype.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <signal.h>
18
19 #include <osmocom/core/msgb.h>
20 #include <osmocom/core/select.h>
21 #include <osmocom/core/application.h>
22 #include <osmocom/core/utils.h>
23 #include <osmocom/core/logging.h>
24
25 #include <osmocom/gsm/gsup.h>
26
27 #include <osmocom/gsupclient/gsup_client.h>
28
29 #include "logging.h"
30
31 extern int gsupc_read_cb(struct osmo_gsup_client *gsupc, struct msgb *msg);
32
33 char smsc_addr_num[21]; /* maximum of 20 digits per GSM 04.11 */
34 uint8_t smsc_addr_ton_npi;
35 FILE *smsc_log_file;
36 char smsc_gsup_name[26];
37 struct osmo_gsup_client *g_gc;
38
39 static struct log_info_cat default_categories[] = {
40 [DMAIN] = {
41 .name = "DMAIN",
42 .description = "Main Program",
43 .enabled = 1, .loglevel = LOGL_DEBUG,
44 },
45 };
46
47 static const struct log_info smsc_log_info = {
48 .cat = default_categories,
49 .num_cat = ARRAY_SIZE(default_categories),
50 };
51
52 static void parse_smsc_addr_arg(char *arg)
53 {
54 char *cp, *dp, *endp;
55 unsigned ndig;
56
57 cp = arg;
58 dp = smsc_addr_num;
59 ndig = 0;
60 while (isdigit(*cp)) {
61 if (ndig >= 20) {
62 fprintf(stderr,
63 "error: SMSC address argument exceeds GSM 04.11 limit of 20 digits\n");
64 exit(1);
65 }
66 *dp++ = *cp++;
67 ndig++;
68 }
69 if (!ndig) {
70 invalid: fprintf(stderr,
71 "error: SMSC address argument \"%s\" is invalid\n",
72 arg);
73 exit(1);
74 }
75 if (!*cp) {
76 /* default to TON=1, NPI=1, i.e., a pretend Global Title */
77 smsc_addr_ton_npi = 0x91;
78 return;
79 }
80 if (*cp++ != ',')
81 goto invalid;
82 if (!*cp)
83 goto invalid;
84 smsc_addr_ton_npi = strtoul(cp, &endp, 0);
85 if (*endp)
86 goto invalid;
87 }
88
89 static void open_log_file(char *filename)
90 {
91 smsc_log_file = fopen(filename, "a");
92 if (!smsc_log_file) {
93 perror(filename);
94 exit(1);
95 }
96 }
97
98 int main(int argc, char **argv)
99 {
100 char *server_host = "127.0.0.1";
101 uint16_t server_port = OSMO_GSUP_PORT;
102 void *ctx = talloc_named_const(NULL, 0, "proto-smsc");
103
104 osmo_init_logging2(ctx, &smsc_log_info);
105
106 if (argc != 3) {
107 fprintf(stderr, "usage: %s smsc-addr logfile\n", argv[0]);
108 exit(1);
109 }
110 parse_smsc_addr_arg(argv[1]);
111 open_log_file(argv[2]);
112
113 sprintf(smsc_gsup_name, "SMSC-%s", smsc_addr_num);
114 g_gc = osmo_gsup_client_create(ctx, smsc_gsup_name, server_host,
115 server_port, gsupc_read_cb, NULL);
116 if (!g_gc) {
117 fprintf(stderr, "error: osmo_gsup_client_create() failed\n");
118 exit(1);
119 }
120
121 while (1) {
122 osmo_select_main(0);
123 }
124
125 exit(0);
126 }