mirror of
https://github.com/renorris/openfsd
synced 2026-03-23 07:15:34 +08:00
40 lines
842 B
Go
40 lines
842 B
Go
package protocol
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type PingPDU struct {
|
|
From string `validate:"required,alphanum,max=7"`
|
|
To string `validate:"required,alphanum,max=7"`
|
|
Timestamp string `validate:"required,max=32"`
|
|
}
|
|
|
|
func (p *PingPDU) Serialize() string {
|
|
return fmt.Sprintf("$PI%s:%s:%s%s", p.From, p.To, p.Timestamp, PacketDelimeter)
|
|
}
|
|
|
|
func ParsePingPDU(rawPacket string) (*PingPDU, error) {
|
|
rawPacket = strings.TrimSuffix(rawPacket, PacketDelimeter)
|
|
rawPacket = strings.TrimPrefix(rawPacket, "$PI")
|
|
fields := strings.Split(rawPacket, Delimeter)
|
|
|
|
if len(fields) != 3 {
|
|
return nil, NewGenericFSDError(SyntaxError)
|
|
}
|
|
|
|
pdu := PingPDU{
|
|
From: fields[0],
|
|
To: fields[1],
|
|
Timestamp: fields[2],
|
|
}
|
|
|
|
err := V.Struct(pdu)
|
|
if err != nil {
|
|
return nil, NewGenericFSDError(SyntaxError)
|
|
}
|
|
|
|
return &pdu, nil
|
|
}
|