| 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.linuxtools.internal.pcap.core.protocol.ipv4; |
| 14 | |
| 15 | import java.net.Inet4Address; |
| 16 | |
| 17 | import org.eclipse.jdt.annotation.NonNull; |
| 18 | import org.eclipse.jdt.annotation.Nullable; |
| 19 | import org.eclipse.linuxtools.internal.pcap.core.endpoint.ProtocolEndpoint; |
| 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 | |
| 29 | private final Inet4Address fIPAddress; |
| 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 | |
| 60 | result = prime * result + fIPAddress.hashCode(); |
| 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 |
| 76 | boolean localEquals = fIPAddress.equals(other.fIPAddress); |
| 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) { |
| 93 | @SuppressWarnings("null") |
| 94 | @NonNull String ret = fIPAddress.getHostAddress(); |
| 95 | return ret; |
| 96 | } |
| 97 | return endpoint.toString() + '/' + fIPAddress.getHostAddress(); |
| 98 | } |
| 99 | |
| 100 | } |