Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 18 | package org.onlab.packet; |
| 19 | |
| 20 | import java.nio.ByteBuffer; |
| 21 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 22 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 23 | import static org.onlab.packet.PacketUtils.*; |
| 24 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Implements ICMP packet format. |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 27 | */ |
| 28 | public class ICMP extends BasePacket { |
| 29 | protected byte icmpType; |
| 30 | protected byte icmpCode; |
| 31 | protected short checksum; |
| 32 | |
sangho | 5eaf033 | 2015-03-09 15:08:12 -0700 | [diff] [blame] | 33 | public static final byte TYPE_ECHO_REQUEST = 0x08; |
| 34 | public static final byte TYPE_ECHO_REPLY = 0x00; |
| 35 | public static final byte SUBTYPE_ECHO_REPLY = 0x00; |
| 36 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 37 | public static final short ICMP_HEADER_LENGTH = 4; |
| 38 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 39 | /** |
| 40 | * @return the icmpType |
| 41 | */ |
| 42 | public byte getIcmpType() { |
| 43 | return this.icmpType; |
| 44 | } |
| 45 | |
| 46 | /** |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 47 | * @param icmpType to set |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 48 | * @return this |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 49 | */ |
| 50 | public ICMP setIcmpType(final byte icmpType) { |
| 51 | this.icmpType = icmpType; |
| 52 | return this; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @return the icmp code |
| 57 | */ |
| 58 | public byte getIcmpCode() { |
| 59 | return this.icmpCode; |
| 60 | } |
| 61 | |
| 62 | /** |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 63 | * @param icmpCode code to set |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 64 | * @return this |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 65 | */ |
| 66 | public ICMP setIcmpCode(final byte icmpCode) { |
| 67 | this.icmpCode = icmpCode; |
| 68 | return this; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @return the checksum |
| 73 | */ |
| 74 | public short getChecksum() { |
| 75 | return this.checksum; |
| 76 | } |
| 77 | |
| 78 | /** |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 79 | * @param checksum the checksum to set |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 80 | * @return this |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 81 | */ |
| 82 | public ICMP setChecksum(final short checksum) { |
| 83 | this.checksum = checksum; |
| 84 | return this; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Serializes the packet. Will compute and set the following fields if they |
| 89 | * are set to specific values at the time serialize is called: -checksum : 0 |
| 90 | * -length : 0 |
| 91 | */ |
| 92 | @Override |
| 93 | public byte[] serialize() { |
| 94 | int length = 4; |
| 95 | byte[] payloadData = null; |
| 96 | if (this.payload != null) { |
| 97 | this.payload.setParent(this); |
| 98 | payloadData = this.payload.serialize(); |
| 99 | length += payloadData.length; |
| 100 | } |
| 101 | |
| 102 | final byte[] data = new byte[length]; |
| 103 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 104 | |
| 105 | bb.put(this.icmpType); |
| 106 | bb.put(this.icmpCode); |
| 107 | bb.putShort(this.checksum); |
| 108 | if (payloadData != null) { |
| 109 | bb.put(payloadData); |
| 110 | } |
| 111 | |
| 112 | if (this.parent != null && this.parent instanceof IPv4) { |
| 113 | ((IPv4) this.parent).setProtocol(IPv4.PROTOCOL_ICMP); |
| 114 | } |
| 115 | |
| 116 | // compute checksum if needed |
| 117 | if (this.checksum == 0) { |
| 118 | bb.rewind(); |
| 119 | int accumulation = 0; |
| 120 | |
| 121 | for (int i = 0; i < length / 2; ++i) { |
| 122 | accumulation += 0xffff & bb.getShort(); |
| 123 | } |
| 124 | // pad to an even number of shorts |
| 125 | if (length % 2 > 0) { |
| 126 | accumulation += (bb.get() & 0xff) << 8; |
| 127 | } |
| 128 | |
| 129 | accumulation = (accumulation >> 16 & 0xffff) |
| 130 | + (accumulation & 0xffff); |
| 131 | this.checksum = (short) (~accumulation & 0xffff); |
| 132 | bb.putShort(2, this.checksum); |
| 133 | } |
| 134 | return data; |
| 135 | } |
| 136 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public IPacket deserialize(final byte[] data, final int offset, |
| 139 | final int length) { |
| 140 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 141 | this.icmpType = bb.get(); |
| 142 | this.icmpCode = bb.get(); |
| 143 | this.checksum = bb.getShort(); |
| 144 | |
| 145 | this.payload = new Data(); |
| 146 | this.payload = this.payload.deserialize(data, bb.position(), bb.limit() |
| 147 | - bb.position()); |
| 148 | this.payload.setParent(this); |
| 149 | return this; |
| 150 | } |
| 151 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 152 | /* |
| 153 | * (non-Javadoc) |
| 154 | * |
| 155 | * @see java.lang.Object#hashCode() |
| 156 | */ |
| 157 | @Override |
| 158 | public int hashCode() { |
| 159 | final int prime = 5807; |
| 160 | int result = super.hashCode(); |
| 161 | result = prime * result + this.icmpType; |
| 162 | result = prime * result + this.icmpCode; |
| 163 | result = prime * result + this.checksum; |
| 164 | return result; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * (non-Javadoc) |
| 169 | * |
| 170 | * @see java.lang.Object#equals(java.lang.Object) |
| 171 | */ |
| 172 | @Override |
| 173 | public boolean equals(final Object obj) { |
| 174 | if (this == obj) { |
| 175 | return true; |
| 176 | } |
| 177 | if (!super.equals(obj)) { |
| 178 | return false; |
| 179 | } |
| 180 | if (!(obj instanceof ICMP)) { |
| 181 | return false; |
| 182 | } |
| 183 | final ICMP other = (ICMP) obj; |
| 184 | if (this.icmpType != other.icmpType) { |
| 185 | return false; |
| 186 | } |
| 187 | if (this.icmpCode != other.icmpCode) { |
| 188 | return false; |
| 189 | } |
| 190 | if (this.checksum != other.checksum) { |
| 191 | return false; |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 196 | /** |
| 197 | * Deserializer function for ICMP packets. |
| 198 | * |
| 199 | * @return deserializer function |
| 200 | */ |
| 201 | public static Deserializer<ICMP> deserializer() { |
| 202 | return (data, offset, length) -> { |
| 203 | checkInput(data, offset, length, ICMP_HEADER_LENGTH); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 204 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 205 | ICMP icmp = new ICMP(); |
| 206 | |
| 207 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 208 | icmp.icmpType = bb.get(); |
| 209 | icmp.icmpCode = bb.get(); |
| 210 | icmp.checksum = bb.getShort(); |
| 211 | |
| 212 | icmp.payload = Data.deserializer() |
| 213 | .deserialize(data, bb.position(), bb.limit() |
| 214 | - bb.position()); |
| 215 | icmp.payload.setParent(icmp); |
| 216 | return icmp; |
| 217 | }; |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 218 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 219 | |
| 220 | @Override |
| 221 | public String toString() { |
| 222 | return toStringHelper(getClass()) |
| 223 | .add("icmpType", Byte.toString(icmpType)) |
| 224 | .add("icmpCode", Byte.toString(icmpCode)) |
| 225 | .add("checksum", Short.toString(checksum)) |
| 226 | .toString(); |
| 227 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 228 | } |