2 * maskmap - Tcl extension for address mask map data structures
3 * Copyright 2006-2012 Ian Jackson
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "chiark_tcl_hbytes.h"
21 /*---------- operations on AddrMap_Entry ----------*/
23 static void ame_init(AddrMap_Entry *ame) {
29 static unsigned ame_clear_unwanted(AddrMap_Entry *ame, int bytes) {
30 /* returns non-0 iff some bits were cleared */
32 unsigned result, sparemask;
35 sparebits= bytes * 8 - ame->prefixlen;
36 if (!sparebits) return 0;
38 sparemask= (1u << sparebits) - 1;
39 datap= &ame->prefix[bytes-1];
41 result= *datap & sparemask;
47 static int ame_parsekey(Tcl_Interp *ip, AddrMap_Entry *ame,
48 Tcl_Obj *prefixo, Tcl_Obj *prefixbitso,
50 /* *ame should be blank entry; after exit (even error exit) it will be valid
51 * - on errors, it will be blank. inmap is 1 if we're parsing an existing
52 * map or 0 if it's an entry to be added or modified. */
54 int suppliedprefixbytes, prefixbits, wantprefixbytes;
58 hbytes_empty(&prefix);
60 rc= pat_hb(ip,prefixo,&prefix); if (rc) goto x_rc;
61 rc= pat_int(ip,prefixbitso,&prefixbits); if (rc) goto x_rc;
63 wantprefixbytes= prefix_bytes(prefixbits);
64 suppliedprefixbytes= hbytes_len(&prefix);
66 if (suppliedprefixbytes < wantprefixbytes) {
67 rc= staticerr(ip, "addr-map entry PREFIX too short for PREFIX-LEN",
68 "HBYTES ADDRMAP SYNTAX UNDERRUN");
71 if (inmap && suppliedprefixbytes > wantprefixbytes) {
72 rc= staticerr(ip, "addr-map existing entry PREFIX too long for PREFIX-LEN",
73 "HBYTES ADDRMAP SYNTAX OVERRUN");
77 ame->prefixlen= prefixbits;
78 ame->prefix= TALLOC(wantprefixbytes); assert(ame->prefix);
79 memcpy(ame->prefix, data, wantprefixbytes);
81 if (ame_clear_unwanted(ame, wantprefixbytes)) {
82 rc= staticerr(ip, "addr-map entry PREFIX contains bits excluded"
83 " by PREFIX-LEN", "HBYTES ADDRMAP SYNTAX EXCLBITS");
94 static int ame_contains(const AddrMap_Entry *ref, const Byte *addr, int len) {
95 int directbytes, leftoverbits;
97 assert(len >= ref->prefixlen);
99 directbytes= ref->prefixlen / 8;
100 if (memcmp(ref->prefix, addr, directbytes)) return 0;
102 leftoverbits= ref->prefixlen % 8;
104 if ((addr[directbytes] & (0xffu << leftoverbits))
105 != search->prefix[directbytes])
111 static int ame_compare(const AddrMap_Entry *a, const AddrMap_Entry *b) {
112 /* +2 = a covers later range of address space than b
113 * +1 = a wholly contains but is not equal to b
114 * 0 = a is identical to b
115 * -1 = b wholly contains but is not equal to a
116 * -2 = b covers later range of address space than a
118 int al= a->prefixlen;
119 int bl= b->prefixlen;
122 if (al==bl) { ml=al; }
123 else if (al<bl) { ml=al; if (ame_contains(a,b->prefix,bl)) return +1; }
124 else if (bl<al) { ml=bl; if (ame_contains(b,a->prefix,al)) return -1; }
126 d= memcmp(b->prefix, a->prefix, prefix_bytes(ml));
132 /*---------- searching maps ----------*/
142 am_binarychop(AddrMap_Value *am, int low_oreq, int high_strict, void *u,
143 int (*test)(AddrMap_Entry *am, void *u) /* -ve => look left */,
148 if (high_strict <= low_oreq) {
149 assert(high_strict == low_oreq);
154 mid= (high_strict + low_oreq) / 2;
155 cmp= test(&am->entries[mid], u);
170 int forbid_aroundsmall;
171 AddrMap_Entry proposed;
176 am_search_binchoptest(AddrMap_Entry *ame, void *u_v) {
177 struct am_search_u *u= u_v;
180 cmp= ame_compare(&u.proposed, ame);
182 case -1: u->sr= sr_inbig; return 0;
183 case 0: u->sr= sr_exact; return 0;
184 case +1: u->sr= sr_aroundsmall; return 0;
190 am_search(AddrMap_Value *am, const AddrMap_Entry *proposed, int *place_r) {
192 struct am_search_u u;
194 u.forbid_aroundsmall= forbid_aroundsmall;
195 u.proposed= proposed;
198 *place_r= am_binarychop(am, 0, am.used, &u, am_search_binchoptest, &found);
200 assert(!!found == (u.sr != sr_notfound));
204 /*---------- useful operations on AddrMap_Value etc. ----------*/
206 /*---------- amendment (complex algorithm) ----------*/
208 struct am_amend_aroundsmall_u {
215 am_amend_aroundsmall_binchoptest(AddrMap_Entry *search, void *u_v) {
216 struct am_amend_aroundsmall_u *u= u_v;
218 cmp= u->sign * ame_compare(search, u->new);
221 case +2: return -u->sign;
222 case +1: return +u->sign;
227 int do_addrmap_amend(ClientData cd, Tcl_Interp *ip,
228 AddrMap_Var map, Tcl_Obj *prefix,
229 Tcl_Obj *preflen, Tcl_Obj *data) {
230 AddrMap_Value *am= map.am;
231 AddrMap_Entry new, *fragment;
232 AddrMap_Entry *breaking, *replacements;
233 int rc, insertat, findend, cmp, nreplacements, new_used;
234 struct am_amend_aroundsmall_u u;
238 rc= ame_parsekey(ip,&new,prefix,preflen,0); if (rc) return rc;
240 sr= am_search(am, &new, &searched);
244 replace_start= searched;
245 replace_end= searched;
253 replace_end= searched+1;
259 replace_start= am_binarychop(am, 0, searched, &u,
260 am_amend_aroundsmall_binchoptest, &dummy);
262 replace_end= am_binarychop(am, searched+1, am.used, &u,
263 am_amend_aroundsmall_binchoptest, &dummy);
267 /* Urgh, we need to break it up. This produces
268 * - innermost prefix (the new one) as specified
269 * - one for each bitlength
271 * > outermost (the existing one)
272 * each one specifying the outermost prefix plus zero, one,
273 * two, etc. bits of the innermost followed by one bit
274 * opposite to the innermost, with the outermost's data
275 * Eg, if we have ff/8=>A and want to amend so that ffff/16=>B
276 * then we replace ff/8 with ff0/9=>A ff8/10=>A ffc/11=>A ...
277 * ... fff8/14=>A fffc/15=>A fffe/16=>A ffff/16=>B.
280 breaking= &am.entries[searched];
281 nreplacements= new.prefix - breaking->prefixlen + 1;
282 fixme check integer overflow ^
283 replacements= TALLOC(sizeof(*replacements) * nreplacements);
285 for (fragmentlen= breaking->prefixlen + 1,
286 left_insert= 0, right_insert= nreplacements;
287 fragmentlen <= new.prefix;
291 fragmentbytes= prefix_bytes(fragmentlen)
292 fragment->prefixlen= fragmentlen;
293 fragment->prefix= TALLOC(fragmentbytes);
294 memcpy(fragment->prefix, new.prefix, fragmentbytes);
295 ame_clear_unwanted(fragment, fragmentbytes);
297 fragment->prefix[fragmentbytes] ^=
298 0x80u >> ((fragmentlen+7) & 7);
300 switch (ame_compare(&fragment, &new)) {
301 case -2: replacements[left_insert++]= fragment; break;
302 case +2: replacements[--right_insert]= fragment; break;
306 assert(left_insert == right_insert-1);
307 replacements[left_insert]= new;
310 replace_end= searched+1;
315 new_used= am.used - (replace_end - replace_start) + nreplacements;
317 if (new_used > am.space)
318 am_reallocentries(am, new_used * 2);
320 for (scan=replacements, i=0;
324 Tcl_IncrRefCount(scan->data);
327 for (i= replace_start, scan= am.entries+i;
333 memmove(am.entries + replace_start + nreplacements,
334 am.entries + replace_end,
335 sizeof(*am.entries) * (am.used - replace_end));
337 memcpy(am.entries + replace_start,
339 sizeof(*am.entries) * nreplacements);
342 if (replacements != &new)
343 /* we don't bother freeing the actual array elements because
344 * if replacements!=&new the array is only full if we're
345 * committed and have already copied the values into the actual
352 /*---------- other substantial operations on mask maps ----------*/
354 int do_addrmap_lookup(ClientData cd, Tcl_Interp *ip,
355 Tcl_Obj *mapo, HBytes_Value addrhb, Tcl_Obj *def,
357 AddrMap_Value *am= (void*)&mapo->internalRep;
358 const Byte *addr= hbytes_data(&addrhb);
359 int addrbytes= hbytes_len(&addrhb);
360 int i, addrbits, place;
363 addrbits= addrbytes * 8;
364 sr= am_search(am, addr, addrbits, &place);
369 if (!def) return staticerr(ip, "address not found in addr-map",
370 "HBYTES ADDRMAP NOMATCH");
375 return staticerr(ip, "address shorter than mask in map",
376 "HBYTES ADDRMAP UNDERRUN");
380 *result= am.entres[place].data;
388 /*---------- Tcl type and arg parsing functions ----------*/