Debian Codegraph @

src/common/trans.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2004 CreepLord ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #define TRANS_INTERNAL_ACCESS
19 #include "trans.h"
20
21 #include <cerrno>
22 #include <cstring>
23
24 #include "common/setup_before.h"
25 #include "common/eventlog.h"
26 #include "common/list.h"
27 #include "common/addr.h"
28 #include "common/util.h"
29 #include "common/xalloc.h"
30 #include "common/setup_after.h"
31
32 #define DEBUG_TRANS
33
34 namespace pvpgn
35 {
36
37 static t_list * trans_head = NULL;
38
39 extern int trans_load(char const * filename, int program)
40 {
41 std::FILE *fp;
42 unsigned int line;
43 unsigned int pos;
44 char *buff;
45 char *temp;
46 char *input;
47 char const *output;
48 char const *exclude;
49 char const *include;
50 unsigned int npos;
51 char *network;
52 char *tmp;
53 char tmp1[32];
54 char tmp2[32];
55 char tmp3[32];
56 t_trans *entry;
57
58 if (!filename) {
59 eventlog(eventlog_level_error, __FUNCTION__, "got NULL filename");
60 return -1;
61 }
62 if (!(fp = std::fopen(filename, "r"))) {
63 eventlog(eventlog_level_error, __FUNCTION__, "could not open file \"{}\" for reading (std::fopen: {})", filename, std::strerror(errno));
64 return -1;
65 }
66 trans_head = list_create();
67 for (line = 1; (buff = file_get_line(fp)); line++) {
68 for (pos = 0; buff[pos] == '\t' || buff[pos] == ' '; pos++);
69 if (buff[pos] == '\0' || buff[pos] == '#') {
70 continue;
71 }
72 if ((temp = std::strrchr(buff, '#'))) {
73 unsigned int len;
74 unsigned int endpos;
75
76 *temp = '\0';
77 len = std::strlen(buff) + 1;
78 for (endpos = len - 1; buff[endpos] == '\t' || buff[endpos] == ' '; endpos--);
79 buff[endpos + 1] = '\0';
80 }
81 if (!(input = std::strtok(buff, " \t"))) { /* std::strtok modifies the string it is passed */
82 eventlog(eventlog_level_error, __FUNCTION__, "missing input line {} of file \"{}\"", line, filename);
83 continue;
84 }
85 /* check for port number - this tells us what programs will use this entry */
86 if (!(temp = std::strrchr(input, ':'))) {
87 eventlog(eventlog_level_error, __FUNCTION__, "missing port # on input line {} of file \"{}\"", line, filename);
88 continue;
89 }
90 temp++;
91 /* bnetd doesn't want the port 4000 entries */
92 if (program == TRANS_BNETD && std::strcmp(temp, "4000") == 0) {
93 #ifdef DEBUG_TRANS
94 eventlog(eventlog_level_debug, __FUNCTION__, "d2gs input (ignoring) \"{}\"", input);
95 #endif
96 continue;
97 }
98 /* d2cs only wants the port 4000 entries */
99 if (program == TRANS_D2CS && std::strcmp(temp, "4000") != 0) {
100 #ifdef DEBUG_TRANS
101 eventlog(eventlog_level_debug, __FUNCTION__, "non d2gs input (ignoring) \"{}\"", input);
102 #endif
103 continue;
104 }
105 if (!(output = std::strtok(NULL, " \t"))) {
106 eventlog(eventlog_level_error, __FUNCTION__, "missing output on line {} of file \"{}\"", line, filename);
107 continue;
108 }
109 if (!(exclude = std::strtok(NULL, " \t"))) {
110 eventlog(eventlog_level_error, __FUNCTION__, "missing exclude on line {} of file \"{}\"", line, filename);
111 continue;
112 }
113 if (!(include = std::strtok(NULL, " \t"))) {
114 eventlog(eventlog_level_error, __FUNCTION__, "missing include on line {} of file \"{}\"", line, filename);
115 continue;
116 }
117 /* add exlude networks */
118 tmp = xstrdup(exclude);
119 npos = 0;
120 while (tmp[npos]) {
121 network = &tmp[npos];
122 for (; tmp[npos] != ',' && tmp[npos] != '\0'; npos++);
123 if (tmp[npos] == '\0')
124 npos--;
125 else
126 tmp[npos] = '\0';
127 if (std::strcmp(network, "NONE") == 0) {
128 npos++;
129 continue;
130 }
131 entry = (t_trans*)xmalloc(sizeof(t_trans));
132 if (!(entry->input = addr_create_str(input, 0, 0))) {
133 eventlog(eventlog_level_error, __FUNCTION__, "could not allocate memory for input address");
134 xfree(entry);
135 npos++;
136 continue;
137 }
138 if (!(entry->output = addr_create_str(input, 0, 0))) {
139 eventlog(eventlog_level_error, __FUNCTION__, "could not allocate memory for output address");
140 addr_destroy(entry->input);
141 xfree(entry);
142 npos++;
143 continue;
144 }
145 if (std::strcmp(network, "ANY") == 0) {
146 if (!(entry->network = netaddr_create_str("0.0.0.0/0"))) {
147 eventlog(eventlog_level_error, __FUNCTION__, "could not allocate memory for network address");
148 addr_destroy(entry->output);
149 addr_destroy(entry->input);
150 xfree(entry);
151 npos++;
152 continue;
153 }
154 }
155 else {
156 if (!(entry->network = netaddr_create_str(network))) {
157 eventlog(eventlog_level_error, __FUNCTION__, "could not allocate memory for network address");
158 addr_destroy(entry->output);
159 addr_destroy(entry->input);
160 xfree(entry);
161 npos++;
162 continue;
163 }
164 }
165 #ifdef DEBUG_TRANS
166 eventlog(eventlog_level_debug, __FUNCTION__,
167 "Adding Host -> {}, Output -> {}, Network {} - (exclude)",
168 addr_get_addr_str(entry->input, tmp1, sizeof(tmp1)),
169 addr_get_addr_str(entry->output, tmp2, sizeof(tmp2)),
170 netaddr_get_addr_str(entry->network, tmp3, sizeof(tmp3)));
171 #endif
172 list_append_data(trans_head, entry);
173 npos++;
174 }
175 xfree(tmp);
176 /* add include networks */
177 tmp = xstrdup(include);
178 npos = 0;
179 while (tmp[npos]) {
180 network = &tmp[npos];
181 for (; tmp[npos] != ',' && tmp[npos] != '\0'; npos++);
182 if (tmp[npos] == '\0')
183 npos--;
184 else
185 tmp[npos] = '\0';
186 if (std::strcmp(network, "NONE") == 0) {
187 npos++;
188 continue;
189 }
190 entry = (t_trans*)xmalloc(sizeof(t_trans));
191 if (!(entry->input = addr_create_str(input, 0, 0))) {
192 eventlog(eventlog_level_error, __FUNCTION__, "could not allocate memory for input address");
193 xfree(entry);
194 npos++;
195 continue;
196 }
197 if (!(entry->output = addr_create_str(output, 0, 0))) {
198 eventlog(eventlog_level_error, __FUNCTION__, "could not allocate memory for output address");
199 addr_destroy(entry->input);
200 xfree(entry);
201 npos++;
202 continue;
203 }
204 if (std::strcmp(network, "ANY") == 0) {
205 if (!(entry->network = netaddr_create_str("0.0.0.0/0"))) {
206 eventlog(eventlog_level_error, __FUNCTION__, "could not allocate memory for network address");
207 addr_destroy(entry->output);
208 addr_destroy(entry->input);
209 xfree(entry);
210 npos++;
211 continue;
212 }
213 }
214 else {
215 if (!(entry->network = netaddr_create_str(network))) {
216 eventlog(eventlog_level_error, __FUNCTION__, "could not allocate memory for network address");
217 addr_destroy(entry->output);
218 addr_destroy(entry->input);
219 xfree(entry);
220 npos++;
221 continue;
222 }
223 }
224 #ifdef DEBUG_TRANS
225 eventlog(eventlog_level_debug, __FUNCTION__,
226 "Adding Host -> {}, Output -> {}, Network {} - (include)",
227 addr_get_addr_str(entry->input, tmp1, sizeof(tmp1)),
228 addr_get_addr_str(entry->output, tmp2, sizeof(tmp2)),
229 netaddr_get_addr_str(entry->network, tmp3, sizeof(tmp3)));
230 #endif
231 list_append_data(trans_head, entry);
232 npos++;
233 }
234 xfree(tmp);
235 }
236 file_get_line(NULL); // clear file_get_line buffer
237 std::fclose(fp);
238 eventlog(eventlog_level_info, __FUNCTION__, "trans file loaded");
239 return 0;
240 }
241
242 extern int trans_unload(void)
243 {
244 t_elem *curr;
245 t_trans *entry;
246
247 if (trans_head) {
248 LIST_TRAVERSE(trans_head, curr)
249 {
250 if (!(entry = (t_trans*)elem_get_data(curr))) {
251 eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list");
252 }
253 else {
254 netaddr_destroy(entry->network);
255 addr_destroy(entry->output);
256 addr_destroy(entry->input);
257 xfree(entry);
258 }
259 list_remove_elem(trans_head, &curr);
260 }
261 list_destroy(trans_head);
262 trans_head = NULL;
263 }
264 return 0;
265 }
266
267 extern int trans_reload(char const * filename, int program)
268 {
269 trans_unload();
270 if (trans_load(filename, program) < 0) return -1;
271 return 0;
272 }
273
274 extern int trans_net(unsigned int clientaddr, unsigned int *addr, unsigned short *port)
275 {
276 t_elem const *curr;
277 t_trans *entry;
278 char temp1[32];
279 char temp2[32];
280 char temp3[32];
281 char temp4[32];
282
283 #ifdef DEBUG_TRANS
284 eventlog(eventlog_level_debug, __FUNCTION__, "checking {} for client {} ...",
285 addr_num_to_addr_str(*addr, *port),
286 addr_num_to_ip_str(clientaddr));
287 #endif
288
289 if (trans_head) {
290 LIST_TRAVERSE_CONST(trans_head, curr)
291 {
292 if (!(entry = (t_trans*)elem_get_data(curr))) {
293 eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list");
294 continue;
295 }
296
297 #ifdef DEBUG_TRANS
298 eventlog(eventlog_level_debug, __FUNCTION__, "against entry -> {} output {} network {}",
299 addr_get_addr_str(entry->input, temp1, sizeof(temp1)),
300 addr_get_addr_str(entry->output, temp2, sizeof(temp2)),
301 netaddr_get_addr_str(entry->network, temp3, sizeof(temp3)));
302 #endif
303 if (addr_get_ip(entry->input) != *addr || addr_get_port(entry->input) != *port) {
304 #ifdef DEBUG_TRANS
305 eventlog(eventlog_level_debug, __FUNCTION__, "entry does match input address");
306 #endif
307 continue;
308 }
309 if (netaddr_contains_addr_num(entry->network, clientaddr) == 0) {
310 #ifdef DEBUG_TRANS
311 eventlog(eventlog_level_debug, __FUNCTION__, "client is not in the correct network");
312 #endif
313 continue;
314 }
315 #ifdef DEBUG_TRANS
316 eventlog(eventlog_level_debug, __FUNCTION__, "{} translated to {}",
317 addr_num_to_addr_str(*addr, *port),
318 addr_get_addr_str(entry->output, temp4, sizeof(temp4)));
319 #endif
320 *addr = addr_get_ip(entry->output);
321 *port = addr_get_port(entry->output);
322 return 1; /* match found in list */
323 }
324 }
325 #ifdef DEBUG_TRANS
326 eventlog(eventlog_level_debug, __FUNCTION__, "no match found for {} (not translated)",
327 addr_num_to_addr_str(*addr, *port));
328 #endif
329 return 0; /* no match found in list */
330 }
331
332 }