alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://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, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onlab.packet; |
| 17 | |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 18 | /** |
| 19 | * Representation of an Ethertype. |
| 20 | */ |
| 21 | public class EthType { |
| 22 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 23 | /** |
| 24 | * A list of known ethertypes. Adding a fully defined enum here will |
| 25 | * associated the ethertype with a textual representation and a parsing |
| 26 | * class. |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 27 | */ |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 28 | public enum EtherType { |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 29 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 30 | ARP(0x806, "arp", org.onlab.packet.ARP.deserializer()), |
| 31 | RARP(0x8035, "rarp", org.onlab.packet.ARP.deserializer()), |
| 32 | IPV4(0x800, "ipv4", org.onlab.packet.IPv4.deserializer()), |
| 33 | IPV6(0x86dd, "ipv6", org.onlab.packet.IPv6.deserializer()), |
| 34 | LLDP(0x88cc, "lldp", org.onlab.packet.LLDP.deserializer()), |
| 35 | VLAN(0x8100, "vlan", null), |
| 36 | BDDP(0x8942, "bddp", org.onlab.packet.LLDP.deserializer()), |
| 37 | MPLS_UNICAST(0x8847, "mpls_unicast", org.onlab.packet.MPLS.deserializer()), |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 38 | MPLS_MULTICAST(0x8848, "mpls_unicast", org.onlab.packet.MPLS.deserializer()), |
| 39 | EAPOL(0x888e, "eapol", org.onlab.packet.EAPOL.deserializer()), |
| 40 | UNKNOWN(0, "unknown", null); |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 41 | |
| 42 | |
| 43 | private final EthType etherType; |
| 44 | private final String type; |
| 45 | private final Deserializer<?> deserializer; |
| 46 | |
| 47 | /** |
| 48 | * Constucts a new ethertype. |
| 49 | * |
| 50 | * @param ethType The actual ethertype |
| 51 | * @param type it's textual representation |
| 52 | * @param deserializer a parser for this ethertype |
| 53 | */ |
| 54 | EtherType(int ethType, String type, Deserializer<?> deserializer) { |
| 55 | this.etherType = new EthType(ethType); |
| 56 | this.type = type; |
| 57 | this.deserializer = deserializer; |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 58 | } |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 59 | |
| 60 | public EthType ethType() { |
| 61 | return etherType; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public String toString() { |
| 66 | return type; |
| 67 | } |
| 68 | |
| 69 | public Deserializer<?> deserializer() { |
| 70 | return deserializer; |
| 71 | } |
| 72 | |
Jonathan Hart | 4a60bb3 | 2015-06-30 15:31:20 -0700 | [diff] [blame] | 73 | public static EtherType lookup(short etherType) { |
| 74 | for (EtherType ethType : EtherType.values()) { |
| 75 | if (ethType.ethType().toShort() == etherType) { |
| 76 | return ethType; |
| 77 | } |
| 78 | } |
| 79 | return UNKNOWN; |
| 80 | } |
| 81 | |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 82 | } |
| 83 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 84 | |
| 85 | private final short etherType; |
| 86 | |
| 87 | /** |
| 88 | * Builds the EthType. |
| 89 | * |
| 90 | * @param etherType an integer representing an ethtype |
| 91 | */ |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 92 | public EthType(int etherType) { |
| 93 | this.etherType = (short) (etherType & 0xFFFF); |
| 94 | } |
| 95 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Builds the EthType. |
| 98 | * |
| 99 | * @param etherType a short representing the ethtype |
| 100 | */ |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 101 | public EthType(short etherType) { |
| 102 | this.etherType = etherType; |
| 103 | } |
| 104 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 105 | /** |
| 106 | * Returns the short value for this ethtype. |
| 107 | * |
| 108 | * @return a short value |
| 109 | */ |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 110 | public short toShort() { |
| 111 | return etherType; |
| 112 | } |
| 113 | |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Looks up the ethertype by it's numerical representation |
| 116 | * and returns it's textual format. |
| 117 | * |
| 118 | * @param etherType the short value of the ethertype |
| 119 | * @return a textual representation |
| 120 | */ |
| 121 | public EtherType lookup(short etherType) { |
| 122 | for (EtherType ethType : EtherType.values()) { |
| 123 | if (ethType.ethType().toShort() == etherType) { |
| 124 | return ethType; |
| 125 | } |
| 126 | } |
| 127 | return null; |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public boolean equals(Object o) { |
| 132 | if (this == o) { |
| 133 | return true; |
| 134 | } |
| 135 | if (o == null || getClass() != o.getClass()) { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | EthType ethType = (EthType) o; |
| 140 | |
| 141 | if (etherType != ethType.etherType) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public int hashCode() { |
| 150 | return (int) etherType; |
| 151 | } |
| 152 | |
| 153 | public String toString() { |
| 154 | EtherType ethType = lookup(this.etherType); |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 155 | return (ethType == null ? String.format("0x%04x", etherType) : |
| 156 | ethType.toString()); |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 157 | } |
| 158 | |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 159 | } |