systemd-resolved: answer with the generated contract's own types (review r1 C-6, C-8)

C-6: import noderesolver (mesh-networking's seat definition) at the zero
version; routes, route, unroute and uplink-names answer with its
RoutesAnswer, RouteAnswer, UnrouteAnswer and UplinkNamesAnswer, never a
map or a parallel struct, and uplink-names' input names is a JSON array
(arrayArg), never a comma- or space-split string.

C-8: the names file write's 15 s is the stated constant
WriteUplinkNamesBound, not an inline literal.

Builds: design 08 sign-in-page §2, §5, §6, §9, DQ-12, C-6, C-8
This commit is contained in:
jochen
2026-11-24 22:00:15 +01:00
parent ae7d0f874b
commit 717f8ec2e5
6 changed files with 121 additions and 45 deletions
@@ -27,8 +27,27 @@ import (
"strconv"
"strings"
"time"
noderesolver "git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/noderesolver"
)
// rfc3339 is a time as the generated contract's answers hold one, RFC 3339, empty for the zero time
// (design 08 sign-in-page C-6, DQ-12).
func rfc3339(t time.Time) string {
if t.IsZero() {
return ""
}
return t.UTC().Format(time.RFC3339)
}
// rfc3339Ptr is the same for a time that may not have happened yet.
func rfc3339Ptr(t *time.Time) string {
if t == nil {
return ""
}
return rfc3339(*t)
}
// Runner runs one command — as root when it changes something — and answers what it printed.
type Runner func(ctx context.Context, name string, args ...string) (string, error)
@@ -288,10 +307,10 @@ func (r *Resolver) Routes(ctx context.Context) (*Routes, error) {
return out, nil
}
// UplinkNamesAnswer is `routes`'s `uplink_names` field (design 08 sign-in-page §2, §9; DQ-3): the applied
// list's names that resolved holds on that link now, never the file's, the link, and resolved's refusal or
// empty.
func (r *Resolver) UplinkNamesAnswer(routes *Routes) map[string]any {
// UplinkNamesField is `routes`'s `uplink_names` field, the generated contract's own type (design 08
// sign-in-page §2, §9, C-6; DQ-3, DQ-12): the applied list's names that resolved holds on that link now,
// never the file's, the link, and resolved's refusal or empty.
func (r *Resolver) UplinkNamesField(routes *Routes) noderesolver.UplinkNames {
applied := ReadUplinkApplied(r.UplinkNamesAppliedFile)
held := []string{}
for _, l := range routes.Links {
@@ -308,14 +327,58 @@ func (r *Resolver) UplinkNamesAnswer(routes *Routes) map[string]any {
}
}
}
return map[string]any{"names": held, "link": applied.Link, "refused": applied.Refused}
return noderesolver.UplinkNames{Names: held, Link: applied.Link, Refused: applied.Refused}
}
// MeshResolversAnswer is `routes`'s `mesh_resolvers` field (design 08 sign-in-page §9; S-N5): whether the
// mesh's resolvers are silent now, by ADR 0688's guard, and since when.
func (r *Resolver) MeshResolversAnswer() map[string]any {
// MeshResolversField is `routes`'s `mesh_resolvers` field, the generated contract's own type (design 08
// sign-in-page §9, C-6; S-N5, DQ-12): whether the mesh's resolvers are silent now, by ADR 0688's guard, and
// since when.
func (r *Resolver) MeshResolversField() noderesolver.MeshResolvers {
s := ReadFallback(r.FallbackFile)
return map[string]any{"silent": s.Silent, "since": s.Since}
return noderesolver.MeshResolvers{Silent: s.Silent, Since: rfc3339(s.Since)}
}
// ScopeField is one scope as the generated contract answers it (design 08 sign-in-page C-6, DQ-12).
func ScopeField(s Scope) noderesolver.Scope {
return noderesolver.Scope{Link: s.Link, Servers: orEmpty(s.Servers), Domains: orEmpty(s.Domains),
DefaultRoute: s.DefaultRoute != nil && *s.DefaultRoute}
}
// ScopeFields is every scope as the generated contract answers them.
func ScopeFields(ss []Scope) []noderesolver.Scope {
out := make([]noderesolver.Scope, len(ss))
for i, s := range ss {
out[i] = ScopeField(s)
}
return out
}
// OutsideWriteField is one outside write as the generated contract answers it.
func OutsideWriteField(d Displacement) noderesolver.OutsideWrite {
return noderesolver.OutsideWrite{ID: d.ID, At: rfc3339(d.At), Writer: d.Writer, TakenBy: d.TakenBy,
TakenAt: rfc3339Ptr(d.TakenAt), Ended: rfc3339Ptr(d.Ended), How: d.How}
}
// OutsideWriteFields is every outside write as the generated contract answers them.
func OutsideWriteFields(ds []Displacement) []noderesolver.OutsideWrite {
out := make([]noderesolver.OutsideWrite, len(ds))
for i, d := range ds {
out[i] = OutsideWriteField(d)
}
return out
}
// RoutesAnswer is `routes`'s whole answer, the generated contract's own type (design 08 sign-in-page §2,
// §5, §9, C-6; DQ-12): what this machine's own resolver sends where, the resolver file's outside writes,
// and the uplink's names and the mesh's fallback.
func (r *Resolver) RoutesAnswer(routes *Routes, outside []Displacement) noderesolver.RoutesAnswer {
return noderesolver.RoutesAnswer{
Mesh: ScopeField(routes.Mesh),
Links: ScopeFields(routes.Links),
OutsideWrites: OutsideWriteFields(outside),
UplinkNames: r.UplinkNamesField(routes),
MeshResolvers: r.MeshResolversField(),
}
}
// withoutMarked is domains without those marked (design 08 sign-in-page DMC-2).
@@ -337,12 +400,8 @@ func orEmpty(s []string) []string {
}
// Routed is what a route did.
type Routed struct {
Link string `json:"link"`
Servers []string `json:"servers"`
Domains []string `json:"domains"`
Said string `json:"said"`
}
// Routed is `route`'s answer, the generated contract's own type (design 08 sign-in-page C-6, DQ-12).
type Routed = noderesolver.RouteAnswer
var (
linkName = regexp.MustCompile(`^[A-Za-z0-9_.:@-]{1,15}$`)
@@ -501,12 +560,17 @@ func (r *Resolver) HostNames(given []string) ([]string, error) {
return out, nil
}
// WriteUplinkNamesBound is how long the names file's write (install, then mv) is given before it is
// abandoned (design 08 sign-in-page §6, C-8): a stated constant, since nothing here says it is a setting
// the machine differs on.
const WriteUplinkNamesBound = 15 * time.Second
// writeUplinkNamesFile writes UplinkNamesFile whole (design 08 sign-in-page §6): `sudo -n install -m 0644
// -o root -g root /dev/stdin <file>.new` with the lines on its input, then `sudo -n mv -f <file>.new
// <file>`, so a reader never sees it half-written. As root (the guard never calls this; only the verb,
// over the bus, does), both run unescalated.
func (r *Resolver) writeUplinkNamesFile(ctx context.Context, content []byte) error {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
ctx, cancel := context.WithTimeout(ctx, WriteUplinkNamesBound)
defer cancel()
newPath := r.UplinkNamesFile + ".new"
program, args := escalated(os.Getuid(), "install", []string{"-m", "0644", "-o", "root", "-g", "root", "/dev/stdin", newPath})
@@ -524,18 +588,10 @@ func (r *Resolver) writeUplinkNamesFile(ctx context.Context, content []byte) err
return err
}
// UplinkNames is `uplink-names`'s answer (design 08 sign-in-page §2): the names now given, the link they
// are answered over now (empty when no link carries a default route), and when the list was set.
type UplinkNames struct {
Names []string `json:"names"`
Link string `json:"link"`
SetAt time.Time `json:"set_at"`
}
// SetUplinkNames writes the host names the uplink's holder gives for its network's own check, through
// UplinkNamesFile (§6), and answers them with the link carrying the default route now (§2, DQ-2): empty
// when there is no such link.
func (r *Resolver) SetUplinkNames(ctx context.Context, given []string) (*UplinkNames, error) {
// UplinkNamesFile (§6), and answers them with the generated contract's own type (C-6, DQ-12): the link
// carrying the default route now (§2, DQ-2), empty when there is no such link, and when the list was set.
func (r *Resolver) SetUplinkNames(ctx context.Context, given []string) (*noderesolver.UplinkNamesAnswer, error) {
names, err := r.HostNames(given)
if err != nil {
return nil, err
@@ -551,7 +607,7 @@ func (r *Resolver) SetUplinkNames(ctx context.Context, given []string) (*UplinkN
if routes, rerr := r.Routes(ctx); rerr == nil {
link, _ = r.DefaultRouteLink(ctx, routes)
}
return &UplinkNames{Names: names, Link: link, SetAt: r.Now()}, nil
return &noderesolver.UplinkNamesAnswer{Names: names, Link: link, SetAt: rfc3339(r.Now())}, nil
}
// ReadUplinkNames reads the host names `uplink-names` last wrote, one a line; empty before it has.
@@ -694,11 +750,8 @@ func (r *Resolver) Route(ctx context.Context, link string, domains, servers []st
Said: fmt.Sprintf("%d domains go to %d servers over %s; every other name to the mesh's resolvers", len(ds), len(ss), link)}, nil
}
// Unrouted is what taking a route away did.
type Unrouted struct {
Link string `json:"link"`
Said string `json:"said"`
}
// Unrouted is `unroute`'s answer, the generated contract's own type (design 08 sign-in-page C-6, DQ-12).
type Unrouted = noderesolver.UnrouteAnswer
// Unroute takes one link's route away. A link that has gone has nothing to take away.
func (r *Resolver) Unroute(ctx context.Context, link string) (*Unrouted, error) {