Skip to content

Commit 94e3508

Browse files
committed
add MarshalBinary and UnmarshalBinary for VLAN and VLANs
1 parent 8fb0010 commit 94e3508

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

etherconn.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,24 @@ type VLAN struct {
153153
EtherType uint16
154154
}
155155

156+
// MarshalBinary implements encoding.BinaryMarshaler interface
157+
func (vlan VLAN) MarshalBinary() (data []byte, err error) {
158+
data = make([]byte, 4)
159+
binary.BigEndian.PutUint16(data[:2], vlan.ID)
160+
binary.BigEndian.PutUint16(data[2:4], vlan.EtherType)
161+
return
162+
}
163+
164+
// UnmarshalBinary implements encoding.BinaryUnmarshaler interface
165+
func (vlan *VLAN) UnmarshalBinary(data []byte) error {
166+
if len(data) < 4 {
167+
return fmt.Errorf("can't unmarshal VLAN from a byte slice less than 4 bytes")
168+
}
169+
vlan.ID = binary.BigEndian.Uint16(data[:2])
170+
vlan.EtherType = binary.BigEndian.Uint16(data[2:4])
171+
return nil
172+
}
173+
156174
// VLANs is a slice of VLAN
157175
type VLANs []*VLAN
158176

@@ -165,6 +183,31 @@ func (vlans VLANs) String() string {
165183
return s
166184
}
167185

186+
// MarshalBinary implements encoding.BinaryMarshaler interface
187+
func (vlans VLANs) MarshalBinary() (data []byte, err error) {
188+
for _, v := range vlans {
189+
b, _ := v.MarshalBinary()
190+
data = append(data, b...)
191+
}
192+
return
193+
}
194+
195+
// UnmarshalBinary implements encoding.BinaryUnmarshaler interface
196+
func (vlans *VLANs) UnmarshalBinary(data []byte) error {
197+
if len(data) < 4 {
198+
return fmt.Errorf("can't unmarshal VLANs from a byte slice less than 4 bytes")
199+
}
200+
*vlans = []*VLAN{}
201+
for start := 0; start+4 <= len(data); start += 4 {
202+
v := new(VLAN)
203+
if err := v.UnmarshalBinary(data[start : start+4]); err != nil {
204+
return err
205+
}
206+
*vlans = append(*vlans, v)
207+
}
208+
return nil
209+
}
210+
168211
// MarshalText implements encoding.TextMarshaler interface
169212
func (vlans VLANs) MarshalText() (text []byte, err error) {
170213
return []byte(vlans.String()), nil

etherconn_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ func TestRUDPConn(t *testing.T) {
786786
// newIDs is the value for SetIDs, newv is the new VLANs after setIDs
787787
type testVLANsCase struct {
788788
v etherconn.VLANs
789+
vbs []byte
789790
vs string
790791
umvs string
791792
newIDs []uint16
@@ -803,6 +804,7 @@ func TestVLANs(t *testing.T) {
803804
},
804805
},
805806
vs: "|300",
807+
vbs: []byte{0x1, 0x2c, 0x81, 0},
806808
newIDs: []uint16{111},
807809
newv: etherconn.VLANs{
808810
&etherconn.VLAN{
@@ -881,6 +883,34 @@ func TestVLANs(t *testing.T) {
881883
EtherType: 0x8200,
882884
},
883885
},
886+
vbs: []byte{0, 100, 0x81, 0, 0, 200, 0x82, 0},
887+
vs: "|100|200",
888+
umvs: "100|200",
889+
newIDs: []uint16{111, 222},
890+
newv: etherconn.VLANs{
891+
&etherconn.VLAN{
892+
ID: 111,
893+
EtherType: 0x8100,
894+
},
895+
&etherconn.VLAN{
896+
ID: 222,
897+
EtherType: 0x8200,
898+
},
899+
},
900+
},
901+
{
902+
shouldFail: true,
903+
v: etherconn.VLANs{
904+
&etherconn.VLAN{
905+
ID: 100,
906+
EtherType: 0x8100,
907+
},
908+
&etherconn.VLAN{
909+
ID: 200,
910+
EtherType: 0x8200,
911+
},
912+
},
913+
vbs: []byte{0, 100, 0x81, 0, 0, 200, 0x82},
884914
vs: "|100|200",
885915
umvs: "100|200",
886916
newIDs: []uint16{111, 222},
@@ -922,6 +952,25 @@ func TestVLANs(t *testing.T) {
922952
},
923953
}
924954
testFunc := func(c testVLANsCase) error {
955+
if len(c.vbs) != 0 {
956+
buf, err := c.v.MarshalBinary()
957+
if err != nil {
958+
return err
959+
}
960+
if !bytes.Equal(buf, c.vbs) {
961+
return fmt.Errorf("%v marshalbinary result %v is different from expected %v", c.v, buf, c.vbs)
962+
}
963+
newv := new(etherconn.VLANs)
964+
err = newv.UnmarshalBinary(buf)
965+
if err != nil {
966+
return err
967+
}
968+
if !newv.Equal(c.v) {
969+
return fmt.Errorf("%v unmarshalbinary result %v is different", c.v, newv)
970+
}
971+
972+
}
973+
925974
if c.v.String() != c.vs {
926975
return fmt.Errorf("c.v string %v is different from expected %v", c.v.String(), c.vs)
927976
}

0 commit comments

Comments
 (0)