comparison smsc-daemon/main.c @ 7:22ddb71f6883

smsc-daemon: convert to setting IPA name on the command line
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 25 Aug 2023 14:47:24 -0800
parents cef4677a4cf8
children d9db8661d9f3
comparison
equal deleted inserted replaced
6:18b7f75fed9b 7:22ddb71f6883
21 #include <osmocom/core/application.h> 21 #include <osmocom/core/application.h>
22 #include <osmocom/core/utils.h> 22 #include <osmocom/core/utils.h>
23 #include <osmocom/core/logging.h> 23 #include <osmocom/core/logging.h>
24 24
25 #include <osmocom/gsm/gsup.h> 25 #include <osmocom/gsm/gsup.h>
26 #include <osmocom/gsm/ipa.h>
26 27
27 #include <osmocom/gsupclient/gsup_client.h> 28 #include <osmocom/gsupclient/gsup_client.h>
28 29
29 #include "logging.h" 30 #include "logging.h"
30 31
31 extern int gsupc_read_cb(struct osmo_gsup_client *gsupc, struct msgb *msg); 32 extern int gsupc_read_cb(struct osmo_gsup_client *gsupc, struct msgb *msg);
32 33
33 char smsc_addr_num[21]; /* maximum of 20 digits per GSM 04.11 */ 34 #define MAX_IPA_NAME 31
34 uint8_t smsc_addr_ton_npi; 35
35 FILE *smsc_log_file; 36 FILE *smsc_log_file;
36 char smsc_gsup_name[26]; 37 char smsc_ipa_name[MAX_IPA_NAME+1];
38 struct ipaccess_unit gsup_ipa_dev;
37 struct osmo_gsup_client *g_gc; 39 struct osmo_gsup_client *g_gc;
38 40
39 static struct log_info_cat default_categories[] = { 41 static struct log_info_cat default_categories[] = {
40 [DMAIN] = { 42 [DMAIN] = {
41 .name = "DMAIN", 43 .name = "DMAIN",
47 static const struct log_info smsc_log_info = { 49 static const struct log_info smsc_log_info = {
48 .cat = default_categories, 50 .cat = default_categories,
49 .num_cat = ARRAY_SIZE(default_categories), 51 .num_cat = ARRAY_SIZE(default_categories),
50 }; 52 };
51 53
52 static void parse_smsc_addr_arg(char *arg) 54 static void set_ipa_name(const char *arg)
53 { 55 {
54 char *cp, *dp, *endp; 56 if (strlen(arg) > MAX_IPA_NAME) {
55 unsigned ndig; 57 fprintf(stderr, "error: IPA name argument is too long\n");
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); 58 exit(1);
74 } 59 }
75 if (!*cp) { 60 strcpy(smsc_ipa_name, arg);
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 } 61 }
88 62
89 static void open_log_file(char *filename) 63 static void open_log_file(const char *filename)
90 { 64 {
91 smsc_log_file = fopen(filename, "a"); 65 smsc_log_file = fopen(filename, "a");
92 if (!smsc_log_file) { 66 if (!smsc_log_file) {
93 perror(filename); 67 perror(filename);
94 exit(1); 68 exit(1);
95 } 69 }
70 }
71
72 static void build_ipa_dev(void)
73 {
74 gsup_ipa_dev.unit_name = "SMSC";
75 gsup_ipa_dev.serno = smsc_ipa_name;
96 } 76 }
97 77
98 int main(int argc, char **argv) 78 int main(int argc, char **argv)
99 { 79 {
100 char *server_host = "127.0.0.1"; 80 char *server_host = "127.0.0.1";
102 void *ctx = talloc_named_const(NULL, 0, "proto-smsc"); 82 void *ctx = talloc_named_const(NULL, 0, "proto-smsc");
103 83
104 osmo_init_logging2(ctx, &smsc_log_info); 84 osmo_init_logging2(ctx, &smsc_log_info);
105 85
106 if (argc != 3) { 86 if (argc != 3) {
107 fprintf(stderr, "usage: %s smsc-addr logfile\n", argv[0]); 87 fprintf(stderr, "usage: %s ipa-name logfile\n", argv[0]);
108 exit(1); 88 exit(1);
109 } 89 }
110 parse_smsc_addr_arg(argv[1]); 90 set_ipa_name(argv[1]);
111 open_log_file(argv[2]); 91 open_log_file(argv[2]);
112 92
113 sprintf(smsc_gsup_name, "SMSC-%s", smsc_addr_num); 93 build_ipa_dev();
114 g_gc = osmo_gsup_client_create(ctx, smsc_gsup_name, server_host, 94 g_gc = osmo_gsup_client_create2(ctx, &gsup_ipa_dev, server_host,
115 server_port, gsupc_read_cb, NULL); 95 server_port, gsupc_read_cb, NULL);
116 if (!g_gc) { 96 if (!g_gc) {
117 fprintf(stderr, "error: osmo_gsup_client_create() failed\n"); 97 fprintf(stderr, "error: osmo_gsup_client_create2() failed\n");
118 exit(1); 98 exit(1);
119 } 99 }
120 100
121 while (1) { 101 while (1) {
122 osmo_select_main(0); 102 osmo_select_main(0);