All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.dhcp.DiscoveryMessage Maven / Gradle / Ivy

/*
 * Copyright 2016 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.dhcp;

import br.com.objectos.net.HardwareAddress;
import br.com.objectos.net.IpAddress;
import br.com.objectos.net.NetShort;
import br.com.objectos.net.NetworkAdapter;

/**
 * @author [email protected] (Marcio Endo)
 */
public class DiscoveryMessage extends ClientMessage {

  final TransactionId xid;
  final NetShort flags;
  final HardwareAddress chaddr;
  final Options options;

  private DiscoveryMessage(TransactionId xid, NetShort flags, HardwareAddress chaddr, Options options) {
    this.xid = xid;
    this.flags = flags;
    this.chaddr = chaddr;
    this.options = options;
  }

  static DiscoveryMessage of(NetworkAdapter adapter) {
    return new DiscoveryMessage(
        TransactionId.next(),
        NetShort.zero(),
        adapter.hardwareAddress(),
        Options.builder()
            .put(Option053MessageType.instance(), MessageType.DHCPDISCOVER)
            .put(Option050RequestedIpAddress.instance(), IpAddress.zero())
            .put(Option055ParamRequestList.instance(), ParamRequestList.standard())
            .build());
  }

  static boolean matches(DhcpDatagram datagram) {
    Options options = datagram.get(DhcpDatagram.OPTIONS);
    MessageType messageType = options.get(Option053MessageType.instance());
    return messageType != null && messageType.equals(MessageType.DHCPDISCOVER);
  }

  static DiscoveryMessage read(DhcpDatagram datagram) {
    return new DiscoveryMessage(
        datagram.get(DhcpDatagram.XID),
        datagram.get(DhcpDatagram.FLAGS),
        datagram.get(DhcpDatagram.CHADDR),
        datagram.get(DhcpDatagram.OPTIONS));
  }

  @Override
  boolean matches(Server server) {
    return true;
  }

  @Override
  OfferMessage nextMessage(Server server) {
    server.onDiscovery(this);
    ConfiguredAdapter adapter = server.configuredAdapter(chaddr);
    ParamRequestList requestList = option(Option055ParamRequestList.instance(), ParamRequestList.standard());
    return requestList.toOfferMessage(xid, flags, adapter);
  }

  @Override
  void onMessageSent(Client client) {
    client.onDiscoverySent(this);
  }

  @Override
  TransactionId xid() {
    return xid;
  }

  @Override
  Options options() {
    return options;
  }

  @Override
  DhcpDatagram toDatagram() {
    return DhcpDatagram.builder()
        .put(DhcpDatagram.OP, OpCode.BOOTREQUEST)
        .put(DhcpDatagram.HTYPE, HardwareType.ETHERNET)
        .put(DhcpDatagram.XID, xid)
        .put(DhcpDatagram.CHADDR, chaddr)
        .put(DhcpDatagram.OPTIONS, options)
        .build();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy