locked
Using DhcpPktSendHook in DHCP Callout API RRS feed

  • Question

  • Hi,

     

    I am currently trying to implement a DHCP plugin that dynamically adds to the static routes depending on results from an external application. I have a DHCP plugin working on a Server 2003 machine but I am stuggling to see how I am meant to modify the DHCP packet to add to the DHCP response.

     

    I have identified that DhcpPktSendHook seems to be the best fit. The issue I have is how to handle memory allocation. I assume that if I am extending the packet that I need to create a new one from scratch, copy the data from the existing one and return the new packet and size in the OUT variables in DhcpPktSendHook.

     

    My questions are:

    1) How should I allocate the memory for the new packet? Do I use new or malloc or something else?

    2) How should I de-allocate the memory for the original packet?

     

    Regards
    Marc

    Tuesday, August 7, 2007 10:11 AM

Answers

  • Hi,

     

    I,ve had the same problem... No information about the memory allocation of the service DHCP !!! so I edit (in hexa)tcpsvcs.exe to look which api it use for memory allocation... I found it use API HeapAlloc / HeapReAlloc / HeapFree

     

       BYTE* pbNewPacket;
       DWORD dwNewPacketSize=packetDHCP.GetPacketSize();
       pbNewPacket=(char*)HeapReAlloc(GetProcessHeap(),0,*Packet,dwNewPacketSize);
       if(!pbNewPacket)
        throw Exception("HeapReAlloc(GetProcessHeap(),0,*Packet,dwTmpPacketSize)==NULL");
       *Packet=pbNewPacket;
       *PacketSize=dwNewPacketSize;
       memcpy(*Packet,packetDHCP.GetPacket(),dwNewPacketSize);

     

    Finally, I suppose it is the right way to modify the allocation of the Packet :-) (5 hours whith a dhcp renew from a client no memory increase in the sevice dhcp...)

     

    bye,bye

    Fabrice

    Tuesday, August 21, 2007 3:38 PM

All replies

  • Can nobody help with this issue? Does nobody use this API? Does anyone at MS read this that could help?

     

    Marc

    Friday, August 10, 2007 9:04 AM
  • Hi,

     

    I,ve had the same problem... No information about the memory allocation of the service DHCP !!! so I edit (in hexa)tcpsvcs.exe to look which api it use for memory allocation... I found it use API HeapAlloc / HeapReAlloc / HeapFree

     

       BYTE* pbNewPacket;
       DWORD dwNewPacketSize=packetDHCP.GetPacketSize();
       pbNewPacket=(char*)HeapReAlloc(GetProcessHeap(),0,*Packet,dwNewPacketSize);
       if(!pbNewPacket)
        throw Exception("HeapReAlloc(GetProcessHeap(),0,*Packet,dwTmpPacketSize)==NULL");
       *Packet=pbNewPacket;
       *PacketSize=dwNewPacketSize;
       memcpy(*Packet,packetDHCP.GetPacket(),dwNewPacketSize);

     

    Finally, I suppose it is the right way to modify the allocation of the Packet :-) (5 hours whith a dhcp renew from a client no memory increase in the sevice dhcp...)

     

    bye,bye

    Fabrice

    Tuesday, August 21, 2007 3:38 PM
  • Thank you very much for that information. It's a shame that MS couldn't just document it, as it is quite important!

     

    Marc

    Wednesday, August 22, 2007 2:41 PM
  • You can't safely memcpy into the heap returned by HeapRealloc.  The above code sample is fallable.

    Friday, September 14, 2007 5:19 PM
  • To allocate and reallocate, you can use the code below

    LPBYTE pbNewPacket;
    pbNewPacket=(LPBYTE)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, PacketSize);
    pbNewPacket=(LPBYTE)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, Packet,PacketSize);

    //here you may want to modify your packet

    HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, Packet);
    Packet=(LPBYTE)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, pbNewPacket,PacketSize);

    However, if you want to modify the packet and you know the position, you can also do:

    packet[position] = 0xcf;
    ...

    Another approach is by mapping the dhcp message format and then modify base on your needs
    http://www.tarunz.org/~vassilii/TAU/protocols/dhcp/frame.htm

    Use wireshark to identify each of those options

    gizeh
    Thursday, December 11, 2008 5:16 PM