Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame^] | 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 | } |