python / python/cpython

socket.AF_PACKET doc: proto is *not* in network byte order

Open
#100,231 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs topic-socket
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Documentation

The documentation of socket.AF_PACKET says that the value of proto in the address representation must be in network-byte-order:

https://github.com/python/cpython/blame/v3.12.0a3/Doc/library/socket.rst#L196

Value must be in network-byte-order.

This is incorrect. It is true that the corresponding Linux field sll_protocol is in network byte order (see packet(7)), but Python automatically converts to/from host byte order:

https://github.com/python/cpython/blob/v3.12.0a3/Modules/socketmodule.c#L1483

https://github.com/python/cpython/blob/v3.12.0a3/Modules/socketmodule.c#L2104

Note: This applies to addresses passed to socket.bind() and returned from socket.recvfrom(), but does not apply to the proto parameter of socket.socket() -- Python doesn't automatically convert it, so the user must supply a value in network byte order (when using AF_PACKET). It might be a good idea to document this, e.g. the documentation of ETH_P_ALL could mention that it needs to be converted using socket.htons() before passing to socket.socket().

Bonus typo:

https://github.com/python/cpython/blame/v3.12.0a3/Doc/library/socket.rst#L188

The packets are represented by the tuple

"packets" is inaccurate and should be changed to "addresses" or "sockets".

Linked PRs
  • gh-154741

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open Doc/library/socket.rst at the AF_PACKET address representation and review the cited Modules/socketmodule.c locations for the byte-order behavior. Update the documentation to distinguish bind/recvfrom addresses from the socket() proto parameter, and correct the “packets” wording; done means the documented conversions and terminology match the implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation, networking
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.