/[LeafOK_CVS]/pvpgn-1.7.4/src/common/trans.c
ViewVC logotype

Annotation of /pvpgn-1.7.4/src/common/trans.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Tue Jun 6 03:41:38 2006 UTC (19 years, 9 months ago) by sysadm
CVS Tags: pvpgn_1-7-4-0_MIL
Branch point for: GNU, MAIN
Content type: text/x-csrc
Initial revision

1 sysadm 1.1 /*
2     * Copyright (C) 2004 CreepLord (creeplord@pvpgn.org)
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 "common/setup_before.h"
20     #include <stdio.h>
21     #ifdef HAVE_STDDEF_H
22     # include <stddef.h>
23     #else
24     # ifndef NULL
25     # define NULL ((void *)0)
26     # endif
27     #endif
28     #ifdef STDC_HEADERS
29     # include <stdlib.h>
30     #else
31     # ifdef HAVE_MALLOC_H
32     # include <malloc.h>
33     # endif
34     #endif
35     #ifdef HAVE_STRING_H
36     # include <string.h>
37     #else
38     # ifdef HAVE_STRINGS_H
39     # include <strings.h>
40     # endif
41     #endif
42     #include "compat/strrchr.h"
43     #include <errno.h>
44     #include "compat/strerror.h"
45     #include "common/eventlog.h"
46     #include "common/list.h"
47     #include "common/addr.h"
48     #include "common/util.h"
49     #include "common/xalloc.h"
50     #include "trans.h"
51     #include "common/setup_after.h"
52    
53     #define DEBUG_TRANS
54    
55     static t_list * trans_head=NULL;
56    
57     extern int trans_load(char const * filename, int program)
58     {
59     FILE *fp;
60     unsigned int line;
61     unsigned int pos;
62     char *buff;
63     char *temp;
64     char const *input;
65     char const *output;
66     char const *exclude;
67     char const *include;
68     unsigned int npos;
69     char *network;
70     char *tmp;
71     char tmp1[32];
72     char tmp2[32];
73     char tmp3[32];
74     t_trans *entry;
75    
76     if (!filename) {
77     eventlog(eventlog_level_error,__FUNCTION__,"got NULL filename");
78     return -1;
79     }
80     if (!(fp = fopen(filename,"r"))) {
81     eventlog(eventlog_level_error,__FUNCTION__,"could not open file \"%s\" for reading (fopen: %s)",filename,pstrerror(errno));
82     return -1;
83     }
84     trans_head = list_create();
85     for (line=1; (buff = file_get_line(fp)); line++) {
86     for (pos=0; buff[pos]=='\t' || buff[pos]==' '; pos++);
87     if (buff[pos]=='\0' || buff[pos]=='#') {
88     continue;
89     }
90     if ((temp = strrchr(buff,'#'))) {
91     unsigned int len;
92     unsigned int endpos;
93    
94     *temp = '\0';
95     len = strlen(buff)+1;
96     for (endpos=len-1; buff[endpos]=='\t' || buff[endpos]==' '; endpos--);
97     buff[endpos+1] = '\0';
98     }
99     if (!(input = strtok(buff," \t"))) { /* strtok modifies the string it is passed */
100     eventlog(eventlog_level_error,__FUNCTION__,"missing input line %u of file \"%s\"",line,filename);
101     continue;
102     }
103     /* check for port number - this tells us what programs will use this entry */
104     if (!(temp = strrchr(input,':'))) {
105     eventlog(eventlog_level_error,__FUNCTION__,"missing port # on input line %u of file \"%s\"",line,filename);
106     continue;
107     }
108     temp++;
109     /* bnetd doesn't want the port 4000 entries */
110     if (program==TRANS_BNETD && strcmp(temp,"4000")==0) {
111     #ifdef DEBUG_TRANS
112     eventlog(eventlog_level_debug,__FUNCTION__,"d2gs input (ignoring) \"%s\"",input);
113     #endif
114     continue;
115     }
116     /* d2cs only wants the port 4000 entries */
117     if (program==TRANS_D2CS && strcmp(temp,"4000")!=0) {
118     #ifdef DEBUG_TRANS
119     eventlog(eventlog_level_debug,__FUNCTION__,"non d2gs input (ignoring) \"%s\"",input);
120     #endif
121     continue;
122     }
123     if (!(output = strtok(NULL," \t"))) {
124     eventlog(eventlog_level_error,__FUNCTION__,"missing output on line %u of file \"%s\"",line,filename);
125     continue;
126     }
127     if (!(exclude = strtok(NULL," \t"))) {
128     eventlog(eventlog_level_error,__FUNCTION__,"missing exclude on line %u of file \"%s\"",line,filename);
129     continue;
130     }
131     if (!(include = strtok(NULL," \t"))) {
132     eventlog(eventlog_level_error,__FUNCTION__,"missing include on line %u of file \"%s\"",line,filename);
133     continue;
134     }
135     /* add exlude networks */
136     tmp = xstrdup(exclude);
137     npos=0;
138     while (tmp[npos]) {
139     network = &tmp[npos];
140     for (; tmp[npos]!=',' && tmp[npos]!='\0'; npos++);
141     if (tmp[npos]=='\0')
142     npos--;
143     else
144     tmp[npos]='\0';
145     if (strcmp(network,"NONE")==0) {
146     npos++;
147     continue;
148     }
149     entry = xmalloc(sizeof(t_trans));
150     if (!(entry->input = addr_create_str(input,0,0))) {
151     eventlog(eventlog_level_error,__FUNCTION__,"could not allocate memory for input address");
152     xfree(entry);
153     npos++;
154     continue;
155     }
156     if (!(entry->output = addr_create_str(input,0,0))) {
157     eventlog(eventlog_level_error,__FUNCTION__,"could not allocate memory for output address");
158     addr_destroy(entry->input);
159     xfree(entry);
160     npos++;
161     continue;
162     }
163     if (strcmp(network,"ANY")==0) {
164     if (!(entry->network = netaddr_create_str("0.0.0.0/0"))) {
165     eventlog(eventlog_level_error,__FUNCTION__,"could not allocate memory for network address");
166     addr_destroy(entry->output);
167     addr_destroy(entry->input);
168     xfree(entry);
169     npos++;
170     continue;
171     }
172     } else {
173     if (!(entry->network = netaddr_create_str(network))) {
174     eventlog(eventlog_level_error,__FUNCTION__,"could not allocate memory for network address");
175     addr_destroy(entry->output);
176     addr_destroy(entry->input);
177     xfree(entry);
178     npos++;
179     continue;
180     }
181     }
182     #ifdef DEBUG_TRANS
183     eventlog(eventlog_level_debug,__FUNCTION__,
184     "Adding Host -> %s, Output -> %s, Network %s - (exclude)",
185     addr_get_addr_str(entry->input,tmp1,sizeof(tmp1)),
186     addr_get_addr_str(entry->output,tmp2,sizeof(tmp2)),
187     netaddr_get_addr_str(entry->network,tmp3,sizeof(tmp3)));
188     #endif
189     list_append_data(trans_head,entry);
190     npos++;
191     }
192     xfree(tmp);
193     /* add include networks */
194     tmp = xstrdup(include);
195     npos=0;
196     while (tmp[npos]) {
197     network = &tmp[npos];
198     for (; tmp[npos]!=',' && tmp[npos]!='\0'; npos++);
199     if (tmp[npos]=='\0')
200     npos--;
201     else
202     tmp[npos]='\0';
203     if (strcmp(network,"NONE")==0) {
204     npos++;
205     continue;
206     }
207     entry = xmalloc(sizeof(t_trans));
208     if (!(entry->input = addr_create_str(input,0,0))) {
209     eventlog(eventlog_level_error,__FUNCTION__,"could not allocate memory for input address");
210     xfree(entry);
211     npos++;
212     continue;
213     }
214     if (!(entry->output = addr_create_str(output,0,0))) {
215     eventlog(eventlog_level_error,__FUNCTION__,"could not allocate memory for output address");
216     addr_destroy(entry->input);
217     xfree(entry);
218     npos++;
219     continue;
220     }
221     if (strcmp(network,"ANY")==0) {
222     if (!(entry->network = netaddr_create_str("0.0.0.0/0"))) {
223     eventlog(eventlog_level_error,__FUNCTION__,"could not allocate memory for network address");
224     addr_destroy(entry->output);
225     addr_destroy(entry->input);
226     xfree(entry);
227     npos++;
228     continue;
229     }
230     } else {
231     if (!(entry->network = netaddr_create_str(network))) {
232     eventlog(eventlog_level_error,__FUNCTION__,"could not allocate memory for network address");
233     addr_destroy(entry->output);
234     addr_destroy(entry->input);
235     xfree(entry);
236     npos++;
237     continue;
238     }
239     }
240     #ifdef DEBUG_TRANS
241     eventlog(eventlog_level_debug,__FUNCTION__,
242     "Adding Host -> %s, Output -> %s, Network %s - (include)",
243     addr_get_addr_str(entry->input,tmp1,sizeof(tmp1)),
244     addr_get_addr_str(entry->output,tmp2,sizeof(tmp2)),
245     netaddr_get_addr_str(entry->network,tmp3,sizeof(tmp3)));
246     #endif
247     list_append_data(trans_head,entry);
248     npos++;
249     }
250     xfree(tmp);
251     }
252     file_get_line(NULL); // clear file_get_line buffer
253     fclose(fp);
254     eventlog(eventlog_level_info,__FUNCTION__,"trans file loaded");
255     return 0;
256     }
257    
258     extern int trans_unload(void)
259     {
260     t_elem *curr;
261     t_trans *entry;
262    
263     if (trans_head) {
264     LIST_TRAVERSE(trans_head,curr)
265     {
266     if (!(entry = elem_get_data(curr))) {
267     eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
268     } else {
269     netaddr_destroy(entry->network);
270     addr_destroy(entry->output);
271     addr_destroy(entry->input);
272     xfree(entry);
273     }
274     list_remove_elem(trans_head,&curr);
275     }
276     list_destroy(trans_head);
277     trans_head = NULL;
278     }
279     return 0;
280     }
281    
282     extern int trans_reload(char const * filename, int program)
283     {
284     trans_unload();
285     if(trans_load(filename,program)<0) return -1;
286     return 0;
287     }
288    
289     extern int trans_net(unsigned int clientaddr, unsigned int *addr, unsigned short *port)
290     {
291     t_elem const *curr;
292     t_trans *entry;
293     char temp1[32];
294     char temp2[32];
295     char temp3[32];
296     char temp4[32];
297    
298     #ifdef DEBUG_TRANS
299     eventlog(eventlog_level_debug,__FUNCTION__,"checking %s for client %s ...",
300     addr_num_to_addr_str(*addr, *port),
301     addr_num_to_ip_str(clientaddr));
302     #endif
303    
304     if (trans_head) {
305     LIST_TRAVERSE_CONST(trans_head,curr)
306     {
307     if (!(entry = elem_get_data(curr))) {
308     eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
309     continue;
310     }
311    
312     #ifdef DEBUG_TRANS
313     eventlog(eventlog_level_debug,__FUNCTION__,"against entry -> %s output %s network %s",
314     addr_get_addr_str(entry->input,temp1,sizeof(temp1)),
315     addr_get_addr_str(entry->output,temp2,sizeof(temp2)),
316     netaddr_get_addr_str(entry->network,temp3,sizeof(temp3)));
317     #endif
318     if (addr_get_ip(entry->input)!=*addr || addr_get_port(entry->input)!=*port) {
319     #ifdef DEBUG_TRANS
320     eventlog(eventlog_level_debug,__FUNCTION__,"entry does match input address");
321     #endif
322     continue;
323     }
324     if (netaddr_contains_addr_num(entry->network,clientaddr)==0) {
325     #ifdef DEBUG_TRANS
326     eventlog(eventlog_level_debug,__FUNCTION__,"client is not in the correct network");
327     #endif
328     continue;
329     }
330     #ifdef DEBUG_TRANS
331     eventlog(eventlog_level_debug,__FUNCTION__,"%s translated to %s",
332     addr_num_to_addr_str(*addr, *port),
333     addr_get_addr_str(entry->output,temp4,sizeof(temp4)));
334     #endif
335     *addr = addr_get_ip(entry->output);
336     *port = addr_get_port(entry->output);
337     return 1; /* match found in list */
338     }
339     }
340     #ifdef DEBUG_TRANS
341     eventlog(eventlog_level_debug,__FUNCTION__,"no match found for %s (not translated)",
342     addr_num_to_addr_str(*addr, *port));
343     #endif
344     return 0; /* no match found in list */
345     }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1