1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
implement IPattr;
include "sys.m";
include "bufio.m";
include "attrdb.m";
attrdb: Attrdb;
Db, Dbentry, Tuples: import attrdb;
include "ip.m";
ip: IP;
IPaddr: import ip;
include "ipattr.m";
init(m: Attrdb, ipa: IP)
{
# sys = load Sys Sys->PATH;
attrdb = m;
ip = ipa;
}
dbattr(s: string): string
{
digit := 0;
dot := 0;
alpha := 0;
hex := 0;
colon := 0;
for(i := 0; i < len s; i++){
case c := s[i] {
'0' to '9' =>
digit = 1;
'a' to 'f' or 'A' to 'F' =>
hex = 1;
'.' =>
dot = 1;
':' =>
colon = 1;
* =>
if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '-' || c == '&')
alpha = 1;
}
}
if(alpha){
if(dot)
return "dom";
return "sys";
}
if(colon)
return "ip";
if(dot){
if(!hex)
return "ip";
return "dom";
}
return "sys";
}
findnetattr(ndb: ref Db, attr: string, val: string, rattr: string): (string, string)
{
(matches, err) := findnetattrs(ndb, attr, val, rattr::nil);
if(matches == nil)
return (nil, err);
(nil, nattr) := hd matches;
na := hd nattr;
#{sys := load Sys Sys->PATH; sys->print("%q=%q->%q ::", attr, val, rattr);for(al:=na.pairs; al != nil; al = tl al)sys->print(" %q=%q", (hd al).attr, (hd al).val); sys->print("\n");}
if(na.name == rattr && na.pairs != nil)
return ((hd na.pairs).val, nil);
return (nil, nil);
}
reverse(l: list of string): list of string
{
rl: list of string;
for(; l != nil; l = tl l)
rl = hd l :: rl;
return rl;
}
valueof(l: list of ref Netattr, attr: string): list of string
{
rl: list of string;
for(; l != nil; l = tl l){
na := hd l;
if(na.name == attr){
for(p := na.pairs; p != nil; p = tl p)
rl = (hd p).val :: rl;
}
}
return reverse(rl);
}
netvalueof(l: list of ref Netattr, attr: string, a: IP->IPaddr): list of string
{
rl: list of string;
for(; l != nil; l = tl l){
na := hd l;
if(na.name == attr && a.mask(na.mask).eq(na.net)){
for(p := na.pairs; p != nil; p = tl p)
rl = (hd p).val :: rl;
}
}
return reverse(rl);
}
findnetattrs(ndb: ref Db, attr: string, val: string, rattrs: list of string): (list of (IPaddr, list of ref Netattr), string)
{
rl: list of (IPaddr, list of ref Netattr);
if(ndb == nil)
return (nil, "no database");
(e, ptr) := ndb.findbyattr(nil, attr, val, "ip");
if(e == nil){
if(attr != "ip")
return (nil, "ip attribute not found");
# look for attributes associated with networks that include `a'
(ok, a) := IPaddr.parse(val);
if(ok < 0)
return (nil, "invalid ip address in db");
netattrs := mkattrlist(rattrs);
netattributes(ndb, a, netattrs);
rl = (a, netattrs) :: nil;
}else{
netattrs: list of ref Netattr;
for(matches := e.findbyattr(attr, val, "ip"); matches != nil; matches = tl matches){
for((nil, allip) := hd matches; allip != nil; allip = tl allip){
ipa := (hd allip).val;
(ok, a) := IPaddr.parse(ipa);
if(ok < 0)
return (nil, "invalid ip address in db");
netattrs = mkattrlist(rattrs);
pptr := ptr;
pe := e;
for(;;){
attribute(pe, a, ip->allbits, netattrs, 1);
(pe, pptr) = ndb.findpair(pptr, attr, val);
if(pe == nil)
break;
}
netattributes(ndb, a, netattrs);
rl = (a, netattrs) :: rl;
}
}
}
results: list of (IPaddr, list of ref Netattr);
for(; rl != nil; rl = tl rl)
results = hd rl :: results;
return (results, nil);
}
netattributes(ndb: ref Db, a: IPaddr, nas: list of ref Netattr): string
{
e: ref Dbentry;
ptr: ref Attrdb->Dbptr;
for(;;){
(e, ptr) = ndb.find(ptr, "ipnet");
if(e == nil)
break;
ipaddr := e.findfirst("ip");
if(ipaddr == nil)
continue;
(ok, netip) := IPaddr.parse(ipaddr);
if(ok < 0)
return "bad ip address in db";
netmask: IPaddr;
mask := e.findfirst("ipmask");
if(mask == nil){
if(!netip.isv4())
continue;
netmask = netip.classmask();
}else{
(ok, netmask) = IPaddr.parsemask(mask);
if(ok < 0)
return "bad ipmask in db";
}
if(a.mask(netmask).eq(netip))
attribute(e, netip, netmask, nas, 0);
}
return nil;
}
attribute(e: ref Dbentry, netip: IPaddr, netmask: IPaddr, nas: list of ref Netattr, ishost: int)
{
for(; nas != nil; nas = tl nas){
na := hd nas;
if(na.pairs != nil){
if(!na.mask.mask(netmask).eq(na.mask))
continue;
# new one is at least as specific
}
matches := e.find(na.name);
if(matches == nil){
if(na.name != "ipmask" || ishost)
continue;
matches = (nil, ref Attrdb->Attr("ipmask", netmask.masktext(), 0)::nil) :: nil;
}
na.net = netip;
na.mask = netmask;
rl: list of ref Attrdb->Attr;
for(; matches != nil; matches = tl matches){
(nil, al) := hd matches;
for(; al != nil; al = tl al)
rl = hd al :: rl;
}
na.pairs = nil;
for(; rl != nil; rl = tl rl)
na.pairs = hd rl :: na.pairs;
}
}
mkattrlist(rattrs: list of string): list of ref Netattr
{
netattrs: list of ref Netattr;
for(; rattrs != nil; rattrs = tl rattrs)
netattrs = ref Netattr(hd rattrs, nil, ip->noaddr, ip->noaddr) :: netattrs;
return netattrs;
}
|