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