nmdb: Add LevelDB support
[nmdb.git] / nmdb / netutils.c
blob663b3c09f134735a43ce897b28aa6663652e0f7c
2 #include <arpa/inet.h> /* htonl() and friends */
3 #include "netutils.h"
6 /* ntohll() and htonll() are not standard, so we define it using an UGLY trick
7 * because there is no standard way to check for endianness at runtime! */
8 uint64_t ntohll(uint64_t x)
10 static int endianness = 0;
12 /* determine the endianness by checking how htonl() behaves; use -1
13 * for little endian and 1 for big endian */
14 if (endianness == 0) {
15 if (htonl(1) == 1)
16 endianness = 1;
17 else
18 endianness = -1;
21 if (endianness == 1) {
22 /* big endian */
23 return x;
26 /* little endian */
27 return ( ntohl( (x >> 32) & 0xFFFFFFFF ) | \
28 ( (uint64_t) ntohl(x & 0xFFFFFFFF) ) << 32 );
31 uint64_t htonll(uint64_t x)
33 static int endianness = 0;
35 /* determine the endianness by checking how htonl() behaves; use -1
36 * for little endian and 1 for big endian */
37 if (endianness == 0) {
38 if (htonl(1) == 1)
39 endianness = 1;
40 else
41 endianness = -1;
44 if (endianness == 1) {
45 /* big endian */
46 return x;
49 /* little endian */
50 return ( htonl( (x >> 32) & 0xFFFFFFFF ) | \
51 ( (uint64_t) htonl(x & 0xFFFFFFFF) ) << 32 );