use IpAddress instead of string

This commit is contained in:
Christian Ulrich 2020-07-22 01:28:55 +02:00
parent 44cc3955b5
commit 12ef92dfe9
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
2 changed files with 10 additions and 8 deletions

View File

@ -1,10 +1,11 @@
from nativesockets import getAddrString 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 type
NetworkInterface* = object NetworkInterface* = object
ipAddress*: string ipAddress*: IpAddress
netMask*: string netMask*: IpAddress
name*: string name*: string
index*: cint index*: cint
flags*: cuint flags*: cuint
@ -25,17 +26,18 @@ proc getifaddrs(ifap: ptr ptr Ifaddrs): int {.header: "<ifaddrs.h>", importc: "g
proc freeifaddrs(ifap: ptr Ifaddrs): void {.header: "<ifaddrs.h>", importc: "freeifaddrs".} proc freeifaddrs(ifap: ptr Ifaddrs): void {.header: "<ifaddrs.h>", importc: "freeifaddrs".}
proc if_nametoindex(ifname: cstring): cuint {.header: "<net/if.h>", importc: "if_nametoindex".} proc if_nametoindex(ifname: cstring): cuint {.header: "<net/if.h>", importc: "if_nametoindex".}
proc fromIpAddress*(address: string): NetworkInterface = proc fromIpAddress*(address: IpAddress): NetworkInterface =
var interfaces: ptr Ifaddrs var interfaces: ptr Ifaddrs
if getifaddrs(addr interfaces) != 0: if getifaddrs(addr interfaces) != 0:
raise newException(NetworkInterfaceError, "getifaddrs failed") raise newException(NetworkInterfaceError, "getifaddrs failed")
var it = interfaces var it = interfaces
while it != nil: while it != nil:
if it.ifa_addr != nil and if it.ifa_addr != nil and
it.ifa_addr.sa_family.cint == AF_INET and (it.ifa_addr.sa_family.cint == AF_INET or
it.ifa_addr.getAddrString() == address: it.ifa_addr.sa_family.cint == AF_INET6) and
$address == it.ifa_addr.getAddrString():
result.ipAddress = address result.ipAddress = address
result.netMask = it.ifa_netmask.getAddrString() result.netMask = parseIpAddress(it.ifa_addr.getAddrString())
result.name = $it.ifa_name result.name = $it.ifa_name
result.index = if_nametoindex(result.name).cint result.index = if_nametoindex(result.name).cint
result.flags = it.ifa_flags result.flags = it.ifa_flags

View File

@ -167,7 +167,7 @@ proc doAccept(puncher: TcpSyniPuncher, future: Future[AsyncSocket]) {.async.} =
proc connect*(puncher: TcpSyniPuncher, proc connect*(puncher: TcpSyniPuncher,
progressCb: PunchProgressCb): Future[AsyncSocket] = progressCb: PunchProgressCb): Future[AsyncSocket] =
result = newFuture[AsyncSocket]("tcp_syni.connect") result = newFuture[AsyncSocket]("tcp_syni.connect")
let iface = fromIpAddress($puncher.srcIp) let iface = fromIpAddress(puncher.srcIp)
let rawFd = setupEthernetCapturingSocket(iface) let rawFd = setupEthernetCapturingSocket(iface)
asyncCheck puncher.captureSeqNumbers(rawFd, progressCb) asyncCheck puncher.captureSeqNumbers(rawFd, progressCb)
for dstPort in puncher.dstPorts: for dstPort in puncher.dstPorts: