What are the
type arguments to the
socket system call?
socket system call:
#include <sys/socket.h> int socket(int domain, int type, int protocol);
Those three arguments determine the protocol we want to use. Actually, the last argument alone -
protocol - seems to fully determine the protocol. For example,
6 is the protocol number for TCP. The
type describe this protocol, and usually the combination of
type also fully determines the protocol, but not always.
It seems to be traditional to pass in the
type, and set
0, meaning the default protocol for the
type pair will be selected. (The
man pages don’t actually describe this behavior, but various network guides do.) For example, this call creates a TCP socket:
int fd = socket(PF_INET, SOCK_STREAM, 0);
PF_INET says “TCP operates in the internet protocol family”.
PF means “protocol family”, and
INET means “internet”. As far as I can tell, “protocol family” and The type
SOCK_STREAM says “TCP provides an ordered, reliable, bidirectional byte stream”.
Most code actually uses
AF_ rather than
AF means “address family”, and the idea was that each “address family” could support several “protocol families”. This didn’t happen; e.g., IPv4 addresses only work in the internet protocol family. Guides say to just use the
AF_ constants; the
PF_ constants are aliased to the
AF_ ones in all implementations.
Here are some more examples:
socket(AF_INET, SOCK_STREAM, 0); // Selects TCP socket(AF_INET, SOCK_STREAM, 6); // Also selects TCP socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Same as above; selects TCP socket(AF_INET, SOCK_DGRAM, 0); // Selects UDP socket(AF_INET, SOCK_SEQPACKET, 0); // Selects SCTP (not implemented on macOS) socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); // Selects the Bluetooth RFCOMM protocol
More by Jim
- Your syntax highlighter is wrong
- Granddad died today
- The Three Ts of Time, Thought and Typing: measuring cost on the web
- I hate telephones
- The sorry state of OpenSSL usability
- The dots do matter: how to scam a Gmail user
- My parents are Flat-Earthers
- How Hacker News stays interesting
- Project C-43: the lost origins of asymmetric crypto
- The hacker hype cycle
- The inception bar: a new phishing method
- Time is running out to catch COVID-19
- A probabilistic pub quiz for nerds
- Smear phishing: a new Android vulnerability
Tagged . All content copyright James Fisher 2017. This post is not associated with my employer. Found an error? Edit this page.