Implement NDP-related classes
* Resolve ONOS-511 * Refactor: Move NDP-related classes into org.onlab.packet.ndp package * Bugfix: payload is not properly set in testSerialize Change-Id: Idb2dbfbd0297152c0e26ac2d5d5f8dba62824660
Showing
13 changed files
with
879 additions
and
11 deletions
... | @@ -48,8 +48,8 @@ import org.onlab.packet.Ethernet; | ... | @@ -48,8 +48,8 @@ import org.onlab.packet.Ethernet; |
48 | import org.onlab.packet.IpAddress; | 48 | import org.onlab.packet.IpAddress; |
49 | import org.onlab.packet.IPacket; | 49 | import org.onlab.packet.IPacket; |
50 | import org.onlab.packet.IPv6; | 50 | import org.onlab.packet.IPv6; |
51 | -import org.onlab.packet.NeighborAdvertisement; | 51 | +import org.onlab.packet.ndp.NeighborAdvertisement; |
52 | -import org.onlab.packet.NeighborSolicitation; | 52 | +import org.onlab.packet.ndp.NeighborSolicitation; |
53 | import org.onlab.packet.VlanId; | 53 | import org.onlab.packet.VlanId; |
54 | import org.osgi.service.component.ComponentContext; | 54 | import org.osgi.service.component.ComponentContext; |
55 | import org.slf4j.Logger; | 55 | import org.slf4j.Logger; | ... | ... |
... | @@ -18,6 +18,11 @@ | ... | @@ -18,6 +18,11 @@ |
18 | 18 | ||
19 | package org.onlab.packet; | 19 | package org.onlab.packet; |
20 | 20 | ||
21 | +import org.onlab.packet.ndp.NeighborAdvertisement; | ||
22 | +import org.onlab.packet.ndp.NeighborSolicitation; | ||
23 | +import org.onlab.packet.ndp.Redirect; | ||
24 | +import org.onlab.packet.ndp.RouterAdvertisement; | ||
25 | +import org.onlab.packet.ndp.RouterSolicitation; | ||
21 | import java.nio.ByteBuffer; | 26 | import java.nio.ByteBuffer; |
22 | import java.util.HashMap; | 27 | import java.util.HashMap; |
23 | import java.util.Map; | 28 | import java.util.Map; |
... | @@ -32,12 +37,16 @@ public class ICMP6 extends BasePacket { | ... | @@ -32,12 +37,16 @@ public class ICMP6 extends BasePacket { |
32 | public static final byte ROUTER_ADVERTISEMENT = (byte) 0x86; | 37 | public static final byte ROUTER_ADVERTISEMENT = (byte) 0x86; |
33 | public static final byte NEIGHBOR_SOLICITATION = (byte) 0x87; | 38 | public static final byte NEIGHBOR_SOLICITATION = (byte) 0x87; |
34 | public static final byte NEIGHBOR_ADVERTISEMENT = (byte) 0x88; | 39 | public static final byte NEIGHBOR_ADVERTISEMENT = (byte) 0x88; |
40 | + public static final byte REDIRECT = (byte) 0x89; | ||
35 | public static final Map<Byte, Class<? extends IPacket>> PROTOCOL_CLASS_MAP = | 41 | public static final Map<Byte, Class<? extends IPacket>> PROTOCOL_CLASS_MAP = |
36 | new HashMap<>(); | 42 | new HashMap<>(); |
37 | 43 | ||
38 | static { | 44 | static { |
45 | + ICMP6.PROTOCOL_CLASS_MAP.put(ICMP6.ROUTER_SOLICITATION, RouterSolicitation.class); | ||
46 | + ICMP6.PROTOCOL_CLASS_MAP.put(ICMP6.ROUTER_ADVERTISEMENT, RouterAdvertisement.class); | ||
39 | ICMP6.PROTOCOL_CLASS_MAP.put(ICMP6.NEIGHBOR_SOLICITATION, NeighborSolicitation.class); | 47 | ICMP6.PROTOCOL_CLASS_MAP.put(ICMP6.NEIGHBOR_SOLICITATION, NeighborSolicitation.class); |
40 | ICMP6.PROTOCOL_CLASS_MAP.put(ICMP6.NEIGHBOR_ADVERTISEMENT, NeighborAdvertisement.class); | 48 | ICMP6.PROTOCOL_CLASS_MAP.put(ICMP6.NEIGHBOR_ADVERTISEMENT, NeighborAdvertisement.class); |
49 | + ICMP6.PROTOCOL_CLASS_MAP.put(ICMP6.REDIRECT, Redirect.class); | ||
41 | } | 50 | } |
42 | 51 | ||
43 | protected byte icmpType; | 52 | protected byte icmpType; | ... | ... |
... | @@ -16,13 +16,18 @@ | ... | @@ -16,13 +16,18 @@ |
16 | 16 | ||
17 | 17 | ||
18 | 18 | ||
19 | -package org.onlab.packet; | 19 | +package org.onlab.packet.ndp; |
20 | + | ||
21 | +import org.onlab.packet.BasePacket; | ||
22 | +import org.onlab.packet.Data; | ||
23 | +import org.onlab.packet.IPacket; | ||
24 | +import org.onlab.packet.Ip6Address; | ||
20 | 25 | ||
21 | import java.nio.ByteBuffer; | 26 | import java.nio.ByteBuffer; |
22 | import java.util.Arrays; | 27 | import java.util.Arrays; |
23 | 28 | ||
24 | /** | 29 | /** |
25 | - * Implements ICMPv6 Neighbor Solicitation packet format. (RFC 4861) | 30 | + * Implements ICMPv6 Neighbor Advertisement packet format. (RFC 4861) |
26 | */ | 31 | */ |
27 | public class NeighborAdvertisement extends BasePacket { | 32 | public class NeighborAdvertisement extends BasePacket { |
28 | public static final byte HEADER_LENGTH = 20; // bytes | 33 | public static final byte HEADER_LENGTH = 20; // bytes | ... | ... |
... | @@ -16,7 +16,12 @@ | ... | @@ -16,7 +16,12 @@ |
16 | 16 | ||
17 | 17 | ||
18 | 18 | ||
19 | -package org.onlab.packet; | 19 | +package org.onlab.packet.ndp; |
20 | + | ||
21 | +import org.onlab.packet.BasePacket; | ||
22 | +import org.onlab.packet.Data; | ||
23 | +import org.onlab.packet.IPacket; | ||
24 | +import org.onlab.packet.Ip6Address; | ||
20 | 25 | ||
21 | import java.nio.ByteBuffer; | 26 | import java.nio.ByteBuffer; |
22 | import java.util.Arrays; | 27 | import java.util.Arrays; | ... | ... |
1 | +/* | ||
2 | + * Copyright 2014 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | + | ||
18 | + | ||
19 | +package org.onlab.packet.ndp; | ||
20 | + | ||
21 | +import org.onlab.packet.BasePacket; | ||
22 | +import org.onlab.packet.Data; | ||
23 | +import org.onlab.packet.IPacket; | ||
24 | +import org.onlab.packet.Ip6Address; | ||
25 | + | ||
26 | +import java.nio.ByteBuffer; | ||
27 | +import java.util.Arrays; | ||
28 | + | ||
29 | +/** | ||
30 | + * Implements ICMPv6 Redirect packet format. (RFC 4861) | ||
31 | + */ | ||
32 | +public class Redirect extends BasePacket { | ||
33 | + public static final byte HEADER_LENGTH = 36; // bytes | ||
34 | + | ||
35 | + protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH]; | ||
36 | + protected byte[] destinationAddress = new byte[Ip6Address.BYTE_LENGTH]; | ||
37 | + | ||
38 | + /** | ||
39 | + * Gets target address. | ||
40 | + * | ||
41 | + * @return the target IPv6 address | ||
42 | + */ | ||
43 | + public byte[] getTargetAddress() { | ||
44 | + return this.targetAddress; | ||
45 | + } | ||
46 | + | ||
47 | + /** | ||
48 | + * Sets target address. | ||
49 | + * | ||
50 | + * @param targetAddress the target IPv6 address to set | ||
51 | + * @return this | ||
52 | + */ | ||
53 | + public Redirect setTargetAddress(final byte[] targetAddress) { | ||
54 | + this.targetAddress = Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH); | ||
55 | + return this; | ||
56 | + } | ||
57 | + | ||
58 | + /** | ||
59 | + * Gets destination address. | ||
60 | + * | ||
61 | + * @return the destination IPv6 address | ||
62 | + */ | ||
63 | + public byte[] getDestinationAddress() { | ||
64 | + return this.destinationAddress; | ||
65 | + } | ||
66 | + | ||
67 | + /** | ||
68 | + * Sets destination address. | ||
69 | + * | ||
70 | + * @param destinationAddress the destination IPv6 address to set | ||
71 | + * @return this | ||
72 | + */ | ||
73 | + public Redirect setDestinationAddress(final byte[] destinationAddress) { | ||
74 | + this.destinationAddress = Arrays.copyOfRange(destinationAddress, 0, Ip6Address.BYTE_LENGTH); | ||
75 | + return this; | ||
76 | + } | ||
77 | + | ||
78 | + @Override | ||
79 | + public byte[] serialize() { | ||
80 | + byte[] payloadData = null; | ||
81 | + if (this.payload != null) { | ||
82 | + this.payload.setParent(this); | ||
83 | + payloadData = this.payload.serialize(); | ||
84 | + } | ||
85 | + | ||
86 | + int payloadLength = payloadData == null ? 0 : (short) payloadData.length; | ||
87 | + | ||
88 | + final byte[] data = new byte[HEADER_LENGTH + payloadLength]; | ||
89 | + final ByteBuffer bb = ByteBuffer.wrap(data); | ||
90 | + | ||
91 | + bb.putInt(0); | ||
92 | + bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH); | ||
93 | + bb.put(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH); | ||
94 | + | ||
95 | + if (payloadData != null) { | ||
96 | + bb.put(payloadData); | ||
97 | + } | ||
98 | + | ||
99 | + return data; | ||
100 | + } | ||
101 | + | ||
102 | + @Override | ||
103 | + public IPacket deserialize(byte[] data, int offset, int length) { | ||
104 | + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); | ||
105 | + | ||
106 | + bb.getInt(); | ||
107 | + bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH); | ||
108 | + bb.get(this.destinationAddress, 0, Ip6Address.BYTE_LENGTH); | ||
109 | + | ||
110 | + this.payload = new Data(); | ||
111 | + this.payload = this.payload.deserialize(data, bb.position(), bb.limit() | ||
112 | + - bb.position()); | ||
113 | + this.payload.setParent(this); | ||
114 | + | ||
115 | + return this; | ||
116 | + } | ||
117 | + | ||
118 | + /* | ||
119 | + * (non-Javadoc) | ||
120 | + * | ||
121 | + * @see java.lang.Object#hashCode() | ||
122 | + */ | ||
123 | + @Override | ||
124 | + public int hashCode() { | ||
125 | + final int prime = 5807; | ||
126 | + int result = super.hashCode(); | ||
127 | + ByteBuffer bb; | ||
128 | + bb = ByteBuffer.wrap(this.targetAddress); | ||
129 | + for (int i = 0; i < 4; i++) { | ||
130 | + result = prime * result + bb.getInt(); | ||
131 | + } | ||
132 | + bb = ByteBuffer.wrap(this.destinationAddress); | ||
133 | + for (int i = 0; i < 4; i++) { | ||
134 | + result = prime * result + bb.getInt(); | ||
135 | + } | ||
136 | + return result; | ||
137 | + } | ||
138 | + | ||
139 | + /* | ||
140 | + * (non-Javadoc) | ||
141 | + * | ||
142 | + * @see java.lang.Object#equals(java.lang.Object) | ||
143 | + */ | ||
144 | + @Override | ||
145 | + public boolean equals(final Object obj) { | ||
146 | + if (this == obj) { | ||
147 | + return true; | ||
148 | + } | ||
149 | + if (!super.equals(obj)) { | ||
150 | + return false; | ||
151 | + } | ||
152 | + if (!(obj instanceof Redirect)) { | ||
153 | + return false; | ||
154 | + } | ||
155 | + final Redirect other = (Redirect) obj; | ||
156 | + if (!Arrays.equals(this.targetAddress, other.targetAddress)) { | ||
157 | + return false; | ||
158 | + } | ||
159 | + if (!Arrays.equals(this.destinationAddress, other.destinationAddress)) { | ||
160 | + return false; | ||
161 | + } | ||
162 | + return true; | ||
163 | + } | ||
164 | +} |
1 | +/* | ||
2 | + * Copyright 2014 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | + | ||
18 | + | ||
19 | +package org.onlab.packet.ndp; | ||
20 | + | ||
21 | +import org.onlab.packet.BasePacket; | ||
22 | +import org.onlab.packet.Data; | ||
23 | +import org.onlab.packet.IPacket; | ||
24 | + | ||
25 | +import java.nio.ByteBuffer; | ||
26 | + | ||
27 | +/** | ||
28 | + * Implements ICMPv6 Router Advertisement packet format. (RFC 4861) | ||
29 | + */ | ||
30 | +public class RouterAdvertisement extends BasePacket { | ||
31 | + public static final byte HEADER_LENGTH = 12; // bytes | ||
32 | + | ||
33 | + protected byte currentHopLimit; | ||
34 | + protected byte mFlag; | ||
35 | + protected byte oFlag; | ||
36 | + protected short routerLifetime; | ||
37 | + protected int reachableTime; | ||
38 | + protected int retransmitTimer; | ||
39 | + | ||
40 | + /** | ||
41 | + * Gets current hop limit. | ||
42 | + * | ||
43 | + * @return the current hop limit | ||
44 | + */ | ||
45 | + public byte getCurrentHopLimit() { | ||
46 | + return this.currentHopLimit; | ||
47 | + } | ||
48 | + | ||
49 | + /** | ||
50 | + * Sets current hop limit. | ||
51 | + * | ||
52 | + * @param currentHopLimit the current hop limit to set | ||
53 | + * @return this | ||
54 | + */ | ||
55 | + public RouterAdvertisement setCurrentHopLimit(final byte currentHopLimit) { | ||
56 | + this.currentHopLimit = currentHopLimit; | ||
57 | + return this; | ||
58 | + } | ||
59 | + | ||
60 | + /** | ||
61 | + * Gets managed address configuration flag. | ||
62 | + * | ||
63 | + * @return the managed address configuration flag | ||
64 | + */ | ||
65 | + public byte getMFlag() { | ||
66 | + return this.mFlag; | ||
67 | + } | ||
68 | + | ||
69 | + /** | ||
70 | + * Sets managed address configuration flag. | ||
71 | + * | ||
72 | + * @param mFlag the managed address configuration flag to set | ||
73 | + * @return this | ||
74 | + */ | ||
75 | + public RouterAdvertisement setMFlag(final byte mFlag) { | ||
76 | + this.mFlag = mFlag; | ||
77 | + return this; | ||
78 | + } | ||
79 | + | ||
80 | + /** | ||
81 | + * Gets other configuration flag. | ||
82 | + * | ||
83 | + * @return the other configuration flag | ||
84 | + */ | ||
85 | + public byte getOFlag() { | ||
86 | + return this.oFlag; | ||
87 | + } | ||
88 | + | ||
89 | + /** | ||
90 | + * Sets other configuration flag. | ||
91 | + * | ||
92 | + * @param oFlag the other configuration flag to set | ||
93 | + * @return this | ||
94 | + */ | ||
95 | + public RouterAdvertisement setOFlag(final byte oFlag) { | ||
96 | + this.oFlag = oFlag; | ||
97 | + return this; | ||
98 | + } | ||
99 | + | ||
100 | + /** | ||
101 | + * Gets router lifetime. | ||
102 | + * | ||
103 | + * @return the router lifetime | ||
104 | + */ | ||
105 | + public short getRouterLifetime() { | ||
106 | + return this.routerLifetime; | ||
107 | + } | ||
108 | + | ||
109 | + /** | ||
110 | + * Sets router lifetime. | ||
111 | + * | ||
112 | + * @param routerLifetime the router lifetime to set | ||
113 | + * @return this | ||
114 | + */ | ||
115 | + public RouterAdvertisement setRouterLifetime(final short routerLifetime) { | ||
116 | + this.routerLifetime = routerLifetime; | ||
117 | + return this; | ||
118 | + } | ||
119 | + | ||
120 | + /** | ||
121 | + * Gets reachable time. | ||
122 | + * | ||
123 | + * @return the reachable time | ||
124 | + */ | ||
125 | + public int getReachableTime() { | ||
126 | + return this.reachableTime; | ||
127 | + } | ||
128 | + | ||
129 | + /** | ||
130 | + * Sets reachable time. | ||
131 | + * | ||
132 | + * @param reachableTime the reachable time to set | ||
133 | + * @return this | ||
134 | + */ | ||
135 | + public RouterAdvertisement setReachableTime(final int reachableTime) { | ||
136 | + this.reachableTime = reachableTime; | ||
137 | + return this; | ||
138 | + } | ||
139 | + | ||
140 | + /** | ||
141 | + * Gets retransmission timer. | ||
142 | + * | ||
143 | + * @return the retransmission timer | ||
144 | + */ | ||
145 | + public int getRetransmitTimer() { | ||
146 | + return this.retransmitTimer; | ||
147 | + } | ||
148 | + | ||
149 | + /** | ||
150 | + * Sets retransmission timer. | ||
151 | + * | ||
152 | + * @param retransmitTimer the retransmission timer to set | ||
153 | + * @return this | ||
154 | + */ | ||
155 | + public RouterAdvertisement setRetransmitTimer(final int retransmitTimer) { | ||
156 | + this.retransmitTimer = retransmitTimer; | ||
157 | + return this; | ||
158 | + } | ||
159 | + | ||
160 | + @Override | ||
161 | + public byte[] serialize() { | ||
162 | + byte[] payloadData = null; | ||
163 | + if (this.payload != null) { | ||
164 | + this.payload.setParent(this); | ||
165 | + payloadData = this.payload.serialize(); | ||
166 | + } | ||
167 | + | ||
168 | + int payloadLength = payloadData == null ? 0 : (short) payloadData.length; | ||
169 | + | ||
170 | + final byte[] data = new byte[HEADER_LENGTH + payloadLength]; | ||
171 | + final ByteBuffer bb = ByteBuffer.wrap(data); | ||
172 | + | ||
173 | + bb.put(this.currentHopLimit); | ||
174 | + bb.put((byte) ((this.mFlag & 0x1) << 7 | (this.oFlag & 0x1) << 6)); | ||
175 | + bb.putShort(routerLifetime); | ||
176 | + bb.putInt(reachableTime); | ||
177 | + bb.putInt(retransmitTimer); | ||
178 | + | ||
179 | + if (payloadData != null) { | ||
180 | + bb.put(payloadData); | ||
181 | + } | ||
182 | + | ||
183 | + return data; | ||
184 | + } | ||
185 | + | ||
186 | + @Override | ||
187 | + public IPacket deserialize(byte[] data, int offset, int length) { | ||
188 | + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); | ||
189 | + int bscratch; | ||
190 | + | ||
191 | + this.currentHopLimit = bb.get(); | ||
192 | + bscratch = bb.get(); | ||
193 | + this.mFlag = (byte) ((bscratch >> 7) & 0x1); | ||
194 | + this.oFlag = (byte) ((bscratch >> 6) & 0x1); | ||
195 | + this.routerLifetime = bb.getShort(); | ||
196 | + this.reachableTime = bb.getInt(); | ||
197 | + this.retransmitTimer = bb.getInt(); | ||
198 | + | ||
199 | + this.payload = new Data(); | ||
200 | + this.payload = this.payload.deserialize(data, bb.position(), bb.limit() | ||
201 | + - bb.position()); | ||
202 | + this.payload.setParent(this); | ||
203 | + | ||
204 | + return this; | ||
205 | + } | ||
206 | + | ||
207 | + /* | ||
208 | + * (non-Javadoc) | ||
209 | + * | ||
210 | + * @see java.lang.Object#hashCode() | ||
211 | + */ | ||
212 | + @Override | ||
213 | + public int hashCode() { | ||
214 | + final int prime = 5807; | ||
215 | + int result = super.hashCode(); | ||
216 | + result = prime * result + this.currentHopLimit; | ||
217 | + result = prime * result + this.mFlag; | ||
218 | + result = prime * result + this.oFlag; | ||
219 | + result = prime * result + this.routerLifetime; | ||
220 | + result = prime * result + this.reachableTime; | ||
221 | + result = prime * result + this.retransmitTimer; | ||
222 | + return result; | ||
223 | + } | ||
224 | + | ||
225 | + /* | ||
226 | + * (non-Javadoc) | ||
227 | + * | ||
228 | + * @see java.lang.Object#equals(java.lang.Object) | ||
229 | + */ | ||
230 | + @Override | ||
231 | + public boolean equals(final Object obj) { | ||
232 | + if (this == obj) { | ||
233 | + return true; | ||
234 | + } | ||
235 | + if (!super.equals(obj)) { | ||
236 | + return false; | ||
237 | + } | ||
238 | + if (!(obj instanceof RouterAdvertisement)) { | ||
239 | + return false; | ||
240 | + } | ||
241 | + final RouterAdvertisement other = (RouterAdvertisement) obj; | ||
242 | + if (this.currentHopLimit != other.currentHopLimit) { | ||
243 | + return false; | ||
244 | + } | ||
245 | + if (this.mFlag != other.mFlag) { | ||
246 | + return false; | ||
247 | + } | ||
248 | + if (this.oFlag != other.oFlag) { | ||
249 | + return false; | ||
250 | + } | ||
251 | + if (this.routerLifetime != other.routerLifetime) { | ||
252 | + return false; | ||
253 | + } | ||
254 | + if (this.reachableTime != other.reachableTime) { | ||
255 | + return false; | ||
256 | + } | ||
257 | + if (this.retransmitTimer != other.retransmitTimer) { | ||
258 | + return false; | ||
259 | + } | ||
260 | + return true; | ||
261 | + } | ||
262 | +} |
1 | +/* | ||
2 | + * Copyright 2014 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | + | ||
18 | + | ||
19 | +package org.onlab.packet.ndp; | ||
20 | + | ||
21 | +import org.onlab.packet.BasePacket; | ||
22 | +import org.onlab.packet.Data; | ||
23 | +import org.onlab.packet.IPacket; | ||
24 | + | ||
25 | +import java.nio.ByteBuffer; | ||
26 | + | ||
27 | +/** | ||
28 | + * Implements ICMPv6 Router Solicitation packet format. (RFC 4861) | ||
29 | + */ | ||
30 | +public class RouterSolicitation extends BasePacket { | ||
31 | + public static final byte HEADER_LENGTH = 4; // bytes | ||
32 | + | ||
33 | + @Override | ||
34 | + public byte[] serialize() { | ||
35 | + byte[] payloadData = null; | ||
36 | + if (this.payload != null) { | ||
37 | + this.payload.setParent(this); | ||
38 | + payloadData = this.payload.serialize(); | ||
39 | + } | ||
40 | + | ||
41 | + int payloadLength = payloadData == null ? 0 : (short) payloadData.length; | ||
42 | + | ||
43 | + final byte[] data = new byte[HEADER_LENGTH + payloadLength]; | ||
44 | + final ByteBuffer bb = ByteBuffer.wrap(data); | ||
45 | + | ||
46 | + bb.putInt(0); | ||
47 | + | ||
48 | + if (payloadData != null) { | ||
49 | + bb.put(payloadData); | ||
50 | + } | ||
51 | + | ||
52 | + return data; | ||
53 | + } | ||
54 | + | ||
55 | + @Override | ||
56 | + public IPacket deserialize(byte[] data, int offset, int length) { | ||
57 | + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); | ||
58 | + | ||
59 | + bb.getInt(); | ||
60 | + | ||
61 | + this.payload = new Data(); | ||
62 | + this.payload = this.payload.deserialize(data, bb.position(), bb.limit() | ||
63 | + - bb.position()); | ||
64 | + this.payload.setParent(this); | ||
65 | + | ||
66 | + return this; | ||
67 | + } | ||
68 | + | ||
69 | + /* | ||
70 | + * (non-Javadoc) | ||
71 | + * | ||
72 | + * @see java.lang.Object#hashCode() | ||
73 | + */ | ||
74 | + @Override | ||
75 | + public int hashCode() { | ||
76 | + return super.hashCode(); | ||
77 | + } | ||
78 | + | ||
79 | + /* | ||
80 | + * (non-Javadoc) | ||
81 | + * | ||
82 | + * @see java.lang.Object#equals(java.lang.Object) | ||
83 | + */ | ||
84 | + @Override | ||
85 | + public boolean equals(final Object obj) { | ||
86 | + if (this == obj) { | ||
87 | + return true; | ||
88 | + } | ||
89 | + if (!super.equals(obj)) { | ||
90 | + return false; | ||
91 | + } | ||
92 | + if (!(obj instanceof RouterSolicitation)) { | ||
93 | + return false; | ||
94 | + } | ||
95 | + return true; | ||
96 | + } | ||
97 | +} |
1 | +/* | ||
2 | + * Copyright 2014 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +/** | ||
18 | + * Utilities for decoding and encoding packets of Neighbor Discovery Protocol | ||
19 | + * for IPv6 (RFC 4861). | ||
20 | + */ | ||
21 | +package org.onlab.packet.ndp; |
... | @@ -16,13 +16,17 @@ | ... | @@ -16,13 +16,17 @@ |
16 | 16 | ||
17 | 17 | ||
18 | 18 | ||
19 | -package org.onlab.packet; | 19 | +package org.onlab.packet.ndp; |
20 | 20 | ||
21 | import org.junit.BeforeClass; | 21 | import org.junit.BeforeClass; |
22 | import org.junit.Test; | 22 | import org.junit.Test; |
23 | +import org.onlab.packet.Data; | ||
23 | 24 | ||
24 | import static org.hamcrest.Matchers.is; | 25 | import static org.hamcrest.Matchers.is; |
25 | -import static org.junit.Assert.*; | 26 | +import static org.junit.Assert.assertArrayEquals; |
27 | +import static org.junit.Assert.assertTrue; | ||
28 | +import static org.junit.Assert.assertFalse; | ||
29 | +import static org.junit.Assert.assertThat; | ||
26 | 30 | ||
27 | /** | 31 | /** |
28 | * Tests for class {@link NeighborAdvertisement}. | 32 | * Tests for class {@link NeighborAdvertisement}. |
... | @@ -61,6 +65,7 @@ public class NeighborAdvertisementTest { | ... | @@ -61,6 +65,7 @@ public class NeighborAdvertisementTest { |
61 | na.setSolicitedFlag((byte) 1); | 65 | na.setSolicitedFlag((byte) 1); |
62 | na.setOverrideFlag((byte) 1); | 66 | na.setOverrideFlag((byte) 1); |
63 | na.setTargetAddress(TARGET_ADDRESS); | 67 | na.setTargetAddress(TARGET_ADDRESS); |
68 | + na.setPayload(data); | ||
64 | 69 | ||
65 | assertArrayEquals(na.serialize(), bytePacket); | 70 | assertArrayEquals(na.serialize(), bytePacket); |
66 | } | 71 | } | ... | ... |
... | @@ -16,15 +16,17 @@ | ... | @@ -16,15 +16,17 @@ |
16 | 16 | ||
17 | 17 | ||
18 | 18 | ||
19 | -package org.onlab.packet; | 19 | +package org.onlab.packet.ndp; |
20 | 20 | ||
21 | import org.junit.BeforeClass; | 21 | import org.junit.BeforeClass; |
22 | import org.junit.Test; | 22 | import org.junit.Test; |
23 | +import org.onlab.packet.Data; | ||
23 | 24 | ||
24 | -import static org.junit.Assert.*; | 25 | +import static org.junit.Assert.assertArrayEquals; |
25 | - | 26 | +import static org.junit.Assert.assertTrue; |
27 | +import static org.junit.Assert.assertFalse; | ||
26 | /** | 28 | /** |
27 | - * Tests for class {@link org.onlab.packet.NeighborSolicitation}. | 29 | + * Tests for class {@link NeighborSolicitation}. |
28 | */ | 30 | */ |
29 | public class NeighborSolicitationTest { | 31 | public class NeighborSolicitationTest { |
30 | private static final byte[] TARGET_ADDRESS = { | 32 | private static final byte[] TARGET_ADDRESS = { |
... | @@ -61,6 +63,7 @@ public class NeighborSolicitationTest { | ... | @@ -61,6 +63,7 @@ public class NeighborSolicitationTest { |
61 | public void testSerialize() { | 63 | public void testSerialize() { |
62 | NeighborSolicitation ns = new NeighborSolicitation(); | 64 | NeighborSolicitation ns = new NeighborSolicitation(); |
63 | ns.setTargetAddress(TARGET_ADDRESS); | 65 | ns.setTargetAddress(TARGET_ADDRESS); |
66 | + ns.setPayload(data); | ||
64 | 67 | ||
65 | assertArrayEquals(ns.serialize(), bytePacket); | 68 | assertArrayEquals(ns.serialize(), bytePacket); |
66 | } | 69 | } | ... | ... |
1 | +/* | ||
2 | + * Copyright 2014 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | + | ||
18 | + | ||
19 | +package org.onlab.packet.ndp; | ||
20 | + | ||
21 | +import org.junit.BeforeClass; | ||
22 | +import org.junit.Test; | ||
23 | +import org.onlab.packet.Data; | ||
24 | + | ||
25 | +import static org.junit.Assert.assertArrayEquals; | ||
26 | +import static org.junit.Assert.assertTrue; | ||
27 | +import static org.junit.Assert.assertFalse; | ||
28 | + | ||
29 | +/** | ||
30 | + * Tests for class {@link Redirect}. | ||
31 | + */ | ||
32 | +public class RedirectTest { | ||
33 | + private static final byte[] TARGET_ADDRESS = { | ||
34 | + (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, | ||
35 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 | ||
36 | + }; | ||
37 | + private static final byte[] DESTINATION_ADDRESS = { | ||
38 | + (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, | ||
39 | + (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce | ||
40 | + }; | ||
41 | + private static final byte[] DESTINATION_ADDRESS2 = { | ||
42 | + (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, | ||
43 | + (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8 | ||
44 | + }; | ||
45 | + private static Data data; | ||
46 | + private static byte[] bytePacket; | ||
47 | + | ||
48 | + @BeforeClass | ||
49 | + public static void setUpBeforeClass() throws Exception { | ||
50 | + data = new Data(); | ||
51 | + data.setData("".getBytes()); | ||
52 | + | ||
53 | + byte[] bytePayload = data.serialize(); | ||
54 | + byte[] byteHeader = { | ||
55 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, | ||
56 | + (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, | ||
57 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, | ||
58 | + (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, | ||
59 | + (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce | ||
60 | + }; | ||
61 | + bytePacket = new byte[byteHeader.length + bytePayload.length]; | ||
62 | + System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); | ||
63 | + System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); | ||
64 | + } | ||
65 | + | ||
66 | + /** | ||
67 | + * Tests serialize and setters. | ||
68 | + */ | ||
69 | + @Test | ||
70 | + public void testSerialize() { | ||
71 | + Redirect rd = new Redirect(); | ||
72 | + rd.setTargetAddress(TARGET_ADDRESS); | ||
73 | + rd.setDestinationAddress(DESTINATION_ADDRESS); | ||
74 | + rd.setPayload(data); | ||
75 | + | ||
76 | + assertArrayEquals(rd.serialize(), bytePacket); | ||
77 | + } | ||
78 | + | ||
79 | + /** | ||
80 | + * Tests deserialize and getters. | ||
81 | + */ | ||
82 | + @Test | ||
83 | + public void testDeserialize() { | ||
84 | + Redirect rd = new Redirect(); | ||
85 | + rd.deserialize(bytePacket, 0, bytePacket.length); | ||
86 | + | ||
87 | + assertArrayEquals(rd.getTargetAddress(), TARGET_ADDRESS); | ||
88 | + assertArrayEquals(rd.getDestinationAddress(), DESTINATION_ADDRESS); | ||
89 | + } | ||
90 | + | ||
91 | + /** | ||
92 | + * Tests comparator. | ||
93 | + */ | ||
94 | + @Test | ||
95 | + public void testEqual() { | ||
96 | + Redirect rd1 = new Redirect(); | ||
97 | + rd1.setTargetAddress(TARGET_ADDRESS); | ||
98 | + rd1.setDestinationAddress(DESTINATION_ADDRESS); | ||
99 | + | ||
100 | + Redirect rd2 = new Redirect(); | ||
101 | + rd2.setTargetAddress(TARGET_ADDRESS); | ||
102 | + rd2.setDestinationAddress(DESTINATION_ADDRESS2); | ||
103 | + | ||
104 | + assertTrue(rd1.equals(rd1)); | ||
105 | + assertFalse(rd1.equals(rd2)); | ||
106 | + } | ||
107 | +} |
1 | +/* | ||
2 | + * Copyright 2014 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | + | ||
18 | + | ||
19 | +package org.onlab.packet.ndp; | ||
20 | + | ||
21 | +import org.junit.BeforeClass; | ||
22 | +import org.junit.Test; | ||
23 | +import org.onlab.packet.Data; | ||
24 | + | ||
25 | +import static org.hamcrest.Matchers.is; | ||
26 | +import static org.junit.Assert.assertArrayEquals; | ||
27 | +import static org.junit.Assert.assertFalse; | ||
28 | +import static org.junit.Assert.assertThat; | ||
29 | +import static org.junit.Assert.assertTrue; | ||
30 | + | ||
31 | +/** | ||
32 | + * Tests for class {@link RouterAdvertisement}. | ||
33 | + */ | ||
34 | +public class RouterAdvertisementTest { | ||
35 | + private static Data data; | ||
36 | + private static byte[] bytePacket; | ||
37 | + | ||
38 | + @BeforeClass | ||
39 | + public static void setUpBeforeClass() throws Exception { | ||
40 | + data = new Data(); | ||
41 | + data.setData("".getBytes()); | ||
42 | + | ||
43 | + byte[] bytePayload = data.serialize(); | ||
44 | + byte[] byteHeader = { | ||
45 | + (byte) 0x03, (byte) 0xc0, (byte) 0x02, (byte) 0x58, | ||
46 | + (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0xe8, | ||
47 | + (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xf4 | ||
48 | + }; | ||
49 | + bytePacket = new byte[byteHeader.length + bytePayload.length]; | ||
50 | + System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); | ||
51 | + System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); | ||
52 | + } | ||
53 | + | ||
54 | + /** | ||
55 | + * Tests serialize and setters. | ||
56 | + */ | ||
57 | + @Test | ||
58 | + public void testSerialize() { | ||
59 | + RouterAdvertisement ra = new RouterAdvertisement(); | ||
60 | + ra.setCurrentHopLimit((byte) 3); | ||
61 | + ra.setMFlag((byte) 1); | ||
62 | + ra.setOFlag((byte) 1); | ||
63 | + ra.setRouterLifetime((short) 0x258); | ||
64 | + ra.setReachableTime(0x3e8); | ||
65 | + ra.setRetransmitTimer(0x1f4); | ||
66 | + ra.setPayload(data); | ||
67 | + | ||
68 | + assertArrayEquals(ra.serialize(), bytePacket); | ||
69 | + } | ||
70 | + | ||
71 | + /** | ||
72 | + * Tests deserialize and getters. | ||
73 | + */ | ||
74 | + @Test | ||
75 | + public void testDeserialize() { | ||
76 | + RouterAdvertisement ra = new RouterAdvertisement(); | ||
77 | + ra.deserialize(bytePacket, 0, bytePacket.length); | ||
78 | + | ||
79 | + assertThat(ra.getCurrentHopLimit(), is((byte) 3)); | ||
80 | + assertThat(ra.getMFlag(), is((byte) 1)); | ||
81 | + assertThat(ra.getOFlag(), is((byte) 1)); | ||
82 | + assertThat(ra.getRouterLifetime(), is((short) 0x258)); | ||
83 | + assertThat(ra.getReachableTime(), is(0x3e8)); | ||
84 | + assertThat(ra.getRetransmitTimer(), is(0x1f4)); | ||
85 | + } | ||
86 | + | ||
87 | + /** | ||
88 | + * Tests comparator. | ||
89 | + */ | ||
90 | + @Test | ||
91 | + public void testEqual() { | ||
92 | + RouterAdvertisement ra1 = new RouterAdvertisement(); | ||
93 | + ra1.setCurrentHopLimit((byte) 3); | ||
94 | + ra1.setMFlag((byte) 1); | ||
95 | + ra1.setOFlag((byte) 1); | ||
96 | + ra1.setRouterLifetime((short) 0x258); | ||
97 | + ra1.setReachableTime(0x3e8); | ||
98 | + ra1.setRetransmitTimer(0x1f4); | ||
99 | + | ||
100 | + RouterAdvertisement ra2 = new RouterAdvertisement(); | ||
101 | + ra2.setCurrentHopLimit((byte) 3); | ||
102 | + ra2.setMFlag((byte) 0); | ||
103 | + ra2.setOFlag((byte) 0); | ||
104 | + ra2.setRouterLifetime((short) 0x1f4); | ||
105 | + ra2.setReachableTime(0x3e8); | ||
106 | + ra2.setRetransmitTimer(0x1f4); | ||
107 | + | ||
108 | + assertTrue(ra1.equals(ra1)); | ||
109 | + assertFalse(ra1.equals(ra2)); | ||
110 | + } | ||
111 | +} |
1 | +/* | ||
2 | + * Copyright 2014 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | + | ||
18 | + | ||
19 | +package org.onlab.packet.ndp; | ||
20 | + | ||
21 | +import org.junit.BeforeClass; | ||
22 | +import org.junit.Test; | ||
23 | +import org.onlab.packet.Data; | ||
24 | + | ||
25 | +import static org.junit.Assert.assertArrayEquals; | ||
26 | +import static org.junit.Assert.assertTrue; | ||
27 | + | ||
28 | +/** | ||
29 | + * Tests for class {@link RouterSolicitation}. | ||
30 | + */ | ||
31 | +public class RouterSolicitationTest { | ||
32 | + private static final byte[] OPTION = { | ||
33 | + (byte) 0x01, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01 | ||
34 | + }; | ||
35 | + private static Data data; | ||
36 | + private static byte[] bytePacket; | ||
37 | + | ||
38 | + @BeforeClass | ||
39 | + public static void setUpBeforeClass() throws Exception { | ||
40 | + data = new Data(); | ||
41 | + data.setData(OPTION); | ||
42 | + | ||
43 | + byte[] bytePayload = data.serialize(); | ||
44 | + byte[] byteHeader = {(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}; | ||
45 | + bytePacket = new byte[byteHeader.length + bytePayload.length]; | ||
46 | + System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); | ||
47 | + System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); | ||
48 | + } | ||
49 | + | ||
50 | + /** | ||
51 | + * Tests serialize and setters. | ||
52 | + */ | ||
53 | + @Test | ||
54 | + public void testSerialize() { | ||
55 | + RouterSolicitation rs = new RouterSolicitation(); | ||
56 | + rs.setPayload(data); | ||
57 | + | ||
58 | + assertArrayEquals(rs.serialize(), bytePacket); | ||
59 | + } | ||
60 | + | ||
61 | + /** | ||
62 | + * Tests deserialize and getters. | ||
63 | + */ | ||
64 | + @Test | ||
65 | + public void testDeserialize() { | ||
66 | + RouterSolicitation rs = new RouterSolicitation(); | ||
67 | + rs.deserialize(bytePacket, 0, bytePacket.length); | ||
68 | + } | ||
69 | + | ||
70 | + /** | ||
71 | + * Tests comparator. | ||
72 | + */ | ||
73 | + @Test | ||
74 | + public void testEqual() { | ||
75 | + RouterSolicitation rs1 = new RouterSolicitation(); | ||
76 | + | ||
77 | + assertTrue(rs1.equals(rs1)); | ||
78 | + } | ||
79 | +} |
-
Please register or login to post a comment