Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 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 | package org.onlab.packet; |
| 17 | |
| 18 | import org.onlab.packet.pim.PIMHello; |
| 19 | import org.onlab.packet.pim.PIMJoinPrune; |
| 20 | |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
| 22 | |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 23 | import java.nio.ByteBuffer; |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 24 | import java.util.Map; |
| 25 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 27 | import static org.onlab.packet.PacketUtils.checkInput; |
| 28 | |
| 29 | /** |
| 30 | * Implements PIM control packet format. |
| 31 | */ |
| 32 | public class PIM extends BasePacket { |
| 33 | |
| 34 | public static final IpAddress PIM_ADDRESS = IpAddress.valueOf("224.0.0.13"); |
| 35 | |
| 36 | public static final byte TYPE_HELLO = 0x00; |
| 37 | public static final byte TYPE_REGISTER = 0x01; |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 38 | public static final byte TYPE_REGISTER_STOP = 0x02; |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 39 | public static final byte TYPE_JOIN_PRUNE_REQUEST = 0x03; |
| 40 | public static final byte TYPE_BOOTSTRAP = 0x04; |
| 41 | public static final byte TYPE_ASSERT = 0x05; |
| 42 | public static final byte TYPE_GRAFT = 0x06; |
| 43 | public static final byte TYPE_GRAFT_ACK = 0x07; |
| 44 | public static final byte TYPE_CANDIDATE_RP_ADV = 0x08; |
| 45 | |
| 46 | public static final int PIM_HEADER_LEN = 4; |
| 47 | |
Rusty Eddy | 0c12e40 | 2015-10-19 19:27:01 -0700 | [diff] [blame] | 48 | public static final byte ADDRESS_FAMILY_IP4 = 0x1; |
| 49 | public static final byte ADDRESS_FAMILY_IP6 = 0x2; |
| 50 | |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 51 | public static final Map<Byte, Deserializer<? extends IPacket>> PROTOCOL_DESERIALIZER_MAP = |
Yuta HIGUCHI | bfc2e92 | 2017-06-07 21:46:05 -0700 | [diff] [blame] | 52 | ImmutableMap.<Byte, Deserializer<? extends IPacket>>builder() |
| 53 | .put(PIM.TYPE_HELLO, PIMHello.deserializer()) |
| 54 | .put(PIM.TYPE_JOIN_PRUNE_REQUEST, PIMJoinPrune.deserializer()) |
| 55 | .build(); |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * PIM Header fields |
| 59 | */ |
| 60 | protected byte version; |
| 61 | protected byte type; |
| 62 | protected byte reserved; |
| 63 | protected short checksum; |
| 64 | |
| 65 | /** |
| 66 | * Default constructor. |
| 67 | */ |
| 68 | public PIM() { |
| 69 | super(); |
| 70 | this.version = 2; |
| 71 | this.reserved = 0; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Return the PIM message type. |
| 76 | * |
| 77 | * @return the pimMsgType |
| 78 | */ |
| 79 | public byte getPimMsgType() { |
| 80 | return this.type; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Set the PIM message type. Currently PIMJoinPrune and PIMHello are |
| 85 | * supported. |
| 86 | * |
| 87 | * @param type PIM message type |
| 88 | * @return PIM Header |
| 89 | */ |
| 90 | public PIM setPIMType(final byte type) { |
| 91 | this.type = type; |
| 92 | return this; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the version of PIM. |
| 97 | * |
| 98 | * @return the PIM version. Must be 2. |
| 99 | */ |
| 100 | public byte getVersion() { |
| 101 | return version; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Set the PIM version type. Should not change from 2. |
| 106 | * |
Thomas Vachuska | b9c7ea6 | 2015-09-10 15:17:02 -0700 | [diff] [blame] | 107 | * @param version PIM version |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 108 | */ |
| 109 | public void setVersion(byte version) { |
| 110 | this.version = version; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get the reserved field. |
| 115 | * |
| 116 | * @return the reserved field. Must be ignored. |
| 117 | */ |
| 118 | public byte getReserved() { |
| 119 | return reserved; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Set the reserved field. |
| 124 | * |
| 125 | * @param reserved should be 0 |
| 126 | */ |
| 127 | public void setReserved(byte reserved) { |
| 128 | this.reserved = reserved; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get the checksum of this packet. |
| 133 | * |
| 134 | * @return the checksum |
| 135 | */ |
| 136 | public short getChecksum() { |
| 137 | return checksum; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Set the checksum. |
| 142 | * |
| 143 | * @param checksum the checksum |
| 144 | */ |
| 145 | public void setChecksum(short checksum) { |
| 146 | this.checksum = checksum; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * (non-Javadoc) |
| 151 | * |
| 152 | * @see java.lang.Object#hashCode() |
| 153 | */ |
| 154 | @Override |
| 155 | public int hashCode() { |
| 156 | final int prime = 5807; |
| 157 | int result = super.hashCode(); |
| 158 | result = prime * result + this.type; |
| 159 | result = prime * result + this.version; |
| 160 | result = prime * result + this.checksum; |
| 161 | return result; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * (non-Javadoc) |
| 166 | * |
| 167 | * @see java.lang.Object#equals(java.lang.Object) |
| 168 | */ |
| 169 | @Override |
| 170 | public boolean equals(final Object obj) { |
| 171 | if (this == obj) { |
| 172 | return true; |
| 173 | } |
| 174 | if (!super.equals(obj)) { |
| 175 | return false; |
| 176 | } |
| 177 | if (!(obj instanceof PIM)) { |
| 178 | return false; |
| 179 | } |
| 180 | final PIM other = (PIM) obj; |
| 181 | if (this.type != other.type) { |
| 182 | return false; |
| 183 | } |
| 184 | if (this.version != other.version) { |
| 185 | return false; |
| 186 | } |
| 187 | if (this.checksum != other.checksum) { |
| 188 | return false; |
| 189 | } |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Serializes the packet. Will compute and set the following fields if they |
| 195 | * are set to specific values at the time serialize is called: -checksum : 0 |
| 196 | * -length : 0 |
| 197 | * |
| 198 | * @return will return the serialized packet |
| 199 | */ |
| 200 | @Override |
| 201 | public byte[] serialize() { |
| 202 | int length = 4; |
| 203 | byte[] payloadData = null; |
| 204 | if (this.payload != null) { |
| 205 | this.payload.setParent(this); |
| 206 | payloadData = this.payload.serialize(); |
| 207 | length += payloadData.length; |
| 208 | } |
| 209 | |
| 210 | final byte[] data = new byte[length]; |
| 211 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 212 | |
| 213 | bb.put((byte) ((this.version & 0xf) << 4 | this.type & 0xf)); |
| 214 | bb.put(this.reserved); |
| 215 | bb.putShort(this.checksum); |
| 216 | if (payloadData != null) { |
| 217 | bb.put(payloadData); |
| 218 | } |
| 219 | |
| 220 | if (this.parent != null && this.parent instanceof PIM) { |
| 221 | ((PIM) this.parent).setPIMType(TYPE_JOIN_PRUNE_REQUEST); |
| 222 | } |
| 223 | |
| 224 | // compute checksum if needed |
| 225 | if (this.checksum == 0) { |
| 226 | bb.rewind(); |
| 227 | int accumulation = 0; |
| 228 | |
| 229 | for (int i = 0; i < length / 2; ++i) { |
| 230 | accumulation += 0xffff & bb.getShort(); |
| 231 | } |
| 232 | // pad to an even number of shorts |
| 233 | if (length % 2 > 0) { |
| 234 | accumulation += (bb.get() & 0xff) << 8; |
| 235 | } |
| 236 | |
| 237 | accumulation = (accumulation >> 16 & 0xffff) |
| 238 | + (accumulation & 0xffff); |
| 239 | this.checksum = (short) (~accumulation & 0xffff); |
| 240 | bb.putShort(2, this.checksum); |
| 241 | } |
| 242 | return data; |
| 243 | } |
| 244 | |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 245 | |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 246 | /** |
| 247 | * Deserializer function for IPv4 packets. |
| 248 | * |
| 249 | * @return deserializer function |
| 250 | */ |
| 251 | public static Deserializer<PIM> deserializer() { |
| 252 | return (data, offset, length) -> { |
| 253 | checkInput(data, offset, length, PIM_HEADER_LEN); |
| 254 | |
| 255 | PIM pim = new PIM(); |
| 256 | |
| 257 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 258 | |
| 259 | byte versionByte = bb.get(); |
| 260 | pim.version = (byte) (versionByte >> 4 & 0xf); |
| 261 | pim.setPIMType((byte) (versionByte & 0xf)); |
| 262 | pim.reserved = bb.get(); |
| 263 | pim.checksum = bb.getShort(); |
| 264 | |
| 265 | Deserializer<? extends IPacket> deserializer; |
| 266 | if (PIM.PROTOCOL_DESERIALIZER_MAP.containsKey(pim.getPimMsgType())) { |
| 267 | deserializer = PIM.PROTOCOL_DESERIALIZER_MAP.get(pim.getPimMsgType()); |
| 268 | } else { |
| 269 | deserializer = Data.deserializer(); |
| 270 | } |
| 271 | |
| 272 | pim.payload = deserializer.deserialize(data, bb.position(), bb.limit() - bb.position()); |
| 273 | pim.payload.setParent(pim); |
| 274 | |
| 275 | return pim; |
| 276 | }; |
| 277 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 278 | |
| 279 | @Override |
| 280 | public String toString() { |
| 281 | return toStringHelper(getClass()) |
| 282 | .add("version", Byte.toString(version)) |
| 283 | .add("type", Byte.toString(type)) |
| 284 | .add("reserved", Byte.toString(reserved)) |
| 285 | .add("checksum", Short.toString(reserved)) |
| 286 | .toString(); |
| 287 | } |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 288 | } |