blob: f77f6ddbc24fe725afdba3a95334d204b53e180c [file] [log] [blame]
Kalhee Kim6222dbe2017-10-26 15:44:37 +00001/*
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
17package org.onlab.packet;
18
19import org.slf4j.Logger;
20
21import java.nio.ByteBuffer;
22import java.util.Objects;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25
26import static org.slf4j.LoggerFactory.getLogger;
27
28/*
29 * Authentication Entry for RIP version 2 - RFC 2082
30 */
31public class RIPV2AuthEntry extends BasePacket {
32 private static final int ENTRY_LEN = 20;
33 private final Logger log = getLogger(getClass());
34 protected short addressFamilyId;
35 protected short type;
36 protected short offset;
37 protected byte keyId;
38 protected byte authLen;
39 protected int sequence;
40
41
42 @Override
43 public byte[] serialize() {
44 ByteBuffer byteBuffer;
45 byteBuffer = ByteBuffer.allocate(ENTRY_LEN);
46 byteBuffer.putShort(addressFamilyId);
47 byteBuffer.putShort(type);
48 byteBuffer.putShort(offset);
49 byteBuffer.put(keyId);
50 byteBuffer.put(authLen);
51 byteBuffer.putInt(sequence);
52 byteBuffer.putInt(0);
53 byteBuffer.putInt(0);
54 return byteBuffer.array();
55 }
56
57 /**
58 * Deserializer function for RIPv2 entry.
59 *
60 * @return deserializer function
61 */
62 public static Deserializer<RIPV2AuthEntry> deserializer() {
63 return (data, offset, length) -> {
64 RIPV2AuthEntry authEntry = new RIPV2AuthEntry();
65
66 checkNotNull(data);
67
68 if (offset < 0 || length < 0 ||
69 length > data.length || offset >= data.length ||
70 offset + length > data.length) {
71 throw new DeserializationException("Illegal offset or length");
72 }
73 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
74 if (bb.remaining() < ENTRY_LEN) {
75 throw new DeserializationException(
76 "Buffer underflow while reading RIP authentication entry");
77 }
78 authEntry.addressFamilyId = bb.getShort();
79 authEntry.type = bb.getShort();
80 authEntry.offset = bb.getShort();
81 authEntry.keyId = bb.get();
82 authEntry.authLen = bb.get();
83 authEntry.sequence = bb.getInt();
84 return authEntry;
85 };
86 }
87
88 /*
89 * (non-Javadoc)
90 *
91 * @see java.lang.Object#hashCode()
92 */
93 @Override
94 public int hashCode() {
95 return Objects.hash(super.hashCode(), sequence, authLen, keyId, offset, addressFamilyId, type);
96 }
97
98 /*
99 * (non-Javadoc)
100 *
101 * @see java.lang.Object#equals(java.lang.Object)
102 */
103 @Override
104 public boolean equals(final Object obj) {
105 if (this == obj) {
106 return true;
107 }
108 if (!(obj instanceof RIPV2AuthEntry)) {
109 return false;
110 }
111 final RIPV2AuthEntry that = (RIPV2AuthEntry) obj;
112
113 return super.equals(that) &&
114 Objects.equals(type, that.type) &&
115 Objects.equals(addressFamilyId, that.addressFamilyId) &&
116 Objects.equals(offset, that.offset) &&
117 Objects.equals(keyId, that.keyId) &&
118 Objects.equals(authLen, that.authLen) &&
119 Objects.equals(sequence, that.sequence);
120 }
121
122 /**
123 * @return the Address Family Identifier
124 */
125 public short getAddressFamilyId() {
126 return this.addressFamilyId;
127 }
128
129 /**
130 * @param addressFamilyIdentifier the address family identifier to set
131 * @return this
132 */
133 public RIPV2AuthEntry setAddressFamilyId(final short addressFamilyIdentifier) {
134 this.addressFamilyId = addressFamilyIdentifier;
135 return this;
136 }
137
138 /**
139 * @return the authentication type
140 */
141 public short getType() {
142 return this.type;
143 }
144
145 /**
146 * @param type the authentication type to set
147 * @return this
148 */
149 public RIPV2AuthEntry setType(final short type) {
150 this.type = type;
151 return this;
152 }
153
154 /**
155 * @return the offset of authentication data
156 */
157 public short getOffset() {
158 return this.offset;
159 }
160
161 /**
162 * @param offset the offset of authentication data to set
163 * @return this
164 */
165 public RIPV2AuthEntry setOffset(final short offset) {
166 this.offset = offset;
167 return this;
168 }
169 /**
170 * @return the subnet mask
171 */
172 public byte getKeyId() {
173 return this.keyId;
174 }
175
176 /**
177 * @param keyId The key id to set
178 * @return this
179 */
180 public RIPV2AuthEntry setKeyId(final byte keyId) {
181 this.keyId = keyId;
182 return this;
183 }
184
185 /**
186 * @return the authentication data length
187 */
188 public byte getAuthLen() {
189 return this.authLen;
190 }
191
192 /**
193 * @param authlen the length of the authentication data to set
194 * @return this
195 */
196 public RIPV2AuthEntry setAuthLen(final byte authlen) {
197 this.authLen = authlen;
198 return this;
199 }
200
201
202 /**
203 * @return the sequence number
204 */
205 public int getSequence() {
206 return this.sequence;
207 }
208
209 /**
210 * @param sequencenumber sequence number to set
211 * @return this
212 */
213 public RIPV2AuthEntry setSequenceNumber(final int sequencenumber) {
214 this.sequence = sequencenumber;
215 return this;
216 }
217
218 /*
219 * (non-Javadoc)
220 *
221 * @see java.lang.Object#toString()
222 */
223 @Override
224 public String toString() {
225 return "RIPV2AuthEntry [address family Id=" + this.addressFamilyId + ", type=" + this.type
226 + ", offset=" + this.offset
227 + ", key ID=" + this.keyId
228 + ", authentication length = " + this.authLen
229 + ", sequence number=" + this.sequence + "]";
230 }
231}