Data streams over TCP or UDP, to IPv4 and/or IPv6 peers - net_open() connects out to a remote host, net_listener_init() accepts incoming connections or datagrams.
More...
|
| | NTP |
| | NTP (Network Time Protocol) is how a device gets the current wall-clock time from a server over the network, rather than relying on a battery-backed RTC it may not have.
|
| |
Data streams over TCP or UDP, to IPv4 and/or IPv6 peers - net_open() connects out to a remote host, net_listener_init() accepts incoming connections or datagrams.
Every socket, in either direction, is a plain sys_iostream_t (System): reading, writing, closing, and readiness notification all go through the same generic sys_iostream_read()/_write()/_close()/_set_callback() API every other stream in picofuse uses, not socket-specific calls.
Connecting out to a remote host:
if (conn != NULL) {
const char *req = "GET / HTTP/1.0\r\n\r\n";
char buf[512];
}
Accepting incoming connections - net_listener_init() returns immediately, and callback fires from then on for as long as the listener stays open (once per accepted connection for net_proto_tcp, once per received datagram for net_proto_udp - see net_listener_init()'s own doc):
void *userdata) {
char buf[512];
}
◆ net_accept_callback_t
Called for each accepted TCP connection or received UDP datagram on a listener.
- Parameters
-
| listener | The listener this arrived on. |
| conn | Stream to read/write. Caller-owned - close with sys_iostream_close() when done with it; net_listener_deinit() on listener does not affect streams already handed out this way. For net_proto_udp this is good for exactly one read and, if replying, one write - see net.h's own doc for why. |
| remote | Address of the remote peer. |
| remote_port | Port of the remote peer. |
| userdata | Opaque pointer, as passed to net_listener_init(). |
Definition at line 76 of file types.h.
◆ net_addr_family_t
Address family for a net_addr_t.
| Enumerator |
|---|
| net_addr_family_v4 | IPv4 - net_addr_t.addr.v4 is valid.
|
| net_addr_family_v6 | IPv6 - net_addr_t.addr.v6 is valid.
|
Definition at line 39 of file types.h.
IPv6 - net_addr_t.addr.v6 is valid.
net_addr_family_t
Address family for a net_addr_t.
IPv4 - net_addr_t.addr.v4 is valid.
◆ net_proto_t
Transport protocol for a socket.
| Enumerator |
|---|
| net_proto_tcp | Connection-oriented, reliable, ordered byte stream.
|
| net_proto_udp | Connectionless, unreliable datagrams.
|
Definition at line 30 of file types.h.
Connectionless, unreliable datagrams.
net_proto_t
Transport protocol for a socket.
Connection-oriented, reliable, ordered byte stream.
◆ net_addr_to_string()
| size_t net_addr_to_string |
( |
const net_addr_t * |
addr, |
|
|
char * |
buf, |
|
|
size_t |
buf_size |
|
) |
| |
Format an address as a human-readable string.
- Parameters
-
| addr | Address to format. |
| buf | Destination buffer. |
| buf_size | Size of buf in bytes. |
- Returns
- Number of characters that would have been written to
buf, not counting the null terminator, same truncation semantics as sys_sprintf().
◆ net_addr_v6()
Build an IPv6 address from sixteen bytes.
- Parameters
-
| bytes | Address bytes, network byte order. Must point to at least 16 bytes. |
◆ net_listener_deinit()
Stop listening and release a listener.
- Parameters
-
| listener | The listener to close, or NULL (a no-op). Streams already handed to callback are unaffected - see its own doc. |
◆ net_listener_init()
Start listening for incoming connections or datagrams.
- Parameters
-
| proto | Transport protocol. |
| addr | Local address to bind to (see net_addr_v4_any()/ net_addr_v6_any() to bind to all interfaces). |
| port | Local port to bind to. |
| callback | Called for each accepted connection or received datagram (see net_proto_t), for as long as the listener stays open. |
| userdata | Opaque pointer passed through to callback. |
- Returns
- A listener handle, or NULL on failure (for example, the port is already in use, or NET_LISTENER_CAPACITY listeners are already open).
◆ net_open()
Open a connection to a remote host.
- Parameters
-
| proto | Transport protocol. |
| addr | Remote address to connect to. |
| port | Remote port to connect to. |
- Returns
- An open stream, or NULL on failure (connection refused, timed out, or no route). Blocks until connected or the attempt fails.
For net_proto_udp this "connects" the socket to a single remote address: every subsequent sys_iostream_write() sends to it, and sys_iostream_read() only ever returns datagrams from it. Use net_listener_init() instead for a UDP socket that receives from arbitrary, not-yet-known peers.