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