2020-11-17 22:50:00 +01:00
|
|
|
import asyncdispatch, asyncnet, net, port_prediction, strformat
|
2020-11-17 20:40:30 +01:00
|
|
|
|
2020-11-18 09:31:09 +01:00
|
|
|
from nativesockets import
|
|
|
|
SockAddr,
|
|
|
|
SockAddr_storage,
|
|
|
|
SockLen,
|
|
|
|
getSockOptInt,
|
|
|
|
setSockOptInt
|
2020-11-17 20:40:30 +01:00
|
|
|
from sequtils import any
|
|
|
|
|
|
|
|
type
|
2020-11-18 16:34:14 +01:00
|
|
|
Attempt* = object
|
2020-11-17 20:40:30 +01:00
|
|
|
## A hole punching attempt.
|
2020-11-18 16:34:14 +01:00
|
|
|
srcPort*: Port
|
|
|
|
dstIp*: IpAddress
|
|
|
|
dstPorts*: seq[Port]
|
|
|
|
future*: Future[Port]
|
2020-11-17 20:40:30 +01:00
|
|
|
|
|
|
|
Puncher* = ref object
|
|
|
|
sock: AsyncSocket
|
|
|
|
attempts: seq[Attempt]
|
|
|
|
|
|
|
|
PunchHoleError* = object of ValueError
|
|
|
|
|
2020-11-18 09:31:09 +01:00
|
|
|
var IPPROTO_IP {.importc: "IPPROTO_IP", header: "<netinet/in.h>".}: cint
|
|
|
|
var IP_TTL {.importc: "IP_TTL", header: "<netinet/in.h>".}: cint
|
|
|
|
|
2020-11-17 20:40:30 +01:00
|
|
|
const Timeout = 3000
|
|
|
|
|
|
|
|
proc `==`(a, b: Attempt): bool =
|
|
|
|
## ``==`` for hole punching attempts.
|
|
|
|
##
|
|
|
|
## Two hole punching attempts are considered equal if their ``srcPort`` and
|
|
|
|
## ``dstIp`` are equal and their ``dstPorts`` overlap.
|
|
|
|
a.srcPort == b.srcPort and a.dstIp == b.dstIp and
|
|
|
|
a.dstPorts.any(proc (p: Port): bool = p in b.dstPorts)
|
|
|
|
|
|
|
|
proc initPuncher*(sock: AsyncSocket): Puncher =
|
|
|
|
Puncher(sock: sock)
|
|
|
|
|
|
|
|
proc punch(puncher: Puncher, peerIp: IpAddress, peerPort: Port,
|
2020-11-18 16:34:14 +01:00
|
|
|
peerProbedPorts: seq[Port], lowTTL: bool, msg: string):
|
|
|
|
Future[Attempt] {.async.} =
|
2020-11-17 20:40:30 +01:00
|
|
|
let punchFuture = newFuture[Port]("punch")
|
|
|
|
let predictedDstPorts = predictPortRange(peerPort, peerProbedPorts)
|
|
|
|
let (_, myPort) = puncher.sock.getLocalAddr()
|
2020-11-18 16:34:14 +01:00
|
|
|
result = Attempt(srcPort: myPort, dstIp: peerIp, dstPorts: predictedDstPorts,
|
|
|
|
future: punchFuture)
|
|
|
|
if puncher.attempts.contains(result):
|
2020-11-17 20:40:30 +01:00
|
|
|
raise newException(PunchHoleError,
|
|
|
|
"hole punching for given parameters already active")
|
2020-11-18 16:34:14 +01:00
|
|
|
puncher.attempts.add(result)
|
|
|
|
echo &"sending msg {msg} to {peerIp}, predicted ports: {result.dstPorts}"
|
2020-11-17 20:40:30 +01:00
|
|
|
var peerAddr: Sockaddr_storage
|
|
|
|
var peerSockLen: SockLen
|
|
|
|
try:
|
2020-11-18 09:31:09 +01:00
|
|
|
var defaultTTL: int
|
|
|
|
if lowTTL:
|
|
|
|
defaultTTL = puncher.sock.getFd.getSockOptInt(IPPROTO_IP, IP_TTL)
|
|
|
|
puncher.sock.getFd.setSockOptInt(IPPROTO_IP, IP_TTL, 2)
|
2020-11-18 16:34:14 +01:00
|
|
|
for dstPort in result.dstPorts:
|
|
|
|
toSockAddr(result.dstIp, dstPort, peerAddr, peerSockLen)
|
2020-11-17 20:40:30 +01:00
|
|
|
# TODO: replace asyncdispatch.sendTo with asyncnet.sendTo (Nim 1.4 required)
|
|
|
|
await sendTo(puncher.sock.getFd().AsyncFD, msg.cstring, msg.len,
|
|
|
|
cast[ptr SockAddr](addr peerAddr), peerSockLen)
|
2020-11-18 09:31:09 +01:00
|
|
|
if lowTTL:
|
|
|
|
puncher.sock.getFd.setSockOptInt(IPPROTO_IP, IP_TTL, defaultTTL)
|
2020-11-17 20:40:30 +01:00
|
|
|
except OSError as e:
|
|
|
|
raise newException(PunchHoleError, e.msg)
|
|
|
|
|
|
|
|
proc initiate*(puncher: Puncher, peerIp: IpAddress, peerPort: Port,
|
2020-11-18 16:34:14 +01:00
|
|
|
peerProbedPorts: seq[Port]): Future[Attempt] =
|
2020-11-18 09:31:09 +01:00
|
|
|
punch(puncher, peerIp, peerPort, peerProbedPorts, true, "SYN")
|
2020-11-17 20:40:30 +01:00
|
|
|
|
|
|
|
proc respond*(puncher: Puncher, peerIp: IpAddress, peerPort: Port,
|
2020-11-18 16:34:14 +01:00
|
|
|
peerProbedPorts: seq[Port]): Future[Attempt] =
|
2020-11-18 09:31:09 +01:00
|
|
|
punch(puncher, peerIp, peerPort, peerProbedPorts, false, "ACK")
|
2020-11-17 20:40:30 +01:00
|
|
|
|
2020-11-18 16:34:14 +01:00
|
|
|
proc finalize*(attempt: Attempt): Future[Port] {.async.} =
|
|
|
|
await attempt.future or sleepAsync(Timeout)
|
|
|
|
if attempt.future.finished:
|
|
|
|
result = attempt.future.read()
|
|
|
|
else:
|
|
|
|
raise newException(PunchHoleError, "timeout")
|
|
|
|
|
2020-11-17 20:40:30 +01:00
|
|
|
proc handleMsg*(puncher: Puncher, msg: string, peerIp: IpAddress,
|
|
|
|
peerPort: Port) =
|
|
|
|
## Handles an incoming UDP message which may complete the Futures returned by
|
|
|
|
## ``initiate`` and ``respond``.
|
2020-11-18 00:10:36 +01:00
|
|
|
if msg == "SYN":
|
|
|
|
# We received a SYN packet. We ignore it because we expected it to be
|
|
|
|
# filtered by our NAT.
|
2020-11-17 23:44:13 +01:00
|
|
|
return
|
2020-11-17 20:40:30 +01:00
|
|
|
let (_, myPort) = puncher.sock.getLocalAddr()
|
|
|
|
let query = Attempt(srcPort: myPort, dstIp: peerIp, dstPorts: @[peerPort])
|
|
|
|
let i = puncher.attempts.find(query)
|
|
|
|
if i != -1:
|
2020-11-18 17:12:18 +01:00
|
|
|
if msg == "ACK":
|
|
|
|
echo &"handling ACK message from {peerIp}:{peerPort}"
|
|
|
|
else:
|
|
|
|
echo &"handling QUIC message from {peerIp}:{peerPort}"
|
2020-11-17 20:40:30 +01:00
|
|
|
puncher.attempts[i].future.complete(peerPort)
|
|
|
|
puncher.attempts.del(i)
|
2020-11-18 18:24:19 +01:00
|
|
|
else:
|
|
|
|
echo &"received unexpected packet from {peerIp}:{peerPort}"
|
2020-11-17 20:40:30 +01:00
|
|
|
|
|
|
|
proc handleMsg*(puncher: Puncher, msg: string,
|
|
|
|
peerAddr: SockAddr | Sockaddr_storage, peerSockLen: SockLen) =
|
|
|
|
var peerIp: IpAddress
|
|
|
|
var peerPort: Port
|
|
|
|
fromSockAddr(peerAddr, peerSockLen, peerIp, peerPort)
|
|
|
|
handleMsg(puncher, msg, peerIp, peerPort)
|