1 /*
2 * Copyright 2012 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
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 * Internet Protocol (IP) families used by the {@link DatagramChannel}
26 *
27 * @deprecated use {@link SocketProtocolFamily}.
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 * Returns the address type of this protocol family.
44 */
45 public Class<? extends InetAddress> addressType() {
46 return addressType;
47 }
48
49 /**
50 * Returns the
51 * <a href="https://www.iana.org/assignments/address-family-numbers/address-family-numbers.xhtml">address number</a>
52 * of the family.
53 */
54 public int addressNumber() {
55 return addressNumber;
56 }
57
58 /**
59 * Returns the {@link InetAddress} that represent the {@code LOCALHOST} for the family.
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 * Returns the {@link InternetProtocolFamily} for the given {@link InetAddress}.
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 }