Add Protocol type and socket_with_protocol() for arbitrary protocol numbers#2733
Open
batkovic75 wants to merge 1 commit intonix-rust:masterfrom
Open
Add Protocol type and socket_with_protocol() for arbitrary protocol numbers#2733batkovic75 wants to merge 1 commit intonix-rust:masterfrom
batkovic75 wants to merge 1 commit intonix-rust:masterfrom
Conversation
3427eb2 to
1c155d4
Compare
Add a new `Protocol` type and `socket_with_protocol()` function that allow
creating sockets with arbitrary protocol numbers not defined in `SockProtocol`.
This is particularly useful for AF_PACKET sockets where Ethernet protocol
numbers (like ETH_P_ARP, ETH_P_802_1Q, etc.) need to be specified.
The `Protocol::ethernet()` helper automatically handles the conversion to
network byte order, which is required for Ethernet protocols.
Example usage:
```rust
let proto = Protocol::ethernet(libc::ETH_P_ARP as u16);
let sock = socket_with_protocol(
AddressFamily::Packet,
SockType::Raw,
SockFlag::empty(),
proto,
)?;
```
1c155d4 to
51e966f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a new
Protocoltype andsocket_with_protocol()function that allow creating sockets with arbitrary protocol numbers not defined in theSockProtocolenum.This is particularly useful for
AF_PACKETsockets where Ethernet protocol numbers (likeETH_P_ARP,ETH_P_8021Q, etc.) need to be specified in network byte order.New APIs
Protocol::new(c_int)- create from raw protocol number (all platforms)Protocol::ethernet(u16)- create from Ethernet protocol with automatic byte order conversion (Linux/Android)Protocol::as_raw()- get the raw valuesocket_with_protocol()- likesocket()but acceptsProtocolimpl From<SockProtocol> for Protocol- convert existing protocolsExample
Design Rationale
This takes an additive approach rather than modifying the existing
SockProtocolenum:socket()API remains unchangedRelated PRs took different approaches:
Htons(u16)andInteger(i32)variants (abandoned)SockProtocolwith a wrapper type everywhere (stale)Eth*variants - this PR provides an escape hatch for the restCloses #854
Test plan
test_protocol_ethernettest that verifies byte order conversion and socket creation