need a second socket for injecting

This commit is contained in:
Christian Ulrich 2020-08-23 15:52:50 +02:00
parent e745141b51
commit 7bc90f02b8
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 7 additions and 5 deletions

View File

@ -56,7 +56,7 @@ proc injectTcpPacket(rawFd: AsyncFD, ipPacket: IpPacket) {.async.} =
cast[ptr SockAddr](addr sockaddr), sockaddrLen) cast[ptr SockAddr](addr sockaddr), sockaddrLen)
echo &"injected {ipPacket.ipAddrSrc}:{ipPacket.tcpPortSrc.int} -> {ipPacket.ipAddrDst}:{ipPacket.tcpPortDst.int} (seq {ipPacket.tcpSeqNumber})" echo &"injected {ipPacket.ipAddrSrc}:{ipPacket.tcpPortSrc.int} -> {ipPacket.ipAddrDst}:{ipPacket.tcpPortDst.int} (seq {ipPacket.tcpSeqNumber})"
except OSError as e: except OSError as e:
echo &"cannot inject {ipPacket.ipAddrSrc}:{ipPacket.tcpPortSrc.int} -> {ipPacket.ipAddrDst}:{ipPacket.tcpPortDst.int} (seq {ipPacket.tcpSeqNumber})", e.msg echo &"cannot inject {ipPacket.ipAddrSrc}:{ipPacket.tcpPortSrc.int} -> {ipPacket.ipAddrDst}:{ipPacket.tcpPortDst.int} (seq {ipPacket.tcpSeqNumber}): ", e.msg
raise newException(PunchHoleError, e.msg) raise newException(PunchHoleError, e.msg)
proc captureSeqNumbers(puncher: TcpSyniPuncher, rawFd: AsyncFD, proc captureSeqNumbers(puncher: TcpSyniPuncher, rawFd: AsyncFD,
@ -80,9 +80,10 @@ proc captureSeqNumbers(puncher: TcpSyniPuncher, rawFd: AsyncFD,
break break
await cb(seqNums) await cb(seqNums)
proc captureAndResendAck(puncher: TcpSyniPuncher, rawFd: AsyncFD) {.async.} = proc captureAndResendAck(puncher: TcpSyniPuncher, captureFd: AsyncFD,
injectFd: AsyncFD) {.async.} =
while true: while true:
let packet = await rawFd.recv(4000) let packet = await captureFd.recv(4000)
if packet == "": if packet == "":
break break
let parsed = parseEthernetPacket(packet) let parsed = parseEthernetPacket(packet)
@ -101,7 +102,7 @@ proc captureAndResendAck(puncher: TcpSyniPuncher, rawFd: AsyncFD) {.async.} =
tcpPortDst: parsed.tcpPortDst, tcpPortDst: parsed.tcpPortDst,
tcpSeqNumber: parsed.tcpSeqNumber, tcpSeqNumber: parsed.tcpSeqNumber,
tcpFlags: parsed.tcpFlags) tcpFlags: parsed.tcpFlags)
await rawFd.injectTcpPacket(ipPacket) await injectFd.injectTcpPacket(ipPacket)
break break
proc initPuncher*(srcPort: Port, dstIp: IpAddress, dstPorts: array[3, Port], proc initPuncher*(srcPort: Port, dstIp: IpAddress, dstPorts: array[3, Port],
@ -159,8 +160,9 @@ proc connect*(puncher: TcpSyniPuncher,
let iface = fromIpAddress(puncher.srcIp) let iface = fromIpAddress(puncher.srcIp)
let captureSeqFd = setupEthernetCapturingSocket(iface) let captureSeqFd = setupEthernetCapturingSocket(iface)
let captureAckFd = setupEthernetCapturingSocket(iface) let captureAckFd = setupEthernetCapturingSocket(iface)
let injectAckFd = setupTcpInjectingSocket()
asyncCheck puncher.captureSeqNumbers(captureSeqFd, progressCb) asyncCheck puncher.captureSeqNumbers(captureSeqFd, progressCb)
asyncCheck puncher.captureAndResendAck(captureAckFd) asyncCheck puncher.captureAndResendAck(captureAckFd, injectAckFd)
await puncher.addFirewallRules() await puncher.addFirewallRules()
try: try:
result = await puncher.connectParallel() result = await puncher.connectParallel()