| 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.endpoint; |
| 14 | |
| 15 | import org.eclipse.jdt.annotation.Nullable; |
| 16 | import org.eclipse.linuxtools.internal.pcap.core.packet.Packet; |
| 17 | |
| 18 | /** |
| 19 | * Abstract class that represents an endpoint. An endpoint is an address where a |
| 20 | * packet is received or sent. Therefore, it is protocol dependent. For |
| 21 | * instance, an Ethernet II endpoint is the MAC address. An Ipv4 endpoint is the |
| 22 | * combination of the MAC address and the IP address. This is useful for |
| 23 | * building packet streams. |
| 24 | * |
| 25 | * @author Vincent Perot |
| 26 | */ |
| 27 | public abstract class ProtocolEndpoint { |
| 28 | |
| 29 | /** |
| 30 | * Empty string for child classes. |
| 31 | */ |
| 32 | protected static final String EMPTY_STRING = ""; //$NON-NLS-1$ |
| 33 | |
| 34 | /** |
| 35 | * The encapsulating endpoint. Much like packets, endpoints are |
| 36 | * encapsulated. The higher the layer of the packet protocol is, the more |
| 37 | * parents an endpoint will have. |
| 38 | */ |
| 39 | private final @Nullable ProtocolEndpoint fParentEndpoint; |
| 40 | |
| 41 | /** |
| 42 | * Constructor of the {@link ProtocolEndpoint} class. It takes a packet to |
| 43 | * get its endpoint. Since every packet has two endpoints (source and |
| 44 | * destination), the isSourceEndpoint parameter is used to specify which |
| 45 | * endpoint to take. |
| 46 | * |
| 47 | * @param packet |
| 48 | * The packet that contains the endpoints. |
| 49 | * @param isSourceEndpoint |
| 50 | * Whether to take the source or the destination endpoint of the |
| 51 | * packet. |
| 52 | */ |
| 53 | public ProtocolEndpoint(Packet packet, boolean isSourceEndpoint) { |
| 54 | Packet parentPacket = packet.getParentPacket(); |
| 55 | if (parentPacket == null) { |
| 56 | fParentEndpoint = null; |
| 57 | } else { |
| 58 | fParentEndpoint = isSourceEndpoint ? |
| 59 | parentPacket.getSourceEndpoint() : |
| 60 | parentPacket.getDestinationEndpoint(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Getter method that returns the parent endpoint. |
| 66 | * |
| 67 | * @return The parent endpoint. |
| 68 | */ |
| 69 | public @Nullable ProtocolEndpoint getParentEndpoint() { |
| 70 | return fParentEndpoint; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public abstract int hashCode(); |
| 75 | |
| 76 | @Override |
| 77 | public abstract boolean equals(@Nullable Object obj); |
| 78 | |
| 79 | @Override |
| 80 | public abstract String toString(); |
| 81 | |
| 82 | } |