comparison sip-in/readconf.c @ 47:62f39c7cee15

themwi-sip-in skeleton started
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 06 Sep 2022 20:33:56 -0800
parents
children 709b78a4ebf0
comparison
equal deleted inserted replaced
46:5427b26525cd 47:62f39c7cee15
1 /*
2 * In this module we implement the reading of /var/gsm/themwi-sip-in.cfg:
3 * the main settings are bind-ip and bind-port, but we also have some
4 * optional config settings.
5 */
6
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <strings.h>
16
17 struct in_addr sip_bind_ip;
18 unsigned sip_bind_port;
19 int cfg_use_100rel;
20
21 static char config_file_pathname[] = "/var/gsm/themwi-sip-in.cfg";
22
23 struct parse_state {
24 int lineno;
25 int set_mask;
26 };
27
28 static void
29 handle_ip(st, kw, var, arg)
30 struct parse_state *st;
31 char *kw, *arg;
32 struct in_addr *var;
33 {
34 var->s_addr = inet_addr(arg);
35 if (var->s_addr == INADDR_NONE) {
36 fprintf(stderr,
37 "%s line %d: invalid IP address argument \"%s\"\n",
38 config_file_pathname, st->lineno, arg);
39 exit(1);
40 }
41 }
42
43 static void
44 handle_num(st, kw, var, arg)
45 struct parse_state *st;
46 char *kw, *arg;
47 unsigned *var;
48 {
49 char *endp;
50
51 *var = strtoul(arg, &endp, 10);
52 if (*endp) {
53 fprintf(stderr, "%s line %d: invalid numeric argument \"%s\"\n",
54 config_file_pathname, st->lineno, arg);
55 exit(1);
56 }
57 }
58
59 static void
60 handle_bool(st, kw, var, arg)
61 struct parse_state *st;
62 char *kw, *arg;
63 int *var;
64 {
65 if (!strcmp(arg, "true") || !strcmp(arg, "on") || !strcmp(arg, "yes")
66 || !strcmp(arg, "1")) {
67 *var = 1;
68 return;
69 }
70 if (!strcmp(arg, "false") || !strcmp(arg, "off") || !strcmp(arg, "no")
71 || !strcmp(arg, "0")) {
72 *var = 0;
73 return;
74 }
75 fprintf(stderr, "%s line %d: invalid boolean argument \"%s\"\n",
76 config_file_pathname, st->lineno, arg);
77 exit(1);
78 }
79
80 static void
81 process_line(st, line)
82 struct parse_state *st;
83 char *line;
84 {
85 char *cp, *np, *arg;
86 void (*handler)(), *var;
87 int set_id;
88
89 if (!index(line, '\n')) {
90 fprintf(stderr, "%s line %d: too long or missing newline\n",
91 config_file_pathname, st->lineno);
92 exit(1);
93 }
94 for (cp = line; isspace(*cp); cp++)
95 ;
96 if (*cp == '\0' || *cp == '#')
97 return;
98 for (np = cp; *cp && !isspace(*cp); cp++)
99 ;
100 if (*cp)
101 *cp++ = '\0';
102 if (!strcmp(np, "bind-ip")) {
103 handler = handle_ip;
104 var = &sip_bind_ip;
105 set_id = 1;
106 } else if (!strcmp(np, "bind-port")) {
107 handler = handle_num;
108 var = &sip_bind_port;
109 set_id = 2;
110 } else if (!strcmp(np, "use-100rel")) {
111 handler = handle_bool;
112 var = &cfg_use_100rel;
113 set_id = 0;
114 } else {
115 fprintf(stderr, "%s line %d: non-understood keyword \"%s\"\n",
116 config_file_pathname, st->lineno, np);
117 exit(1);
118 }
119 if (st->set_mask & set_id) {
120 fprintf(stderr, "%s line %d: duplicate %s setting\n",
121 config_file_pathname, st->lineno, np);
122 exit(1);
123 }
124 while (isspace(*cp))
125 cp++;
126 if (*cp == '\0' || *cp == '#') {
127 inv_syntax: fprintf(stderr,
128 "%s line %d: %s setting requires one argument\n",
129 config_file_pathname, st->lineno, np);
130 exit(1);
131 }
132 for (arg = cp; *cp && !isspace(*cp); cp++)
133 ;
134 if (*cp)
135 *cp++ = '\0';
136 while (isspace(*cp))
137 cp++;
138 if (*cp != '\0' && *cp != '#')
139 goto inv_syntax;
140 handler(st, np, var, arg);
141 st->set_mask |= set_id;
142 }
143
144 read_config_file()
145 {
146 FILE *inf;
147 struct parse_state pst;
148 char linebuf[256];
149
150 inf = fopen(config_file_pathname, "r");
151 if (!inf) {
152 perror(config_file_pathname);
153 exit(1);
154 }
155 pst.set_mask = 0;
156 for (pst.lineno = 1; fgets(linebuf, sizeof linebuf, inf); pst.lineno++)
157 process_line(&pst, linebuf);
158 fclose(inf);
159 if (pst.set_mask != 3) {
160 fprintf(stderr, "error: %s did not set all required settings\n",
161 config_file_pathname);
162 exit(1);
163 }
164 }