/
IPv6Endpoint.java
96 lines (80 loc) · 2.93 KB
/
IPv6Endpoint.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
/*******************************************************************************
* Copyright (c) 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
*******************************************************************************/
package org.eclipse.tracecompass.internal.pcap.core.protocol.ipv6;
import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
import java.net.Inet6Address;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.internal.pcap.core.endpoint.ProtocolEndpoint;
/**
* Class that extends the {@link ProtocolEndpoint} class. It represents the
* endpoint at an IPv6 level.
*/
public class IPv6Endpoint extends ProtocolEndpoint {
private final Inet6Address fIPAddress;
/**
* Constructor of the {@link IPv6Endpoint} class. It takes a packet to get
* its endpoint. Since every packet has two endpoints (source and
* destination), the isSourceEndpoint parameter is used to specify which
* endpoint to take.
*
* @param packet
* The packet that contains the endpoints.
* @param isSourceEndpoint
* Whether to take the source or the destination endpoint of the
* packet.
*/
public IPv6Endpoint(IPv6Packet packet, boolean isSourceEndpoint) {
super(packet, isSourceEndpoint);
fIPAddress = isSourceEndpoint ? packet.getSourceIpAddress() : packet.getDestinationIpAddress();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
ProtocolEndpoint endpoint = getParentEndpoint();
if (endpoint == null) {
result = 0;
} else {
result = endpoint.hashCode();
}
result = prime * result + fIPAddress.hashCode();
return result;
}
@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof IPv6Endpoint)) {
return false;
}
IPv6Endpoint other = (IPv6Endpoint) obj;
// Check on layer
boolean localEquals = fIPAddress.equals(other.fIPAddress);
if (!localEquals) {
return false;
}
// Check above layers.
ProtocolEndpoint endpoint = getParentEndpoint();
if (endpoint != null) {
return endpoint.equals(other.getParentEndpoint());
}
return true;
}
@Override
public String toString() {
ProtocolEndpoint endpoint = getParentEndpoint();
if (endpoint == null) {
return checkNotNull(fIPAddress.getHostAddress());
}
return endpoint.toString() + '/' + fIPAddress.getHostAddress();
}
}