Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 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 | } |