Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4522b46616 | ||
|
|
616f13071f | ||
|
|
521c632087 |
@@ -144,8 +144,9 @@ routewatch/
|
||||
- **Reconnection**: Exponential backoff (5s-320s) with reset after 30s of stable connection
|
||||
- **IP Lookup**: the most specific live route is found by looking up the
|
||||
address's prefix at each mask length, longest first, in the prefix index
|
||||
(at most 33 lookups for IPv4, 129 for IPv6); prefixes are stored in the
|
||||
text form Go's `net/netip` prints
|
||||
(at most 25 lookups for IPv4, 113 for IPv6); prefixes are stored in the
|
||||
text form Go's `net/netip` prints. Routes shorter than /8 for IPv4 or /16
|
||||
for IPv6, such as a default route, are not used.
|
||||
|
||||
### Database Schema
|
||||
|
||||
|
||||
@@ -37,6 +37,14 @@ const (
|
||||
ipv6Bits = 128
|
||||
)
|
||||
|
||||
// The IP lookup does not use routes shorter than these mask lengths. A default
|
||||
// route or a route this short only says where a peer sends traffic it has no
|
||||
// better route for; it does not say who holds the address.
|
||||
const (
|
||||
minLookupMaskLengthV4 = 8
|
||||
minLookupMaskLengthV6 = 16
|
||||
)
|
||||
|
||||
// SQLite memory tuning. cache_size and busy_timeout go in the DSN so every
|
||||
// pooled connection gets them; the heap limits are process-wide and set once.
|
||||
const (
|
||||
@@ -1832,11 +1840,12 @@ func (d *Database) GetIPInfo(ip string) (*IPInfo, error) {
|
||||
// GetIPInfoContext returns comprehensive IP information with context support.
|
||||
//
|
||||
// It finds the most specific live route for the address with point lookups on
|
||||
// the prefix index: for each mask length from the longest (32 or 128) down to 0
|
||||
// it looks up the address's network prefix at that length, and the first length
|
||||
// with a live route wins. That is at most 33 or 129 indexed lookups, however
|
||||
// many routes are live. The prefix is looked up in the text form net/netip
|
||||
// prints, which is the form the prefix handler stores.
|
||||
// the prefix index: for each mask length from the longest (32 or 128) down to
|
||||
// minLookupMaskLengthV4 or minLookupMaskLengthV6 it looks up the address's
|
||||
// network prefix at that length, and the first length with a live route wins.
|
||||
// That is at most 25 or 113 indexed lookups, however many routes are live. The
|
||||
// prefix is looked up in the text form net/netip prints, which is the form the
|
||||
// prefix handler stores.
|
||||
func (d *Database) GetIPInfoContext(ctx context.Context, ip string) (*IPInfo, error) {
|
||||
addr, err := netip.ParseAddr(ip)
|
||||
if err != nil {
|
||||
@@ -1847,9 +1856,11 @@ func (d *Database) GetIPInfoContext(ctx context.Context, ip string) (*IPInfo, er
|
||||
|
||||
info := &IPInfo{IP: ip, IPVersion: ipVersionV6}
|
||||
routesTable, prefixesTable := "live_routes_v6", "prefixes_v6"
|
||||
minMaskLength := minLookupMaskLengthV6
|
||||
if addr.Is4() {
|
||||
info.IPVersion = ipVersionV4
|
||||
routesTable, prefixesTable = "live_routes_v4", "prefixes_v4"
|
||||
minMaskLength = minLookupMaskLengthV4
|
||||
}
|
||||
|
||||
// Route info with peer count and prefix first_seen
|
||||
@@ -1890,7 +1901,7 @@ func (d *Database) GetIPInfoContext(ctx context.Context, ip string) (*IPInfo, er
|
||||
var whoisUpdatedAt sql.NullTime
|
||||
|
||||
found := false
|
||||
for bits := addr.BitLen(); bits >= 0; bits-- {
|
||||
for bits := addr.BitLen(); bits >= minMaskLength; bits-- {
|
||||
prefix := netip.PrefixFrom(addr, bits).Masked().String()
|
||||
err := stmt.QueryRowContext(ctx, prefix).Scan(
|
||||
&info.Netblock,
|
||||
|
||||
@@ -133,6 +133,78 @@ func TestGetIPInfoFindsMostSpecificLiveRoute(t *testing.T) {
|
||||
lookup("2001:db8:1:2::3", "2001:db8:1::/48")
|
||||
}
|
||||
|
||||
// TestGetIPInfoIgnoresShortRoutes checks that the IP lookup does not report a
|
||||
// default route, or a route shorter than /8 for IPv4 or /16 for IPv6, as the
|
||||
// route for an address, and that a route of exactly /8 or /16, or longer, still
|
||||
// is. The prefixes are written out so that a wrong limit fails the test.
|
||||
func TestGetIPInfoIgnoresShortRoutes(t *testing.T) {
|
||||
cfg := &config.Config{StateDir: t.TempDir()}
|
||||
|
||||
db, err := New(cfg, logger.New())
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create database: %v", err)
|
||||
}
|
||||
defer func() { _ = db.Close() }()
|
||||
|
||||
ts := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||
announce := func(prefix string, asn int) {
|
||||
t.Helper()
|
||||
|
||||
err := db.UpsertLiveRouteBatch([]*LiveRoute{{
|
||||
ID: uuid.New(),
|
||||
Prefix: prefix,
|
||||
MaskLength: netip.MustParsePrefix(prefix).Bits(),
|
||||
IPVersion: detectIPVersion(prefix),
|
||||
OriginASN: asn,
|
||||
PeerIP: testPeerIP,
|
||||
ASPath: []int{asn},
|
||||
NextHop: testPeerIP,
|
||||
LastUpdated: ts,
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatalf("UpsertLiveRouteBatch(%s): %v", prefix, err)
|
||||
}
|
||||
}
|
||||
|
||||
// lookup checks that ip resolves to the live prefix netblock from asn.
|
||||
lookup := func(ip, netblock string, asn int) {
|
||||
t.Helper()
|
||||
|
||||
info, err := db.GetIPInfo(ip)
|
||||
if err != nil {
|
||||
t.Errorf("GetIPInfo(%s): %v", ip, err)
|
||||
|
||||
return
|
||||
}
|
||||
if info.Netblock != netblock || info.ASN != asn {
|
||||
t.Errorf("GetIPInfo(%s) = %s AS%d, want %s AS%d", ip, info.Netblock, info.ASN, netblock, asn)
|
||||
}
|
||||
}
|
||||
|
||||
// Default and very short routes, and routes one bit shorter than each limit.
|
||||
announce("2000::/3", 64500)
|
||||
announce("0.0.0.0/0", 64500)
|
||||
announce("2000::/15", 64500)
|
||||
announce("192.0.0.0/7", 64500)
|
||||
|
||||
for _, ip := range []string{"2001:db8::1", "192.0.2.1"} {
|
||||
if info, err := db.GetIPInfo(ip); !errors.Is(err, ErrNoRoute) {
|
||||
t.Errorf("GetIPInfo(%s) with only short routes = %+v, %v; want ErrNoRoute", ip, info, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Routes of exactly the minimum length are used.
|
||||
announce("2001::/16", 64501)
|
||||
announce("192.0.0.0/8", 64501)
|
||||
|
||||
lookup("2001:db8::1", "2001::/16", 64501)
|
||||
lookup("192.0.2.1", "192.0.0.0/8", 64501)
|
||||
|
||||
announce("2001:db8::/32", 64502)
|
||||
|
||||
lookup("2001:db8::1", "2001:db8::/32", 64502)
|
||||
}
|
||||
|
||||
// TestConnectionPoolPragmas holds several pooled connections open at once and
|
||||
// checks each one carries the per-connection settings from the DSN, plus the
|
||||
// process-wide hard heap limit, and sees the new file with auto_vacuum
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
@@ -748,8 +749,15 @@ func (s *Server) handlePrefixDetailJSON() http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
// Combine prefix and length into CIDR notation
|
||||
prefix := prefixParam + "/" + lenParam
|
||||
// Look the prefix up in the text form net/netip prints, the form the
|
||||
// tables store, so that any valid way of writing it finds the route.
|
||||
parsed, err := netip.ParsePrefix(prefixParam + "/" + lenParam)
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusBadRequest, err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
prefix := parsed.Masked().String()
|
||||
|
||||
routes, err := s.db.GetPrefixDetailsContext(r.Context(), prefix)
|
||||
if err != nil {
|
||||
@@ -912,8 +920,15 @@ func (s *Server) handlePrefixDetail() http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
// Combine prefix and length into CIDR notation
|
||||
prefix := prefixParam + "/" + lenParam
|
||||
// Look the prefix up in the text form net/netip prints, the form the
|
||||
// tables store, so that any valid way of writing it finds the route.
|
||||
parsed, err := netip.ParsePrefix(prefixParam + "/" + lenParam)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid prefix", http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
prefix := parsed.Masked().String()
|
||||
|
||||
routes, err := s.db.GetPrefixDetailsContext(r.Context(), prefix)
|
||||
if err != nil {
|
||||
|
||||
@@ -148,6 +148,58 @@ func TestStatsHandlersAnswerFromMemory(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestPrefixDetailHandlersParsePrefix checks that both prefix pages find a
|
||||
// stored route whatever valid text form the URL writes its prefix in, and
|
||||
// answer 400 for a prefix that does not parse. The tables store the form
|
||||
// net/netip prints. Before the fix the URL text was looked up as typed, so
|
||||
// "2001:DB8:0:0::/32" was not found although "2001:db8::/32" was stored, and
|
||||
// "foo/24" answered 500 (https://git.eeqj.de/sneak/routewatch/issues/71).
|
||||
func TestPrefixDetailHandlersParsePrefix(t *testing.T) {
|
||||
db, err := database.New(&config.Config{StateDir: t.TempDir()}, logger.New())
|
||||
if err != nil {
|
||||
t.Fatalf("database.New: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
|
||||
ts := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||
if err := db.UpsertLiveRouteBatch([]*database.LiveRoute{
|
||||
{
|
||||
ID: uuid.New(), Prefix: "192.0.2.0/24", MaskLength: 24, IPVersion: 4,
|
||||
OriginASN: 64500, PeerIP: "198.51.100.1", ASPath: []int{64500}, NextHop: "198.51.100.1",
|
||||
LastUpdated: ts,
|
||||
},
|
||||
{
|
||||
ID: uuid.New(), Prefix: "2001:db8::/32", MaskLength: 32, IPVersion: 6,
|
||||
OriginASN: 64501, PeerIP: "2001:db8::1", ASPath: []int{64501}, NextHop: "2001:db8::1",
|
||||
LastUpdated: ts,
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("UpsertLiveRouteBatch: %v", err)
|
||||
}
|
||||
|
||||
s := New(db, streamer.New(logger.New(), metrics.New()), logger.New(), &config.Config{})
|
||||
|
||||
tests := []struct {
|
||||
path string
|
||||
want int
|
||||
}{
|
||||
{"/api/v1/prefix/foo/24", http.StatusBadRequest},
|
||||
{"/prefix/foo/24", http.StatusBadRequest},
|
||||
{"/api/v1/prefix/2001:DB8:0:0::/32", http.StatusOK},
|
||||
{"/prefix/2001:DB8:0:0::/32", http.StatusOK},
|
||||
{"/api/v1/prefix/192.0.2.1/24", http.StatusOK},
|
||||
{"/prefix/192.0.2.1/24", http.StatusOK},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
rec := httptest.NewRecorder()
|
||||
s.router.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, tt.path, nil))
|
||||
if rec.Code != tt.want {
|
||||
t.Errorf("%s: status %d, want %d; body %s", tt.path, rec.Code, tt.want, rec.Body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// settledGoroutineCount lets transient goroutines finish, then reports the
|
||||
// current count.
|
||||
func settledGoroutineCount() int {
|
||||
|
||||
Reference in New Issue
Block a user