diff --git a/chronos/transports/datagram.nim b/chronos/transports/datagram.nim index 7e55fe9d..d0f22230 100644 --- a/chronos/transports/datagram.nim +++ b/chronos/transports/datagram.nim @@ -236,6 +236,13 @@ when defined(windows): closeSocket(localSock) raiseTransportOsError(err) + if ServerFlags.Broadcast in flags: + if not setSockOpt(localSock, SOL_SOCKET, SO_BROADCAST, 1): + let err = osLastError() + if sock == asyncInvalidSocket: + closeSocket(localSock) + raiseTransportOsError(err) + ## Fix for Q263823. var bytesRet: DWORD var bval = WINBOOL(0) @@ -418,6 +425,13 @@ else: closeSocket(localSock) raiseTransportOsError(err) + if ServerFlags.Broadcast in flags: + if not setSockOpt(localSock, SOL_SOCKET, SO_BROADCAST, 1): + let err = osLastError() + if sock == asyncInvalidSocket: + closeSocket(localSock) + raiseTransportOsError(err) + if local.port != Port(0): var saddr: Sockaddr_storage var slen: SockLen diff --git a/tests/testdatagram.nim b/tests/testdatagram.nim index 5a25ef00..dac73a7e 100644 --- a/tests/testdatagram.nim +++ b/tests/testdatagram.nim @@ -463,6 +463,24 @@ suite "Datagram Transport test suite": except: discard + proc testBroadcast(): Future[int] {.async.} = + const expectMessage = "BROADCAST MESSAGE" + var ta1 = initTAddress("0.0.0.0:45010") + var bta = initTAddress("255.255.255.255:45010") + var res = 0 + proc clientMark(transp: DatagramTransport, + raddr: TransportAddress): Future[void] {.async.} = + var bmsg = transp.getMessage() + var smsg = cast[string](bmsg) + if smsg == expectMessage: + inc(res) + transp.close() + var dgram1 = newDatagramTransport(clientMark, local = ta1, + flags = {Broadcast}) + await dgram1.sendTo(bta, expectMessage) + await wait(dgram1.join(), 5.seconds) + result = res + test "close(transport) test": check waitFor(testTransportClose()) == true test m1: @@ -483,3 +501,5 @@ suite "Datagram Transport test suite": check waitFor(test3(true)) == ClientsCount * MessagesCount test "Datagram connection reset test": check waitFor(testConnReset()) == true + test "Broadcast test": + check waitFor(testBroadcast()) == 1