schuetzp85 - 2012-03-27

Good Evening!

I try programming a software, which to use an DHCP Server, in Java!
So I found this nice API! ;-) Thank you very much!

Firstly I read out the DHCPDISCOVER paket to the console!
Secoundly I read this paket and make a DHCPOFFER Paket with the DHCPResponseFactory.makeDHCPOffer method!
Now I want to send this DHCPOFFER paket to the client!
How can I do this?

There is my Code:

package install;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import org.dhcp4java.DHCPConstants;
import org.dhcp4java.DHCPOption;
import org.dhcp4java.DHCPPacket;
import org.dhcp4java.DHCPResponseFactory;
public class mainClass {
    private mainClass() {
        throw new UnsupportedOperationException();
    }
    public static void main(String[] args) {
        try {
            DatagramSocket socket = new DatagramSocket(
                    DHCPConstants.BOOTP_REQUEST_PORT);
            while (true) {
                DatagramPacket pac = new DatagramPacket(new byte[1500], 1500);
                socket.receive(pac);
                DHCPPacket dhcp = DHCPPacket.getPacket(pac);
                System.out.println(dhcp.toString() + "\n");
                if (dhcp.getDHCPMessageType() == DHCPConstants.DHCPDISCOVER) {
                    byte IP[] = { (byte) 192, (byte) 168, (byte) 1, (byte) 30 };
                    InetAddress offeredAddress = InetAddress.getByAddress(IP);
                    byte IP2[] = { (byte) 192, (byte) 168, (byte) 1, (byte) 5 };
                    InetAddress serverIdentifier = InetAddress
                            .getByAddress(IP2);
                    DHCPOption[] options = { DHCPOption.newOptionAsByte(
                            DHCPConstants.DHO_DHCP_MESSAGE_TYPE,
                            DHCPConstants.DHCPOFFER) };
                    DHCPPacket dhcp2 = DHCPResponseFactory.makeDHCPOffer(dhcp,
                            offeredAddress, -1, serverIdentifier, "TEST",
                            options);
                    System.out.println(dhcp2.toString() + "\n");

                    // I want send my DHCPOFFER Paket here!
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I now the DHCP Options for the DHCPOFFER paket aren't completely! But this doesn't matter at the moment!

So and there is the console output:

DHCP Packet
comment=
address=0.0.0.0(68)
op=BOOTREQUEST(1)
htype=HTYPE_ETHER(1)
hlen=6
hops=0
xid=0x2A5B76AB
secs=4
flags=0xffff8000
ciaddr=0.0.0.0
yiaddr=0.0.0.0
siaddr=0.0.0.0
giaddr=0.0.0.0
chaddr=0x000C295B76AB
sname=
file=
Options follows:
DHO_DHCP_MESSAGE_TYPE(53)=DHCPDISCOVER
DHO_DHCP_PARAMETER_REQUEST_LIST(55)=1 2 3 5 6 11 12 13 15 16 17 18 43 54 60 67 128 129 130 131 132 133 134 135
DHO_DHCP_MAX_MESSAGE_SIZE(57)=1260
(97)=0x00564D6464B30AEC3484A7F288615B76AB
(93)=0x0000
(94)=0x010201
DHO_VENDOR_CLASS_IDENTIFIER(60)="PXEClient:Arch:00000:UNDI:002001"
padding=0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

DHCP Packet
comment=
address=255.255.255.255(68)
op=BOOTREPLY(2)
htype=HTYPE_ETHER(1)
hlen=6
hops=0
xid=0x2A5B76AB
secs=0
flags=0xffff8000
ciaddr=0.0.0.0
yiaddr=192.168.1.30
siaddr=0.0.0.0
giaddr=0.0.0.0
chaddr=0x000C295B76AB
sname=
file=
Options follows:
DHO_DHCP_MESSAGE_TYPE(53)=DHCPOFFER
DHO_DHCP_LEASE_TIME(51)=-1
DHO_DHCP_SERVER_IDENTIFIER(54)=192.168.1.5
DHO_DHCP_MESSAGE(56)="TEST"
padding=

Thank you for your help!