2010-10-27 24 views
0

我正在開發一個應用程序,我想通過DHCP服務器提取一些信息。所以我在端口67 UDP Broadcast上發送一個DHCPINFORM數據包。我面臨的問題是,我沒有得到任何迴應,例如DHCPACK或來自DHCP服務器的任何其他消息,這意味着它有時會一次發送一個DHCPACK數據包,有時甚至完全沒有,這可能是什麼原因導致的?從C#獲取DHCPINFORM消息響應中的問題#

這裏是我的網絡配置 我的IP:192.168.3.31 DHCP服務器爲:192.168.3.108

,我使用C#代碼是

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

請幫助我嗎?

回答

1

我不知道是不是真的有什麼問題,但不會是更容易使用,而不是API,

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

DHCP服務器編號API http://msdn.microsoft.com/en-us/library/aa363372(v=VS.85).aspx

+0

感謝A_Nablsi,可以請你給我使用上述庫DHCPINFORM包中一些代碼或workign例子嗎?因爲上述鏈接上沒有代碼可用。 – 2010-10-27 11:23:23

+0

告訴我你在做什麼,我可以幫助你,我很難理解你寫的代碼。 – 2010-10-27 11:38:45

+0

我想要在DHCP數據包的選項字段中獲得供應商特定信息。它的代碼是43.我已經嘗試使用上面提到的API,我已經使用OPTION_VENDOR_SPEC_INFO作爲optionID,但是函數沒有在緩衝區中返回任何東西,但它沒有返回任何錯誤。但是當我用OPTION_HOST_NAME optionId檢查它時,它會返回正確的主機名。可能是什麼問題? – 2010-10-28 07:29:37

0

這個問題對我來說看起來像是一個簡單的操作順序:因爲直到發送DHCPINFORM之後纔開始接收UdpClient,可能服務器對您而言速度太快,而DHCPACK com在端口68被綁定之前,es被放入(並被丟棄)。

你應該能夠做到整個事務(發送和接收)使用單一UdpClient綁定端口68