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