| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Vincent Perot - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.internal.pcap.core.protocol; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.Collection; |
| 17 | import java.util.List; |
| 18 | |
| 19 | /** |
| 20 | * Enumeration used for describing the different known protocols. |
| 21 | * |
| 22 | * @author Vincent Perot |
| 23 | */ |
| 24 | public enum PcapProtocol { |
| 25 | |
| 26 | // Layer 0 |
| 27 | /** |
| 28 | * The Pcap Protocol is not a real protocol but is used as an helper to |
| 29 | * generate Pcap packets. |
| 30 | */ |
| 31 | PCAP("Packet Capture", "pcap", Layer.LAYER_0, false), //$NON-NLS-1$ //$NON-NLS-2$ |
| 32 | |
| 33 | // Layer 1 |
| 34 | // Should always be empty. |
| 35 | |
| 36 | // Layer 2 |
| 37 | /** |
| 38 | * The description of the Ethernet II Protocol. |
| 39 | */ |
| 40 | ETHERNET_II("Ethernet II", "eth", Layer.LAYER_2, true), //$NON-NLS-1$ //$NON-NLS-2$ |
| 41 | |
| 42 | // Layer 3 |
| 43 | /** |
| 44 | * The description of the Internet Protocol Version 4. |
| 45 | */ |
| 46 | IPV4("Internet Protocol Version 4", "ipv4", Layer.LAYER_3, true), //$NON-NLS-1$ //$NON-NLS-2$ |
| 47 | |
| 48 | // Layer 4 |
| 49 | /** |
| 50 | * The description of the Transmission Control Protocol. |
| 51 | */ |
| 52 | TCP("Transmission Control Protocol", "tcp", Layer.LAYER_4, true), //$NON-NLS-1$ //$NON-NLS-2$ |
| 53 | /** |
| 54 | * The description of the User Datagram Protocol. |
| 55 | */ |
| 56 | UDP("User Datagram Protocol", "udp", Layer.LAYER_4, true), //$NON-NLS-1$ //$NON-NLS-2$ |
| 57 | |
| 58 | // Layer 5 |
| 59 | |
| 60 | // Layer 6 |
| 61 | |
| 62 | // Layer 7 |
| 63 | /** |
| 64 | * This protocol is used as an helper if the protocol of a packet is not |
| 65 | * recognized. Since all its data goes into payload, it can also be seen as |
| 66 | * a "payload packet". This is considered to be on layer 7 since its always |
| 67 | * the most encapsulated packet if present. |
| 68 | */ |
| 69 | UNKNOWN("Payload", "???", Layer.LAYER_7, false); //$NON-NLS-1$ //$NON-NLS-2$ |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Enum that lists constants related to protocols/layers. |
| 74 | * |
| 75 | * See http://en.wikipedia.org/wiki/OSI_model#Description_of_OSI_layers. |
| 76 | * |
| 77 | * @author Vincent Perot |
| 78 | */ |
| 79 | public static enum Layer { |
| 80 | |
| 81 | /** |
| 82 | * Layer 0. This layer is not an OSI layer but is used as an helper to store |
| 83 | * the pseudo-protocol PCAP. |
| 84 | */ |
| 85 | LAYER_0, |
| 86 | |
| 87 | /** Layer 1 of the OSI model */ |
| 88 | LAYER_1, |
| 89 | |
| 90 | /** Layer 2 of the OSI model */ |
| 91 | LAYER_2, |
| 92 | |
| 93 | /** Layer 3 of the OSI model */ |
| 94 | LAYER_3, |
| 95 | |
| 96 | /** Layer 4 of the OSI model */ |
| 97 | LAYER_4, |
| 98 | |
| 99 | /** Layer 5 of the OSI model */ |
| 100 | LAYER_5, |
| 101 | |
| 102 | /** Layer 6 of the OSI model */ |
| 103 | LAYER_6, |
| 104 | |
| 105 | /** Layer 7 of the OSI model */ |
| 106 | LAYER_7; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | // Fields |
| 111 | private final String fName; |
| 112 | private final String fShortName; |
| 113 | private final Layer fLayer; |
| 114 | private final boolean fSupportsStream; |
| 115 | |
| 116 | private PcapProtocol(String name, String shortName, Layer layer, boolean supportsStream) { |
| 117 | fName = name; |
| 118 | fShortName = shortName; |
| 119 | fLayer = layer; |
| 120 | fSupportsStream = supportsStream; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Getter method for the long name of the protocol. |
| 125 | * |
| 126 | * @return The long name of the protocol, as a string. |
| 127 | */ |
| 128 | public String getName() { |
| 129 | return fName; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Getter method for the short name of the protocol. |
| 134 | * |
| 135 | * @return The short name of the protocol, as a string. |
| 136 | */ |
| 137 | public String getShortName() { |
| 138 | return fShortName; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Getter method for the OSI layer of the protocol. |
| 143 | * |
| 144 | * @return The layer of the protocol. |
| 145 | */ |
| 146 | public Layer getLayer() { |
| 147 | return fLayer; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Getter method that indicates if the protocol supports streams. |
| 152 | * |
| 153 | * @return Whether the protocol supports streams or not. |
| 154 | */ |
| 155 | public boolean supportsStream() { |
| 156 | return fSupportsStream; |
| 157 | } |
| 158 | |
| 159 | // TODO make an immutable list that holds this data instead of computing it |
| 160 | // everytime. |
| 161 | |
| 162 | /** |
| 163 | * Method that returns a list of all the protocols included in a certain OSI |
| 164 | * layer. |
| 165 | * |
| 166 | * @param layer |
| 167 | * The layer of the protocols. |
| 168 | * @return The protocols on that layer. |
| 169 | */ |
| 170 | public static Collection<PcapProtocol> getProtocolsOnLayer(Layer layer) { |
| 171 | List<PcapProtocol> protocolsOnLayer = new ArrayList<>(); |
| 172 | for (PcapProtocol p : PcapProtocol.values()) { |
| 173 | if (p.getLayer() == layer) { |
| 174 | protocolsOnLayer.add(p); |
| 175 | } |
| 176 | } |
| 177 | return protocolsOnLayer; |
| 178 | } |
| 179 | } |