diff --git a/ngtcp2.nim b/ngtcp2.nim index b781ac7..b35d932 100644 --- a/ngtcp2.nim +++ b/ngtcp2.nim @@ -10,20 +10,103 @@ 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".} - const socketheader = "" + {. + 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: - const socketheader = "" + {. + 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 - sockaddr* - {.header: socketheader, importc: "struct sockaddr".} = object - sockaddr_storage* - {.header: socketheader, importc: "struct sockaddr_storage".} = object + 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-08T15:24:49+02:00 +# Generated @ 2020-09-10T16:49:03+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/sockets.nim b/sockets.nim index 97cff44..9632749 100644 --- a/sockets.nim +++ b/sockets.nim @@ -1,13 +1,96 @@ # Import socket definitions from system +import nativesockets +export Port when defined(windows): {.passL: "-lws2_32".} - const socketheader = "" + {. + 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: - const socketheader = "" + {. + 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 - sockaddr* - {.header: socketheader, importc: "struct sockaddr".} = object - sockaddr_storage* - {.header: socketheader, importc: "struct sockaddr_storage".} = object + 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 diff --git a/tests/testNgtcp2.nim b/tests/testNgtcp2.nim index c1f37a8..dd3416d 100644 --- a/tests/testNgtcp2.nim +++ b/tests/testNgtcp2.nim @@ -6,3 +6,20 @@ 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