Skip to content

Commit a17eb32

Browse files
committed
util: provide htonll function
Instead of brittle tests to try to delegate to a system's version of a 64-bit byte order conversion function, include our own.
1 parent 0bc3595 commit a17eb32

File tree

6 files changed

+20
-31
lines changed

6 files changed

+20
-31
lines changed

src/compat.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,6 @@
2121
# include <stdbool.h>
2222
#endif
2323

24-
#ifdef __linux__
25-
/* See man page endian(3) */
26-
# include <endian.h>
27-
# define htonll htobe64
28-
#elif defined(__NetBSD__) || defined(__OpenBSD__)
29-
/* See man page htobe64(3) */
30-
# include <endian.h>
31-
# define htonll htobe64
32-
#elif defined(__FreeBSD__)
33-
/* See man page bwaps64(9) */
34-
# include <sys/endian.h>
35-
# define htonll htobe64
36-
#elif defined(sun) || defined(__sun)
37-
/* See man page byteorder(3SOCKET) */
38-
# include <sys/types.h>
39-
# include <netinet/in.h>
40-
# include <inttypes.h>
41-
42-
# if !defined(htonll)
43-
# if defined(_BIG_ENDIAN)
44-
# define htonll(x) (x)
45-
# else
46-
# define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl((uint64_t)(x) >> 32))
47-
# endif
48-
# endif
49-
#endif
50-
5124
#ifndef MIN
5225
# define MIN(x, y) (((x) < (y)) ? (x) : (y))
5326
#endif

src/ntlm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ static bool generate_lm2_response(ntlm_client *ntlm,
11261126
size_t lm2_len = 16;
11271127
uint64_t local_nonce;
11281128

1129-
local_nonce = htonll(ntlm->nonce);
1129+
local_nonce = ntlm_htonll(ntlm->nonce);
11301130

11311131
if (!ntlm_hmac_ctx_reset(ntlm->hmac_ctx) ||
11321132
!ntlm_hmac_md5_init(ntlm->hmac_ctx,
@@ -1198,8 +1198,8 @@ static bool generate_ntlm2_response(ntlm_client *ntlm)
11981198

11991199
/* the blob's integer values are in network byte order */
12001200
signature = htonl(0x01010000);
1201-
timestamp = htonll(ntlm->timestamp);
1202-
nonce = htonll(ntlm->nonce);
1201+
timestamp = ntlm_htonll(ntlm->timestamp);
1202+
nonce = ntlm_htonll(ntlm->nonce);
12031203

12041204
/* construct the blob */
12051205
memcpy(&blob[0], &signature, 4);

src/util.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <stdlib.h>
1010
#include <stdint.h>
11+
#include <arpa/inet.h>
1112

1213
#include "compat.h"
1314
#include "util.h"
@@ -19,3 +20,16 @@ void ntlm_memzero(void *data, size_t size)
1920
while (size--)
2021
*scan++ = 0x0;
2122
}
23+
24+
uint64_t ntlm_htonll(uint64_t value)
25+
{
26+
static union {
27+
uint32_t i;
28+
char c[8];
29+
} test = { 0x01020304 };
30+
31+
if (test.c[0] == 0x01)
32+
return value;
33+
else
34+
return ((uint64_t)htonl(value) << 32) | htonl((uint64_t)value >> 32);
35+
}

src/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
#define PRIVATE_UTIL_H__
1111

1212
extern void ntlm_memzero(void *data, size_t size);
13+
extern uint64_t ntlm_htonll(uint64_t value);
1314

1415
#endif /* PRIVATE_UTIL_H__ */

tests/challenge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void test_challenge__sample(void)
8888
cl_ntlm_pass(ntlm, ntlm_client_set_challenge(ntlm,
8989
challenge_msg, sizeof(challenge_msg)));
9090

91-
cl_assert_equal_i(htonll(0x0123456789abcdef),
91+
cl_assert_equal_i(ntlm_htonll(0x0123456789abcdef),
9292
ntlm_client_challenge_nonce(ntlm));
9393

9494
cl_assert_equal_s("DOMAIN", ntlm_client_target(ntlm));

tests/ntlm_tests.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "clar.h"
55
#include "ntlm.h"
6+
#include "util.h"
67

78
#define cl_ntlm_pass(ntlm, expr) cl_ntlm_expect((ntlm), (expr), 0, __FILE__, __LINE__)
89

0 commit comments

Comments
 (0)