View Javadoc

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    *   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, 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 io.netty.util.internal.ObjectUtil;
19  
20  import java.io.IOException;
21  import java.net.InetAddress;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.Map;
26  import java.util.Map.Entry;
27  
28  final class TcpMd5Util {
29  
30      static Collection<InetAddress> newTcpMd5Sigs(AbstractEpollChannel channel, Collection<InetAddress> current,
31                                           Map<InetAddress, byte[]> newKeys) throws IOException {
32          ObjectUtil.checkNotNull(channel, "channel");
33          ObjectUtil.checkNotNull(current, "current");
34          ObjectUtil.checkNotNull(newKeys, "newKeys");
35  
36          // Validate incoming values
37          for (Entry<InetAddress, byte[]> e : newKeys.entrySet()) {
38              final byte[] key = e.getValue();
39              if (e.getKey() == null) {
40                  throw new IllegalArgumentException("newKeys contains an entry with null address: " + newKeys);
41              }
42              if (key == null) {
43                  throw new NullPointerException("newKeys[" + e.getKey() + ']');
44              }
45              if (key.length == 0) {
46                  throw new IllegalArgumentException("newKeys[" + e.getKey() + "] has an empty key.");
47              }
48              if (key.length > Native.TCP_MD5SIG_MAXKEYLEN) {
49                  throw new IllegalArgumentException("newKeys[" + e.getKey() +
50                      "] has a key with invalid length; should not exceed the maximum length (" +
51                          Native.TCP_MD5SIG_MAXKEYLEN + ')');
52              }
53          }
54  
55          // Remove mappings not present in the new set.
56          for (InetAddress addr : current) {
57              if (!newKeys.containsKey(addr)) {
58                  Native.setTcpMd5Sig(channel.fd().intValue(), addr, null);
59              }
60          }
61  
62          if (newKeys.isEmpty()) {
63              return Collections.emptySet();
64          }
65  
66          // Set new mappings and store addresses which we set.
67          final Collection<InetAddress> addresses = new ArrayList<InetAddress>(newKeys.size());
68          for (Entry<InetAddress, byte[]> e : newKeys.entrySet()) {
69              Native.setTcpMd5Sig(channel.fd().intValue(), e.getKey(), e.getValue());
70              addresses.add(e.getKey());
71          }
72  
73          return addresses;
74      }
75  
76      private TcpMd5Util() {
77      }
78  }