IPPROTO_IP vs IPPROTO_TCP/IPPROTO_UDP

前端 未结 2 1284
眼角桃花
眼角桃花 2020-12-24 08:19

I\'m having some trouble finding documentation on what the distinction between these settings for the third argument to socket is. I know about TCP and UDP and

2条回答
  •  生来不讨喜
    2020-12-24 08:44

    From the socket(2) Linux manual page, from man-pages 5.08:

    Normally only a single protocol exists to support a particular socket type within a given protocol family, in which case protocol can be specified as 0. However, it is possible that many protocols may exist, in which case a particular protocol must be specified in this manner.

    IPPROTO_IP is just the usual placeholder constant from the IPPROTO_* family with the value 0 in the socket API since its early days.

    Of course in the interest of forward compatibility, in case more socket types are added for the protocol family in the future, which could break 0 with UDP/TCP or change its behaviour, it would be best to look up the specific protocol value for the protocol you want and use it, rather than using 0 or IPPROTO_IP.

    Note that the IPPROTO_* header values serve multiple purposes in different contexts: in addition to being used as parameter values to identify protocols here in the socket API and internally in its implementation code, they are also used as the values of the IP protocol field in the IP packets themselves. These IP protocol values, or protocol numbers in IANA terminology, themselves have an additional purpose, identifying different extension header types in IPv6. This is why 0 is also IPPROTO_HOPOPTS, the IPv6 Hop-by-Hop Option extension header identifier.

提交回复
热议问题