otp_socket.c

Include dependency graph for otp_socket.c:

digraph {
    graph [bgcolor="#00000000"]
    node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2]
    edge [color="#1414CE"]
    "13" [label="stdbool.h" tooltip="stdbool.h"]
    "27" [label="dictionary.h" tooltip="dictionary.h"]
    "14" [label="stdatomic.h" tooltip="stdatomic.h"]
    "5" [label="atom.h" tooltip="atom.h"]
    "36" [label="posix_nifs.h" tooltip="posix_nifs.h"]
    "44" [label="trace.h" tooltip="trace.h"]
    "15" [label="synclist.h" tooltip="synclist.h"]
    "26" [label="defaultatoms.h" tooltip="defaultatoms.h"]
    "4" [label="stdint.h" tooltip="stdint.h"]
    "6" [label="stdlib.h" tooltip="stdlib.h"]
    "29" [label="inet.h" tooltip="inet.h"]
    "41" [label="valueshashtable.h" tooltip="valueshashtable.h"]
    "37" [label="scheduler.h" tooltip="scheduler.h"]
    "32" [label="exportedfunction.h" tooltip="exportedfunction.h"]
    "2" [label="context.h" tooltip="context.h"]
    "43" [label="errno.h" tooltip="errno.h"]
    "35" [label="port.h" tooltip="port.h"]
    "20" [label="utils.h" tooltip="utils.h"]
    "8" [label="term_typedef.h" tooltip="term_typedef.h"]
    "21" [label="stddef.h" tooltip="stddef.h"]
    "9" [label="limits.h" tooltip="limits.h"]
    "34" [label="otp_socket_platform.h" tooltip="otp_socket_platform.h"]
    "22" [label="refc_binary.h" tooltip="refc_binary.h"]
    "18" [label="string.h" tooltip="string.h"]
    "25" [label="mailbox.h" tooltip="mailbox.h"]
    "39" [label="module.h" tooltip="module.h"]
    "17" [label="term.h" tooltip="term.h"]
    "7" [label="erl_nif.h" tooltip="erl_nif.h"]
    "30" [label="interop.h" tooltip="interop.h"]
    "42" [label="time.h" tooltip="time.h"]
    "23" [label="resources.h" tooltip="resources.h"]
    "12" [label="smp.h" tooltip="smp.h"]
    "28" [label="erl_nif_priv.h" tooltip="erl_nif_priv.h"]
    "38" [label="sys.h" tooltip="sys.h"]
    "40" [label="atomshashtable.h" tooltip="atomshashtable.h"]
    "31" [label="nifs.h" tooltip="nifs.h"]
    "3" [label="globalcontext.h" tooltip="globalcontext.h"]
    "1" [label="/home/runner/work/AtomVM/AtomVM/src/libAtomVM/otp_socket.c" tooltip="/home/runner/work/AtomVM/AtomVM/src/libAtomVM/otp_socket.c" fillcolor="#BFBFBF"]
    "19" [label="memory.h" tooltip="memory.h"]
    "33" [label="otp_socket.h" tooltip="otp_socket.h"]
    "16" [label="stdio.h" tooltip="stdio.h"]
    "24" [label="timer_list.h" tooltip="timer_list.h"]
    "11" [label="list.h" tooltip="list.h"]
    "10" [label="inttypes.h" tooltip="inttypes.h"]
    "27" -> "11" [dir=forward tooltip="include"]
    "27" -> "17" [dir=forward tooltip="include"]
    "5" -> "4" [dir=forward tooltip="include"]
    "5" -> "6" [dir=forward tooltip="include"]
    "36" -> "32" [dir=forward tooltip="include"]
    "36" -> "3" [dir=forward tooltip="include"]
    "36" -> "17" [dir=forward tooltip="include"]
    "15" -> "11" [dir=forward tooltip="include"]
    "15" -> "16" [dir=forward tooltip="include"]
    "15" -> "12" [dir=forward tooltip="include"]
    "26" -> "3" [dir=forward tooltip="include"]
    "29" -> "30" [dir=forward tooltip="include"]
    "29" -> "19" [dir=forward tooltip="include"]
    "37" -> "2" [dir=forward tooltip="include"]
    "37" -> "3" [dir=forward tooltip="include"]
    "32" -> "17" [dir=forward tooltip="include"]
    "2" -> "3" [dir=forward tooltip="include"]
    "2" -> "11" [dir=forward tooltip="include"]
    "2" -> "25" [dir=forward tooltip="include"]
    "2" -> "12" [dir=forward tooltip="include"]
    "2" -> "17" [dir=forward tooltip="include"]
    "2" -> "24" [dir=forward tooltip="include"]
    "35" -> "2" [dir=forward tooltip="include"]
    "35" -> "26" [dir=forward tooltip="include"]
    "35" -> "3" [dir=forward tooltip="include"]
    "35" -> "19" [dir=forward tooltip="include"]
    "35" -> "17" [dir=forward tooltip="include"]
    "20" -> "21" [dir=forward tooltip="include"]
    "8" -> "9" [dir=forward tooltip="include"]
    "8" -> "10" [dir=forward tooltip="include"]
    "8" -> "4" [dir=forward tooltip="include"]
    "22" -> "13" [dir=forward tooltip="include"]
    "22" -> "6" [dir=forward tooltip="include"]
    "22" -> "11" [dir=forward tooltip="include"]
    "22" -> "23" [dir=forward tooltip="include"]
    "22" -> "12" [dir=forward tooltip="include"]
    "25" -> "13" [dir=forward tooltip="include"]
    "25" -> "11" [dir=forward tooltip="include"]
    "25" -> "12" [dir=forward tooltip="include"]
    "25" -> "8" [dir=forward tooltip="include"]
    "25" -> "20" [dir=forward tooltip="include"]
    "39" -> "13" [dir=forward tooltip="include"]
    "39" -> "4" [dir=forward tooltip="include"]
    "39" -> "5" [dir=forward tooltip="include"]
    "39" -> "40" [dir=forward tooltip="include"]
    "39" -> "2" [dir=forward tooltip="include"]
    "39" -> "32" [dir=forward tooltip="include"]
    "39" -> "3" [dir=forward tooltip="include"]
    "39" -> "17" [dir=forward tooltip="include"]
    "39" -> "41" [dir=forward tooltip="include"]
    "17" -> "13" [dir=forward tooltip="include"]
    "17" -> "4" [dir=forward tooltip="include"]
    "17" -> "16" [dir=forward tooltip="include"]
    "17" -> "6" [dir=forward tooltip="include"]
    "17" -> "18" [dir=forward tooltip="include"]
    "17" -> "19" [dir=forward tooltip="include"]
    "17" -> "22" [dir=forward tooltip="include"]
    "17" -> "20" [dir=forward tooltip="include"]
    "17" -> "8" [dir=forward tooltip="include"]
    "7" -> "8" [dir=forward tooltip="include"]
    "30" -> "2" [dir=forward tooltip="include"]
    "30" -> "17" [dir=forward tooltip="include"]
    "23" -> "6" [dir=forward tooltip="include"]
    "23" -> "7" [dir=forward tooltip="include"]
    "23" -> "11" [dir=forward tooltip="include"]
    "23" -> "19" [dir=forward tooltip="include"]
    "12" -> "13" [dir=forward tooltip="include"]
    "12" -> "14" [dir=forward tooltip="include"]
    "28" -> "2" [dir=forward tooltip="include"]
    "28" -> "19" [dir=forward tooltip="include"]
    "38" -> "3" [dir=forward tooltip="include"]
    "38" -> "39" [dir=forward tooltip="include"]
    "38" -> "4" [dir=forward tooltip="include"]
    "38" -> "42" [dir=forward tooltip="include"]
    "40" -> "5" [dir=forward tooltip="include"]
    "31" -> "5" [dir=forward tooltip="include"]
    "31" -> "2" [dir=forward tooltip="include"]
    "31" -> "32" [dir=forward tooltip="include"]
    "3" -> "4" [dir=forward tooltip="include"]
    "3" -> "5" [dir=forward tooltip="include"]
    "3" -> "7" [dir=forward tooltip="include"]
    "3" -> "11" [dir=forward tooltip="include"]
    "3" -> "12" [dir=forward tooltip="include"]
    "3" -> "15" [dir=forward tooltip="include"]
    "3" -> "17" [dir=forward tooltip="include"]
    "3" -> "24" [dir=forward tooltip="include"]
    "1" -> "2" [dir=forward tooltip="include"]
    "1" -> "26" [dir=forward tooltip="include"]
    "1" -> "27" [dir=forward tooltip="include"]
    "1" -> "28" [dir=forward tooltip="include"]
    "1" -> "3" [dir=forward tooltip="include"]
    "1" -> "29" [dir=forward tooltip="include"]
    "1" -> "30" [dir=forward tooltip="include"]
    "1" -> "11" [dir=forward tooltip="include"]
    "1" -> "25" [dir=forward tooltip="include"]
    "1" -> "19" [dir=forward tooltip="include"]
    "1" -> "31" [dir=forward tooltip="include"]
    "1" -> "33" [dir=forward tooltip="include"]
    "1" -> "35" [dir=forward tooltip="include"]
    "1" -> "36" [dir=forward tooltip="include"]
    "1" -> "37" [dir=forward tooltip="include"]
    "1" -> "38" [dir=forward tooltip="include"]
    "1" -> "17" [dir=forward tooltip="include"]
    "1" -> "20" [dir=forward tooltip="include"]
    "1" -> "43" [dir=forward tooltip="include"]
    "1" -> "44" [dir=forward tooltip="include"]
    "19" -> "7" [dir=forward tooltip="include"]
    "19" -> "8" [dir=forward tooltip="include"]
    "19" -> "20" [dir=forward tooltip="include"]
    "19" -> "4" [dir=forward tooltip="include"]
    "19" -> "6" [dir=forward tooltip="include"]
    "33" -> "3" [dir=forward tooltip="include"]
    "33" -> "31" [dir=forward tooltip="include"]
    "33" -> "34" [dir=forward tooltip="include"]
    "24" -> "13" [dir=forward tooltip="include"]
    "24" -> "4" [dir=forward tooltip="include"]
    "24" -> "11" [dir=forward tooltip="include"]
}

Defines

CLOSED_FD
ADDR_ATOM
CLOSE_INTERNAL_ATOM
ACCEPT_ATOM
RECV_ATOM
DEFAULT_BUFFER_SIZE
MIN(A, B)

Enums

enum otp_socket_shutdown_direction

Values:

enumerator OtpSocketInvalidShutdownDirection
enumerator OtpSocketReadShutdownDirection
enumerator OtpSocketWriteShutdownDirection
enumerator OtpSocketReadWriteShutdownDirection
enum otp_socket_setopt_level

Values:

enumerator OtpSocketInvalidSetoptLevel
enumerator OtpSocketSetoptLevelSocket
enumerator OtpSocketSetoptLevelOTP

Functions

static void socket_dtor(ErlNifEnv *caller_env, void *obj)
static void socket_down(ErlNifEnv *caller_env, void *obj, ErlNifPid *pid, ErlNifMonitor *mon)
void otp_socket_init(GlobalContext *global)
static inline int get_domain(GlobalContext *global, term domain_term, bool *ok)
static inline int get_type(GlobalContext *global, term type_term, bool *ok)
static inline int get_protocol(GlobalContext *global, term protocol_term, bool *ok)
static inline term make_error_tuple(term reason, Context *ctx)
static term nif_socket_open(Context *ctx, int argc, term argv[])
bool term_to_otp_socket(term socket_term, struct SocketResource **rsrc_obj, Context *ctx)
bool term_is_otp_socket(term socket_term)
static int send_closed_notification(Context *ctx, struct SocketResource *rsrc_obj)
static term nif_socket_close(Context *ctx, int argc, term argv[])
static term nif_socket_select_read(Context *ctx, int argc, term argv[])
static term nif_socket_select_stop(Context *ctx, int argc, term argv[])
static term nif_socket_setopt(Context *ctx, int argc, term argv[])
switch (level_val)
static term nif_socket_sockname(Context *ctx, int argc, term argv[])
if (UNLIKELY(memory_ensure_free(ctx, TUPLE_SIZE(2)+term_map_size_in_terms(2)+TUPLE_SIZE(4)) !=MEMORY_GC_OK))
term_set_map_assoc (map, 0, ADDR_ATOM, address)
term_set_map_assoc (map, 1, PORT_ATOM, port_number)
static term nif_socket_peername(Context *ctx, int argc, term argv[])
static term nif_socket_bind(Context *ctx, int argc, term argv[])
static term nif_socket_listen(Context *ctx, int argc, term argv[])
static term nif_socket_accept(Context *ctx, int argc, term argv[])
ssize_t socket_recv(struct SocketResource *rsrc_obj, uint8_t *buf, size_t len, int flags, term *from, Heap *heap)
static term nif_socket_recv_internal(Context *ctx, term argv[], bool is_recvfrom)
static term nif_socket_recv(Context *ctx, int argc, term argv[])
static term nif_socket_recvfrom(Context *ctx, int argc, term argv[])
ssize_t socket_send(struct SocketResource *rsrc_obj, const uint8_t *buf, size_t len, term dest)
static term nif_socket_send_internal(Context *ctx, int argc, term argv[], bool is_sendto)
static term nif_socket_send(Context *ctx, int argc, term argv[])
static term nif_socket_sendto(Context *ctx, int argc, term argv[])
static term nif_socket_connect(Context *ctx, int argc, term argv[])
static term nif_socket_shutdown(Context *ctx, int argc, term argv[])
const struct Nif *otp_socket_nif_get_nif(const char *nifname)

Variables

static const char *const addr_atom = ATOM_STR("\x4", "addr")
static const char *const any_atom = ATOM_STR("\x3", "any")
static const char *const invalid_option_atom = ATOM_STR("\xE", "invalid_option")
static const char *const invalid_value_atom = ATOM_STR("\xD", "invalid_value")
static const char *const linger_atom = ATOM_STR("\x6", "linger")
static const char *const loopback_atom = ATOM_STR("\x8", "loopback")
static const char *const onoff_atom = ATOM_STR("\x5", "onoff")
static const char *const port_atom = ATOM_STR("\x4", "port")
static const char *const rcvbuf_atom = ATOM_STR("\x6", "rcvbuf")
static const char *const reuseaddr_atom = ATOM_STR("\x9", "reuseaddr")
static const AtomStringIntPair otp_socket_shutdown_direction_table[] = {{ATOM_STR("\x4", "read"), OtpSocketReadShutdownDirection}, {ATOM_STR("\x5", "write"), OtpSocketWriteShutdownDirection}, {ATOM_STR("\xA", "read_write"), OtpSocketReadWriteShutdownDirection},}
static const AtomStringIntPair otp_socket_setopt_level_table[] = {{ATOM_STR("\x6", "socket"), OtpSocketSetoptLevelSocket}, {ATOM_STR("\x3", "otp"), OtpSocketSetoptLevelOTP},}
static ErlNifResourceType *socket_resource_type
static const ErlNifResourceTypeInit SocketResourceTypeInit  = {.members = 3,.dtor = socket_dtor,.stop = NULL,.down = socket_down,}
term level_tuple = argv[1]
term value = argv[2]
term level = term_get_tuple_element(level_tuple, 0)
int level_val = interop_atom_term_select_int(otp_socket_setopt_level_table, level, global)
else   = {         term address = inet_make_addr4(ip4_u32, &ctx->heap)
term port_number = term_from_int(port_u16)
term map = term_alloc_map(2, &ctx->heap)
term return_value = port_create_tuple2(ctx, OK_ATOM, map)
term result = OK_ATOM
static const struct Nif socket_open_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_open}
static const struct Nif socket_close_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_close}
static const struct Nif socket_select_stop_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_select_stop}
static const struct Nif socket_setopt_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_setopt}
static const struct Nif socket_bind_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_bind}
static const struct Nif socket_listen_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_listen}
static const struct Nif socket_sockname_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_sockname}
static const struct Nif socket_peername_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_peername}
static const struct Nif socket_select_read_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_select_read}
static const struct Nif socket_accept_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_accept}
static const struct Nif socket_recv_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_recv}
static const struct Nif socket_recvfrom_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_recvfrom}
static const struct Nif socket_send_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_send}
static const struct Nif socket_sendto_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_sendto}
static const struct Nif socket_connect_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_connect}
static const struct Nif socket_shutdown_nif  = {.base.type =NIFFunctionType,.nif_ptr = nif_socket_shutdown}