Branch data Line data Source code
1 : : // $Id: Active.cc 1282 2005-09-07 17:02:02Z vern $
2 : :
3 : : #include "config.h"
4 : :
5 : : #include <sys/types.h>
6 : : #include <sys/socket.h>
7 : :
8 : : #include <netinet/in.h>
9 : :
10 : : #include <arpa/inet.h>
11 : :
12 : : #include <map>
13 : :
14 : : #include "Active.h"
15 : : #include "util.h"
16 : : #include "Dict.h"
17 : :
18 [ # # ][ - + ]: 18 : declare(PDict,string);
19 : : typedef PDict(string) MachineMap;
20 [ # # ][ - + ]: 6 : declare(PDict,MachineMap);
21 : : typedef PDict(MachineMap) ActiveMap;
22 [ # # ][ - + ]: 6 : declare(PDict,NumericData);
23 : : typedef PDict(NumericData) ActiveMapNumeric;
24 : :
25 : 6 : static ActiveMap active_map;
26 : 6 : static ActiveMapNumeric active_map_numeric;
27 : :
28 : 6 : static MachineMap default_values;
29 : : static NumericData default_values_numeric;
30 : :
31 : : static bool map_was_loaded = false;
32 : :
33 : 0 : bool get_map_result(uint32 ip_addr, const char* key, string& result)
34 : : {
35 : : const MachineMap* machine_map;
36 : : #ifndef ACTIVE_MAPPING
37 : 0 : machine_map = &default_values;
38 : : #else
39 : : HashKey machinekey(ip_addr);
40 : : machine_map = active_map.Lookup(&machinekey);
41 : :
42 : : if ( ! machine_map )
43 : : machine_map = &default_values;
44 : : #endif
45 : 0 : HashKey mapkey(key);
46 : 0 : string* entry = machine_map->Lookup(&mapkey);
47 [ # # ]: 0 : if ( ! entry )
48 : : {
49 : 0 : entry = default_values.Lookup(&mapkey);
50 : : }
51 : :
52 [ # # ]: 0 : if ( ! entry )
53 : : {
54 : 0 : internal_error("Unknown active mapping entry requested: %s", key);
55 : : return false;
56 : : }
57 : :
58 : 0 : result = *entry;
59 : :
60 : 0 : return true;
61 : : }
62 : :
63 : 1876 : bool get_map_result(uint32 ip_addr, const NumericData*& result)
64 : : {
65 : : #ifndef ACTIVE_MAPPING
66 : 1876 : result = &default_values_numeric;
67 : 1876 : return true;
68 : : #endif
69 : : HashKey machinekey(&ip_addr, 1);
70 : : NumericData* entry = active_map_numeric.Lookup(&machinekey);
71 : :
72 : : if ( ! entry )
73 : : result = &default_values_numeric;
74 : : else
75 : : result = entry;
76 : :
77 : : return true;
78 : : }
79 : :
80 : :
81 : 0 : char* chop (char* s)
82 : : {
83 : 0 : s[strlen(s) - 1] = 0;
84 : 0 : return s;
85 : : }
86 : :
87 : 6 : map < const char *, ReassemblyPolicy, ltstr > reassem_names;
88 : :
89 : : // Remember to index the table with IP address in network order!
90 : 3 : bool load_mapping_table(const char* map_file)
91 : : {
92 : 3 : reassem_names["BSD"] = RP_BSD;
93 : 3 : reassem_names["linux"] = RP_LINUX;
94 : 3 : reassem_names["last"] = RP_LAST;
95 : 3 : reassem_names["first"] = RP_FIRST;
96 : :
97 : : // Default values are read in from AM file under IP address 0.0.0.0
98 : : default_values.Insert(new HashKey("accepts_rst_outside_window"),
99 : 3 : new string("no"));
100 : : default_values.Insert(new HashKey("accepts_rst_in_window"),
101 : 3 : new string("yes"));
102 : : default_values.Insert(new HashKey("accepts_rst_in_sequence"),
103 : 3 : new string("yes"));
104 : : default_values.Insert(new HashKey("mtu"),
105 : 3 : new string("0"));
106 : :
107 : 3 : default_values_numeric.path_MTU = 0; // 0 = unknown
108 : 3 : default_values_numeric.hops = 0; // 0 = unknown;
109 : 3 : default_values_numeric.ip_reassem = RP_UNKNOWN;
110 : 3 : default_values_numeric.tcp_reassem = RP_UNKNOWN;
111 : 3 : default_values_numeric.accepts_rst_in_window = true;
112 : 3 : default_values_numeric.accepts_rst_outside_window = false;
113 : :
114 : :
115 [ + - - + ]: 3 : if ( map_file && strlen(map_file) )
116 : : {
117 : 0 : FILE* f = fopen(map_file, "r");
118 [ # # ]: 0 : if ( ! f )
119 : 0 : return false;
120 : :
121 : : char buf[512];
122 [ # # ]: 0 : if ( ! fgets(buf, sizeof(buf), f) )
123 : 0 : error("Error reading mapping file.\n");
124 : :
125 : 0 : int num_fields = atoi(buf);
126 : :
127 [ # # ][ # # ]: 0 : string* field_names = new string[num_fields];
[ # # ]
128 [ # # ]: 0 : for ( int i = 0; i < num_fields; ++i )
129 : : {
130 [ # # ]: 0 : if ( ! fgets(buf, sizeof(buf), f) )
131 : 0 : error("Error reading mapping file.\n");
132 : 0 : field_names[i] = chop(buf);
133 : : }
134 : :
135 [ # # ]: 0 : if ( ! fgets(buf, sizeof(buf), f) )
136 : 0 : error("Error reading mapping file.\n");
137 : :
138 : 0 : int num_machines = atoi(buf);
139 : :
140 [ # # ]: 0 : for ( int j = 0; j < num_machines; ++j )
141 : : { // read ip address, parse it
142 [ # # ]: 0 : if ( ! fgets(buf, sizeof(buf), f) )
143 : 0 : error("Error reading mapping file.\n");
144 : :
145 : : uint32 ip;
146 : : in_addr in;
147 [ # # ]: 0 : if ( ! inet_aton(chop(buf), &in) )
148 : 0 : error("Error reading mapping file.\n");
149 : :
150 : 0 : ip = in.s_addr;
151 : :
152 : : MachineMap* newmap;
153 : : NumericData* newnumeric;
154 : :
155 [ # # ]: 0 : if ( ip ) // ip = 0.0.0.0 = default values
156 : : {
157 : 0 : newmap = new MachineMap;
158 : 0 : newnumeric = new NumericData;
159 : : }
160 : : else
161 : : {
162 : 0 : newmap = &default_values;
163 : 0 : newnumeric = &default_values_numeric;
164 : : }
165 : :
166 [ # # ][ # # ]: 0 : for ( int i = 0; i < num_fields; ++i )
167 : : {
168 [ # # ]: 0 : if ( ! fgets(buf, sizeof(buf), f) )
169 : 0 : error("Error reading mapping file.\n");
170 : :
171 : 0 : string fname = field_names[i];
172 : :
173 : 0 : chop(buf);
174 : :
175 : : // Don't try to parse an unknown value ("").
176 [ # # ]: 0 : if ( streq(buf, "") )
177 : 0 : continue;
178 : :
179 : 0 : HashKey mapkey(fname.c_str());
180 : 0 : newmap->Insert(&mapkey, new string(buf));
181 : :
182 [ # # ]: 0 : if ( fname == "mtu" )
183 : 0 : newnumeric->path_MTU = atoi(buf);
184 : :
185 [ # # ]: 0 : else if ( fname == "hops" )
186 : 0 : newnumeric->hops = atoi(buf);
187 : :
188 [ # # ]: 0 : else if ( fname == "tcp_segment_overlap" )
189 : 0 : newnumeric->tcp_reassem = reassem_names[buf];
190 : :
191 [ # # ]: 0 : else if ( fname == "overlap_policy" )
192 : 0 : newnumeric->ip_reassem = reassem_names[buf];
193 : :
194 [ # # ]: 0 : else if ( fname == "accepts_rst_in_window" )
195 : 0 : newnumeric->accepts_rst_in_window = streq(buf, "yes");
196 : :
197 [ # # ]: 0 : else if ( fname == "accepts_rst_outside_window" )
198 : 0 : newnumeric->accepts_rst_outside_window = streq(buf, "yes");
199 : :
200 [ # # ]: 0 : else if ( fname == "accepts_rst_in_sequence" )
201 : 0 : newnumeric->accepts_rst_in_sequence = streq(buf, "yes");
202 : :
203 : : else
204 : 0 : warn("unrecognized mapping file tag:", fname.c_str());
205 : : }
206 : :
207 : 0 : HashKey machinekey(&ip, 1);
208 : 0 : active_map.Insert(&machinekey, newmap);
209 : 0 : active_map_numeric.Insert(&machinekey, newnumeric);
210 : : }
211 : :
212 [ # # ][ # # ]: 0 : delete [] field_names;
213 : : }
214 : :
215 : 3 : map_was_loaded = true;
216 : :
217 : 3 : return true;
218 [ + - ][ + - ]: 6 : }
|