must use sendTo to inject ip packet

This commit is contained in:
Christian Ulrich 2020-07-17 20:06:52 +02:00
parent 32060fe9dc
commit c5f6348cf2
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 13 additions and 3 deletions

View File

@ -1,6 +1,6 @@
import asyncdispatch, asyncnet, strformat
from net import IpAddress, Port, `$`, `==`, getPrimaryIPAddr
from nativesockets import setSockOptInt
from net import IpAddress, Port, `$`, `==`, getPrimaryIPAddr, toSockAddr
from nativesockets import SockAddr, Sockaddr_storage, SockLen, setSockOptInt
import asyncutils
import ip_packet
import network_interface
@ -90,7 +90,17 @@ proc injectSyns(rawFd: AsyncFD, srcIp: IpAddress, srcPort: Port,
tcpPortDst: srcPort,
tcpSeqNumber: seqNum,
tcpFlags: {SYN})
asyncCheck rawFd.send(serialize(ipPacket))
echo &"injecting {srcIP}:{srcPort.int} -> {dstIp}:{dstPort.int} (seq {seqNum})"
try:
let packet = serialize(ipPacket)
var sockaddr: Sockaddr_storage
var sockaddrLen: SockLen
toSockAddr(dstIp, dstPort, sockaddr, sockaddrLen)
await rawFd.sendTo(packet.cstring, packet.len,
cast[ptr SockAddr](addr sockaddr), sockaddrLen)
echo &"injected {srcIP}:{srcPort.int} -> {dstIp}:{dstPort.int} (seq {seqNum})"
except OSError as e:
echo "cannot inject: ", e.msg
proc initPuncher*(srcPort: Port, dstIp: IpAddress, dstPorts: array[3, Port],
seqNums: seq[uint32] = @[]): Future[TcpSyniPuncher] {.async.} =