Why does this happen?
u_char macDst[6], macSrc[6], ipType[2]={0x88, 0x92};
u_char header[] = {0x04,0x00,0x20,0x00,...,};
const int len = sizeof(macDst) + sizeof(macSrc) + sizeof(header) + 2;
u_char* pck[len];
int j = 0, length = sizeof(macDst);
for (j; j < length; j++)
{
pck[j] = &macDst[j];
}
length += sizeof(macSrc);
for (j; j < length; j++)
{
pck[j] = &macSrc[j - sizeof(macDst)];
}
pck[j++] = &ipType[0];
pck[j++] = &ipType[1];
length += sizeof(header) + 2;
for (j; j < len; j++)
{
pck[j] = &header[j - sizeof(macDst) - sizeof(macSrc)];
}
u_char testSend[170];
memcpy(testSend, *pck, sizeof(pck));
The var testSend only has macDst values (before initialized), while in the pointer I have all of them. If I copy them one by one, I get a good result:
for (int k = 0; k < 170; k++)
{
testSend[k] = *pck[k];
}
Any idea??
Thanks!!
Edit:
This is a physic problem:
-----ooooooooo------------aaaa----bbbb----
- Null Data
When I make the pointers array I am doing this:
oooooooooaaaabbbb
But when I sent the packet and when I did the memcpy (or memmove) I was saying this: take this pointer and this amount of memory and copy them:
----|oooooooooo----------|----aaaaa----bbbb----
being the amount the sum of the 3 memory places.
I find the code convoluted, and I would never write it this way. Still, the error here:
memcpy(testSend, *pck, sizeof(pck));
You are dereferencing pck
- and point to the first element, which is a char*
. This is what you copy using your memcpy. On the other hand, your hand-written loop does dereference the pointer before copying it.
Fill the testSend
array directly:
u_char *p = testSend;
memcpy(p, macDst, sizeof(macDst)); p += sizeof(macDst);
memcpy(p, macSrc, sizeof(macSrc)); p += sizeof(macSrc);
memcpy(p, ipType, sizeof(ipType); p += sizeof(ipType);
memcpy(p, header, sizeof(header));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.