Skip to content

Commit 8be2368

Browse files
Use offsetof to compute ENetProtocolHeader's sentTime offset (#276)
* Use the standard offsetof C library function to compute the ENetProtocolHeader sentTime offset. * The build system now checks for the C offsetof macro, and is expanded into a new enet macro for checking the offset of a field.
1 parent 657eaf9 commit 8be2368

4 files changed

Lines changed: 24 additions & 3 deletions

File tree

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ check_function_exists("gethostbyname_r" HAS_GETHOSTBYNAME_R)
1414
check_function_exists("gethostbyaddr_r" HAS_GETHOSTBYADDR_R)
1515
check_function_exists("inet_pton" HAS_INET_PTON)
1616
check_function_exists("inet_ntop" HAS_INET_NTOP)
17+
check_c_source_compiles("
18+
#include <stddef.h>
19+
struct S { int a; double b; };
20+
int main() {
21+
return (int)offsetof(struct S, b);
22+
}
23+
" HAS_OFFSETOF)
1724
check_struct_has_member("struct msghdr" "msg_flags" "sys/types.h;sys/socket.h" HAS_MSGHDR_FLAGS)
1825
set(CMAKE_EXTRA_INCLUDE_FILES "sys/types.h" "sys/socket.h")
1926
check_type_size("socklen_t" HAS_SOCKLEN_T BUILTIN_TYPES_ONLY)
@@ -48,6 +55,9 @@ endif()
4855
if(HAS_INET_NTOP)
4956
add_definitions(-DHAS_INET_NTOP=1)
5057
endif()
58+
if(HAS_OFFSETOF)
59+
add_definitions(-DHAS_OFFSETOF=1)
60+
endif()
5161
if(HAS_MSGHDR_FLAGS)
5262
add_definitions(-DHAS_MSGHDR_FLAGS=1)
5363
endif()

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ AC_CHECK_FUNC(poll, [AC_DEFINE(HAS_POLL)])
1515
AC_CHECK_FUNC(fcntl, [AC_DEFINE(HAS_FCNTL)])
1616
AC_CHECK_FUNC(inet_pton, [AC_DEFINE(HAS_INET_PTON)])
1717
AC_CHECK_FUNC(inet_ntop, [AC_DEFINE(HAS_INET_NTOP)])
18+
AC_CHECK_DECLS(offsetof, [AC_DEFINE(HAS_OFFSETOF)], [], [#include <stddef.h>])
1819

1920
AC_CHECK_MEMBER(struct msghdr.msg_flags, [AC_DEFINE(HAS_MSGHDR_FLAGS)], , [#include <sys/socket.h>])
2021

include/enet/utility.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@
55
#ifndef __ENET_UTILITY_H__
66
#define __ENET_UTILITY_H__
77

8+
#ifdef HAS_OFFSETOF
9+
#include <stddef.h>
10+
#endif
11+
812
#define ENET_MAX(x, y) ((x) > (y) ? (x) : (y))
913
#define ENET_MIN(x, y) ((x) < (y) ? (x) : (y))
1014
#define ENET_DIFFERENCE(x, y) ((x) < (y) ? (y) - (x) : (x) - (y))
1115

16+
#ifdef HAS_OFFSETOF
17+
#define ENET_OFFSETOF(str, field) (offsetof(str, field))
18+
#else
19+
#define ENET_OFFSETOF(str, field) ((size_t) & ((str *) 0) -> field)
20+
#endif
21+
1222
#endif /* __ENET_UTILITY_H__ */
1323

protocol.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
10201020
enet_uint16 peerID, flags;
10211021
enet_uint8 sessionID;
10221022

1023-
if (host -> receivedDataLength < (size_t) & ((ENetProtocolHeader *) 0) -> sentTime)
1023+
if (host -> receivedDataLength < ENET_OFFSETOF(ENetProtocolHeader, sentTime))
10241024
return 0;
10251025

10261026
header = (ENetProtocolHeader *) host -> receivedData;
@@ -1030,7 +1030,7 @@ enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
10301030
flags = peerID & ENET_PROTOCOL_HEADER_FLAG_MASK;
10311031
peerID &= ~ (ENET_PROTOCOL_HEADER_FLAG_MASK | ENET_PROTOCOL_HEADER_SESSION_MASK);
10321032

1033-
headerSize = (flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME ? sizeof (ENetProtocolHeader) : (size_t) & ((ENetProtocolHeader *) 0) -> sentTime);
1033+
headerSize = (flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME ? sizeof (ENetProtocolHeader) : ENET_OFFSETOF(ENetProtocolHeader, sentTime));
10341034
if (host -> checksum != NULL)
10351035
headerSize += sizeof (enet_uint32);
10361036

@@ -1682,7 +1682,7 @@ enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch
16821682
host -> buffers -> dataLength = sizeof (ENetProtocolHeader);
16831683
}
16841684
else
1685-
host -> buffers -> dataLength = (size_t) & ((ENetProtocolHeader *) 0) -> sentTime;
1685+
host -> buffers -> dataLength = ENET_OFFSETOF(ENetProtocolHeader, sentTime);
16861686

16871687
shouldCompress = 0;
16881688
if (host -> compressor.context != NULL && host -> compressor.compress != NULL)

0 commit comments

Comments
 (0)