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