| 1 |
/* |
| 2 |
* Copyright (C) 2010 Wayne Meissner |
| 3 |
* |
| 4 |
* This file is part of jnr. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package jnr.netdb; |
| 20 |
|
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.Arrays; |
| 23 |
import java.util.Collection; |
| 24 |
import java.util.Collections; |
| 25 |
import java.util.HashMap; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Map; |
| 28 |
|
| 29 |
/** |
| 30 |
* Pre-compiled table of IANA protocol numbers |
| 31 |
*/ |
| 32 |
class IANAProtocolsDB implements ProtocolsDB { |
| 33 |
|
| 34 |
private final Map<String, Protocol> nameToProto; |
| 35 |
private final Map<Integer, Protocol> numberToProto; |
| 36 |
|
| 37 |
public static final IANAProtocolsDB getInstance() { |
| 38 |
return SingletonHolder.INSTANCE; |
| 39 |
} |
| 40 |
|
| 41 |
private static final class SingletonHolder { |
| 42 |
public static final IANAProtocolsDB INSTANCE = initProtocolDB().build(); |
| 43 |
} |
| 44 |
|
| 45 |
private IANAProtocolsDB(Map<String, Protocol> nameToProto, Map<Integer, Protocol> numberToProto) { |
| 46 |
this.nameToProto = nameToProto; |
| 47 |
this.numberToProto = numberToProto; |
| 48 |
} |
| 49 |
|
| 50 |
public Protocol getProtocolByName(String name) { |
| 51 |
return nameToProto.get(name); |
| 52 |
} |
| 53 |
|
| 54 |
public Protocol getProtocolByNumber(Integer number) { |
| 55 |
return numberToProto.get(number); |
| 56 |
} |
| 57 |
|
| 58 |
public Collection<Protocol> getAllProtocols() { |
| 59 |
return Collections.unmodifiableCollection(nameToProto.values()); |
| 60 |
} |
| 61 |
|
| 62 |
private static final class ProtocolDBBuilder { |
| 63 |
private static final List<String> emptyAliases = Collections.emptyList(); |
| 64 |
private final Map<String, Protocol> nameToProto = new HashMap<String, Protocol>(); |
| 65 |
private final Map<Integer, Protocol> numberToProto = new HashMap<Integer, Protocol>(); |
| 66 |
|
| 67 |
public void add(String name, int proto, String... aliases) { |
| 68 |
List<String> aliasesList = aliases.length > 0 ? new ArrayList<String>(Arrays.asList(aliases)) : emptyAliases; |
| 69 |
|
| 70 |
Protocol p = new Protocol(name, proto, aliasesList); |
| 71 |
nameToProto.put(name, p); |
| 72 |
for (String alias : aliases) { |
| 73 |
nameToProto.put(alias, p); |
| 74 |
} |
| 75 |
|
| 76 |
numberToProto.put(proto, p); |
| 77 |
} |
| 78 |
|
| 79 |
public IANAProtocolsDB build() { |
| 80 |
return new IANAProtocolsDB(nameToProto, numberToProto); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
private static final ProtocolDBBuilder initProtocolDB() { |
| 85 |
ProtocolDBBuilder builder = new ProtocolDBBuilder(); |
| 86 |
|
| 87 |
builder.add("ip", 0, "IP"); |
| 88 |
builder.add("icmp", 1, "ICMP"); |
| 89 |
builder.add("igmp", 2, "IGMP"); |
| 90 |
builder.add("ggp", 3, "GGP"); |
| 91 |
builder.add("ipencap", 4, "IP-ENCAP"); |
| 92 |
builder.add("st2", 5, "ST2"); |
| 93 |
builder.add("tcp", 6, "TCP"); |
| 94 |
builder.add("cbt", 7, "CBT"); |
| 95 |
builder.add("egp", 8, "EGP"); |
| 96 |
builder.add("igp", 9, "IGP"); |
| 97 |
builder.add("bbn-rcc", 10, "BBN-RCC-MON"); |
| 98 |
builder.add("nvp", 11, "NVP-II"); |
| 99 |
builder.add("pup", 12, "PUP"); |
| 100 |
builder.add("argus", 13, "ARGUS"); |
| 101 |
builder.add("emcon", 14, "EMCON"); |
| 102 |
builder.add("xnet", 15, "XNET"); |
| 103 |
builder.add("chaos", 16, "CHAOS"); |
| 104 |
builder.add("udp", 17, "UDP"); |
| 105 |
builder.add("mux", 18, "MUX"); |
| 106 |
builder.add("dcn", 19, "DCN-MEAS"); |
| 107 |
builder.add("hmp", 20, "HMP"); |
| 108 |
builder.add("prm", 21, "PRM"); |
| 109 |
builder.add("xns-idp", 22, "XNS-IDP"); |
| 110 |
builder.add("trunk-1", 23, "TRUNK-1"); |
| 111 |
builder.add("trunk-2", 24, "TRUNK-2"); |
| 112 |
builder.add("leaf-1", 25, "LEAF-1"); |
| 113 |
builder.add("leaf-2", 26, "LEAF-2"); |
| 114 |
builder.add("rdp", 27, "RDP"); |
| 115 |
builder.add("irtp", 28, "IRTP"); |
| 116 |
builder.add("iso-tp4", 29, "ISO-TP4"); |
| 117 |
builder.add("netblt", 30, "NETBLT"); |
| 118 |
builder.add("mfe-nsp", 31, "MFE-NSP"); |
| 119 |
builder.add("merit-inp", 32, "MERIT-INP"); |
| 120 |
builder.add("sep", 33, "SEP"); |
| 121 |
builder.add("3pc", 34, "3PC"); |
| 122 |
builder.add("idpr", 35, "IDPR"); |
| 123 |
builder.add("xtp", 36, "XTP"); |
| 124 |
builder.add("ddp", 37, "DDP"); |
| 125 |
builder.add("idpr-cmtp", 38, "IDPR-CMTP"); |
| 126 |
builder.add("tp++", 39, "TP++"); |
| 127 |
builder.add("il", 40, "IL"); |
| 128 |
builder.add("ipv6", 41, "IPV6"); |
| 129 |
builder.add("sdrp", 42, "SDRP"); |
| 130 |
builder.add("ipv6-route", 43, "IPV6-ROUTE"); |
| 131 |
builder.add("ipv6-frag", 44, "IPV6-FRAG"); |
| 132 |
builder.add("idrp", 45, "IDRP"); |
| 133 |
builder.add("rsvp", 46, "RSVP"); |
| 134 |
builder.add("gre", 47, "GRE"); |
| 135 |
builder.add("mhrp", 48, "MHRP"); |
| 136 |
builder.add("bna", 49, "BNA"); |
| 137 |
builder.add("esp", 50, "ESP"); |
| 138 |
builder.add("ah", 51, "AH"); |
| 139 |
builder.add("i-nlsp", 52, "I-NLSP"); |
| 140 |
builder.add("swipe", 53, "SWIPE"); |
| 141 |
builder.add("narp", 54, "NARP"); |
| 142 |
builder.add("mobile", 55, "MOBILE"); |
| 143 |
builder.add("tlsp", 56, "TLSP"); |
| 144 |
builder.add("skip", 57, "SKIP"); |
| 145 |
builder.add("ipv6-icmp", 58, "IPV6-ICMP"); |
| 146 |
builder.add("ipv6-nonxt", 59, "IPV6-NONXT"); |
| 147 |
builder.add("ipv6-opts", 60, "IPV6-OPTS"); |
| 148 |
builder.add("cftp", 62, "CFTP"); |
| 149 |
builder.add("sat-expak", 64, "SAT-EXPAK"); |
| 150 |
builder.add("kryptolan", 65, "KRYPTOLAN"); |
| 151 |
builder.add("rvd", 66, "RVD"); |
| 152 |
builder.add("ippc", 67, "IPPC"); |
| 153 |
builder.add("sat-mon", 69, "SAT-MON"); |
| 154 |
builder.add("visa", 70, "VISA"); |
| 155 |
builder.add("ipcv", 71, "IPCV"); |
| 156 |
builder.add("cpnx", 72, "CPNX"); |
| 157 |
builder.add("cphb", 73, "CPHB"); |
| 158 |
builder.add("wsn", 74, "WSN"); |
| 159 |
builder.add("pvp", 75, "PVP"); |
| 160 |
builder.add("br-sat-mon", 76, "BR-SAT-MON"); |
| 161 |
builder.add("sun-nd", 77, "SUN-ND"); |
| 162 |
builder.add("wb-mon", 78, "WB-MON"); |
| 163 |
builder.add("wb-expak", 79, "WB-EXPAK"); |
| 164 |
builder.add("iso-ip", 80, "ISO-IP"); |
| 165 |
builder.add("vmtp", 81, "VMTP"); |
| 166 |
builder.add("secure-vmtp", 82, "SECURE-VMTP"); |
| 167 |
builder.add("vines", 83, "VINES"); |
| 168 |
builder.add("ttp", 84, "TTP"); |
| 169 |
builder.add("nsfnet-igp", 85, "NSFNET-IGP"); |
| 170 |
builder.add("dgp", 86, "DGP"); |
| 171 |
builder.add("tcf", 87, "TCF"); |
| 172 |
builder.add("eigrp", 88, "EIGRP"); |
| 173 |
builder.add("ospf", 89, "OSPFIGP"); |
| 174 |
builder.add("sprite-rpc", 90, "Sprite-RPC"); |
| 175 |
builder.add("larp", 91, "LARP"); |
| 176 |
builder.add("mtp", 92, "MTP"); |
| 177 |
builder.add("ax.25", 93, "AX.25"); |
| 178 |
builder.add("ipip", 94, "IPIP"); |
| 179 |
builder.add("micp", 95, "MICP"); |
| 180 |
builder.add("scc-sp", 96, "SCC-SP"); |
| 181 |
builder.add("etherip", 97, "ETHERIP"); |
| 182 |
builder.add("encap", 98, "ENCAP"); |
| 183 |
builder.add("gmtp", 100, "GMTP"); |
| 184 |
builder.add("ifmp", 101, "IFMP"); |
| 185 |
builder.add("pnni", 102, "PNNI"); |
| 186 |
builder.add("pim", 103, "PIM"); |
| 187 |
builder.add("aris", 104, "ARIS"); |
| 188 |
builder.add("scps", 105, "SCPS"); |
| 189 |
builder.add("qnx", 106, "QNX"); |
| 190 |
builder.add("a/n", 107, "A/N"); |
| 191 |
builder.add("ipcomp", 108, "IPComp"); |
| 192 |
builder.add("snp", 109, "SNP"); |
| 193 |
builder.add("compaq-peer", 110, "Compaq-Peer"); |
| 194 |
builder.add("ipx-in-ip", 111, "IPX-in-IP"); |
| 195 |
builder.add("vrrp", 112, "VRRP"); |
| 196 |
builder.add("pgm", 113, "PGM"); |
| 197 |
builder.add("l2tp", 115, "L2TP"); |
| 198 |
builder.add("ddx", 116, "DDX"); |
| 199 |
builder.add("iatp", 117, "IATP"); |
| 200 |
builder.add("st", 118, "ST"); |
| 201 |
builder.add("srp", 119, "SRP"); |
| 202 |
builder.add("uti", 120, "UTI"); |
| 203 |
builder.add("smp", 121, "SMP"); |
| 204 |
builder.add("sm", 122, "SM"); |
| 205 |
builder.add("ptp", 123, "PTP"); |
| 206 |
builder.add("isis", 124, "ISIS"); |
| 207 |
builder.add("fire", 125, "FIRE"); |
| 208 |
builder.add("crtp", 126, "CRTP"); |
| 209 |
builder.add("crdup", 127, "CRUDP"); |
| 210 |
builder.add("sscopmce", 128, "SSCOPMCE"); |
| 211 |
builder.add("iplt", 129, "IPLT"); |
| 212 |
builder.add("sps", 130, "SPS"); |
| 213 |
builder.add("pipe", 131, "PIPE"); |
| 214 |
builder.add("sctp", 132, "SCTP"); |
| 215 |
builder.add("fc", 133, "FC"); |
| 216 |
builder.add("divert", 254, "DIVERT"); |
| 217 |
|
| 218 |
|
| 219 |
return builder; |
| 220 |
} |
| 221 |
} |