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