include TCP window size and reuse parsed packet when resending

This commit is contained in:
Christian Ulrich 2020-08-23 22:48:34 +02:00
parent fe2b52f168
commit 0125993ccd
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
2 changed files with 9 additions and 14 deletions

View File

@ -68,6 +68,7 @@ type
tcpSeqNumber*: uint32
tcpAckNumber*: uint32
tcpFlags*: set[TcpFlag]
tcpWindowSize*: uint16
else:
discard
@ -96,7 +97,8 @@ proc parseEthernetPacket*(input: string): IpPacket =
tcpPortDst: Port(ntohs(tcpHeader.th_dport)),
tcpSeqNumber: ntohl(tcpHeader.th_seq),
tcpAckNumber: ntohl(tcpHeader.th_ack),
tcpFlags: cast[set[TcpFlag]](tcpHeader.th_flags))
tcpFlags: cast[set[TcpFlag]](tcpHeader.th_flags),
tcpWindowSize: ntohs(tcpHeader.th_win))
else:
result = IpPacket(protocol: other)
else:
@ -158,7 +160,7 @@ proc serialize*(packet: IpPacket): string =
tcpHeader.th_ack = htonl(packet.tcpAckNumber)
tcpHeader.th_off = 5
tcpHeader.th_flags = cast[uint8](packet.tcpFlags)
tcpHeader.th_win = htons(1452 * 10)
tcpHeader.th_win = htons(packet.tcpWindowSize)
tcpHeader.th_urp = 0
tcpHeader.th_sum = htons(tcpChecksum(result))

View File

@ -86,7 +86,7 @@ proc captureAndResendAck(puncher: TcpSyniPuncher, captureFd: AsyncFD,
let packet = await captureFd.recv(4000)
if packet == "":
break
let parsed = parseEthernetPacket(packet)
var parsed = parseEthernetPacket(packet)
if parsed.protocol == tcp and
parsed.ipAddrSrc == puncher.srcIp and
parsed.tcpPortSrc.int == puncher.srcPort.int and
@ -94,16 +94,8 @@ proc captureAndResendAck(puncher: TcpSyniPuncher, captureFd: AsyncFD,
parsed.tcpFlags == {ACK}:
for port in puncher.dstPorts:
if parsed.tcpPortDst.int == port.int:
let ipPacket = IpPacket(protocol: tcp,
ipAddrSrc: puncher.srcIp,
ipAddrDst: puncher.dstIp,
ipTTL: 64,
tcpPortSrc: puncher.srcPort,
tcpPortDst: parsed.tcpPortDst,
tcpSeqNumber: parsed.tcpSeqNumber,
tcpAckNumber: parsed.tcpAckNumber,
tcpFlags: parsed.tcpFlags)
await injectFd.injectTcpPacket(ipPacket)
parsed.ipTTL = 64
await injectFd.injectTcpPacket(parsed)
return
proc initPuncher*(srcPort: Port, dstIp: IpAddress, dstPorts: array[3, Port],
@ -201,7 +193,8 @@ proc accept*(puncher: TcpSyniPuncher): Future[AsyncSocket] {.async.} =
tcpPortDst: puncher.srcPort,
tcpSeqNumber: seqNum,
tcpAckNumber: 0,
tcpFlags: {SYN})
tcpFlags: {SYN},
tcpWindowSize: 1452 * 10)
asyncCheck rawFd.injectTcpPacket(ipPacket)
let sock = newAsyncSocket()
sock.setSockOpt(OptReuseAddr, true)