Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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.onosproject.lisp.msg.protocols; |
| 17 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 18 | import com.google.common.base.Objects; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 21 | import io.netty.buffer.ByteBuf; |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 22 | import org.onlab.util.ByteOperator; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 23 | import org.onlab.util.ImmutableByteSequence; |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 24 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 25 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 26 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 27 | |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 28 | import java.util.Arrays; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 29 | import java.util.List; |
| 30 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 31 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 32 | import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.MapRecordReader; |
| 33 | import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.MapRecordWriter; |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 34 | |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 35 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 36 | /** |
| 37 | * Default LISP map register message class. |
| 38 | */ |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 39 | public final class DefaultLispMapRegister implements LispMapRegister { |
| 40 | |
| 41 | private final long nonce; |
| 42 | private final short keyId; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 43 | private final short authDataLength; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 44 | private final byte[] authenticationData; |
| 45 | private final byte recordCount; |
| 46 | private final List<LispMapRecord> mapRecords; |
| 47 | private final boolean proxyMapReply; |
| 48 | private final boolean wantMapNotify; |
| 49 | |
| 50 | /** |
| 51 | * A private constructor that protects object instantiation from external. |
| 52 | * |
| 53 | * @param nonce nonce |
| 54 | * @param keyId key identifier |
| 55 | * @param authenticationData authentication data |
| 56 | * @param recordCount record count number |
| 57 | * @param mapRecords a collection of map records |
| 58 | * @param proxyMapReply proxy map reply flag |
| 59 | * @param wantMapNotify want map notify flag |
| 60 | */ |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 61 | private DefaultLispMapRegister(long nonce, short keyId, short authDataLength, |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 62 | byte[] authenticationData, byte recordCount, |
| 63 | List<LispMapRecord> mapRecords, |
| 64 | boolean proxyMapReply, boolean wantMapNotify) { |
| 65 | this.nonce = nonce; |
| 66 | this.keyId = keyId; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 67 | this.authDataLength = authDataLength; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 68 | this.authenticationData = authenticationData; |
| 69 | this.recordCount = recordCount; |
| 70 | this.mapRecords = mapRecords; |
| 71 | this.proxyMapReply = proxyMapReply; |
| 72 | this.wantMapNotify = wantMapNotify; |
| 73 | } |
| 74 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 75 | @Override |
| 76 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 77 | return LispType.LISP_MAP_REGISTER; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void writeTo(ByteBuf byteBuf) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 82 | // TODO: serialize LispMapRegister message |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Builder createBuilder() { |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 87 | return new DefaultRegisterBuilder(); |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 88 | } |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 89 | |
| 90 | @Override |
| 91 | public boolean isProxyMapReply() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 92 | return proxyMapReply; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean isWantMapNotify() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 97 | return wantMapNotify; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public byte getRecordCount() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 102 | return recordCount; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public long getNonce() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 107 | return nonce; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public short getKeyId() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 112 | return keyId; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 116 | public short getAuthDataLength() { |
| 117 | return authDataLength; |
| 118 | } |
| 119 | |
| 120 | @Override |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 121 | public byte[] getAuthenticationData() { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 122 | if (authenticationData != null && authenticationData.length != 0) { |
| 123 | return ImmutableByteSequence.copyFrom(authenticationData).asArray(); |
| 124 | } else { |
| 125 | return new byte[0]; |
| 126 | } |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 130 | public List<LispMapRecord> getMapRecords() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 131 | return ImmutableList.copyOf(mapRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 132 | } |
| 133 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 134 | @Override |
| 135 | public String toString() { |
| 136 | return toStringHelper(this) |
| 137 | .add("type", getType()) |
| 138 | .add("nonce", nonce) |
| 139 | .add("recordCount", recordCount) |
| 140 | .add("keyId", keyId) |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 141 | .add("authentication data length", authDataLength) |
| 142 | .add("authentication data", authenticationData) |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 143 | .add("mapRecords", mapRecords) |
| 144 | .add("proxyMapReply", proxyMapReply) |
| 145 | .add("wantMapNotify", wantMapNotify).toString(); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public boolean equals(Object o) { |
| 150 | if (this == o) { |
| 151 | return true; |
| 152 | } |
| 153 | if (o == null || getClass() != o.getClass()) { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | DefaultLispMapRegister that = (DefaultLispMapRegister) o; |
| 158 | return Objects.equal(nonce, that.nonce) && |
| 159 | Objects.equal(recordCount, that.recordCount) && |
| 160 | Objects.equal(keyId, that.keyId) && |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 161 | Objects.equal(authDataLength, that.authDataLength) && |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 162 | Arrays.equals(authenticationData, that.authenticationData) && |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 163 | Objects.equal(proxyMapReply, that.proxyMapReply) && |
| 164 | Objects.equal(wantMapNotify, that.wantMapNotify); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public int hashCode() { |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 169 | return Objects.hashCode(nonce, recordCount, keyId, authDataLength, |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 170 | proxyMapReply, wantMapNotify) + Arrays.hashCode(authenticationData); |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 171 | } |
| 172 | |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 173 | public static final class DefaultRegisterBuilder implements RegisterBuilder { |
| 174 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 175 | private long nonce; |
| 176 | private short keyId; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 177 | private short authDataLength; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 178 | private byte[] authenticationData; |
| 179 | private byte recordCount; |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 180 | private List<LispMapRecord> mapRecords; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 181 | private boolean proxyMapReply; |
| 182 | private boolean wantMapNotify; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 183 | |
| 184 | @Override |
| 185 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 186 | return LispType.LISP_MAP_REGISTER; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 190 | public RegisterBuilder withIsProxyMapReply(boolean proxyMapReply) { |
| 191 | this.proxyMapReply = proxyMapReply; |
| 192 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 196 | public RegisterBuilder withIsWantMapNotify(boolean wantMapNotify) { |
| 197 | this.wantMapNotify = wantMapNotify; |
| 198 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public RegisterBuilder withRecordCount(byte recordCount) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 203 | this.recordCount = recordCount; |
| 204 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public RegisterBuilder withNonce(long nonce) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 209 | this.nonce = nonce; |
| 210 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 214 | public RegisterBuilder withAuthDataLength(short authDataLength) { |
| 215 | this.authDataLength = authDataLength; |
| 216 | return this; |
| 217 | } |
| 218 | |
| 219 | @Override |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 220 | public RegisterBuilder withKeyId(short keyId) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 221 | this.keyId = keyId; |
| 222 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public RegisterBuilder withAuthenticationData(byte[] authenticationData) { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 227 | if (authenticationData != null) { |
| 228 | this.authenticationData = authenticationData; |
| 229 | } else { |
| 230 | this.authenticationData = new byte[0]; |
| 231 | } |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 232 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 236 | public RegisterBuilder withMapRecords(List<LispMapRecord> mapRecords) { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 237 | if (mapRecords != null) { |
| 238 | this.mapRecords = ImmutableList.copyOf(mapRecords); |
| 239 | } else { |
| 240 | this.mapRecords = Lists.newArrayList(); |
| 241 | } |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 242 | return this; |
| 243 | } |
| 244 | |
| 245 | @Override |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 246 | public LispMapRegister build() { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 247 | if (authenticationData == null) { |
| 248 | authenticationData = new byte[0]; |
| 249 | } |
| 250 | |
| 251 | if (mapRecords == null) { |
| 252 | mapRecords = Lists.newArrayList(); |
| 253 | } |
| 254 | |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 255 | return new DefaultLispMapRegister(nonce, keyId, authDataLength, |
| 256 | authenticationData, recordCount, mapRecords, proxyMapReply, wantMapNotify); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 257 | } |
| 258 | } |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 259 | |
| 260 | /** |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 261 | * A LISP message reader for MapRegister message. |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 262 | */ |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 263 | public static final class RegisterReader implements LispMessageReader<LispMapRegister> { |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 264 | |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 265 | private static final int PROXY_MAP_REPLY_INDEX = 3; |
| 266 | private static final int WANT_MAP_NOTIFY_INDEX = 0; |
| 267 | private static final int RESERVED_SKIP_LENGTH = 1; |
| 268 | |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 269 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 270 | public LispMapRegister readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 271 | |
| 272 | if (byteBuf.readerIndex() != 0) { |
| 273 | return null; |
| 274 | } |
| 275 | |
| 276 | // proxyMapReply -> 1 bit |
| 277 | boolean proxyMapReplyFlag = ByteOperator.getBit(byteBuf.readByte(), PROXY_MAP_REPLY_INDEX); |
| 278 | |
| 279 | // let's skip the reserved field |
| 280 | byteBuf.skipBytes(RESERVED_SKIP_LENGTH); |
| 281 | |
| 282 | byte reservedWithFlag = byteBuf.readByte(); |
| 283 | |
| 284 | // wantMapReply -> 1 bit |
| 285 | boolean wantMapNotifyFlag = ByteOperator.getBit(reservedWithFlag, WANT_MAP_NOTIFY_INDEX); |
| 286 | |
| 287 | // record count -> 8 bits |
| 288 | byte recordCount = (byte) byteBuf.readUnsignedByte(); |
| 289 | |
| 290 | // nonce -> 64 bits |
| 291 | long nonce = byteBuf.readLong(); |
| 292 | |
| 293 | // keyId -> 16 bits |
| 294 | short keyId = byteBuf.readShort(); |
| 295 | |
| 296 | // authenticationDataLength -> 16 bits |
| 297 | short authLength = byteBuf.readShort(); |
| 298 | |
| 299 | // authenticationData -> depends on the authenticationDataLength |
| 300 | byte[] authData = new byte[authLength]; |
| 301 | byteBuf.readBytes(authData); |
| 302 | |
| 303 | List<LispMapRecord> mapRecords = Lists.newArrayList(); |
| 304 | for (int i = 0; i < recordCount; i++) { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 305 | mapRecords.add(new MapRecordReader().readFrom(byteBuf)); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | return new DefaultRegisterBuilder() |
| 309 | .withIsProxyMapReply(proxyMapReplyFlag) |
| 310 | .withIsWantMapNotify(wantMapNotifyFlag) |
| 311 | .withRecordCount(recordCount) |
| 312 | .withNonce(nonce) |
| 313 | .withKeyId(keyId) |
| 314 | .withAuthenticationData(authData) |
| 315 | .withMapRecords(mapRecords) |
| 316 | .build(); |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 317 | } |
| 318 | } |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 319 | |
| 320 | /** |
| 321 | * LISP map register message writer class. |
| 322 | */ |
| 323 | public static class RegisterWriter implements LispMessageWriter<LispMapRegister> { |
| 324 | |
| 325 | private static final int REGISTER_MSG_TYPE = 3; |
| 326 | private static final int REGISTER_SHIFT_BIT = 4; |
| 327 | |
| 328 | private static final int PROXY_MAP_REPLY_SHIFT_BIT = 3; |
| 329 | |
| 330 | private static final int ENABLE_BIT = 1; |
| 331 | private static final int DISABLE_BIT = 0; |
| 332 | |
| 333 | private static final int UNUSED_ZERO = 0; |
| 334 | |
| 335 | @Override |
| 336 | public void writeTo(ByteBuf byteBuf, LispMapRegister message) throws LispWriterException { |
| 337 | |
| 338 | // specify LISP message type |
| 339 | byte msgType = (byte) (REGISTER_MSG_TYPE << REGISTER_SHIFT_BIT); |
| 340 | |
| 341 | // proxy map reply flag |
| 342 | byte proxyMapReply = DISABLE_BIT; |
| 343 | if (message.isProxyMapReply()) { |
| 344 | proxyMapReply = (byte) (ENABLE_BIT << PROXY_MAP_REPLY_SHIFT_BIT); |
| 345 | } |
| 346 | |
| 347 | byteBuf.writeByte(msgType + proxyMapReply); |
| 348 | |
| 349 | // fill zero into reserved field |
| 350 | byteBuf.writeByte((short) UNUSED_ZERO); |
| 351 | |
| 352 | // want map notify flag |
| 353 | byte wantMapNotify = DISABLE_BIT; |
| 354 | if (message.isWantMapNotify()) { |
| 355 | wantMapNotify = (byte) ENABLE_BIT; |
| 356 | } |
| 357 | |
| 358 | byteBuf.writeByte(wantMapNotify); |
| 359 | |
| 360 | // record count |
| 361 | byteBuf.writeByte(message.getRecordCount()); |
| 362 | |
| 363 | // nonce |
| 364 | byteBuf.writeLong(message.getNonce()); |
| 365 | |
| 366 | // keyId |
| 367 | byteBuf.writeShort(message.getKeyId()); |
| 368 | |
| 369 | // authentication data length in octet |
| 370 | byteBuf.writeShort(message.getAuthDataLength()); |
| 371 | |
| 372 | // authentication data |
| 373 | byte[] data = message.getAuthenticationData(); |
| 374 | byte[] clone; |
| 375 | if (data != null) { |
| 376 | clone = data.clone(); |
| 377 | Arrays.fill(clone, (byte) UNUSED_ZERO); |
| 378 | } |
| 379 | |
| 380 | byteBuf.writeBytes(data); |
| 381 | |
| 382 | // TODO: need to implement MAC authentication mechanism |
| 383 | |
| 384 | // serialize map records |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 385 | MapRecordWriter writer = new MapRecordWriter(); |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 386 | List<LispMapRecord> records = message.getMapRecords(); |
| 387 | |
| 388 | for (int i = 0; i < records.size(); i++) { |
| 389 | writer.writeTo(byteBuf, records.get(i)); |
| 390 | } |
| 391 | } |
| 392 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 393 | } |