Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocate 6 bytes to copy MAC addresses when unmarshalling ethernet match fields #30 #62

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions openflow13/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ func (m *EthDstField) MarshalBinary() (data []byte, err error) {
}

func (m *EthDstField) UnmarshalBinary(data []byte) error {
m.EthDst = make([]byte, 6)
copy(m.EthDst, data)
return nil
}
Expand Down Expand Up @@ -700,6 +701,7 @@ func (m *EthSrcField) MarshalBinary() (data []byte, err error) {
}

func (m *EthSrcField) UnmarshalBinary(data []byte) error {
m.EthSrc = make([]byte, 6)
copy(m.EthSrc, data)
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions openflow15/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ func (m *EthDstField) MarshalBinary() (data []byte, err error) {
}

func (m *EthDstField) UnmarshalBinary(data []byte) error {
m.EthDst = make([]byte, 6)
copy(m.EthDst, data)
return nil
}
Expand Down Expand Up @@ -899,6 +900,7 @@ func (m *EthSrcField) MarshalBinary() (data []byte, err error) {
}

func (m *EthSrcField) UnmarshalBinary(data []byte) error {
m.EthSrc = make([]byte, 6)
copy(m.EthSrc, data)
return nil
}
Expand Down
55 changes: 55 additions & 0 deletions openflow15/match_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package openflow15_test

import (
"bytes"
"fmt"
"net"
"testing"

"antrea.io/libOpenflow/openflow15"
)

func TestMatchEthAddresses(t *testing.T) {
ethSrcAddress, _ := net.ParseMAC("aa:aa:aa:aa:aa:aa")
ethDstAddress, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff")

ofMatch := openflow15.NewMatch()
{
macSrcField := openflow15.NewEthSrcField(ethSrcAddress, nil)
ofMatch.AddField(*macSrcField)

macDstField := openflow15.NewEthDstField(ethDstAddress, nil)
ofMatch.AddField(*macDstField)
}

if err := checkMatchSerializationConsistency(ofMatch); err != nil {
t.Fatal(err)
}
}

func checkMatchSerializationConsistency(ofMatch *openflow15.Match) error {
// Serialize the original match
ofMatchRaw, err := ofMatch.MarshalBinary()
if err != nil {
return fmt.Errorf("failed to marshal match: %w", err)
}

// Deserialize into a new match object
ofMatchRecovered := openflow15.NewMatch()
if err := ofMatchRecovered.UnmarshalBinary(ofMatchRaw); err != nil {
return fmt.Errorf("failed to unmarshal match: %w", err)
}

// Serialize the recovered match for comparison
ofMatchRecoveredRaw, err := ofMatchRecovered.MarshalBinary()
if err != nil {
return fmt.Errorf("failed to marshal recovered match: %w", err)
}

// Check for serialization consistency
if !bytes.Equal(ofMatchRaw, ofMatchRecoveredRaw) {
return fmt.Errorf("initial and recovered match structures do not match")
}

return nil
}
Loading