查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2015 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.channel.epoll;
17  
18  import static io.netty.util.internal.ObjectUtil.checkNotNull;
19  import static io.netty.util.internal.ObjectUtil.checkNotNullWithIAE;
20  import static io.netty.util.internal.ObjectUtil.checkNonEmpty;
21  
22  import java.io.IOException;
23  import java.net.InetAddress;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.Map;
28  import java.util.Map.Entry;
29  
30  final class TcpMd5Util {
31  
32      static Collection<InetAddress> newTcpMd5Sigs(AbstractEpollChannel channel, Collection<InetAddress> current,
33                                           Map<InetAddress, byte[]> newKeys) throws IOException {
34          checkNotNull(channel, "channel");
35          checkNotNull(current, "current");
36          checkNotNull(newKeys, "newKeys");
37  
38          // Validate incoming values
39          for (Entry<InetAddress, byte[]> e : newKeys.entrySet()) {
40              final byte[] key = e.getValue();
41              checkNotNullWithIAE(e.getKey(), "e.getKey");
42              checkNonEmpty(key, e.getKey().toString());
43              if (key.length > Native.TCP_MD5SIG_MAXKEYLEN) {
44                  throw new IllegalArgumentException("newKeys[" + e.getKey() +
45                      "] has a key with invalid length; should not exceed the maximum length (" +
46                          Native.TCP_MD5SIG_MAXKEYLEN + ')');
47              }
48          }
49  
50          // Remove mappings not present in the new set.
51          for (InetAddress addr : current) {
52              if (!newKeys.containsKey(addr)) {
53                  channel.socket.setTcpMd5Sig(addr, null);
54              }
55          }
56  
57          if (newKeys.isEmpty()) {
58              return Collections.emptySet();
59          }
60  
61          // Set new mappings and store addresses which we set.
62          final Collection<InetAddress> addresses = new ArrayList<InetAddress>(newKeys.size());
63          for (Entry<InetAddress, byte[]> e : newKeys.entrySet()) {
64              channel.socket.setTcpMd5Sig(e.getKey(), e.getValue());
65              addresses.add(e.getKey());
66          }
67  
68          return addresses;
69      }
70  
71      private TcpMd5Util() {
72      }
73  }