Dawnline 0.20.0
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / Backend (Go: vet, tests, a real ubusd and uhttpd) (push) Blocked by required conditions
CI / Package files (push) Blocked by required conditions

- **Backend rewritten in Go.** One daemon, `dawnlined`, replaces the whole ucode backend: the rpcd plugin, the agent, the CLI and the updater. The package no longer depends on ucode.
- **Per-architecture builds.** The release carries the backend for arm64, ARMv7, mips, mipsle and amd64; the router fetches its own after the package is installed and checks its sha256. Dawnline does not run on other architectures (older ARM, 32-bit x86, RISC-V).
- **Faster on the router.** A firewall sync takes 0.06 s instead of 0.9 s; a full apply takes about 300 ms. The portal is served by the daemon itself, without a CGI process per request.
- **Live pages.** The admin and the portal follow a live stream from the router instead of re-fetching the whole state every 30 seconds.
- **Admin redesigned in the portal's style.** Three sections (Overview · Log · Settings); a server is a page of its own with People, Places and Settings; a device opens as a dialog over its person.
- **Changes wait for «Apply».** Every setting, and a person's access, is staged in one dock before it reaches the router.
- **Configuration rollout.** After a change that makes devices fetch their configuration again, the admin shows how many have fetched it and who has not. Every setting shows beforehand (⟳N) how many devices it will touch.
- **Waiting for a decision.** Overview and People put first the people who need one (access ended, limit reached, a device that does not connect), each with its one action a tap away.
- **Bulk actions on people.** «Select» (or a long press on a card) picks several people to extend by a month, pause or resume at once.
- **Undo.** An action done from a card or a request is undone from its toast.
- **Update dialogs redesigned.** An update is one view from its question to its result; Dawnline's own update runs in it like every other, and the page reloads itself once the new version answers. Other versions are a list, each with its notes and Install on a page of its own.
- **Site to site: networks behind a router.** A router connected as a device can have networks behind it (up to eight, /16 to /30): the server routes them and applies the router's access to them. A network that shares addresses with a tunnel, the router's own networks or another site is refused by name.
- **Routers from the portal.** People can add a router and name its networks on their own page; the admin accepts or declines with a reason, or lets them through without asking.
- **Check access and Health know the networks behind routers.**
- **Usage limits reworked.** A monthly limit no longer follows the calendar. A person's month starts with their first traffic and ends a month later; the next one starts only when they connect again, so time away uses up none of it. Before, everyone's limit reset on the 1st.
- Extra data lasts until that person's month ends; «Reset usage» starts the month anew.
- A limit is for a month or for the whole term; the admin and the portal show when it resets.
- Charts that said «this month» show the last 30 days.
- After the update nobody loses what was counted: the month that was running becomes the person's first and ends on the 1st.
- **Access by time of day removed.** The Hours limit is gone from the admin, the portal and the backend; saved hours are dropped.
- **Expiry simplified.** An end date always stops access, with a heads-up three days before; the per-person choice is gone.
- **No false disconnects from sleeping phones.** A phone or a tablet stays online through ten minutes of silence.
- **Mobile Safari fixes.** Pressing a control no longer sends the page to its top; tab tracks that do not fit scroll; switches animate after a press.
This commit is contained in:
Aemeath committed 2026-11-13 09:43:50 +03:00
1 parent f8130e39a8
commit ac90575b0b
788 files changed
+139244 -41720

No files matched your search

