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