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

View File

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