+357
View File
@@ -0,0 +1,357 @@
// Package ubus speaks the wire protocol of ubusd, OpenWrt's message bus, so the
// backend can publish an object of its own and notify its subscribers without
// libubus. LuCI reaches that object through uhttpd (/ubus: calls as JSON-RPC,
// notifications as an event stream), with rpcd's access lists in between.
//
// This file is the encoding: libubox's blob attributes, which carry the
// protocol's own fields, and blobmsg, the JSON-like values inside them. It
// owns no socket; conn.go does.
package ubus
import (
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"math"
"sort"
)
// A blob attribute is a 4-byte big-endian word (flag, 7 bits of id, 24 bits of
// length counted from the word itself) and its payload, padded to 4 bytes.
const (
attrHeader = 4
attrExtended = 0x80000000
attrIDMask = 0x7f000000
attrIDShift = 24
attrLenMask = 0x00ffffff
)
// The types of a blobmsg value (the id of an extended attribute).
const (
msgNull = 0
msgArray = 1
msgTable = 2
msgString = 3
msgInt64 = 4
msgInt32 = 5
msgInt16 = 6
msgBool = 7
msgDouble = 8
)
var errTruncated = errors.New("ubus: truncated attribute")
func pad4(n int) int { return (n + 3) &^ 3 }
// open starts an attribute and returns where its header is, for closeAttr.
func open(b []byte) ([]byte, int) {
at := len(b)
return append(b, 0, 0, 0, 0), at
}
// closeAttr writes the header word of the attribute opened at `at` (`word` is
// its flag and id, the length is added here) and pads it.
func closeAttr(b []byte, at int, word uint32) []byte {
binary.BigEndian.PutUint32(b[at:], word|uint32(len(b)-at))
for len(b)%4 != 0 {
b = append(b, 0)
}
return b
}
func putBytes(b []byte, id uint32, p []byte) []byte {
b, at := open(b)
b = append(b, p...)
return closeAttr(b, at, id<<attrIDShift)
}
func putString(b []byte, id uint32, s string) []byte {
b, at := open(b)
b = append(b, s...)
b = append(b, 0)
return closeAttr(b, at, id<<attrIDShift)
}
func putUint32(b []byte, id uint32, v uint32) []byte {
b, at := open(b)
b = binary.BigEndian.AppendUint32(b, v)
return closeAttr(b, at, id<<attrIDShift)
}
func putUint8(b []byte, id uint32, v uint8) []byte {
b, at := open(b)
b = append(b, v)
return closeAttr(b, at, id<<attrIDShift)
}
// attr is one parsed attribute: its id, whether it is a blobmsg value, and its
// payload without the padding.
type attr struct {
id uint32
extended bool
data []byte
}
// next takes the first attribute off b. The lengths come from the other side
// of a socket: every one is checked against what is there.
func next(b []byte) (attr, []byte, error) {
if len(b) < attrHeader {
return attr{}, nil, errTruncated
}
w := binary.BigEndian.Uint32(b)
n := int(w & attrLenMask)
if n < attrHeader || n > len(b) {
return attr{}, nil, errTruncated
}
a := attr{id: (w & attrIDMask) >> attrIDShift, extended: w&attrExtended != 0, data: b[attrHeader:n]}
rest := pad4(n)
if rest > len(b) {
rest = len(b)
}
return a, b[rest:], nil
}
// each calls fn for every attribute in b.
func each(b []byte, fn func(attr) error) error {
for len(b) > 0 {
a, rest, err := next(b)
if err != nil {
return err
}
if err := fn(a); err != nil {
return err
}
b = rest
}
return nil
}
/* ---------- blobmsg: values ---------- */
// putValue appends one named blobmsg value. Values are what encoding/json
// makes of a document (nil, bool, float64, json.Number, string, []any,
// map[string]any) and Go's integers; anything else goes through encoding/json
// first, so a struct with json tags is sent the way it would be marshalled.
// Table keys are sorted: the same value always makes the same bytes.
func putValue(b []byte, name string, v any) ([]byte, error) {
switch x := v.(type) {
case nil:
b, at := putName(b, name)
return closeAttr(b, at, attrExtended|msgNull<<attrIDShift), nil
case bool:
b, at := putName(b, name)
if x {
b = append(b, 1)
} else {
b = append(b, 0)
}
return closeAttr(b, at, attrExtended|msgBool<<attrIDShift), nil
case string:
b, at := putName(b, name)
b = append(b, x...)
b = append(b, 0)
return closeAttr(b, at, attrExtended|msgString<<attrIDShift), nil
case int:
return putInt(b, name, int64(x)), nil
case int32:
return putInt(b, name, int64(x)), nil
case int64:
return putInt(b, name, x), nil
case uint32:
return putInt(b, name, int64(x)), nil
case uint64:
if x > math.MaxInt64 {
return putDouble(b, name, float64(x)), nil
}
return putInt(b, name, int64(x)), nil
case float64:
// A whole number is an integer on the bus, as it is in the JSON it came from.
if x == math.Trunc(x) && math.Abs(x) < 1<<53 {
return putInt(b, name, int64(x)), nil
}
return putDouble(b, name, x), nil
case json.Number:
if i, err := x.Int64(); err == nil {
return putInt(b, name, i), nil
}
f, err := x.Float64()
if err != nil {
return nil, fmt.Errorf("ubus: %q is not a number", string(x))
}
return putDouble(b, name, f), nil
case []any:
b, at := putName(b, name)
var err error
for _, e := range x {
if b, err = putValue(b, "", e); err != nil {
return nil, err
}
}
return closeAttr(b, at, attrExtended|msgArray<<attrIDShift), nil
case map[string]any:
b, at := putName(b, name)
var err error
if b, err = putFields(b, x); err != nil {
return nil, err
}
return closeAttr(b, at, attrExtended|msgTable<<attrIDShift), nil
}
raw, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("ubus: cannot encode %T: %w", v, err)
}
var plain any
if err := json.Unmarshal(raw, &plain); err != nil {
return nil, err
}
return putValue(b, name, plain)
}
// putName starts a blobmsg attribute: the header word (written by closeAttr),
// then the length of the name, the name and its NUL, padded to 4.
func putName(b []byte, name string) ([]byte, int) {
b, at := open(b)
b = binary.BigEndian.AppendUint16(b, uint16(len(name)))
b = append(b, name...)
b = append(b, 0)
for (len(b)-at)%4 != 0 {
b = append(b, 0)
}
return b, at
}
func putInt(b []byte, name string, v int64) []byte {
b, at := putName(b, name)
if v >= math.MinInt32 && v <= math.MaxInt32 {
b = binary.BigEndian.AppendUint32(b, uint32(int32(v)))
return closeAttr(b, at, attrExtended|msgInt32<<attrIDShift)
}
b = binary.BigEndian.AppendUint64(b, uint64(v))
return closeAttr(b, at, attrExtended|msgInt64<<attrIDShift)
}
func putDouble(b []byte, name string, v float64) []byte {
b, at := putName(b, name)
b = binary.BigEndian.AppendUint64(b, math.Float64bits(v))
return closeAttr(b, at, attrExtended|msgDouble<<attrIDShift)
}
// putFields appends the fields of a table, sorted by name.
func putFields(b []byte, m map[string]any) ([]byte, error) {
names := make([]string, 0, len(m))
for k := range m {
// The name's length is 16 bits on the wire.
if len(k) > math.MaxUint16 {
return nil, fmt.Errorf("ubus: field name of %d bytes", len(k))
}
names = append(names, k)
}
sort.Strings(names)
var err error
for _, k := range names {
if b, err = putValue(b, k, m[k]); err != nil {
return nil, err
}
}
return b, nil
}
// encodeTable is a table's fields as they sit inside a message's data attribute.
func encodeTable(m map[string]any) ([]byte, error) {
return putFields(make([]byte, 0, 256), m)
}
// A value nested deeper than this is refused: the bus carries documents, and a
// peer that sends tables inside tables without end is not sending one.
const maxDepth = 32
// decodeTable reads the fields of a table. Integers come back as int64,
// whatever their width on the wire.
func decodeTable(b []byte, depth int) (map[string]any, error) {
out := map[string]any{}
err := each(b, func(a attr) error {
name, v, err := decodeValue(a, depth)
if err != nil {
return err
}
out[name] = v
return nil
})
return out, err
}
func decodeValue(a attr, depth int) (string, any, error) {
if !a.extended || len(a.data) < 2 {
return "", nil, errors.New("ubus: not a blobmsg value")
}
n := int(binary.BigEndian.Uint16(a.data))
hdr := pad4(2 + n + 1)
if 2+n > len(a.data) {
return "", nil, errTruncated
}
name := string(a.data[2 : 2+n])
var d []byte
if hdr <= len(a.data) {
d = a.data[hdr:]
}
switch a.id {
case msgNull:
return name, nil, nil
case msgBool:
if len(d) < 1 {
return "", nil, errTruncated
}
return name, d[0] != 0, nil
case msgInt16:
if len(d) < 2 {
return "", nil, errTruncated
}
return name, int64(int16(binary.BigEndian.Uint16(d))), nil
case msgInt32:
if len(d) < 4 {
return "", nil, errTruncated
}
return name, int64(int32(binary.BigEndian.Uint32(d))), nil
case msgInt64:
if len(d) < 8 {
return "", nil, errTruncated
}
return name, int64(binary.BigEndian.Uint64(d)), nil
case msgDouble:
if len(d) < 8 {
return "", nil, errTruncated
}
return name, math.Float64frombits(binary.BigEndian.Uint64(d)), nil
case msgString:
// The NUL that ends it is part of the length.
for i, c := range d {
if c == 0 {
d = d[:i]
break
}
}
return name, string(d), nil
case msgTable:
if depth >= maxDepth {
return "", nil, errors.New("ubus: value nested too deep")
}
t, err := decodeTable(d, depth+1)
return name, t, err
case msgArray:
if depth >= maxDepth {
return "", nil, errors.New("ubus: value nested too deep")
}
list := []any{}
err := each(d, func(e attr) error {
_, v, err := decodeValue(e, depth+1)
if err != nil {
return err
}
list = append(list, v)
return nil
})
return name, list, err
}
return "", nil, fmt.Errorf("ubus: unknown blobmsg type %d", a.id)
}
+93
View File
@@ -0,0 +1,93 @@
package ubus
import (
"encoding/hex"
"reflect"
"testing"
)
// A table survives the wire: integers come back as int64, the rest as sent.
func TestTableRoundTrip(t *testing.T) {
in := map[string]any{
"name": "Myata",
"on": true,
"off": false,
"none": nil,
"small": 42,
"neg": -7,
"big": int64(5_000_000_000),
"whole": float64(3),
"frac": 1.5,
"list": []any{"a", int64(2), map[string]any{"k": "v"}},
"deep": map[string]any{"x": map[string]any{"y": []any{}}},
"": "an empty name",
}
b, err := encodeTable(in)
if err != nil {
t.Fatal(err)
}
if len(b)%4 != 0 {
t.Fatalf("%d bytes: not padded to 4", len(b))
}
got, err := decodeTable(b, 0)
if err != nil {
t.Fatal(err)
}
want := map[string]any{
"name": "Myata", "on": true, "off": false, "none": nil,
"small": int64(42), "neg": int64(-7), "big": int64(5_000_000_000), "whole": int64(3), "frac": 1.5,
"list": []any{"a", int64(2), map[string]any{"k": "v"}},
"deep": map[string]any{"x": map[string]any{"y": []any{}}},
"": "an empty name",
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %#v\nwant %#v", got, want)
}
}
// The bytes libubox writes for { "a": 1, "s": "hi" } (blobmsg_add_u32, blobmsg_add_string).
func TestKnownBytes(t *testing.T) {
b, err := encodeTable(map[string]any{"a": 1, "s": "hi"})
if err != nil {
t.Fatal(err)
}
want := "8500000c" + "00016100" + "00000001" + // int32 "a" = 1
"8300000b" + "00017300" + "68690000" // string "s" = "hi\0", 11 bytes padded to 12
if got := hex.EncodeToString(b); got != want {
t.Fatalf("got %s\nwant %s", got, want)
}
}
// A struct goes the way encoding/json would write it.
func TestStructThroughJSON(t *testing.T) {
type dev struct {
ID string `json:"id"`
Online bool `json:"online"`
Rx uint64 `json:"rx"`
}
b, err := encodeTable(map[string]any{"d": dev{"d1", true, 1 << 40}})
if err != nil {
t.Fatal(err)
}
got, err := decodeTable(b, 0)
if err != nil {
t.Fatal(err)
}
want := map[string]any{"d": map[string]any{"id": "d1", "online": true, "rx": int64(1 << 40)}}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %#v", got)
}
}
// Lengths that point past the end are an error, never a panic.
func TestTruncated(t *testing.T) {
b, _ := encodeTable(map[string]any{"name": "Myata", "t": map[string]any{"k": []any{1, 2, 3}}})
for n := 1; n < len(b); n++ {
decodeTable(b[:n], 0)
}
bad := append([]byte{}, b...)
bad[3] = 0xff
if _, err := decodeTable(bad, 0); err == nil {
t.Fatal("a length past the end was accepted")
}
}
+116
View File
@@ -0,0 +1,116 @@
// Bus keeps the backend's objects on ubus for as long as the backend runs.
// ubusd restarts (a package upgrade, a crash): the connection ends, and the
// objects are put back on the next one. Until then notifications are dropped
// and calls fail; nothing waits for the bus.
package ubus
import (
"context"
"sync"
"time"
)
// Bus is the backend's place on ubus, reconnected when it is lost.
type Bus struct {
path string
objects []*Object
// Log is told why a connection was not made or ended, once per change.
Log func(format string, args ...any)
mu sync.Mutex
conn *Conn
}
// NewBus prepares a bus at path (Socket on a router) with these objects.
func NewBus(path string, objects ...*Object) *Bus {
return &Bus{path: path, objects: objects, Log: func(string, ...any) {}}
}
// Run keeps the connection until ctx ends. Between tries it waits a little
// longer each time, up to 5 s: ubusd comes back within a second or two.
func (b *Bus) Run(ctx context.Context) {
wait := 200 * time.Millisecond
said := ""
for ctx.Err() == nil {
err := b.session(ctx)
if ctx.Err() != nil {
return
}
// The same reason is not repeated on every try.
if msg := err.Error(); msg != said {
b.Log("ubus: %s; trying again", msg)
said = msg
}
select {
case <-ctx.Done():
return
case <-time.After(wait):
}
if wait < 5*time.Second {
wait *= 2
}
}
}
// session is one connection: dial, add the objects, stay until it ends.
func (b *Bus) session(ctx context.Context) error {
c, err := Dial(b.path)
if err != nil {
return err
}
defer c.Close()
for _, o := range b.objects {
actx, cancel := context.WithTimeout(ctx, 10*time.Second)
err := c.Add(actx, o)
cancel()
if err != nil {
return err
}
}
b.mu.Lock()
b.conn = c
b.mu.Unlock()
defer func() {
b.mu.Lock()
b.conn = nil
b.mu.Unlock()
// Whoever watched is gone with the connection.
for _, o := range b.objects {
if o.Watched != nil {
o.Watched(false)
}
}
}()
select {
case <-ctx.Done():
return ctx.Err()
case <-c.Done():
c.mu.Lock()
err := c.err
c.mu.Unlock()
return err
}
}
// Notify sends a notification to the object's subscribers; without a
// connection it is dropped (a subscriber that comes back asks for what it missed).
func (b *Bus) Notify(o *Object, kind string, data map[string]any) error {
b.mu.Lock()
c := b.conn
b.mu.Unlock()
if c == nil {
return ErrClosed
}
return c.Notify(o, kind, data)
}
// Call invokes a method of another object on the bus.
func (b *Bus) Call(ctx context.Context, object, method string, args map[string]any) (map[string]any, error) {
b.mu.Lock()
c := b.conn
b.mu.Unlock()
if c == nil {
return nil, ErrClosed
}
return c.Call(ctx, object, method, args)
}
+585
View File
@@ -0,0 +1,585 @@
// One connection to ubusd: the hello, objects added with the signature of
// their methods, the answers to calls made on them, notifications to their
// subscribers, and calls this side makes on other objects.
//
// A Conn lives as long as its socket. ubusd going away (a restart) ends it;
// Bus (bus.go) dials again and puts the objects back. One goroutine reads; a
// call on an object runs in its own goroutine, so a slow answer holds up
// neither the reader nor the other callers.
package ubus
import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"sync"
)
// Socket is where ubusd listens on OpenWrt.
const Socket = "/var/run/ubus/ubus.sock"
// The types of a message (ubusmsg.h).
const (
typeHello = 0
typeStatus = 1
typeData = 2
typeLookup = 4
typeInvoke = 5
typeAddObject = 6
typeNotify = 10
)
// The fields of a message (ubusmsg.h).
const (
fieldStatus = 1
fieldObjPath = 2
fieldObjID = 3
fieldMethod = 4
fieldSignature = 6
fieldData = 7
fieldActive = 9
fieldNoReply = 10
fieldMax = 14
)
// maxMessage is the largest message ubusd takes (UBUS_MAX_MSGLEN); a larger
// one makes it drop the client.
const maxMessage = 1 << 20
// Status is how a call ended, as ubus counts it. Zero is success.
type Status int
// The statuses a handler may return (as an error) and a call may fail with.
const (
StatusInvalidCommand Status = 1
StatusInvalidArgument Status = 2
StatusMethodNotFound Status = 3
StatusNotFound Status = 4
StatusNoData Status = 5
StatusPermissionDenied Status = 6
StatusTimeout Status = 7
StatusNotSupported Status = 8
StatusUnknownError Status = 9
StatusConnectionFailed Status = 10
)
var statusText = map[Status]string{
StatusInvalidCommand: "invalid command", StatusInvalidArgument: "invalid argument",
StatusMethodNotFound: "method not found", StatusNotFound: "not found", StatusNoData: "no data",
StatusPermissionDenied: "permission denied", StatusTimeout: "timeout", StatusNotSupported: "not supported",
StatusUnknownError: "unknown error", StatusConnectionFailed: "connection failed",
}
func (s Status) Error() string {
if t, ok := statusText[s]; ok {
return "ubus: " + t
}
return fmt.Sprintf("ubus: status %d", int(s))
}
// ErrClosed is what calls return once the connection to ubusd has ended.
var ErrClosed = errors.New("ubus: connection closed")
// Handler answers a call. The arguments are the caller's table (through
// uhttpd, with its session id in "ubus_rpc_session"). A nil table answers with
// nothing; an error that is a Status ends the call with it, any other with
// StatusUnknownError.
type Handler func(ctx context.Context, args map[string]any) (map[string]any, error)
// Kind is the type of an argument in a method's signature (`ubus -v list`).
type Kind uint32
// The kinds of argument a signature names.
const (
Array Kind = msgArray
Table Kind = msgTable
String Kind = msgString
Int Kind = msgInt32
Bool Kind = msgBool
)
// Method is one method of an object.
type Method struct {
Name string
Params map[string]Kind
Handle Handler
// Waits marks a method that holds its answer until it has something to say
// (a long poll). Such calls are counted apart from the quick ones, which
// they would otherwise keep out, and one too many is refused at once.
Waits bool
}
// Object is what the backend publishes on the bus.
type Object struct {
Name string
Methods []Method
// Watched is called when the object's first subscriber comes and when its
// last one leaves, from the reader's goroutine: it must not block.
Watched func(active bool)
}
type message struct {
kind uint8
seq uint16
peer uint32
body []byte
}
// fields is a message's attributes by id; nil where one is absent.
type fields [fieldMax + 1][]byte
func (m *message) fields() (fields, error) {
var f fields
err := each(m.body, func(a attr) error {
if a.id <= fieldMax {
// An attribute that is there with no payload still counts as present.
if a.data == nil {
a.data = []byte{}
}
f[a.id] = a.data
}
return nil
})
return f, err
}
func (f *fields) uint32(id int) (uint32, bool) {
if len(f[id]) < 4 {
return 0, false
}
return binary.BigEndian.Uint32(f[id]), true
}
func (f *fields) string(id int) string {
d := f[id]
for i, c := range d {
if c == 0 {
return string(d[:i])
}
}
return string(d)
}
// reply is what a request this side made has received so far.
type reply struct {
data [][]byte
status Status
}
// Conn is a connection to ubusd.
type Conn struct {
c net.Conn
ctx context.Context
cancel context.CancelFunc
wmu sync.Mutex // one message at a time on the socket
mu sync.Mutex
seq uint16
pending map[uint16]chan reply
partial map[uint16]*reply
objects map[uint32]*Object
ids map[*Object]uint32
looked map[string]uint32
err error
// Calls on our objects that run now: at most this many, the rest wait in
// the reader, which is how a caller that floods the bus is slowed down.
slots chan struct{}
// Calls that wait (Method.Waits) and may each take most of a minute.
waits chan struct{}
}
// Dial connects to ubusd at path and waits for its hello.
func Dial(path string) (*Conn, error) {
c, err := net.Dial("unix", path)
if err != nil {
return nil, err
}
conn := &Conn{
c: c,
pending: map[uint16]chan reply{},
partial: map[uint16]*reply{},
objects: map[uint32]*Object{},
ids: map[*Object]uint32{},
looked: map[string]uint32{},
slots: make(chan struct{}, 8),
waits: make(chan struct{}, 64),
}
conn.ctx, conn.cancel = context.WithCancel(context.Background())
m, err := conn.read()
if err == nil && m.kind != typeHello {
err = fmt.Errorf("ubus: expected a hello, got message type %d", m.kind)
}
if err != nil {
c.Close()
return nil, err
}
go conn.loop()
return conn, nil
}
// Close ends the connection; everything waiting on it returns ErrClosed.
func (c *Conn) Close() error {
c.fail(ErrClosed)
return nil
}
// Done is closed when the connection has ended.
func (c *Conn) Done() <-chan struct{} { return c.ctx.Done() }
func (c *Conn) fail(err error) {
c.mu.Lock()
if c.err == nil {
c.err = err
for seq, ch := range c.pending {
close(ch)
delete(c.pending, seq)
}
}
c.mu.Unlock()
c.cancel()
c.c.Close()
}
func (c *Conn) read() (message, error) {
var h [12]byte
if _, err := io.ReadFull(c.c, h[:]); err != nil {
return message{}, err
}
if h[0] != 0 {
return message{}, fmt.Errorf("ubus: protocol version %d", h[0])
}
n := int(binary.BigEndian.Uint32(h[8:]) & attrLenMask)
if n < attrHeader || n > maxMessage {
return message{}, fmt.Errorf("ubus: message of %d bytes", n)
}
m := message{kind: h[1], seq: binary.BigEndian.Uint16(h[2:]), peer: binary.BigEndian.Uint32(h[4:])}
m.body = make([]byte, n-attrHeader)
if _, err := io.ReadFull(c.c, m.body); err != nil {
return message{}, err
}
return m, nil
}
func (c *Conn) write(kind uint8, seq uint16, peer uint32, body []byte) error {
n := attrHeader + len(body)
if n > maxMessage {
return fmt.Errorf("ubus: message of %d bytes is over the bus's limit", n)
}
b := make([]byte, 12, 12+len(body))
b[1] = kind
binary.BigEndian.PutUint16(b[2:], seq)
binary.BigEndian.PutUint32(b[4:], peer)
binary.BigEndian.PutUint32(b[8:], uint32(n))
b = append(b, body...)
c.wmu.Lock()
_, err := c.c.Write(b)
c.wmu.Unlock()
if err != nil {
c.fail(err)
}
return err
}
// request sends a message and waits for the status that ends its answer.
func (c *Conn) request(ctx context.Context, kind uint8, peer uint32, body []byte) (reply, error) {
ch := make(chan reply, 1)
c.mu.Lock()
if c.err != nil {
c.mu.Unlock()
return reply{}, ErrClosed
}
// A sequence number still waited for is not given out again.
for {
c.seq++
if _, busy := c.pending[c.seq]; !busy {
break
}
}
seq := c.seq
c.pending[seq] = ch
c.mu.Unlock()
if err := c.write(kind, seq, peer, body); err != nil {
c.forget(seq)
return reply{}, err
}
select {
case r, ok := <-ch:
if !ok {
return reply{}, ErrClosed
}
if r.status != 0 {
return r, r.status
}
return r, nil
case <-ctx.Done():
c.forget(seq)
return reply{}, ctx.Err()
}
}
func (c *Conn) forget(seq uint16) {
c.mu.Lock()
delete(c.pending, seq)
delete(c.partial, seq)
c.mu.Unlock()
}
func (c *Conn) loop() {
for {
m, err := c.read()
if err != nil {
c.fail(err)
return
}
f, err := m.fields()
if err != nil {
c.fail(err)
return
}
switch m.kind {
case typeData, typeStatus:
c.answered(m, &f)
case typeInvoke:
c.invoked(m, &f)
case typeNotify:
// From ubusd: the object got its first subscriber or lost its last.
id, _ := f.uint32(fieldObjID)
c.mu.Lock()
o := c.objects[id]
c.mu.Unlock()
if o != nil && o.Watched != nil && f[fieldActive] != nil {
o.Watched(len(f[fieldActive]) > 0 && f[fieldActive][0] != 0)
}
}
}
}
// answered takes a part of the answer to a request of ours: its data, or the
// status that ends it.
func (c *Conn) answered(m message, f *fields) {
c.mu.Lock()
defer c.mu.Unlock()
ch, ok := c.pending[m.seq]
if !ok {
return
}
r := c.partial[m.seq]
if r == nil {
r = &reply{}
c.partial[m.seq] = r
}
if m.kind == typeData {
// An answer to a lookup or to adding an object is the message itself; a
// method's answer sits in its data field.
r.data = append(r.data, m.body)
return
}
st, _ := f.uint32(fieldStatus)
r.status = Status(st)
delete(c.pending, m.seq)
delete(c.partial, m.seq)
ch <- *r
}
// invoked runs a call on one of our objects and answers it.
func (c *Conn) invoked(m message, f *fields) {
id, _ := f.uint32(fieldObjID)
name := f.string(fieldMethod)
silent := len(f[fieldNoReply]) > 0 && f[fieldNoReply][0] != 0
c.mu.Lock()
o := c.objects[id]
c.mu.Unlock()
end := func(st Status) {
if silent {
return
}
b := putUint32(nil, fieldStatus, uint32(st))
b = putUint32(b, fieldObjID, id)
c.write(typeStatus, m.seq, m.peer, b)
}
var handle Handler
slots := c.slots
if o != nil {
for i := range o.Methods {
if o.Methods[i].Name == name {
handle = o.Methods[i].Handle
if o.Methods[i].Waits {
slots = c.waits
}
}
}
}
if handle == nil {
end(StatusMethodNotFound)
return
}
args, err := decodeTable(f[fieldData], 0)
if err != nil {
end(StatusInvalidArgument)
return
}
if slots == c.waits {
select {
case slots <- struct{}{}:
default:
// The caller asks again later, by its own clock.
end(StatusTimeout)
return
}
} else {
select {
case slots <- struct{}{}:
case <-c.ctx.Done():
return
}
}
go func() {
defer func() { <-slots }()
out, err := handle(c.ctx, args)
var st Status
if err != nil && !errors.As(err, &st) {
st = StatusUnknownError
}
if err == nil && out != nil && !silent {
data, eerr := encodeTable(out)
if eerr != nil {
st = StatusUnknownError
} else {
b := putUint32(nil, fieldObjID, id)
b = putBytes(b, fieldData, data)
if c.write(typeData, m.seq, m.peer, b) != nil {
return
}
}
}
end(st)
}()
}
// Add publishes an object. Its methods can be called as soon as this returns.
func (c *Conn) Add(ctx context.Context, o *Object) error {
b := putString(nil, fieldObjPath, o.Name)
b, at := open(b)
var err error
for _, m := range o.Methods {
sig := make(map[string]any, len(m.Params))
for k, v := range m.Params {
sig[k] = int64(v)
}
if b, err = putValue(b, m.Name, sig); err != nil {
return err
}
}
b = closeAttr(b, at, fieldSignature<<attrIDShift)
r, err := c.request(ctx, typeAddObject, 0, b)
if err != nil {
return fmt.Errorf("ubus: adding %s: %w", o.Name, err)
}
for _, d := range r.data {
f, err := (&message{body: d}).fields()
if err != nil {
return err
}
if id, ok := f.uint32(fieldObjID); ok {
c.mu.Lock()
c.objects[id] = o
c.ids[o] = id
c.mu.Unlock()
return nil
}
}
return fmt.Errorf("ubus: adding %s: no object id in the answer", o.Name)
}
// Notify sends a notification of this type to the object's subscribers and
// does not wait for them. With no subscriber ubusd drops it.
func (c *Conn) Notify(o *Object, kind string, data map[string]any) error {
c.mu.Lock()
id, ok := c.ids[o]
c.mu.Unlock()
if !ok {
return fmt.Errorf("ubus: %s is not on the bus", o.Name)
}
payload, err := encodeTable(data)
if err != nil {
return err
}
b := putUint32(nil, fieldObjID, id)
b = putString(b, fieldMethod, kind)
b = putUint8(b, fieldNoReply, 1)
b = putBytes(b, fieldData, payload)
return c.write(typeNotify, 0, id, b)
}
// Call invokes a method of another object and returns the table it answers
// with (empty when it answers with nothing).
func (c *Conn) Call(ctx context.Context, object, method string, args map[string]any) (map[string]any, error) {
id, err := c.lookup(ctx, object)
if err != nil {
return nil, err
}
payload, err := encodeTable(args)
if err != nil {
return nil, err
}
b := putUint32(nil, fieldObjID, id)
b = putString(b, fieldMethod, method)
b = putBytes(b, fieldData, payload)
r, err := c.request(ctx, typeInvoke, id, b)
if err != nil {
// The object may have come back under another id (its owner restarted).
if errors.Is(err, StatusNotFound) {
c.mu.Lock()
delete(c.looked, object)
c.mu.Unlock()
}
return nil, fmt.Errorf("ubus: %s.%s: %w", object, method, err)
}
out := map[string]any{}
for _, d := range r.data {
f, err := (&message{body: d}).fields()
if err != nil {
return nil, err
}
t, err := decodeTable(f[fieldData], 0)
if err != nil {
return nil, err
}
for k, v := range t {
out[k] = v
}
}
return out, nil
}
func (c *Conn) lookup(ctx context.Context, object string) (uint32, error) {
c.mu.Lock()
id, ok := c.looked[object]
c.mu.Unlock()
if ok {
return id, nil
}
r, err := c.request(ctx, typeLookup, 0, putString(nil, fieldObjPath, object))
if err != nil {
return 0, fmt.Errorf("ubus: looking up %s: %w", object, err)
}
for _, d := range r.data {
f, err := (&message{body: d}).fields()
if err != nil {
return 0, err
}
if id, ok := f.uint32(fieldObjID); ok && f.string(fieldObjPath) == object {
c.mu.Lock()
c.looked[object] = id
c.mu.Unlock()
return id, nil
}
}
return 0, fmt.Errorf("ubus: looking up %s: %w", object, StatusNotFound)
}
+132
View File
@@ -0,0 +1,132 @@
//go:build ubusd
// Against a real ubusd, with OpenWrt's own ubus tool on the other side
// (scripts/backend-container.sh runs it in the rootfs containers).
package ubus
import (
"bufio"
"context"
"encoding/json"
"os/exec"
"reflect"
"testing"
"time"
)
func TestRealBus(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
watched := make(chan bool, 4)
obj := &Object{
Name: "dawnline.selftest",
Methods: []Method{
{Name: "echo", Params: map[string]Kind{"value": Table}, Handle: func(_ context.Context, args map[string]any) (map[string]any, error) {
return args, nil
}},
{Name: "nothing", Handle: func(context.Context, map[string]any) (map[string]any, error) { return nil, nil }},
{Name: "refuse", Handle: func(context.Context, map[string]any) (map[string]any, error) {
return nil, StatusPermissionDenied
}},
},
Watched: func(on bool) { watched <- on },
}
bus := NewBus(Socket, obj)
bus.Log = t.Logf
go bus.Run(ctx)
// On the bus once the tool sees it.
for exec.CommandContext(ctx, "ubus", "list", obj.Name).Run() != nil {
if ctx.Err() != nil {
t.Fatal("the object never appeared on the bus")
}
time.Sleep(50 * time.Millisecond)
}
// The signature, as `ubus -v list` prints it.
out, err := exec.CommandContext(ctx, "ubus", "-v", "list", obj.Name).CombinedOutput()
if err != nil {
t.Fatalf("ubus -v list: %v\n%s", err, out)
}
t.Logf("%s", out)
// A call from the tool: what comes back is what went in.
in := `{"s":"x","n":1,"big":5000000000,"f":2.5,"b":true,"z":null,"l":[1,"two",{"k":false}],"t":{"u":{}}}`
out, err = exec.CommandContext(ctx, "ubus", "call", obj.Name, "echo", in).Output()
if err != nil {
t.Fatalf("ubus call: %v", err)
}
var want, got any
json.Unmarshal([]byte(in), &want)
if err := json.Unmarshal(out, &got); err != nil {
t.Fatalf("answer is not JSON: %s", out)
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("echo: got %s", out)
}
if out, err := exec.CommandContext(ctx, "ubus", "call", obj.Name, "nothing").CombinedOutput(); err != nil {
t.Fatalf("a call answered with nothing failed: %v %s", err, out)
}
// A handler's status is the tool's exit code, negated (an 8-bit one: 256 - 6).
err = exec.CommandContext(ctx, "ubus", "call", obj.Name, "refuse").Run()
if ee, ok := err.(*exec.ExitError); !ok || ee.ExitCode() != 256-int(StatusPermissionDenied) {
t.Fatalf("refuse: %v", err)
}
if err := exec.CommandContext(ctx, "ubus", "call", obj.Name, "absent").Run(); err == nil {
t.Fatal("a method that is not there answered")
}
// A call from our side, on a second connection.
c, err := Dial(Socket)
if err != nil {
t.Fatal(err)
}
defer c.Close()
res, err := c.Call(ctx, obj.Name, "echo", map[string]any{"hello": "bus", "n": 7})
if err != nil || res["hello"] != "bus" || res["n"] != int64(7) {
t.Fatalf("Call: %v %v", res, err)
}
if _, err := c.Call(ctx, "no.such.object", "x", nil); err == nil {
t.Fatal("a call on an object that is not there answered")
}
// A subscriber: we are told it came, it gets the notification, we are told it left.
sub := exec.CommandContext(ctx, "ubus", "subscribe", obj.Name)
pipe, _ := sub.StdoutPipe()
if err := sub.Start(); err != nil {
t.Fatal(err)
}
select {
case on := <-watched:
if !on {
t.Fatal("told the subscriber left before it came")
}
case <-ctx.Done():
t.Fatal("never told of the subscriber")
}
if err := bus.Notify(obj, "patch", map[string]any{"rev": 12, "ops": map[string]any{"online": true}}); err != nil {
t.Fatal(err)
}
line, err := bufio.NewReader(pipe).ReadString('\n')
if err != nil {
t.Fatal(err)
}
var note map[string]any
if err := json.Unmarshal([]byte(line), &note); err != nil {
t.Fatalf("notification is not JSON: %s", line)
}
wantNote := map[string]any{"patch": map[string]any{"rev": float64(12), "ops": map[string]any{"online": true}}}
if !reflect.DeepEqual(note, wantNote) {
t.Fatalf("notification: %s", line)
}
sub.Process.Kill()
sub.Wait()
select {
case on := <-watched:
if on {
t.Fatal("told a subscriber came when it left")
}
case <-ctx.Done():
t.Fatal("never told the subscriber left")
}
}