Skip to content
BSD syscall#97

svc · unix #97

socket

Creates a new communication endpoint and returns a file descriptor referring to it.

Prototype

int socket(int domain, int type, int protocol);

Returns: int

Arguments

NameTypeDirDescription
domainint-
typeint-
protocolint-

Version history

XNU tagmacOS#
xnu-1456.1.26macOS 10.6 Snow Leopard97
xnu-1699.24.8macOS 10.7 Lion97
xnu-2050.18.24macOS 10.8 Mountain Lion97
xnu-2422.115.4macOS 10.9 Mavericks97
xnu-2782.40.9macOS 10.10 Yosemite97
xnu-3247.1.106macOS 10.11 El Capitan97
xnu-3789.1.32macOS 10.12 Sierra97
xnu-4570.1.46macOS 10.13 High Sierra97
xnu-4903.221.2macOS 10.14 Mojave97
xnu-6153.11.26macOS 10.15 Catalina97
xnu-7195.50.7.100.1macOS 11.0 Big Sur97
xnu-8019.41.5macOS 12.0 Monterey97
xnu-8792.41.9macOS 13.0 Ventura97
xnu-10002.1.13macOS 14.0 Sonoma97
xnu-11215.1.10macOS 15.0 Sequoia97
xnu-11417.101.15macOS 15.4 Sequoia97
xnu-12377.1.9macOS 26.0 Tahoe97
xnu-10002.41.997
xnu-10002.61.397
xnu-10002.81.597
xnu-10063.101.1597
xnu-10063.121.397
xnu-10063.141.197
xnu-11215.41.397
xnu-11215.61.597
xnu-11215.81.497
xnu-11417.121.697
xnu-11417.140.6997
xnu-12377.101.1597
xnu-12377.41.697
xnu-12377.61.1297
xnu-12377.81.497
xnu-1486.2.1197
xnu-1504.15.397
xnu-1504.3.1297
xnu-1504.7.497
xnu-1504.9.1797
xnu-1504.9.2697
xnu-1504.9.3797
xnu-1699.22.7397
xnu-1699.22.8197
xnu-1699.24.2397
xnu-1699.26.897
xnu-1699.32.797
xnu-2050.22.1397
xnu-2050.24.1597
xnu-2050.48.1197
xnu-2050.7.997
xnu-2050.9.297
xnu-2422.1.7297
xnu-2422.100.1397
xnu-2422.110.1797
xnu-2422.90.2097
xnu-2782.1.9797
xnu-2782.10.7297
xnu-2782.20.4897
xnu-2782.30.597
xnu-3247.10.1197
xnu-3248.20.5597
xnu-3248.30.497
xnu-3248.40.18497
xnu-3248.50.2197
xnu-3248.60.1097
xnu-3789.21.497
xnu-3789.31.297
xnu-3789.41.397
xnu-3789.51.297
xnu-3789.60.2497
xnu-3789.70.1697
xnu-4570.20.6297
xnu-4570.31.397
xnu-4570.41.297
xnu-4570.51.197
xnu-4570.61.197
xnu-4570.71.297
xnu-4903.231.497
xnu-4903.241.197
xnu-4903.270.4797
xnu-6153.101.697
xnu-6153.121.197
xnu-6153.141.197
xnu-6153.41.397
xnu-6153.61.197
xnu-6153.81.597
xnu-7195.101.197
xnu-7195.121.397
xnu-7195.141.297
xnu-7195.60.7597
xnu-7195.81.397
xnu-8019.61.597
xnu-8019.80.2497
xnu-8020.101.497
xnu-8020.121.397
xnu-8020.140.4197
xnu-8792.61.297
xnu-8792.81.297
xnu-8796.101.597
xnu-8796.121.297
xnu-8796.141.397

Examples

Create a TCP socket

#include <sys/socket.h>
#include <netinet/in.h>

int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0) perror("socket");

Notes

macOS implements BSD sockets in xnu's bsd/kern/uipc_socket.c on top of Mach-backed file descriptors. The kernel routes the (domain, type, protocol) triple through pr_usrreqs of the matching protosw, then attaches the resulting socket to a fileproc so it can participate in select/kqueue/poll. Since Big Sur, user-space TCP/UDP for many flows is brokered through the Network Extension framework and the Skywalk data path, but the legacy socket() entry point remains the universal allocator. Sandboxed apps may receive ENOTSUP for AF_INET/AF_INET6 when the network entitlements are missing.

Detection

Endpoint Security has no dedicated event for raw socket() creation. To observe socket lifecycle, attach a NetworkExtension content filter (NEFilterDataProvider) or use the proc_info PROC_PIDFDSOCKETINFO walker against /dev/fdesc. DTrace's syscall::socket:entry probe is reliable on Intel and on Apple Silicon when SIP is disabled.

Related APIs

socketpairbindconnectconnectxsetsockoptshutdownnecp_open