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"]
"8" [label="stdbool.h" tooltip="stdbool.h"]
"34" [label="interop.h" tooltip="interop.h"]
"32" [label="erl_nif_priv.h" tooltip="erl_nif_priv.h"]
"25" [label="refc_binary.h" tooltip="refc_binary.h"]
"35" [label="nifs.h" tooltip="nifs.h"]
"29" [label="defaultatoms.h" tooltip="defaultatoms.h"]
"5" [label="atom.h" tooltip="atom.h"]
"11" [label="assert.h" tooltip="assert.h"]
"31" [label="dictionary.h" tooltip="dictionary.h"]
"23" [label="utils.h" tooltip="utils.h"]
"16" [label="synclist.h" tooltip="synclist.h"]
"15" [label="list.h" tooltip="list.h"]
"4" [label="stdint.h" tooltip="stdint.h"]
"6" [label="stdlib.h" tooltip="stdlib.h"]
"2" [label="context.h" tooltip="context.h"]
"22" [label="memory.h" tooltip="memory.h"]
"42" [label="sys.h" tooltip="sys.h"]
"47" [label="errno.h" tooltip="errno.h"]
"43" [label="module.h" tooltip="module.h"]
"45" [label="valueshashtable.h" tooltip="valueshashtable.h"]
"49" [label="lwip/udp.h" tooltip="lwip/udp.h"]
"48" [label="lwip/tcp.h" tooltip="lwip/tcp.h"]
"26" [label="resources.h" tooltip="resources.h"]
"33" [label="inet.h" tooltip="inet.h"]
"19" [label="term.h" tooltip="term.h"]
"24" [label="stddef.h" tooltip="stddef.h"]
"9" [label="erl_nif.h" tooltip="erl_nif.h"]
"36" [label="exportedfunction.h" tooltip="exportedfunction.h"]
"30" [label="defaultatoms.def" tooltip="defaultatoms.def"]
"12" [label="limits.h" tooltip="limits.h"]
"7" [label="atom_table.h" tooltip="atom_table.h"]
"38" [label="otp_socket_platform.h" tooltip="otp_socket_platform.h"]
"20" [label="sys/types.h" tooltip="sys/types.h"]
"21" [label="string.h" tooltip="string.h"]
"10" [label="term_typedef.h" tooltip="term_typedef.h"]
"3" [label="globalcontext.h" tooltip="globalcontext.h"]
"41" [label="scheduler.h" tooltip="scheduler.h"]
"18" [label="smp.h" tooltip="smp.h"]
"44" [label="atomshashtable.h" tooltip="atomshashtable.h"]
"46" [label="time.h" tooltip="time.h"]
"1" [label="/__w/AtomVM/AtomVM/src/libAtomVM/otp_socket.c" tooltip="/__w/AtomVM/AtomVM/src/libAtomVM/otp_socket.c" fillcolor="#BFBFBF"]
"37" [label="otp_socket.h" tooltip="otp_socket.h"]
"28" [label="timer_list.h" tooltip="timer_list.h"]
"40" [label="posix_nifs.h" tooltip="posix_nifs.h"]
"27" [label="mailbox.h" tooltip="mailbox.h"]
"39" [label="port.h" tooltip="port.h"]
"50" [label="trace.h" tooltip="trace.h"]
"17" [label="stdio.h" tooltip="stdio.h"]
"14" [label="ets.h" tooltip="ets.h"]
"13" [label="inttypes.h" tooltip="inttypes.h"]
"34" -> "2" [dir=forward tooltip="include"]
"34" -> "19" [dir=forward tooltip="include"]
"32" -> "2" [dir=forward tooltip="include"]
"32" -> "22" [dir=forward tooltip="include"]
"32" -> "26" [dir=forward tooltip="include"]
"25" -> "8" [dir=forward tooltip="include"]
"25" -> "6" [dir=forward tooltip="include"]
"25" -> "15" [dir=forward tooltip="include"]
"25" -> "26" [dir=forward tooltip="include"]
"35" -> "5" [dir=forward tooltip="include"]
"35" -> "2" [dir=forward tooltip="include"]
"35" -> "36" [dir=forward tooltip="include"]
"29" -> "3" [dir=forward tooltip="include"]
"29" -> "30" [dir=forward tooltip="include"]
"5" -> "4" [dir=forward tooltip="include"]
"5" -> "6" [dir=forward tooltip="include"]
"31" -> "15" [dir=forward tooltip="include"]
"31" -> "19" [dir=forward tooltip="include"]
"23" -> "24" [dir=forward tooltip="include"]
"23" -> "17" [dir=forward tooltip="include"]
"23" -> "6" [dir=forward tooltip="include"]
"16" -> "17" [dir=forward tooltip="include"]
"16" -> "15" [dir=forward tooltip="include"]
"16" -> "18" [dir=forward tooltip="include"]
"15" -> "8" [dir=forward tooltip="include"]
"2" -> "3" [dir=forward tooltip="include"]
"2" -> "15" [dir=forward tooltip="include"]
"2" -> "27" [dir=forward tooltip="include"]
"2" -> "18" [dir=forward tooltip="include"]
"2" -> "19" [dir=forward tooltip="include"]
"2" -> "28" [dir=forward tooltip="include"]
"22" -> "4" [dir=forward tooltip="include"]
"22" -> "6" [dir=forward tooltip="include"]
"22" -> "9" [dir=forward tooltip="include"]
"22" -> "10" [dir=forward tooltip="include"]
"22" -> "23" [dir=forward tooltip="include"]
"42" -> "3" [dir=forward tooltip="include"]
"42" -> "43" [dir=forward tooltip="include"]
"42" -> "4" [dir=forward tooltip="include"]
"42" -> "46" [dir=forward tooltip="include"]
"43" -> "8" [dir=forward tooltip="include"]
"43" -> "4" [dir=forward tooltip="include"]
"43" -> "5" [dir=forward tooltip="include"]
"43" -> "7" [dir=forward tooltip="include"]
"43" -> "44" [dir=forward tooltip="include"]
"43" -> "2" [dir=forward tooltip="include"]
"43" -> "36" [dir=forward tooltip="include"]
"43" -> "3" [dir=forward tooltip="include"]
"43" -> "19" [dir=forward tooltip="include"]
"43" -> "45" [dir=forward tooltip="include"]
"26" -> "6" [dir=forward tooltip="include"]
"26" -> "9" [dir=forward tooltip="include"]
"26" -> "15" [dir=forward tooltip="include"]
"26" -> "22" [dir=forward tooltip="include"]
"33" -> "34" [dir=forward tooltip="include"]
"33" -> "22" [dir=forward tooltip="include"]
"19" -> "20" [dir=forward tooltip="include"]
"19" -> "8" [dir=forward tooltip="include"]
"19" -> "4" [dir=forward tooltip="include"]
"19" -> "17" [dir=forward tooltip="include"]
"19" -> "6" [dir=forward tooltip="include"]
"19" -> "21" [dir=forward tooltip="include"]
"19" -> "22" [dir=forward tooltip="include"]
"19" -> "25" [dir=forward tooltip="include"]
"19" -> "23" [dir=forward tooltip="include"]
"19" -> "10" [dir=forward tooltip="include"]
"9" -> "10" [dir=forward tooltip="include"]
"36" -> "19" [dir=forward tooltip="include"]
"7" -> "8" [dir=forward tooltip="include"]
"7" -> "5" [dir=forward tooltip="include"]
"10" -> "11" [dir=forward tooltip="include"]
"10" -> "12" [dir=forward tooltip="include"]
"10" -> "13" [dir=forward tooltip="include"]
"10" -> "4" [dir=forward tooltip="include"]
"3" -> "4" [dir=forward tooltip="include"]
"3" -> "5" [dir=forward tooltip="include"]
"3" -> "7" [dir=forward tooltip="include"]
"3" -> "9" [dir=forward tooltip="include"]
"3" -> "14" [dir=forward tooltip="include"]
"3" -> "15" [dir=forward tooltip="include"]
"3" -> "27" [dir=forward tooltip="include"]
"3" -> "18" [dir=forward tooltip="include"]
"3" -> "16" [dir=forward tooltip="include"]
"3" -> "19" [dir=forward tooltip="include"]
"3" -> "28" [dir=forward tooltip="include"]
"41" -> "2" [dir=forward tooltip="include"]
"41" -> "3" [dir=forward tooltip="include"]
"18" -> "8" [dir=forward tooltip="include"]
"44" -> "5" [dir=forward tooltip="include"]
"1" -> "2" [dir=forward tooltip="include"]
"1" -> "29" [dir=forward tooltip="include"]
"1" -> "31" [dir=forward tooltip="include"]
"1" -> "32" [dir=forward tooltip="include"]
"1" -> "3" [dir=forward tooltip="include"]
"1" -> "33" [dir=forward tooltip="include"]
"1" -> "34" [dir=forward tooltip="include"]
"1" -> "15" [dir=forward tooltip="include"]
"1" -> "27" [dir=forward tooltip="include"]
"1" -> "22" [dir=forward tooltip="include"]
"1" -> "35" [dir=forward tooltip="include"]
"1" -> "37" [dir=forward tooltip="include"]
"1" -> "39" [dir=forward tooltip="include"]
"1" -> "40" [dir=forward tooltip="include"]
"1" -> "25" [dir=forward tooltip="include"]
"1" -> "41" [dir=forward tooltip="include"]
"1" -> "18" [dir=forward tooltip="include"]
"1" -> "42" [dir=forward tooltip="include"]
"1" -> "19" [dir=forward tooltip="include"]
"1" -> "23" [dir=forward tooltip="include"]
"1" -> "47" [dir=forward tooltip="include"]
"1" -> "48" [dir=forward tooltip="include"]
"1" -> "49" [dir=forward tooltip="include"]
"1" -> "50" [dir=forward tooltip="include"]
"37" -> "3" [dir=forward tooltip="include"]
"37" -> "35" [dir=forward tooltip="include"]
"37" -> "38" [dir=forward tooltip="include"]
"28" -> "8" [dir=forward tooltip="include"]
"28" -> "4" [dir=forward tooltip="include"]
"28" -> "15" [dir=forward tooltip="include"]
"40" -> "36" [dir=forward tooltip="include"]
"40" -> "3" [dir=forward tooltip="include"]
"40" -> "19" [dir=forward tooltip="include"]
"27" -> "8" [dir=forward tooltip="include"]
"27" -> "15" [dir=forward tooltip="include"]
"27" -> "10" [dir=forward tooltip="include"]
"27" -> "23" [dir=forward tooltip="include"]
"39" -> "2" [dir=forward tooltip="include"]
"39" -> "29" [dir=forward tooltip="include"]
"39" -> "3" [dir=forward tooltip="include"]
"39" -> "22" [dir=forward tooltip="include"]
"39" -> "19" [dir=forward tooltip="include"]
"14" -> "15" [dir=forward tooltip="include"]
"14" -> "16" [dir=forward tooltip="include"]
"14" -> "19" [dir=forward tooltip="include"]
}](../../../_images/graphviz-c629f613b190463b67725d8fa61803c7e996c6be.png)
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
-
enum SocketState
Values:
-
enumerator SocketStateClosed = 0
-
enumerator SocketStateUDP = 1 << 1
-
enumerator SocketStateTCP = 1 << 2
-
enumerator SocketStateSelectingRead = 1 << 3
-
enumerator SocketStateConnected = 1 << 4
-
enumerator SocketStateListening = 1 << 5
-
enumerator SocketStateUDPIdle = SocketStateUDP
-
enumerator SocketStateUDPSelectingRead = SocketStateUDP | SocketStateSelectingRead
-
enumerator SocketStateTCPNew = SocketStateTCP
-
enumerator SocketStateTCPConnected = SocketStateTCP | SocketStateConnected
-
enumerator SocketStateTCPSelectingRead = SocketStateTCPConnected | SocketStateSelectingRead
-
enumerator SocketStateTCPListening = SocketStateTCP | SocketStateListening
-
enumerator SocketStateTCPSelectingAccept = SocketStateTCPListening | SocketStateSelectingRead
-
enumerator SocketStateClosed = 0
Functions
-
static err_t tcp_recv_cb(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
-
static void udp_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
-
static void socket_dtor(ErlNifEnv *caller_env, void *obj)
-
static void socket_down(ErlNifEnv *caller_env, void *obj, ErlNifPid *pid, ErlNifMonitor *mon)
-
static void select_event_send_notification_from_nif(struct SocketResource *rsrc_obj, Context *locked_ctx)
-
static void select_event_send_notification_from_handler(struct SocketResource *rsrc_obj, int32_t process_id)
-
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.
- 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
-
static int send_closed_notification(Context *ctx, term socket_term, int32_t selecting_process_id, struct SocketResource *rsrc_obj)
-
static void finalize_close_hander(struct LWIPEvent *event)
-
static struct SocketResource *make_accepted_socket_resource(struct tcp_pcb *newpcb)
-
static void tcp_accept_handler(struct LWIPEvent *event)
-
static err_t tcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err)
-
static void tcp_recv_handler(struct LWIPEvent *event)
-
static void udp_recv_handler(struct LWIPEvent *event)
-
static term make_accepted_socket_term(Context *ctx, struct SocketResource *conn_rsrc_obj)
-
static size_t copy_pbuf_data(struct pbuf *src, size_t offset, size_t count, uint8_t *dst)
-
static ssize_t do_socket_recv(struct SocketResource *rsrc_obj, uint8_t *buf, size_t len, int flags, term *from, Heap *heap)
-
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
-
static term nif_socket_recv_lwip(Context *ctx, term resource_term, struct SocketResource *rsrc_obj, size_t len, bool is_recvfrom)
-
static ssize_t do_socket_send(struct SocketResource *rsrc_obj, const uint8_t *buf, size_t len, term dest)
-
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
-
static void trap_answer_ok(struct LWIPEvent *event)
-
static void trap_answer_closed(struct LWIPEvent *event)
-
static err_t tcp_connected_cb(void *arg, struct tcp_pcb *tpcb, err_t err)
-
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 char *const type_atom = ATOM_STR("\x4", "type")
- 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,}
-
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_getopt_nif = {.base.type = NIFFunctionType, .nif_ptr = nif_socket_getopt}
-
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}
-
struct TCPAcceptedItem
Collaboration diagram for TCPAcceptedItem:
-
struct TCPReceivedItem
Collaboration diagram for TCPReceivedItem:
-
struct UDPReceivedItem
Collaboration diagram for UDPReceivedItem:
-
struct SocketResource
Collaboration diagram for SocketResource: