diff --git a/build.sh b/build.sh index 85e6ceb..90097b4 100755 --- a/build.sh +++ b/build.sh @@ -9,14 +9,8 @@ fi # run cmake on ngtcp2 sources cmake -S "${root}/sources" -B "${root}/build" -# C includes -cat "${root}/includes.nim" > "${root}/ngtcp2.nim" - -# dividing line -echo >> "${root}/ngtcp2.nim" - -# socket definitions -cat "${root}/sockets.nim" >> "${root}/ngtcp2.nim" +# add prelude +cat "${root}/prelude.nim" > "${root}/ngtcp2.nim" # dividing line echo >> "${root}/ngtcp2.nim" diff --git a/ngtcp2.nim b/ngtcp2.nim index b35d932..9a6b022 100644 --- a/ngtcp2.nim +++ b/ngtcp2.nim @@ -1,112 +1,18 @@ -# C include directories - import os import strformat +import ngtcp2/sockets +# Socket definitions +export sockets + +# C include directories const root = currentSourcePath.parentDir const sourceInclude = root/"sources"/"lib"/"includes" const buildInclude = root/"build"/"lib"/"includes" {.passC: fmt"-I{sourceInclude} -I{buildInclude}".} -# Import socket definitions from system -import nativesockets -export Port - -when defined(windows): - {.passL: "-lws2_32".} - {. - pragma: import_in_addr, - importc: "struct in_addr", - header: "" - .} - {. - pragma: import_in6_addr, - importc: "struct in6_addr", - header: "" - .} - {. - pragma: import_sockaddr, - importc: "struct sockaddr", - header: "" - .} - {. - pragma: import_sockaddr_in, - importc: "struct sockaddr_in", - header: "" - .} - {. - pragma: import_sockaddr_in6, - importc: "struct sockaddr_in6", - header: "" - .} - {. - pragma: import_sockaddr_storage, - importc: "struct sockaddr_storage", - header: "" - .} -else: - {. - pragma: import_in_addr, - importc: "struct in_addr", - header: "" - .} - {. - pragma: import_in6_addr, - importc: "struct in6_addr", - header: "" - .} - {. - pragma: import_sockaddr - importc: "struct sockaddr", - header: "" - .} - {. - pragma: import_sockaddr_in, - importc: "struct sockaddr_in", - header: "" - .} - {. - pragma: import_sockaddr_in6, - importc: "struct sockaddr_in6", - header: "" - .} - {. - pragma: import_sockaddr_storage, - importc: "struct sockaddr_storage", - header: "" - .} - -type - AddressFamily* {.size: sizeof(uint16).} = enum - AF_INET = nativesockets.AF_INET - AF_INET6 = nativesockets.AF_INET6 - in_addr* {.import_in_addr.} = object - s_addr*: uint32 - in6_addr* {.import_in6_addr.} = object - s6_addr*: array[16, uint8] - sockaddr_storage* {.import_sockaddr_storage.} = object - sockaddr_in* {.import_sockaddr_in.} = object - sin_family*: AddressFamily - sin_port*: Port - sin_addr*: in_addr - sin_zero*: array[8, char] - sockaddr_in6* {.import_sockaddr_in6.} = object - sin6_family*: AddressFamily - sin6_port*: Port - sin6_flowinfo*: uint32 - sin6_address*: in6_addr - sin6_scopy_id*: uint32 - sockaddr* {.import_sockaddr.} = object - sa_family*: AddressFamily - sa_data*: array[14, char] - SocketAddress* {.union.} = object - address*: sockaddr - ipv4*: sockaddr_in - ipv6*: sockaddr_in6 - storage*: sockaddr_storage - -# Generated @ 2020-09-10T16:49:03+02:00 +# Generated @ 2020-09-10T17:06:23+02:00 # Command line: # /home/user/.nimble/pkgs/nimterop-0.6.11/nimterop/toast --compile=./sources/lib/ngtcp2_acktr.c --compile=./sources/lib/ngtcp2_addr.c --compile=./sources/lib/ngtcp2_buf.c --compile=./sources/lib/ngtcp2_cc.c --compile=./sources/lib/ngtcp2_cid.c --compile=./sources/lib/ngtcp2_conn.c --compile=./sources/lib/ngtcp2_conv.c --compile=./sources/lib/ngtcp2_crypto.c --compile=./sources/lib/ngtcp2_err.c --compile=./sources/lib/ngtcp2_gaptr.c --compile=./sources/lib/ngtcp2_idtr.c --compile=./sources/lib/ngtcp2_ksl.c --compile=./sources/lib/ngtcp2_log.c --compile=./sources/lib/ngtcp2_map.c --compile=./sources/lib/ngtcp2_mem.c --compile=./sources/lib/ngtcp2_path.c --compile=./sources/lib/ngtcp2_pkt.c --compile=./sources/lib/ngtcp2_ppe.c --compile=./sources/lib/ngtcp2_pq.c --compile=./sources/lib/ngtcp2_pv.c --compile=./sources/lib/ngtcp2_qlog.c --compile=./sources/lib/ngtcp2_range.c --compile=./sources/lib/ngtcp2_ringbuf.c --compile=./sources/lib/ngtcp2_rob.c --compile=./sources/lib/ngtcp2_rst.c --compile=./sources/lib/ngtcp2_rtb.c --compile=./sources/lib/ngtcp2_str.c --compile=./sources/lib/ngtcp2_strm.c --compile=./sources/lib/ngtcp2_vec.c --compile=./sources/lib/ngtcp2_version.c --pnim --preprocess --noHeader --defines=NGTCP2_STATICLIB --includeDirs=./sources/lib/includes --includeDirs=./build/lib/includes ./sources/lib/includes/ngtcp2/ngtcp2.h diff --git a/ngtcp2.nimble b/ngtcp2.nimble index 2898089..3189341 100644 --- a/ngtcp2.nimble +++ b/ngtcp2.nimble @@ -3,7 +3,7 @@ version = "0.1.0" author = "Status Research & Development GmbH" description = "Nim wrapper around the ngtcp2 library" license = "MIT" -installDirs = @["sources", "build"] +installDirs = @["sources", "build", "ngtcp2"] installFiles = @["ngtcp2.nim"] requires "nim >= 1.2.6" diff --git a/sockets.nim b/ngtcp2/sockets.nim similarity index 100% rename from sockets.nim rename to ngtcp2/sockets.nim diff --git a/includes.nim b/prelude.nim similarity index 81% rename from includes.nim rename to prelude.nim index d1d0897..7e7ad4f 100644 --- a/includes.nim +++ b/prelude.nim @@ -1,8 +1,11 @@ -# C include directories - import os import strformat +import ngtcp2/sockets +# Socket definitions +export sockets + +# C include directories const root = currentSourcePath.parentDir const sourceInclude = root/"sources"/"lib"/"includes" const buildInclude = root/"build"/"lib"/"includes" diff --git a/tests/testNgtcp2.nim b/tests/testNgtcp2.nim index dd3416d..c1f37a8 100644 --- a/tests/testNgtcp2.nim +++ b/tests/testNgtcp2.nim @@ -6,20 +6,3 @@ test "default settings": ngtcp2_settings_default(addr settings) check settings.transport_params.max_udp_payload_size > 0 check settings.transport_params.active_connection_id_limit > 0 - -test "BSD socket addresses": - check sizeof(in_addr) == 4 - check sizeof(in6_addr) == 16 - check sizeof(sockaddr) < sizeof(sockaddr_storage) - check sizeof(sockaddr_in) < sizeof(sockaddr_storage) - check sizeof(sockaddr_in6) < sizeof(sockaddr_storage) - check sizeof(sockaddr_in.sin_family) == 2 - check sizeof(sockaddr_in.sin_port) == 2 - check cast[uint16](AF_INET) == 2 - -test "SocketAddress union": - var address: SocketAddress - check sizeof(address) == sizeof(sockaddr_storage) - address.address.sa_family = AF_INET - check address.ipv4.sin_family == AF_INET - check address.ipv6.sin6_family == AF_INET diff --git a/tests/testSockets.nim b/tests/testSockets.nim new file mode 100644 index 0000000..83ee8df --- /dev/null +++ b/tests/testSockets.nim @@ -0,0 +1,19 @@ +import unittest +import ngtcp2 + +test "BSD socket addresses": + check sizeof(in_addr) == 4 + check sizeof(in6_addr) == 16 + check sizeof(sockaddr) < sizeof(sockaddr_storage) + check sizeof(sockaddr_in) < sizeof(sockaddr_storage) + check sizeof(sockaddr_in6) < sizeof(sockaddr_storage) + check sizeof(sockaddr_in.sin_family) == 2 + check sizeof(sockaddr_in.sin_port) == 2 + check cast[uint16](AF_INET) == 2 + +test "SocketAddress union": + var address: SocketAddress + check sizeof(address) == sizeof(sockaddr_storage) + address.address.sa_family = AF_INET + check address.ipv4.sin_family == AF_INET + check address.ipv6.sin6_family == AF_INET