mirror of
https://github.com/danderson/netboot.git
synced 2025-10-18 02:51:22 +02:00
Made dhcpv6 packet easier to test
This commit is contained in:
parent
b6741d78b8
commit
bf8701a8c5
@ -5,6 +5,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"golang.org/x/tools/go/gcimporter15/testdata"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MessageType uint8
|
type MessageType uint8
|
||||||
@ -28,21 +29,16 @@ const (
|
|||||||
type Packet struct {
|
type Packet struct {
|
||||||
Type MessageType
|
Type MessageType
|
||||||
TransactionID [3]byte
|
TransactionID [3]byte
|
||||||
Options []byte
|
Options Options
|
||||||
}
|
}
|
||||||
|
|
||||||
func MakePacket(bs []byte, len int) *Packet {
|
func MakePacket(bs []byte, len int) (*Packet, error) {
|
||||||
ret := &Packet{Type: MessageType(bs[0]), Options: make([]byte, len - 4)}
|
options, err := MakeOptions(bs[4:]) // 4:len?
|
||||||
copy(ret.TransactionID[:], bs[1:4])
|
|
||||||
copy(ret.Options[:], bs[4:len])
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Packet) UnmarshalOptions() (Options, error) {
|
|
||||||
ret, err := MakeOptions(p.Options)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("packet has malformed options section: %s", err)
|
return nil, fmt.Errorf("packet has malformed options section: %s", err)
|
||||||
}
|
}
|
||||||
|
ret := &Packet{Type: MessageType(bs[0]), Options: options}
|
||||||
|
copy(ret.TransactionID[:], bs[1:4])
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +58,7 @@ func (p *Packet) BuildResponse(serverDuid []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Packet) BuildMsgAdvertise(serverDuid []byte) ([]byte, error) {
|
func (p *Packet) BuildMsgAdvertise(serverDuid []byte) ([]byte, error) {
|
||||||
in_options, _ := p.UnmarshalOptions()
|
in_options := p.Options
|
||||||
ret_options := make(Options)
|
ret_options := make(Options)
|
||||||
|
|
||||||
ret_options.AddOption(&Option{Id: OptClientId, Length: uint16(len(in_options[OptClientId].Value)), Value: in_options[OptClientId].Value})
|
ret_options.AddOption(&Option{Id: OptClientId, Length: uint16(len(in_options[OptClientId].Value)), Value: in_options[OptClientId].Value})
|
||||||
@ -92,7 +88,7 @@ func (p *Packet) BuildMsgAdvertise(serverDuid []byte) ([]byte, error) {
|
|||||||
// TODO: OptClientArchType may not be present
|
// TODO: OptClientArchType may not be present
|
||||||
|
|
||||||
func (p *Packet) BuildMsgReply(serverDuid []byte) ([]byte, error) {
|
func (p *Packet) BuildMsgReply(serverDuid []byte) ([]byte, error) {
|
||||||
in_options, _ := p.UnmarshalOptions()
|
in_options := p.Options
|
||||||
ret_options := make(Options)
|
ret_options := make(Options)
|
||||||
|
|
||||||
ret_options.AddOption(&Option{Id: OptClientId, Length: uint16(len(in_options[OptClientId].Value)), Value: in_options[OptClientId].Value})
|
ret_options.AddOption(&Option{Id: OptClientId, Length: uint16(len(in_options[OptClientId].Value)), Value: in_options[OptClientId].Value})
|
||||||
@ -116,7 +112,7 @@ func (p *Packet) BuildMsgReply(serverDuid []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Packet) BuildMsgInformationRequestReply(serverDuid []byte) ([]byte, error) {
|
func (p *Packet) BuildMsgInformationRequestReply(serverDuid []byte) ([]byte, error) {
|
||||||
in_options, _ := p.UnmarshalOptions()
|
in_options := p.Options
|
||||||
ret_options := make(Options)
|
ret_options := make(Options)
|
||||||
|
|
||||||
ret_options.AddOption(&Option{Id: OptClientId, Length: uint16(len(in_options[OptClientId].Value)), Value: in_options[OptClientId].Value})
|
ret_options.AddOption(&Option{Id: OptClientId, Length: uint16(len(in_options[OptClientId].Value)), Value: in_options[OptClientId].Value})
|
||||||
@ -138,7 +134,7 @@ func (p *Packet) BuildMsgInformationRequestReply(serverDuid []byte) ([]byte, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Packet) BuildMsgReleaseReply(serverDuid []byte) ([]byte, error){
|
func (p *Packet) BuildMsgReleaseReply(serverDuid []byte) ([]byte, error){
|
||||||
in_options, _ := p.UnmarshalOptions()
|
in_options := p.Options
|
||||||
ret_options := make(Options)
|
ret_options := make(Options)
|
||||||
|
|
||||||
ret_options.AddOption(&Option{Id: OptClientId, Length: uint16(len(in_options[OptClientId].Value)), Value: in_options[OptClientId].Value})
|
ret_options.AddOption(&Option{Id: OptClientId, Length: uint16(len(in_options[OptClientId].Value)), Value: in_options[OptClientId].Value})
|
||||||
@ -172,7 +168,7 @@ func (p *Packet) ShouldDiscard(serverDuid []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ShouldDiscardSolicit(p *Packet) error {
|
func ShouldDiscardSolicit(p *Packet) error {
|
||||||
options, _ := MakeOptions(p.Options)
|
options := p.Options
|
||||||
if !options.RequestedBootFileUrlOption() {
|
if !options.RequestedBootFileUrlOption() {
|
||||||
return fmt.Errorf("'Solicit' packet doesn't have file url option")
|
return fmt.Errorf("'Solicit' packet doesn't have file url option")
|
||||||
}
|
}
|
||||||
@ -186,7 +182,7 @@ func ShouldDiscardSolicit(p *Packet) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ShouldDiscardRequest(p *Packet, serverDuid []byte) error {
|
func ShouldDiscardRequest(p *Packet, serverDuid []byte) error {
|
||||||
options, _ := MakeOptions(p.Options)
|
options := p.Options
|
||||||
if !options.RequestedBootFileUrlOption() {
|
if !options.RequestedBootFileUrlOption() {
|
||||||
return fmt.Errorf("'Request' packet doesn't have file url option")
|
return fmt.Errorf("'Request' packet doesn't have file url option")
|
||||||
}
|
}
|
||||||
@ -203,7 +199,7 @@ func ShouldDiscardRequest(p *Packet, serverDuid []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ShouldDiscardInformationRequest(p *Packet, serverDuid []byte) error {
|
func ShouldDiscardInformationRequest(p *Packet, serverDuid []byte) error {
|
||||||
options, _ := MakeOptions(p.Options)
|
options := p.Options
|
||||||
if !options.RequestedBootFileUrlOption() {
|
if !options.RequestedBootFileUrlOption() {
|
||||||
return fmt.Errorf("'Information-request' packet doesn't have boot file url option")
|
return fmt.Errorf("'Information-request' packet doesn't have boot file url option")
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,7 @@ func (s *ServerV6) serveDHCP(conn *dhcp6.Conn) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
opts, _ := pkt.UnmarshalOptions()
|
s.log("dhcpv6", fmt.Sprintf("Received (%d) packet (%d): %s\n", pkt.Type, pkt.TransactionID, pkt.Options.HumanReadable()))
|
||||||
s.log("dhcpv6", fmt.Sprintf("Received (%d) packet (%d): %s\n", pkt.Type, pkt.TransactionID, opts.HumanReadable()))
|
|
||||||
|
|
||||||
response, _ := pkt.BuildResponse(s.Duid)
|
response, _ := pkt.BuildResponse(s.Duid)
|
||||||
if err := conn.SendDHCP(src, response); err != nil {
|
if err := conn.SendDHCP(src, response); err != nil {
|
||||||
@ -26,8 +25,8 @@ func (s *ServerV6) serveDHCP(conn *dhcp6.Conn) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
reply_packet := dhcp6.MakePacket(response, len(response))
|
reply_packet, _ := dhcp6.MakePacket(response, len(response))
|
||||||
reply_opts,_ := reply_packet.UnmarshalOptions()
|
reply_opts := reply_packet.Options
|
||||||
|
|
||||||
s.log("dhcpv6", fmt.Sprintf("Sent (%d) packet (%d): %s\n", reply_packet.Type, reply_packet.TransactionID, reply_opts.HumanReadable()))
|
s.log("dhcpv6", fmt.Sprintf("Sent (%d) packet (%d): %s\n", reply_packet.Type, reply_packet.TransactionID, reply_opts.HumanReadable()))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user