/
UnknownPacket.java
210 lines (182 loc) · 6.32 KB
/
UnknownPacket.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*******************************************************************************
* 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.unknown;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.internal.pcap.core.packet.Packet;
import org.eclipse.tracecompass.internal.pcap.core.protocol.PcapProtocol;
import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile;
import org.eclipse.tracecompass.internal.pcap.core.util.ConversionHelper;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
/**
* Class that represents an Unknown packet. It is possible to get such a packet
* if the protocol has not been implemented in this library or if the parent
* packet was invalid (in certain cases only). The header of such a packet is
* inexistent.
*
* @author Vincent Perot
*/
public class UnknownPacket extends Packet {
private final @Nullable Packet fChildPacket;
private final ByteBuffer fPayload;
private @Nullable UnknownEndpoint fSourceEndpoint;
private @Nullable UnknownEndpoint fDestinationEndpoint;
private @Nullable Map<String, String> fFields;
/**
* Constructor of an Unknown Packet object.
*
* @param file
* The file to which this packet belongs.
* @param parent
* The parent packet of this packet.
* @param packet
* The entire packet (header and payload).
*/
public UnknownPacket(PcapFile file, @Nullable Packet parent, ByteBuffer packet) {
super(file, parent, PcapProtocol.UNKNOWN);
// The endpoints are lazy loaded. They are defined in the get*Endpoint()
// methods.
fSourceEndpoint = null;
fDestinationEndpoint = null;
fFields = null;
// Header is not used. All data go into payload.
fPayload = packet;
fChildPacket = findChildPacket();
}
@Override
public @Nullable Packet getChildPacket() {
return fChildPacket;
}
@Override
public @Nullable ByteBuffer getPayload() {
return fPayload;
}
@Override
protected @Nullable Packet findChildPacket() {
return null;
}
@Override
public String toString() {
byte[] array = Arrays.copyOfRange(fPayload.array(), fPayload.arrayOffset(), fPayload.arrayOffset() + fPayload.limit());
String string = "Payload: " + ConversionHelper.bytesToHex(array, true); //$NON-NLS-1$
final Packet child = fChildPacket;
if (child != null) {
return string + child.toString();
}
return string;
}
@Override
public boolean validate() {
// Not yet implemented. ATM, we consider that all packets are valid.
// This is the case for all packets.
// TODO Implement it.
return true;
}
@Override
public UnknownEndpoint getSourceEndpoint() {
@Nullable
UnknownEndpoint endpoint = fSourceEndpoint;
if (endpoint == null) {
endpoint = new UnknownEndpoint(this, true);
}
fSourceEndpoint = endpoint;
return fSourceEndpoint;
}
@Override
public UnknownEndpoint getDestinationEndpoint() {
@Nullable
UnknownEndpoint endpoint = fDestinationEndpoint;
if (endpoint == null) {
endpoint = new UnknownEndpoint(this, false);
}
fDestinationEndpoint = endpoint;
return fDestinationEndpoint;
}
@Override
public Map<String, String> getFields() {
Map<String, String> map = fFields;
if (map == null) {
byte[] array = Arrays.copyOfRange(fPayload.array(), fPayload.arrayOffset(), fPayload.arrayOffset() + fPayload.limit());
Builder<String, String> builder = ImmutableMap.<@NonNull String, @NonNull String> builder()
.put("Binary", ConversionHelper.bytesToHex(array, true)); //$NON-NLS-1$
StringBuilder sb = new StringBuilder();
for (byte b : array) {
char ch = (char) b;
if (ch >= 32 && ch < 127) {
sb.append(ch);
} else {
sb.append('·');
}
}
builder.put("Character", sb.toString()); //$NON-NLS-1$
fFields = builder.build();
return fFields;
}
return map;
}
@Override
public String getLocalSummaryString() {
return "Len: " + fPayload.limit() + " bytes"; //$NON-NLS-1$ //$NON-NLS-2$
}
@Override
protected String getSignificationString() {
return "Data: " + fPayload.limit() + " bytes"; //$NON-NLS-1$ //$NON-NLS-2$
}
@Override
public Packet getMostEcapsulatedPacket() {
Packet packet = this.getParentPacket();
if (packet == null) {
return this;
}
return packet;
}
@Override
public int hashCode() {
final int prime = 31;
int result = prime;
final Packet child = fChildPacket;
if (child != null) {
result += child.hashCode();
}
if (child == null) {
result = prime * result + payloadHashCode(fPayload);
}
return result;
}
@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
UnknownPacket other = (UnknownPacket) obj;
if (!Objects.equals(fChildPacket, other.fChildPacket)) {
return false;
}
if (fChildPacket == null && !payloadEquals(fPayload, other.fPayload)) {
return false;
}
return true;
}
}