简体   繁体   中英

Linux: ntohl does not work correctly

I have a project that needs to build on Windows, Linux, and VxWorks. The project is built on Linux and Windows but cross compiled for VxWorks. To handle endianness across multiple platforms, it uses ntoh.h. The Linux machine is little endian but ntohl doesn't swap in my program.

I wrote a test program that directly includes in.h. That swaps appropriately. I wrote another test program that just includes the ntoh.h. That swaps appropriately. Both test programs link to lib64/libc.so.6.

However, when I compile my project, ntohl doesn't swap. I can't break on ntohl using gdb "break ntohl" command. When building, I see LITTLE ENDIAN warning (see below) and do not see the "SHOULDNT BE HERE" error.

Please help. I don't understand why this problem is occurring.

Below is ntoh.h:

#ifndef __ntoh__
#define __ntoh__

#include "basic_types.h"

#ifdef WIN32
    #include <winsock2.h>
#elif LINUX
    #include <netinet/in.h>

    //This is here to determine what __BYTE_ORDER is set to in netinet/in.h.
    // Not in original code 
    #if __BYTE_ORDER == __BIG_ENDIAN
    #warning BIG ENDIAN BYTE ORDER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    #endif 

    //This is here to determine what __BYTE_ORDER is set to in netinet/in.h. 
    // Not in original code
    #if __BYTE_ORDER == __LITTLE_ENDIAN
    #warning YAY LITTLE ENDIAN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    #endif 
#else

  #error SHOULDNT BE HERE     //added for debugging purposes
  #define ntohl(x)        (x)
  #define ntohs(x)        (x)
  #define htonl(x)        (x)
  #define htons(x)        (x)

#endif

#endif // __ntoh__

Part of my compile command:

g++ -DDAU_PARSER -DNO_MT -DTEST_CLOCK -DLINUX  -g -Irelease/include -Irelease/include/Record_Data/ -Irelease/include/Utility -o dauParser DAU_Support_Tools/src/dau_parser.cpp DAU_Support_Tools/src/dau_parser_write_data_to_file.cpp Utility/src/Messaging/Communications/Message.cpp Utility/src/time_type.cpp Utility/src/collectable.cpp Utility/src/clist.cpp Utility/src/clock.cpp Utility/src/test_clock.cpp Utility/src/mutex.cpp Utility/src/ntoh.cpp ... 

The error is generated by the following lines:

int deadbeef = 0xDEADBEEF; 
printf("TESTING DEADBEEF %x %x\n", deadbeef, ntohl(deadbeef) ); 

The output from those two lines produce same output. TESTING DEADBEEF deadbeef deadbeef

The output from those two lines produce same output. TESTING DEADBEEF deadbeef deadbeef

Well, something is wrong, but we can't tell you what. You have to debug this problem, as you are the only one who can observe it.

Start with the simplest possible example:

cat t.c; gcc t.c && ./a.out
#include <netinet/in.h>
#include <stdio.h>

int main() {
  int deadbeef = 0xDEADBEEF; 
  printf("TESTING DEADBEEF %x %x\n", deadbeef, ntohl(deadbeef));
  return 0;
}


TESTING DEADBEEF deadbeef efbeadde

Did this produce expected result?

  • No: your toolchain and headers are busted.
  • Yes: your toolchain is ok, but your actual code does something different from the example.
    Run your code through preprocessor: gcc -dD -E -DLINUX ntoh.cpp , and look at what the ntohl macro expands to, and where it's coming from.

My guess is that you have something stupid in one of your headers, eg

#undef ntohl
#define ntohl(x) (x)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM