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