Commit | Line | Data |
---|---|---|
5255c030 VP |
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 | ||
93d1d135 | 13 | package org.eclipse.linuxtools.internal.pcap.core.protocol.ipv4; |
5255c030 | 14 | |
d6fca387 | 15 | import java.net.Inet4Address; |
5255c030 | 16 | |
d6fca387 | 17 | import org.eclipse.jdt.annotation.NonNull; |
5255c030 | 18 | import org.eclipse.jdt.annotation.Nullable; |
93d1d135 | 19 | import org.eclipse.linuxtools.internal.pcap.core.endpoint.ProtocolEndpoint; |
5255c030 VP |
20 | |
21 | /** | |
22 | * Class that extends the {@link ProtocolEndpoint} class. It represents the | |
23 | * endpoint at an IPv4 level. | |
24 | * | |
25 | * @author Vincent Perot | |
26 | */ | |
27 | public class IPv4Endpoint extends ProtocolEndpoint { | |
28 | ||
d6fca387 | 29 | private final Inet4Address fIPAddress; |
5255c030 VP |
30 | |
31 | /** | |
32 | * Constructor of the {@link IPv4Endpoint} class. It takes a packet to get | |
33 | * its endpoint. Since every packet has two endpoints (source and | |
34 | * destination), the isSourceEndpoint parameter is used to specify which | |
35 | * endpoint to take. | |
36 | * | |
37 | * @param packet | |
38 | * The packet that contains the endpoints. | |
39 | * @param isSourceEndpoint | |
40 | * Whether to take the source or the destination endpoint of the | |
41 | * packet. | |
42 | */ | |
43 | public IPv4Endpoint(IPv4Packet packet, boolean isSourceEndpoint) { | |
44 | super(packet, isSourceEndpoint); | |
45 | fIPAddress = isSourceEndpoint ? packet.getSourceIpAddress() : packet.getDestinationIpAddress(); | |
46 | } | |
47 | ||
48 | @Override | |
49 | public int hashCode() { | |
50 | final int prime = 31; | |
51 | int result = 1; | |
52 | ||
53 | ProtocolEndpoint endpoint = getParentEndpoint(); | |
54 | if (endpoint == null) { | |
55 | result = 0; | |
56 | } else { | |
57 | result = endpoint.hashCode(); | |
58 | } | |
59 | ||
d6fca387 | 60 | result = prime * result + fIPAddress.hashCode(); |
5255c030 VP |
61 | return result; |
62 | } | |
63 | ||
64 | @Override | |
65 | public boolean equals(@Nullable Object obj) { | |
66 | if (this == obj) { | |
67 | return true; | |
68 | } | |
69 | if (!(obj instanceof IPv4Endpoint)) { | |
70 | return false; | |
71 | } | |
72 | ||
73 | IPv4Endpoint other = (IPv4Endpoint) obj; | |
74 | ||
75 | // Check on layer | |
d6fca387 | 76 | boolean localEquals = fIPAddress.equals(other.fIPAddress); |
5255c030 VP |
77 | if (!localEquals) { |
78 | return false; | |
79 | } | |
80 | ||
81 | // Check above layers. | |
82 | ProtocolEndpoint endpoint = getParentEndpoint(); | |
83 | if (endpoint != null) { | |
84 | return endpoint.equals(other.getParentEndpoint()); | |
85 | } | |
86 | return true; | |
87 | } | |
88 | ||
89 | @Override | |
90 | public String toString() { | |
91 | ProtocolEndpoint endpoint = getParentEndpoint(); | |
92 | if (endpoint == null) { | |
d6fca387 VP |
93 | @SuppressWarnings("null") |
94 | @NonNull String ret = fIPAddress.getHostAddress(); | |
95 | return ret; | |
5255c030 | 96 | } |
d6fca387 | 97 | return endpoint.toString() + '/' + fIPAddress.getHostAddress(); |
5255c030 VP |
98 | } |
99 | ||
100 | } |