nim-webrtc/webrtc/stun/stun_transport.nim

121 lines
3.7 KiB
Nim
Raw Permalink Normal View History

feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
# Nim-WebRTC
# Copyright (c) 2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
feat: dtls connection using mbedtls (#10) * feat: dtls connection using mbedtls * refactor: change according to the stun protocol rework * chore: rename init proc into new * docs: adds object field comments * chore: split dtls.nim into two files & renaming * chore: remove useless code * chore: remove TODOs as they were addressed with a Stun refactorization * fix: oversight on dtls.new * feat: add dtls test * chore: added license & used pragma on testdtls * fix: remove usage of deprecated TrackerCounter * fix: trackers counter * fix: - add windows linking library - make stun stop asynchronous (causing issue on macos) - store private key and certificate * chore: renaming test * docs: update DtlsConn comment * fix: remove code duplicate * chore: update comment * chore: remove duplication mbedtls initialization code in accept/connect and un-expose mbedtls context * feat: add exception management to dtls_transport * fix: check address family before handshake * fix: exhaustive case * fix: do not create dtlsConn if the address family is not IP * chore: remove entropy from MbedTLSCtx * chore: remove asyncspawn of cleanupdtlsconn * chore: ctx is no longer public * test: add a test with more than 2 nodes * chore: started is now useful * chore: update Dtls.stop * chore: removed unecessary todos * docs: add comments on DtlsConn.read and getters * feat: add tracker for dtls connection and transport * chore: privatize local and remote certificate * style: use nph * fix: remove laddr from dtls_conn (not used) * style: sort imports * chore: clean Dtls.stop * fix: remote address is no longer exposed * fix: raddr change oversight * chore: change `verify` name * chore: changed `sendFuture: Future[void]` into `dataToSend: seq[byte]` * chore: avoid sequence copy * chore: change assert message --------- Co-authored-by: diegomrsantos <diegomrsantos@gmail.com>
2024-08-13 13:54:03 +00:00
import tables, sequtils
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
import chronos, chronicles, bearssl
import stun_connection, stun_message, ../udp_transport
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
logScope:
topics = "webrtc stun stun_transport"
const
StunTransportTracker* = "webrtc.stun.transport"
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
StunMaxPendingConnections = 512
type
Stun* = ref object
connections: Table[TransportAddress, StunConn]
pendingConn: AsyncQueue[StunConn]
feat: dtls connection using mbedtls (#10) * feat: dtls connection using mbedtls * refactor: change according to the stun protocol rework * chore: rename init proc into new * docs: adds object field comments * chore: split dtls.nim into two files & renaming * chore: remove useless code * chore: remove TODOs as they were addressed with a Stun refactorization * fix: oversight on dtls.new * feat: add dtls test * chore: added license & used pragma on testdtls * fix: remove usage of deprecated TrackerCounter * fix: trackers counter * fix: - add windows linking library - make stun stop asynchronous (causing issue on macos) - store private key and certificate * chore: renaming test * docs: update DtlsConn comment * fix: remove code duplicate * chore: update comment * chore: remove duplication mbedtls initialization code in accept/connect and un-expose mbedtls context * feat: add exception management to dtls_transport * fix: check address family before handshake * fix: exhaustive case * fix: do not create dtlsConn if the address family is not IP * chore: remove entropy from MbedTLSCtx * chore: remove asyncspawn of cleanupdtlsconn * chore: ctx is no longer public * test: add a test with more than 2 nodes * chore: started is now useful * chore: update Dtls.stop * chore: removed unecessary todos * docs: add comments on DtlsConn.read and getters * feat: add tracker for dtls connection and transport * chore: privatize local and remote certificate * style: use nph * fix: remove laddr from dtls_conn (not used) * style: sort imports * chore: clean Dtls.stop * fix: remote address is no longer exposed * fix: raddr change oversight * chore: change `verify` name * chore: changed `sendFuture: Future[void]` into `dataToSend: seq[byte]` * chore: avoid sequence copy * chore: change assert message --------- Co-authored-by: diegomrsantos <diegomrsantos@gmail.com>
2024-08-13 13:54:03 +00:00
readingLoop: Future[void].Raising([CancelledError])
udp: UdpTransport
feat: dtls connection using mbedtls (#10) * feat: dtls connection using mbedtls * refactor: change according to the stun protocol rework * chore: rename init proc into new * docs: adds object field comments * chore: split dtls.nim into two files & renaming * chore: remove useless code * chore: remove TODOs as they were addressed with a Stun refactorization * fix: oversight on dtls.new * feat: add dtls test * chore: added license & used pragma on testdtls * fix: remove usage of deprecated TrackerCounter * fix: trackers counter * fix: - add windows linking library - make stun stop asynchronous (causing issue on macos) - store private key and certificate * chore: renaming test * docs: update DtlsConn comment * fix: remove code duplicate * chore: update comment * chore: remove duplication mbedtls initialization code in accept/connect and un-expose mbedtls context * feat: add exception management to dtls_transport * fix: check address family before handshake * fix: exhaustive case * fix: do not create dtlsConn if the address family is not IP * chore: remove entropy from MbedTLSCtx * chore: remove asyncspawn of cleanupdtlsconn * chore: ctx is no longer public * test: add a test with more than 2 nodes * chore: started is now useful * chore: update Dtls.stop * chore: removed unecessary todos * docs: add comments on DtlsConn.read and getters * feat: add tracker for dtls connection and transport * chore: privatize local and remote certificate * style: use nph * fix: remove laddr from dtls_conn (not used) * style: sort imports * chore: clean Dtls.stop * fix: remote address is no longer exposed * fix: raddr change oversight * chore: change `verify` name * chore: changed `sendFuture: Future[void]` into `dataToSend: seq[byte]` * chore: avoid sequence copy * chore: change assert message --------- Co-authored-by: diegomrsantos <diegomrsantos@gmail.com>
2024-08-13 13:54:03 +00:00
laddr*: TransportAddress
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
usernameProvider: StunUsernameProvider
usernameChecker: StunUsernameChecker
passwordProvider: StunPasswordProvider
rng: ref HmacDrbgContext
proc addConnToTable(self: Stun, conn: StunConn) =
proc cleanup() =
self.connections.del(conn.raddr)
self.connections[conn.raddr] = conn
conn.addOnClose(cleanup)
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
proc accept*(self: Stun): Future[StunConn] {.async: (raises: [CancelledError]).} =
## Accept a Stun Connection
##
var res: StunConn
while true:
res = await self.pendingConn.popFirst()
if res.closed != true: # The connection could be closed before being accepted
break
return res
proc connect*(
self: Stun,
raddr: TransportAddress
): Future[StunConn] {.async: (raises: []).} =
## Connect to a remote address, creating a Stun Connection
##
self.connections.withValue(raddr, res):
return res[]
do:
let res = StunConn.new(self.udp, raddr, false, self.usernameProvider,
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
self.usernameChecker, self.passwordProvider, self.rng)
self.addConnToTable(res)
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
return res
proc stunReadLoop(self: Stun) {.async: (raises: [CancelledError]).} =
while true:
let (buf, raddr) = await self.udp.read()
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
var stunConn: StunConn
if not self.connections.hasKey(raddr):
stunConn = StunConn.new(self.udp, raddr, true, self.usernameProvider,
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
self.usernameChecker, self.passwordProvider, self.rng)
self.addConnToTable(stunConn)
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
await self.pendingConn.addLast(stunConn)
else:
try:
stunConn = self.connections[raddr]
except KeyError as exc:
doAssert(false, "Should never happen")
if isStunMessage(buf):
await stunConn.stunMsgs.addLast(buf)
else:
await stunConn.dataRecv.addLast(buf)
feat: dtls connection using mbedtls (#10) * feat: dtls connection using mbedtls * refactor: change according to the stun protocol rework * chore: rename init proc into new * docs: adds object field comments * chore: split dtls.nim into two files & renaming * chore: remove useless code * chore: remove TODOs as they were addressed with a Stun refactorization * fix: oversight on dtls.new * feat: add dtls test * chore: added license & used pragma on testdtls * fix: remove usage of deprecated TrackerCounter * fix: trackers counter * fix: - add windows linking library - make stun stop asynchronous (causing issue on macos) - store private key and certificate * chore: renaming test * docs: update DtlsConn comment * fix: remove code duplicate * chore: update comment * chore: remove duplication mbedtls initialization code in accept/connect and un-expose mbedtls context * feat: add exception management to dtls_transport * fix: check address family before handshake * fix: exhaustive case * fix: do not create dtlsConn if the address family is not IP * chore: remove entropy from MbedTLSCtx * chore: remove asyncspawn of cleanupdtlsconn * chore: ctx is no longer public * test: add a test with more than 2 nodes * chore: started is now useful * chore: update Dtls.stop * chore: removed unecessary todos * docs: add comments on DtlsConn.read and getters * feat: add tracker for dtls connection and transport * chore: privatize local and remote certificate * style: use nph * fix: remove laddr from dtls_conn (not used) * style: sort imports * chore: clean Dtls.stop * fix: remote address is no longer exposed * fix: raddr change oversight * chore: change `verify` name * chore: changed `sendFuture: Future[void]` into `dataToSend: seq[byte]` * chore: avoid sequence copy * chore: change assert message --------- Co-authored-by: diegomrsantos <diegomrsantos@gmail.com>
2024-08-13 13:54:03 +00:00
proc stop*(self: Stun) {.async: (raises: []).} =
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
## Stop the Stun transport and close all the connections
##
feat: dtls connection using mbedtls (#10) * feat: dtls connection using mbedtls * refactor: change according to the stun protocol rework * chore: rename init proc into new * docs: adds object field comments * chore: split dtls.nim into two files & renaming * chore: remove useless code * chore: remove TODOs as they were addressed with a Stun refactorization * fix: oversight on dtls.new * feat: add dtls test * chore: added license & used pragma on testdtls * fix: remove usage of deprecated TrackerCounter * fix: trackers counter * fix: - add windows linking library - make stun stop asynchronous (causing issue on macos) - store private key and certificate * chore: renaming test * docs: update DtlsConn comment * fix: remove code duplicate * chore: update comment * chore: remove duplication mbedtls initialization code in accept/connect and un-expose mbedtls context * feat: add exception management to dtls_transport * fix: check address family before handshake * fix: exhaustive case * fix: do not create dtlsConn if the address family is not IP * chore: remove entropy from MbedTLSCtx * chore: remove asyncspawn of cleanupdtlsconn * chore: ctx is no longer public * test: add a test with more than 2 nodes * chore: started is now useful * chore: update Dtls.stop * chore: removed unecessary todos * docs: add comments on DtlsConn.read and getters * feat: add tracker for dtls connection and transport * chore: privatize local and remote certificate * style: use nph * fix: remove laddr from dtls_conn (not used) * style: sort imports * chore: clean Dtls.stop * fix: remote address is no longer exposed * fix: raddr change oversight * chore: change `verify` name * chore: changed `sendFuture: Future[void]` into `dataToSend: seq[byte]` * chore: avoid sequence copy * chore: change assert message --------- Co-authored-by: diegomrsantos <diegomrsantos@gmail.com>
2024-08-13 13:54:03 +00:00
try:
await allFutures(toSeq(self.connections.values()).mapIt(it.close()))
except CancelledError as exc:
discard
await self.readingLoop.cancelAndWait()
untrackCounter(StunTransportTracker)
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
proc defaultUsernameProvider(): string = ""
proc defaultUsernameChecker(username: seq[byte]): bool = true
proc defaultPasswordProvider(username: seq[byte]): seq[byte] = @[]
proc new*(
T: type Stun,
udp: UdpTransport,
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
usernameProvider: StunUsernameProvider = defaultUsernameProvider,
usernameChecker: StunUsernameChecker = defaultUsernameChecker,
passwordProvider: StunPasswordProvider = defaultPasswordProvider,
rng: ref HmacDrbgContext = HmacDrbgContext.new(),
): T =
## Initialize the Stun transport
##
var self = T(
udp: udp,
feat: dtls connection using mbedtls (#10) * feat: dtls connection using mbedtls * refactor: change according to the stun protocol rework * chore: rename init proc into new * docs: adds object field comments * chore: split dtls.nim into two files & renaming * chore: remove useless code * chore: remove TODOs as they were addressed with a Stun refactorization * fix: oversight on dtls.new * feat: add dtls test * chore: added license & used pragma on testdtls * fix: remove usage of deprecated TrackerCounter * fix: trackers counter * fix: - add windows linking library - make stun stop asynchronous (causing issue on macos) - store private key and certificate * chore: renaming test * docs: update DtlsConn comment * fix: remove code duplicate * chore: update comment * chore: remove duplication mbedtls initialization code in accept/connect and un-expose mbedtls context * feat: add exception management to dtls_transport * fix: check address family before handshake * fix: exhaustive case * fix: do not create dtlsConn if the address family is not IP * chore: remove entropy from MbedTLSCtx * chore: remove asyncspawn of cleanupdtlsconn * chore: ctx is no longer public * test: add a test with more than 2 nodes * chore: started is now useful * chore: update Dtls.stop * chore: removed unecessary todos * docs: add comments on DtlsConn.read and getters * feat: add tracker for dtls connection and transport * chore: privatize local and remote certificate * style: use nph * fix: remove laddr from dtls_conn (not used) * style: sort imports * chore: clean Dtls.stop * fix: remote address is no longer exposed * fix: raddr change oversight * chore: change `verify` name * chore: changed `sendFuture: Future[void]` into `dataToSend: seq[byte]` * chore: avoid sequence copy * chore: change assert message --------- Co-authored-by: diegomrsantos <diegomrsantos@gmail.com>
2024-08-13 13:54:03 +00:00
laddr: udp.laddr,
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
usernameProvider: usernameProvider,
usernameChecker: usernameChecker,
passwordProvider: passwordProvider,
rng: rng
)
feat: dtls connection using mbedtls (#10) * feat: dtls connection using mbedtls * refactor: change according to the stun protocol rework * chore: rename init proc into new * docs: adds object field comments * chore: split dtls.nim into two files & renaming * chore: remove useless code * chore: remove TODOs as they were addressed with a Stun refactorization * fix: oversight on dtls.new * feat: add dtls test * chore: added license & used pragma on testdtls * fix: remove usage of deprecated TrackerCounter * fix: trackers counter * fix: - add windows linking library - make stun stop asynchronous (causing issue on macos) - store private key and certificate * chore: renaming test * docs: update DtlsConn comment * fix: remove code duplicate * chore: update comment * chore: remove duplication mbedtls initialization code in accept/connect and un-expose mbedtls context * feat: add exception management to dtls_transport * fix: check address family before handshake * fix: exhaustive case * fix: do not create dtlsConn if the address family is not IP * chore: remove entropy from MbedTLSCtx * chore: remove asyncspawn of cleanupdtlsconn * chore: ctx is no longer public * test: add a test with more than 2 nodes * chore: started is now useful * chore: update Dtls.stop * chore: removed unecessary todos * docs: add comments on DtlsConn.read and getters * feat: add tracker for dtls connection and transport * chore: privatize local and remote certificate * style: use nph * fix: remove laddr from dtls_conn (not used) * style: sort imports * chore: clean Dtls.stop * fix: remote address is no longer exposed * fix: raddr change oversight * chore: change `verify` name * chore: changed `sendFuture: Future[void]` into `dataToSend: seq[byte]` * chore: avoid sequence copy * chore: change assert message --------- Co-authored-by: diegomrsantos <diegomrsantos@gmail.com>
2024-08-13 13:54:03 +00:00
self.readingLoop = self.stunReadLoop()
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
self.pendingConn = newAsyncQueue[StunConn](StunMaxPendingConnections)
trackCounter(StunTransportTracker)
feat: stun protocol & stun connection (#9) * feat: stun protocol & stun connection * rename getResponse into getPong and test it * add Username attribute * genUfrag procedure * Add generateRandomSeq to generate a transaction id * First draft of getPing * Use UdpPacketInfo tuple * Change closing debug message * Add proper exception tracking * Change StunConn init behavior * Add a last UdpPacketInfo * Add comments * refactor: change connection management * Add a lot of comments/Finish refactor * Add copyright headers on test files * simplify newRng proc for testing * add exception tracking for stun transport asynchronous proc * remove ping/pong example building in the ci * rename getPong test * remove maximum connections * Add ICE stun attributes * Stun rework * feat: getBindingRequest * fix oversight & add comments * Test rework * Adds continue in stunMessageHandler loop * remove `doAssert(false)` from Stun.connect() * Update TODO * fix comment typo * fix: test lacking precision * docs: add StunConn.init() comments * chore: make teststun more readable * feat: use withValue instead of getOrDefault in Stun.connect() * feat: add check if Fingerprint is valid * refactor: getAttribute and username/password provider * chore: removes genUfrag, should be in libp2p instead * chore: remove redundant test * chore: change warn log to debug * docs: update getBindingResponse/Request comments * chore: renames init into new * fix: compilation warnings * chore: change closed line to be at the end of the close procedure * feat: limit queues size
2024-05-24 12:14:30 +00:00
return self