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 | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 22 | import io.netty.buffer.Unpooled; |
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 | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 26 | import org.onosproject.lisp.msg.authentication.LispAuthenticationFactory; |
| 27 | import org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 28 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 29 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.MapRecordReader; |
| 30 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.MapRecordWriter; |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 33 | |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 34 | import java.util.Arrays; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 35 | import java.util.List; |
| 36 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 37 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 38 | import static org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum.valueOf; |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 39 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 40 | /** |
| 41 | * Default LISP map notify message class. |
| 42 | */ |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 43 | public final class DefaultLispMapNotify extends AbstractLispMessage |
| 44 | implements LispMapNotify { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 45 | |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 46 | private static final Logger log = LoggerFactory.getLogger(DefaultLispMapNotify.class); |
| 47 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 48 | private final long nonce; |
| 49 | private final short keyId; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 50 | private final short authDataLength; |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 51 | private final byte[] authData; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 52 | private final List<LispMapRecord> mapRecords; |
| 53 | |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 54 | static final NotifyWriter WRITER; |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 55 | |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 56 | static { |
| 57 | WRITER = new NotifyWriter(); |
| 58 | } |
| 59 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 60 | /** |
| 61 | * A private constructor that protects object instantiation from external. |
| 62 | * |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 63 | * @param nonce nonce |
| 64 | * @param keyId key identifier |
| 65 | * @param authData authentication data |
| 66 | * @param mapRecords a collection of map records |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 67 | */ |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 68 | private DefaultLispMapNotify(long nonce, short keyId, short authDataLength, |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 69 | byte[] authData, List<LispMapRecord> mapRecords) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 70 | this.nonce = nonce; |
| 71 | this.keyId = keyId; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 72 | this.authDataLength = authDataLength; |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 73 | this.authData = authData; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 74 | this.mapRecords = mapRecords; |
| 75 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 76 | |
| 77 | @Override |
| 78 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 79 | return LispType.LISP_MAP_NOTIFY; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
Yoonseon Han | ca814bf | 2016-09-12 11:37:48 -0700 | [diff] [blame] | 83 | public void writeTo(ByteBuf byteBuf) throws LispWriterException { |
| 84 | WRITER.writeTo(byteBuf, this); |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public Builder createBuilder() { |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 89 | return new DefaultNotifyBuilder(); |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 90 | } |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 91 | |
| 92 | @Override |
| 93 | public long getNonce() { |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 94 | return nonce; |
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 short getKeyId() { |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 104 | return keyId; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public short getAuthDataLength() { |
| 109 | return authDataLength; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 113 | public byte[] getAuthData() { |
| 114 | if (authData != null && authData.length != 0) { |
| 115 | return ImmutableByteSequence.copyFrom(authData).asArray(); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 116 | } else { |
| 117 | return new byte[0]; |
| 118 | } |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 122 | public List<LispMapRecord> getMapRecords() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 123 | return ImmutableList.copyOf(mapRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 124 | } |
| 125 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 126 | @Override |
| 127 | public String toString() { |
| 128 | return toStringHelper(this) |
| 129 | .add("type", getType()) |
| 130 | .add("nonce", nonce) |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 131 | .add("keyId", keyId) |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 132 | .add("authentication data length", authDataLength) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 133 | .add("authentication data", authData) |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 134 | .add("mapRecords", mapRecords).toString(); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public boolean equals(Object o) { |
| 139 | if (this == o) { |
| 140 | return true; |
| 141 | } |
| 142 | if (o == null || getClass() != o.getClass()) { |
| 143 | return false; |
| 144 | } |
| 145 | DefaultLispMapNotify that = (DefaultLispMapNotify) o; |
| 146 | return Objects.equal(nonce, that.nonce) && |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 147 | Objects.equal(keyId, that.keyId) && |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 148 | Objects.equal(authDataLength, that.authDataLength) && |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 149 | Arrays.equals(authData, that.authData); |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public int hashCode() { |
Jian Li | 42b3e43 | 2016-08-31 01:05:20 +0900 | [diff] [blame] | 154 | return Objects.hashCode(nonce, keyId, authDataLength) + |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 155 | Arrays.hashCode(authData); |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 156 | } |
| 157 | |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 158 | public static final class DefaultNotifyBuilder implements NotifyBuilder { |
| 159 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 160 | private long nonce; |
| 161 | private short keyId; |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 162 | private short authDataLength; |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 163 | private byte[] authData; |
| 164 | private String authKey; |
Jian Li | d4e6370 | 2016-08-30 18:29:20 +0900 | [diff] [blame] | 165 | private List<LispMapRecord> mapRecords = Lists.newArrayList(); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 166 | |
| 167 | @Override |
| 168 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 169 | return LispType.LISP_MAP_NOTIFY; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public NotifyBuilder withNonce(long nonce) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 174 | this.nonce = nonce; |
| 175 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 179 | public NotifyBuilder withKeyId(short keyId) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 180 | this.keyId = keyId; |
| 181 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 185 | public NotifyBuilder withAuthKey(String key) { |
| 186 | this.authKey = key; |
| 187 | return this; |
| 188 | } |
| 189 | |
| 190 | @Override |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 191 | public NotifyBuilder withAuthDataLength(short authDataLength) { |
| 192 | this.authDataLength = authDataLength; |
| 193 | return this; |
| 194 | } |
| 195 | |
| 196 | @Override |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 197 | public NotifyBuilder withAuthData(byte[] authData) { |
| 198 | if (authData != null) { |
| 199 | this.authData = authData; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 200 | } else { |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 201 | this.authData = new byte[0]; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 202 | } |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 203 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 207 | public NotifyBuilder withMapRecords(List<LispMapRecord> mapRecords) { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 208 | if (mapRecords != null) { |
| 209 | this.mapRecords = ImmutableList.copyOf(mapRecords); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 210 | } |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 211 | return this; |
| 212 | } |
| 213 | |
| 214 | @Override |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 215 | public LispMapNotify build() { |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 216 | |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 217 | // if authentication data is not specified, we will calculate it |
| 218 | if (authData == null) { |
| 219 | LispAuthenticationFactory factory = LispAuthenticationFactory.getInstance(); |
| 220 | |
| 221 | authDataLength = LispAuthenticationKeyEnum.valueOf(keyId).getHashLength(); |
| 222 | byte[] tmpAuthData = new byte[authDataLength]; |
| 223 | Arrays.fill(tmpAuthData, (byte) 0); |
| 224 | authData = tmpAuthData; |
| 225 | |
| 226 | ByteBuf byteBuf = Unpooled.buffer(); |
| 227 | try { |
| 228 | new DefaultLispMapNotify(nonce, keyId, authDataLength, |
| 229 | authData, mapRecords).writeTo(byteBuf); |
| 230 | } catch (LispWriterException e) { |
| 231 | log.warn("Failed to serialize map notify message", e); |
| 232 | } |
| 233 | |
| 234 | byte[] bytes = new byte[byteBuf.readableBytes()]; |
| 235 | byteBuf.readBytes(bytes); |
| 236 | |
| 237 | if (authKey == null) { |
| 238 | log.warn("Must specify authentication key"); |
| 239 | } |
| 240 | |
| 241 | authData = factory.createAuthenticationData(valueOf(keyId), authKey, bytes); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame] | 242 | } |
| 243 | |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 244 | return new DefaultLispMapNotify(nonce, keyId, authDataLength, authData, mapRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 245 | } |
| 246 | } |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 247 | |
| 248 | /** |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 249 | * A LISP message reader for MapNotify message. |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 250 | */ |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 251 | public static final class NotifyReader implements LispMessageReader<LispMapNotify> { |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 252 | |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 253 | private static final int RESERVED_SKIP_LENGTH = 3; |
| 254 | |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 255 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame] | 256 | public LispMapNotify readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 257 | |
| 258 | if (byteBuf.readerIndex() != 0) { |
| 259 | return null; |
| 260 | } |
| 261 | |
| 262 | // skip first three bytes as they represent type and reserved fields |
| 263 | byteBuf.skipBytes(RESERVED_SKIP_LENGTH); |
| 264 | |
| 265 | // record count -> 8 bits |
| 266 | byte recordCount = (byte) byteBuf.readUnsignedByte(); |
| 267 | |
| 268 | // nonce -> 64 bits |
| 269 | long nonce = byteBuf.readLong(); |
| 270 | |
| 271 | // keyId -> 16 bits |
| 272 | short keyId = byteBuf.readShort(); |
| 273 | |
| 274 | // authenticationDataLength -> 16 bits |
| 275 | short authLength = byteBuf.readShort(); |
| 276 | |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 277 | // authData -> depends on the authenticationDataLength |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 278 | byte[] authData = new byte[authLength]; |
| 279 | byteBuf.readBytes(authData); |
| 280 | |
| 281 | List<LispMapRecord> mapRecords = Lists.newArrayList(); |
| 282 | for (int i = 0; i < recordCount; i++) { |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 283 | mapRecords.add(new MapRecordReader().readFrom(byteBuf)); |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | return new DefaultNotifyBuilder() |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 287 | .withNonce(nonce) |
| 288 | .withKeyId(keyId) |
| 289 | .withAuthDataLength(authLength) |
| 290 | .withAuthData(authData) |
| 291 | .withMapRecords(mapRecords) |
| 292 | .build(); |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 293 | } |
| 294 | } |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 295 | |
| 296 | /** |
| 297 | * A LISP message reader for MapNotify message. |
| 298 | */ |
| 299 | public static final class NotifyWriter implements LispMessageWriter<LispMapNotify> { |
| 300 | |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 301 | private static final int NOTIFY_SHIFT_BIT = 4; |
| 302 | |
| 303 | private static final int UNUSED_ZERO = 0; |
| 304 | |
| 305 | @Override |
| 306 | public void writeTo(ByteBuf byteBuf, LispMapNotify message) throws LispWriterException { |
| 307 | |
| 308 | // specify LISP message type |
Jian Li | cbc57e3 | 2016-09-14 09:06:54 +0900 | [diff] [blame] | 309 | byte msgType = (byte) (LispType.LISP_MAP_NOTIFY.getTypeCode() << NOTIFY_SHIFT_BIT); |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 310 | byteBuf.writeByte(msgType); |
| 311 | |
| 312 | // reserved field |
| 313 | byteBuf.writeShort((short) UNUSED_ZERO); |
| 314 | |
| 315 | // record count |
Jian Li | 42b3e43 | 2016-08-31 01:05:20 +0900 | [diff] [blame] | 316 | byteBuf.writeByte(message.getMapRecords().size()); |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 317 | |
| 318 | // nonce |
| 319 | byteBuf.writeLong(message.getNonce()); |
| 320 | |
| 321 | // keyId |
| 322 | byteBuf.writeShort(message.getKeyId()); |
| 323 | |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 324 | // authentication data and its length |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 325 | if (message.getAuthData() == null) { |
Jian Li | f8c2d4a | 2016-09-15 02:33:12 +0900 | [diff] [blame] | 326 | byteBuf.writeShort((short) 0); |
| 327 | } else { |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 328 | byteBuf.writeShort(message.getAuthData().length); |
| 329 | byteBuf.writeBytes(message.getAuthData()); |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 330 | } |
| 331 | |
Jian Li | edc5db1 | 2016-08-23 17:30:19 +0900 | [diff] [blame] | 332 | // serialize map records |
| 333 | MapRecordWriter writer = new MapRecordWriter(); |
| 334 | List<LispMapRecord> records = message.getMapRecords(); |
| 335 | |
| 336 | for (int i = 0; i < records.size(); i++) { |
| 337 | writer.writeTo(byteBuf, records.get(i)); |
| 338 | } |
| 339 | } |
| 340 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 341 | } |