Charles M.C. Chan
Committed by Pavlin Radoslavov

ONOS-512: Implement IPv6 Extension Headers

* Create IExtensionHeader interface
    - setNextHeader, getNextHeader interface for all extension header classes
        - Except EncapSecurityPayload, in which the nextHeader field is encrypted

* Create BaseOptions class
    - Super class of HopByHopOptions and DestinationOptions, since these two are very similar

* Implement following classes with unit test
    - HopByHopOptions
    - DestinationOptions
    - Fragment
    - Routing
    - Authentication
    - EncapSecurityPayload

Change-Id: If65894eccf20ac90f04bc2b0cb42aac6dd5a9674
1 /* 1 /*
2 - * Copyright 2014 Open Networking Laboratory 2 + * Copyright 2014-2015 Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -18,6 +18,13 @@ ...@@ -18,6 +18,13 @@
18 18
19 package org.onlab.packet; 19 package org.onlab.packet;
20 20
21 +import org.onlab.packet.ipv6.Authentication;
22 +import org.onlab.packet.ipv6.DestinationOptions;
23 +import org.onlab.packet.ipv6.EncapSecurityPayload;
24 +import org.onlab.packet.ipv6.Fragment;
25 +import org.onlab.packet.ipv6.IExtensionHeader;
26 +import org.onlab.packet.ipv6.HopByHopOptions;
27 +import org.onlab.packet.ipv6.Routing;
21 import java.nio.ByteBuffer; 28 import java.nio.ByteBuffer;
22 import java.util.Arrays; 29 import java.util.Arrays;
23 import java.util.HashMap; 30 import java.util.HashMap;
...@@ -26,13 +33,20 @@ import java.util.Map; ...@@ -26,13 +33,20 @@ import java.util.Map;
26 /** 33 /**
27 * Implements IPv6 packet format. (RFC 2460) 34 * Implements IPv6 packet format. (RFC 2460)
28 */ 35 */
29 -public class IPv6 extends BasePacket { 36 +public class IPv6 extends BasePacket implements IExtensionHeader {
30 public static final byte FIXED_HEADER_LENGTH = 40; // bytes 37 public static final byte FIXED_HEADER_LENGTH = 40; // bytes
31 38
32 - // TODO: Implement extension header.
33 public static final byte PROTOCOL_TCP = 0x6; 39 public static final byte PROTOCOL_TCP = 0x6;
34 public static final byte PROTOCOL_UDP = 0x11; 40 public static final byte PROTOCOL_UDP = 0x11;
35 public static final byte PROTOCOL_ICMP6 = 0x3A; 41 public static final byte PROTOCOL_ICMP6 = 0x3A;
42 + public static final byte PROTOCOL_HOPOPT = 0x00;
43 + public static final byte PROTOCOL_ROUTING = 0x2B;
44 + public static final byte PROTOCOL_FRAG = 0x2C;
45 + public static final byte PROTOCOL_ESP = 0x32;
46 + public static final byte PROTOCOL_AH = 0x33;
47 + public static final byte PROTOCOL_DSTOPT = 0x3C;
48 +
49 +
36 public static final Map<Byte, Class<? extends IPacket>> PROTOCOL_CLASS_MAP = 50 public static final Map<Byte, Class<? extends IPacket>> PROTOCOL_CLASS_MAP =
37 new HashMap<>(); 51 new HashMap<>();
38 52
...@@ -40,6 +54,12 @@ public class IPv6 extends BasePacket { ...@@ -40,6 +54,12 @@ public class IPv6 extends BasePacket {
40 IPv6.PROTOCOL_CLASS_MAP.put(IPv6.PROTOCOL_ICMP6, ICMP6.class); 54 IPv6.PROTOCOL_CLASS_MAP.put(IPv6.PROTOCOL_ICMP6, ICMP6.class);
41 IPv6.PROTOCOL_CLASS_MAP.put(IPv6.PROTOCOL_TCP, TCP.class); 55 IPv6.PROTOCOL_CLASS_MAP.put(IPv6.PROTOCOL_TCP, TCP.class);
42 IPv6.PROTOCOL_CLASS_MAP.put(IPv6.PROTOCOL_UDP, UDP.class); 56 IPv6.PROTOCOL_CLASS_MAP.put(IPv6.PROTOCOL_UDP, UDP.class);
57 + IPv6.PROTOCOL_CLASS_MAP.put(IPv6.PROTOCOL_HOPOPT, HopByHopOptions.class);
58 + IPv6.PROTOCOL_CLASS_MAP.put(IPv6.PROTOCOL_ROUTING, Routing.class);
59 + IPv6.PROTOCOL_CLASS_MAP.put(IPv6.PROTOCOL_FRAG, Fragment.class);
60 + IPv6.PROTOCOL_CLASS_MAP.put(IPv6.PROTOCOL_ESP, EncapSecurityPayload.class);
61 + IPv6.PROTOCOL_CLASS_MAP.put(IPv6.PROTOCOL_AH, Authentication.class);
62 + IPv6.PROTOCOL_CLASS_MAP.put(IPv6.PROTOCOL_DSTOPT, DestinationOptions.class);
43 } 63 }
44 64
45 protected byte version; 65 protected byte version;
...@@ -119,21 +139,12 @@ public class IPv6 extends BasePacket { ...@@ -119,21 +139,12 @@ public class IPv6 extends BasePacket {
119 return this; 139 return this;
120 } 140 }
121 141
122 - /** 142 + @Override
123 - * Gets next header.
124 - *
125 - * @return the next header
126 - */
127 public byte getNextHeader() { 143 public byte getNextHeader() {
128 return this.nextHeader; 144 return this.nextHeader;
129 } 145 }
130 146
131 - /** 147 + @Override
132 - * Sets next header.
133 - *
134 - * @param nextHeader the next header to set
135 - * @return this
136 - */
137 public IPv6 setNextHeader(final byte nextHeader) { 148 public IPv6 setNextHeader(final byte nextHeader) {
138 this.nextHeader = nextHeader; 149 this.nextHeader = nextHeader;
139 return this; 150 return this;
......
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +import org.onlab.packet.BasePacket;
20 +import org.onlab.packet.Data;
21 +import org.onlab.packet.IPacket;
22 +import org.onlab.packet.IPv6;
23 +import java.nio.ByteBuffer;
24 +import java.util.Arrays;
25 +
26 +/**
27 + * Implements IPv6 authentication extension header format. (RFC 4302)
28 + */
29 +public class Authentication extends BasePacket implements IExtensionHeader {
30 + public static final byte FIXED_HEADER_LENGTH = 12; // bytes
31 + public static final byte LENGTH_UNIT = 4; // bytes per unit
32 + public static final byte MINUS = 2;
33 +
34 + protected byte nextHeader;
35 + protected byte payloadLength;
36 + protected int securityParamIndex;
37 + protected int sequence;
38 + protected byte[] integrityCheck;
39 +
40 + @Override
41 + public byte getNextHeader() {
42 + return this.nextHeader;
43 + }
44 +
45 + @Override
46 + public Authentication setNextHeader(final byte nextHeader) {
47 + this.nextHeader = nextHeader;
48 + return this;
49 + }
50 +
51 + /**
52 + * Gets the payload length of this header.
53 + *
54 + * @return the payload length
55 + */
56 + public byte getPayloadLength() {
57 + return this.payloadLength;
58 + }
59 +
60 + /**
61 + * Sets the payload length of this header.
62 + *
63 + * @param payloadLength the payload length to set
64 + * @return this
65 + */
66 + public Authentication setPayloadLength(final byte payloadLength) {
67 + this.payloadLength = payloadLength;
68 + return this;
69 + }
70 +
71 + /**
72 + * Gets the security parameter index of this header.
73 + *
74 + * @return the security parameter index
75 + */
76 + public int getSecurityParamIndex() {
77 + return this.securityParamIndex;
78 + }
79 +
80 + /**
81 + * Sets the security parameter index of this header.
82 + *
83 + * @param securityParamIndex the security parameter index to set
84 + * @return this
85 + */
86 + public Authentication setSecurityParamIndex(final int securityParamIndex) {
87 + this.securityParamIndex = securityParamIndex;
88 + return this;
89 + }
90 +
91 + /**
92 + * Gets the sequence number of this header.
93 + *
94 + * @return the sequence number
95 + */
96 + public int getSequence() {
97 + return this.sequence;
98 + }
99 +
100 + /**
101 + * Sets the sequence number of this header.
102 + *
103 + * @param sequence the sequence number to set
104 + * @return this
105 + */
106 + public Authentication setSequence(final int sequence) {
107 + this.sequence = sequence;
108 + return this;
109 + }
110 +
111 + /**
112 + * Gets the integrity check value of this header.
113 + *
114 + * @return the integrity check value
115 + */
116 + public byte[] getIntegrityCheck() {
117 + return this.integrityCheck;
118 + }
119 +
120 + /**
121 + * Sets the integrity check value of this header.
122 + *
123 + * @param integrityCheck the integrity check value to set
124 + * @return this
125 + */
126 + public Authentication setIngegrityCheck(final byte[] integrityCheck) {
127 + this.integrityCheck =
128 + Arrays.copyOfRange(integrityCheck, 0, integrityCheck.length);
129 + return this;
130 + }
131 +
132 + /**
133 + * Gets the total length of this header.
134 + * According to spec, payload length should be the total length of this AH
135 + * in 4-octet unit, minus 2
136 + *
137 + * @return the total length
138 + */
139 + public int getTotalLength() {
140 + return (this.payloadLength + MINUS) * LENGTH_UNIT;
141 + }
142 +
143 + @Override
144 + public byte[] serialize() {
145 + byte[] payloadData = null;
146 + if (this.payload != null) {
147 + this.payload.setParent(this);
148 + payloadData = this.payload.serialize();
149 + }
150 +
151 + int headerLength = FIXED_HEADER_LENGTH + integrityCheck.length;
152 + int payloadLength = 0;
153 + if (payloadData != null) {
154 + payloadLength = payloadData.length;
155 + }
156 +
157 + final byte[] data = new byte[headerLength + payloadLength];
158 + final ByteBuffer bb = ByteBuffer.wrap(data);
159 +
160 + bb.put(this.nextHeader);
161 + bb.put(this.payloadLength);
162 + bb.putShort((short) 0);
163 + bb.putInt(this.securityParamIndex);
164 + bb.putInt(this.sequence);
165 + bb.put(this.integrityCheck, 0, integrityCheck.length);
166 +
167 + if (payloadData != null) {
168 + bb.put(payloadData);
169 + }
170 +
171 + if (this.parent != null && this.parent instanceof IExtensionHeader) {
172 + ((IExtensionHeader) this.parent).setNextHeader(IPv6.PROTOCOL_AH);
173 + }
174 + return data;
175 + }
176 +
177 + @Override
178 + public IPacket deserialize(byte[] data, int offset, int length) {
179 + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
180 + this.nextHeader = bb.get();
181 + this.payloadLength = bb.get();
182 + bb.getShort();
183 + this.securityParamIndex = bb.getInt();
184 + this.sequence = bb.getInt();
185 + int icvLength = getTotalLength() - FIXED_HEADER_LENGTH;
186 + this.integrityCheck = new byte[icvLength];
187 + bb.get(this.integrityCheck, 0, icvLength);
188 +
189 + IPacket payload;
190 + if (IPv6.PROTOCOL_CLASS_MAP.containsKey(this.nextHeader)) {
191 + final Class<? extends IPacket> clazz = IPv6.PROTOCOL_CLASS_MAP
192 + .get(this.nextHeader);
193 + try {
194 + payload = clazz.newInstance();
195 + } catch (final Exception e) {
196 + throw new RuntimeException(
197 + "Error parsing payload for Authentication packet", e);
198 + }
199 + } else {
200 + payload = new Data();
201 + }
202 + this.payload = payload.deserialize(data, bb.position(),
203 + bb.limit() - bb.position());
204 + this.payload.setParent(this);
205 +
206 + return this;
207 + }
208 +
209 + /*
210 + * (non-Javadoc)
211 + *
212 + * @see java.lang.Object#hashCode()
213 + */
214 + @Override
215 + public int hashCode() {
216 + final int prime = 5807;
217 + int result = super.hashCode();
218 + result = prime * result + this.nextHeader;
219 + result = prime * result + this.payloadLength;
220 + result = prime * result + this.securityParamIndex;
221 + result = prime * result + this.sequence;
222 + for (byte b : this.integrityCheck) {
223 + result = prime * result + b;
224 + }
225 + return result;
226 + }
227 +
228 + /*
229 + * (non-Javadoc)
230 + *
231 + * @see java.lang.Object#equals(java.lang.Object)
232 + */
233 + @Override
234 + public boolean equals(final Object obj) {
235 + if (this == obj) {
236 + return true;
237 + }
238 + if (!super.equals(obj)) {
239 + return false;
240 + }
241 + if (!(obj instanceof Authentication)) {
242 + return false;
243 + }
244 + final Authentication other = (Authentication) obj;
245 + if (this.nextHeader != other.nextHeader) {
246 + return false;
247 + }
248 + if (this.payloadLength != other.payloadLength) {
249 + return false;
250 + }
251 + if (this.securityParamIndex != other.securityParamIndex) {
252 + return false;
253 + }
254 + if (this.sequence != other.sequence) {
255 + return false;
256 + }
257 + if (!Arrays.equals(this.integrityCheck, other.integrityCheck)) {
258 + return false;
259 + }
260 + return true;
261 + }
262 +}
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +import org.onlab.packet.BasePacket;
20 +import org.onlab.packet.Data;
21 +import org.onlab.packet.IPacket;
22 +import org.onlab.packet.IPv6;
23 +
24 +import java.nio.ByteBuffer;
25 +import java.util.Arrays;
26 +
27 +/**
28 + * Base class for hop-by-hop options and destination options.
29 + */
30 +public class BaseOptions extends BasePacket implements IExtensionHeader {
31 + public static final byte FIXED_HEADER_LENGTH = 2; // bytes
32 + public static final byte FIXED_OPTIONS_LENGTH = 6; // bytes
33 + public static final byte LENGTH_UNIT = 8; // bytes per unit
34 +
35 + protected byte nextHeader;
36 + protected byte headerExtLength;
37 + protected byte[] options;
38 + protected byte type;
39 +
40 + @Override
41 + public byte getNextHeader() {
42 + return this.nextHeader;
43 + }
44 +
45 + @Override
46 + public BaseOptions setNextHeader(final byte nextHeader) {
47 + this.nextHeader = nextHeader;
48 + return this;
49 + }
50 +
51 + /**
52 + * Gets the extension length of this header.
53 + *
54 + * @return header length
55 + */
56 + public byte getHeaderExtLength() {
57 + return this.headerExtLength;
58 + }
59 +
60 + /**
61 + * Sets the extension length of this header.
62 + *
63 + * @param headerExtLength the header length to set
64 + * @return this
65 + */
66 + public BaseOptions setHeaderExtLength(final byte headerExtLength) {
67 + this.headerExtLength = headerExtLength;
68 + return this;
69 + }
70 +
71 + /**
72 + * Gets the options.
73 + *
74 + * @return the options
75 + */
76 + public byte[] getOptions() {
77 + return this.options;
78 + }
79 +
80 + /**
81 + * Sets the options.
82 + *
83 + * @param options the options to set
84 + * @return this
85 + */
86 + public BaseOptions setOptions(final byte[] options) {
87 + this.options =
88 + Arrays.copyOfRange(options, 0, options.length);
89 + return this;
90 + }
91 +
92 + /**
93 + * Gets the type of this option.
94 + *
95 + * @return the type
96 + */
97 + protected byte getType() {
98 + return this.type;
99 + }
100 +
101 + /**
102 + * Sets the type of this option.
103 + * Must be either IPv6.PROTOCOL_HOPOPT or IPv6.PROTOCOL_DSTOPT
104 + *
105 + * @param type the type to set
106 + * @return this
107 + */
108 + protected BaseOptions setType(final byte type) {
109 + this.type = type;
110 + return this;
111 + }
112 +
113 + @Override
114 + public byte[] serialize() {
115 + byte[] payloadData = null;
116 + if (this.payload != null) {
117 + this.payload.setParent(this);
118 + payloadData = this.payload.serialize();
119 + }
120 +
121 + int headerLength = FIXED_HEADER_LENGTH + options.length;
122 + int payloadLength = 0;
123 + if (payloadData != null) {
124 + payloadLength = payloadData.length;
125 + }
126 +
127 + final byte[] data = new byte[headerLength + payloadLength];
128 + final ByteBuffer bb = ByteBuffer.wrap(data);
129 +
130 + bb.put(this.nextHeader);
131 + bb.put(this.headerExtLength);
132 + bb.put(this.options, 0, options.length);
133 +
134 + if (payloadData != null) {
135 + bb.put(payloadData);
136 + }
137 +
138 + if (this.parent != null && this.parent instanceof IExtensionHeader) {
139 + ((IExtensionHeader) this.parent).setNextHeader(this.type);
140 + }
141 + return data;
142 + }
143 +
144 + @Override
145 + public IPacket deserialize(byte[] data, int offset, int length) {
146 + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
147 + this.nextHeader = bb.get();
148 + this.headerExtLength = bb.get();
149 + int optionLength =
150 + FIXED_OPTIONS_LENGTH + LENGTH_UNIT * this.headerExtLength;
151 + this.options = new byte[optionLength];
152 + bb.get(this.options, 0, optionLength);
153 +
154 + IPacket payload;
155 + if (IPv6.PROTOCOL_CLASS_MAP.containsKey(this.nextHeader)) {
156 + final Class<? extends IPacket> clazz = IPv6.PROTOCOL_CLASS_MAP
157 + .get(this.nextHeader);
158 + try {
159 + payload = clazz.newInstance();
160 + } catch (final Exception e) {
161 + throw new RuntimeException(
162 + "Error parsing payload for BaseOptions packet", e);
163 + }
164 + } else {
165 + payload = new Data();
166 + }
167 + this.payload = payload.deserialize(data, bb.position(),
168 + bb.limit() - bb.position());
169 + this.payload.setParent(this);
170 +
171 + return this;
172 + }
173 +
174 + /*
175 + * (non-Javadoc)
176 + *
177 + * @see java.lang.Object#hashCode()
178 + */
179 + @Override
180 + public int hashCode() {
181 + final int prime = 5807;
182 + int result = super.hashCode();
183 + result = prime * result + this.nextHeader;
184 + result = prime * result + this.headerExtLength;
185 + for (byte b : this.options) {
186 + result = prime * result + b;
187 + }
188 + return result;
189 + }
190 +
191 + /*
192 + * (non-Javadoc)
193 + *
194 + * @see java.lang.Object#equals(java.lang.Object)
195 + */
196 + @Override
197 + public boolean equals(final Object obj) {
198 + if (this == obj) {
199 + return true;
200 + }
201 + if (!super.equals(obj)) {
202 + return false;
203 + }
204 + if (!(obj instanceof BaseOptions)) {
205 + return false;
206 + }
207 + final BaseOptions other = (BaseOptions) obj;
208 + if (this.nextHeader != other.nextHeader) {
209 + return false;
210 + }
211 + if (this.headerExtLength != other.headerExtLength) {
212 + return false;
213 + }
214 + if (!Arrays.equals(this.options, other.options)) {
215 + return false;
216 + }
217 + if (this.type != other.type) {
218 + return false;
219 + }
220 + return true;
221 + }
222 +}
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +import org.onlab.packet.IPv6;
20 +
21 +/**
22 + * Implements IPv6 Destination Options extension header format. (RFC 2460)
23 + */
24 +public class DestinationOptions extends BaseOptions {
25 + public DestinationOptions() {
26 + super();
27 + this.setType(IPv6.PROTOCOL_DSTOPT);
28 + }
29 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +import org.onlab.packet.BasePacket;
20 +import org.onlab.packet.Data;
21 +import org.onlab.packet.IPacket;
22 +import org.onlab.packet.IPv6;
23 +import java.nio.ByteBuffer;
24 +
25 +/**
26 + * Implements IPv6 Encapsulating Security Payload (ESP) extension header format.
27 + * (RFC 4303)
28 + */
29 +public class EncapSecurityPayload extends BasePacket {
30 + public static final byte HEADER_LENGTH = 8; // bytes
31 +
32 + protected int securityParamIndex;
33 + protected int sequence;
34 + //
35 + // NOTE: The remaining fields including payload data, padding length and
36 + // next header are encrypted and all considered as a payload of ESP.
37 + //
38 +
39 + /**
40 + * Gets the security parameter index of this header.
41 + *
42 + * @return the security parameter index
43 + */
44 + public int getSecurityParamIndex() {
45 + return this.securityParamIndex;
46 + }
47 +
48 + /**
49 + * Sets the security parameter index of this header.
50 + *
51 + * @param securityParamIndex the security parameter index to set
52 + * @return this
53 + */
54 + public EncapSecurityPayload setSecurityParamIndex(final int securityParamIndex) {
55 + this.securityParamIndex = securityParamIndex;
56 + return this;
57 + }
58 +
59 + /**
60 + * Gets the sequence number of this header.
61 + *
62 + * @return the sequence number
63 + */
64 + public int getSequence() {
65 + return this.sequence;
66 + }
67 +
68 + /**
69 + * Sets the sequence number of this header.
70 + *
71 + * @param sequence the sequence number to set
72 + * @return this
73 + */
74 + public EncapSecurityPayload setSequence(final int sequence) {
75 + this.sequence = sequence;
76 + return this;
77 + }
78 +
79 + @Override
80 + public byte[] serialize() {
81 + byte[] payloadData = null;
82 + if (this.payload != null) {
83 + this.payload.setParent(this);
84 + payloadData = this.payload.serialize();
85 + }
86 +
87 + int payloadLength = 0;
88 + if (payloadData != null) {
89 + payloadLength = payloadData.length;
90 + }
91 +
92 + final byte[] data = new byte[HEADER_LENGTH + payloadLength];
93 + final ByteBuffer bb = ByteBuffer.wrap(data);
94 +
95 + bb.putInt(this.securityParamIndex);
96 + bb.putInt(this.sequence);
97 +
98 + if (payloadData != null) {
99 + bb.put(payloadData);
100 + }
101 +
102 + if (this.parent != null && this.parent instanceof IExtensionHeader) {
103 + ((IExtensionHeader) this.parent).setNextHeader(IPv6.PROTOCOL_ESP);
104 + }
105 + return data;
106 + }
107 +
108 + @Override
109 + public IPacket deserialize(byte[] data, int offset, int length) {
110 + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
111 + this.securityParamIndex = bb.getInt();
112 + this.sequence = bb.getInt();
113 +
114 + this.payload = new Data();
115 + this.payload.deserialize(data, bb.position(),
116 + bb.limit() - bb.position());
117 + this.payload.setParent(this);
118 +
119 + return this;
120 + }
121 +
122 + /*
123 + * (non-Javadoc)
124 + *
125 + * @see java.lang.Object#hashCode()
126 + */
127 + @Override
128 + public int hashCode() {
129 + final int prime = 5807;
130 + int result = super.hashCode();
131 + result = prime * result + this.securityParamIndex;
132 + result = prime * result + this.sequence;
133 + return result;
134 + }
135 +
136 + /*
137 + * (non-Javadoc)
138 + *
139 + * @see java.lang.Object#equals(java.lang.Object)
140 + */
141 + @Override
142 + public boolean equals(final Object obj) {
143 + if (this == obj) {
144 + return true;
145 + }
146 + if (!super.equals(obj)) {
147 + return false;
148 + }
149 + if (!(obj instanceof EncapSecurityPayload)) {
150 + return false;
151 + }
152 + final EncapSecurityPayload other = (EncapSecurityPayload) obj;
153 + if (this.securityParamIndex != other.securityParamIndex) {
154 + return false;
155 + }
156 + if (this.sequence != other.sequence) {
157 + return false;
158 + }
159 + return true;
160 + }
161 +}
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +import org.onlab.packet.BasePacket;
20 +import org.onlab.packet.Data;
21 +import org.onlab.packet.IPacket;
22 +import org.onlab.packet.IPv6;
23 +
24 +import java.nio.ByteBuffer;
25 +
26 +/**
27 + * Implements IPv6 fragment extension header format. (RFC 2460)
28 + */
29 +public class Fragment extends BasePacket implements IExtensionHeader {
30 + public static final byte HEADER_LENGTH = 8; // bytes
31 +
32 + protected byte nextHeader;
33 + protected short fragmentOffset;
34 + protected byte moreFragment;
35 + protected int identification;
36 +
37 + @Override
38 + public byte getNextHeader() {
39 + return this.nextHeader;
40 + }
41 +
42 + @Override
43 + public Fragment setNextHeader(final byte nextHeader) {
44 + this.nextHeader = nextHeader;
45 + return this;
46 + }
47 +
48 + /**
49 + * Gets the fragment offset of this header.
50 + *
51 + * @return fragment offset
52 + */
53 + public short getFragmentOffset() {
54 + return this.fragmentOffset;
55 + }
56 +
57 + /**
58 + * Sets the fragment offset of this header.
59 + *
60 + * @param fragmentOffset the fragment offset to set
61 + * @return this
62 + */
63 + public Fragment setFragmentOffset(final short fragmentOffset) {
64 + this.fragmentOffset = fragmentOffset;
65 + return this;
66 + }
67 +
68 + /**
69 + * Gets the more fragment flag of this header.
70 + *
71 + * @return more fragment flag
72 + */
73 + public byte getMoreFragment() {
74 + return this.moreFragment;
75 + }
76 +
77 + /**
78 + * Sets the more fragment flag of this header.
79 + *
80 + * @param moreFragment the more fragment flag to set
81 + * @return this
82 + */
83 + public Fragment setMoreFragment(final byte moreFragment) {
84 + this.moreFragment = moreFragment;
85 + return this;
86 + }
87 +
88 + /**
89 + * Gets the identification of this header.
90 + *
91 + * @return identification
92 + */
93 + public int getIdentification() {
94 + return this.identification;
95 + }
96 +
97 + /**
98 + * Sets the identification of this header.
99 + *
100 + * @param identification the identification to set
101 + * @return this
102 + */
103 + public Fragment setIdentification(final int identification) {
104 + this.identification = identification;
105 + return this;
106 + }
107 +
108 + @Override
109 + public byte[] serialize() {
110 + byte[] payloadData = null;
111 + if (this.payload != null) {
112 + this.payload.setParent(this);
113 + payloadData = this.payload.serialize();
114 + }
115 +
116 + int payloadLength = 0;
117 + if (payloadData != null) {
118 + payloadLength = payloadData.length;
119 + }
120 +
121 + final byte[] data = new byte[HEADER_LENGTH + payloadLength];
122 + final ByteBuffer bb = ByteBuffer.wrap(data);
123 +
124 + bb.put(this.nextHeader);
125 + bb.put((byte) 0);
126 + bb.putShort((short) (
127 + (this.fragmentOffset & 0x1fff) << 3 |
128 + this.moreFragment & 0x1
129 + ));
130 + bb.putInt(this.identification);
131 +
132 + if (payloadData != null) {
133 + bb.put(payloadData);
134 + }
135 +
136 + if (this.parent != null && this.parent instanceof IExtensionHeader) {
137 + ((IExtensionHeader) this.parent).setNextHeader(IPv6.PROTOCOL_FRAG);
138 + }
139 + return data;
140 + }
141 +
142 + @Override
143 + public IPacket deserialize(byte[] data, int offset, int length) {
144 + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
145 + this.nextHeader = bb.get();
146 + bb.get();
147 + short sscratch = bb.getShort();
148 + this.fragmentOffset = (short) (sscratch >> 3 & 0x1fff);
149 + this.moreFragment = (byte) (sscratch & 0x1);
150 + this.identification = bb.getInt();
151 +
152 + IPacket payload;
153 + if (IPv6.PROTOCOL_CLASS_MAP.containsKey(this.nextHeader)) {
154 + final Class<? extends IPacket> clazz = IPv6.PROTOCOL_CLASS_MAP
155 + .get(this.nextHeader);
156 + try {
157 + payload = clazz.newInstance();
158 + } catch (final Exception e) {
159 + throw new RuntimeException(
160 + "Error parsing payload for Fragment packet", e);
161 + }
162 + } else {
163 + payload = new Data();
164 + }
165 + this.payload = payload.deserialize(data, bb.position(),
166 + bb.limit() - bb.position());
167 + this.payload.setParent(this);
168 +
169 + return this;
170 + }
171 +
172 + /*
173 + * (non-Javadoc)
174 + *
175 + * @see java.lang.Object#hashCode()
176 + */
177 + @Override
178 + public int hashCode() {
179 + final int prime = 5807;
180 + int result = super.hashCode();
181 + result = prime * result + this.nextHeader;
182 + result = prime * result + this.fragmentOffset;
183 + result = prime * result + this.moreFragment;
184 + result = prime * result + this.identification;
185 + return result;
186 + }
187 +
188 + /*
189 + * (non-Javadoc)
190 + *
191 + * @see java.lang.Object#equals(java.lang.Object)
192 + */
193 + @Override
194 + public boolean equals(final Object obj) {
195 + if (this == obj) {
196 + return true;
197 + }
198 + if (!super.equals(obj)) {
199 + return false;
200 + }
201 + if (!(obj instanceof Fragment)) {
202 + return false;
203 + }
204 + final Fragment other = (Fragment) obj;
205 + if (this.nextHeader != other.nextHeader) {
206 + return false;
207 + }
208 + if (this.fragmentOffset != other.fragmentOffset) {
209 + return false;
210 + }
211 + if (this.moreFragment != other.moreFragment) {
212 + return false;
213 + }
214 + if (this.identification != other.identification) {
215 + return false;
216 + }
217 + return true;
218 + }
219 +}
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +import org.onlab.packet.IPv6;
20 +
21 +/**
22 + * Implements IPv6 Hop-by-hop Options extension header format. (RFC 2460)
23 + */
24 +public class HopByHopOptions extends BaseOptions {
25 + public HopByHopOptions() {
26 + super();
27 + this.setType(IPv6.PROTOCOL_HOPOPT);
28 + }
29 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +/**
20 + * Interface for IPv6 extension header.
21 + */
22 +public interface IExtensionHeader {
23 + /**
24 + * Gets the type of next header.
25 + *
26 + * @return next header
27 + */
28 + public byte getNextHeader();
29 +
30 + /**
31 + * Sets the type of next header.
32 + *
33 + * @param nextHeader the next header to set
34 + * @return this
35 + */
36 + public IExtensionHeader setNextHeader(final byte nextHeader);
37 +}
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +import org.onlab.packet.BasePacket;
20 +import org.onlab.packet.Data;
21 +import org.onlab.packet.IPacket;
22 +import org.onlab.packet.IPv6;
23 +
24 +import java.nio.ByteBuffer;
25 +import java.util.Arrays;
26 +
27 +/**
28 + * Implements IPv6 routing extension header format. (RFC 2460)
29 + */
30 +public class Routing extends BasePacket implements IExtensionHeader {
31 + public static final byte FIXED_HEADER_LENGTH = 4; // bytes
32 + public static final byte FIXED_ROUTING_DATA_LENGTH = 4; // bytes
33 + public static final byte LENGTH_UNIT = 8; // bytes per unit
34 +
35 + protected byte nextHeader;
36 + protected byte headerExtLength;
37 + protected byte routingType;
38 + protected byte segmentsLeft;
39 + protected byte[] routingData;
40 +
41 + @Override
42 + public byte getNextHeader() {
43 + return this.nextHeader;
44 + }
45 +
46 + @Override
47 + public Routing setNextHeader(final byte nextHeader) {
48 + this.nextHeader = nextHeader;
49 + return this;
50 + }
51 +
52 + /**
53 + * Gets the extension length of this header.
54 + *
55 + * @return header length
56 + */
57 + public byte getHeaderExtLength() {
58 + return this.headerExtLength;
59 + }
60 +
61 + /**
62 + * Sets the extension length of this header.
63 + *
64 + * @param headerExtLength the header length to set
65 + * @return this
66 + */
67 + public Routing setHeaderExtLength(final byte headerExtLength) {
68 + this.headerExtLength = headerExtLength;
69 + return this;
70 + }
71 +
72 + /**
73 + * Gets the routing type of this header.
74 + *
75 + * @return routing type
76 + */
77 + public byte getRoutingType() {
78 + return this.routingType;
79 + }
80 +
81 + /**
82 + * Sets the routing type of this header.
83 + *
84 + * @param routingType the routing type to set
85 + * @return this
86 + */
87 + public Routing setRoutingType(final byte routingType) {
88 + this.routingType = routingType;
89 + return this;
90 + }
91 +
92 + /**
93 + * Gets the number of remaining route segments of this header.
94 + *
95 + * @return number of remaining route segments
96 + */
97 + public byte getSegmentsLeft() {
98 + return this.segmentsLeft;
99 + }
100 +
101 + /**
102 + * Sets the number of remaining route segments of this header.
103 + *
104 + * @param segmentsLeft the number of remaining route segments to set
105 + * @return this
106 + */
107 + public Routing setSegmntsLeft(final byte segmentsLeft) {
108 + this.segmentsLeft = segmentsLeft;
109 + return this;
110 + }
111 +
112 + /**
113 + * Gets the routing data.
114 + *
115 + * @return the routing data
116 + */
117 + public byte[] getRoutingData() {
118 + return this.routingData;
119 + }
120 +
121 + /**
122 + * Sets the routing data.
123 + *
124 + * @param routingData the routing data to set
125 + * @return this
126 + */
127 + public Routing setRoutingData(final byte[] routingData) {
128 + this.routingData =
129 + Arrays.copyOfRange(routingData, 0, routingData.length);
130 + return this;
131 + }
132 +
133 + @Override
134 + public byte[] serialize() {
135 + byte[] payloadData = null;
136 + if (this.payload != null) {
137 + this.payload.setParent(this);
138 + payloadData = this.payload.serialize();
139 + }
140 +
141 + int headerLength = FIXED_HEADER_LENGTH + routingData.length;
142 + int payloadLength = 0;
143 + if (payloadData != null) {
144 + payloadLength = payloadData.length;
145 + }
146 +
147 + final byte[] data = new byte[headerLength + payloadLength];
148 + final ByteBuffer bb = ByteBuffer.wrap(data);
149 +
150 + bb.put(this.nextHeader);
151 + bb.put(this.headerExtLength);
152 + bb.put(this.routingType);
153 + bb.put(this.segmentsLeft);
154 + bb.put(this.routingData, 0, routingData.length);
155 +
156 + if (payloadData != null) {
157 + bb.put(payloadData);
158 + }
159 +
160 + if (this.parent != null && this.parent instanceof IExtensionHeader) {
161 + ((IExtensionHeader) this.parent).setNextHeader(IPv6.PROTOCOL_ROUTING);
162 + }
163 + return data;
164 + }
165 +
166 + @Override
167 + public IPacket deserialize(byte[] data, int offset, int length) {
168 + final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
169 + this.nextHeader = bb.get();
170 + this.headerExtLength = bb.get();
171 + this.routingType = bb.get();
172 + this.segmentsLeft = bb.get();
173 + int dataLength =
174 + FIXED_ROUTING_DATA_LENGTH + LENGTH_UNIT * this.headerExtLength;
175 + this.routingData = new byte[dataLength];
176 + bb.get(this.routingData, 0, dataLength);
177 +
178 + IPacket payload;
179 + if (IPv6.PROTOCOL_CLASS_MAP.containsKey(this.nextHeader)) {
180 + final Class<? extends IPacket> clazz = IPv6.PROTOCOL_CLASS_MAP
181 + .get(this.nextHeader);
182 + try {
183 + payload = clazz.newInstance();
184 + } catch (final Exception e) {
185 + throw new RuntimeException(
186 + "Error parsing payload for Routing packet", e);
187 + }
188 + } else {
189 + payload = new Data();
190 + }
191 + this.payload = payload.deserialize(data, bb.position(),
192 + bb.limit() - bb.position());
193 + this.payload.setParent(this);
194 +
195 + return this;
196 + }
197 +
198 + /*
199 + * (non-Javadoc)
200 + *
201 + * @see java.lang.Object#hashCode()
202 + */
203 + @Override
204 + public int hashCode() {
205 + final int prime = 5807;
206 + int result = super.hashCode();
207 + result = prime * result + this.nextHeader;
208 + result = prime * result + this.headerExtLength;
209 + result = prime * result + this.routingType;
210 + result = prime * result + this.segmentsLeft;
211 + for (byte b : this.routingData) {
212 + result = prime * result + b;
213 + }
214 + return result;
215 + }
216 +
217 + /*
218 + * (non-Javadoc)
219 + *
220 + * @see java.lang.Object#equals(java.lang.Object)
221 + */
222 + @Override
223 + public boolean equals(final Object obj) {
224 + if (this == obj) {
225 + return true;
226 + }
227 + if (!super.equals(obj)) {
228 + return false;
229 + }
230 + if (!(obj instanceof Routing)) {
231 + return false;
232 + }
233 + final Routing other = (Routing) obj;
234 + if (this.nextHeader != other.nextHeader) {
235 + return false;
236 + }
237 + if (this.headerExtLength != other.headerExtLength) {
238 + return false;
239 + }
240 + if (this.routingType != other.routingType) {
241 + return false;
242 + }
243 + if (this.segmentsLeft != other.segmentsLeft) {
244 + return false;
245 + }
246 + if (!Arrays.equals(this.routingData, other.routingData)) {
247 + return false;
248 + }
249 + return true;
250 + }
251 +}
...\ No newline at end of file ...\ No newline at end of file
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 IPv6 extension headers.
19 + */
20 +package org.onlab.packet.ipv6;
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +import org.junit.BeforeClass;
20 +import org.junit.Test;
21 +import org.onlab.packet.Data;
22 +import org.onlab.packet.UDP;
23 +
24 +import static org.hamcrest.Matchers.is;
25 +import static org.junit.Assert.assertArrayEquals;
26 +import static org.junit.Assert.assertFalse;
27 +import static org.junit.Assert.assertThat;
28 +import static org.junit.Assert.assertTrue;
29 +
30 +/**
31 + * Tests for class {@link Authentication}.
32 + */
33 +public class AuthenticationTest {
34 + private static Data data;
35 + private static UDP udp;
36 + private static byte[] icv = {
37 + (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44
38 + };
39 + private static byte[] bytePacket;
40 +
41 + @BeforeClass
42 + public static void setUpBeforeClass() throws Exception {
43 + data = new Data();
44 + data.setData("testSerialize".getBytes());
45 + udp = new UDP();
46 + udp.setPayload(data);
47 +
48 + byte[] bytePayload = udp.serialize();
49 + byte[] byteHeader = {
50 + (byte) 0x11, (byte) 0x02, (byte) 0x00, (byte) 0x00,
51 + (byte) 0x13, (byte) 0x57, (byte) 0x24, (byte) 0x68,
52 + (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x00,
53 + (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44
54 + };
55 + bytePacket = new byte[byteHeader.length + bytePayload.length];
56 + System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
57 + System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
58 + }
59 +
60 + /**
61 + * Tests serialize and setters.
62 + */
63 + @Test
64 + public void testSerialize() {
65 + Authentication auth = new Authentication();
66 + auth.setNextHeader((byte) 0x11);
67 + auth.setPayloadLength((byte) 0x02);
68 + auth.setSecurityParamIndex(0x13572468);
69 + auth.setSequence(0xffff00);
70 + auth.setIngegrityCheck(icv);
71 + auth.setPayload(udp);
72 +
73 + assertArrayEquals(auth.serialize(), bytePacket);
74 + }
75 +
76 + /**
77 + * Tests deserialize and getters.
78 + */
79 + @Test
80 + public void testDeserialize() {
81 + Authentication auth = new Authentication();
82 + auth.deserialize(bytePacket, 0, bytePacket.length);
83 +
84 + assertThat(auth.getNextHeader(), is((byte) 0x11));
85 + assertThat(auth.getPayloadLength(), is((byte) 0x02));
86 + assertThat(auth.getSecurityParamIndex(), is(0x13572468));
87 + assertThat(auth.getSequence(), is(0xffff00));
88 + assertArrayEquals(auth.getIntegrityCheck(), icv);
89 + }
90 +
91 + /**
92 + * Tests comparator.
93 + */
94 + @Test
95 + public void testEqual() {
96 + Authentication auth1 = new Authentication();
97 + auth1.setNextHeader((byte) 0x11);
98 + auth1.setPayloadLength((byte) 0x02);
99 + auth1.setSecurityParamIndex(0x13572468);
100 + auth1.setSequence(0xffff00);
101 + auth1.setIngegrityCheck(icv);
102 +
103 + Authentication auth2 = new Authentication();
104 + auth2.setNextHeader((byte) 0x11);
105 + auth2.setPayloadLength((byte) 0x02);
106 + auth2.setSecurityParamIndex(0x13572467);
107 + auth2.setSequence(0xffff00);
108 + auth2.setIngegrityCheck(icv);
109 +
110 + assertTrue(auth1.equals(auth1));
111 + assertFalse(auth1.equals(auth2));
112 + }
113 +}
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +import org.junit.BeforeClass;
20 +import org.junit.Test;
21 +import org.onlab.packet.Data;
22 +import org.onlab.packet.IPv6;
23 +import org.onlab.packet.UDP;
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 BaseOptions}.
33 + */
34 +public class BaseOptionsTest {
35 + private static Data data;
36 + private static UDP udp;
37 + private static byte[] options = {
38 + (byte) 0x00, (byte) 0x03,
39 + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00
40 + };
41 + private static byte[] bytePacket;
42 +
43 + @BeforeClass
44 + public static void setUpBeforeClass() throws Exception {
45 + data = new Data();
46 + data.setData("testSerialize".getBytes());
47 + udp = new UDP();
48 + udp.setPayload(data);
49 +
50 + byte[] bytePayload = udp.serialize();
51 + byte[] byteHeader = {
52 + (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0x03,
53 + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00
54 + };
55 + bytePacket = new byte[byteHeader.length + bytePayload.length];
56 + System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
57 + System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
58 + }
59 +
60 + /**
61 + * Tests serialize and setters.
62 + */
63 + @Test
64 + public void testSerialize() {
65 + BaseOptions baseopt = new BaseOptions();
66 + baseopt.setNextHeader((byte) 0x11);
67 + baseopt.setHeaderExtLength((byte) 0x00);
68 + baseopt.setOptions(options);
69 + baseopt.setPayload(udp);
70 +
71 + assertArrayEquals(baseopt.serialize(), bytePacket);
72 + }
73 +
74 + /**
75 + * Tests deserialize and getters.
76 + */
77 + @Test
78 + public void testDeserialize() {
79 + BaseOptions baseopt = new BaseOptions();
80 + baseopt.deserialize(bytePacket, 0, bytePacket.length);
81 +
82 + assertThat(baseopt.getNextHeader(), is((byte) 0x11));
83 + assertThat(baseopt.getHeaderExtLength(), is((byte) 0x00));
84 + assertArrayEquals(baseopt.getOptions(), options);
85 + }
86 +
87 + /**
88 + * Tests comparator.
89 + */
90 + @Test
91 + public void testEqual() {
92 + BaseOptions baseopt1 = new BaseOptions();
93 + baseopt1.setNextHeader((byte) 0x11);
94 + baseopt1.setHeaderExtLength((byte) 0x00);
95 + baseopt1.setOptions(options);
96 + baseopt1.setType(IPv6.PROTOCOL_HOPOPT);
97 +
98 + BaseOptions baseopt2 = new BaseOptions();
99 + baseopt2.setNextHeader((byte) 0x11);
100 + baseopt2.setHeaderExtLength((byte) 0x00);
101 + baseopt2.setOptions(options);
102 + baseopt1.setType(IPv6.PROTOCOL_DSTOPT);
103 +
104 + assertTrue(baseopt1.equals(baseopt1));
105 + assertFalse(baseopt1.equals(baseopt2));
106 + }
107 +}
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +import org.junit.Test;
20 +import org.onlab.packet.IPv6;
21 +
22 +import static org.hamcrest.Matchers.is;
23 +import static org.junit.Assert.assertThat;
24 +
25 +/**
26 + * Tests for class {@link DestinationOptions}.
27 + */
28 +public class DestinationOptionsTest {
29 + /**
30 + * Tests constructor.
31 + */
32 + @Test
33 + public void testConstructor() {
34 + DestinationOptions dstopt = new DestinationOptions();
35 + assertThat(dstopt.getType(), is(IPv6.PROTOCOL_DSTOPT));
36 + }
37 +}
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +import org.junit.BeforeClass;
20 +import org.junit.Test;
21 +import org.onlab.packet.Data;
22 +import java.util.Arrays;
23 +
24 +import static org.hamcrest.Matchers.is;
25 +import static org.junit.Assert.assertArrayEquals;
26 +import static org.junit.Assert.assertFalse;
27 +import static org.junit.Assert.assertThat;
28 +import static org.junit.Assert.assertTrue;
29 +
30 +/**
31 + * Tests for class {@link EncapSecurityPayload}.
32 + */
33 +public class EncapSecurityPayloadTest {
34 + private static Data data;
35 + private static byte[] dataByte = new byte[32];
36 + private static byte[] bytePacket;
37 +
38 + @BeforeClass
39 + public static void setUpBeforeClass() throws Exception {
40 + Arrays.fill(dataByte, (byte) 0xff);
41 + data = new Data().setData(dataByte);
42 +
43 + byte[] bytePayload = data.serialize();
44 + byte[] byteHeader = {
45 + (byte) 0x13, (byte) 0x57, (byte) 0x24, (byte) 0x68,
46 + (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x00
47 + };
48 + bytePacket = new byte[byteHeader.length + bytePayload.length];
49 + System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
50 + System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
51 + }
52 +
53 + /**
54 + * Tests serialize and setters.
55 + */
56 + @Test
57 + public void testSerialize() {
58 + EncapSecurityPayload esp = new EncapSecurityPayload();
59 + esp.setSecurityParamIndex(0x13572468);
60 + esp.setSequence(0xffff00);
61 + esp.setPayload(data);
62 +
63 + assertArrayEquals(esp.serialize(), bytePacket);
64 + }
65 +
66 + /**
67 + * Tests deserialize and getters.
68 + */
69 + @Test
70 + public void testDeserialize() {
71 + EncapSecurityPayload esp = new EncapSecurityPayload();
72 + esp.deserialize(bytePacket, 0, bytePacket.length);
73 +
74 + assertThat(esp.getSecurityParamIndex(), is(0x13572468));
75 + assertThat(esp.getSequence(), is(0xffff00));
76 + }
77 +
78 + /**
79 + * Tests comparator.
80 + */
81 + @Test
82 + public void testEqual() {
83 + EncapSecurityPayload esp1 = new EncapSecurityPayload();
84 + esp1.setSecurityParamIndex(0x13572468);
85 + esp1.setSequence(0xffff00);
86 +
87 + EncapSecurityPayload esp2 = new EncapSecurityPayload();
88 + esp2.setSecurityParamIndex(0x13572468);
89 + esp2.setSequence(0xfffff0);
90 +
91 + assertTrue(esp1.equals(esp1));
92 + assertFalse(esp1.equals(esp2));
93 + }
94 +}
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +import org.junit.BeforeClass;
20 +import org.junit.Test;
21 +import org.onlab.packet.Data;
22 +import org.onlab.packet.UDP;
23 +
24 +import static org.hamcrest.Matchers.is;
25 +import static org.junit.Assert.assertArrayEquals;
26 +import static org.junit.Assert.assertFalse;
27 +import static org.junit.Assert.assertThat;
28 +import static org.junit.Assert.assertTrue;
29 +
30 +/**
31 + * Tests for class {@link Fragment}.
32 + */
33 +public class FragmentTest {
34 + private static Data data;
35 + private static UDP udp;
36 + private static byte[] bytePacket;
37 +
38 + @BeforeClass
39 + public static void setUpBeforeClass() throws Exception {
40 + data = new Data();
41 + data.setData("testSerialize".getBytes());
42 + udp = new UDP();
43 + udp.setPayload(data);
44 +
45 + byte[] bytePayload = udp.serialize();
46 + byte[] byteHeader = {
47 + (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0xf9,
48 + (byte) 0x00, (byte) 0x00, (byte) 0x13, (byte) 0x57
49 + };
50 + bytePacket = new byte[byteHeader.length + bytePayload.length];
51 + System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
52 + System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
53 + }
54 +
55 + /**
56 + * Tests serialize and setters.
57 + */
58 + @Test
59 + public void testSerialize() {
60 + Fragment frag = new Fragment();
61 + frag.setNextHeader((byte) 0x11);
62 + frag.setFragmentOffset((short) 0x1f);
63 + frag.setMoreFragment((byte) 1);
64 + frag.setIdentification(0x1357);
65 + frag.setPayload(udp);
66 +
67 + assertArrayEquals(frag.serialize(), bytePacket);
68 + }
69 +
70 + /**
71 + * Tests deserialize and getters.
72 + */
73 + @Test
74 + public void testDeserialize() {
75 + Fragment frag = new Fragment();
76 + frag.deserialize(bytePacket, 0, bytePacket.length);
77 +
78 + assertThat(frag.getNextHeader(), is((byte) 0x11));
79 + assertThat(frag.getFragmentOffset(), is((short) 0x1f));
80 + assertThat(frag.getMoreFragment(), is((byte) 1));
81 + assertThat(frag.getIdentification(), is(0x1357));
82 + }
83 +
84 + /**
85 + * Tests comparator.
86 + */
87 + @Test
88 + public void testEqual() {
89 + Fragment frag1 = new Fragment();
90 + frag1.setNextHeader((byte) 0x11);
91 + frag1.setFragmentOffset((short) 0x1f);
92 + frag1.setMoreFragment((byte) 1);
93 + frag1.setIdentification(0x1357);
94 +
95 + Fragment frag2 = new Fragment();
96 + frag2.setNextHeader((byte) 0x11);
97 + frag2.setFragmentOffset((short) 0x1f);
98 + frag2.setMoreFragment((byte) 1);
99 + frag2.setIdentification(0x1358);
100 +
101 + assertTrue(frag1.equals(frag1));
102 + assertFalse(frag1.equals(frag2));
103 + }
104 +}
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +import org.junit.Test;
20 +import org.onlab.packet.IPv6;
21 +
22 +import static org.hamcrest.Matchers.is;
23 +import static org.junit.Assert.assertThat;
24 +
25 +/**
26 + * Tests for class {@link HopByHopOptions}.
27 + */
28 +public class HopByHopOptionsTest {
29 + /**
30 + * Tests constructor.
31 + */
32 + @Test
33 + public void testConstructor() {
34 + HopByHopOptions hopopt = new HopByHopOptions();
35 + assertThat(hopopt.getType(), is(IPv6.PROTOCOL_HOPOPT));
36 + }
37 +}
1 +/*
2 + * Copyright 2014-2015 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 +package org.onlab.packet.ipv6;
18 +
19 +import org.junit.BeforeClass;
20 +import org.junit.Test;
21 +import org.onlab.packet.Data;
22 +import org.onlab.packet.UDP;
23 +
24 +import static org.hamcrest.Matchers.is;
25 +import static org.junit.Assert.assertArrayEquals;
26 +import static org.junit.Assert.assertFalse;
27 +import static org.junit.Assert.assertThat;
28 +import static org.junit.Assert.assertTrue;
29 +
30 +/**
31 + * Tests for class {@link Routing}.
32 + */
33 +public class RoutingTest {
34 + private static Data data;
35 + private static UDP udp;
36 + private static byte[] routingData = {
37 + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
38 + (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
39 + (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
40 + (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
41 + (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
42 + };
43 + private static byte[] bytePacket;
44 +
45 + @BeforeClass
46 + public static void setUpBeforeClass() throws Exception {
47 + data = new Data();
48 + data.setData("testSerialize".getBytes());
49 + udp = new UDP();
50 + udp.setPayload(data);
51 +
52 + byte[] bytePayload = udp.serialize();
53 + byte[] byteHeader = {
54 + (byte) 0x11, (byte) 0x02, (byte) 0x00, (byte) 0x03,
55 + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
56 + (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
57 + (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
58 + (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
59 + (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 + Routing routing = new Routing();
72 + routing.setNextHeader((byte) 0x11);
73 + routing.setHeaderExtLength((byte) 0x02);
74 + routing.setRoutingType((byte) 0x00);
75 + routing.setSegmntsLeft((byte) 0x03);
76 + routing.setRoutingData(routingData);
77 + routing.setPayload(udp);
78 +
79 + assertArrayEquals(routing.serialize(), bytePacket);
80 + }
81 +
82 + /**
83 + * Tests deserialize and getters.
84 + */
85 + @Test
86 + public void testDeserialize() {
87 + Routing routing = new Routing();
88 + routing.deserialize(bytePacket, 0, bytePacket.length);
89 +
90 + assertThat(routing.getNextHeader(), is((byte) 0x11));
91 + assertThat(routing.getHeaderExtLength(), is((byte) 0x02));
92 + assertThat(routing.getRoutingType(), is((byte) 0x00));
93 + assertThat(routing.getSegmentsLeft(), is((byte) 0x03));
94 + assertArrayEquals(routing.getRoutingData(), routingData);
95 + }
96 +
97 + /**
98 + * Tests comparator.
99 + */
100 + @Test
101 + public void testEqual() {
102 + Routing routing1 = new Routing();
103 + routing1.setNextHeader((byte) 0x11);
104 + routing1.setHeaderExtLength((byte) 0x02);
105 + routing1.setRoutingType((byte) 0x00);
106 + routing1.setSegmntsLeft((byte) 0x03);
107 + routing1.setRoutingData(routingData);
108 +
109 + Routing routing2 = new Routing();
110 + routing2.setNextHeader((byte) 0x11);
111 + routing2.setHeaderExtLength((byte) 0x02);
112 + routing2.setRoutingType((byte) 0x00);
113 + routing2.setSegmntsLeft((byte) 0x02);
114 + routing2.setRoutingData(routingData);
115 +
116 + assertTrue(routing1.equals(routing1));
117 + assertFalse(routing1.equals(routing2));
118 + }
119 +}