Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cleanups #58

Merged
merged 9 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/oc-client/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*
Oc-client is the OC-Daemon client.
*/
package main

import "github.com/telekom-mms/oc-daemon/internal/client"
Expand Down
3 changes: 3 additions & 0 deletions cmd/oc-daemon-vpncscript/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*
Oc-daemon-vpncscript is the vpncscript used by OC-Daemon.
*/
package main

import "github.com/telekom-mms/oc-daemon/internal/vpncscript"
Expand Down
3 changes: 3 additions & 0 deletions cmd/oc-daemon/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*
Oc-daemon is the OC-Daemon.
*/
package main

import "github.com/telekom-mms/oc-daemon/internal/daemon"
Expand Down
20 changes: 11 additions & 9 deletions internal/addrmon/addrmon.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package addrmon contains the address monitor.
package addrmon

import (
Expand All @@ -8,14 +9,14 @@ import (
"github.com/vishvananda/netlink"
)

// Update is an address update
// Update is an address update.
type Update struct {
Add bool
Address net.IPNet
Index int
}

// AddrMon is an address monitor
// AddrMon is an address monitor.
type AddrMon struct {
events chan netlink.AddrUpdate
updates chan *Update
Expand All @@ -24,7 +25,7 @@ type AddrMon struct {
closed chan struct{}
}

// sendUpdate sends an address update
// sendUpdate sends an address update.
func (a *AddrMon) sendUpdate(update *Update) {
select {
case a.updates <- update:
Expand All @@ -35,7 +36,7 @@ func (a *AddrMon) sendUpdate(update *Update) {
// netlinkAddrSubscribeWithOptions is netlink.AddrSubscribeWithOptions for testing.
var netlinkAddrSubscribeWithOptions = netlink.AddrSubscribeWithOptions

// RegisterAddrUpdates registers for addr update events
// RegisterAddrUpdates registers for addr update events.
var RegisterAddrUpdates = func(a *AddrMon) (chan netlink.AddrUpdate, error) {
// register for addr update events
events := make(chan netlink.AddrUpdate)
Expand All @@ -49,7 +50,7 @@ var RegisterAddrUpdates = func(a *AddrMon) (chan netlink.AddrUpdate, error) {
return events, nil
}

// start starts the address monitor
// start starts the address monitor.
func (a *AddrMon) start() {
defer close(a.closed)
defer close(a.updates)
Expand Down Expand Up @@ -84,6 +85,7 @@ func (a *AddrMon) start() {
go func() {
for range a.events {
// wait for channel shutdown
log.Debug("AddrMon dropping event after stop")
}
}()

Expand All @@ -93,7 +95,7 @@ func (a *AddrMon) start() {
}
}

// Start starts the address monitor
// Start starts the address monitor.
func (a *AddrMon) Start() error {
// register for addr update events
events, err := RegisterAddrUpdates(a)
Expand All @@ -106,18 +108,18 @@ func (a *AddrMon) Start() error {
return nil
}

// Stop stops the address monitor
// Stop stops the address monitor.
func (a *AddrMon) Stop() {
close(a.done)
<-a.closed
}

// Updates returns the address updates channel
// Updates returns the address updates channel.
func (a *AddrMon) Updates() chan *Update {
return a.updates
}

// NewAddrMon returns a new address monitor
// NewAddrMon returns a new address monitor.
func NewAddrMon() *AddrMon {
return &AddrMon{
updates: make(chan *Update),
Expand Down
6 changes: 3 additions & 3 deletions internal/addrmon/addrmon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/vishvananda/netlink"
)

// TestAddrMonStartStop tests Start and Stop of AddrMon
// TestAddrMonStartStop tests Start and Stop of AddrMon.
func TestAddrMonStartStop(t *testing.T) {
// clean up after tests
oldRegisterAddrUpdates := RegisterAddrUpdates
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestAddrMonStartStop(t *testing.T) {
addrMon.Stop()
}

// TestAddrMonUpdates tests Updates of AddrMon
// TestAddrMonUpdates tests Updates of AddrMon.
func TestAddrMonUpdates(t *testing.T) {
addrMon := NewAddrMon()
got := addrMon.Updates()
Expand All @@ -103,7 +103,7 @@ func TestAddrMonUpdates(t *testing.T) {
}
}

// TestNewAddrMon tests NewAddrMon
// TestNewAddrMon tests NewAddrMon.
func TestNewAddrMon(t *testing.T) {
addrMon := NewAddrMon()
if addrMon.updates == nil ||
Expand Down
16 changes: 8 additions & 8 deletions internal/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ import (
)

var (
// SocketFile is the unix socket file
// SocketFile is the unix socket file.
SocketFile = "/run/oc-daemon/daemon.sock"

// SocketOwner is the owner of the socket file
// SocketOwner is the owner of the socket file.
SocketOwner = ""

// SocketGroup is the group of the socket file
// SocketGroup is the group of the socket file.
SocketGroup = ""

// SocketPermissions are the file permissions of the socket file
// SocketPermissions are the file permissions of the socket file.
SocketPermissions = "0700"

// RequestTimeout is the timeout for an entire request/response
// exchange initiated by a client
// exchange initiated by a client.
RequestTimeout = 30 * time.Second
)

// Config is a server configuration
// Config is a server configuration.
type Config struct {
SocketFile string
SocketOwner string
Expand All @@ -32,7 +32,7 @@ type Config struct {
RequestTimeout time.Duration
}

// Valid returns whether server config is valid
// Valid returns whether server config is valid.
func (c *Config) Valid() bool {
if c == nil ||
c.SocketFile == "" ||
Expand All @@ -51,7 +51,7 @@ func (c *Config) Valid() bool {
return true
}

// NewConfig returns a new server configuration
// NewConfig returns a new server configuration.
func NewConfig() *Config {
return &Config{
SocketFile: SocketFile,
Expand Down
4 changes: 2 additions & 2 deletions internal/api/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import "testing"

// TestConfigValid tests Valid of Config
// TestConfigValid tests Valid of Config.
func TestConfigValid(t *testing.T) {
// test invalid
for _, invalid := range []*Config{
Expand Down Expand Up @@ -31,7 +31,7 @@ func TestConfigValid(t *testing.T) {
}
}

// TestNewConfig tests NewConfig
// TestNewConfig tests NewConfig.
func TestNewConfig(t *testing.T) {
sc := NewConfig()
if !sc.Valid() {
Expand Down
18 changes: 9 additions & 9 deletions internal/api/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
)

const (
// MaxPayloadLength is the maximum allowed length of a message payload
// MaxPayloadLength is the maximum allowed length of a message payload.
MaxPayloadLength = 32768
)

// Message types
// Message types.
const (
TypeNone = iota
TypeOK
Expand All @@ -20,19 +20,19 @@ const (
TypeUndefined
)

// Header is a message header
// Header is a message header.
type Header struct {
Type uint16
Length uint16
}

// Message is an API message
// Message is an API message.
type Message struct {
Header
Value []byte
}

// NewMessage returns a new message with type t and payload p
// NewMessage returns a new message with type t and payload p.
func NewMessage(t uint16, p []byte) *Message {
if len(p) > MaxPayloadLength {
return nil
Expand All @@ -46,17 +46,17 @@ func NewMessage(t uint16, p []byte) *Message {
}
}

// NewOK returns a new OK message with payload p
// NewOK returns a new OK message with payload p.
func NewOK(p []byte) *Message {
return NewMessage(TypeOK, p)
}

// NewError returns a new error message with payload p
// NewError returns a new error message with payload p.
func NewError(p []byte) *Message {
return NewMessage(TypeError, p)
}

// ReadMessage returns the next message from r
// ReadMessage returns the next message from r.
func ReadMessage(r io.Reader) (*Message, error) {
// read header
h := &Header{}
Expand Down Expand Up @@ -88,7 +88,7 @@ func ReadMessage(r io.Reader) (*Message, error) {
return m, nil
}

// WriteMessage writes message m to r
// WriteMessage writes message m to r.
func WriteMessage(w io.Writer, m *Message) error {
// write header
err := binary.Write(w, binary.LittleEndian, m.Header)
Expand Down
8 changes: 4 additions & 4 deletions internal/api/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
)

// TestNewMessage tests NewMessage
// TestNewMessage tests NewMessage.
func TestNewMessage(t *testing.T) {
// message types
for _, typ := range []uint16{
Expand All @@ -32,15 +32,15 @@ func TestNewMessage(t *testing.T) {
}
}

// TestNewOK tests NewOK
// TestNewOK tests NewOK.
func TestNewOK(t *testing.T) {
msg := NewOK(nil)
if msg.Type != TypeOK {
t.Errorf("got %d, want %d", msg.Type, TypeOK)
}
}

// TestNewError tests NewError
// TestNewError tests NewError.
func TestNewError(t *testing.T) {
msg := NewError(nil)
if msg.Type != TypeError {
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestWriteMessageErrors(t *testing.T) {
}
}

// TestReadWriteMessage tests ReadMessage and WriteMessage
// TestReadWriteMessage tests ReadMessage and WriteMessage.
func TestReadWriteMessage(t *testing.T) {
want := &Message{
Header: Header{
Expand Down
16 changes: 8 additions & 8 deletions internal/api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,51 @@ import (
log "github.com/sirupsen/logrus"
)

// Request is a request from a client
// Request is a request from a client.
type Request struct {
msg *Message
reply []byte
err string
conn net.Conn
}

// Type returns the type of the request
// Type returns the type of the request.
func (r *Request) Type() uint16 {
return r.msg.Type
}

// Data returns the data in the API request
// Data returns the data in the API request.
func (r *Request) Data() []byte {
return r.msg.Value
}

// Reply sets the data in the reply for this request
// Reply sets the data in the reply for this request.
func (r *Request) Reply(b []byte) {
r.reply = b
}

// Error sets the error reply message for this request
// Error sets the error reply message for this request.
func (r *Request) Error(msg string) {
r.err = msg
}

// sendOK sends an ok message back to the client
// sendOK sends an ok message back to the client.
func (r *Request) sendOK() {
o := NewOK(r.reply)
if err := WriteMessage(r.conn, o); err != nil {
log.WithError(err).Error("Daemon message send error")
}
}

// sendError sends an error back to the client
// sendError sends an error back to the client.
func (r *Request) sendError() {
e := NewError([]byte(r.err))
if err := WriteMessage(r.conn, e); err != nil {
log.WithError(err).Error("Daemon message send error")
}
}

// Close closes the API request
// Close closes the API request.
func (r *Request) Close() {
defer func() {
_ = r.conn.Close()
Expand Down
Loading
Loading