Uncooked community sockets are a curious beasts, as until you’ve a robust urge to implement your personal low-level community protocol, it’s a subject that’s most likely greatest left to the (well-paid) specialists. That mentioned, you possibly can completely use uncooked sockets in nearly each working system, however one needs to be conscious of some issues, the dearth of portability being one in all them. That is what tripped [Swagnik] up whereas making an attempt to jot down a low-level network ping (ICMP) utility, by studying the Linux socket documentation whereas testing on MacOS. It’s all BSD-style sockets, in spite of everything, proper?
Because it seems, the community stacks in Linux and MacOS have some refined variations, which change into obvious if you learn the pleasant manuals. For Linux, the raw(7) man entry for IPv4 sockets make it clear that the IP_HDRINCL
socket possibility is default by default for IPPROTO_RAW
sockets. That is completely different from MacOS, which is successfully FreeBSD with shiny make-up. Like FreeBSD, the MacOS man page makes it clear that the IP_HDRINCL
possibility shouldn’t be set by default.
In order that’s simple, proper? Simply fireplace off a setsockopt()
name on the uncooked socket and that’s carried out. Not fairly. The Linux man web page notes that it can’t obtain all IP protocols, whereas the FreeBSD/MacOS model makes no such exceptions. There’s additionally the difficulty of endianness, which is the place [Swagnik]’s weblog publish appears to err. The declare is that on MacOS the acquired IPv4 uncooked socket header is in host (little endian) order, whereas the documentation clearly notes that these are in community (large endian) order, which the weblog publish additionally exhibits.
The place issues get actually enjoyable is when transferring from IPv4 uncooked sockets to IPv6 uncooked sockets, as [Michael F. Schönitzer] covered for Linux again in 2018 already. IPv6 uncooked sockets drop IP_HDRINCL
and requires an entire completely different strategy. The endianness additionally adjustments, as IPv6 uncooked sockets underneath Linux should ship and can obtain knowledge in community byte order, placing it according to FreeBSD uncooked sockets.