46 lines
788 B
Go
46 lines
788 B
Go
package host
|
|
|
|
import (
|
|
"fmt"
|
|
"lux/proto"
|
|
)
|
|
|
|
type LuxOptionType uint
|
|
|
|
const (
|
|
LuxOptionTypeWAN = 0
|
|
LuxOptionTypeNetIf = 1
|
|
)
|
|
|
|
type LuxOption interface {
|
|
Read(rd *proto.LuxBuffer) error
|
|
Write(wd *proto.LuxBuffer)
|
|
|
|
Type() LuxOptionType
|
|
}
|
|
|
|
func ReadLuxOption(rd *proto.LuxBuffer) (LuxOption, error) {
|
|
var err error
|
|
optVal, err := rd.ReadUint16()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch LuxOptionType(optVal) {
|
|
case LuxOptionTypeWAN:
|
|
opt := &LuxOptionWAN{}
|
|
err = opt.Read(rd)
|
|
return opt, err
|
|
case LuxOptionTypeNetIf:
|
|
opt := NewLuxOptionNetIf()
|
|
err := opt.Read(rd)
|
|
return &opt, err
|
|
default:
|
|
return nil, fmt.Errorf("unknown option %d", optVal)
|
|
}
|
|
}
|
|
|
|
func WriteLuxOption(wd *proto.LuxBuffer, opt LuxOption) {
|
|
wd.WriteUint16(uint16(opt.Type()))
|
|
opt.Write(wd)
|
|
}
|