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
28
29 @Deprecated
30 public enum InternetProtocolFamily {
31 IPv4(Inet4Address.class, 1),
32 IPv6(Inet6Address.class, 2);
33
34 private final Class<? extends InetAddress> addressType;
35 private final int addressNumber;
36
37 InternetProtocolFamily(Class<? extends InetAddress> addressType, int addressNumber) {
38 this.addressType = addressType;
39 this.addressNumber = addressNumber;
40 }
41
42
43
44
45 public Class<? extends InetAddress> addressType() {
46 return addressType;
47 }
48
49
50
51
52
53
54 public int addressNumber() {
55 return addressNumber;
56 }
57
58
59
60
61 public InetAddress localhost() {
62 switch (this) {
63 case IPv4:
64 return NetUtil.LOCALHOST4;
65 case IPv6:
66 return NetUtil.LOCALHOST6;
67 default:
68 throw new IllegalStateException("Unsupported family " + this);
69 }
70 }
71
72
73
74
75 public static InternetProtocolFamily of(InetAddress address) {
76 if (address instanceof Inet4Address) {
77 return IPv4;
78 }
79 if (address instanceof Inet6Address) {
80 return IPv6;
81 }
82 throw new IllegalArgumentException("address " + address + " not supported");
83 }
84
85 public SocketProtocolFamily toSocketProtocolFamily() {
86 switch (this) {
87 case IPv4:
88 return SocketProtocolFamily.INET;
89 case IPv6:
90 return SocketProtocolFamily.INET6;
91 default:
92 throw new IllegalStateException();
93 }
94 }
95 }