Documentation
¶
Overview ¶
Package geo looks a client's country up in a country table (design 0011 §5.1): the sorted IP ranges, each with a country code, that the operator compiles a country database (MMDB) into and sends to engines (inkwall.geo.v1.CountryTable).
A Table holds the ranges in pointer-free slices, one per family, so the garbage collector never scans them, and an index on the top 16 bits of the address narrows the binary search to a few steps: a lookup takes well under 150 ns at the bound of 32 MiB, with no allocation. A Table is checked completely when it is loaded (Check), and trusted after: a bad table can only give a wrong country, never a crash.
Index ¶
Constants ¶
const ( // MaxRangeBytes bounds the ranges: 6 bytes per IPv4 range (a start and // a code) and 18 per IPv6 range. 5.5 million IPv4 ranges, or 1.8 // million IPv6 ranges, or any mix in between. MaxRangeBytes = 32 << 20 // MaxAliases bounds the IPv6 prefixes that alias the IPv4 table. // MaxMind's databases have three. MaxAliases = 256 // MaxDatabaseType bounds the database type, in bytes. MaxDatabaseType = 256 // MaxTableBytes bounds a serialized CountryTable: the ranges and their // framing (field tags, lengths, the metadata, the aliases). MaxTableBytes = MaxRangeBytes + 64<<10 // MaxCompressedBytes bounds the gzipped table, as it is sent and // stored: 16 Secrets of 900 KiB (pkg/bundlesecret). MaxCompressedBytes = 16 * 900 << 10 )
Bounds of a table (design 0011 §4.2).
Variables ¶
var ErrHash = errors.New("country table: the bytes don't match the table hash")
ErrHash is returned when a table's bytes don't match the hash that names them.
Functions ¶
func Check ¶
func Check(m *geov1.CountryTable) error
Check checks a table completely: the lengths match, the starts are strictly increasing from zero, the codes are two letters or none, the aliases are prefixes of /1 to /96 outside ::/96, sorted and disjoint, and the table is within the bounds.
func Encode ¶
func Encode(m *geov1.CountryTable) (compressed []byte, size int, err error)
Encode checks m and returns it serialized and gzipped, as it is sent and stored, and its serialized size (what a bundle's GeoTableRef names). The same m gives the same bytes, so the same database gives the same table hash.
Types ¶
type Country ¶
type Country uint16
Country is a two-letter country code, (c1 << 8) | c2 with each letter 'A' to 'Z': DE is 'D'<<8 | 'E'. The zero Country is no country.
const NoCountry Country = 0
NoCountry is the Country of an address the table has no country for.
func ParseCountry ¶
ParseCountry returns the Country of s, two upper-case ASCII letters, and whether s is one. Lower case is refused rather than folded, as the CRD refuses it.
type Result ¶
type Result uint8
Result is what a lookup found.
const ( // ResultCountry: the table maps the address to a country. ResultCountry Result = iota // ResultPrivate: no country, and the address is loopback, link-local, // private (RFC 1918, RFC 4193), shared (RFC 6598, CGNAT) or // unspecified, or there is no valid address at all. Geo never denies // it. ResultPrivate // ResultUnknown: no country for a public address. ResultUnknown // ResultNoTable: there is no table to look in. ResultNoTable )
Results of a lookup (design 0011 §2.2).
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set is a set of countries: a bit for each of the 26 × 26 two-letter codes, 88 bytes, so a policy's deny or allow list is one bit test. The zero Set is empty.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table is a checked country table. It is immutable and safe for concurrent use. The nil Table holds no countries (ResultNoTable).
func Decode ¶
Decode loads a gzipped table: it checks the bytes against hash, the table hash a signed bundle names, before it decompresses them, then decompresses through size, the serialized size the bundle names, and checks the table.
func FromProto ¶
func FromProto(m *geov1.CountryTable) (*Table, error)
FromProto checks m and returns its Table. The Table keeps m's slices: m must not change after.
func (*Table) DatabaseType ¶
DatabaseType is the type of the database the table was compiled from, e.g. GeoLite2-Country.
func (*Table) Hash ¶
Hash returns the table hash: the SHA-256 of the gzipped table, as Decode checked it. Zero for a Table made by FromProto.
func (*Table) Lookup ¶
Lookup returns a's country and what the lookup found. A country found always wins, even for a private address a custom database maps. An IPv4-mapped IPv6 address is its IPv4 address, and an address in ::/96 or in one of the table's aliases (MaxMind's ::ffff:0:0/96, 2001::/32 and 2002::/16) is looked up in the IPv4 table by the 32 bits after the prefix, as the database's own reader resolves it.