edu.uiuc.cs.net.DPRPManager
Class DPRPMessage

java.lang.Object
  |
  +--edu.uiuc.cs.net.DPRPManager.DPRPMessage
All Implemented Interfaces:
java.lang.Cloneable

public class DPRPMessage
extends java.lang.Object
implements java.lang.Cloneable

This class represents an individual DPRP Message. Interestingly, this class is actually the cornerstone of this entire piece of software. Not only does it lay down the format of the messages passed in the protocol, but it also includes key code for serializing message objects into byte streams (and also deserializing them back out again). It also overrides the 'equals()' and 'hashcode()' methods from class 'Object' (the only class to do so). Furthermore, this class contains many support methods that make dealing with messages easier for both the client and the server. Finally, this file contains a series of custom exceptions for the DPRPManager.

Version:
1.1 - October 31st, 2001
Author:
Andy Reitz (areitz@cs.uiuc.edu)

Field Summary
static byte CLI_OFFER_ACK
          Code for DPRP_OFFER_ACK Message.
static byte CLI_OFFER_NACK
          Code for DPRP_CLI_OFFER_NACK Message.
static byte CLI_RENEW_REQUEST
          Code for DPRP_CLI_RENEW_REQUEST Message.
static byte CLI_REQUEST
          Code for DPRP_CLI_REQUEST Message.
static byte CLI_TERMINATE_LEASE
          Sent by the client, when the the user wishes to immediately terminate a lease.
static int DEFAULT_PORT
          The default port that the server listens on.
private  java.net.InetAddress dstAddr
          The destination IP address of the message.
private  int dstPort
          The destination port for the message.
private  int duration
          The proposed lease duration, in seconds.
static byte ERROR
          Code for DPRP_ERROR Message.
private  java.net.InetAddress extAddr
          The external IP address of the gateway.
private  int extPort
          The external Port reserved by the Gateway.
private  short flags
          The flags, which can represent control options.
private  byte messageType
          The message type, defines the intended nature of the message.
private static byte[] nullIPAddr
          An IP address of all zeros, in byte array format.
private  byte protocol
          The protocol, either TCP (6) or UDP (17).
private static java.util.Random rando
          The Random object singleton instance.
private  java.net.InetAddress srcAddr
          The source IP address of the sender.
private  int srcPort
          The source port on the sender.
static byte SRV_COMMIT_OFFER
          Code for DPRP_SRV_COMMIT_OFFER Message.
static byte SRV_OFFER
          Code for DPRP_SRV_OFFER Message.
static byte SRV_OFFER_NACK_ACK
          Code for DPRP_SRV_OFFER_NACK_ACK Message.
static byte SRV_RENEW_ACK
          Code for DPRP_SRV_RENEW_ACK Message.
static byte SRV_RENEW_NACK
          Code for DPRP_SRV_RENEW_NACK Message.
static byte SRV_TERMINATE_ACK
          Sent by the server, responding to a termination request.
static java.lang.String[] supportedProtocols
          Return the supported protocols, in text format.
static byte TCP_PROTOCOL
          The numeric representation of the TCP protocol, as found in "/etc/services".
static byte UDP_PROTOCOL
          The numeric representation of the UDP protocol, as found in "/etc/services".
private  int xid
          The session identifier, ala DHCP.
 
Constructor Summary
DPRPMessage()
          Constructor for DPRPMessage.
DPRPMessage(java.net.InetAddress inServerIP, int inServerPort)
          Constructor for DPRPMessage.
 
Method Summary
protected  java.lang.Object clone()
          Overloaded from class Object, creates a copy of this message.
private  void defaultValues()
          Fills in various variables with default values.
 DPRPMessage deserialize(byte[] inBytes)
          This method converts a byte array representation of a DPRPMessage into an actual DPRPMessage object.
 boolean equals(DPRPMessage cmp)
          Compares two DPRPMessage objects for equality.
 boolean equals(java.lang.Object o)
          Compares this message to another object for equality.
static int[] fixupSigned(byte[] byteAry)
          Gets the unsigned equivalent of each byte in an array.
 void flipSendRecv()
          Flips the source and destination addresses in place.
 java.net.InetAddress getDestinationAddress()
          Get the destination address.
 int getDestinationPort()
          Get the destination port.
 int getDuration()
          Get the duration, in seconds.
 java.net.InetAddress getExternalIP()
          Get the external IP address.
 int getExternalPort()
          Get the external port.
 short getFlags()
          Get the flags.
 java.lang.String getMessageStr()
          Converts a messageType to a human-readable string equivalent.
 byte getMessageType()
          Get the message type.
private  int getNextXid()
          Returns the next xid.
 java.lang.String getPrimaryKey()
          Returns the primary key for this message: "externalIPAddr:externalPort:protocol".
 byte getProtocol()
          Get the protocol, as a byte.
 java.lang.String getProtocolStr()
          Get the protocol, as a string.
 java.net.InetAddress getSourceAddress()
          Get the source IP address.
 java.lang.String getSourceKey()
          Returns the source key, which is "sourceIPAddr:sourcePort".
 int getSourcePort()
          Get the source port.
 int getXid()
          Get the session ID.
 int hashCode()
          Overloaded from Object, once again.
static java.lang.String IParyToString(byte[] ba)
          Converts a byte[4] array into dotted-quad IP address notation.
static java.lang.String IParyToString(int[] ba)
          Converts an int[4] array into dotted-quad IP address notation.
 void printDump()
          Prints a human-readable version of the message to stdout.
static java.lang.String protToString(byte p)
          Converts a IP protocol identifier into a human-readable string.
 byte[] serialize()
          Converts a DPRPMessage into a byte stream.
 void setDestinationAddress(java.net.InetAddress dip)
          Set the destination IP address.
 void setDestinationPort(int dp)
          Set the destination port.
 void setDuration(int id)
          Set the duration this message.
 void setExternalIP(java.net.InetAddress eip)
          Set the external IP address.
 void setExternalPort(int ep)
          Set the external port for this message.
 void setFlags(short ifl)
          Set the flags for this message.
 void setMessageType(byte mt)
          Set the message type (constants in this class).
 void setProtocol(byte inp)
          Set the IP protocol.
 void setSourceAddress(java.net.InetAddress cip)
          Set the source IP address.
 void setSourcePort(int cp)
          Set the source port.
 void setXid(int xxx)
          Sets the xid.
static byte stringToProt(java.lang.String str)
          Converts a protocol string to its numeric representation.
 
Methods inherited from class java.lang.Object
, finalize, getClass, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

messageType

private byte messageType
The message type, defines the intended nature of the message.

protocol

private byte protocol
The protocol, either TCP (6) or UDP (17).

flags

private short flags
The flags, which can represent control options.

xid

private int xid
The session identifier, ala DHCP.

duration

private int duration
The proposed lease duration, in seconds.

srcAddr

private java.net.InetAddress srcAddr
The source IP address of the sender.

srcPort

private int srcPort
The source port on the sender.

dstAddr

private java.net.InetAddress dstAddr
The destination IP address of the message.

dstPort

private int dstPort
The destination port for the message.

extAddr

private java.net.InetAddress extAddr
The external IP address of the gateway.

extPort

private int extPort
The external Port reserved by the Gateway.

rando

private static java.util.Random rando
The Random object singleton instance.

nullIPAddr

private static byte[] nullIPAddr
An IP address of all zeros, in byte array format. Used as in initializer.

ERROR

public static final byte ERROR
Code for DPRP_ERROR Message. This is sent whenever an error occurs, either on the client or server side. A specific error message (as defined in the DPRPErrors class) is then placed in the flags variable.

CLI_REQUEST

public static final byte CLI_REQUEST
Code for DPRP_CLI_REQUEST Message. This is sent solely by the client, in order to request a new lease.

SRV_OFFER

public static final byte SRV_OFFER
Code for DPRP_SRV_OFFER Message. This is sent by the server, in order to respond to the client's request with a lease offer.

CLI_OFFER_NACK

public static final byte CLI_OFFER_NACK
Code for DPRP_CLI_OFFER_NACK Message. This is sent by the client, in order to refuse the server's offer.

SRV_OFFER_NACK_ACK

public static final byte SRV_OFFER_NACK_ACK
Code for DPRP_SRV_OFFER_NACK_ACK Message. This is sent by the server, in order to acknowledge that it processed the client's offer refusal. This is necessary, because the in the process of generating an offer, the server reserves certain state, which the client must make sure is released.

CLI_OFFER_ACK

public static final byte CLI_OFFER_ACK
Code for DPRP_OFFER_ACK Message. This is sent by the client, in order to accept the server's generous offer.

SRV_COMMIT_OFFER

public static final byte SRV_COMMIT_OFFER
Code for DPRP_SRV_COMMIT_OFFER Message. This is sent by the server, when it has committed the state. Thus, when the client receives this message, it knows that it is free to start using the external port.

CLI_RENEW_REQUEST

public static final byte CLI_RENEW_REQUEST
Code for DPRP_CLI_RENEW_REQUEST Message. This is sent by the client in order to attempt to renew an outstanding lease.

SRV_RENEW_ACK

public static final byte SRV_RENEW_ACK
Code for DPRP_SRV_RENEW_ACK Message. This is sent by the server, telling the client that it has in fact renewed the lease. The client then updates its lease based upon the parameters contained in this message, and then goes to sleep.

SRV_RENEW_NACK

public static final byte SRV_RENEW_NACK
Code for DPRP_SRV_RENEW_NACK Message. This is sent by the server, when it cannot renew the client's lease. In the flags, the server will place an errorcode (again, from DPRPErrors), which will give the client a reason for the renew refusal.

CLI_TERMINATE_LEASE

public static final byte CLI_TERMINATE_LEASE
Sent by the client, when the the user wishes to immediately terminate a lease.

SRV_TERMINATE_ACK

public static final byte SRV_TERMINATE_ACK
Sent by the server, responding to a termination request.

DEFAULT_PORT

public static final int DEFAULT_PORT
The default port that the server listens on.

TCP_PROTOCOL

public static final byte TCP_PROTOCOL
The numeric representation of the TCP protocol, as found in "/etc/services".

UDP_PROTOCOL

public static final byte UDP_PROTOCOL
The numeric representation of the UDP protocol, as found in "/etc/services".

supportedProtocols

public static final java.lang.String[] supportedProtocols
Return the supported protocols, in text format.
Constructor Detail

DPRPMessage

public DPRPMessage(java.net.InetAddress inServerIP,
                   int inServerPort)
Constructor for DPRPMessage.
Parameters:
inServerName - the destination IP address
inServerPort - the destination port

DPRPMessage

public DPRPMessage()
Constructor for DPRPMessage.
Method Detail

defaultValues

private void defaultValues()
Fills in various variables with default values.

serialize

public byte[] serialize()
Converts a DPRPMessage into a byte stream. This process readies a message for transmission over the network. It is essentially a form of serialization, but it should be confused with the JDK's serialization framework (I'm not using any of that blarney).
Returns:
A byte array, containing all of the essentials of this message.

deserialize

public DPRPMessage deserialize(byte[] inBytes)
This method converts a byte array representation of a DPRPMessage into an actual DPRPMessage object. This is called in the DPRPSocket class, in order to build a DPRPMessage object as soon as the bytes come off of the wire.

This method works by plucking out a series of bytes, and attempting to convert them into a more complex data type. Assuming all of these conversions, these complex objects are stuffed into a DPRPMessage, which is then returned. In case the decoding does not proceed properly, this method returns an generic message of type DPRP_ERROR, with the INVALID_PACKET_FMT errorcode set. Receivers will treat this as any other error, and notify the sender appropriately.

Parameters:
inBytes - An array of bytes, containing the key bits of a DPRPMessage object.
Returns:
A valid DPRPMessage object, based on the byte array.

getNextXid

private int getNextXid()
Returns the next xid. This is done by creating a Random singleton, and getting the next integer in the sequence.
Returns:
A random number, in sequence.

setMessageType

public void setMessageType(byte mt)
Set the message type (constants in this class).

setProtocol

public void setProtocol(byte inp)
Set the IP protocol.

setDestinationAddress

public void setDestinationAddress(java.net.InetAddress dip)
Set the destination IP address.

setDestinationPort

public void setDestinationPort(int dp)
Set the destination port.

setSourceAddress

public void setSourceAddress(java.net.InetAddress cip)
Set the source IP address.

setSourcePort

public void setSourcePort(int cp)
Set the source port.

setExternalIP

public void setExternalIP(java.net.InetAddress eip)
Set the external IP address.

setExternalPort

public void setExternalPort(int ep)
Set the external port for this message.

setFlags

public void setFlags(short ifl)
Set the flags for this message.

setDuration

public void setDuration(int id)
Set the duration this message.

setXid

public void setXid(int xxx)
Sets the xid.
Parameters:
xxx - Moonshine just like mom used to make.

getMessageType

public byte getMessageType()
Get the message type.

getProtocol

public byte getProtocol()
Get the protocol, as a byte.

getProtocolStr

public java.lang.String getProtocolStr()
Get the protocol, as a string.

getSourceAddress

public java.net.InetAddress getSourceAddress()
Get the source IP address.

getSourcePort

public int getSourcePort()
Get the source port.

getDestinationAddress

public java.net.InetAddress getDestinationAddress()
Get the destination address.

getDestinationPort

public int getDestinationPort()
Get the destination port.

getExternalIP

public java.net.InetAddress getExternalIP()
Get the external IP address.

getExternalPort

public int getExternalPort()
Get the external port.

getFlags

public short getFlags()
Get the flags.

getDuration

public int getDuration()
Get the duration, in seconds.

getXid

public int getXid()
Get the session ID.

fixupSigned

public static int[] fixupSigned(byte[] byteAry)
Gets the unsigned equivalent of each byte in an array. This accomplished by looping through the array, and adding 256 to each byte that is negative. The resulting values are copied into an equivalent int array, and returned.
Parameters:
byteAry - An array of bytes to be de-signed.
Returns:
An integer array, of the same length as the input.

IParyToString

public static java.lang.String IParyToString(byte[] ba)
Converts a byte[4] array into dotted-quad IP address notation.
Parameters:
ba - A byte array.
Returns:
A string, in "x.y.z.q" form.

IParyToString

public static java.lang.String IParyToString(int[] ba)
Converts an int[4] array into dotted-quad IP address notation.
Parameters:
ba - An array of ints (to capture the unsigned byte properties)
Returns:
A string, in the form "x.y.z.q".

protToString

public static java.lang.String protToString(byte p)
Converts a IP protocol identifier into a human-readable string.
Parameters:
p - A byte, representing the protocol (as found in '/etc/protocols').
Returns:
A 3-letter string, representing the protocol identifier.

stringToProt

public static byte stringToProt(java.lang.String str)
                         throws DPRPInvalidProtocolException
Converts a protocol string to its numeric representation.
Parameters:
str - A string to be converted.
Returns:
The numeric representation, in byte form.
Throws:
DPRPInvalidProtocolException - If we don't know what the hell this string is supposed to represent.

getPrimaryKey

public java.lang.String getPrimaryKey()
Returns the primary key for this message: "externalIPAddr:externalPort:protocol".

getSourceKey

public java.lang.String getSourceKey()
Returns the source key, which is "sourceIPAddr:sourcePort".

flipSendRecv

public void flipSendRecv()
Flips the source and destination addresses in place.

clone

protected java.lang.Object clone()
Overloaded from class Object, creates a copy of this message. This is done by calling clone() from the superclass, and then filling in each field on this new object.
Overrides:
clone in class java.lang.Object
Returns:
An identical copy of this object. It would be much cooler if I could overload the return value, and have it return an actual DPRPMessage. There is entirely too much casting in this language.

hashCode

public int hashCode()
Overloaded from Object, once again. The only requirement for this method is that the int should be consistent for objects that are unchanging. Right now I just return the xid, which is a poor substitute for "the real thing".
Overrides:
hashCode in class java.lang.Object
Returns:
The xid of this message.

equals

public boolean equals(java.lang.Object o)
Compares this message to another object for equality. This method solved a very, very, very pernicious bug, in that when Java invokes the equals method in something like a Vector, it first casts the parameter object to class Object. Thus, we need to trap this invocation, and check to see if the Object is really a DPRPMessage. If it is, then we invoke the real equals method. Otherwise, the objects are said to be not equal.
Overrides:
equals in class java.lang.Object
Parameters:
o - A generic object.
Returns:
False if the object isn't a DPRPMessage, the result of the real equals() method otherwise.

equals

public boolean equals(DPRPMessage cmp)
Compares two DPRPMessage objects for equality. This entails going through each class variable, and checking for equality. If every field is identical, then the messages are said to be identical.
Parameters:
cmp - The DPRPMessage to compare against.
Returns:
True if the messages are field-equal, false otherwise.

getMessageStr

public java.lang.String getMessageStr()
Converts a messageType to a human-readable string equivalent.

printDump

public void printDump()
Prints a human-readable version of the message to stdout.