#define AF_UNIX 3 #define SOCK_DGRAM 0b000000000000000001 #define SOCK_RAW 0b000000000000000010 #define SOCK_STREAM 0b000000000000000100 #define SOCK_NONBLOCK 0b010000000000000000 #define SOCK_CLOEXEC 0b100000000000000000 int socket(int domain, int type);
This syscall for creating sockets, the passed fields can be used for selecting the type of socket to create. The available sockets type are:
AF_INET
: Basically IPv4 socket.
The address of a INET domain socket takes the shape of
struct sockaddr_in { uint32_t sin_family; uint16_t sin_port; char sin_addr[4]; uint8_t pad[8]; };
type
can be one of SOCK_DGRAM
or SOCK_STREAM
.
SOCK_DGRAM
will be translated to TCP
, while SOCK_STREAM
will be translated to UDP
.
AF_INET6
: Basically IPv6 socket.
The address of a INET domain socket takes the shape of
struct sockaddr_in6 { uint32_t sin6_family; uint16_t sin6_port; uint32_t sin6_flowinfo; char sin6_addr[16]; uint32_t sin6_scope_id; };
type
can be one of SOCK_DGRAM
or SOCK_STREAM
.
SOCK_DGRAM
will be translated to TCP
, while SOCK_STREAM
will be translated to UDP
.
AF_UNIX
: UNIX domain socket for local communication, this sockets
can be unnamed or bound to filesystem paths.
The address of a UNIX domain socket takes the shape of
struct sockaddr_un { uint32_t sun_family; // AF_UNIX. char path[]; // Must be null terminated. };
type
can be one of:
SOCK_DGRAM
: Unreliable, connection-less, datagram-based interface. When
used with INET protocols, it will correspond to UDP. When connected, these
sockets will just cache the address for further reception / sending.
SOCK_STREAM
: Reliable, connection-based stream-based interface.
Connection, accepting, and listening will be necessary for a proper handshake.
SOCK_RAW
: Raw communication directly with the domain layer. When using
these, no TCP or UDP will be done whatsoever, and the user will be free to
implement their own protocol on top, or none at all and just use the domain
datagram transport. Not supported for some protocols, like UNIX domain sockets.
Any socket type may have type
be OR’ed with SOCK_NONBLOCK
or
SOCK_CLOEXEC
for setting the created socket nonblock or cloese on exec
respectively.
The syscall returns the resulting FD or -1
on failure, with the
following errno:
EINVAL
: Invalid combination of flags.
EMFILE
: No available file descriptor slots.