From: <ga...@us...> - 2012-12-21 13:50:32
|
Revision: 5937 http://jnode.svn.sourceforge.net/jnode/?rev=5937&view=rev Author: galatnm Date: 2012-12-21 13:50:22 +0000 (Fri, 21 Dec 2012) Log Message: ----------- NET : ARP improvements + basic tests. Modified Paths: -------------- trunk/net/src/net/org/jnode/net/arp/ARPCache.java trunk/net/src/net/org/jnode/net/arp/ARPCacheEntry.java trunk/net/src/net/org/jnode/net/arp/ARPHeader.java trunk/net/src/net/org/jnode/net/arp/ARPNetworkLayer.java Added Paths: ----------- trunk/net/src/net/org/jnode/net/arp/ARPOperation.java trunk/net/src/test/org/jnode/net/ trunk/net/src/test/org/jnode/net/arp/ trunk/net/src/test/org/jnode/net/arp/ARPHeaderTest.java Removed Paths: ------------- trunk/net/src/net/org/jnode/net/arp/ARPConstants.java Modified: trunk/net/src/net/org/jnode/net/arp/ARPCache.java =================================================================== --- trunk/net/src/net/org/jnode/net/arp/ARPCache.java 2012-12-19 08:38:08 UTC (rev 5936) +++ trunk/net/src/net/org/jnode/net/arp/ARPCache.java 2012-12-21 13:50:22 UTC (rev 5937) @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.Map; import org.apache.log4j.Logger; import org.jnode.net.HardwareAddress; @@ -35,33 +36,33 @@ */ public class ARPCache { - /** My logger */ private static final Logger log = Logger.getLogger(ARPCache.class); - private final HashMap<HardwareAddress, ARPCacheEntry> hw2p = + + private final Map<HardwareAddress, ARPCacheEntry> networkToProtocolAddresses = new HashMap<HardwareAddress, ARPCacheEntry>(); - private final HashMap<ProtocolAddress, ARPCacheEntry> p2hw = + + private final Map<ProtocolAddress, ARPCacheEntry> protocolToNetworkAddresses = new HashMap<ProtocolAddress, ARPCacheEntry>(); /** * Remove all cached entries */ public synchronized void clear() { - hw2p.clear(); - p2hw.clear(); + networkToProtocolAddresses.clear(); + protocolToNetworkAddresses.clear(); } /** - * Update/Add an extry to the cache + * Update/Add an entry to the cache * - * @param hwAddress - * @param pAddress + * @param hardwareAddress Network address + * @param protocolAddress Protocol address */ - public synchronized void set(HardwareAddress hwAddress, ProtocolAddress pAddress, + public synchronized void set(HardwareAddress hardwareAddress, ProtocolAddress protocolAddress, boolean dynamic) { - final ARPCacheEntry entry = new ARPCacheEntry(hwAddress, pAddress, dynamic); - hw2p.put(hwAddress, entry); - p2hw.put(pAddress, entry); - // log.debug("Adding ARP cache " + hwAddress + " - " + pAddress); + final ARPCacheEntry entry = new ARPCacheEntry(hardwareAddress, protocolAddress, dynamic); + networkToProtocolAddresses.put(hardwareAddress, entry); + protocolToNetworkAddresses.put(protocolAddress, entry); notifyAll(); } @@ -69,18 +70,18 @@ * Gets the cached netword address for the given protocol address, or null * if not found. * - * @param pAddress + * @param protocolAddress */ - public synchronized HardwareAddress get(ProtocolAddress pAddress) { - final ARPCacheEntry entry = (ARPCacheEntry) p2hw.get(pAddress); + public synchronized HardwareAddress get(ProtocolAddress protocolAddress) { + final ARPCacheEntry entry = protocolToNetworkAddresses.get(protocolAddress); if (entry == null) { return null; } if (entry.isExpired()) { log.debug("Removing expired ARP entry " + entry); - p2hw.remove(pAddress); - if (hw2p.get(entry.getHwAddress()) == entry) { - hw2p.remove(entry.getHwAddress()); + protocolToNetworkAddresses.remove(protocolAddress); + if (networkToProtocolAddresses.get(entry.getHwAddress()) == entry) { + networkToProtocolAddresses.remove(entry.getHwAddress()); } return null; } @@ -91,17 +92,17 @@ * Gets the cached protocol address for the given netword address, or null * if not found. * - * @param hwAddress + * @param hardwareAddress */ - public synchronized ProtocolAddress get(HardwareAddress hwAddress) { - final ARPCacheEntry entry = (ARPCacheEntry) hw2p.get(hwAddress); + public synchronized ProtocolAddress get(HardwareAddress hardwareAddress) { + final ARPCacheEntry entry = networkToProtocolAddresses.get(hardwareAddress); if (entry == null) { return null; } if (entry.isExpired()) { - hw2p.remove(hwAddress); - if (p2hw.get(entry.getPAddress()) == entry) { - p2hw.remove(entry.getPAddress()); + networkToProtocolAddresses.remove(hardwareAddress); + if (protocolToNetworkAddresses.get(entry.getPAddress()) == entry) { + protocolToNetworkAddresses.remove(entry.getPAddress()); } return null; } @@ -112,7 +113,7 @@ * Return all cache-entries. */ public synchronized Collection<ARPCacheEntry> entries() { - return new ArrayList<ARPCacheEntry>(hw2p.values()); + return new ArrayList<ARPCacheEntry>(networkToProtocolAddresses.values()); } /** Modified: trunk/net/src/net/org/jnode/net/arp/ARPCacheEntry.java =================================================================== --- trunk/net/src/net/org/jnode/net/arp/ARPCacheEntry.java 2012-12-19 08:38:08 UTC (rev 5936) +++ trunk/net/src/net/org/jnode/net/arp/ARPCacheEntry.java 2012-12-21 13:50:22 UTC (rev 5937) @@ -30,22 +30,27 @@ */ public class ARPCacheEntry { - private final long creationTime; - private final HardwareAddress hwAddress; - private final ProtocolAddress pAddress; + private static final long ARP_CACHE_LIFETIME = 10 * 60 * 1000; + + private final long creationTime; + private long lifeTime; + private final HardwareAddress hardwareAddress; + private final ProtocolAddress protocolAddress; private final boolean dynamic; /** * Create a new instance * - * @param hwAddress - * @param pAddress + * @param hardwareAddress + * @param protocolAddress * @param dynamic */ - public ARPCacheEntry(HardwareAddress hwAddress, ProtocolAddress pAddress, boolean dynamic) { - this.hwAddress = hwAddress; - this.pAddress = pAddress; + public ARPCacheEntry(HardwareAddress hardwareAddress, ProtocolAddress protocolAddress, boolean dynamic) { + this.hardwareAddress = hardwareAddress; + this.protocolAddress = protocolAddress; this.creationTime = System.currentTimeMillis(); + // TODO make ARP cache lifetime configurable + this.lifeTime = ARP_CACHE_LIFETIME; this.dynamic = dynamic; } @@ -60,23 +65,21 @@ * Is this entry expired? */ public boolean isExpired() { - final long age = (System.currentTimeMillis() - creationTime); - // TODO make ARP cache lifetime configurable - return (age >= 10 * 60 * 1000); + return ((System.currentTimeMillis() - creationTime) >= lifeTime); } /** * Gets the network address of this entry */ public HardwareAddress getHwAddress() { - return hwAddress; + return hardwareAddress; } /** * Gets the protocol address of this entry */ public ProtocolAddress getPAddress() { - return pAddress; + return protocolAddress; } /** @@ -98,6 +101,6 @@ * @see java.lang.Object#toString() */ public String toString() { - return pAddress + " " + hwAddress + ' ' + ((dynamic) ? "dynamic" : "static"); + return protocolAddress + " " + hardwareAddress + ' ' + ((dynamic) ? "dynamic" : "static"); } } Deleted: trunk/net/src/net/org/jnode/net/arp/ARPConstants.java =================================================================== --- trunk/net/src/net/org/jnode/net/arp/ARPConstants.java 2012-12-19 08:38:08 UTC (rev 5936) +++ trunk/net/src/net/org/jnode/net/arp/ARPConstants.java 2012-12-21 13:50:22 UTC (rev 5937) @@ -1,35 +0,0 @@ -/* - * $Id$ - * - * Copyright (C) 2003-2012 JNode.org - * - * This library is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation; either version 2.1 of the License, or - * (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public - * License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this library; If not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -package org.jnode.net.arp; - -/** - * @author epr - */ -public interface ARPConstants { - - public static final int ARP_REQUEST = 1; - public static final int ARP_REPLY = 2; - public static final int RARP_REQUEST = 3; - public static final int RARP_REPLY = 4; - - /** Delay between ARP requests */ - public static final int ARP_REQUEST_DELAY = 1500; -} Modified: trunk/net/src/net/org/jnode/net/arp/ARPHeader.java =================================================================== --- trunk/net/src/net/org/jnode/net/arp/ARPHeader.java 2012-12-19 08:38:08 UTC (rev 5936) +++ trunk/net/src/net/org/jnode/net/arp/ARPHeader.java 2012-12-21 13:50:22 UTC (rev 5937) @@ -36,13 +36,15 @@ private final static int ARP_DATA_LENGTH = 28; - private HardwareAddress srcHWAddress; - private ProtocolAddress srcPAddress; - private HardwareAddress targetHWAddress; - private ProtocolAddress targetPAddress; - private int op; - private final int hwtype; - private final int ptype; + private HardwareAddress sourceHardwareAddress; + private ProtocolAddress sourceProtocolAddress; + private HardwareAddress destinationHardwareAddress; + private ProtocolAddress destinationProtocolAddress; + private ARPOperation operation; + private final int hardwareType; + private final int protocolType; + private int hardwareAddressSize; + private int protocolAddressSize; /** * Create a new instance @@ -56,15 +58,17 @@ * @param ptype */ public ARPHeader(HardwareAddress srcHWAddress, ProtocolAddress srcPAddress, - HardwareAddress targetHWAddress, ProtocolAddress targetPAddress, int op, int hwtype, - int ptype) { - this.srcHWAddress = srcHWAddress; - this.srcPAddress = srcPAddress; - this.targetHWAddress = targetHWAddress; - this.targetPAddress = targetPAddress; - this.op = op; - this.hwtype = hwtype; - this.ptype = ptype; + HardwareAddress targetHWAddress, ProtocolAddress targetPAddress, ARPOperation op, int hwtype, + int ptype, int hwSize, int pSize) { + this.sourceHardwareAddress = srcHWAddress; + this.sourceProtocolAddress = srcPAddress; + this.destinationHardwareAddress = targetHWAddress; + this.destinationProtocolAddress = targetPAddress; + this.operation = op; + this.hardwareType = hwtype; + this.protocolType = ptype; + this.hardwareAddressSize = hwSize; + this.protocolAddressSize = pSize; } /** @@ -73,18 +77,18 @@ * @param skbuf */ public ARPHeader(SocketBuffer skbuf) throws SocketException { - hwtype = skbuf.get16(0); - ptype = skbuf.get16(2); - // int hwsize = skbuf.get(4); - // int psize = skbuf.get(5); - op = skbuf.get16(6); - if ((hwtype == 1) && (ptype == EthernetConstants.ETH_P_IP)) { - srcHWAddress = new EthernetAddress(skbuf, 8); - srcPAddress = new IPv4Address(skbuf, 14); - targetHWAddress = new EthernetAddress(skbuf, 18); - targetPAddress = new IPv4Address(skbuf, 24); + hardwareType = skbuf.get16(0); + protocolType = skbuf.get16(2); + hardwareAddressSize = skbuf.get(4); + protocolAddressSize = skbuf.get(5); + operation = ARPOperation.getType(skbuf.get16(6)); + if ((hardwareType == 1) && (protocolType == EthernetConstants.ETH_P_IP)) { + sourceHardwareAddress = new EthernetAddress(skbuf, 8); + sourceProtocolAddress = new IPv4Address(skbuf, 14); + destinationHardwareAddress = new EthernetAddress(skbuf, 18); + destinationProtocolAddress = new IPv4Address(skbuf, 24); } else { - throw new SocketException("Unknown hw,ptype: " + hwtype + ',' + ptype); + throw new SocketException("Unknown hw,ptype: " + hardwareType + ',' + protocolType); } } @@ -92,7 +96,7 @@ * Gets the length of this header in bytes */ public int getLength() { - return (8 + (srcHWAddress.getLength() + srcPAddress.getLength()) * 2); + return (8 + (sourceHardwareAddress.getLength() + sourceProtocolAddress.getLength()) * 2); } /** @@ -101,21 +105,21 @@ * @param skbuf */ public void prefixTo(SocketBuffer skbuf) { - skbuf.insert(8 + (srcHWAddress.getLength() + srcPAddress.getLength()) * 2); + skbuf.insert(8 + (sourceHardwareAddress.getLength() + sourceProtocolAddress.getLength()) * 2); int ofs = 0; - skbuf.set16(ofs + 0, hwtype); - skbuf.set16(ofs + 2, ptype); - skbuf.set(ofs + 4, srcHWAddress.getLength()); - skbuf.set(ofs + 5, srcPAddress.getLength()); - skbuf.set16(ofs + 6, op); + skbuf.set16(ofs + 0, hardwareType); + skbuf.set16(ofs + 2, protocolType); + skbuf.set(ofs + 4, sourceHardwareAddress.getLength()); + skbuf.set(ofs + 5, sourceProtocolAddress.getLength()); + skbuf.set16(ofs + 6, operation.getId()); ofs += 8; - srcHWAddress.writeTo(skbuf, ofs); - ofs += srcHWAddress.getLength(); - srcPAddress.writeTo(skbuf, ofs); - ofs += srcPAddress.getLength(); - targetHWAddress.writeTo(skbuf, ofs); - ofs += targetHWAddress.getLength(); - targetPAddress.writeTo(skbuf, ofs); + sourceHardwareAddress.writeTo(skbuf, ofs); + ofs += sourceHardwareAddress.getLength(); + sourceProtocolAddress.writeTo(skbuf, ofs); + ofs += sourceProtocolAddress.getLength(); + destinationHardwareAddress.writeTo(skbuf, ofs); + ofs += destinationHardwareAddress.getLength(); + destinationProtocolAddress.writeTo(skbuf, ofs); } /** @@ -135,14 +139,14 @@ * Gets the source address of the packet described in this header */ public ProtocolAddress getSourceAddress() { - return srcPAddress; + return sourceProtocolAddress; } /** * Gets the source address of the packet described in this header */ public ProtocolAddress getDestinationAddress() { - return targetPAddress; + return destinationProtocolAddress; } public int getDataLength() { @@ -153,83 +157,93 @@ * Gets the hardware type */ public int getHType() { - return hwtype; + return hardwareType; } /** * Gets the operation */ - public int getOperation() { - return op; + public ARPOperation getOperation() { + return operation; } /** * Gets the protocol type */ public int getPType() { - return ptype; + return protocolType; } /** * Gets the source hardware address */ public HardwareAddress getSrcHWAddress() { - return srcHWAddress; + return sourceHardwareAddress; } /** * Gets the source protocol address */ public ProtocolAddress getSrcPAddress() { - return srcPAddress; + return sourceProtocolAddress; } /** * Gets the target hardware address */ public HardwareAddress getTargetHWAddress() { - return targetHWAddress; + return destinationHardwareAddress; } /** * Gets the target protocol address */ public ProtocolAddress getTargetPAddress() { - return targetPAddress; + return destinationProtocolAddress; } + + public int getHardwareAddressSize() { + return hardwareAddressSize; + } + + public int getProtocolAddressSize() { + return protocolAddressSize; + } /** * Swap the two src and target addresses * */ public void swapAddresses() { - final HardwareAddress hwTmp = targetHWAddress; - final ProtocolAddress pTmp = targetPAddress; - targetHWAddress = srcHWAddress; - targetPAddress = srcPAddress; - srcHWAddress = hwTmp; - srcPAddress = pTmp; + final HardwareAddress hwTmp = destinationHardwareAddress; + final ProtocolAddress pTmp = destinationProtocolAddress; + destinationHardwareAddress = sourceHardwareAddress; + destinationProtocolAddress = sourceProtocolAddress; + sourceHardwareAddress = hwTmp; + sourceProtocolAddress = pTmp; } + /** - * @param i + * + * @param operation */ - public void setOperation(int i) { - op = i; + public void setOperation(ARPOperation operation) { + this.operation = operation; } /** * @param address */ public void setSrcHWAddress(HardwareAddress address) { - srcHWAddress = address; + sourceHardwareAddress = address; } /** * @param address */ public void setSrcPAddress(ProtocolAddress address) { - srcPAddress = address; + sourceProtocolAddress = address; } } Modified: trunk/net/src/net/org/jnode/net/arp/ARPNetworkLayer.java =================================================================== --- trunk/net/src/net/org/jnode/net/arp/ARPNetworkLayer.java 2012-12-19 08:38:08 UTC (rev 5936) +++ trunk/net/src/net/org/jnode/net/arp/ARPNetworkLayer.java 2012-12-21 13:50:22 UTC (rev 5937) @@ -46,14 +46,20 @@ * @author epr */ @SharedStatics -public class ARPNetworkLayer implements NetworkLayer, ARPConstants { - private static final boolean DEBUG = false; +public class ARPNetworkLayer implements NetworkLayer { + + private static final int IPv4_PROTOCOL_SIZE = 4; + /** Delay between ARP requests in millisecond */ + public static final int ARP_REQUEST_DELAY = 1500; + /** * My logger */ private static final Logger log = Logger.getLogger(ARPNetworkLayer.class); + private static final boolean DEBUG = false; + /** * My statistics */ @@ -149,7 +155,7 @@ stat.opackets.inc(); hdr.swapAddresses(); hdr.setSrcHWAddress(deviceAPI.getAddress()); - hdr.setOperation(ARP_REPLY); + hdr.setOperation(ARPOperation.ARP_REPLY); skbuf.clear(); skbuf.setProtocolID(getProtocolID()); hdr.prefixTo(skbuf); @@ -315,11 +321,11 @@ final NetDeviceAPI api = getAPI(device); final HardwareAddress srcHwAddr = api.getAddress(); final HardwareAddress trgHwAddr = srcHwAddr.getDefaultBroadcastAddress(); - final int op = ARP_REQUEST; + final ARPOperation op = ARPOperation.ARP_REQUEST; final int hwtype = srcHwAddr.getType(); final int ptype = address.getType(); - final ARPHeader hdr = new ARPHeader(srcHwAddr, myAddress, trgHwAddr, address, op, hwtype, ptype); + final ARPHeader hdr = new ARPHeader(srcHwAddr, myAddress, trgHwAddr, address, op, hwtype, ptype,EthernetConstants.ETH_ALEN,IPv4_PROTOCOL_SIZE); final SocketBuffer skbuf = new SocketBuffer(); skbuf.setProtocolID(EthernetConstants.ETH_P_ARP); hdr.prefixTo(skbuf); Added: trunk/net/src/net/org/jnode/net/arp/ARPOperation.java =================================================================== --- trunk/net/src/net/org/jnode/net/arp/ARPOperation.java (rev 0) +++ trunk/net/src/net/org/jnode/net/arp/ARPOperation.java 2012-12-21 13:50:22 UTC (rev 5937) @@ -0,0 +1,26 @@ +package org.jnode.net.arp; + + +public enum ARPOperation { + ARP_REQUEST (1), + ARP_REPLY (2), + RARP_REQUEST (3), + RARP_REPLY (4); + + private int id; + + private ARPOperation(int id){ + this.id = id; + } + + public int getId(){ + return this.id; + } + + public static ARPOperation getType(int id){ + for(ARPOperation t : ARPOperation.values()){ + return t; + } + return null; + } +} Added: trunk/net/src/test/org/jnode/net/arp/ARPHeaderTest.java =================================================================== --- trunk/net/src/test/org/jnode/net/arp/ARPHeaderTest.java (rev 0) +++ trunk/net/src/test/org/jnode/net/arp/ARPHeaderTest.java 2012-12-21 13:50:22 UTC (rev 5937) @@ -0,0 +1,40 @@ +package org.jnode.net.arp; + +import static org.junit.Assert.*; + +import java.net.SocketException; + +import org.jnode.net.SocketBuffer; +import org.jnode.net.ethernet.EthernetConstants; +import org.junit.Test; + +public class ARPHeaderTest { + + private static final int ARP_HEADER_LENGTH = 28; + + @Test + public void testHeaderFromSocketBuffer() throws SocketException { + SocketBuffer buffer = getSocketBuffer(); + ARPHeader header = new ARPHeader(buffer); + assertEquals(ARP_HEADER_LENGTH,header.getLength()); + assertEquals(ARPOperation.ARP_REQUEST,header.getOperation()); + assertEquals(1,header.getHType()); + assertEquals(EthernetConstants.ETH_P_IP,header.getPType()); + } + + private SocketBuffer getSocketBuffer() { + SocketBuffer buffer = new SocketBuffer(ARP_HEADER_LENGTH); + buffer.append(ARP_HEADER_LENGTH); + buffer.set16(0, 1); + buffer.set16(2, 0x800); + buffer.set(4, 6); + buffer.set(5, 4); + buffer.set16(6, 1); + buffer.set(8, 0); + buffer.set(14, 0); + buffer.set(18, 0); + buffer.set(24, 0); + return buffer; + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |