[Javanetsim-cvs] javaNetSim/core/protocolsuite/tcp_ip ProtocolStack.java,1.3,1.4 UDP_packet.java,1.3
Status: Beta
Brought to you by:
darkkey
From: gift <gi...@us...> - 2005-11-18 19:41:41
|
Update of /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4528/core/protocolsuite/tcp_ip Modified Files: ProtocolStack.java UDP_packet.java Udp.java Log Message: UDP vNEXT :))) Index: ProtocolStack.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/ProtocolStack.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ProtocolStack.java 17 Nov 2005 20:13:56 -0000 1.3 --- ProtocolStack.java 18 Nov 2005 19:41:32 -0000 1.4 *************** *** 1190,1193 **** --- 1190,1250 ---- } + + /** + + * Sends a UDP packet to the IPAddress provided + + * @author robert_hulford + + * @author bevan_calliess + + * @author angela_brown + + * @param inDestIPAddress - Destination IP address eg: 192.168.0.2 + + * @throws CommunicationException + + * @version v0.20 + + */ + + /** + * This method sends the UDP packet + * @author gift (sourceforge.net user) + * @param inDestIPAddress destination IP address + * @param InterfaceName name of the intarface that sends UDP e.g. "eth0" + * @param indestPort destination port number + * @param insrcPort sorce port number + * @return Nothing + * @exception CommunicationException + * @exception LowLinkException + * @exception InvalidNetworkLayerDeviceException + * @version v0.20 + * @see CommunicationException + * @see LowLinkException + * @see InvalidNetworkLayerDeviceException + */ + + public void sendUDP(String inDestIPAddress, int indestPort, int insrcPort) throws CommunicationException, LowLinkException, InvalidNetworkLayerDeviceException + { + + //lets get first network interface name + //will be used when resolving source IP of UDP sender + String FirstInterfaceName; + Node temp = (Node)mParentNode; + FirstInterfaceName = temp.getFirstInterfaceName(); + + if (FirstInterfaceName!="unavailable") + { + if(IPV4Address.validateDecIP(inDestIPAddress)) + { + UDP_packet tosend = mUDPprotocol.sendUDPPacket(inDestIPAddress,FirstInterfaceName,indestPort,insrcPort); + sendPacket(tosend); + }else + throw new CommunicationException("Packet dropped host unreachable: " + inDestIPAddress); + } else + throw new InvalidNetworkLayerDeviceException("Network interface unavailable on host \"" + temp.getName() +"\"."); + + } }//EOF Index: Udp.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/Udp.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Udp.java 17 Nov 2005 20:13:56 -0000 1.2 --- Udp.java 18 Nov 2005 19:41:32 -0000 1.3 *************** *** 59,62 **** --- 59,64 ---- import java.io.Serializable; + import java.util.*; + import core.CommunicationException; *************** *** 71,76 **** import core.protocolsuite.tcp_ip.ProtocolStack; ! ! /** --- 73,77 ---- import core.protocolsuite.tcp_ip.ProtocolStack; ! ///import core.protocolsuite.tcp_ip.UDP_HashTableElement; /** *************** *** 112,120 **** ! class Udp implements Serializable ! { private ProtocolStack mParentStack; ! /** --- 113,141 ---- ! public class Udp implements Serializable ! { ! ! /* ! * UDP_HashTableElement.java ! * ! * Created on 18 November 2005, 21:09 ! */ + /** + * @author gift (sourceforge.net user) + * @version v0.10, 18 Nov 2005 + */ + public class UDP_HashTableElement + { + public byte PortStatus; // 0 - free port; 1 - port is being listened to; 2 - closed; 3 - busy. + public Object application; //points to application that listens to this port | provided PortStatus==1 + public String connectedtoIP; //contains IP of the other connected computer | provided PortStatus==3 + public int connectedtoPort; //contains port number of the other connected computer | provided PortStatus==3 + } + + private Hashtable PortTable = new Hashtable(); private ProtocolStack mParentStack; ! private static final int PORT_QUANT=100; ! private static final int PORT_START_NUMBER=3000; /** *************** *** 127,131 **** public Udp(ProtocolStack inParentStack) { ! mParentStack = inParentStack; } --- 148,164 ---- public Udp(ProtocolStack inParentStack) { ! int i; ! UDP_HashTableElement Elm = new UDP_HashTableElement(); ! ! Elm.PortStatus=0; ! Elm.application=null; ! Elm.connectedtoIP=""; ! Elm.connectedtoPort=0; ! mParentStack = inParentStack; ! ! for(i=0;i<PORT_QUANT;i++) ! { ! PortTable.put((PORT_START_NUMBER+i),Elm); ! } } *************** *** 135,139 **** * and prints out a message to the layer info. * @author gift (sourceforge.net user) ! * @param Protocol Stack * @return Nothing. * @exception TransportLayerException If UDP CHECK_SUM != 1 --- 168,172 ---- * and prints out a message to the layer info. * @author gift (sourceforge.net user) ! * @param inPacket a packet to receive (decompilate) * @return Nothing. * @exception TransportLayerException If UDP CHECK_SUM != 1 *************** *** 149,172 **** if(mParentStack.isInternalIP(inPacket.getDestIPAddress())){ ! // test check sum of UDP packet ! ! if (inPacket.getCheck_Sum() == 1) { ! //Create Layer info ! LayerInfo UDP_Info = new LayerInfo(getClass().getName()); ! UDP_Info.setObjectName(mParentStack.getParentNodeName()); ! UDP_Info.setDataType("UDP Packet"); ! UDP_Info.setLayer("Transport"); ! UDP_Info.setDescription("UDP packet received from "+ inPacket.getSourceIPAddress() + " message length " +inPacket.getUDP_MessageLength() + " bytes."); ! Simulation.addLayerInfo(UDP_Info); } else { ! throw new TransportLayerException("UDP Error: Incorrect checksum on receiving."); ! } } } --- 182,264 ---- if(mParentStack.isInternalIP(inPacket.getDestIPAddress())){ ! // test if destination UDP port exists on this NeworkLayerDevice ! if (PortTable.get(inPacket.get_destPort()) !=null) { ! ! //let's check all the things dealing with destination port number ! UDP_HashTableElement Elm = new UDP_HashTableElement(); ! Elm=(UDP_HashTableElement)PortTable.get(inPacket.get_destPort()); ! ! switch(Elm.PortStatus) ! { ! case 0: //port is free => Error: no application to receive UDP ! throw new TransportLayerException("UDP Error: no application listening to port "+inPacket.get_destPort() +" on host \""+ mParentStack.getParentNodeName()+"\"." ); ! // no "break" needed ;) ! case 1: // port is being listened to by a new application. Have to "establish" (in UDP no connections are being established) a new connection by replacing hashtable element ! Elm.PortStatus=3; //port will be busy from now ! Elm.connectedtoIP=inPacket.getSourceIPAddress(); ! Elm.connectedtoPort=inPacket.get_srcPort(); ! ! //now we decompose UDP datagram ! ! // everything is OK, one more test: check sum of UDP packet ! if (inPacket.getCheck_Sum() == 1) ! { ! //Create Layer info ! LayerInfo UDP_Info = new LayerInfo(getClass().getName()); ! UDP_Info.setObjectName(mParentStack.getParentNodeName()); ! UDP_Info.setDataType("UDP Packet"); ! UDP_Info.setLayer("Transport"); ! UDP_Info.setDescription("UDP packet received from "+ inPacket.getSourceIPAddress() + ":" + inPacket.get_srcPort() + " message: \"" +inPacket.getUDP_message() + "\". \r\n" + "UDP Port " + inPacket.get_destPort() + " has status \"busy\" from now. Host \"" + mParentStack.getParentNodeName()+"\"."); ! Simulation.addLayerInfo(UDP_Info); ! } else { ! throw new TransportLayerException("UDP Error: Incorrect checksum on receiving."); ! } ! break; ! ! case 2: //port is closed ! throw new TransportLayerException("UDP Error: port is closed! Port "+inPacket.get_destPort() +" on host \""+ mParentStack.getParentNodeName()+"\"." ); ! // no "break" needed ;) ! case 3: //busy port, let's check whether it's busy by us and our application ;) ! if ((Elm.connectedtoIP==inPacket.getSourceIPAddress()) && (Elm.connectedtoPort==inPacket.get_srcPort())) //temporary ! { ! //now we decompose UDP datagram ! ! // everything is OK, one more test: check sum of UDP packet ! if (inPacket.getCheck_Sum() == 1) ! { ! //Create Layer info ! ! LayerInfo UDP_Info = new LayerInfo(getClass().getName()); ! ! UDP_Info.setObjectName(mParentStack.getParentNodeName()); ! ! UDP_Info.setDataType("UDP Packet"); ! ! UDP_Info.setLayer("Transport"); ! ! UDP_Info.setDescription("UDP packet received from "+ inPacket.getSourceIPAddress() + ":" + inPacket.get_srcPort() + " message: \"" +inPacket.getUDP_message() + "\". \r\n" + "Host \"" + mParentStack.getParentNodeName()+"\"."); ! ! Simulation.addLayerInfo(UDP_Info); ! } else { ! throw new TransportLayerException("UDP Error: Incorrect checksum on receiving."); ! } ! } else { ! throw new TransportLayerException("UDP Error: Port is connected to another host or/and port. \r\n" + "Port "+inPacket.get_destPort() +" on host \""+ mParentStack.getParentNodeName()+"\"."); ! } ! break; ! ! default: //UDP Error: unknown port status :( ! throw new TransportLayerException("UDP Error: unknown port status! Port "+inPacket.get_destPort() +" on host \""+ mParentStack.getParentNodeName()+"\"."); ! } } else { ! throw new TransportLayerException("UDP Error: Port " + inPacket.get_destPort() + " does not exist. Host \"" + mParentStack.getParentNodeName()+"\"."); ! } } } *************** *** 176,188 **** * and prints out a message to the layer info. * @author gift (sourceforge.net user) ! * @param inDestIPAddress ! * @param InterfaceName name of the intarface that sends UDP e.g. "eth0" * @return UDP_packet * @version v0.20 */ ! public UDP_packet sendUDPPacket(String inDestIPAddress, String InterfaceName) ! { ! UDP_packet tosend = new UDP_packet(inDestIPAddress, mParentStack.getIPAddress(InterfaceName)); //Create Layer info --- 268,283 ---- * and prints out a message to the layer info. * @author gift (sourceforge.net user) ! * @param inDestIPAddress destination IP address ! * @param FirstInterfaceName name of the first network intarface of the node that sends UDP e.g. "eth0" ! * @param indestPort destination port number ! * @param insrcPort sorce port number * @return UDP_packet * @version v0.20 */ ! public UDP_packet sendUDPPacket(String inDestIPAddress,String FirstInterfaceName,int indestPort, int insrcPort) ! { ! ! UDP_packet tosend = new UDP_packet(inDestIPAddress,mParentStack.getIPAddress(FirstInterfaceName),indestPort,insrcPort); //Create Layer info *************** *** 196,200 **** UDP_Info.setLayer("Transport"); ! UDP_Info.setDescription("Created UDP packet to " + inDestIPAddress); Simulation.addLayerInfo(UDP_Info); --- 291,295 ---- UDP_Info.setLayer("Transport"); ! UDP_Info.setDescription("Created UDP packet to " + inDestIPAddress + ":" + insrcPort +"."); Simulation.addLayerInfo(UDP_Info); Index: UDP_packet.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/UDP_packet.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** UDP_packet.java 17 Nov 2005 20:13:56 -0000 1.3 --- UDP_packet.java 18 Nov 2005 19:41:32 -0000 1.4 *************** *** 85,89 **** private int UDP_MessageLength; ! private String UDP_message; private static final int PTCL = 17; //see RFC :) --- 85,93 ---- private int UDP_MessageLength; ! private String UDP_message; ! ! private int UDP_srcPort; ! ! private int UDP_destPort; private static final int PTCL = 17; //see RFC :) *************** *** 100,112 **** * This method passes the destination and source addresses into the super class * @author gift (sourceforge.net user) ! * @param destination IP address, source IP address * @return Nothing. * @version v0.20 */ ! public UDP_packet(String inDestIPAddress, String inSourceIPAddress) { super (inDestIPAddress); this.setSourceIPAddress(inSourceIPAddress); UDP_MessageLength = HEAD_LENGTH; UDP_message = ""; --- 104,121 ---- * This method passes the destination and source addresses into the super class * @author gift (sourceforge.net user) ! * @param inDestIPAddress destination IP address ! * @param inSourceIPAddress source IP address ! * @param indestPort destination port number ! * @param insrcPort sorce port number * @return Nothing. * @version v0.20 */ ! public UDP_packet(String inDestIPAddress, String inSourceIPAddress, int indestPort, int insrcPort) { super (inDestIPAddress); this.setSourceIPAddress(inSourceIPAddress); + UDP_destPort = indestPort; + UDP_srcPort = insrcPort; UDP_MessageLength = HEAD_LENGTH; UDP_message = ""; *************** *** 170,178 **** * This if for design and future implementation of UDP_packet * @author gift (sourceforge.net user) ! * @param Nothing. * @return Nothing. * @version v0.20 */ ! public void calculateCheck_Sum() { --- 179,187 ---- * This if for design and future implementation of UDP_packet * @author gift (sourceforge.net user) ! * @param Unused. * @return Nothing. * @version v0.20 */ ! public void calculate_Check_Sum() { *************** *** 204,207 **** --- 213,240 ---- } + /** + * This method gets source port + * @author gift (sourceforge.net user) + * @param Unused. + * @return source UDP port number. + * @version v0.20 + */ + public int get_srcPort() + { + return UDP_srcPort; + } + + /** + * This method gets destination port + * @author gift (sourceforge.net user) + * @param Unused. + * @return destination UDP port number. + * @version v0.20 + */ + public int get_destPort() + { + return UDP_destPort; + } + }//EOF |