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