Skip to content

Add IP_RECVTTL and IP_RECVERR constants to socket module #120056

Closed
@sobolevn

Description

@sobolevn

Feature or enhancement

I recently needed these three constants in a real code, I had to create them manually.

Docs from Linux: https://man7.org/linux/man-pages/man7/ip.7.html

       IP_RECVERR (since Linux 2.2)
              Enable extended reliable error message passing.  When
              enabled on a datagram socket, all generated errors will be
              queued in a per-socket error queue.  When the user
              receives an error from a socket operation, the errors can
              be received by calling [recvmsg(2)](https://man7.org/linux/man-pages/man2/recvmsg.2.html) with the MSG_ERRQUEUE
              flag set.  The sock_extended_err structure describing the
              error will be passed in an ancillary message with the type
              IP_RECVERR and the level IPPROTO_IP.  This is useful for
              reliable error handling on unconnected sockets.  The
              received data portion of the error queue contains the
              error packet.

              The IP_RECVERR control message contains a
              sock_extended_err structure:

                  #define SO_EE_ORIGIN_NONE    0
                  #define SO_EE_ORIGIN_LOCAL   1
                  #define SO_EE_ORIGIN_ICMP    2
                  #define SO_EE_ORIGIN_ICMP6   3

                  struct sock_extended_err {
                      uint32_t ee_errno;   /* error number */
                      uint8_t  ee_origin;  /* where the error originated */
                      uint8_t  ee_type;    /* type */
                      uint8_t  ee_code;    /* code */
                      uint8_t  ee_pad;
                      uint32_t ee_info;    /* additional information */
                      uint32_t ee_data;    /* other data */
                      /* More data may follow */
                  };

                  struct sockaddr *SO_EE_OFFENDER(struct sock_extended_err *);

              ee_[errno](https://man7.org/linux/man-pages/man3/errno.3.html) contains the errno number of the queued error.
              ee_origin is the origin code of where the error
              originated.  The other fields are protocol-specific.  The
              macro SO_EE_OFFENDER returns a pointer to the address of
              the network object where the error originated from given a
              pointer to the ancillary message.  If this address is not
              known, the sa_family member of the sockaddr contains
              AF_UNSPEC and the other fields of the sockaddr are
              undefined.

              IP uses the sock_extended_err structure as follows:
              ee_origin is set to SO_EE_ORIGIN_ICMP for errors received
              as an ICMP packet, or SO_EE_ORIGIN_LOCAL for locally
              generated errors.  Unknown values should be ignored.
              ee_type and ee_code are set from the type and code fields
              of the ICMP header.  ee_info contains the discovered MTU
              for EMSGSIZE errors.  The message also contains the
              sockaddr_in of the node caused the error, which can be
              accessed with the SO_EE_OFFENDER macro.  The sin_family
              field of the SO_EE_OFFENDER address is AF_UNSPEC when the
              source was unknown.  When the error originated from the
              network, all IP options (IP_OPTIONS, IP_TTL, etc.) enabled
              on the socket and contained in the error packet are passed
              as control messages.  The payload of the packet causing
              the error is returned as normal payload.  Note that TCP
              has no error queue; MSG_ERRQUEUE is not permitted on
              SOCK_STREAM sockets.  IP_RECVERR is valid for TCP, but all
              errors are returned by socket function return or SO_ERROR
              only.

              For raw sockets, IP_RECVERR enables passing of all
              received ICMP errors to the application, otherwise errors
              are reported only on connected sockets

              It sets or retrieves an integer boolean flag.  IP_RECVERR
              defaults to off.

       IP_RECVTTL (since Linux 2.2)
              When this flag is set, pass a IP_TTL control message with
              the time-to-live field of the received packet as a 32 bit
              integer.  Not supported for SOCK_STREAM sockets.

Linked PRs

Metadata

Metadata

Assignees

Labels

stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions