From a1eb30360b10b850b3105dac1daae64c73de5a03 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Fri, 4 Aug 2023 08:08:34 +0200 Subject: [PATCH] fix invalid protocol casts (#430) --- chronos/transports/datagram.nim | 10 +++++----- chronos/transports/stream.nim | 20 +++++++++----------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/chronos/transports/datagram.nim b/chronos/transports/datagram.nim index 3e10f76..665bc0e 100644 --- a/chronos/transports/datagram.nim +++ b/chronos/transports/datagram.nim @@ -466,11 +466,11 @@ else: var res = if isNil(child): DatagramTransport() else: child if sock == asyncInvalidSocket: - var proto = Protocol.IPPROTO_UDP - if local.family == AddressFamily.Unix: - # `Protocol` enum is missing `0` value, so we making here cast, until - # `Protocol` enum will not support IPPROTO_IP == 0. - proto = cast[Protocol](0) + let proto = + if local.family == AddressFamily.Unix: + Protocol.IPPROTO_IP + else: + Protocol.IPPROTO_UDP localSock = createAsyncSocket(local.getDomain(), SockType.SOCK_DGRAM, proto) if localSock == asyncInvalidSocket: diff --git a/chronos/transports/stream.nim b/chronos/transports/stream.nim index 45e4054..44a39b2 100644 --- a/chronos/transports/stream.nim +++ b/chronos/transports/stream.nim @@ -1475,14 +1475,13 @@ else: var saddr: Sockaddr_storage slen: SockLen - proto: Protocol var retFuture = newFuture[StreamTransport]("stream.transport.connect") address.toSAddr(saddr, slen) - proto = Protocol.IPPROTO_TCP - if address.family == AddressFamily.Unix: - # `Protocol` enum is missing `0` value, so we making here cast, until - # `Protocol` enum will not support IPPROTO_IP == 0. - proto = cast[Protocol](0) + let proto = + if address.family == AddressFamily.Unix: + Protocol.IPPROTO_IP + else: + Protocol.IPPROTO_TCP let sock = createAsyncSocket(address.getDomain(), SockType.SOCK_STREAM, proto) @@ -1938,11 +1937,10 @@ proc createStreamServer*(host: TransportAddress, else: # Posix if sock == asyncInvalidSocket: - var proto = Protocol.IPPROTO_TCP - if host.family == AddressFamily.Unix: - # `Protocol` enum is missing `0` value, so we making here cast, until - # `Protocol` enum will not support IPPROTO_IP == 0. - proto = cast[Protocol](0) + let proto = if host.family == AddressFamily.Unix: + Protocol.IPPROTO_IP + else: + Protocol.IPPROTO_TCP serverSocket = createAsyncSocket(host.getDomain(), SockType.SOCK_STREAM, proto)