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
|
||||
|
||||
type
|
||||
|
@ -50,8 +51,8 @@ type
|
|||
IpPacket* = object
|
||||
case protocol*: Protocol
|
||||
of tcp:
|
||||
tcpIpSrc*: string
|
||||
tcpIpDst*: string
|
||||
tcpIpSrc*: IpAddress
|
||||
tcpIpDst*: IpAddress
|
||||
tcpPortSrc*: Port
|
||||
tcpPortDst*: Port
|
||||
tcpSeqNumber*: uint32
|
||||
|
@ -66,16 +67,20 @@ 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](input.cstring) + sizeof(Ether_header))
|
||||
let ipSrc = $inet_ntoa(ipHeader.ip_src)
|
||||
let ipDst = $inet_ntoa(ipHeader.ip_dst)
|
||||
let ipSrcScalar = ntohl(ipHeader.ip_src.s_addr)
|
||||
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:
|
||||
let tcpHeader = cast[ptr Tcphdr](cast[int](ipHeader) + ipHeader.ip_hl.int * 4)
|
||||
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))
|
||||
tcpIpSrc: ipSrc,
|
||||
tcpIpDst: ipDst,
|
||||
tcpPortSrc: Port(ntohs(tcpHeader.th_sport)),
|
||||
tcpPortDst: Port(ntohs(tcpHeader.th_dport)),
|
||||
tcpSeqNumber: ntohl(tcpHeader.th_seq))
|
||||
else:
|
||||
result = IpPacket(protocol: other)
|
||||
else:
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import asyncdispatch, asyncnet, strformat
|
||||
from net import IpAddress, Port, `$`, getPrimaryIPAddr
|
||||
from net import IpAddress, Port, `$`, `==`, getPrimaryIPAddr
|
||||
from nativesockets import setSockOptInt
|
||||
import asyncutils
|
||||
import ip_packet
|
||||
|
@ -68,9 +68,9 @@ proc captureSeqNumbers(puncher: TcpSyniPuncher, rawFd: AsyncFD,
|
|||
echo "packet len: ", packet.len
|
||||
let parsed = parseEthernetPacket(packet)
|
||||
if parsed.protocol == tcp and
|
||||
parsed.tcpIpSrc == $puncher.srcIp and
|
||||
parsed.tcpIpSrc == puncher.srcIp and
|
||||
parsed.tcpPortSrc.int == puncher.srcPort.int and
|
||||
parsed.tcpIpDst == $puncher.dstIp:
|
||||
parsed.tcpIpDst == puncher.dstIp:
|
||||
for i, port in puncher.dstPorts.pairs:
|
||||
if parsed.tcpPortDst.int == port.int:
|
||||
seqNums.add(parsed.tcpSeqNumber)
|
||||
|
|
Loading…
Reference in New Issue