From: <ga...@us...> - 2012-12-21 14:01:17
|
Revision: 5938 http://jnode.svn.sourceforge.net/jnode/?rev=5938&view=rev Author: galatnm Date: 2012-12-21 14:01:09 +0000 (Fri, 21 Dec 2012) Log Message: ----------- NET : ICMP improvements. Modified Paths: -------------- trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPAddressMaskHeader.java trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPConstants.java trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPEchoHeader.java trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPExHeader.java trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPHeader.java trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPProtocol.java trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPTimestampHeader.java trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPUnreachableHeader.java Added Paths: ----------- trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPHeaderFactory.java trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPType.java Modified: trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPAddressMaskHeader.java =================================================================== --- trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPAddressMaskHeader.java 2012-12-21 13:50:22 UTC (rev 5937) +++ trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPAddressMaskHeader.java 2012-12-21 14:01:09 UTC (rev 5938) @@ -33,9 +33,9 @@ /** * @param type */ - public ICMPAddressMaskHeader(int type, int identifier, int seqNumber, IPv4Address subnetMask) { + public ICMPAddressMaskHeader(ICMPType type, int identifier, int seqNumber, IPv4Address subnetMask) { super(type, 0, identifier, seqNumber); - if ((type != ICMP_ADDRESS) && (type != ICMP_ADDRESSREPLY)) { + if ((type != ICMPType.ICMP_ADDRESS) && (type != ICMPType.ICMP_ADDRESSREPLY)) { throw new IllegalArgumentException("Invalid type " + type); } this.subnetMask = subnetMask; @@ -46,8 +46,8 @@ */ public ICMPAddressMaskHeader(SocketBuffer skbuf) { super(skbuf); - final int type = getType(); - if ((type != ICMP_ADDRESS) && (type != ICMP_ADDRESSREPLY)) { + final ICMPType type = getType(); + if ((type != ICMPType.ICMP_ADDRESS) && (type != ICMPType.ICMP_ADDRESSREPLY)) { throw new IllegalArgumentException("Invalid type " + type); } this.subnetMask = new IPv4Address(skbuf, 8); Modified: trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPConstants.java =================================================================== --- trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPConstants.java 2012-12-21 13:50:22 UTC (rev 5937) +++ trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPConstants.java 2012-12-21 14:01:09 UTC (rev 5938) @@ -25,22 +25,6 @@ */ public interface ICMPConstants { - public static final int ICMP_ECHOREPLY = 0; /* Echo Reply */ - public static final int ICMP_DEST_UNREACH = 3; /* Destination Unreachable */ - public static final int ICMP_SOURCE_QUENCH = 4; /* Source Quench */ - public static final int ICMP_REDIRECT = 5; /* Redirect (change route) */ - public static final int ICMP_ECHO = 8; /* Echo Request */ - public static final int ICMP_TIME_EXCEEDED = 11; /* Time Exceeded */ - public static final int ICMP_PARAMETERPROB = 12; /* Parameter Problem */ - public static final int ICMP_TIMESTAMP = 13; /* Timestamp Request */ - public static final int ICMP_TIMESTAMPREPLY = 14; /* Timestamp Reply */ - public static final int ICMP_INFO_REQUEST = 15; /* Information Request */ - public static final int ICMP_INFO_REPLY = 16; /* Information Reply */ - public static final int ICMP_ADDRESS = 17; /* Address Mask Request */ - public static final int ICMP_ADDRESSREPLY = 18; /* Address Mask Reply */ - public static final int NR_ICMP_TYPES = 18; - - /* Codes for UNREACH. */ public static final int ICMP_NET_UNREACH = 0; /* Network Unreachable */ public static final int ICMP_HOST_UNREACH = 1; /* Host Unreachable */ Modified: trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPEchoHeader.java =================================================================== --- trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPEchoHeader.java 2012-12-21 13:50:22 UTC (rev 5937) +++ trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPEchoHeader.java 2012-12-21 14:01:09 UTC (rev 5938) @@ -32,10 +32,15 @@ * @param identifier * @param seqNumber */ - public ICMPEchoHeader(int type, int identifier, int seqNumber) { + public ICMPEchoHeader(ICMPType type, int identifier, int seqNumber) { super(type, 0, identifier, seqNumber); } + public ICMPEchoHeader(int identifier, int seqNumber) { + super(ICMPType.ICMP_ECHO, 0, identifier, seqNumber); + } + + /** * @param skbuf */ @@ -58,9 +63,9 @@ * @return A header that is a suitable reply to this message */ public ICMPEchoHeader createReplyHeader() { - if (getType() != ICMP_ECHO) { + if (getType() != ICMPType.ICMP_ECHO) { throw new IllegalArgumentException("Not an echo request"); } - return new ICMPEchoHeader(ICMP_ECHOREPLY, getIdentifier(), getSeqNumber()); + return new ICMPEchoHeader(ICMPType.ICMP_ECHOREPLY, getIdentifier(), getSeqNumber()); } } Modified: trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPExHeader.java =================================================================== --- trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPExHeader.java 2012-12-21 13:50:22 UTC (rev 5937) +++ trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPExHeader.java 2012-12-21 14:01:09 UTC (rev 5938) @@ -35,7 +35,7 @@ /** * @param type */ - public ICMPExHeader(int type, int code, int identifier, int seqNumber) { + public ICMPExHeader(ICMPType type, int code, int identifier, int seqNumber) { super(type, code); this.identifier = identifier; this.seqNumber = seqNumber; Modified: trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPHeader.java =================================================================== --- trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPHeader.java 2012-12-21 13:50:22 UTC (rev 5937) +++ trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPHeader.java 2012-12-21 14:01:09 UTC (rev 5938) @@ -30,7 +30,7 @@ */ public abstract class ICMPHeader implements TransportLayerHeader, ICMPConstants { - private final int type; + private final ICMPType type; private final int code; private final boolean checksumOk; @@ -40,10 +40,7 @@ * @param type * @param code */ - public ICMPHeader(int type, int code) { - if ((type < 0) || (type > NR_ICMP_TYPES)) { - throw new IllegalArgumentException("Invalid type " + type); - } + public ICMPHeader(ICMPType type, int code) { if (code < 0) { throw new IllegalArgumentException("Invalid code " + code); } @@ -58,7 +55,7 @@ * @param skbuf */ public ICMPHeader(SocketBuffer skbuf) { - this.type = skbuf.get(0); + this.type = ICMPType.getType(skbuf.get(0)); this.code = skbuf.get(1); final int dataLength = ((IPv4Header) skbuf.getNetworkLayerHeader()).getDataLength(); final int ccs = IPv4Utils.calcChecksum(skbuf, 0, dataLength); @@ -70,7 +67,7 @@ */ public void prefixTo(SocketBuffer skbuf) { skbuf.insert(getLength()); - skbuf.set(0, type); + skbuf.set(0, type.getId()); skbuf.set(1, code); skbuf.set16(2, 0); // Checksum, overwritten later doPrefixTo(skbuf); @@ -114,7 +111,7 @@ /** * Gets the type field */ - public int getType() { + public ICMPType getType() { return type; } Added: trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPHeaderFactory.java =================================================================== --- trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPHeaderFactory.java (rev 0) +++ trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPHeaderFactory.java 2012-12-21 14:01:09 UTC (rev 5938) @@ -0,0 +1,46 @@ +package org.jnode.net.ipv4.icmp; + +import java.net.SocketException; + +import org.jnode.net.SocketBuffer; + +public class ICMPHeaderFactory { + + /** + * Create a type specific ICMP header. The type is read from the first first + * in the skbuf. + * + * @param skbuf + * @throws SocketException + */ + public static ICMPHeader createHeader(SocketBuffer skbuf) throws SocketException { + final ICMPType type = ICMPType.getType(skbuf.get(0)); + switch (type) { + case ICMP_DEST_UNREACH: + return new ICMPUnreachableHeader(skbuf); + + case ICMP_TIMESTAMP: + case ICMP_TIMESTAMPREPLY: + return new ICMPTimestampHeader(skbuf); + + case ICMP_ADDRESS: + case ICMP_ADDRESSREPLY: + return new ICMPAddressMaskHeader(skbuf); + + case ICMP_ECHOREPLY: + case ICMP_ECHO: + return new ICMPEchoHeader(skbuf); + + case ICMP_SOURCE_QUENCH: + case ICMP_REDIRECT: + case ICMP_TIME_EXCEEDED: + case ICMP_PARAMETERPROB: + case ICMP_INFO_REQUEST: + case ICMP_INFO_REPLY: + throw new SocketException("Not implemented"); + default: + throw new SocketException("Unknown ICMP type " + type); + } + } + +} Modified: trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPProtocol.java =================================================================== --- trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPProtocol.java 2012-12-21 13:50:22 UTC (rev 5937) +++ trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPProtocol.java 2012-12-21 14:01:09 UTC (rev 5938) @@ -44,7 +44,9 @@ public class ICMPProtocol implements IPv4Protocol, IPv4Constants, ICMPConstants, QueueProcessor<SocketBuffer> { - /** My logger */ + private static final String IPNAME_ICMP = "icmp"; + + /** My logger */ private Logger log = Logger.getLogger(getClass()); /** The IP service we're a part of */ @@ -77,7 +79,7 @@ * @see org.jnode.net.ipv4.IPv4Protocol#getName() */ public String getName() { - return "icmp"; + return IPNAME_ICMP; } /** @@ -96,7 +98,7 @@ stat.ipackets.inc(); try { - final ICMPHeader hdr = createHeader(skbuf); + final ICMPHeader hdr = ICMPHeaderFactory.createHeader(skbuf); skbuf.setTransportLayerHeader(hdr); skbuf.pull(hdr.getLength()); @@ -181,43 +183,8 @@ send(ipReplyHdr, hdr.createReplyHeader(), new SocketBuffer(skbuf)); } - /** - * Create a type specific ICMP header. The type is read from the first first - * in the skbuf. - * - * @param skbuf - * @throws SocketException - */ - private ICMPHeader createHeader(SocketBuffer skbuf) throws SocketException { - final int type = skbuf.get(0); - switch (type) { - case ICMP_DEST_UNREACH: - return new ICMPUnreachableHeader(skbuf); + - case ICMP_TIMESTAMP: - case ICMP_TIMESTAMPREPLY: - return new ICMPTimestampHeader(skbuf); - - case ICMP_ADDRESS: - case ICMP_ADDRESSREPLY: - return new ICMPAddressMaskHeader(skbuf); - - case ICMP_ECHOREPLY: - case ICMP_ECHO: - return new ICMPEchoHeader(skbuf); - - case ICMP_SOURCE_QUENCH: - case ICMP_REDIRECT: - case ICMP_TIME_EXCEEDED: - case ICMP_PARAMETERPROB: - case ICMP_INFO_REQUEST: - case ICMP_INFO_REPLY: - throw new SocketException("Not implemented"); - default: - throw new SocketException("Unknown ICMP type " + type); - } - } - /** * @see org.jnode.net.ipv4.IPv4Protocol#getStatistics() */ @@ -242,13 +209,11 @@ private void processReplyRequest(SocketBuffer skbuf) { final ICMPHeader hdr = (ICMPHeader) skbuf.getTransportLayerHeader(); try { - switch (hdr.getType()) { - case ICMP_ECHO: + if(hdr.getType() == ICMPType.ICMP_ECHO) { sendEchoReply((ICMPEchoHeader) hdr, skbuf); - break; } } catch (SocketException ex) { - log.debug("Error in ICMP reply", ex); + log.error("Error in ICMP reply", ex); } } Modified: trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPTimestampHeader.java =================================================================== --- trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPTimestampHeader.java 2012-12-21 13:50:22 UTC (rev 5937) +++ trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPTimestampHeader.java 2012-12-21 14:01:09 UTC (rev 5938) @@ -39,10 +39,10 @@ * @param receiveTimestamp * @param transmitTimestamp */ - public ICMPTimestampHeader(int type, int identifier, int seqNumber, int originateTimestamp, + public ICMPTimestampHeader(ICMPType type, int identifier, int seqNumber, int originateTimestamp, int receiveTimestamp, int transmitTimestamp) { super(type, 0, identifier, seqNumber); - if ((type != ICMP_TIMESTAMP) && (type != ICMP_TIMESTAMPREPLY)) { + if ((type != ICMPType.ICMP_TIMESTAMP) && (type != ICMPType.ICMP_TIMESTAMPREPLY)) { throw new IllegalArgumentException("Invalid type " + type); } this.originateTimestamp = originateTimestamp; @@ -55,8 +55,8 @@ */ public ICMPTimestampHeader(SocketBuffer skbuf) { super(skbuf); - final int type = getType(); - if ((type != ICMP_TIMESTAMP) && (type != ICMP_TIMESTAMPREPLY)) { + final ICMPType type = getType(); + if ((type != ICMPType.ICMP_TIMESTAMP) && (type != ICMPType.ICMP_TIMESTAMPREPLY)) { throw new IllegalArgumentException("Invalid type " + type); } this.originateTimestamp = skbuf.get32(8); Added: trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPType.java =================================================================== --- trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPType.java (rev 0) +++ trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPType.java 2012-12-21 14:01:09 UTC (rev 5938) @@ -0,0 +1,35 @@ +package org.jnode.net.ipv4.icmp; + +public enum ICMPType { + ICMP_ECHOREPLY(0), /* Echo Reply */ + ICMP_DEST_UNREACH(3), /* Destination Unreachable */ + ICMP_SOURCE_QUENCH(4), /* Source Quench */ + ICMP_REDIRECT(5), /* Redirect (change route) */ + ICMP_ECHO(8), /* Echo Request */ + ICMP_TIME_EXCEEDED(11), /* Time Exceeded */ + ICMP_PARAMETERPROB(12), /* Parameter Problem */ + ICMP_TIMESTAMP(13), /* Timestamp Request */ + ICMP_TIMESTAMPREPLY(14), /* Timestamp Reply */ + ICMP_INFO_REQUEST(15), /* Information Request */ + ICMP_INFO_REPLY(16), /* Information Reply */ + ICMP_ADDRESS(17), /* Address Mask Request */ + ICMP_ADDRESSREPLY(18); /* Address Mask Reply */ + + private int id; + + private ICMPType(int id){ + this.id = id; + } + + public int getId() { + return id; + } + + public static ICMPType getType(int id){ + for(ICMPType t : ICMPType.values()){ + return t; + } + return null; + } + +} Modified: trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPUnreachableHeader.java =================================================================== --- trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPUnreachableHeader.java 2012-12-21 13:50:22 UTC (rev 5937) +++ trunk/net/src/net/org/jnode/net/ipv4/icmp/ICMPUnreachableHeader.java 2012-12-21 14:01:09 UTC (rev 5938) @@ -31,7 +31,7 @@ * @param code */ public ICMPUnreachableHeader(int code) { - super(ICMP_DEST_UNREACH, code); + super(ICMPType.ICMP_DEST_UNREACH, code); } /** @@ -39,8 +39,8 @@ */ public ICMPUnreachableHeader(SocketBuffer skbuf) { super(skbuf); - final int type = getType(); - if (type != ICMP_DEST_UNREACH) { + final ICMPType type = getType(); + if (type != ICMPType.ICMP_DEST_UNREACH) { throw new IllegalArgumentException("Invalid type " + type); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |