Files
haza/opt.go

226 lines
4.9 KiB
Go
Raw Normal View History

2025-09-26 19:58:22 +09:00
package haza
import "encoding/binary"
import "net"
type Dhcp4OptCode = uint8
type Dhcp4MsgType = uint8
const (
_ Dhcp4MsgType = iota
DHCP4_MSG_DISCOVER = 1
DHCP4_MSG_OFFER = 2
DHCP4_MSG_REQUEST = 3
DHCP4_MSG_DECLINE = 4
DHCP4_MSG_ACK = 5
DHCP4_MSG_NAK = 6
DHCP4_MSG_RELEASE = 7
DHCP4_MSG_INFORM = 8
DHCP4_MSG_FORCE_RENEW = 9
DHCP4_MSG_LEASE_QUERY = 10
DHCP4_MSG_LEASE_UNASSIGNED = 11
DHCP4_MSG_LEASE_UNKNOWN = 12
DHCP4_MSG_LEASE_ACTIVE = 13
DHCP4_MSG_BULK_LEASE_QUERY = 14
DHCP4_MSG_LEASE_QUERY_DONE = 15
DHCP4_MSG_ACTIVE_LEASE_QUERY = 16
DHCP4_MSG_LEASE_QUERY_STATUS = 17
DHCP4_MSG_TLS = 18
)
const (
DHCP4_OPT_PADDING Dhcp4OptCode = iota // 0
DHCP4_OPT_SUBNET_MASK = 0x01
DHCP4_OPT_TIME_OFFSET = 0x02
DHCP4_OPT_ROUTER = 0x03
DHCP4_OPT_TIME_SERVER = 0x04
DHCP4_OPT_NAME_SERVER = 0x05
DHCP4_OPT_DNS_SERVER = 0x06
DHCP4_OPT_LOG_SERVER = 0x07
DHCP4_OPT_QUOTE_SERVER = 0x08
DHCP4_OPT_LPR_SERVER = 0x09
DHCP4_OPT_HOST_NAME = 0x0C
DHCP4_OPT_VENDOR_SPECIFIC = 0x2B
DHCP4_OPT_REQUESTED_IPADDR = 0x32
DHCP4_OPT_LEASE_TIME = 0x33
DHCP4_OPT_OVERLOAD = 0x34
DHCP4_OPT_MSG_TYPE = 0x35
DHCP4_OPT_SERVER_ID = 0x36
DHCP4_OPT_PARAM_REQ_LIST = 0x37
DHCP4_OPT_ERROR_TEXT = 0x38
DHCP4_OPT_MAX_MSG_SIZE = 0x39
DHCP4_OPT_RENEWAL_TIME = 0x3A // T1
DHCP4_OPT_REBINDING_TIME = 0x3B // T2
DHCP4_OPT_CLIENT_ID = 0x3D
DHCP4_OPT_END = 0xFF
)
const (
DHCP4_OPT_VAL_UINT8 = 0
)
type Dhcp4Opt interface {
Code() Dhcp4OptCode
Bytes() []byte
}
type Dhcp4OptBase struct {
code Dhcp4OptCode
}
type Dhcp4OptIp4Addr struct {
Dhcp4OptBase
value net.IP
}
type Dhcp4OptIp4Addrs struct {
Dhcp4OptBase
value []net.IP
}
type Dhcp4OptString struct {
Dhcp4OptBase
value string
}
type Dhcp4OptBytes struct {
Dhcp4OptBase
value []byte
}
type Dhcp4OptUint32 struct {
Dhcp4OptBase
value uint32
}
type Dhcp4OptUint16 struct {
Dhcp4OptBase
value uint16
}
type Dhcp4OptUint8 struct {
Dhcp4OptBase
value uint8
}
func (opt *Dhcp4OptBase) Code() Dhcp4OptCode {
return opt.code
}
func (opt *Dhcp4OptIp4Addr) Bytes() []byte {
// net.IP itself is []byte
var buf []byte
buf = opt.value.To4()
if buf == nil { return []byte{} }
return buf
}
func (opt *Dhcp4OptIp4Addrs) Bytes() []byte {
var buf []byte
var v net.IP
var v4 []byte
var count int
count = 0
for _, ip := range opt.value {
if ip.To4() != nil { count++ }
}
buf = make([]byte, 0, count * net.IPv4len)
for _, v = range opt.value {
v4 = v.To4()
if v4 == nil { continue }
buf = append(buf, v4...)
}
return buf
}
func (opt *Dhcp4OptString) Bytes() []byte {
return []byte(opt.value)
}
func (opt *Dhcp4OptBytes) Bytes() []byte {
return opt.value
}
func (opt *Dhcp4OptUint32) Bytes() []byte {
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], opt.value)
return buf[:]
}
func (opt *Dhcp4OptUint16) Bytes() []byte {
var buf [2]byte
binary.BigEndian.PutUint16(buf[:], opt.value)
return buf[:]
}
func (opt *Dhcp4OptUint8) Bytes() []byte {
return []byte{opt.value}
}
func Dhcp4OptMsgType(v uint8) Dhcp4Opt {
return &Dhcp4OptUint8{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_MSG_TYPE}, value: v}
}
func Dhcp4OptSubnetMask(v net.IP) Dhcp4Opt {
return &Dhcp4OptIp4Addr{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_SUBNET_MASK}, value: v}
}
func Dhcp4OptRouter(v net.IP) Dhcp4Opt {
return &Dhcp4OptIp4Addr{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_ROUTER}, value: v}
}
func Dhcp4OptTimeServers(v []net.IP) Dhcp4Opt {
return &Dhcp4OptIp4Addrs{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_TIME_SERVER}, value: v}
}
func Dhcp4OptDnsServers(v []net.IP) Dhcp4Opt {
return &Dhcp4OptIp4Addrs{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_DNS_SERVER}, value: v}
}
func Dhcp4OptHostName(v string) Dhcp4Opt {
return &Dhcp4OptString{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_DNS_SERVER}, value: v}
}
func Dhcp4OptReqIpaddr(v net.IP) Dhcp4Opt {
return &Dhcp4OptIp4Addr{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_REQUESTED_IPADDR}, value: v}
}
func Dhcp4OptLeaseTime(v uint32) Dhcp4Opt {
return &Dhcp4OptUint32{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_LEASE_TIME}, value: v}
}
func Dhcp4OptServerId(v net.IP) Dhcp4Opt {
return &Dhcp4OptIp4Addr{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_SERVER_ID}, value: v}
}
func Dhcp4OptParamReqList(v []Dhcp4OptCode) Dhcp4Opt {
return &Dhcp4OptBytes{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_PARAM_REQ_LIST}, value: v}
}
func Dhcp4OptErrorText(v string) Dhcp4Opt {
return &Dhcp4OptString{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_ERROR_TEXT}, value: v}
}
func Dhcp4OptMaxMsgSize(v uint16) Dhcp4Opt {
return &Dhcp4OptUint16{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_MAX_MSG_SIZE}, value: v}
}
func Dhcp4OptRenewalTime(v uint32) Dhcp4Opt {
return &Dhcp4OptUint32{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_RENEWAL_TIME}, value: v}
}
func Dhcp4OptRebindingTime(v uint32) Dhcp4Opt {
return &Dhcp4OptUint32{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_REBINDING_TIME}, value: v}
}
func Dhcp4OptClientId(v string) Dhcp4Opt {
return &Dhcp4OptString{Dhcp4OptBase: Dhcp4OptBase{code: DHCP4_OPT_CLIENT_ID}, value: v}
}