[Javanetsim-cvs] javaNetSim/core/protocolsuite/tcp_ip DHCPC.java, 1.1, 1.2 DHCPD.java, 1.1, 1.2 DHC
Status: Beta
Brought to you by:
darkkey
From: Alexander B. <da...@us...> - 2007-10-14 17:19:12
|
Update of /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv21615/core/protocolsuite/tcp_ip Modified Files: DHCPC.java DHCPD.java DHCPPacket.java ICMP_packet.java IP_packet.java IpV4.java ProtocolStack.java Tcp.java Udp.java Log Message: Added WAN interfaces class prototypes... Index: DHCPC.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/DHCPC.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** DHCPC.java 13 Oct 2007 12:57:00 -0000 1.1 --- DHCPC.java 14 Oct 2007 17:19:07 -0000 1.2 *************** *** 1,7 **** /* * DHCPC.java - * - * Created on 19 Nov 2005 Ç., 14:09 - * */ --- 1,4 ---- *************** *** 31,36 **** /** Creates a new instance of DHCPC */ ! public DHCPC(ProtocolStack inParentStack, int listenPort, int appType, int UID) { ! super(inParentStack, listenPort, appType, UID); appSock = mParentStack.SL().socket(jnSocket.UDP_socket, this); running = false; --- 28,33 ---- /** Creates a new instance of DHCPC */ ! public DHCPC(ProtocolStack inParentStack, int UID) { ! super(inParentStack, 0,0, UID); appSock = mParentStack.SL().socket(jnSocket.UDP_socket, this); running = false; *************** *** 48,56 **** mParentStack.SL().bind(appSock, mParentStack.getSrcIP(), 68); mParentStack.SL().listen(appSock); ! printLayerInfo("DHCP Client", "DHCPC binded port " + listenPort + "."); } catch (Exception e) { ! printLayerInfo("DHCP Client", "Error: cannot bind port " + listenPort + "."); ! throw new TransportLayerException("Cannot bind port " + listenPort + "."); } } --- 45,53 ---- mParentStack.SL().bind(appSock, mParentStack.getSrcIP(), 68); mParentStack.SL().listen(appSock); ! printLayerInfo("DHCP Client", "DHCP Client binded port " + 68 + "."); } catch (Exception e) { ! printLayerInfo("DHCP Client", "Error: cannot bind port " + 68 + "."); ! throw new TransportLayerException("Cannot bind port " + 68 + "."); } } *************** *** 148,156 **** p.fromBytes(Data); ! if(p.op == 2 && p.xid == xid){ switch(p.msgType){ case 2: //OFFER if(!completed){ DHCPPacket r = new DHCPPacket(); r.op = 1; --- 145,162 ---- p.fromBytes(Data); ! if(p.op == 2 && p.xid == xid){ switch(p.msgType){ case 2: //OFFER if(!completed){ + + LayerInfo protInfo = new LayerInfo(getClass().getName()); + protInfo.setObjectName(mParentStack.getParentNodeName()); + protInfo.setDataType("DHCP client"); + protInfo.setLayer("Application "); + protInfo.setDescription("Recieved DHCPOFFER(xid=" + p.xid + ") from " + p.DHCPServer + " with op=2 yiaddr='" + p.yiaddr + + "' lease time=" + p.leaseTime + "."); + Simulation.addLayerInfo(protInfo); + DHCPPacket r = new DHCPPacket(); r.op = 1; *************** *** 159,162 **** --- 165,182 ---- DHCPServer = r.DHCPServer = p.DHCPServer; r.WantedIP = p.yiaddr; + r.chaddr = mParentStack.getMACAddress(iface); + + protInfo = new LayerInfo(getClass().getName()); + protInfo.setObjectName(mParentStack.getParentNodeName()); + protInfo.setDataType("DHCP client"); + protInfo.setLayer("Application "); + protInfo.setDescription("Sending DHCPREQUEST(xid=" + p.xid + ") packet to 255.255.255.255 with op=1 chaddr='" + + r.chaddr + "' DHCP Server='" + r.DHCPServer + "' preferred IP='" + r.WantedIP + "'."); + Simulation.addLayerInfo(protInfo); + try{ + SendData(appSock, "255.255.255.255", 67, r.toBytes()); + }catch(CommunicationException e){ + throw new TransportLayerException(e.toString()); + } } break; *************** *** 167,172 **** if(!completed){ // here set up interfaces... try{ ! mParentStack.setIPAddress(iface,p.yiaddr ); mParentStack.setCustomSubnetMask(iface,p.SubnetMask ); mParentStack.setDefaultGateway(p.Gateway); --- 187,207 ---- if(!completed){ // here set up interfaces... + LayerInfo protInfo = new LayerInfo(getClass().getName()); + protInfo.setObjectName(mParentStack.getParentNodeName()); + protInfo.setDataType("DHCP client"); + protInfo.setLayer("Application "); + protInfo.setDescription("Recieved DHCPACK(xid=" + p.xid + ") from " + p.DHCPServer + " with op=2 yiaddr='" + p.yiaddr + + "' lease time=" + p.leaseTime + " subnet mask='" + p.SubnetMask + "' default gateway='" + p.Gateway + "'."); + Simulation.addLayerInfo(protInfo); + + protInfo = new LayerInfo(getClass().getName()); + protInfo.setObjectName(mParentStack.getParentNodeName()); + protInfo.setDataType("DHCP client"); + protInfo.setLayer("Application "); + protInfo.setDescription("Updating " + iface + " IP configuration."); + Simulation.addLayerInfo(protInfo); + try{ ! mParentStack.setIPAddress(iface, p.yiaddr ); mParentStack.setCustomSubnetMask(iface,p.SubnetMask ); mParentStack.setDefaultGateway(p.Gateway); *************** *** 182,235 **** }else{ //Invalid or not our packet; simply drop - } - //processing the protocol doings. - /*if(appType == 0){ - //client processing recieve - // printing some ... LayerInfo protInfo = new LayerInfo(getClass().getName()); protInfo.setObjectName(mParentStack.getParentNodeName()); ! protInfo.setDataType("DHCPC Protocol Data"); ! protInfo.setLayer("Application "); ! protInfo.setDescription("Recieving DHCPC message '" + Data + "' from server."); ! Simulation.addLayerInfo(protInfo); ! ! //mParentStack.UDP().closePort(sock); ! ! }else{ ! //server processing recieve ! try{ ! String sdHost = mParentStack.SL().get_socket(sock).dst_IP; ! int sdPort = mParentStack.SL().get_socket(sock).dst_port; ! ! LayerInfo protInfo = new LayerInfo(getClass().getName()); ! protInfo.setObjectName(mParentStack.getParentNodeName()); ! protInfo.setDataType("DHCPC Protocol Data"); protInfo.setLayer("Application "); ! protInfo.setDescription("Recieving DHCPC message '" + Data + "' from client " + sdHost + ":" + sdPort + ". Total recieved messages by server: " + recieved); Simulation.addLayerInfo(protInfo); ! ! LayerInfo protInfo2 = new LayerInfo(getClass().getName()); ! protInfo2.setObjectName(mParentStack.getParentNodeName()); ! protInfo2.setDataType("DHCPC Protocol Data"); ! protInfo2.setLayer("Application "); ! protInfo2.setDescription("Sending DHCPC message '" + Data + "' to client."); ! Simulation.addLayerInfo(protInfo2); ! ! SendData(Data); ! ! /*LayerInfo protInfo3 = new LayerInfo(getClass().getName()); ! protInfo3.setObjectName(mParentStack.getParentNodeName()); ! protInfo3.setDataType("DHCPC Protocol Data"); ! protInfo3.setLayer("Application "); ! protInfo3.setDescription("Server closing connection. Now listening on " + listenPort + "."); ! Simulation.addLayerInfo(protInfo3);*/ ! ! //Close(); ! //Listen(); ! /* }catch(Exception e){ ! System.out.println(e.toString()); ! ///*TODO*: here to catch ! } ! }*/ } --- 217,227 ---- }else{ //Invalid or not our packet; simply drop LayerInfo protInfo = new LayerInfo(getClass().getName()); protInfo.setObjectName(mParentStack.getParentNodeName()); ! protInfo.setDataType("DHCP client"); protInfo.setLayer("Application "); ! protInfo.setDescription("Recieved DHCP packet with invalid xid=" + p.xid + " and/or data."); Simulation.addLayerInfo(protInfo); ! } } *************** *** 243,250 **** */ public void StartDHCPC(String inIface, String WantedIP) throws CommunicationException, LowLinkException, InvalidNetworkLayerDeviceException, TransportLayerException{ running = true; completed = false; iface = inIface; ! Listen(); --- 235,246 ---- */ public void StartDHCPC(String inIface, String WantedIP) throws CommunicationException, LowLinkException, InvalidNetworkLayerDeviceException, TransportLayerException{ + if(running){ + Close(); + } + running = true; completed = false; iface = inIface; ! Listen(); *************** *** 253,261 **** xid = d.xid; d.WantedIP = WantedIP; ! d.chaddr = mParentStack.getIPAddress(iface); ! d.ciaddr = mParentStack.getMACAddress(iface); d.msgType = 1; ! SendData(appSock, "255.255.255.255", 67, d.toBytes()); } } --- 249,278 ---- xid = d.xid; d.WantedIP = WantedIP; ! d.ciaddr = mParentStack.getIPAddress(iface); ! d.chaddr = mParentStack.getMACAddress(iface); d.msgType = 1; ! if(d.ciaddr == null){ ! d.ciaddr = ""; ! } ! ! String Data = d.toBytes(); ! ! LayerInfo protInfo = new LayerInfo(getClass().getName()); ! protInfo.setObjectName(mParentStack.getParentNodeName()); ! protInfo.setDataType("DHCP client"); ! protInfo.setLayer("Application "); ! protInfo.setDescription("Starting DHCP discover process on interface " + iface + "... Transaction ID is " + xid + "."); ! Simulation.addLayerInfo(protInfo); ! ! protInfo = new LayerInfo(getClass().getName()); ! protInfo.setObjectName(mParentStack.getParentNodeName()); ! protInfo.setDataType("DHCP client"); ! protInfo.setLayer("Application "); ! protInfo.setDescription("Sending DHCPDISCOVER packet to 255.255.255.255 with op=1 chaddr='" + ! d.chaddr + "' ciaddr='" + d.ciaddr + "' preferred IP='" + WantedIP + "'."); ! Simulation.addLayerInfo(protInfo); ! ! SendData(appSock, "255.255.255.255", 67, Data); } } Index: DHCPPacket.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/DHCPPacket.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** DHCPPacket.java 13 Oct 2007 12:57:00 -0000 1.1 --- DHCPPacket.java 14 Oct 2007 17:19:07 -0000 1.2 *************** *** 2,9 **** * DHCPPacket.java * - * Created on 11 Îêòÿáðü 2007 ã., 19:20 - * - * To change this template, choose Tools | Template Manager - * and open the template in the editor. */ --- 2,5 ---- *************** *** 84,88 **** //| | //| | ! tmp = convertMAC(chaddr); c[28] = tmp.charAt(0); c[29] = tmp.charAt(1); --- 80,89 ---- //| | //| | ! ! try{ ! tmp = convertMAC(chaddr); ! }catch(Exception e){ ! System.out.println(e.toString()); ! } c[28] = tmp.charAt(0); c[29] = tmp.charAt(1); *************** *** 165,171 **** res += String.valueOf(o,0,optIdx); ! for(int j=0; j<res.length(); j++){ ! System.out.println(j + ":\t" + (short) res.charAt(j) ); ! } return res; --- 166,172 ---- res += String.valueOf(o,0,optIdx); ! /*for(int j=0; j<res.length(); j++){ ! System.out.println(j + ":\t" + (short) res.charAt(j) ); ! }*/ return res; *************** *** 176,181 **** int i = 0; - //Integer. - for(int j = 0; j<6; j++){ mac[j] = (char)Integer.parseInt(MAC.substring(i, i+2),16); --- 177,180 ---- *************** *** 190,199 **** byte[] ip = new byte[4]; ip[0] = (byte)o1; ! ip[1] = (byte)o1; ! ip[2] = (byte)o1; ! ip[3] = (byte)o1; try{ res = java.net.InetAddress.getByAddress(ip).getHostAddress(); ! }catch(Exception e){ res = ""; } return res; --- 189,201 ---- byte[] ip = new byte[4]; ip[0] = (byte)o1; ! ip[1] = (byte)o2; ! ip[2] = (byte)o3; ! ip[3] = (byte)o4; try{ res = java.net.InetAddress.getByAddress(ip).getHostAddress(); ! }catch(Exception e){ ! e.printStackTrace(); ! res = ""; ! } return res; Index: Udp.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/Udp.java,v retrieving revision 1.40 retrieving revision 1.41 diff -C2 -d -r1.40 -r1.41 *** Udp.java 14 Oct 2007 00:22:14 -0000 1.40 --- Udp.java 14 Oct 2007 17:19:07 -0000 1.41 *************** *** 112,116 **** { // test if this packet is for a local Address. ! if(mParentStack.isInternalIP(inPacket.getDestIPAddress())){ // test if destination UDP port exists on this NeworkLayerDevice --- 112,116 ---- { // test if this packet is for a local Address. ! if(mParentStack.isInternalIP(inPacket.getDestIPAddress()) || mParentStack.isBroadcastIP(inPacket.getDestIPAddress())){ // test if destination UDP port exists on this NeworkLayerDevice Index: ProtocolStack.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/ProtocolStack.java,v retrieving revision 1.58 retrieving revision 1.59 diff -C2 -d -r1.58 -r1.59 *** ProtocolStack.java 13 Oct 2007 12:57:00 -0000 1.58 --- ProtocolStack.java 14 Oct 2007 17:19:07 -0000 1.59 *************** *** 221,228 **** --- 221,252 ---- String GatewayAddress = null; String outInterface[] = new String[2]; + if(!IPV4Address.validateDecIP(inPacket.mDestIPAddress)){ throw new CommunicationException("Packet dropped host unreachable: " + inPacket.mDestIPAddress); } + try{ + IPV4Address p_ip = new IPV4Address(inPacket.mDestIPAddress); + + if(p_ip.isBroadcast()){ + NetworkLayerDevice temp = (NetworkLayerDevice)mParentNode; + ArrayList nics = mParentNode.getAllInterfacesNames(); + + for(int i=0; i<nics.size(); i++){ + if(temp.isActiveInterface((String)nics.get(i))){ + LayerInfo routeInfo = new LayerInfo(getClass().getName()); + routeInfo.setObjectName(getParentNodeName()); + routeInfo.setDataType(trimClassName(inPacket.getClass().getName())); + routeInfo.setLayer("Network"); + routeInfo.setDescription("Packet Received: Network Layer Device is sending broadcast packet through interface " + (String)nics.get(i) + "."); + Simulation.addLayerInfo(routeInfo); + inPacket.mSourceIPAddress = mIPprotocol.getIPAddress((String)nics.get(i)); + temp.sendPacket("FF:FF:FF:FF:FF:FF", inPacket, (String)nics.get(i) ); + } + } + + return ; + } + outInterface = mIPprotocol.router(inPacket.getDestIPAddress()); try{ *************** *** 324,327 **** --- 348,353 ---- System.out.println("::" + inPacket + "::"); System.out.println("ProtocolStack.java: SendPacket " + e.toString()); + }catch(Exception e){ + e.printStackTrace(); } *************** *** 1243,1247 **** } ! /** --- 1269,1282 ---- } ! public boolean isBroadcastIP(String inTestIPAddress){ ! try{ ! IPV4Address ip = new IPV4Address(inTestIPAddress); ! if(ip.isBroadcast()){ ! return true; ! } ! }catch(Exception e){} ! ! return false; ! } /** Index: Tcp.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/Tcp.java,v retrieving revision 1.99 retrieving revision 1.100 diff -C2 -d -r1.99 -r1.100 *** Tcp.java 14 Oct 2007 00:22:14 -0000 1.99 --- Tcp.java 14 Oct 2007 17:19:07 -0000 1.100 *************** *** 2,6 **** Java Network Simulator (javaNetSim) ! Copyright (c) 2006, 2005, Ice Team; All rights reserved. Copyright (c) 2004, jFirewallSim development team; All rights reserved. --- 2,6 ---- Java Network Simulator (javaNetSim) ! Copyright (c) 2007, 2006, 2005, Ice Team; All rights reserved. Copyright (c) 2004, jFirewallSim development team; All rights reserved. Index: IP_packet.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/IP_packet.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** IP_packet.java 24 Feb 2006 10:20:11 -0000 1.3 --- IP_packet.java 14 Oct 2007 17:19:07 -0000 1.4 *************** *** 246,249 **** --- 246,257 ---- } + + public String toBytes(){ + return RawtoBytes() + IPtoBytes(); + } + + public String IPtoBytes(){ + return "I|" + mSourceIPAddress + "|" + mDestIPAddress + "#"; + } }//EOF Index: ICMP_packet.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/ICMP_packet.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ICMP_packet.java 20 Nov 2005 20:30:53 -0000 1.2 --- ICMP_packet.java 14 Oct 2007 17:19:07 -0000 1.3 *************** *** 116,118 **** --- 116,126 ---- } + public String toBytes(){ + return RawtoBytes() + IPtoBytes() + ICMPtoBytes(); + } + + public String ICMPtoBytes(){ + return "M|" + mMessageCode + "|" + ICMP_message + "#"; + } + }//EOF \ No newline at end of file Index: IpV4.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/IpV4.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** IpV4.java 13 Oct 2007 12:57:00 -0000 1.11 --- IpV4.java 14 Oct 2007 17:19:07 -0000 1.12 *************** *** 858,863 **** Enumeration en = ipAddress.elements(); - - while(en.hasMoreElements()){ --- 858,861 ---- Index: DHCPD.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/DHCPD.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** DHCPD.java 13 Oct 2007 12:57:00 -0000 1.1 --- DHCPD.java 14 Oct 2007 17:19:07 -0000 1.2 *************** *** 1,7 **** /* * DHCPD.java - * - * Created on 19 Nov 2005 Ç., 14:09 - * */ --- 1,4 ---- *************** *** 31,34 **** --- 28,35 ---- } + public pool new_pool(){ + return new pool(); + } + public class lease{ public String IP; *************** *** 45,49 **** public int leaseTime; ! public long leased; } --- 46,56 ---- public int leaseTime; ! public long leased; ! ! public lease(){ ! negotiation_started = 0; ! xid = 0; ! completed = false; ! } } *************** *** 52,57 **** /** Creates a new instance of DHCPD */ ! public DHCPD(ProtocolStack inParentStack, int listenPort, int appType, int UID) { ! super(inParentStack, listenPort, appType, UID); appSock = mParentStack.SL().socket(jnSocket.UDP_socket, this); } --- 59,64 ---- /** Creates a new instance of DHCPD */ ! public DHCPD(ProtocolStack inParentStack, int UID) { ! super(inParentStack, 0, 0, UID); appSock = mParentStack.SL().socket(jnSocket.UDP_socket, this); } *************** *** 67,75 **** mParentStack.SL().bind(appSock, mParentStack.getSrcIP(), 67); mParentStack.SL().listen(appSock); ! printLayerInfo("DHCP server", "DHCP server starts listening in port " + listenPort + "."); } catch (Exception e) { ! printLayerInfo("DHCP server", "Error: cannot bind port " + listenPort + "."); ! throw new TransportLayerException("Cannot bind port " + listenPort + "."); } } --- 74,82 ---- mParentStack.SL().bind(appSock, mParentStack.getSrcIP(), 67); mParentStack.SL().listen(appSock); ! printLayerInfo("DHCP server", "DHCP server starts listening in port " + 67 + "."); } catch (Exception e) { ! printLayerInfo("DHCP server", "Error: cannot bind port " + 67 + "."); ! throw new TransportLayerException("Cannot bind port " + 67 + "."); } } *************** *** 165,168 **** --- 172,224 ---- lease l = new lease(); + l.IP = ""; + + String IP; + String Genmask; + String Network, PoolNetwork; + String PoolName; + + for(Enumeration e = pools.keys(); e.hasMoreElements();){ + PoolName = (String)e.nextElement(); + pool p = (pool)pools.get(PoolName); + + try{ + byte b[] = java.net.InetAddress.getByName(p.IP).getAddress(); + + boolean done = false; + + while(!done){ + if(b[3] < 254){ + b[3]++; + }else if(b[2] < 254){ + b[2]++; + }else if(b[1] < 254){ + b[1]++; + }else if(b[0] < 254){ + b[0]++; + }else{ + break; + } + + IP = java.net.InetAddress.getByAddress(b).getHostAddress(); + Network = IPV4Address.toDecimalString(IPV4Address.IPandMask(IPV4Address.toBinaryString(IP), IPV4Address.toBinaryString(p.Genmask))); + PoolNetwork = IPV4Address.toDecimalString(IPV4Address.IPandMask(IPV4Address.toBinaryString(p.IP), IPV4Address.toBinaryString(p.Genmask))); + + if(Network.equals(PoolNetwork)){ + if(!leases.containsKey(IP)){ + l.IP = IP; + l.Genmask = p.Genmask; + l.Gateway = p.Gateway; + l.MAC = MAC; + return l; + } + }else{ + break; + } + } + + }catch(Exception e2){} + } + return l; } *************** *** 183,208 **** switch(p.msgType){ case 1: IP = findLease(p.chaddr); if(IP==""){ lease l = advertLease(p.chaddr); - l.xid = p.xid; - l.negotiation_started = (System.currentTimeMillis()/1000); - - leases.put(l.IP, l); - - DHCPPacket o = new DHCPPacket(); - - o.chaddr = p.chaddr; - o.msgType = 2; - o.DHCPServer = mParentStack.SL().get_socket(appSock).src_IP; - o.yiaddr = l.IP; - o.SubnetMask = l.Genmask; - o.Gateway = l.Gateway; ! try{ ! SendData(appSock, "255.255.255.255", 68, o.toBytes()); ! }catch(CommunicationException e){ ! throw new TransportLayerException(e.toString()); } } --- 239,285 ---- switch(p.msgType){ case 1: + { + LayerInfo protInfo = new LayerInfo(getClass().getName()); + protInfo.setObjectName(mParentStack.getParentNodeName()); + protInfo.setDataType("DHCP Server"); + protInfo.setLayer("Application "); + protInfo.setDescription("Recieved DHCPDISCOVER(xid=" + p.xid + ") from " + p.chaddr + " with op=1."); + Simulation.addLayerInfo(protInfo); + IP = findLease(p.chaddr); if(IP==""){ lease l = advertLease(p.chaddr); ! if(l.IP!=""){ ! l.xid = p.xid; ! l.negotiation_started = (System.currentTimeMillis()/1000); ! ! leases.put(l.IP, l); ! ! DHCPPacket o = new DHCPPacket(); ! ! o.chaddr = p.chaddr; ! o.msgType = 2; ! o.DHCPServer = mParentStack.SL().get_socket(appSock).src_IP; ! o.leaseTime = 3600; ! o.yiaddr = l.IP; ! o.op = 2; ! l.xid = o.xid = p.xid; ! o.SubnetMask = l.Genmask; ! o.Gateway = l.Gateway; ! ! protInfo = new LayerInfo(getClass().getName()); ! protInfo.setObjectName(mParentStack.getParentNodeName()); ! protInfo.setDataType("DHCP Server"); ! protInfo.setLayer("Application "); ! protInfo.setDescription("Sending DHCPOFFER(xid=" + o.xid + ") packet to 255.255.255.255 with op=1 chaddr='" + ! o.chaddr + "' DHCP Server='" + o.DHCPServer + "' yiaddr='" + o.yiaddr + "'."); ! ! try{ ! SendData(appSock, "255.255.255.255", 68, o.toBytes()); ! }catch(CommunicationException e){ ! throw new TransportLayerException(e.toString()); ! } } } *************** *** 213,218 **** --- 290,347 ---- } */ + } break; case 3: + IP = findLease(p.chaddr); + + LayerInfo protInfo = new LayerInfo(getClass().getName()); + protInfo.setObjectName(mParentStack.getParentNodeName()); + protInfo.setDataType("DHCP Server"); + protInfo.setLayer("Application "); + protInfo.setDescription("Recieved DHCPREQUEST(xid=" + p.xid + ") from " + p.chaddr + " with op=1 and preferred IP='" + p.WantedIP + "'."); + Simulation.addLayerInfo(protInfo); + + if(IP!=""){ + lease l = (lease)leases.get(IP); + + if(l.xid == p.xid){ + l.leased = (System.currentTimeMillis()/1000); + l.negotiation_started = 0; + l.completed = true; + + DHCPPacket o = new DHCPPacket(); + + o.chaddr = p.chaddr; + o.msgType = 5; + o.DHCPServer = mParentStack.SL().get_socket(appSock).src_IP; + o.leaseTime = 3600; + o.yiaddr = l.IP; + o.xid = l.xid; + o.op = 2; + o.SubnetMask = l.Genmask; + o.Gateway = l.Gateway; + o.WantedIP = ""; + + protInfo = new LayerInfo(getClass().getName()); + protInfo.setObjectName(mParentStack.getParentNodeName()); + protInfo.setDataType("DHCP Server"); + protInfo.setLayer("Application "); + protInfo.setDescription("Sending DHCPACK(xid=" + o.xid + ") packet to 255.255.255.255 with op=1 chaddr='" + + o.chaddr + "' DHCP Server='" + o.DHCPServer + "' yiaddr='" + o.yiaddr + "'..."); + + try{ + SendData(appSock, "255.255.255.255", 68, o.toBytes()); + }catch(CommunicationException e){ + throw new TransportLayerException(e.toString()); + } + }else{ + protInfo = new LayerInfo(getClass().getName()); + protInfo.setObjectName(mParentStack.getParentNodeName()); + protInfo.setDataType("DHCP Server"); + protInfo.setLayer("Application "); + protInfo.setDescription("Recieved DHCP packet with invalid xid=" + p.xid + "."); + Simulation.addLayerInfo(protInfo); + } + } /*if(!findLease()){ *************** *** 223,278 **** }else{ // drop it; - } - - /* recieved++; - //processing the protocol doings. - if(appType == 0){ - //client processing recieve - // printing some ... - LayerInfo protInfo = new LayerInfo(getClass().getName()); - protInfo.setObjectName(mParentStack.getParentNodeName()); - protInfo.setDataType("DHCPD Protocol Data"); - protInfo.setLayer("Application "); - protInfo.setDescription("Recieving DHCPD message '" + Data + "' from server."); - Simulation.addLayerInfo(protInfo); - - //mParentStack.UDP().closePort(sock); - - }else{ - //server processing recieve - try{ - String sdHost = mParentStack.SL().get_socket(sock).dst_IP; - int sdPort = mParentStack.SL().get_socket(sock).dst_port; - LayerInfo protInfo = new LayerInfo(getClass().getName()); protInfo.setObjectName(mParentStack.getParentNodeName()); ! protInfo.setDataType("DHCPD Protocol Data"); protInfo.setLayer("Application "); ! protInfo.setDescription("Recieving DHCPD message '" + Data + "' from client " + sdHost + ":" + sdPort + ". Total recieved messages by server: " + recieved); Simulation.addLayerInfo(protInfo); ! ! LayerInfo protInfo2 = new LayerInfo(getClass().getName()); ! protInfo2.setObjectName(mParentStack.getParentNodeName()); ! protInfo2.setDataType("DHCPD Protocol Data"); ! protInfo2.setLayer("Application "); ! protInfo2.setDescription("Sending DHCPD message '" + Data + "' to client."); ! Simulation.addLayerInfo(protInfo2); ! ! SendData(Data); ! ! /*LayerInfo protInfo3 = new LayerInfo(getClass().getName()); ! protInfo3.setObjectName(mParentStack.getParentNodeName()); ! protInfo3.setDataType("DHCPD Protocol Data"); ! protInfo3.setLayer("Application "); ! protInfo3.setDescription("Server closing connection. Now listening on " + listenPort + "."); ! Simulation.addLayerInfo(protInfo3);*/ ! ! //Close(); ! //Listen(); ! /* }catch(Exception e){ ! System.out.println(e.toString()); ! ///*TODO*: here to catch ! } ! }*/ } --- 352,362 ---- }else{ // drop it; LayerInfo protInfo = new LayerInfo(getClass().getName()); protInfo.setObjectName(mParentStack.getParentNodeName()); ! protInfo.setDataType("DHCP Server"); protInfo.setLayer("Application "); ! protInfo.setDescription("Recieved DHCP packet with invalid data."); Simulation.addLayerInfo(protInfo); ! } } |