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