Kalhee Kim | 6222dbe | 2017-10-26 15:44:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | * Implemented according to RFC 2080 |
| 19 | */ |
| 20 | |
| 21 | package org.onlab.packet; |
| 22 | |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | import static org.onlab.packet.PacketUtils.checkInput; |
| 29 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 30 | import org.slf4j.Logger; |
| 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | |
| 33 | /** |
| 34 | * Representation of an RIPng Packet. |
| 35 | */ |
| 36 | public class RIPng extends BasePacket { |
| 37 | /** |
| 38 | * Routing Information Protocol Next Generation packet. |
| 39 | * ------------------------------------------ |cmdType (1) | version(1) | |
| 40 | * ------------------------------------------ |reserved (2) | |
| 41 | * ------------------------------------------ | route entries (n*20) | |
| 42 | * ------------------------------------------ |
| 43 | * |
| 44 | */ |
| 45 | // the case of no route entry |
| 46 | public static final int MIN_HEADER_LENGTH = 4; |
| 47 | |
| 48 | |
| 49 | private final Logger log = getLogger(getClass()); |
| 50 | |
| 51 | public enum CmdType { |
| 52 | RIPngREQUEST(1), |
| 53 | RIPngRESPONSE(2); |
| 54 | |
| 55 | protected int value; |
| 56 | |
| 57 | CmdType(final int value) { |
| 58 | this.value = value; |
| 59 | } |
| 60 | |
| 61 | public int getValue() { |
| 62 | return this.value; |
| 63 | } |
| 64 | |
| 65 | public static CmdType getType(final int value) { |
| 66 | switch (value) { |
| 67 | case 1: |
| 68 | return RIPngREQUEST; |
| 69 | case 2: |
| 70 | return RIPngRESPONSE; |
| 71 | default: |
| 72 | return null; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | protected byte cmdType; |
| 78 | protected byte version; |
| 79 | protected short reserved; |
| 80 | protected List<RIPngEntry> rtEntries = new ArrayList<RIPngEntry>(); |
| 81 | |
| 82 | /** |
| 83 | * @return the cmdType |
| 84 | */ |
| 85 | public byte getCmdType() { |
| 86 | return this.cmdType; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @param cmdType |
| 91 | * the cmdType to set |
| 92 | * @return this |
| 93 | */ |
| 94 | public RIPng setCmdType(final byte cmdType) { |
| 95 | this.cmdType = cmdType; |
| 96 | return this; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @return the version |
| 101 | */ |
| 102 | public byte getVersion() { |
| 103 | return this.version; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @param version |
| 108 | * the version to set |
| 109 | * @return this |
| 110 | */ |
| 111 | public RIPng setVersion(final byte version) { |
| 112 | this.version = version; |
| 113 | return this; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @return the reserved short |
| 118 | */ |
| 119 | public short getReserved() { |
| 120 | return this.reserved; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @param reserved |
| 125 | * the reserved short to set |
| 126 | * @return this |
| 127 | */ |
| 128 | public RIPng setReserved(final short reserved) { |
| 129 | this.reserved = reserved; |
| 130 | return this; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @return the route entries |
| 135 | */ |
| 136 | public List<RIPngEntry> getRtEntries() { |
| 137 | return this.rtEntries; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @param entries |
| 142 | * the route entries to set |
| 143 | * @return this |
| 144 | */ |
| 145 | public RIPng setRtEntries(final List<RIPngEntry> entries) { |
| 146 | this.rtEntries = entries; |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public byte[] serialize() { |
| 152 | // not guaranteed to retain length/exact format |
| 153 | this.resetChecksum(); |
| 154 | |
| 155 | final byte[] data = new byte[MIN_HEADER_LENGTH + RIPngEntry.ENTRY_LEN * rtEntries.size()]; |
| 156 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 157 | bb.put(this.cmdType); |
| 158 | bb.put(this.version); |
| 159 | bb.putShort(this.reserved); |
| 160 | for (final RIPngEntry entry : this.rtEntries) { |
| 161 | bb.put(entry.serialize()); |
| 162 | } |
| 163 | // assume the rest is padded out with zeroes |
| 164 | return data; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Deserializer function for RIPng packets. |
| 169 | * |
| 170 | * @return deserializer function |
| 171 | */ |
| 172 | public static Deserializer<RIPng> deserializer() { |
| 173 | return (data, offset, length) -> { |
| 174 | RIPng ripng = new RIPng(); |
| 175 | |
| 176 | checkInput(data, offset, length, MIN_HEADER_LENGTH); |
| 177 | |
| 178 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 179 | |
| 180 | ripng.cmdType = bb.get(); |
| 181 | ripng.version = bb.get(); |
| 182 | ripng.reserved = bb.getShort(); |
| 183 | |
| 184 | // read route entries |
| 185 | while (bb.hasRemaining() && (bb.remaining() >= RIPngEntry.ENTRY_LEN)) { |
| 186 | RIPngEntry rtEntry; |
| 187 | byte[] rtData = new byte[RIPngEntry.ENTRY_LEN]; |
| 188 | bb.get(rtData); |
| 189 | |
| 190 | rtEntry = RIPngEntry.deserializer().deserialize(rtData, 0, rtData.length); |
| 191 | ripng.rtEntries.add(rtEntry); |
| 192 | } |
| 193 | |
| 194 | return ripng; |
| 195 | }; |
| 196 | } |
| 197 | /* |
| 198 | * (non-Javadoc) |
| 199 | * |
| 200 | * @see java.lang.Object#hashCode() |
| 201 | */ |
| 202 | @Override |
| 203 | public int hashCode() { |
| 204 | return Objects.hash(super.hashCode(), cmdType, reserved, version, rtEntries); |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * (non-Javadoc) |
| 209 | * |
| 210 | * @see java.lang.Object#equals(java.lang.Object) |
| 211 | */ |
| 212 | @Override |
| 213 | public boolean equals(final Object obj) { |
| 214 | if (this == obj) { |
| 215 | return true; |
| 216 | } |
| 217 | if (!(obj instanceof RIPng)) { |
| 218 | return false; |
| 219 | } |
| 220 | final RIPng that = (RIPng) obj; |
| 221 | |
| 222 | return super.equals(that) && |
| 223 | Objects.equals(version, that.version) && |
| 224 | Objects.equals(reserved, that.reserved) && |
| 225 | Objects.equals(cmdType, that.cmdType) && |
| 226 | Objects.equals(rtEntries, that.rtEntries); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public String toString() { |
| 231 | return toStringHelper(getClass()) |
| 232 | .add("cmdType", Byte.toString(cmdType)) |
| 233 | .add("version", Byte.toString(version)) |
| 234 | .add("reserved", Short.toString(reserved)) |
| 235 | .toString(); |
| 236 | // TODO: need to handle route entries |
| 237 | } |
| 238 | } |