[Dibbler] dibbler-server an debian-armel

Christian Andersen lists at milindur.de
Tue Aug 19 02:10:33 CEST 2008


Hallo,

> Well, my proposal was quicker:
>
>     unsigned short code   = buf[pos]*256 + buf[pos+1];
>     pos+=2;
>     unsigned short length = buf[pos]*256 + buf[pos+1];
>     pos+=2;
>
> Didn't it work? As far as I understand, reading a byte is always
> successful. Byte multipled by 256 is automatically cast to short.

Yes, that worked, too. But this only works for little endian systems, not
for big endian. Better would be:

    unsigned short code   = ntohs(buf[pos] + buf[pos+1]*256);
    pos+=2;
    unsigned short length = ntohs(buf[pos] + buf[pos+1]*256);
    pos+=2;

But I think that also the code where data is written into byte-buffers has
to be modified, e.g.:

    *((u_short*)DUID)=htons(this->DUIDType);
    *((u_short*)(DUID+2))=htons((short)macType);

Such code only works on ARM (and similar) if it is guaranteed that the
DUID, DUID+2 and so on are properly aligned for the pointer-type they are
casted to. In this case (uint16_t) it has to be a multiple of 2, for
uint32_t a multiple of 4.

Maybe are functions like these a solution:

uint16_t getUShort(const char *buf)
{
    return ntohs(buf[0] + buf[1] * 256);
}

void putUShort(char *buf, uint16_t val)
{
    uint16_t tmp = htons(val);
    buf[0] = tmp;

}


> Calling memcpy just to copy 2 bytes is quite an overhead. I just want to
> make completely sure before I rewrite all network related code...
>
> Regards,
> Tomek
>
> _______________________________________________
> http://klub.com.pl/cgi-bin/mailman/listinfo/dibbler
>




More information about the Dibbler mailing list