use IpAddress instead of strings
This commit is contained in:
parent
d97a0eaed7
commit
4e554b5a83
|
@ -1,4 +1,5 @@
|
||||||
from nativesockets import Port, ntohs, ntohl
|
from nativesockets import ntohs, ntohl, htons, htonl
|
||||||
|
from net import IpAddress, IpAddressFamily, Port, `$`
|
||||||
from posix import InAddr, inet_ntoa
|
from posix import InAddr, inet_ntoa
|
||||||
|
|
||||||
type
|
type
|
||||||
|
@ -50,8 +51,8 @@ type
|
||||||
IpPacket* = object
|
IpPacket* = object
|
||||||
case protocol*: Protocol
|
case protocol*: Protocol
|
||||||
of tcp:
|
of tcp:
|
||||||
tcpIpSrc*: string
|
tcpIpSrc*: IpAddress
|
||||||
tcpIpDst*: string
|
tcpIpDst*: IpAddress
|
||||||
tcpPortSrc*: Port
|
tcpPortSrc*: Port
|
||||||
tcpPortDst*: Port
|
tcpPortDst*: Port
|
||||||
tcpSeqNumber*: uint32
|
tcpSeqNumber*: uint32
|
||||||
|
@ -66,16 +67,20 @@ proc parseEthernetPacket*(input: string): IpPacket =
|
||||||
let etherHeader = cast[ptr Ether_header](input.cstring)
|
let etherHeader = cast[ptr Ether_header](input.cstring)
|
||||||
if ntohs(etherHeader.ether_type) == ETHERTYPE_IP:
|
if ntohs(etherHeader.ether_type) == ETHERTYPE_IP:
|
||||||
let ipHeader = cast[ptr Ip](cast[int](input.cstring) + sizeof(Ether_header))
|
let ipHeader = cast[ptr Ip](cast[int](input.cstring) + sizeof(Ether_header))
|
||||||
let ipSrc = $inet_ntoa(ipHeader.ip_src)
|
let ipSrcScalar = ntohl(ipHeader.ip_src.s_addr)
|
||||||
let ipDst = $inet_ntoa(ipHeader.ip_dst)
|
let ipDstScalar = ntohl(ipHeader.ip_dst.s_addr)
|
||||||
|
let ipSrc = IpAddress(family: Ipv4,
|
||||||
|
address_v4: cast[array[4, uint8]](ipSrcScalar))
|
||||||
|
let ipDst = IpAddress(family: Ipv4,
|
||||||
|
address_v4: cast[array[4, uint8]](ipDstScalar))
|
||||||
if ipHeader.ip_p.int == IPPROTO_TCP:
|
if ipHeader.ip_p.int == IPPROTO_TCP:
|
||||||
let tcpHeader = cast[ptr Tcphdr](cast[int](ipHeader) + ipHeader.ip_hl.int * 4)
|
let tcpHeader = cast[ptr Tcphdr](cast[int](ipHeader) + ipHeader.ip_hl.int * 4)
|
||||||
result = IpPacket(protocol: tcp,
|
result = IpPacket(protocol: tcp,
|
||||||
tcpIpSrc: ipSrc,
|
tcpIpSrc: ipSrc,
|
||||||
tcpIpDst: ipDst,
|
tcpIpDst: ipDst,
|
||||||
tcpPortSrc: Port(ntohs(tcpHeader.th_sport)),
|
tcpPortSrc: Port(ntohs(tcpHeader.th_sport)),
|
||||||
tcpPortDst: Port(ntohs(tcpHeader.th_dport)),
|
tcpPortDst: Port(ntohs(tcpHeader.th_dport)),
|
||||||
tcpSeqNumber: ntohl(tcpHeader.th_seq))
|
tcpSeqNumber: ntohl(tcpHeader.th_seq))
|
||||||
else:
|
else:
|
||||||
result = IpPacket(protocol: other)
|
result = IpPacket(protocol: other)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import asyncdispatch, asyncnet, strformat
|
import asyncdispatch, asyncnet, strformat
|
||||||
from net import IpAddress, Port, `$`, getPrimaryIPAddr
|
from net import IpAddress, Port, `$`, `==`, getPrimaryIPAddr
|
||||||
from nativesockets import setSockOptInt
|
from nativesockets import setSockOptInt
|
||||||
import asyncutils
|
import asyncutils
|
||||||
import ip_packet
|
import ip_packet
|
||||||
|
@ -68,9 +68,9 @@ proc captureSeqNumbers(puncher: TcpSyniPuncher, rawFd: AsyncFD,
|
||||||
echo "packet len: ", packet.len
|
echo "packet len: ", packet.len
|
||||||
let parsed = parseEthernetPacket(packet)
|
let parsed = parseEthernetPacket(packet)
|
||||||
if parsed.protocol == tcp and
|
if parsed.protocol == tcp and
|
||||||
parsed.tcpIpSrc == $puncher.srcIp and
|
parsed.tcpIpSrc == puncher.srcIp and
|
||||||
parsed.tcpPortSrc.int == puncher.srcPort.int and
|
parsed.tcpPortSrc.int == puncher.srcPort.int and
|
||||||
parsed.tcpIpDst == $puncher.dstIp:
|
parsed.tcpIpDst == puncher.dstIp:
|
||||||
for i, port in puncher.dstPorts.pairs:
|
for i, port in puncher.dstPorts.pairs:
|
||||||
if parsed.tcpPortDst.int == port.int:
|
if parsed.tcpPortDst.int == port.int:
|
||||||
seqNums.add(parsed.tcpSeqNumber)
|
seqNums.add(parsed.tcpSeqNumber)
|
||||||
|
|
Loading…
Reference in New Issue