1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.channel.epoll;
17
18 import java.io.IOException;
19 import java.net.InetAddress;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Map;
24 import java.util.Map.Entry;
25
26 import static io.netty5.util.internal.ObjectUtil.checkNonEmpty;
27 import static io.netty5.util.internal.ObjectUtil.checkNotNullWithIAE;
28 import static java.util.Objects.requireNonNull;
29
30 final class TcpMd5Util {
31
32 static Collection<InetAddress> newTcpMd5Sigs(AbstractEpollChannel channel, Collection<InetAddress> current,
33 Map<InetAddress, byte[]> newKeys) throws IOException {
34 requireNonNull(channel, "channel");
35 requireNonNull(current, "current");
36 requireNonNull(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<>(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 }