Adding new socket options - specifically socket.IPV6_HDRINCL - Ideas - Discussions on Python.org
Adding new socket options - specifically socket.IPV6_HDRINCL Ideas Rosuav (Chris Angelico) November 20, 2025, 6:46am 1 On Linux, you can create a raw IPv4 socket and send something out with your own hand-crafted IP header. To do this, you need to sock.setsockopt(socket.AF_INET, socket.IP_HDRINCL, 1) to prevent the normal IP header from being added. On IPv6 sockets, this can still be done, but there’s no socket.IPV6_HDRINCL constant.
The number to use is 36 (not the same as IP_HDRINCL), and calling setsockopt does have the correct effect, since the underlying kernel does support this. How do new socket option constants get added? Can this one get added? For the time being, I’m just using the raw integer, but that’s ugly. encukou (Petr Viktorin) November 20, 2025, 7:25am 2 How do new socket option constants get added? See #120058 for example. Ping me for review, or if you need any help.
2 Likes Rosuav (Chris Angelico) November 20, 2025, 7:37am 3 Ah cool, thanks. Will get to that shortly! 1 Like Rosuav (Chris Angelico) November 21, 2025, 10:19am 4 Issue: Add socket constant IPV6_HDRINCL and maybe others · Issue #141817 · python/cpython · GitHub PR: gh-141817: Add IPV6_HDRINCL constant to the socket module by Rosuav · Pull Request #141818 · python/cpython · GitHub I made a script to search the Linux header files for more constants.
How many of these should also be added? There are a good many. [1] #ifndef _LINUX_IN_H Found a define: IP_ROUTER_ALERT 5 Found a define: IP_PKTOPTIONS 9 Found a define: IP_MTU_DISCOVER 10 Found a define: IP_MTU 14 Found a define: IP_IPSEC_POLICY 16 Found a define: IP_XFRM_POLICY 17 Found a define: IP_PASSSEC 18 Found a define: IP_ORIGDSTADDR 20 Found a define: IP_MINTTL 21 Found a define: IP_NODEFRAG 22 Found a define: IP_CHECKSUM 23 Found a define: IP_RECVFRAGSIZE 25 Found a define: IP_RECVERR_RFC4884 26 Found a define: IP_PMTUDISC_DONT 0 /* Never send DF frames */ Found a define: IP_PMTUDISC_WANT 1 /* Use per route hints */ Found a define: IP_PMTUDISC_DO 2 /* Always DF */ Found a define: IP_PMTUDISC_PROBE 3 /* Ignore dst pmtu */ Found a define: IP_PMTUDISC_INTERFACE 4 Found a define: IP_PMTUDISC_OMIT 5 Found a define: IP_MSFILTER 41 Found a define: MCAST_JOIN_GROUP 42 Found a define: MCAST_BLOCK_SOURCE 43 Found a define: MCAST_UNBLOCK_SOURCE 44 Found a define: MCAST_LEAVE_GROUP 45 Found a define: MCAST_JOIN_SOURCE_GROUP 46 Found a define: MCAST_LEAVE_SOURCE_GROUP 47 Found a define: MCAST_MSFILTER 48 Found a define: IP_MULTICAST_ALL 49 Found a define: IP_UNICAST_IF 50 Found a define: IP_LOCAL_PORT_RANGE 51 Found a define: IP_PROTOCOL 52 Found a define: MCAST_EXCLUDE 0 Found a define: MCAST_INCLUDE 1 #if __UAPI_DEF_SOCKADDR_IN Found a define: __SOCK_SIZE__ 16 /* sizeof(struct sockaddr) */ #endif #if __UAPI_DEF_IN_CLASS Found a define: IN_CLASSA_NSHIFT 24 Found a define: IN_CLASSA_MAX 128 Found a define: IN_CLASSB_NSHIFT 16 Found a define: IN_CLASSB_MAX 65536 Found a define: IN_CLASSC_NSHIFT 8 Found a define: IN_CLASSE_NSHIFT 0 Found a define: IN_LOOPBACKNET 127 #endif #endif /* _LINUX_IN_H */ #ifndef _LINUX_IN6_H Found a define: IPV6_FL_A_GET 0 Found a define: IPV6_FL_A_PUT 1 Found a define: IPV6_FL_A_RENEW 2 Found a define: IPV6_FL_F_CREATE 1 Found a define: IPV6_FL_F_EXCL 2 Found a define: IPV6_FL_F_REFLECT 4 Found a define: IPV6_FL_F_REMOTE 8 Found a define: IPV6_FL_S_NONE 0 Found a define: IPV6_FL_S_EXCL 1 Found a define: IPV6_FL_S_PROCESS 2 Found a define: IPV6_FL_S_USER 3 Found a define: IPV6_FL_S_ANY 255 #if __UAPI_DEF_IPPROTO_V6 Found a define: IPPROTO_MH 135 /* IPv6 mobility header */ #endif /* __UAPI_DEF_IPPROTO_V6 */ Found a define: IPV6_TLV_PAD1 0 Found a define: IPV6_TLV_PADN 1 Found a define: IPV6_TLV_ROUTERALERT 5 Found a define: IPV6_TLV_CALIPSO 7 /* RFC 5570 */ Found a define: IPV6_TLV_IOAM 49 /* RFC 9486 */ Found a define: IPV6_TLV_JUMBO 194 Found a define: IPV6_TLV_HAO 201 /* home address option */ #if __UAPI_DEF_IPV6_OPTIONS Found a define: IPV6_ADDRFORM 1 Found a define: IPV6_2292PKTINFO 2 Found a define: IPV6_2292HOPOPTS 3 Found a define: IPV6_2292DSTOPTS 4 Found a define: IPV6_2292RTHDR 5 Found a define: IPV6_2292PKTOPTIONS 6 Found a define: IPV6_2292HOPLIMIT 8 Found a define: IPV6_AUTHHDR 10 /* obsolete */ Found a define: IPV6_FLOWINFO 11 Found a define: IPV6_ADD_MEMBERSHIP 20 Found a define: IPV6_DROP_MEMBERSHIP 21 Found a define: IPV6_ROUTER_ALERT 22 Found a define: IPV6_MTU_DISCOVER 23 Found a define: IPV6_MTU 24 Found a define: IPV6_JOIN_ANYCAST 27 Found a define: IPV6_LEAVE_ANYCAST 28 Found a define: IPV6_MULTICAST_ALL 29 Found a define: IPV6_ROUTER_ALERT_ISOLATE 30 Found a define: IPV6_RECVERR_RFC4884 31 Found a define: IPV6_PMTUDISC_DONT 0 Found a define: IPV6_PMTUDISC_WANT 1 Found a define: IPV6_PMTUDISC_DO 2 Found a define: IPV6_PMTUDISC_PROBE 3 Found a define: IPV6_PMTUDISC_INTERFACE 4 Found a define: IPV6_PMTUDISC_OMIT 5 Found a define: IPV6_FLOWLABEL_MGR 32 Found a define: IPV6_FLOWINFO_SEND 33 Found a define: IPV6_IPSEC_POLICY 34 Found a define: IPV6_XFRM_POLICY 35 Found a define: IPV6_HDRINCL 36 #endif #if 0 /* not yet */ Found a define: IPV6_USE_MIN_MTU 63 #endif Found a define: IPV6_AUTOFLOWLABEL 70 Found a define: IPV6_ADDR_PREFERENCES 72 Found a define: IPV6_MINHOPCOUNT 73 Found a define: IPV6_ORIGDSTADDR 74 Found a define: IPV6_TRANSPARENT 75 Found a define: IPV6_UNICAST_IF 76 Found a define: IPV6_RECVFRAGSIZE 77 Found a define: IPV6_FREEBIND 78 #endif /* _LINUX_IN6_H */ I’m happy to add any more that are of interest, while we’re doing the whole adding of constants thing.
One might even say there are a thundering lot . ↩︎ vstinner (Victor Stinner) November 22, 2025, 5:15pm 5 What is the purpose of these constants? Are they commonly used? Usually I prefer to only add constants when someone comes with an use case. 1 Like Rosuav (Chris Angelico) November 22, 2025, 8:14pm 6 I have no idea - but then, up until a week ago, that would have been my response about IPV6_HDRINCL too. That’s fair.
The downside is, not everyone knows how to get more constants added, and maybe won’t even know that the constants are missing. It’s like when Alice noted that the Hatter was missing a few parts, and he said that, things being what they were, he hardly missed them. [1] At the time, his head wasn’t even attached to his torso, never mind his arms and legs. It all makes sense in context… ↩︎