[go: up one dir, main page]

Menu

#39 Endian issue, pcapsipdump not working on big endian

trunk
closed-fixed
Aex Aey
endian (1)
5
2019-10-08
2019-10-06
micmac1
No

Hi all,

I'd like to update pcapsipdump on OpenWrt from 0.2 to a current trunk snapshot. I ran into two problems.

1) I get errors like these:

/home/sk/tmp/openwrt/staging_dir/toolchain-mips_24kc_gcc-7.4.0_musl/bin/g++-uc: line 22: [: s: integer expression expected
In file included from trigger.cpp:9:0:
pcapsipdump_lib.h:18:34: error: 'time_t' does not name a type; did you mean 'size_t'?
                            const time_t t);
                                  ^~~~~~
                                  size_t

To get around this I moved the <sys/stat.h> from pcapsipdump_lib.cpp to pcapsipdump_lib.h (patch attached).

2) When starting the cross-compiled binary on my Big Endian host it just sits there and does nothing. The old 0.2 pcapsipdump on the other hand works fine.

The problem for me is that pcapsipdump.h only checks if __LITTLE_ENDIAN or __BIG_ENDIAN is defined and then "does stuff". When looking at line 31:

if !defined (__LITTLE_ENDIAN) && !defined (__BIG_ENDIAN)

it's clear that checking the defines later on to base decisions on doesn't make sense: They're both defined. So the first check will always be true (Little Endian). Which is why pcapsipdump works there, no doubt, but on BE it doesn't.

I changed the tests to

#if __BYTE_ORDER == __LITTLE_ENDIAN
...
#elif __BYTE_ORDER == __BIG_ENDIAN
...

and that seems to work out fine. pcapsipdump now does its job.

I'll attach the patch for that, too. Not sure if you want to do it like this or some other way.

Kind regards,
Seb

1 Attachments

Discussion

  • micmac1

    micmac1 - 2019-10-06

    Here's the endian patch I used.

     
  • Aex Aey

    Aex Aey - 2019-10-06

    time_t fix is in [r150], thanks!

     

    Related

    Commit: [r150]

  • Aex Aey

    Aex Aey - 2019-10-06
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -13,7 +13,7 @@
                                       size_t
     ~~~
    
    -To get around this I moved the &lt;sys/stat.h&gt; from pcapsipdump_lib.cpp to pcapsipdump_lib.h (patch attached).
    +To get around this I moved the &amp;lt;sys/stat.h&amp;gt; from pcapsipdump_lib.cpp to pcapsipdump_lib.h (patch attached).
    
     2) When starting the cross-compiled binary on my Big Endian host it just sits there and does nothing. The old 0.2 pcapsipdump on the other hand works fine.
    
    • status: open --> pending-accepted
    • assigned_to: Aex Aey
     
  • Aex Aey

    Aex Aey - 2019-10-06

    Re: endinannes though - that is not what is going on here. __LITTLE_ENDIAN or __BIG_ENDIAN are used to construct packet header structs. On most systems, one of them is defined to indicate endianness (not both). If neither is defined, then, as a fallback __BYTE_ORDER is probed, and either __LITTLE_ENDIAN or __BIG_ENDIAN is defined locally.

    Do you complile this natively on BE host? Which gcc/libc? Can you try to compile lines 31..42 of pcapsipdump.h standalone to see if this is what compliler chokes on?

     
  • micmac1

    micmac1 - 2019-10-07

    Hello Aex,

    I crosscompile on x86_64 with OpenWrt buildroot. gcc is 7.4.0 and libc in this case is musl. Will try tonight.

    I think __LITTLE_ENDIAN and __BIG_ENDIAN can't be used for tests, because they seem to be defined both at the same time. I put an #error below

    if !defined (__LITTLE_ENDIAN) && !defined (__BIG_ENDIAN)

    and the build never reached this error. Also, pcapsipdump testing like this ("do this if both __LITTLE_ENDIAN and __BIG_ENDIAN aren't defined") suggests this is normal.

    I found this site: https://sourceforge.net/p/predef/wiki/Endianness/. It suggests that the two are just values that are used by __BYTE_ORDER macros.

    And the system headers used them like this, which is why testing __BYTE_ORDER for __LITTLE_ENDIAN and __BIG_ENDIAN works. You can probably also check if either __LITTLE_ENDIAN__ or __BIG_ENDIAN__ are defined according to the website (mind the trailing underscores).

    Kind regards,
    Seb

    PS: argh, SF doesn't seem to show all the underscores that I typed in the last paragraph there :(
    EDIT (aexaey): I've edited your comment above to add backtick around keywords. That preserves underscores in SF's markdown dialect.

     

    Last edit: Aex Aey 2019-10-07
  • Aex Aey

    Aex Aey - 2019-10-07

    Ok, I you are right. I was confused beween solaris-style defines (either _LITTLE_ENDIAN == 1 or _BIG_ENDIAN == 1; other one undefined) and BSD/Linux-style defines (either __BYTE_ORDER == __LITTLE_ENDIAN or __BYTE_ORDER == __BIG_ENDIAN; all three always defined), so trunk since [r139] was broken on BE systems. Should be fixed in [r151], can you give it a try?

     

    Related

    Commit: [r139]
    Commit: [r151]

    • micmac1

      micmac1 - 2019-10-07

      Sure, will try when I get home and ping you back. Thanks!

       
  • micmac1

    micmac1 - 2019-10-07

    Hello Aex,

    It's working fine now without patches. I compile-tested on x86_64, once a native compile and once a cross-combile for MIPS BE, endian was properly detected during both. Run-tested on MIPS BE.

    Thank you very much!

    Regards,
    Seb

    P.S.: I didn't see a way to close this ticket. Maybe you can close it when you get around to it.

     
  • Aex Aey

    Aex Aey - 2019-10-08
    • status: pending-accepted --> closed-fixed
     

Log in to post a comment.