Question DHCP SetClientInfo Lease Expiration

AgentSmithers

Member
Joined
Apr 15, 2008
Messages
5
Programming Experience
3-5
Hi Everyone,
Anyone know what the proper API is to set a DHCP Client to Reserved lease.
When I run this code It sets it to reserved Inactive but when a release and renew from the actual client takes place it goes back to a normal lease that is set by default on the server.
I'm guessing maybe there is something wrong with my structure?
I also set the Type to DHCP which is not sticking as well when I call DhcpSetClientInfoV4. The lease time does show "Reserved (inactive)" when the Api is called and it does return Error_Success.
Any input?
Let me know if you need more code.
Thanks,
-Agent


VB.NET:
Public Sub ListSubnetClients(ByVal ServerIP As String, ByVal SubnetIP As String)

        Dim Client_Array As DHCP_CLIENT_INFO_ARRAY
        Dim DHCP_Clients() As DHCP_CLIENT_INFO

        Dim i, j As Int16

        Dim pt As IntPtr

        Dim Read_Clients, Total_Clients As Int32
        Dim Error_Code As Int32
        Dim Rem_Handle As IntPtr
        Dim Scope_I As UInt32

        'Scope_I = "10.0.3.0"
        Scope_I = StringIPAddressToUInt32(SubnetIP) ' Dot2LongIP(SubnetIP)

        'Call dhcpsapi
        Error_Code = DhcpEnumSubnetClients(ServerIP, Scope_I, Rem_Handle, 65537, pt, Read_Clients, Total_Clients)

        Client_Array = Marshal.PtrToStructure(pt, GetType(DHCP_CLIENT_INFO_ARRAY))

        ReDim DHCP_Clients(Client_Array.NumElements - 1)

        Dim MacAddr As Net.NetworkInformation.PhysicalAddress
        For i = 0 To Client_Array.NumElements - 1
            pt = Marshal.ReadIntPtr(Client_Array.Clients, j)
            DHCP_Clients(i) = Marshal.PtrToStructure(pt, GetType(DHCP_CLIENT_INFO))

            Debug.WriteLine("ClientComment: " & DHCP_Clients(i).ClientComment)
            Debug.WriteLine("ClientIpAddress: " & IPConvert(DHCP_Clients(i).ClientIpAddress))

            Dim HardwareAddress As String = String.Format("{0:x2}-{1:x2}-{2:x2}-{3:x2}-{4:x2}-{5:x2}", Marshal.ReadByte(DHCP_Clients(i).ClientHardwareAddress.Data), Marshal.ReadByte(DHCP_Clients(i).ClientHardwareAddress.Data, 1), Marshal.ReadByte(DHCP_Clients(i).ClientHardwareAddress.Data, 2), Marshal.ReadByte(DHCP_Clients(i).ClientHardwareAddress.Data, 3), Marshal.ReadByte(DHCP_Clients(i).ClientHardwareAddress.Data, 4), Marshal.ReadByte(DHCP_Clients(i).ClientHardwareAddress.Data, 5))

            Debug.WriteLine("ClientHardwareAddress: " & HardwareAddress)
            Debug.WriteLine("ClientLeaseExpires: " & DHCP_Clients(i).ClientLeaseExpires.ConvertIntToDateTime)
            Debug.WriteLine("ClientName: " & DHCP_Clients(i).ClientName)
            Debug.WriteLine("OwnerHoost->HostName: " & DHCP_Clients(i).OwnerHost.HostName)
            Debug.WriteLine("OwnerHost->IpAddress: " & IPConvert(DHCP_Clients(i).OwnerHost.IpAddress))
            Debug.WriteLine("OwnerHost->NetBiosName: " & DHCP_Clients(i).OwnerHost.NetBiosName)
            Debug.WriteLine("SubnetMask: " & UInt32IPAddressToString(DHCP_Clients(i).SubnetMask))

            If IPConvert(DHCP_Clients(i).ClientIpAddress).ToString.Trim = "10.246.36.101" Then
                Dim DHCPv4 As New DHCP_CLIENT_INFOV4
                DHCPv4.bClientType = DHCP_CLIENT_TYPE.CLIENT_TYPE_BOTH
                DHCPv4.ClientComment = DHCP_Clients(i).ClientComment
                DHCPv4.ClientHardwareAddress = DHCP_Clients(i).ClientHardwareAddress
                DHCPv4.ClientIpAddress = DHCP_Clients(i).ClientIpAddress
                DHCPv4.ClientLeaseExpires = DHCP_Clients(i).ClientLeaseExpires
                DHCPv4.ClientName = DHCP_Clients(i).ClientName
                DHCPv4.OwnerHost = DHCP_Clients(i).OwnerHost
                DHCPv4.SubnetMask = DHCP_Clients(i).SubnetMask

                Dim MyPtrr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(New DHCP_CLIENT_INFOV4))
                'DHCP_Clients(i).ClientLeaseExpires.SetToReserved()
                DHCPv4.ClientLeaseExpires.SetToInfinte()
                Marshal.StructureToPtr(DHCPv4, MyPtrr, False)
                'Debug.WriteLine("Lease Updated: " & DhcpSetClientInfo(ServerIP, MyPtrr))
                Debug.WriteLine("Lease Updated: " & DhcpSetClientInfoV4(ServerIP, MyPtrr))
                End
            End If

            Debug.WriteLine(vbCrLf & vbCrLf)

            pt = IntPtr.Zero
            j = j + 4

            HowManyClients += 1
        Next i

    End Sub
 

Attachments

  • 2013-09-13_1133.png
    2013-09-13_1133.png
    4.4 KB · Views: 19
Back
Top