unsigned chars
-
Hi guys, I am trying to copy into a struct the contents of a buffer returned from the winpcap library, the libray function call returns a pointer to the buffer like the following type:
const UCHAR *pkt_data
I am trying to declare something to hold the data where // Packet data ??? is.typedef struct DATAGRAM { UINT packetID; // Sequence Number ip_header ipHeader; // IP header pcap_pkthdr wpHeader; // Winpcap header unsigned char pkt_data[]; // Packet data ??? } DATAGRAM;
I would prefer to keep it as UCHARS because it makes it easier to parse the data later on into headers such as tcp, so my question is how should declare the type in the struct and how can i copy the data in from the buffer?? Cheers Packetlos
-
Hi guys, I am trying to copy into a struct the contents of a buffer returned from the winpcap library, the libray function call returns a pointer to the buffer like the following type:
const UCHAR *pkt_data
I am trying to declare something to hold the data where // Packet data ??? is.typedef struct DATAGRAM { UINT packetID; // Sequence Number ip_header ipHeader; // IP header pcap_pkthdr wpHeader; // Winpcap header unsigned char pkt_data[]; // Packet data ??? } DATAGRAM;
I would prefer to keep it as UCHARS because it makes it easier to parse the data later on into headers such as tcp, so my question is how should declare the type in the struct and how can i copy the data in from the buffer?? Cheers Packetlos
This "unsigned char pkt_data[];" should not pass the compiler. Insted the stucture should look something like this:
typedef struct DATAGRAM{
UINT packetID; // Sequence Number
ip_header ipHeader; // IP header
pcap_pkthdr wpHeader; // Winpcap header
unsigned char pkt_data[1]; // Packet data ???
} DATAGRAM;Then if you know if you know the nuber of bytes you will be receiving you can allocate the need memory like so: DATAGRAM* pDatat = (DATAGRAM*)malloc(sizeof(DATAGRAM) + nDataSize) or the C++ equivalent to allocate the need memory and then copy the data into pData->pkt_data as you see fit. If you do not know the data size ahead of time things get a lot more complicated and you may want to use one of the STL templates or if programming using MFC you could replace "unsigned char pkt_data[1];" with "CByteArray pkt_data;". CByteArray stores unsigned chars, because that is what a byte is. Good Luck1 INTP
-
This "unsigned char pkt_data[];" should not pass the compiler. Insted the stucture should look something like this:
typedef struct DATAGRAM{
UINT packetID; // Sequence Number
ip_header ipHeader; // IP header
pcap_pkthdr wpHeader; // Winpcap header
unsigned char pkt_data[1]; // Packet data ???
} DATAGRAM;Then if you know if you know the nuber of bytes you will be receiving you can allocate the need memory like so: DATAGRAM* pDatat = (DATAGRAM*)malloc(sizeof(DATAGRAM) + nDataSize) or the C++ equivalent to allocate the need memory and then copy the data into pData->pkt_data as you see fit. If you do not know the data size ahead of time things get a lot more complicated and you may want to use one of the STL templates or if programming using MFC you could replace "unsigned char pkt_data[1];" with "CByteArray pkt_data;". CByteArray stores unsigned chars, because that is what a byte is. Good Luck1 INTP
Hi John, Thanks for taking time to reply, CByteArray seems to be exactly what I need, I can calculate the size of the data and set the CByteArray like this:
tempDatagram.pkt_data.SetSize(dataLen);
But then how do I copy the data into the array using the given pointer to the buffer of UCHARS? -
Hi John, Thanks for taking time to reply, CByteArray seems to be exactly what I need, I can calculate the size of the data and set the CByteArray like this:
tempDatagram.pkt_data.SetSize(dataLen);
But then how do I copy the data into the array using the given pointer to the buffer of UCHARS?