/** * This method recieves data from the other side. * * @param data to recv * @author key * @version v0.01 */ @Override public void RecvData(int sock, String Data) throws LowLinkException, TransportLayerException { DHCPPacket p = new DHCPPacket(); if (!running) return; 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; r.xid = xid; r.msgType = 3; 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); resend = r.toBytes(); try { SendData(appSock, "255.255.255.255", 67, resend); } catch (CommunicationException e) { throw new TransportLayerException(e.toString()); } } break; case 4: // DECLINE // sleeping break; case 5: // ACK 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); resend = ""; resendings = 0; try { mParentStack.setIPAddress(iface, p.yiaddr); mParentStack.setCustomSubnetMask(iface, p.SubnetMask); mParentStack.setDefaultGateway(p.Gateway); } catch (Exception e) { } leaseTime = p.leaseTime; DHCPServer = p.DHCPServer; completed = true; } break; } } 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); } }