From 12ef92dfe9416c0ad1f8c7269d38510645bbf6d8 Mon Sep 17 00:00:00 2001 From: Christian Ulrich Date: Wed, 22 Jul 2020 01:28:55 +0200 Subject: [PATCH] use IpAddress instead of string --- network_interface.nim | 16 +++++++++------- tcp_syni.nim | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/network_interface.nim b/network_interface.nim index 8fd38b1..c383d5f 100644 --- a/network_interface.nim +++ b/network_interface.nim @@ -1,10 +1,11 @@ from nativesockets import getAddrString -from posix import SockAddr, Sockaddr_in, inet_ntoa, AF_INET +from net import IpAddress, parseIpAddress, `$` +from posix import SockAddr, AF_INET, AF_INET6 type NetworkInterface* = object - ipAddress*: string - netMask*: string + ipAddress*: IpAddress + netMask*: IpAddress name*: string index*: cint flags*: cuint @@ -25,17 +26,18 @@ proc getifaddrs(ifap: ptr ptr Ifaddrs): int {.header: "", importc: "g proc freeifaddrs(ifap: ptr Ifaddrs): void {.header: "", importc: "freeifaddrs".} proc if_nametoindex(ifname: cstring): cuint {.header: "", importc: "if_nametoindex".} -proc fromIpAddress*(address: string): NetworkInterface = +proc fromIpAddress*(address: IpAddress): NetworkInterface = var interfaces: ptr Ifaddrs if getifaddrs(addr interfaces) != 0: raise newException(NetworkInterfaceError, "getifaddrs failed") var it = interfaces while it != nil: if it.ifa_addr != nil and - it.ifa_addr.sa_family.cint == AF_INET and - it.ifa_addr.getAddrString() == address: + (it.ifa_addr.sa_family.cint == AF_INET or + it.ifa_addr.sa_family.cint == AF_INET6) and + $address == it.ifa_addr.getAddrString(): result.ipAddress = address - result.netMask = it.ifa_netmask.getAddrString() + result.netMask = parseIpAddress(it.ifa_addr.getAddrString()) result.name = $it.ifa_name result.index = if_nametoindex(result.name).cint result.flags = it.ifa_flags diff --git a/tcp_syni.nim b/tcp_syni.nim index 5a9804f..01335c5 100644 --- a/tcp_syni.nim +++ b/tcp_syni.nim @@ -167,7 +167,7 @@ proc doAccept(puncher: TcpSyniPuncher, future: Future[AsyncSocket]) {.async.} = proc connect*(puncher: TcpSyniPuncher, progressCb: PunchProgressCb): Future[AsyncSocket] = result = newFuture[AsyncSocket]("tcp_syni.connect") - let iface = fromIpAddress($puncher.srcIp) + let iface = fromIpAddress(puncher.srcIp) let rawFd = setupEthernetCapturingSocket(iface) asyncCheck puncher.captureSeqNumbers(rawFd, progressCb) for dstPort in puncher.dstPorts: