otp_socket.c
Include dependency graph for otp_socket.c:
Defines
-
TAG "otp_socket"
-
CLOSED_FD 0
-
ADDR_ATOM globalcontext_make_atom(global, addr_atom)
-
CLOSE_INTERNAL_ATOM globalcontext_make_atom(global, close_internal_atom)
-
ACCEPT_ATOM globalcontext_make_atom(global, accept_atom)
-
RECV_ATOM globalcontext_make_atom(global, recv_atom)
-
DEFAULT_BUFFER_SIZE 512
-
MIN(A, B) (((A) < (B)) ? (A) : (B))
Enums
Functions
-
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)
Allocate memory on ctx and make and return an error tuple from immediate term reason.
This function is meant to be called from a nif that should return its result directly, to allow for further processing of a possible out of memory exception. @end
- Parameters:
reason – the reason, should be an immediate (atom or integer)
ctx – the current context
- Returns:
a term
-
bool term_to_otp_socket(term socket_term, struct SocketResource **rsrc_obj, Context *ctx)
Get the resource object associated with a socket term.
- Parameters:
socket_term – the term with the socket
otp_socket – on output, the socket resource
ctx – the current context
- Returns:
true in case of success
-
bool term_is_otp_socket(term socket_term)
Determine if a term is a socket term.
- Parameters:
socket_term – the term to test
- Returns:
true if it is a term
- switch (level_val)
- 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)
-
ssize_t socket_recv(struct SocketResource *rsrc_obj, uint8_t *buf, size_t len, int flags, term *from, Heap *heap)
Read data from a socket.
- Parameters:
socket – the socket resource
buf – buffer to store data
len – number of bytes
flags – flags passed to recvfrom
from – filled with origin address using recvfrom (can be NULL)
heap – heap to build the origin address term (can be NULL if from is NULL)
- Returns:
the number of read bytes or a value from SocketErrors
-
ssize_t socket_send(struct SocketResource *rsrc_obj, const uint8_t *buf, size_t len, term dest)
Send data to a socket (without blocking)
- Parameters:
socket – the socket resource
buf – buffer to send
len – number of bytes
dest – destination address or invalid term for sendto/send
- Returns:
the number of written bytes or a value from SocketErrors
Variables
- 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 = 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)
-
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}