From 5db4dfa18e4e64d829cc366e8d348057c0ad7b1c Mon Sep 17 00:00:00 2001 From: Jeroen Koekkoek Date: Fri, 10 Nov 2023 16:24:27 +0100 Subject: [PATCH] Drop dependency on winsock2.h --- CMakeLists.txt | 2 +- src/fallback/endian.h.in | 12 +++++++++--- src/fallback/parser.c | 1 + src/fallback/time.h | 2 +- src/fallback/type.h | 2 +- src/generic/ilnp64.h | 8 ++++---- src/generic/number.h | 12 +++--------- src/generic/ttl.h | 2 +- src/haswell/parser.c | 1 + src/types.h | 15 ++++++++------- src/westmere/ip4.h | 2 +- src/westmere/parser.c | 1 + src/westmere/time.h | 2 +- src/westmere/type.h | 2 +- tests/time.c | 8 ++------ tests/types.c | 5 ----- 16 files changed, 36 insertions(+), 41 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bf67852..e7b51ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -227,7 +227,7 @@ foreach(match ${matches}) endforeach() file(GENERATE OUTPUT config.h CONTENT "${template}") -configure_file(src/fallback/endian.h.in src/fallback/endian.h @ONLY) +configure_file(src/fallback/endian.h.in fallback/endian.h @ONLY) if(BUILD_TESTING) diff --git a/src/fallback/endian.h.in b/src/fallback/endian.h.in index 9f5a4d1..c03e5c2 100644 --- a/src/fallback/endian.h.in +++ b/src/fallback/endian.h.in @@ -49,9 +49,15 @@ #elif __APPLE__ #include -#define BYTE_ORDER __BYTE_ORDER__ -#define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__ -#define BIG_ENDIAN __ORDER_BIG_ENDIAN__ +#ifndef BYTE_ORDER +# define BYTE_ORDER __BYTE_ORDER__ +#endif +#ifndef LITTLE_ENDIAN +# define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__ +#endif +#ifndef BIG_ENDIAN +# define BIG_ENDIAN __ORDER_BIG_ENDIAN__ +#endif #define htobe16(x) OSSwapHostToBigInt16(x) #define htobe32(x) OSSwapHostToBigInt32(x) diff --git a/src/fallback/parser.c b/src/fallback/parser.c index 18e25c6..e983bec 100644 --- a/src/fallback/parser.c +++ b/src/fallback/parser.c @@ -9,6 +9,7 @@ #include "zone.h" #include "diagnostic.h" #include "log.h" +#include "fallback/endian.h" #include "fallback/bits.h" #include "lexer.h" #include "table.h" diff --git a/src/fallback/time.h b/src/fallback/time.h index 7106542..198be86 100644 --- a/src/fallback/time.h +++ b/src/fallback/time.h @@ -80,7 +80,7 @@ static zone_really_inline int32_t parse_time( const uint64_t minutes = hours * 60 + min; const uint64_t seconds = minutes * 60 + sec; - uint32_t time = htonl((uint32_t)seconds); + uint32_t time = htobe32((uint32_t)seconds); memcpy(&parser->rdata->octets[parser->rdata->length], &time, sizeof(time)); parser->rdata->length += sizeof(time); return ZONE_INT32; diff --git a/src/fallback/type.h b/src/fallback/type.h index 0caa8d8..ea2858a 100644 --- a/src/fallback/type.h +++ b/src/fallback/type.h @@ -400,7 +400,7 @@ static zone_really_inline int32_t parse_type( return r; scan_type(parser, type, field, token, &c, &s); - c = htons(c); + c = htobe16(c); memcpy(&parser->rdata->octets[parser->rdata->length], &c, sizeof(c)); parser->rdata->length += sizeof(c); return ZONE_TYPE; diff --git a/src/generic/ilnp64.h b/src/generic/ilnp64.h index 2b7b8e0..5cb2c22 100644 --- a/src/generic/ilnp64.h +++ b/src/generic/ilnp64.h @@ -50,10 +50,10 @@ static zone_really_inline int32_t parse_ilnp64( if (n != 3 || p == g || p - g > 4 || contiguous[(uint8_t)*p] == CONTIGUOUS) SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), TNAME(type)); - a[0] = htons(a[0]); - a[1] = htons(a[1]); - a[2] = htons(a[2]); - a[3] = htons(a[3]); + a[0] = htobe16(a[0]); + a[1] = htobe16(a[1]); + a[2] = htobe16(a[2]); + a[3] = htobe16(a[3]); memcpy(parser->rdata->octets+parser->rdata->length, a, 8); parser->rdata->length += 8; return ZONE_ILNP64; diff --git a/src/generic/number.h b/src/generic/number.h index 99a83ae..87e86c5 100644 --- a/src/generic/number.h +++ b/src/generic/number.h @@ -9,12 +9,6 @@ #ifndef NUMBER_H #define NUMBER_H -#if _WIN32 -#include -#else -#include -#endif - zone_nonnull_all static zone_really_inline int32_t parse_symbol8( zone_parser_t *parser, @@ -82,7 +76,7 @@ static zone_really_inline int32_t parse_symbol16( SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), TNAME(type)); } - uint16_t n16 = htons((uint16_t)n); + uint16_t n16 = htobe16((uint16_t)n); memcpy(&parser->rdata->octets[parser->rdata->length], &n16, sizeof(n16)); parser->rdata->length += sizeof(n16); return ZONE_INT16; @@ -141,7 +135,7 @@ static zone_really_inline int32_t parse_int16( if (n > UINT16_MAX || p - token->data > 5 || is_contiguous((uint8_t)*p)) SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), TNAME(type)); - uint16_t n16 = htons((uint16_t)n); + uint16_t n16 = htobe16((uint16_t)n); memcpy(&parser->rdata->octets[parser->rdata->length], &n16, sizeof(n16)); parser->rdata->length += sizeof(n16); return ZONE_INT16; @@ -171,7 +165,7 @@ static zone_really_inline int32_t parse_int32( if (n > UINT32_MAX || p - token->data > 10 || is_contiguous((uint8_t)*p)) SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), TNAME(type)); - const uint32_t n32 = htonl((uint32_t)n); + const uint32_t n32 = htobe32((uint32_t)n); memcpy(&parser->rdata->octets[parser->rdata->length], &n32, sizeof(n32)); parser->rdata->length += sizeof(n32); return ZONE_INT32; diff --git a/src/generic/ttl.h b/src/generic/ttl.h index 7036d40..8084b6a 100644 --- a/src/generic/ttl.h +++ b/src/generic/ttl.h @@ -136,7 +136,7 @@ static zone_really_inline int32_t parse_ttl( if ((r = scan_ttl(parser, type, field, token, &t)) < 0) return r; - t = htonl(t); + t = htobe32(t); memcpy(&parser->rdata->octets[parser->rdata->length], &t, sizeof(t)); parser->rdata->length += sizeof(t); return ZONE_TTL; diff --git a/src/haswell/parser.c b/src/haswell/parser.c index 5d100e5..f7fb3bc 100644 --- a/src/haswell/parser.c +++ b/src/haswell/parser.c @@ -10,6 +10,7 @@ #include "diagnostic.h" #include "log.h" #include "haswell/simd.h" +#include "fallback/endian.h" #include "haswell/bits.h" #include "lexer.h" #include "table.h" diff --git a/src/types.h b/src/types.h index e7a2e12..9c2ec86 100644 --- a/src/types.h +++ b/src/types.h @@ -74,6 +74,7 @@ struct type_descriptor { }; #if _WIN32 +#include typedef SSIZE_T ssize_t; #define strncasecmp(s1, s2, n) _strnicmp(s1, s2, n) #else @@ -121,7 +122,7 @@ static zone_really_inline ssize_t check_ttl( SYNTAX_ERROR(parser, "Missing %s in %s", NAME(field), TNAME(type)); memcpy(&number, data, sizeof(number)); - number = ntohl(number); + number = be32toh(number); if (number > INT32_MAX) SEMANTIC_ERROR(parser, "Invalid %s in %s", NAME(field), TNAME(type)); @@ -996,9 +997,9 @@ static int32_t parse_loc_rdata( return result; north_south: if (token->data[0] == 'N') - latitude = htonl((1u<<31) + degrees); + latitude = htobe32((1u<<31) + degrees); else if (token->data[1] == 'S') - latitude = htonl((1u<<31) - degrees); + latitude = htobe32((1u<<31) - degrees); else SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(&fields[4]), TNAME(type)); @@ -1024,9 +1025,9 @@ static int32_t parse_loc_rdata( return result; east_west: if (token->data[0] == 'E') - longitude = htonl((1u<<31) + degrees); + longitude = htobe32((1u<<31) + degrees); else if (token->data[0] == 'W') - longitude = htonl((1u<<31) - degrees); + longitude = htobe32((1u<<31) - degrees); else SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(&fields[5]), TNAME(type)); @@ -1039,7 +1040,7 @@ static int32_t parse_loc_rdata( if (scan_altitude(token->data, token->length, &altitude) == -1) SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(&fields[6]), TNAME(type)); - altitude = htonl(altitude); + altitude = htobe32(altitude); memcpy(&parser->rdata->octets[12], &altitude, sizeof(altitude)); // size @@ -1827,7 +1828,7 @@ static int32_t parse_hip_rdata( if (parser->rdata->length > 65535u + 4u + hit_length) SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(&type->rdata.fields[4]), TNAME(type)); - uint16_t pk_length = htons((uint16_t)((parser->rdata->length - hit_length) - 4)); + uint16_t pk_length = htobe16((uint16_t)((parser->rdata->length - hit_length) - 4)); memcpy(&parser->rdata->octets[2], &pk_length, sizeof(pk_length)); lex(parser, token); diff --git a/src/westmere/ip4.h b/src/westmere/ip4.h index 335f646..dbc9678 100644 --- a/src/westmere/ip4.h +++ b/src/westmere/ip4.h @@ -209,7 +209,7 @@ static zone_really_inline int32_t parse_ip4( if (sse_inet_aton(token->data, o, &n) != 1 || is_contiguous((uint8_t)token->data[n])) SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), TNAME(type)); - parser->rdata->length += sizeof(struct in_addr); + parser->rdata->length += 4; return ZONE_IP4; } diff --git a/src/westmere/parser.c b/src/westmere/parser.c index c4015de..9033a3e 100644 --- a/src/westmere/parser.c +++ b/src/westmere/parser.c @@ -10,6 +10,7 @@ #include "diagnostic.h" #include "log.h" #include "westmere/simd.h" +#include "fallback/endian.h" #include "westmere/bits.h" #include "lexer.h" #include "table.h" diff --git a/src/westmere/time.h b/src/westmere/time.h index 89d9058..e7874cb 100644 --- a/src/westmere/time.h +++ b/src/westmere/time.h @@ -136,7 +136,7 @@ static zone_really_inline int32_t parse_time( if (contiguous[(uint8_t)token->data[14]] == CONTIGUOUS) SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), TNAME(type)); - uint32_t time = htonl(sse_result); + uint32_t time = htobe32(sse_result); memcpy(&parser->rdata->octets[parser->rdata->length], &time, sizeof(time)); parser->rdata->length += sizeof(time); return ZONE_INT32; diff --git a/src/westmere/type.h b/src/westmere/type.h index 5d9aed3..65e9847 100644 --- a/src/westmere/type.h +++ b/src/westmere/type.h @@ -263,7 +263,7 @@ static zone_really_inline int32_t parse_type( if ((r = scan_type(parser, type, field, token, &c, &s)) != ZONE_TYPE) SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), TNAME(type)); - c = htons(c); + c = htobe16(c); memcpy(&parser->rdata->octets[parser->rdata->length], &c, sizeof(c)); parser->rdata->length += sizeof(c); return ZONE_TYPE; diff --git a/tests/time.c b/tests/time.c index 522cca7..2d197c2 100644 --- a/tests/time.c +++ b/tests/time.c @@ -10,13 +10,9 @@ #include #include #include -#if _WIN32 -#include -#else -#include -#endif #include "zone.h" +#include "fallback/endian.h" static int32_t add_rr( zone_parser_t *parser, @@ -106,7 +102,7 @@ void time_stamp_syntax(void **state) continue; uint32_t seconds; memcpy(&seconds, rdata.octets+8, sizeof(seconds)); - seconds = ntohl(seconds); + seconds = be32toh(seconds); assert_int_equal(seconds, tests[i].seconds); } } diff --git a/tests/types.c b/tests/types.c index a681ad1..d768556 100644 --- a/tests/types.c +++ b/tests/types.c @@ -10,11 +10,6 @@ #include #include #include -#if _WIN32 -#include -#else -#include -#endif #include "zone.h"