srv9p

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2025 License: AGPL-3.0 Imports: 7 Imported by: 0

README

srv9p : serve 9P namespaces on embedded devices

Copyright (C) 2024-present, Bernd Fix >Y<

srv9p is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

srv9p is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see http://www.gnu.org/licenses/.

SPDX-License-Identifier: AGPL3.0-or-later

Caveat

This is work-in-progess. Use at your own risk...

Intro

srv9p serves namespaces over 9P and is meant for embedded devices with TCP/IP connectivity but limited RAM/ROM space. It is written in Go and can be compiled with tinygo for the following devices:

  • Raspberry Pico2 W [RP2350]
  • ... (more to come)

For the 9P protocol implementation, srv9p uses a library (https://git.sr.ht/~moody/ninep) that is compact and sufficient.

Example

See the example app to learn how to use srv9p:

tinygo build -target=pico2-w -o srv9p.uf2 ./example/pico2w.go

You need to edit ./example/pico2w.go to set the WiFi SSID and password; you also need to set the hostname for the device and the 9P listening port (usually 564). As an alternative you can set the values at compile time by adding -ldflags "-X ..." to the command-line above.

Documentation

Index

Constants

View Source
const (
	StatUNK     = iota // unknown status (init)
	StatOK             // processing active
	StatDEV            // device failure
	StatNS             // namespace construction failed
	StatSRV            // can't serve namespace
	StatIP             // invalid IP address
	StatWIFI           // can't connect to AP
	StatWPA2           // WPA2 failed
	StatDHCP1          // DHCP request failed
	StatDHCP2          // no DHCP reply
	StatLISTEN1        // failed to create listener
	StatLISTEN2        // failed to initialize listener
	StatPORT           // invalid port specified
	StatEXCP           // exception (panic) occured
)

status codes

Variables

This section is empty.

Functions

This section is empty.

Types

type Device

type Device interface {
	// LED on or off (if applicable)
	LED(on bool)

	// SetupListener returns a TCP listener on the given port.
	// On embedded devices with WiFi connectivity the following steps are
	// performed:
	//    1. Connect to access point with given SSID and join with WPA2 password
	//    2. Use DHCP to get a network address; query for given hostname. Assign
	//       IP address if DHCP fails (if IP is a valid address).
	//    3. Listen to the specified TCP port
	// Devices with running TCP/IP stack can skip steps 1. and 2. in their
	// implementation.
	SetupListener(host, ip, ssid, passwd string, port uint16) (lst net.Listener, state int)
}

Device is a hardware abstraction

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

Entry in the filesystem

func (*Entry) IsDir

func (e *Entry) IsDir() bool

IsDir returns true if entry is a directory

func (*Entry) SetOwner added in v0.0.2

func (e *Entry) SetOwner(user, group string)

SetOwner of entry if different from namespace owner

type File

type File interface {
	Read(offset uint64, count uint32) ([]byte, error)
	Write(offset uint64, data []byte) (uint32, error)
}

File interface for file handler implementations: The interface methods are called by the 9p protocol handler on demand. The implementation is free to handle the read/write calls according to its own logic.

type FuncFile added in v0.0.2

type FuncFile struct {
	NopFile
	// contains filtered or unexported fields
}

FuncFile content is returned by a function.

func NewFuncFile added in v0.0.2

func NewFuncFile(read ReadFcn, write WriteFcn) *FuncFile

NewFuncFile with specified function.

func (*FuncFile) Read added in v0.0.2

func (f *FuncFile) Read(offset uint64, count uint32) ([]byte, error)

Read implementation: return file content.

func (*FuncFile) Write added in v0.0.3

func (f *FuncFile) Write(offset uint64, data []byte) (num uint32, err error)

Write implementation: return bytes written

type Namespace

type Namespace struct {
	ninep.NopFS // use default handlers where needed
	// contains filtered or unexported fields
}

Namespace is a synthetic filesystem.

func NewNamespace

func NewNamespace(user, group string, perm uint32) *Namespace

NewNamespace creates a new filesystem (with root directory) for the given user and group. All subdirectories and files belong to the same owner and group. If required, the ownership can be redefined on an entry with the SetOwner() method of an entry.

func (*Namespace) Attach

func (ns *Namespace) Attach(t *ninep.Tattach)

Attach to 9p session

func (*Namespace) Get

func (ns *Namespace) Get(path string) (*Entry, error)

Get entry with given path

func (*Namespace) HandleReq added in v0.0.2

func (ns *Namespace) HandleReq(c net.Conn)

Handle 9p requests

func (*Namespace) NewDir added in v0.0.2

func (ns *Namespace) NewDir(path string, perm uint32) (err error)

NewDir creates a directory entry for the filesystem.

func (*Namespace) NewFile added in v0.0.2

func (ns *Namespace) NewFile(path string, perm uint32, impl File) (err error)

func (*Namespace) Open

func (ns *Namespace) Open(t *ninep.Topen, q *ninep.Qid)

Open entry for file operation

func (*Namespace) Read

func (ns *Namespace) Read(t *ninep.Tread, q *ninep.Qid)

Read from entry. Either return the content of a file or the listing from a directory.

func (*Namespace) Serve

func (ns *Namespace) Serve(listen string) error

Serve the 9p protocol for the given listen string

func (*Namespace) Stat

func (ns *Namespace) Stat(t *ninep.Tstat, q *ninep.Qid)

Stat returns information for a filesytem entry.

func (*Namespace) Walk

func (ns *Namespace) Walk(cur *ninep.Qid, next string) *ninep.Qid

Walk to child entry with name "next".

func (*Namespace) Write added in v0.0.2

func (ns *Namespace) Write(t *ninep.Twrite, q *ninep.Qid)

Write to entry (files only)

func (*Namespace) Wstat added in v0.0.3

func (ns *Namespace) Wstat(t *ninep.Twstat, q *ninep.Qid)

Wstat updates information for a filesytem entry (ignored).

type NopFile added in v0.0.2

type NopFile struct{}

NopFile ignores all read/write requests

func (*NopFile) Read added in v0.0.2

func (f *NopFile) Read(offset uint64, count uint32) (data []byte, err error)

Read returns emtpy file

func (*NopFile) Write added in v0.0.2

func (f *NopFile) Write(uint64, []byte) (num uint32, err error)

Write to file is ignored

type ReadFcn added in v0.0.3

type ReadFcn func(offset uint64, count uint32) ([]byte, error)

Read/Write function prototypes

type Status

type Status struct {
	// contains filtered or unexported fields
}

Status handler. Show current status depending on hardware device.

func NewStatus

func NewStatus(dev Device) (state *Status)

NewStatus creates a new status display

func (*Status) Get

func (state *Status) Get() (int, int)

Get current state and repeat counter

func (*Status) Set

func (state *Status) Set(flag, num int)

Set status and repeat <num> times.

func (*Status) Trap added in v0.0.2

func (state *Status) Trap(t time.Duration)

Trap critical failures (panic)

type TextFile added in v0.0.2

type TextFile struct {
	NopFile
	// contains filtered or unexported fields
}

TextFile with (small) static text content.

func NewTextFile added in v0.0.2

func NewTextFile(content string) *TextFile

NewTextFile with given text content.

func (*TextFile) Read added in v0.0.2

func (f *TextFile) Read(offset uint64, count uint32) ([]byte, error)

Read implementation: return file content.

type WriteFcn added in v0.0.3

type WriteFcn func(offset uint64, data []byte) (uint32, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL