forked from paaguti/java-mrt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributes.java
More file actions
318 lines (273 loc) · 9.17 KB
/
Attributes.java
File metadata and controls
318 lines (273 loc) · 9.17 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// This file is part of java-mrt
// A library to parse MRT files
// This file is released under LGPL 3.0
// http://www.gnu.org/licenses/lgpl-3.0-standalone.html
package org.javamrt.mrt;
import java.net.InetAddress;
import java.util.Vector;
import org.javamrt.utils.Debug;
import org.javamrt.utils.RecordAccess;
public class Attributes {
private String toStr = null;
/*
* private Vector <Attribute> getAttributes () { return attributes; }
*/
private Vector<Attribute> attributes = new Vector<Attribute>(MRTConstants.ATTRIBUTE_TOTAL + 1);
public Attributes(byte[] record, int attrLen, int attrPos, int attrBytes, boolean addPath)
throws Exception {
if (attrBytes != 2 && attrBytes != 4)
throw new AttributeException(String.format(
"Attributes needs attrBytes 2 or 4 (not %d", attrBytes));
decode(record, attrLen, attrPos, attrBytes, addPath);
}
public Attributes(byte[] record, int attrLen, int attrPos, boolean addPath) throws Exception {
decode(record, attrLen, attrPos, 2, addPath);
}
private void decode(byte[] record, int attrLen, int attrPos, int attrBytes, boolean addPath)
throws Exception {
byte[] buffer;
int here = attrPos;
if (Debug.compileDebug)
Debug.printf("Attributes(...,%d,%d,%d)\n", attrLen, attrPos,
attrBytes);
for (int i = 0; i <= MRTConstants.ATTRIBUTE_TOTAL; i++)
if (i == MRTConstants.ATTRIBUTE_NEXT_HOP)
attributes.addElement(new NextHop());
else
attributes.addElement(null);
while (here < attrLen + attrPos) {
int flag = RecordAccess.getU8(record, here);
int type = RecordAccess.getU8(record, here + 1);
int len;
int dato;
if ((flag & MRTConstants.BGP_ATTR_FLAG_EXTLEN) == 0) {
len = RecordAccess.getU8(record, here + 2);
dato = here + 3;
} else {
len = RecordAccess.getU16(record, here + 2);
dato = here + 4;
}
buffer = RecordAccess.getBytes(record, dato, len);
here = dato + len;
if (Debug.compileDebug)
Debug.printf("Flag = 0x%02x(%s) type = %02d Len=%d\n", flag,
MRTConstants.attrFlags((byte) flag), type, len);
switch (type) {
case MRTConstants.AS_PATH:
Attribute asPath = new ASPath(buffer, attrBytes);
attributes.set(MRTConstants.ATTRIBUTE_AS_PATH, asPath);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_AS_PATH = " + asPath);
break;
case MRTConstants.ORIGIN:
Attribute attrOrigin = new AttrOrigin(buffer);
attributes.set(MRTConstants.ATTRIBUTE_ORIGIN, attrOrigin);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_ORIGIN, ");
break;
case MRTConstants.NEXT_HOP:
Attribute nextHop = new NextHop(buffer);
attributes.set(MRTConstants.ATTRIBUTE_NEXT_HOP, nextHop);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_NEXT_HOP " + nextHop);
break;
case MRTConstants.LOCAL_PREF:
Attribute localPref = new LocalPref(buffer);
attributes.set(MRTConstants.ATTRIBUTE_LOCAL_PREF, localPref);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_LOCAL_PREF, ");
break;
case MRTConstants.MULTI_EXIT:
Attribute med = new Med(buffer);
attributes.set(MRTConstants.ATTRIBUTE_MULTI_EXIT, med);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_MULTI_EXIT, ");
break;
case MRTConstants.COMMUNITY:
Attribute community = new Community(buffer);
attributes.set(MRTConstants.ATTRIBUTE_COMMUNITY, community);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_COMMUNITY, ");
break;
case MRTConstants.ATOMIC_AGGREGATE:
Attribute atomAggr = new AtomAggr();
attributes.set(MRTConstants.ATTRIBUTE_ATOMIC_AGGREGATE,
atomAggr);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_ATOMIC_AGGREGATE, ");
break;
case MRTConstants.AGGREGATOR:
Attribute aggregator = new Aggregator(buffer, attrBytes);
attributes.set(MRTConstants.ATTRIBUTE_AGGREGATOR, aggregator);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_AGGREGATOR, ");
break;
case MRTConstants.ORIGINATOR_ID:
Attribute originatorId = new OriginatorID(buffer);
attributes.set(MRTConstants.ATTRIBUTE_ORIGINATOR_ID,
originatorId);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_ORIGINATOR_ID, ");
break;
case MRTConstants.CLUSTER_LIST:
Attribute clusterList = new ClusterList(buffer);
attributes
.set(MRTConstants.ATTRIBUTE_CLUSTER_LIST, clusterList);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_CLUSTER_LIST, ");
break;
case MRTConstants.DPA:
Attribute dpa = new Dpa(buffer);
attributes.set(MRTConstants.ATTRIBUTE_DPA, dpa);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_DPA, ");
break;
case MRTConstants.ADVERTISER:
Attribute advertiser = new Advertiser(buffer);
attributes.set(MRTConstants.ATTRIBUTE_ADVERTISER, advertiser);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_ADVERTISER, ");
break;
case MRTConstants.CLUSTER_ID:
Attribute clusterId = new ClusterId(buffer);
attributes.set(MRTConstants.ATTRIBUTE_CLUSTER_ID, clusterId);
if (Debug.compileDebug)
Debug.printf("ATTRIBUTE_CLUSTER_ID = %s\n", clusterId
.toString());
break;
case MRTConstants.MP_REACH:
MpReach mpReach = new MpReach(buffer, addPath);
attributes.set(MRTConstants.ATTRIBUTE_MP_REACH, mpReach);
InetAddress nhia = mpReach.getNextHops().firstElement();
try {
NextHop nh = new NextHop(nhia);
attributes.set(MRTConstants.ATTRIBUTE_NEXT_HOP, nh);
if (Debug.compileDebug)
Debug.printf("ATTRIBUTE_MP_REACH :%s\n NEXT HOP:%s",
mpReach.toString(), nh.toString());
} catch (NullPointerException npe) {
// ignore silently
}
break;
case MRTConstants.MP_UNREACH:
Attribute mpUnreach = new MpUnReach(buffer, addPath);
attributes.set(MRTConstants.ATTRIBUTE_MP_UNREACH, mpUnreach);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_MP_UNREACH " + mpUnreach);
break;
case MRTConstants.EXT_COMMUNITIES:
Attribute extCommunities = new ExtCommunities(buffer);
attributes.set(MRTConstants.ATTRIBUTE_EXT_COMMUNITIES,
extCommunities);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_EXT_COMMUNITIES, ");
break;
/*
* TODO: Handle 4 byte AS stuff correctly
*
* Observed in rrc01: around April 2007 sometimes AS4PATH is set to
* 0
*/
case MRTConstants.AS4_PATH:
if (buffer.length == 0) {
hasAS4PathBug = true;
} else {
/**
* throws RFC4893Exception, AttributeException
*/
// As4Path.replaceAS23456(buffer, (ASPath) getAttribute(MRTConstants.ATTRIBUTE_AS_PATH));
RFC4893.replaceAS23456(buffer, (ASPath) getAttribute(MRTConstants.ATTRIBUTE_AS_PATH));
}
break;
case MRTConstants.AS4_AGGREGATOR:
/*
* Override 2byte AS Aggregator
*
* TODO: sanity check: make sure 2 byte aggregator was 23456
*/
Attribute as4Aggregator = new Aggregator(buffer, 4);
// attributes.set (ATTRIBUTE_AS4_AGGREGATOR, as4Aggregator);
attributes
.set(MRTConstants.ATTRIBUTE_AGGREGATOR, as4Aggregator);
if (Debug.compileDebug)
Debug.println("ATTRIBUTE_AS4_AGGREGATOR ");
break;
// Expired but present in RRC!
case MRTConstants.ATTRIBUTE_ASPATHLIMIT:
Attribute asPathLimit = new ASPathLimit(buffer);
attributes.set(MRTConstants.ATTRIBUTE_ASPATHLIMIT, asPathLimit);
if (Debug.compileDebug)
Debug.printf("ATTRIBUTE_ASPATHLIMIT %s\n", asPathLimit
.toString());
this.hasASPATHLimit = true;
break;
default:
throw new AttributeException(type);
}
}
}
public Attribute getAttribute(int index) throws Exception {
return attributes.elementAt(index);
}
public String toString() {
if (toStr != null)
return toStr;
toStr = new String();
for (int i = MRTConstants.ATTRIBUTE_AS_PATH; i <= MRTConstants.ATTRIBUTE_TOTAL; i++) {
if (attributes.elementAt(i) != null)
toStr = toStr.concat(attributes.elementAt(i).toString());
else {
if (i == MRTConstants.ATTRIBUTE_LOCAL_PREF
|| i == MRTConstants.ATTRIBUTE_MULTI_EXIT)
toStr = toStr.concat("0");
else if (i == MRTConstants.ATTRIBUTE_ATOMIC_AGGREGATE)
toStr = toStr.concat("NAG");
}
toStr = toStr.concat("|");
}
return toStr;
}
public ASPath getASPath() {
return (ASPath) attributes.elementAt(MRTConstants.ATTRIBUTE_AS_PATH);
}
public Community getCommunity() {
Community result = (Community) attributes
.elementAt(MRTConstants.ATTRIBUTE_COMMUNITY);
if (result != null)
return result;
return Community.empty();
}
public Med getMed() {
Med result = (Med) attributes
.elementAt(MRTConstants.ATTRIBUTE_MULTI_EXIT);
if (result == null)
return new Med(0);
return result;
}
public boolean hasAS4PathBug = false;
public boolean hasASPATHLimit = false;
public boolean equals(Object o) {
if (o == null)
return false;
if (o == this)
return true;
if (o instanceof Attributes) {
//
// duplicated if _all_ attributes are the same
//
for (int i = 0; i < MRTConstants.ATTRIBUTE_TOTAL; i++) {
Attribute a1 = this.attributes.elementAt(i), a2 = ((Attributes) o).attributes
.elementAt(i);
if (a1 == null) {
if (a2 != null)
return false;
continue;
}
if (!a1.equals(a2))
return false;
}
return true;
} // else
return false;
}
}