Skip to content

Commit

Permalink
Drop dependency on winsock2.h
Browse files Browse the repository at this point in the history
  • Loading branch information
k0ekk0ek committed Nov 10, 2023
1 parent f3a12f3 commit 457b505
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 41 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions src/fallback/endian.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@
#elif __APPLE__
#include <libkern/OSByteOrder.h>

#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)
Expand Down
1 change: 1 addition & 0 deletions src/fallback/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/fallback/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/fallback/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/generic/ilnp64.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 3 additions & 9 deletions src/generic/number.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
#ifndef NUMBER_H
#define NUMBER_H

#if _WIN32
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif

zone_nonnull_all
static zone_really_inline int32_t parse_symbol8(
zone_parser_t *parser,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/generic/ttl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/haswell/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 8 additions & 7 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ struct type_descriptor {
};

#if _WIN32
#include <basetsd.h>
typedef SSIZE_T ssize_t;
#define strncasecmp(s1, s2, n) _strnicmp(s1, s2, n)
#else
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));

Expand All @@ -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));

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/westmere/ip4.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/westmere/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/westmere/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/westmere/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 2 additions & 6 deletions tests/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@
#include <setjmp.h>
#include <string.h>
#include <cmocka.h>
#if _WIN32
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif

#include "zone.h"
#include "fallback/endian.h"

static int32_t add_rr(
zone_parser_t *parser,
Expand Down Expand Up @@ -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);
}
}
5 changes: 0 additions & 5 deletions tests/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
#include <setjmp.h>
#include <string.h>
#include <cmocka.h>
#if _WIN32
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif

#include "zone.h"

Expand Down

0 comments on commit 457b505

Please sign in to comment.