Skip to content

Commit 7451f9f

Browse files
committed
fix: Add Windows support for ntohl/ntohs with platform-specific headers
Replace POSIX-specific arpa/inet.h with conditional compilation that uses winsock2.h on Windows and arpa/inet.h on POSIX systems. This ensures the driver can be compiled on Windows without modification. Changes: - cassandra/cython_marshal.pyx: Add platform detection for ntohs/ntohl - cassandra/ioutils.pyx: Add platform detection for ntohl Signed-off-by: Yaniv Kaul <yaniv.kaul@scylladb.com>
1 parent a018c7a commit 7451f9f

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

cassandra/cython_marshal.pyx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,17 @@ from libc.string cimport memcpy
2020
from cassandra.buffer cimport Buffer, buf_read, to_bytes
2121

2222
# Use ntohs/ntohl for efficient big-endian to native conversion (single bswap instruction on x86)
23-
cdef extern from "arpa/inet.h" nogil:
24-
uint16_t ntohs(uint16_t netshort)
25-
uint32_t ntohl(uint32_t netlong)
23+
# Platform-specific header: arpa/inet.h on POSIX, winsock2.h on Windows
24+
cdef extern from *:
25+
"""
26+
#ifdef _WIN32
27+
#include <winsock2.h>
28+
#else
29+
#include <arpa/inet.h>
30+
#endif
31+
"""
32+
uint16_t ntohs(uint16_t netshort) nogil
33+
uint32_t ntohl(uint32_t netlong) nogil
2634

2735
cdef bint is_little_endian
2836
from cassandra.util import is_little_endian

cassandra/ioutils.pyx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,16 @@ from libc.stdint cimport int32_t, uint32_t
1919
from cassandra.bytesio cimport BytesIOReader
2020

2121
# Use ntohl for efficient big-endian to native conversion (single bswap instruction)
22-
cdef extern from "arpa/inet.h" nogil:
23-
uint32_t ntohl(uint32_t netlong)
22+
# Platform-specific header: arpa/inet.h on POSIX, winsock2.h on Windows
23+
cdef extern from *:
24+
"""
25+
#ifdef _WIN32
26+
#include <winsock2.h>
27+
#else
28+
#include <arpa/inet.h>
29+
#endif
30+
"""
31+
uint32_t ntohl(uint32_t netlong) nogil
2432

2533

2634
cdef inline int get_buf(BytesIOReader reader, Buffer *buf_out) except -1:

0 commit comments

Comments
 (0)