Jian Li | 2775935 | 2016-10-04 20:14:42 +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 | |
| 18 | import com.google.common.base.Objects; |
| 19 | import io.netty.buffer.ByteBuf; |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 20 | import io.netty.buffer.Unpooled; |
| 21 | import org.onosproject.lisp.msg.authentication.LispAuthenticationFactory; |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 22 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 23 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 24 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
| 25 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 26 | import org.onosproject.lisp.msg.types.LispNoAddress; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame^] | 27 | import org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum; |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 30 | |
| 31 | import java.util.Arrays; |
| 32 | |
| 33 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 34 | import static org.onosproject.lisp.msg.authentication.LispAuthenticationKeyEnum.valueOf; |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Default LISP info request message class. |
| 38 | */ |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 39 | public class DefaultLispInfoRequest extends DefaultLispInfo |
| 40 | implements LispInfoRequest { |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 41 | |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 42 | private static final Logger log = |
| 43 | LoggerFactory.getLogger(DefaultLispInfoRequest.class); |
| 44 | |
| 45 | static final InfoRequestWriter WRITER; |
| 46 | |
| 47 | static { |
| 48 | WRITER = new InfoRequestWriter(); |
| 49 | } |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 50 | |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 51 | /** |
| 52 | * A private constructor that protects object instantiation from external. |
| 53 | * |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 54 | * @param infoReply info reply flag |
| 55 | * @param nonce nonce |
| 56 | * @param keyId key identifier |
| 57 | * @param authDataLength authentication data length |
| 58 | * @param authData authentication data |
| 59 | * @param ttl Time-To-Live value |
| 60 | * @param maskLength EID prefix mask length |
| 61 | * @param eidPrefix EID prefix |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 62 | */ |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 63 | protected DefaultLispInfoRequest(boolean infoReply, long nonce, short keyId, |
| 64 | short authDataLength, byte[] authData, |
| 65 | int ttl, byte maskLength, |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 66 | LispAfiAddress eidPrefix) { |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 67 | |
| 68 | super(infoReply, nonce, keyId, authDataLength, authData, ttl, |
| 69 | maskLength, eidPrefix); |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public String toString() { |
| 74 | return toStringHelper(this) |
| 75 | .add("type", getType()) |
| 76 | .add("nonce", nonce) |
| 77 | .add("keyId", keyId) |
| 78 | .add("authentication data length", authDataLength) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 79 | .add("authentication data", authData) |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 80 | .add("TTL", ttl) |
| 81 | .add("EID mask length", maskLength) |
| 82 | .add("EID prefix", eidPrefix).toString(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public boolean equals(Object o) { |
| 87 | if (this == o) { |
| 88 | return true; |
| 89 | } |
| 90 | if (o == null || getClass() != o.getClass()) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | DefaultLispInfoRequest that = (DefaultLispInfoRequest) o; |
| 95 | return Objects.equal(nonce, that.nonce) && |
| 96 | Objects.equal(keyId, that.keyId) && |
| 97 | Objects.equal(authDataLength, that.authDataLength) && |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 98 | Arrays.equals(authData, that.authData) && |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 99 | Objects.equal(ttl, that.ttl) && |
| 100 | Objects.equal(maskLength, that.maskLength) && |
| 101 | Objects.equal(eidPrefix, that.eidPrefix); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public int hashCode() { |
| 106 | return Objects.hashCode(nonce, keyId, authDataLength, ttl, maskLength, |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 107 | eidPrefix) + Arrays.hashCode(authData); |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 108 | } |
| 109 | |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 110 | @Override |
| 111 | public void writeTo(ByteBuf byteBuf) throws LispWriterException { |
| 112 | WRITER.writeTo(byteBuf, this); |
| 113 | } |
| 114 | |
| 115 | public static final class DefaultInfoRequestBuilder |
| 116 | implements InfoRequestBuilder { |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 117 | |
| 118 | private boolean infoReply; |
| 119 | private long nonce; |
| 120 | private short keyId; |
| 121 | private short authDataLength; |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 122 | private byte[] authData; |
| 123 | private String authKey; |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 124 | private int ttl; |
| 125 | private byte maskLength; |
| 126 | private LispAfiAddress eidPrefix; |
| 127 | |
| 128 | @Override |
| 129 | public LispType getType() { |
| 130 | return LispType.LISP_INFO; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | @Override |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 135 | public InfoRequestBuilder withIsInfoReply(boolean infoReply) { |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 136 | this.infoReply = infoReply; |
| 137 | return this; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public InfoRequestBuilder withNonce(long nonce) { |
| 142 | this.nonce = nonce; |
| 143 | return this; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public InfoRequestBuilder withAuthDataLength(short authDataLength) { |
| 148 | this.authDataLength = authDataLength; |
| 149 | return this; |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public InfoRequestBuilder withKeyId(short keyId) { |
| 154 | this.keyId = keyId; |
| 155 | return this; |
| 156 | } |
| 157 | |
| 158 | @Override |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 159 | public InfoRequestBuilder withAuthData(byte[] authenticationData) { |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 160 | if (authenticationData != null) { |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 161 | this.authData = authenticationData; |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 162 | } |
| 163 | return this; |
| 164 | } |
| 165 | |
| 166 | @Override |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 167 | public InfoRequestBuilder withAuthKey(String key) { |
| 168 | this.authKey = key; |
| 169 | return this; |
| 170 | } |
| 171 | |
| 172 | @Override |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 173 | public InfoRequestBuilder withTtl(int ttl) { |
| 174 | this.ttl = ttl; |
| 175 | return this; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public InfoRequestBuilder withMaskLength(byte maskLength) { |
| 180 | this.maskLength = maskLength; |
| 181 | return this; |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public InfoRequestBuilder withEidPrefix(LispAfiAddress eidPrefix) { |
| 186 | this.eidPrefix = eidPrefix; |
| 187 | return this; |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public LispInfoRequest build() { |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 192 | |
| 193 | // if authentication data is not specified, we will calculate it |
| 194 | if (authData == null) { |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 195 | LispAuthenticationFactory factory = |
| 196 | LispAuthenticationFactory.getInstance(); |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 197 | |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 198 | authDataLength = |
| 199 | LispAuthenticationKeyEnum.valueOf(keyId).getHashLength(); |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 200 | byte[] tmpAuthData = new byte[authDataLength]; |
| 201 | Arrays.fill(tmpAuthData, (byte) 0); |
| 202 | authData = tmpAuthData; |
| 203 | |
| 204 | ByteBuf byteBuf = Unpooled.buffer(); |
| 205 | try { |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 206 | new DefaultLispInfoRequest(infoReply, nonce, keyId, |
| 207 | authDataLength, authData, ttl, |
| 208 | maskLength, eidPrefix) |
| 209 | .writeTo(byteBuf); |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 210 | } catch (LispWriterException e) { |
| 211 | log.warn("Failed to serialize info request", e); |
| 212 | } |
| 213 | |
| 214 | byte[] bytes = new byte[byteBuf.readableBytes()]; |
| 215 | byteBuf.readBytes(bytes); |
| 216 | |
| 217 | if (authKey == null) { |
| 218 | log.warn("Must specify authentication key"); |
| 219 | } |
| 220 | |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 221 | authData = factory |
| 222 | .createAuthenticationData(valueOf(keyId), authKey, bytes); |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 223 | } |
| 224 | |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 225 | return new DefaultLispInfoRequest(infoReply, nonce, keyId, |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 226 | authDataLength, authData, ttl, maskLength, eidPrefix); |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * A LISP message reader for InfoRequest message. |
| 232 | */ |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 233 | public static class InfoRequestReader |
| 234 | implements LispMessageReader<LispInfoRequest> { |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 235 | |
| 236 | @Override |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 237 | public LispInfoRequest readFrom(ByteBuf byteBuf) |
| 238 | throws LispParseError, LispReaderException { |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 239 | |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame^] | 240 | LispInfo lispInfo = deserialize(byteBuf); |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 241 | |
| 242 | return new DefaultInfoRequestBuilder() |
Jian Li | 6ef1b3f | 2016-11-12 18:16:06 +0900 | [diff] [blame] | 243 | .withIsInfoReply(lispInfo.isInfoReply()) |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 244 | .withNonce(lispInfo.getNonce()) |
| 245 | .withKeyId(lispInfo.getKeyId()) |
| 246 | .withAuthDataLength(lispInfo.getAuthDataLength()) |
Jian Li | d1a109e | 2016-11-12 09:00:42 +0900 | [diff] [blame] | 247 | .withAuthData(lispInfo.getAuthData()) |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 248 | .withTtl(lispInfo.getTtl()) |
| 249 | .withMaskLength(lispInfo.getMaskLength()) |
| 250 | .withEidPrefix(lispInfo.getPrefix()).build(); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * A LISP message writer for InfoRequest message. |
| 256 | */ |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 257 | public static final class InfoRequestWriter |
| 258 | implements LispMessageWriter<LispInfoRequest> { |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 259 | |
| 260 | @Override |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 261 | public void writeTo(ByteBuf byteBuf, LispInfoRequest message) |
| 262 | throws LispWriterException { |
| 263 | |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame^] | 264 | serialize(byteBuf, message); |
yoonseon | 980cd7c | 2016-11-18 14:18:46 -0800 | [diff] [blame] | 265 | |
| 266 | //Fill AFI=0, no address |
| 267 | new LispAfiAddress.AfiAddressWriter() |
| 268 | .writeTo(byteBuf, new LispNoAddress()); |
| 269 | |
Jian Li | 2775935 | 2016-10-04 20:14:42 +0900 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |