Skip to content

afpacket/pcapgo: fix htons in big endian systems#75

Merged
mosajjal merged 1 commit intogopacket:masterfrom
wgrr:fix-htons
Nov 9, 2024
Merged

afpacket/pcapgo: fix htons in big endian systems#75
mosajjal merged 1 commit intogopacket:masterfrom
wgrr:fix-htons

Conversation

@wgrr
Copy link
Copy Markdown
Contributor

@wgrr wgrr commented Jul 26, 2024

Currently afpacket/pcapgo fails to receive any packet in big endian hosts because the interface index is never matched due the swap, regardless the host endianess

An alternative for unsafe would be add a dependency to Go maintained x/sys/cpu and hope the tag list is maintained accordingly as architectures are added and removed, I found it too big of a dependency for a simple question, and that's cheap to compute at runtime, but it's up to you.

The old implementation swaps the bits regardless of the host endianess.

Signed-off-by: wagner riffel <w@104d.net>
@mosajjal mosajjal merged commit 5c63dfa into gopacket:master Nov 9, 2024
@mejedi
Copy link
Copy Markdown
Contributor

mejedi commented Dec 13, 2024

I don't think the use of unsafe is justified here.

Surprisingly, idiomatic dumb code is pretty efficient:

func htons(v uint16) uint16 {
	var buf [4]byte
	binary.NativeEndian.PutUint16(buf[:], v)
	return binary.BigEndian.Uint16(buf[:])
}

https://godbolt.org/z/KzhzeTo9r

@mejedi
Copy link
Copy Markdown
Contributor

mejedi commented Jan 23, 2025

#108

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants