File: ip_range_counter.go

package info (click to toggle)
tendermint-go-p2p 0.0~git20170113.0.3d98f67-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 248 kB
  • ctags: 425
  • sloc: sh: 23; makefile: 4
file content (29 lines) | stat: -rw-r--r-- 606 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package p2p

import (
	"strings"
)

// TODO Test
func AddToIPRangeCounts(counts map[string]int, ip string) map[string]int {
	changes := make(map[string]int)
	ipParts := strings.Split(ip, ":")
	for i := 1; i < len(ipParts); i++ {
		prefix := strings.Join(ipParts[:i], ":")
		counts[prefix] += 1
		changes[prefix] = counts[prefix]
	}
	return changes
}

// TODO Test
func CheckIPRangeCounts(counts map[string]int, limits []int) bool {
	for prefix, count := range counts {
		ipParts := strings.Split(prefix, ":")
		numParts := len(ipParts)
		if limits[numParts] < count {
			return false
		}
	}
	return true
}