1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
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 }