Beispiel #1
0
  /**
   * This method processes a DHCPC sending by client
   *
   * @param data to send
   * @param server host
   * @param server port
   * @author key
   * @version v0.01
   */
  public void StartDHCPC(String inIface, String WantedIP)
      throws CommunicationException, LowLinkException, InvalidNetworkLayerDeviceException,
          TransportLayerException {
    if (running) {
      Close();
    }

    running = true;
    completed = false;
    iface = inIface;

    Listen();

    DHCPPacket d = new DHCPPacket();
    d.op = 1;
    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();

    resend = Data;
    resendings = 0;

    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);

    mParentStack.getParentNode().startTimerTask(this, 5000);

    SendData(appSock, "255.255.255.255", 67, Data);
  }
Beispiel #2
0
 @Override
 public void Timer(int code) {
   if (running) {
     if (!completed) {
       if (resend != "" && resendings++ < 5) {
         LayerInfo protInfo = new LayerInfo(getClass().getName());
         protInfo.setObjectName(mParentStack.getParentNodeName());
         protInfo.setDataType("DHCP client");
         protInfo.setLayer("Application ");
         protInfo.setDescription("Resening DHCP packet to 255.255.255.255 due to timer.");
         Simulation.addLayerInfo(protInfo);
         try {
           SendData(appSock, "255.255.255.255", 67, resend);
         } catch (Exception e) {
         }
       }
     } else {
       // re-lease
     }
   }
 }
Beispiel #3
0
  /**
   * 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);
    }
  }