View Javadoc
1   /*
2    * Copyright 2020 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.quic;
17  
18  import java.nio.ByteBuffer;
19  
20  /**
21   * Creates new connection id instances.
22   */
23  public interface QuicConnectionIdGenerator {
24      /**
25       * Creates a new connection id with the given length. This method may not be supported by
26       * a sign id generator implementation as a sign id generator should always have an input
27       * to sign with, otherwise this method may generate the same id which may cause some
28       * unpredictable issues when we use it.
29       *
30       * @param length    the length of the id.
31       * @return          the id.
32       */
33      ByteBuffer newId(int length);
34  
35      /**
36       * Creates a new connection id with the given length. The given input may be used to sign or
37       * seed the id, or may be ignored (depending on the implementation).
38       *
39       * @param input     the input which may be used to generate the id.
40       * @param length    the length of the id.
41       * @return          the id.
42       */
43      ByteBuffer newId(ByteBuffer input, int length);
44  
45      /**
46       * Creates a new connection id with the given length. The given source connection id and destionation connection id
47       * may be used to sign or seed the id, or may be ignored (depending on the implementation).
48       *
49       * @param scid      the source connection id which may be used to generate the id.
50       * @param dcid      the destination connection id which may be used to generate the id.
51       * @param length    the length of the id.
52       * @return          the id.
53       */
54      default ByteBuffer newId(ByteBuffer scid, ByteBuffer dcid, int length) {
55          return newId(dcid, length);
56      }
57  
58      /**
59       * Returns the maximum length of a connection id.
60       *
61       * @return the maximum length of a connection id that is supported.
62       */
63      int maxConnectionIdLength();
64  
65      /**
66       * Returns true if the implementation is idempotent, which means we will get the same id
67       * with the same input ByteBuffer. Otherwise, returns false.
68       *
69       * @return whether the implementation is idempotent.
70       */
71      boolean isIdempotent();
72  
73      /**
74       * Return a {@link QuicConnectionIdGenerator} which randomly generates new connection ids.
75       *
76       * @return a {@link QuicConnectionIdGenerator} which randomly generated ids.
77       */
78      static QuicConnectionIdGenerator randomGenerator() {
79          return SecureRandomQuicConnectionIdGenerator.INSTANCE;
80      }
81  
82      /**
83       * Return a {@link QuicConnectionIdGenerator} which generates new connection ids by signing the given input.
84       *
85       * @return a {@link QuicConnectionIdGenerator} which generates ids by signing the given input.
86       */
87      static QuicConnectionIdGenerator signGenerator() {
88          return HmacSignQuicConnectionIdGenerator.INSTANCE;
89      }
90  }