Properly count IPv4 and IPv6 networks listening (exclude link-local ones). Close #22

This commit is contained in:
Kleissner
2021-05-18 23:22:11 +02:00
parent 9eb1127b40
commit 8e2043728c
3 changed files with 33 additions and 7 deletions

View File

@@ -481,12 +481,11 @@ func isPacketSizeExceed(currentSize int, testSize int) bool {
return currentSize+testSize > udpMaxPacketSize-packetLengthMin
}
func selfFeatureSupport() (feature byte) {
// networksMutex not needed here
if len(networks4) > 0 {
func FeatureSupport() (feature byte) {
if countListen4 > 0 {
feature |= 1 << FeatureIPv4Listen
}
if len(networks6) > 0 {
if countListen6 > 0 {
feature |= 1 << FeatureIPv6Listen
}
return feature
@@ -514,7 +513,7 @@ createPacketLoop:
packetSize := announcementPayloadHeaderSize
raw[0] = byte(ProtocolVersion) // Protocol
raw[1] = selfFeatureSupport() // Feature support
raw[1] = FeatureSupport() // Feature support
//raw[2] = Actions // Action bit array
binary.LittleEndian.PutUint32(raw[3:7], BlockchainHeight)
binary.LittleEndian.PutUint64(raw[7:15], BlockchainVersion)
@@ -656,7 +655,7 @@ createPacketLoop:
packetSize := announcementPayloadHeaderSize
raw[0] = byte(ProtocolVersion) // Protocol
raw[1] = selfFeatureSupport() // Feature support
raw[1] = FeatureSupport() // Feature support
//raw[2] = Actions // Action bit array
binary.LittleEndian.PutUint32(raw[3:7], BlockchainHeight)
binary.LittleEndian.PutUint64(raw[7:15], BlockchainVersion)

View File

@@ -42,10 +42,14 @@ var networks4, networks6 []*Network
// single mutex for both network lists. Higher granularity currently not needed.
var networksMutex sync.RWMutex
// countListenX is the number of networks listened to, excluding link-local only listeners. This number might be different than len(networksN).
// This is useful to determine if there are any IPv4 or IPv6 listeners for potential external connections. This can be used to determine IPv4_LISTEN and IPv6_LISTEN.
var countListen4, countListen6 int64
// Default ports to use. This may be randomized in the future to prevent fingerprinting (and subsequent blocking) by corporate and ISP firewalls.
const defaultPort = 'p' // 112
// ReplyTimeout is the round-trip timeout.
// ReplyTimeout is the round-trip timeout for message sequences.
var ReplyTimeout = 20
// AutoAssignPort assigns a port for the given IP. Use port 0 for zero configuration.
@@ -103,6 +107,14 @@ const maxPacketSize = 65536
// Listen starts listening for incoming packets on the given UDP connection
func (network *Network) Listen() {
if !network.address.IP.IsLinkLocalUnicast() {
if IsIPv4(network.address.IP) {
atomic.AddInt64(&countListen4, 1)
} else {
atomic.AddInt64(&countListen6, 1)
}
}
for !network.isTerminated {
// Buffer: Must be created for each packet as it is passed as pointer.
// If the buffer is too small, ReadFromUDP only reads until its length and returns this error: "wsarecvfrom: A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself."
@@ -274,6 +286,14 @@ func (network *Network) Terminate() {
return
}
if !network.address.IP.IsLinkLocalUnicast() {
if IsIPv4(network.address.IP) {
atomic.AddInt64(&countListen4, -1)
} else {
atomic.AddInt64(&countListen6, -1)
}
}
// set the termination signal
network.isTerminated = true
close(network.terminateSignal) // safety guaranteed via lock

View File

@@ -111,6 +111,13 @@ If a bucket is full when a new peer connects `ShouldEvict` is called. It compare
* The default reply timeout (round-trip time) is 20 seconds set in `ReplyTimeout`. This applies to Response and Pong messages. The RTT timeout implies an average minimum connection speed between peers of about 6.4 KB/s for files of 64 KB size.
* Separate timeouts for file transfers will be established.
### Network Listen
Unless specified in the config via `Listen`, it will listen on all network adapters. The default port is 112, but that may be randomized in the future.
* Traffic between link-local unicast IPs and non link-local IPs is not allowed.
* UPnP is supported on IPv4 only for now.
## Contributing
Please note that by contributing code, documentation, ideas, snippets, or any other intellectual property you agree that you have all the necessary rights and you agree that we, the Peernet organization, may use it for any purpose.