more precise naming

master
Christian Ulrich 2020-07-09 21:10:25 +02:00
parent 17c4fa47f2
commit a415842398
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
3 changed files with 21 additions and 21 deletions

View File

@ -47,7 +47,7 @@ type
tcp
other
PacketInfo* = object
IpPacket* = object
case protocol*: Protocol
of tcp:
tcpIpSrc*: string
@ -62,21 +62,21 @@ var
ETHERTYPE_IP {.importc: "ETHERTYPE_IP", header: "<netinet/if_ether.h>".}: cushort
IPPROTO_TCP {.importc: "IPPROTO_TCP", header: "<netinet/in.h>".}: cint
proc fromString*(packet: string): PacketInfo =
let etherHeader = cast[ptr Ether_header](packet.cstring)
proc parseEthernetPacket*(input: string): IpPacket =
let etherHeader = cast[ptr Ether_header](input.cstring)
if ntohs(etherHeader.ether_type) == ETHERTYPE_IP:
let ipHeader = cast[ptr Ip](cast[int](packet.cstring) + sizeof(Ether_header))
let ipHeader = cast[ptr Ip](cast[int](input.cstring) + sizeof(Ether_header))
let ipSrc = $inet_ntoa(ipHeader.ip_src)
let ipDst = $inet_ntoa(ipHeader.ip_dst)
if ipHeader.ip_p.int == IPPROTO_TCP:
let tcpHeader = cast[ptr Tcphdr](cast[int](ipHeader) + ipHeader.ip_hl.int * 4)
result = PacketInfo(protocol: tcp,
tcpIpSrc: ipSrc,
tcpIpDst: ipDst,
tcpPortSrc: Port(ntohs(tcpHeader.th_sport)),
tcpPortDst: Port(ntohs(tcpHeader.th_dport)),
tcpSeqNumber: ntohl(tcpHeader.th_seq))
result = IpPacket(protocol: tcp,
tcpIpSrc: ipSrc,
tcpIpDst: ipDst,
tcpPortSrc: Port(ntohs(tcpHeader.th_sport)),
tcpPortDst: Port(ntohs(tcpHeader.th_dport)),
tcpSeqNumber: ntohl(tcpHeader.th_seq))
else:
result = PacketInfo(protocol: other)
result = IpPacket(protocol: other)
else:
result = PacketInfo(protocol: other)
result = IpPacket(protocol: other)

View File

@ -30,7 +30,7 @@ var
PACKET_ADD_MEMBERSHIP {.importc: "PACKET_ADD_MEMBERSHIP", header: "<linux/if_packet.h>".}: cushort
PACKET_MR_PROMISC {.importc: "PACKET_MR_PROMISC", header: "<linux/if_packet.h>".}: cushort
proc setupRawSocket*(iface: NetworkInterface): AsyncFD =
proc setupEthernetCapturingSocket*(iface: NetworkInterface): AsyncFD =
# Create a raw packet socket. For accessing outgoing packets we need to use
# ETH_P_ALL which is needed in network byte order, see packet(7) man page.
result = createAsyncNativeSocket(AF_PACKET.cint,

View File

@ -66,14 +66,14 @@ proc captureSeqNumbers(puncher: TcpSyniPuncher, rawFd: AsyncFD,
if packet == "":
break
echo "packet len: ", packet.len
let packetInfo = fromString(packet)
if packetInfo.protocol == tcp and
packetInfo.tcpIpSrc == $puncher.srcIp and
packetInfo.tcpPortSrc.int == puncher.srcPort.int and
packetInfo.tcpIpDst == $puncher.dstIp:
let parsed = parseEthernetPacket(packet)
if parsed.protocol == tcp and
parsed.tcpIpSrc == $puncher.srcIp and
parsed.tcpPortSrc.int == puncher.srcPort.int and
parsed.tcpIpDst == $puncher.dstIp:
for i, port in puncher.dstPorts.pairs:
if packetInfo.tcpPortDst.int == port.int:
seqNums.add(packetInfo.tcpSeqNumber)
if parsed.tcpPortDst.int == port.int:
seqNums.add(parsed.tcpSeqNumber)
break
await cb(seqNums)
@ -111,7 +111,7 @@ proc connect*(puncher: TcpSyniPuncher,
progressCb: PunchProgressCb): Future[AsyncSocket] =
result = newFuture[AsyncSocket]("tcp_syni.connect")
let iface = fromIpAddress($puncher.srcIp)
let rawFd = setupRawSocket(iface)
let rawFd = setupEthernetCapturingSocket(iface)
asyncCheck puncher.captureSeqNumbers(rawFd, progressCb)
for dstPort in puncher.dstPorts:
asyncCheck doConnect(puncher.srcIp, puncher.srcPort, puncher.dstIp,