To receive UDP packets, a socket has to be allocated using udp_socket_alloc() and bound to a local port with udp_bind_local(). For each packet received, the socket callback function gets called.
For being able to send UDP packets, an additional call to udp_bind_remote() is needed. Binding a socket to a remote host has two consequences:
Files | |
file | udp.c |
UDP implementation (license: GPLv2). | |
file | udp.h |
UDP header (license: GPLv2). | |
Modules | |
UDP configuration | |
Defines | |
#define | udp_socket_valid(socket) |
Checks if the given number is a valid socket identifier. | |
#define | udp_get_buffer() |
Retrieves a pointer to the UDP transmit buffer. | |
#define | udp_get_buffer_size() |
Retrieves the maximum payload size of the transmit buffer. | |
Functions | |
void | udp_init () |
Initializes the UDP layer. | |
bool | udp_handle_packet (const uint8_t *ip_remote, const struct udp_header *packet, uint16_t packet_len) |
Forwards incoming packets to the appropriate UDP socket. | |
int | udp_socket_alloc (udp_callback callback) |
Allocates a new UDP socket. | |
bool | udp_socket_free (int socket) |
Deallocates an UDP socket and makes it available for future allocations. | |
bool | udp_bind_local (int socket, uint16_t port_local) |
Bind an UDP socket to a local port. | |
bool | udp_bind_remote (int socket, const uint8_t *ip_remote, uint16_t port_remote) |
Bind an UDP socket to a remote port and optionally to a remote host. | |
bool | udp_unbind_remote (int socket) |
Remove the remote binding of an UDP socket. | |
bool | udp_send (int socket, uint16_t data_len) |
Send an UDP packet from the transmit buffer to a remote host. |
#define udp_socket_valid | ( | socket | ) |
Checks if the given number is a valid socket identifier.
[in] | socket | The socket identifier to check. |
#define udp_get_buffer | ( | ) |
Retrieves a pointer to the UDP transmit buffer.
For sending UDP packets, write your data to the buffer returned by this function and call udp_send_packet() afterwards.
#define udp_get_buffer_size | ( | ) |
Retrieves the maximum payload size of the transmit buffer.
You may not send more bytes per UDP packet than returned by this function.
void udp_init | ( | ) |
Initializes the UDP layer.
This function has to be called once on startup.
bool udp_handle_packet | ( | const uint8_t * | ip_remote, | |
const struct udp_header * | packet, | |||
uint16_t | packet_len | |||
) |
Forwards incoming packets to the appropriate UDP socket.
true
if a matching socket was found, false
if the packet was discarded. int udp_socket_alloc | ( | udp_callback | callback | ) |
Allocates a new UDP socket.
Each socket has an attached callback function which gets called for every packet which arrives on the socket.
A socket is identified by a non-negative integer. Use udp_socket_valid() to check if the returned socket is valid.
[in] | callback | Pointer to the callback function which gets attached to the new socket. |
-1
on failure. bool udp_socket_free | ( | int | socket | ) |
Deallocates an UDP socket and makes it available for future allocations.
After freeing the socket, it can no longer be used.
[in] | socket | The identifier of the socket which is to be deallocated. |
true
if the socket has been deallocated, false
on failure. bool udp_bind_local | ( | int | socket, | |
uint16_t | port_local | |||
) |
Bind an UDP socket to a local port.
Specifies which port the incoming packet must be addressed to in order to be received by the socket.
[in] | socket | The identifier of the socket which is to be locally bound. |
[in] | port_local | The port which should be bound to the socket. |
true
if the binding could be established, false
otherwise. bool udp_bind_remote | ( | int | socket, | |
const uint8_t * | ip_remote, | |||
uint16_t | port_remote | |||
) |
Bind an UDP socket to a remote port and optionally to a remote host.
Binding an UDP socket to a remote host and/or port has two consequences:
A remote ip of NULL means "broadcast"/"accept all".
[in] | socket | The identifier of the socket which is to be remotely bound. |
[in] | ip_remote | The remote IP address of the host to which to bind the socket, or NULL. |
[in] | port_remote | The remote port to which to bind the socket. |
true
if the binding could be established, false
otherwise. bool udp_unbind_remote | ( | int | socket | ) |
Remove the remote binding of an UDP socket.
Note that after the socket has been remotely unbound, it receives packets from any remote host which are sent to the socket's local port.
[in] | socket | The identifier of the socket which is to be remotely unbound. |
true
if the binding could be removed, false
otherwise. bool udp_send | ( | int | socket, | |
uint16_t | data_len | |||
) |
Send an UDP packet from the transmit buffer to a remote host.
Note that before using this function, the socket must have been locally and remotely bound via udp_socket_bind_local() and udp_socket_bind_remote().
[in] | socket | The identifier of the socket via which to send the packet. |
[in] | data_len | The payload length of the packet. |
true
if the packet was successfully sent, false
on failure.