1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.socket;
17
18 import io.netty.util.NetUtil;
19
20 import java.net.Inet4Address;
21 import java.net.Inet6Address;
22 import java.net.InetAddress;
23
24
25
26
27 public enum InternetProtocolFamily {
28 IPv4(Inet4Address.class, 1),
29 IPv6(Inet6Address.class, 2);
30
31 private final Class<? extends InetAddress> addressType;
32 private final int addressNumber;
33
34 InternetProtocolFamily(Class<? extends InetAddress> addressType, int addressNumber) {
35 this.addressType = addressType;
36 this.addressNumber = addressNumber;
37 }
38
39
40
41
42 public Class<? extends InetAddress> addressType() {
43 return addressType;
44 }
45
46
47
48
49
50
51 public int addressNumber() {
52 return addressNumber;
53 }
54
55
56
57
58 public InetAddress localhost() {
59 switch (this) {
60 case IPv4:
61 return NetUtil.LOCALHOST4;
62 case IPv6:
63 return NetUtil.LOCALHOST6;
64 default:
65 throw new IllegalStateException("Unsupported family " + this);
66 }
67 }
68
69
70
71
72 public static InternetProtocolFamily of(InetAddress address) {
73 if (address instanceof Inet4Address) {
74 return IPv4;
75 }
76 if (address instanceof Inet6Address) {
77 return IPv6;
78 }
79 throw new IllegalArgumentException("address " + address + " not supported");
80 }
81 }