2018-07-23 10:44:30 -06:00
|
|
|
package netshovel
|
2018-07-23 09:58:31 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"github.com/dirtbags/netshovel/gapstring"
|
|
|
|
)
|
|
|
|
|
2018-07-23 15:34:22 -06:00
|
|
|
type PacketFactory func()Packet
|
|
|
|
|
2018-07-23 09:58:31 -06:00
|
|
|
type Packet struct {
|
2018-07-23 15:34:22 -06:00
|
|
|
Name string
|
2018-07-23 09:58:31 -06:00
|
|
|
Opcode int
|
|
|
|
Description string
|
|
|
|
When time.Time
|
|
|
|
Payload gapstring.GapString
|
|
|
|
Fields map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
var never = time.Unix(0, 0)
|
|
|
|
|
|
|
|
func NewPacket() Packet {
|
|
|
|
return Packet{
|
2018-07-23 15:34:22 -06:00
|
|
|
Name: "Generic",
|
2018-07-23 09:58:31 -06:00
|
|
|
Opcode: -1,
|
|
|
|
Description: "Undefined",
|
|
|
|
When: never,
|
|
|
|
Payload: gapstring.GapString{},
|
|
|
|
Fields: map[string]string{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pkt *Packet) Describe() string {
|
|
|
|
out := new(strings.Builder)
|
|
|
|
|
2018-07-23 15:34:22 -06:00
|
|
|
fmt.Fprintf(out, " %s %s %d: %s\n",
|
2018-07-23 10:44:30 -06:00
|
|
|
pkt.When.UTC().Format(time.RFC3339Nano),
|
2018-07-23 15:34:22 -06:00
|
|
|
pkt.Name,
|
2018-07-23 09:58:31 -06:00
|
|
|
pkt.Opcode,
|
|
|
|
pkt.Description,
|
|
|
|
)
|
|
|
|
keys := make([]string, len(pkt.Fields))
|
|
|
|
i := 0
|
|
|
|
for k := range(pkt.Fields) {
|
|
|
|
keys[i] = k
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
2018-07-23 15:34:22 -06:00
|
|
|
for _, k := range keys {
|
|
|
|
fmt.Fprintf(out, " %s: %s\n", k, pkt.Fields[k])
|
|
|
|
}
|
|
|
|
fmt.Fprint(out, pkt.Payload.Hexdump())
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pkt *Packet) Set(key, value string) {
|
|
|
|
pkt.Fields[key] = value
|
2018-07-23 09:58:31 -06:00
|
|
|
}
|