Daniel Park | d27dcda | 2018-08-07 01:40:40 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | package org.onlab.packet; |
| 17 | |
| 18 | import java.nio.ByteBuffer; |
| 19 | |
| 20 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 21 | import static org.onlab.packet.PacketUtils.checkInput; |
| 22 | |
| 23 | /** |
| 24 | * ICMP packet class for echo purpose. |
| 25 | */ |
| 26 | public class ICMPEcho extends BasePacket { |
| 27 | private short identifier; |
| 28 | private short sequenceNum; |
| 29 | |
| 30 | public static final short ICMP_ECHO_HEADER_LENGTH = 4; |
| 31 | |
| 32 | /** |
| 33 | * Sets the identifier. |
| 34 | * |
| 35 | * @param identifier identifier |
| 36 | * @return this |
| 37 | */ |
| 38 | public ICMPEcho setIdentifier(final short identifier) { |
| 39 | this.identifier = identifier; |
| 40 | return this; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Gets the identifier. |
| 45 | * |
| 46 | * @return identifier |
| 47 | */ |
| 48 | public short getIdentifier() { |
| 49 | return this.identifier; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Sets the sequencer number. |
| 54 | * |
| 55 | * @param sequenceNum sequence number |
| 56 | * @return this |
| 57 | */ |
| 58 | public ICMPEcho setSequenceNum(final short sequenceNum) { |
| 59 | this.sequenceNum = sequenceNum; |
| 60 | return this; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Gets the sequence number. |
| 65 | * |
| 66 | * @return sequence number |
| 67 | */ |
| 68 | public short getSequenceNum() { |
| 69 | return this.sequenceNum; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Serializes the packet. Will compute and set the following fields if they |
| 74 | * are set to specific values at the time serialize is called: -checksum : 0 |
| 75 | * -length : 0 |
| 76 | */ |
| 77 | @Override |
| 78 | public byte[] serialize() { |
| 79 | |
| 80 | int length = ICMP_ECHO_HEADER_LENGTH; |
| 81 | byte[] payloadData = null; |
| 82 | if (this.payload != null) { |
| 83 | this.payload.setParent(this); |
| 84 | payloadData = this.payload.serialize(); |
| 85 | length += payloadData.length; |
| 86 | } |
| 87 | |
| 88 | final byte[] data = new byte[length]; |
| 89 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 90 | |
| 91 | bb.putShort(this.identifier); |
| 92 | bb.putShort(this.sequenceNum); |
| 93 | if (payloadData != null) { |
| 94 | bb.put(payloadData); |
| 95 | } |
| 96 | return data; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * (non-Javadoc) |
| 101 | * |
| 102 | * @see java.lang.Object#hashCode() |
| 103 | */ |
| 104 | @Override |
| 105 | public int hashCode() { |
| 106 | final int prime = 5807; |
| 107 | int result = super.hashCode(); |
| 108 | result = prime * result + this.identifier; |
| 109 | result = prime * result + this.sequenceNum; |
| 110 | return result; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * (non-Javadoc) |
| 115 | * |
| 116 | * @see java.lang.Object#equals(java.lang.Object) |
| 117 | */ |
| 118 | @Override |
| 119 | public boolean equals(final Object obj) { |
| 120 | if (this == obj) { |
| 121 | return true; |
| 122 | } |
| 123 | if (!super.equals(obj)) { |
| 124 | return false; |
| 125 | } |
| 126 | if (!(obj instanceof ICMPEcho)) { |
| 127 | return false; |
| 128 | } |
| 129 | final ICMPEcho other = (ICMPEcho) obj; |
| 130 | |
| 131 | if (this.identifier != other.identifier) { |
| 132 | return false; |
| 133 | } |
| 134 | if (this.sequenceNum != other.sequenceNum) { |
| 135 | return false; |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Deserializer function for ICMPEcho packets. |
| 142 | * |
| 143 | * @return deserializer function |
| 144 | */ |
| 145 | public static Deserializer<ICMPEcho> deserializer() { |
| 146 | return (data, offset, length) -> { |
| 147 | checkInput(data, offset, length, ICMP_ECHO_HEADER_LENGTH); |
| 148 | |
| 149 | ICMPEcho icmp = new ICMPEcho(); |
| 150 | |
| 151 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 152 | |
| 153 | icmp.identifier = bb.getShort(); |
| 154 | icmp.sequenceNum = bb.getShort(); |
| 155 | |
| 156 | return icmp; |
| 157 | }; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public String toString() { |
| 162 | return toStringHelper(getClass()) |
| 163 | .add("ICMP echo identifier", Short.toString(identifier)) |
| 164 | .add("ICMP echo sequenceNumber", Short.toString(sequenceNum)) |
| 165 | .toString(); |
| 166 | } |
| 167 | } |