1 /*****************************************************************************
2  *  begin                : Fri Jul 13 2001                                   *
3  *  copyright            : (C) 2001 by Hans Marcus Kruger                    *
4  *  email                : hanskruger@iname.com                              *
5  ****************************************************************************/
6 
7 /*****************************************************************************
8  *  This program is free software; you can redistribute it and/or modify     *
9  *  it under the terms of the GNU General Public License as published by     *
10  *  the Free Software Foundation; either version 2 of the License, or        *
11  *  (at your option) any later version.                                      *
12  *                                                                           *
13  ****************************************************************************/
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 //#include <sys/types.h>
18 //#include <string.h>
19 //#include <pcap.h>
20 //#include <signal.h>
21 //#include <limits.h>
22 //#include <unistd.h>
23 //#include <fcntl.h>
24 //#include <time.h>
25 //#include <netinet/in.h>
26 //#include "readconfig.h"
27 #include "ip_table.h"
28 //void cipa(unsigned int ip, unsigned char cip[]);
29 
30 //#define DEBUG(s) printf("traff(%d): ",getpid()); s;
31 #define DEBUG(s)
32 
33 //----------------------------------------------------------------------------------
ip_table_destroy_table(t_table * table)34 int ip_table_destroy_table(t_table * table) {
35   t_IP_MAG * root;
36   unsigned char cip[4];
37   int i0,i1,i2,i3;
38   t_IP_MAG *l1, *l2,*l3;
39 
40   root = table->table;
41   cipa(table->next, cip);
42 
43 
44   for (i0 = cip[0]; i0 < 256; i0++) {
45     if ((*root)[i0] == 0) continue;
46     l1 = (*root)[i0];
47     for (i1 = cip[1]; i1 < 256; i1++) {
48       if ((*l1)[i1] == 0) continue;
49       l2 = (*l1)[i1];
50       for (i2 = cip[2]; i2 < 256; i2++) {
51         if ((*l2)[i2] == 0) continue;
52         l3 = (*l2)[i2];
53 	for (i3 = cip[3]; i3 < 256; i3++) {
54 	  if ((*l3)[i3] == 0) continue;
55 	  free((*l3)[i3]);
56 	}
57 	cip[3] = 0;
58 	free(l3);
59       }
60       cip[2] = 0;
61       free(l2);
62     }
63     cip[3] = 0;
64     free(l1);
65   }
66   free(table->table);
67   free(table);
68   return 0;
69 }
70 //----------------------------------------------------------------------------------
ip_table_init()71 t_table * ip_table_init() {
72   t_table * table;
73   table = malloc(sizeof(t_table));
74   table->table = malloc(sizeof(t_IP_MAG));
75   ip_table_init_mag(table->table);
76   table->next = 0;
77   return table;
78 }
79 //----------------------------------------------------------------------------------
ip_table_fetch_next(t_table * table)80 void * ip_table_fetch_next(t_table * table) {
81   t_IP_MAG * root;
82   unsigned char cip[4];
83   int i0,i1,i2,i3;
84   t_IP_MAG *l1, *l2,*l3;
85 
86 
87   root = table->table;
88   cipa(table->next, cip);
89   i0 = cip[0]; i1 = cip[1]; i2 = cip[2]; i3 = cip[3];
90 
91   for (i0 = cip[0]; i0 < 256; i0++) {
92     DEBUG(printf("fetchnext: level: 1 i0: %3d i1: %3d, i2: %3d i3: %3d\n", i0, i1, i2, i3);)
93     if ((*root)[i0] == 0) continue;
94     l1 = (*root)[i0];
95     for (i1 = cip[1]; i1 < 256; i1++ ) {
96       DEBUG(printf("fetchnext: level: 2 i0: %3d i1: %3d, i2: %3d i3: %3d\n", i0, i1, i2, i3);)
97       if ((*l1)[i1] == 0) continue;
98       l2 = (*l1)[i1];
99       for (i2 = cip[2]; i2 < 256; i2++) {
100         DEBUG(printf("fetchnext: level: 3 i0: %3d i1: %3d, i2: %3d i3: %3d\n", i0, i1, i2, i3);)
101         if ((*l2)[i2] == 0) continue;
102         l3 = (*l2)[i2];
103 	for (i3 = cip[3]; i3 < 256; i3++) {
104 	  DEBUG(printf("fetchnext: level: 4 i0: %3d i1: %3d, i2: %3d i3: %3d\n", i0, i1, i2, i3);)
105           if ((*l3)[i3] == 0) continue;
106 	  table->next = (i0 << 24) + (i1 << 16) + (i2 << 8) + i3 + 1;
107 	  return (*l3)[i3];
108 	}
109 	cip[3] = 0;
110       }
111       cip[2] = 0;
112       cip[3] = 0;
113     }
114     cip[1] = 0;
115     cip[2] = 0;
116     cip[3] = 0;
117   }
118   table->next = 0;
119   return 0;
120 }
121 //----------------------------------------------------------------------------------
ip_table_insert(t_table * table,unsigned int ip,void * pointer)122 int ip_table_insert(t_table * table, unsigned int ip, void * pointer) {
123   t_IP_MAG * root;
124 // This function will create all necessary IP-mags, if necessary, and will include the pointer on its possition
125   t_IP_MAG * mag;
126   unsigned char uip[4];
127   t_IP_MAG * new_mag = 0;
128   int i;
129 
130   root = table->table;
131   mag = root;
132 
133   cipa(ip, uip); // Convert ip to 4 chars
134 
135 //  printf("Adding: %d.%d.%d.%d\n",uip[0],uip[1],uip[2],uip[3]);
136 
137   if (!mag) return 0;
138 
139   for (i = 0; i < LEVEL; i++) {
140   // Let's cycle through the levels. If one needed level does not exists it must be created.
141   //   printf("\nLevel: %d  uip.cip[%d]: %d  (*mag)[uip.cip[i]]: %x -->",i ,i, uip[i],(*mag)[uip[i]]);
142     if ( (!(*mag)[uip[i]]) && (i < LEVEL-1) ) {
143       // a new level has to be created
144       new_mag = (t_IP_MAG*) malloc(sizeof(t_IP_MAG));
145       ip_table_init_mag(new_mag);
146       (*mag)[uip[i]] = new_mag;
147 //    printf("%x",(*mag)[uip[i]]);
148       mag = (t_IP_MAG*) new_mag;
149       new_mag=0;
150     } else if ( ((*mag)[uip[i]]) && (i < LEVEL-1)) {
151       // there is another mag under it....
152       mag = (t_IP_MAG*) (*mag)[uip[i]];
153     } else if( (!(*mag)[uip[i]]) && (i == (LEVEL-1))) {
154       // insert pointer
155       (*mag)[uip[i]] = pointer;
156 //    printf("(%x)\n",pointer);
157       return 1;
158     } else {
159       // mag for this level does exist, but there is already a value stored!
160       //but we will allow it to set the pointer to zero. The other programm has free the pointer before!
161       if (! pointer) {      // Overwrite only if supplied pointer is NULL!!!
162         (*mag)[uip[i]] = 0;
163         return 1;
164       }
165       return 0;
166     } // else if
167   } // for
168 
169   return 0;
170 }
171 //----------------------------------------------------------------------------------
ip_table_get_entry(t_table * table,unsigned int ip)172 void * ip_table_get_entry(t_table * table, unsigned int ip) {
173   t_IP_MAG * root;
174   t_IP_MAG * mag;
175 
176   unsigned char uip[4];
177   int i;
178 
179   root = table->table;
180   mag = root;
181 
182   if (!root) {
183 		return NULL;
184 	}
185 
186   cipa(ip, uip);
187   for (i = 0; i < LEVEL; i++) {
188 //    printf("cip[%d] = %d\n",i, uip[i]);
189     if ( !(*mag)[ uip[i] ]) {
190       // some of the mags or entry does not exist
191       return NULL;
192     } else if (i < (LEVEL-1)) {
193       // go down another level
194       mag = (t_IP_MAG*) (*mag)[uip[i]];
195     } else if (i == (LEVEL-1)) {
196       // That's it....
197       return (*mag)[uip[i]];
198     }
199   }
200   return NULL;
201 }
202 //--------------------------------------------------------------------------------------
ip_table_count(t_table * table)203 int ip_table_count(t_table * table) {
204   t_IP_MAG * root;
205   root = table->table;
206   return ip_table_count_in_mag((t_IP_MAG *)root, 0);
207 }
208 //--------------------------------------------------------------------------------------
ip_table_count_in_mag(t_IP_MAG * mag,int level)209 int ip_table_count_in_mag(t_IP_MAG * mag, int level) {
210   int count;
211   int i;
212 
213   count = 0;
214   if (level >= LEVEL) { return 0; }
215   if (mag == 0) { return 0; }
216 
217   for (i = 0; i < IP_MAG_MAX; i++) {
218     if ( (level == (LEVEL-1)) && (*mag)[i] ) {
219       // found one!
220 // //     printf("Found entry %x in level %d, entry %d\n",(*mag)[i], level, i);
221       count++;
222     } else if ( (*mag)[i] && (level < (LEVEL-1)) ) {
223 //      printf("Entring in %x in level %d, entry %d\n",(*mag)[i], level, i);
224         count += ip_table_count_in_mag((t_IP_MAG *)(*mag)[i], level+1);
225     }
226   }
227   return count;
228 }
229 //--------------------------------------------------------------------------------------
ip_table_init_mag(t_IP_MAG * mag)230 void ip_table_init_mag ( t_IP_MAG* mag ) {
231   int i;
232   for (i = 0; i < IP_MAG_MAX; i++ ) {
233     (*mag)[i] = NULL;
234   }
235 }
236 //----------------------------------------------------------------------------------
237 /*void cipa(unsigned int ip, unsigned char cip[]) {
238   cip[0] = ip>>24;
239   cip[1] = ((ip<<8)>>24);
240   cip[2] = ((ip<<16)>>24);
241   cip[3] = ((ip<<24)>>24);
242   }*/
243 
244