2020-07-10 19:36:41 +02:00
|
|
|
from nativesockets import ntohs, ntohl, htons, htonl
|
|
|
|
from net import IpAddress, IpAddressFamily, Port, `$`
|
2020-05-21 01:17:37 +02:00
|
|
|
from posix import InAddr, inet_ntoa
|
2020-07-18 12:38:21 +02:00
|
|
|
import unittest
|
2020-05-21 01:17:37 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
Ether_header {.importc: "struct ether_header", pure, final,
|
|
|
|
header: "<netinet/if_ether.h>".} = object
|
2020-07-10 00:28:17 +02:00
|
|
|
ether_dhost: array[6, uint8]
|
|
|
|
ether_shost: array[6, uint8]
|
2020-05-21 01:17:37 +02:00
|
|
|
ether_type: cushort
|
|
|
|
|
|
|
|
Ip {.importc: "struct ip", pure, final,
|
|
|
|
header: "<netinet/ip.h>".} = object
|
|
|
|
when cpuEndian == littleEndian:
|
2020-07-10 00:28:17 +02:00
|
|
|
ip_hl {.bitsize:4.}: uint # header length
|
|
|
|
ip_v {.bitsize:4.}: uint # version
|
2020-05-21 01:17:37 +02:00
|
|
|
else:
|
2020-07-10 00:28:17 +02:00
|
|
|
ip_v {.bitsize:4.}: uint # version
|
|
|
|
ip_hl {.bitsize:4.}: uint # header length
|
|
|
|
ip_tos: uint8 # type of service
|
|
|
|
ip_len: cushort # total length
|
2020-05-21 01:17:37 +02:00
|
|
|
ip_id: cushort # identification
|
2020-07-10 00:28:17 +02:00
|
|
|
ip_off: cushort # fragment offset field
|
|
|
|
ip_ttl: uint8 # time to live
|
2020-05-21 01:17:37 +02:00
|
|
|
ip_p: cuchar # protocol
|
|
|
|
ip_sum: cushort # checksum
|
|
|
|
ip_src: InAddr # source address
|
|
|
|
ip_dst: InAddr # destination address
|
|
|
|
|
|
|
|
Tcphdr {.importc: "struct tcphdr", pure, final,
|
|
|
|
header: "<netinet/tcp.h>".} = object
|
2020-07-10 00:28:17 +02:00
|
|
|
th_sport: uint16 # source port
|
|
|
|
th_dport: uint16 # destination port
|
2020-05-21 01:17:37 +02:00
|
|
|
th_seq: uint32 # sequence number
|
|
|
|
th_ack: uint32 # ackkowledgment number
|
|
|
|
when cpuEndian == littleEndian:
|
2020-07-10 00:28:17 +02:00
|
|
|
th_x2 {.bitsize:4.}: uint8 # (unused)
|
|
|
|
th_off {.bitsize:4.}: uint8 # data offset
|
2020-05-21 01:17:37 +02:00
|
|
|
else:
|
2020-07-10 00:28:17 +02:00
|
|
|
th_off {.bitsize:4.}: uint8 # (unused)
|
|
|
|
th_x2 {.bitsize:4.}: uint8 # data offset
|
|
|
|
th_flags: uint8 # flags
|
|
|
|
th_win: uint16 # window
|
|
|
|
th_sum: uint16 # checksum
|
|
|
|
th_urp: uint16 # urgent pointer
|
2020-05-21 01:17:37 +02:00
|
|
|
|
2020-10-24 18:43:10 +02:00
|
|
|
TransportProtocol* = enum
|
2020-05-21 01:17:37 +02:00
|
|
|
tcp
|
|
|
|
other
|
|
|
|
|
2020-07-17 01:01:01 +02:00
|
|
|
TcpFlag* {.size: sizeof(uint8).} = enum
|
|
|
|
FIN
|
|
|
|
SYN
|
|
|
|
RST
|
|
|
|
PSH
|
|
|
|
ACK
|
|
|
|
URG
|
|
|
|
|
2020-07-09 21:10:25 +02:00
|
|
|
IpPacket* = object
|
2020-08-23 14:36:19 +02:00
|
|
|
ipAddrSrc*: IpAddress
|
|
|
|
ipAddrDst*: IpAddress
|
|
|
|
ipTTL*: uint8
|
2020-10-24 18:43:10 +02:00
|
|
|
case protocol*: TransportProtocol
|
2020-05-21 01:17:37 +02:00
|
|
|
of tcp:
|
|
|
|
tcpPortSrc*: Port
|
|
|
|
tcpPortDst*: Port
|
|
|
|
tcpSeqNumber*: uint32
|
2020-08-23 22:38:10 +02:00
|
|
|
tcpAckNumber*: uint32
|
2020-07-17 01:01:01 +02:00
|
|
|
tcpFlags*: set[TcpFlag]
|
2020-08-23 22:48:34 +02:00
|
|
|
tcpWindowSize*: uint16
|
2020-05-21 01:17:37 +02:00
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
var
|
|
|
|
ETHERTYPE_IP {.importc: "ETHERTYPE_IP", header: "<netinet/if_ether.h>".}: cushort
|
|
|
|
IPPROTO_TCP {.importc: "IPPROTO_TCP", header: "<netinet/in.h>".}: cint
|
|
|
|
|
2020-07-09 21:10:25 +02:00
|
|
|
proc parseEthernetPacket*(input: string): IpPacket =
|
|
|
|
let etherHeader = cast[ptr Ether_header](input.cstring)
|
2020-05-21 01:17:37 +02:00
|
|
|
if ntohs(etherHeader.ether_type) == ETHERTYPE_IP:
|
2020-07-09 21:10:25 +02:00
|
|
|
let ipHeader = cast[ptr Ip](cast[int](input.cstring) + sizeof(Ether_header))
|
2020-07-13 09:38:58 +02:00
|
|
|
let ipSrcScalar = ipHeader.ip_src.s_addr
|
|
|
|
let ipDstScalar = ipHeader.ip_dst.s_addr
|
2020-07-10 19:36:41 +02:00
|
|
|
let ipSrc = IpAddress(family: Ipv4,
|
|
|
|
address_v4: cast[array[4, uint8]](ipSrcScalar))
|
|
|
|
let ipDst = IpAddress(family: Ipv4,
|
|
|
|
address_v4: cast[array[4, uint8]](ipDstScalar))
|
2020-05-21 01:17:37 +02:00
|
|
|
if ipHeader.ip_p.int == IPPROTO_TCP:
|
|
|
|
let tcpHeader = cast[ptr Tcphdr](cast[int](ipHeader) + ipHeader.ip_hl.int * 4)
|
2020-07-17 01:01:01 +02:00
|
|
|
|
2020-07-09 21:10:25 +02:00
|
|
|
result = IpPacket(protocol: tcp,
|
2020-08-23 14:36:19 +02:00
|
|
|
ipAddrSrc: ipSrc,
|
|
|
|
ipAddrDst: ipDst,
|
|
|
|
ipTTL: ipHeader.ip_ttl,
|
2020-07-10 19:36:41 +02:00
|
|
|
tcpPortSrc: Port(ntohs(tcpHeader.th_sport)),
|
|
|
|
tcpPortDst: Port(ntohs(tcpHeader.th_dport)),
|
2020-07-17 01:01:01 +02:00
|
|
|
tcpSeqNumber: ntohl(tcpHeader.th_seq),
|
2020-08-23 22:38:10 +02:00
|
|
|
tcpAckNumber: ntohl(tcpHeader.th_ack),
|
2020-08-23 22:48:34 +02:00
|
|
|
tcpFlags: cast[set[TcpFlag]](tcpHeader.th_flags),
|
|
|
|
tcpWindowSize: ntohs(tcpHeader.th_win))
|
2020-05-21 01:17:37 +02:00
|
|
|
else:
|
2020-07-09 21:10:25 +02:00
|
|
|
result = IpPacket(protocol: other)
|
2020-05-21 01:17:37 +02:00
|
|
|
else:
|
2020-07-09 21:10:25 +02:00
|
|
|
result = IpPacket(protocol: other)
|
2020-07-10 19:37:47 +02:00
|
|
|
|
2020-07-18 12:38:21 +02:00
|
|
|
proc tcpChecksum(buffer: string): uint16 =
|
|
|
|
let ip = cast[ptr Ip](buffer.cstring)
|
|
|
|
var tcp = cast[ptr uint16](cast[ByteAddress](ip) + ip.ip_hl.int * 4)
|
|
|
|
var tcpLen = ntohs(ip.ip_len) - ip.ip_hl.uint16 * 4
|
|
|
|
var checksum = 0.uint32
|
|
|
|
# Add source IP address
|
|
|
|
checksum = checksum + (ntohl(ip.ip_src.s_addr) shr 16 and 0xFFFF).uint16
|
|
|
|
checksum = checksum + (ntohl(ip.ip_src.s_addr) and 0xFFFF).uint16
|
|
|
|
# Add dest IP address
|
|
|
|
checksum = checksum + (ntohl(ip.ip_dst.s_addr) shr 16 and 0xFFFF).uint16
|
|
|
|
checksum = checksum + (ntohl(ip.ip_dst.s_addr) and 0xFFFF).uint16
|
|
|
|
# Add protocol and reserved
|
|
|
|
checksum = checksum + ip.ip_p.uint16
|
|
|
|
# Add length of the IP payload
|
|
|
|
checksum = checksum + tcpLen
|
|
|
|
# Add IP payload
|
|
|
|
while tcpLen > 1:
|
|
|
|
checksum = checksum + ntohs(tcp[])
|
|
|
|
tcp = cast[ptr uint16](cast[ByteAddress](tcp) + sizeof(uint16))
|
|
|
|
tcpLen = tcpLen - sizeof(uint16).uint16
|
2020-07-18 12:42:49 +02:00
|
|
|
# Add leftover byte
|
2020-07-18 12:38:21 +02:00
|
|
|
if tcpLen != 0:
|
|
|
|
checksum = checksum + cast[ptr uint8](tcp)[]
|
|
|
|
# Fold 16-bit segments of the 32-bit checksum and add carry bit. Result is the
|
|
|
|
# one's complement.
|
|
|
|
checksum = (checksum and 0xFFFF).uint16 + (checksum shr 16).uint16
|
|
|
|
checksum = checksum + checksum shr 16
|
|
|
|
result = not checksum.uint16
|
|
|
|
|
2020-07-10 19:37:47 +02:00
|
|
|
proc serialize*(packet: IpPacket): string =
|
|
|
|
case packet.protocol
|
|
|
|
of tcp:
|
2020-07-18 12:38:21 +02:00
|
|
|
result = newString(sizeof(Ip) + sizeof(Tcphdr))
|
|
|
|
zeroMem(result.cstring, result.len)
|
2020-08-23 14:36:19 +02:00
|
|
|
let srcIp = InAddr(s_addr: cast[uint32](packet.ipAddrSrc.address_v4))
|
|
|
|
let dstIp = InAddr(s_addr: cast[uint32](packet.ipAddrDst.address_v4))
|
2020-07-18 12:38:21 +02:00
|
|
|
|
|
|
|
let ipHeader = cast[ptr Ip](addr result[0])
|
|
|
|
ipHeader.ip_hl = 5
|
|
|
|
ipHeader.ip_v = 4
|
|
|
|
ipHeader.ip_tos = 0
|
|
|
|
ipHeader.ip_len = htons(sizeof(Ip).uint16 + sizeof(TcpHdr).uint16)
|
|
|
|
ipHeader.ip_id = htons(54321) # FIXME = random number
|
|
|
|
ipHeader.ip_off = 0
|
2020-08-23 14:36:19 +02:00
|
|
|
ipHeader.ip_ttl = packet.ipTTL
|
2020-07-18 12:38:21 +02:00
|
|
|
ipHeader.ip_p = 6.cuchar
|
|
|
|
ipHeader.ip_src = srcIp
|
|
|
|
ipHeader.ip_dst = dstIp
|
|
|
|
|
|
|
|
let tcpHeader = cast[ptr Tcphdr](addr result[sizeof(Ip)])
|
|
|
|
tcpHeader.th_sport = htons(packet.tcpPortSrc.uint16)
|
|
|
|
tcpHeader.th_dport = htons(packet.tcpPortDst.uint16)
|
|
|
|
tcpHeader.th_seq = htonl(packet.tcpSeqNumber)
|
2020-08-23 22:38:10 +02:00
|
|
|
tcpHeader.th_ack = htonl(packet.tcpAckNumber)
|
2020-07-18 12:38:21 +02:00
|
|
|
tcpHeader.th_off = 5
|
|
|
|
tcpHeader.th_flags = cast[uint8](packet.tcpFlags)
|
2020-08-23 22:48:34 +02:00
|
|
|
tcpHeader.th_win = htons(packet.tcpWindowSize)
|
2020-07-18 12:38:21 +02:00
|
|
|
tcpHeader.th_urp = 0
|
|
|
|
tcpHeader.th_sum = htons(tcpChecksum(result))
|
|
|
|
|
2020-07-10 19:37:47 +02:00
|
|
|
else:
|
|
|
|
raise newException(ValueError, "protocol not supported")
|
2020-07-18 12:38:21 +02:00
|
|
|
|
|
|
|
suite "ip_packet tests":
|
|
|
|
setup:
|
|
|
|
var
|
|
|
|
ipHeader = Ip(ip_hl: 5, ip_v: 4, ip_tos: 0, ip_len: htons(40),
|
|
|
|
ip_id: htons(54321), ip_off: 0, ip_ttl: 64, ip_p: 6.cuchar,
|
|
|
|
ip_src: InAddr(s_addr: htonl(0x5abb2bd9.uint32)),
|
|
|
|
ip_dst: InAddr(s_addr: htonl(0x0a000069.uint32)))
|
|
|
|
tcpHeader = Tcphdr(th_sport: htons(4321), th_dport: htons(1234),
|
|
|
|
th_seq: htonl(345364), th_ack: 0, th_off: 5,
|
|
|
|
th_flags: cast[uint8]({SYN}), th_win: htons(1452 * 10),
|
|
|
|
th_urp: 0)
|
|
|
|
|
|
|
|
test "tcpChecksum":
|
|
|
|
var buffer = newString(sizeof(Ip) + sizeof(Tcphdr))
|
|
|
|
zeroMem(addr buffer[0], buffer.len)
|
|
|
|
copyMem(addr buffer[0], addr ipHeader, sizeof(Ip))
|
|
|
|
copyMem(addr buffer[sizeof(Ip)], addr tcpHeader, sizeof(Tcphdr))
|
|
|
|
check(tcpChecksum(buffer) == 35681)
|