Some refactoring of packet.go and proper comments
This commit is contained in:
parent
a89787613e
commit
1daf8f01e1
|
@ -12,26 +12,9 @@ import (
|
|||
|
||||
// FieldValue implements protocol.ProtocolFieldReferencer
|
||||
type FieldValue struct {
|
||||
Field string
|
||||
FieldNum int
|
||||
|
||||
Value string
|
||||
}
|
||||
|
||||
// GetProtocolField returns the protocol field
|
||||
func (fv *FieldValue) GetProtocolField() (string, int) {
|
||||
return fv.Field, fv.FieldNum
|
||||
}
|
||||
|
||||
// SetProtocolField sets the protocl field
|
||||
func (fv *FieldValue) SetProtocolField(f string, fn int) {
|
||||
fv.Field = f
|
||||
fv.FieldNum = fn
|
||||
}
|
||||
|
||||
// SetValue sets the value
|
||||
func (fv *FieldValue) SetValue(v string) {
|
||||
fv.Value = v
|
||||
Generate bool
|
||||
}
|
||||
|
||||
// Layer implements protocol.ProtocolReferencer
|
||||
|
@ -50,29 +33,33 @@ type Packet struct {
|
|||
Layers []Layer
|
||||
}
|
||||
|
||||
// NewPacketStructure creates a new empty packet structure
|
||||
func NewPacketStructure() *Packet {
|
||||
p := Packet{}
|
||||
return &p
|
||||
}
|
||||
|
||||
// UpdateMetaData is still a method stub
|
||||
func UpdateMetaData(
|
||||
pack *Packet,
|
||||
) {
|
||||
// still empty
|
||||
}
|
||||
|
||||
// NewPacketLayer creates a new empty Layer structure
|
||||
func NewPacketLayer() *Layer {
|
||||
p := Layer{}
|
||||
return &p
|
||||
}
|
||||
|
||||
// NewEmptyFieldValue creates a new empty field value
|
||||
func NewEmptyFieldValue() *FieldValue {
|
||||
f := FieldValue{}
|
||||
return &f
|
||||
}
|
||||
|
||||
// NewFieldValue creates a new prefilled field value
|
||||
func NewFieldValue(
|
||||
field string,
|
||||
fieldnum int,
|
||||
value string,
|
||||
) *FieldValue {
|
||||
f := FieldValue{
|
||||
|
@ -81,12 +68,14 @@ func NewFieldValue(
|
|||
return &f
|
||||
}
|
||||
|
||||
// AppendField appends a new FieldValue to the Layer
|
||||
func (pack *Layer) AppendField(field *FieldValue) int {
|
||||
i := len(pack.Values)
|
||||
pack.Values = append(pack.Values, *field)
|
||||
return i + 1
|
||||
}
|
||||
|
||||
// AddField adds a FieldValue at any position
|
||||
func (pack *Layer) AddField(index int, field *FieldValue) {
|
||||
if len(pack.Values) == index {
|
||||
pack.AppendField(field)
|
||||
|
@ -99,10 +88,12 @@ func (pack *Layer) AddField(index int, field *FieldValue) {
|
|||
pack.Values = ret
|
||||
}
|
||||
|
||||
// AppendLayer appends a Layer to the list of Layers
|
||||
func (pack *Packet) AppendLayer(layer *Layer) {
|
||||
pack.Layers = append(pack.Layers, *layer)
|
||||
}
|
||||
|
||||
// AddLayer adds a Layer at any position
|
||||
func (pack *Packet) AddLayer(index int, layer *Layer) {
|
||||
if len(pack.Layers) == index {
|
||||
pack.AppendLayer(layer)
|
||||
|
@ -115,14 +106,17 @@ func (pack *Packet) AddLayer(index int, layer *Layer) {
|
|||
pack.Layers = ret
|
||||
}
|
||||
|
||||
// UpdateField updates a field of a layer
|
||||
func (pack *Layer) UpdateField(e int, field *FieldValue) {
|
||||
pack.Values[e] = *field
|
||||
}
|
||||
|
||||
// UpdateLayer updates a Layer with a reference to a new Layer
|
||||
func (pack *Packet) UpdateLayer(e int, layer *Layer) {
|
||||
pack.Layers[e] = *layer
|
||||
}
|
||||
|
||||
// RemoveField removes a field from a Layer
|
||||
func (pack *Layer) RemoveField(e int) {
|
||||
l := len(pack.Values) - 1
|
||||
ret := make([]FieldValue, l)
|
||||
|
@ -131,6 +125,7 @@ func (pack *Layer) RemoveField(e int) {
|
|||
pack.Values = ret
|
||||
}
|
||||
|
||||
// RemoveLayer removes a layer from the list at specified index
|
||||
func (pack *Packet) RemoveLayer(e int) {
|
||||
l := len(pack.Layers) - 1
|
||||
ret := make([]Layer, l)
|
||||
|
@ -139,6 +134,7 @@ func (pack *Packet) RemoveLayer(e int) {
|
|||
pack.Layers = ret
|
||||
}
|
||||
|
||||
// Load a packet from a file path
|
||||
func (pack *Packet) Load(path string) error {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
|
@ -155,12 +151,14 @@ func (pack *Packet) Load(path string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// UpdateData updates the data holding elements with the actual hex/b64 binary representations and a sha1 hash of it.
|
||||
func (pack *Packet) UpdateData() {
|
||||
pack.Hex = fmt.Sprintf("%x", pack.data)
|
||||
pack.B64 = base64.RawStdEncoding.EncodeToString(pack.data)
|
||||
pack.Sha1 = fmt.Sprintf("%x", sha1.Sum(pack.data))
|
||||
}
|
||||
|
||||
// ToJSON returns a JSON string of the packet structure
|
||||
func (pack *Packet) ToJSON() string {
|
||||
pack.UpdateData()
|
||||
data, err := json.MarshalIndent(*pack, "", " ")
|
||||
|
@ -170,6 +168,7 @@ func (pack *Packet) ToJSON() string {
|
|||
return string(data)
|
||||
}
|
||||
|
||||
// Save the packet structure as JSON file at specified path
|
||||
func (pack *Packet) Save(path string) error {
|
||||
pack.UpdateData()
|
||||
data, err := json.MarshalIndent(*pack, "", " ")
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
package packet
|
||||
|
||||
// Meta struct is the absolute minimum of metadata
|
||||
type Meta struct {
|
||||
Name string
|
||||
Revision int
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package protocol
|
||||
|
||||
// [impl->dsn~protocol-data-architecture~0>>utest]
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package protocol
|
||||
|
||||
// [utest->dsn~protocol-data-architecture~0]
|
||||
// [utest->impl~protocol-data-architecture~0]
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
|
|
Loading…
Reference in New Issue