2021-04-28 14:20:05 +00:00
|
|
|
# nim-eth
|
|
|
|
# Copyright (c) 2020-2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-11-26 17:20:15 +00:00
|
|
|
import
|
|
|
|
std/[tables, hashes],
|
2021-03-05 20:23:54 +00:00
|
|
|
stew/results, stew/shims/net as stewNet, chronos, chronicles
|
2020-11-26 17:20:15 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
IpLimits* = object
|
|
|
|
limit*: uint
|
|
|
|
ips: Table[ValidIpAddress, uint]
|
|
|
|
|
2021-03-05 20:23:54 +00:00
|
|
|
func hash(ip: ValidIpAddress): Hash = hash($ip)
|
2020-11-26 17:20:15 +00:00
|
|
|
|
2021-03-05 20:23:54 +00:00
|
|
|
func inc*(ipLimits: var IpLimits, ip: ValidIpAddress): bool =
|
2020-11-26 17:20:15 +00:00
|
|
|
let val = ipLimits.ips.getOrDefault(ip, 0)
|
|
|
|
if val < ipLimits.limit:
|
|
|
|
ipLimits.ips[ip] = val + 1
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
false
|
|
|
|
|
2021-03-05 20:23:54 +00:00
|
|
|
func dec*(ipLimits: var IpLimits, ip: ValidIpAddress) =
|
2020-11-26 17:20:15 +00:00
|
|
|
let val = ipLimits.ips.getOrDefault(ip, 0)
|
|
|
|
if val == 1:
|
|
|
|
ipLimits.ips.del(ip)
|
|
|
|
elif val > 1:
|
|
|
|
ipLimits.ips[ip] = val - 1
|
2021-03-02 16:13:29 +00:00
|
|
|
|
2021-03-05 20:23:54 +00:00
|
|
|
func isPublic*(address: TransportAddress): bool =
|
2021-03-02 16:13:29 +00:00
|
|
|
# TODO: Some are still missing, for special reserved addresses see:
|
|
|
|
# https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
|
|
|
|
# https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
|
|
|
|
if address.isLoopback() or address.isSiteLocal() or
|
|
|
|
address.isMulticast() or address.isLinkLocal():
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
true
|
|
|
|
|
2021-03-05 20:23:54 +00:00
|
|
|
func isPublic*(address: IpAddress): bool =
|
2021-03-02 16:13:29 +00:00
|
|
|
let a = initTAddress(address, Port(0))
|
|
|
|
a.isPublic
|
2021-03-05 20:23:54 +00:00
|
|
|
|
|
|
|
proc getRouteIpv4*(): Result[ValidIpAddress, cstring] =
|
|
|
|
# Avoiding Exception with initTAddress and can't make it work with static.
|
|
|
|
# Note: `publicAddress` is only used an "example" IP to find the best route,
|
|
|
|
# no data is send over the network to this IP!
|
|
|
|
let
|
|
|
|
publicAddress = TransportAddress(family: AddressFamily.IPv4,
|
|
|
|
address_v4: [1'u8, 1, 1, 1], port: Port(0))
|
|
|
|
route = getBestRoute(publicAddress)
|
|
|
|
|
|
|
|
if route.source.isUnspecified():
|
|
|
|
err("No best ipv4 route found")
|
|
|
|
else:
|
|
|
|
let ip = try: route.source.address()
|
|
|
|
except ValueError as e:
|
|
|
|
# This should not occur really.
|
|
|
|
error "Address convertion error", exception = e.name, msg = e.msg
|
|
|
|
return err("Invalid IP address")
|
|
|
|
ok(ValidIpAddress.init(ip))
|