From 187b23216242f3e735a3b8acf19d8782df11e0d2 Mon Sep 17 00:00:00 2001 From: Christian Ulrich Date: Tue, 21 Jul 2020 11:10:56 +0200 Subject: [PATCH] use dynamic length of destination ports and captured sequence numbers (3 for now) --- examples/app/app.nim | 2 +- message.nim | 9 +++++++++ punchd.nim | 5 +++-- tcp_syni.nim | 11 ++++++----- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/examples/app/app.nim b/examples/app/app.nim index d7d6e46..81d0be1 100644 --- a/examples/app/app.nim +++ b/examples/app/app.nim @@ -44,7 +44,7 @@ type srcPorts: array[3, Port] dstIp: IpAddress dstPorts: array[3, Port] - seqNumbers: array[10, uint32] + seqNumbers: seq[uint32] # Exceptions PunchdError = object of ValueError # FIXME: not used yet diff --git a/message.nim b/message.nim index 7d87f1d..7522268 100644 --- a/message.nim +++ b/message.nim @@ -25,6 +25,15 @@ proc parseField[S, T](input: string, output: var array[S, T]) = for i in 0 .. S.high: parseField(parts[i], output[i]) +proc parseField[T](input: string, output: var seq[T]) = + let parts = input.split(",") + if parts.len < 1: + raise newException(ValueError, "Sequence is empty") + output = newSeq[T](parts.len) + echo "before for loop" + for i in 0 .. parts.len - 1: + parseField(parts[i], output[i]) + proc parseField[T: tuple | object](input: string, output: var T) = var fieldCount = 0 for _ in output.fields: diff --git a/punchd.nim b/punchd.nim index db78b8b..b5bd257 100644 --- a/punchd.nim +++ b/punchd.nim @@ -21,7 +21,7 @@ type dstPorts: array[3, Port] srcIp: IpAddress srcPorts: array[3, Port] - seqNums: array[10, uint32] # FIXME: this should be a seq + seqNums: seq[uint32] proc handleRequest(line: string, unixSock: AsyncSocket) {.async.} = var id: string @@ -35,6 +35,7 @@ proc handleRequest(line: string, unixSock: AsyncSocket) {.async.} = of "tcp-syni-connect": let req = parseMessage[TcpSyniConnect](args[2]) proc handleSeqNumbers(seqNumbers: seq[uint32]) {.async.} = + echo "progress! seqNumbers: ", seqNumbers let content = @["tcp-syni-accept", $req.srcIp, req.srcPorts.join(","), $req.dstIp, req.dstPorts.join(","), seqNumbers.join(",")].join("|") @@ -45,7 +46,7 @@ proc handleRequest(line: string, unixSock: AsyncSocket) {.async.} = of "tcp-syni-accept": let req = parseMessage[TcpSyniAccept](args[2]) puncher = await initPuncher(req.srcPorts[0], req.dstIp, req.dstPorts, - @(req.seqNums)) +- req.seqNums) sock = await puncher.accept() else: diff --git a/tcp_syni.nim b/tcp_syni.nim index a7e49e2..4b58568 100644 --- a/tcp_syni.nim +++ b/tcp_syni.nim @@ -14,7 +14,7 @@ type srcIp: IpAddress srcPort: Port dstIp: IpAddress - dstPorts: array[10, Port] + dstPorts: seq[Port] seqNums: seq[uint32] PunchProgressCb* = proc (seqNums: seq[uint32]) {.async.} @@ -105,10 +105,11 @@ proc initPuncher*(srcPort: Port, dstIp: IpAddress, dstPorts: array[3, Port], seqNums: seq[uint32] = @[]): Future[TcpSyniPuncher] {.async.} = let localIp = getPrimaryIPAddr(dstIp) # TODO: do real port prediction - var predictedDstPorts: array[10, Port] - let basePort = min(dstPorts[1].uint16, uint16.high - 9) - for i in 0.uint16 .. 9.uint16: - predictedDstPorts[i] = Port(basePort + i) + var predictedDstPorts = newSeq[Port](3) + let basePort = min(dstPorts[1].uint16, + uint16.high - (predictedDstPorts.len - 1).uint16) + for i in 0 .. predictedDstPorts.len - 1: + predictedDstPorts[i] = Port(basePort + i.uint16) result = TcpSyniPuncher(srcIp: localIp, srcPort: srcPort, dstIp: dstIp, dstPorts: predictedDstPorts, seqNums: seqNums) for dstPort in result.dstPorts: