views:

20

answers:

1

I am developing an application in which I want to pull out some information through the DHCP server. so I am sending a DHCPINFORM packet on port 67 UDP Broadcast. The problem I am facing is that I am not getting response i.e. DHCPACK or anyother message from DHCP server all the times, means some times it send back an DHCPACK packet at once and some time not at all, what might be cause of this issue?

Here is my network configuration My IP: 192.168.3.31 DHCP Server is at: 192.168.3.108

and C# code I am using is as

private void SendDHCPINFORM()
    {
        UdpClient udpClient = new UdpClient();
        IPEndPoint dhcpClientEndpoint = new IPEndPoint(IPAddress.Broadcast, 67);

        #region Making Data DHCPINFORM
        D_op = 1;
        D_htype = 1;
        D_hlen = 6;
        D_hops = 0;
        D_xid = new byte[4];
        Random objRandom = new Random(8000);
        objRandom.NextBytes(D_xid);

        D_secs = new byte[2];
        D_secs = BitConverter.GetBytes(Convert.ToInt16(0));

        D_flags = new byte[2];
        D_flags = BitConverter.GetBytes(Convert.ToInt16(0));

        D_ciaddr = new byte[4];
        D_ciaddr[0] = 192;
        D_ciaddr[1] = 168;
        D_ciaddr[2] = 3;
        D_ciaddr[3] = 31;

        D_yiaddr = new byte[4];
        D_yiaddr = BitConverter.GetBytes(0);

        D_siaddr = new byte[4];
        D_siaddr = BitConverter.GetBytes(0);

        D_giaddr = new byte[4];
        D_giaddr = BitConverter.GetBytes(0);

        //00-13-D3-D5-27-73
        D_chaddr = new byte[16];
        D_chaddr[0] = 0;
        D_chaddr[1] = 19;
        D_chaddr[2] = 211;
        D_chaddr[3] = 213;
        D_chaddr[4] = 39;
        D_chaddr[5] = 115;

        D_sname = new byte[64];


        D_file = new byte[128];

        M_Cookie = new byte[4];
        M_Cookie[0] = 99;
        M_Cookie[1] = 130;
        M_Cookie[2] = 83;
        M_Cookie[3] = 99;


        D_options = new byte[60];

        //Making DHCPINFORM message
        byte[] messageType = new byte[3];
        messageType[0] = 53;
        messageType[1] = 1;
        messageType[2] = 8; //Message Type from 1-8
        Array.Copy(messageType, D_options, messageType.Length);

        int options_offset = 3;

        byte[] serverIdentifier = new byte[6];
        serverIdentifier[0] = 54;
        serverIdentifier[1] = 4;
        serverIdentifier[2] = 192;
        serverIdentifier[3] = 168;
        serverIdentifier[4] = 3;
        serverIdentifier[5] = 108;
        Array.Copy(serverIdentifier, 0, D_options, options_offset, serverIdentifier.Length);

        options_offset += serverIdentifier.Length;
        byte[] dummyPacket = new byte[3];
        dummyPacket[0] = 116;
        dummyPacket[1] = 1;
        dummyPacket[2] = 1;
        Array.Copy(dummyPacket, 0, D_options, options_offset, dummyPacket.Length);

        options_offset += dummyPacket.Length;
        byte[] clientIdentifier = new byte[9];
        clientIdentifier[0] = 61;
        clientIdentifier[1] = 7;
        clientIdentifier[2] = 1;
        Array.Copy(D_chaddr, 0, clientIdentifier, 3, 6);
        Array.Copy(clientIdentifier, 0, D_options, options_offset, clientIdentifier.Length);

        options_offset += clientIdentifier.Length;
        byte[] hostName = new byte[7];
        hostName[0] = 12;
        hostName[1] = 5;
        Array.Copy(Encoding.ASCII.GetBytes("host1"), 0, hostName, 2, Encoding.ASCII.GetBytes("host1").Length);
        Array.Copy(hostName, 0, D_options, options_offset, hostName.Length);

        options_offset += hostName.Length;
        byte[] vendorClassID = new byte[10];
        vendorClassID[0] = 60;
        vendorClassID[1] = 8;
        //Array.Copy(Encoding.ASCII.GetBytes("Unspecified"), 0, vendorClassID, 2, Encoding.ASCII.GetBytes("Unspecified").Length);
        Array.Copy(Encoding.ASCII.GetBytes("MSFT 5.0"), 0, vendorClassID, 2, Encoding.ASCII.GetBytes("MSFT 5.0").Length);
        Array.Copy(vendorClassID, 0, D_options, options_offset, vendorClassID.Length);

        options_offset += vendorClassID.Length;
        byte[] paramRequestList = new byte[13];
        paramRequestList[0] = 55;
        paramRequestList[1] = 11;
        paramRequestList[2] = 1;
        paramRequestList[3] = 15;
        paramRequestList[4] = 3;
        paramRequestList[5] = 6;
        paramRequestList[6] = 44;
        paramRequestList[7] = 46;
        paramRequestList[8] = 47;
        paramRequestList[9] = 31;
        paramRequestList[10] = 33;
        paramRequestList[11] = 249;
        paramRequestList[12] = 43;
        //Array.Copy(paramRequestList, 0, D_options, 31, paramRequestList.Length);
        Array.Copy(paramRequestList, 0, D_options, options_offset, paramRequestList.Length);

        options_offset += paramRequestList.Length;
        byte[] vendorSpecificInfo = new byte[5];
        vendorSpecificInfo[0] = 43;
        vendorSpecificInfo[1] = 2;
        vendorSpecificInfo[2] = 220;
        vendorSpecificInfo[3] = 0;
        vendorSpecificInfo[4] = 255;
        Array.Copy(vendorSpecificInfo, 0, D_options, options_offset, vendorSpecificInfo.Length);

        byte[] dhcpMessage = new byte[300];
        dhcpMessage[0] = D_op;
        dhcpMessage[1] = D_htype;
        dhcpMessage[2] = D_hlen;
        dhcpMessage[3] = D_hops;

        int destinationIndex = 4;
        Array.Copy(D_xid, 0, dhcpMessage, destinationIndex, D_xid.Length);

        destinationIndex = destinationIndex + D_xid.Length;
        Array.Copy(D_secs, 0, dhcpMessage, destinationIndex, D_secs.Length);

        destinationIndex = destinationIndex + D_secs.Length;
        Array.Copy(D_flags, 0, dhcpMessage, destinationIndex, D_flags.Length);

        destinationIndex = destinationIndex + D_flags.Length;
        Array.Copy(D_ciaddr, 0, dhcpMessage, destinationIndex, D_ciaddr.Length);

        destinationIndex = destinationIndex + D_ciaddr.Length;
        Array.Copy(D_yiaddr, 0, dhcpMessage, destinationIndex, D_yiaddr.Length);

        destinationIndex = destinationIndex + D_yiaddr.Length;
        Array.Copy(D_siaddr, 0, dhcpMessage, destinationIndex, D_siaddr.Length);

        destinationIndex = destinationIndex + D_siaddr.Length;
        Array.Copy(D_giaddr, 0, dhcpMessage, destinationIndex, D_giaddr.Length);

        destinationIndex = destinationIndex + D_giaddr.Length;
        Array.Copy(D_chaddr, 0, dhcpMessage, destinationIndex, D_chaddr.Length);

        destinationIndex = destinationIndex + D_chaddr.Length;
        Array.Copy(D_sname, 0, dhcpMessage, destinationIndex, D_sname.Length);

        destinationIndex = destinationIndex + D_sname.Length;
        Array.Copy(D_file, 0, dhcpMessage, destinationIndex, D_file.Length);

        destinationIndex = destinationIndex + D_file.Length;
        Array.Copy(M_Cookie, 0, dhcpMessage, destinationIndex, M_Cookie.Length);

        destinationIndex = destinationIndex + M_Cookie.Length;
        Array.Copy(D_options, 0, dhcpMessage, destinationIndex, D_options.Length);
        #endregion

        udpClient.Send(dhcpMessage, 300, dhcpClientEndpoint);

        UdpClient udpServerResponse = new UdpClient(68);
        IPEndPoint dhcpServerEndPoint = new IPEndPoint(IPAddress.Any, 0);

        //The following line is receiving the response from DHCP server
        //works some time immediately and some time not even after couple 
        //of minutes and program goes to halt state? Am I making some mistake?
        byte[] dataReceived = udpServerResponse.Receive(ref dhcpServerEndPoint);

        Console.WriteLine("Message Received");
    }

Please help me out?

+1  A: 

I don't know really what is the problem but won't it be easier to use the API instead,

DHCP Client API http://msdn.microsoft.com/en-us/library/aa363344(v=VS.85).aspx

DHCP Server Callout API http://msdn.microsoft.com/en-us/library/aa363372(v=VS.85).aspx

A_Nablsi
Thanks A_Nablsi, can you please send me some code or workign example for DHCPINFORM packet using above library? as no code is available on the above link.
Ummar
Tell me what are you trying to do so I can help you, It's difficult for me to understand the code you wrote.
A_Nablsi
I want to get the Vendor Specific information in the Options field of DHCP packet. its code is 43. I have tried with the above API you mentioned, I have used OPTION_VENDOR_SPEC_INFO as optionID, but the fuction didn't return any thing in buffer, however it is not returning any error. but when I check it with OPTION_HOST_NAME optionId it returns the correct hostname. what might be the problem?
Ummar