从 C# 获取 DHCPINFORM 消息响应时出现问题

发布于 2024-09-28 22:16:27 字数 7294 浏览 11 评论 0原文

我正在开发一个应用程序,我想在其中通过 DHCP 服务器提取一些信息。所以我在端口 67 UDP 广播上发送 DHCPINFORM 数据包。我面临的问题是我始终没有收到来自 DHCP 服务器的响应,即 DHCPACK 或任何其他消息,这意味着有时它会立即发回 DHCPACK 数据包,有时根本不会发回,这可能是什么原因造成的?

这是我的网络配置 我的IP:192.168.3.31 DHCP 服务器位于:192.168.3.108

,我使用的 C# 代码是 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");
    }

请帮帮我?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恏ㄋ傷疤忘ㄋ疼 2024-10-05 22:16:27

不是更容易吗

我真的不知道问题是什么,但使用API DHCP 客户端 API
http://msdn.microsoft.com/en- us/library/aa363344(v=VS.85).aspx

DHCP 服务器调出 API
http://msdn.microsoft.com/en-我们/库/aa363372(v=VS.85).aspx

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

初见终念 2024-10-05 22:16:27

对我来说,这个问题看起来像是一个简单的操作顺序问题:由于您直到发送 DHCPINFORM 后才开始接收 UdpClient,因此服务器可能太快对于您来说,DHCPACK 在端口 68 绑定之前进入(并被丢弃)。

您应该能够使用单个 UdpClient 绑定端口 68 来完成整个事务(发送和接收)。

The issue looks like a simple order of operations problem to me: since you don't start the receiving UdpClient until after sending the DHCPINFORM, it's possible that the server is too fast for you and the DHCPACK comes in (and is discarded) before port 68 is bound.

You should be able to do the entire transaction (send and receive) using a single UdpClient binding port 68.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文