#include "nip_init.h"
#include "mem.h"
Go to the source code of this file.
Data Structures | |
struct | nip_udp_sock_addr |
struct | nip_udp_buffer |
Item of UDP Packet Queue. More... | |
struct | nip_udp_sock |
struct | nip_udp_header |
Defines | |
#define | NIP_UDP_MAX_SOCKETS 255 |
Define maximum number of UDP sockets for dynamic socket array. | |
#define | NIP_UDP_FIXED_SOCKETS 4 |
Fixed number of UDP sockets to be used for fixed socket configuration. | |
#define | NIP_UDP_USE_FIXED_SOCKETS 1 |
Use fixed socket array (static allocation of memory for fixed numer of sockets) or use dynamic array of sockets with at most NIP_UDP_MAX_SOCKETS sockets. | |
#define | NIP_UDP_SOCKET_CHECK_INTVL 1 |
Any socket will not be checked for changes twice within the interval, unless its timer has been reset. | |
#define | NIP_UDP_PACKET_QUEUE_SIZE 4 |
Each entry in packet queue has cost of sizeof(struct nip_udp_buffer). | |
#define | NIP_UDP_NO_SOCKET 0xFFFF |
#define | NIP_UDP_EOQ NIP_UDP_PACKET_QUEUE_SIZE |
End of queue. | |
#define | NIP_UDP_SOCK_FLG_USED 0x01 |
socket in use? | |
#define | NIP_UDP_SOCK_FLG_NON_BLOCK 0x02 |
non-blocking socket? to be set by user | |
#define | NIP_UDP_SOCK_FLG_LISTENING 0x04 |
bind socket to port? to be set by user | |
Typedefs | |
typedef uint16_t | nip_udp_sock_id_t |
typedef uint16_t | nip_udp_sock_cnt_t |
typedef uint8_t | nip_udp_queue_id_t |
typedef uint8_t | nip_udp_queue_cnt_t |
Enumerations | |
enum | nip_udp_buf_type_t { NIP_UDP_RX_BUF, NIP_UDP_TX_BUF } |
Functions | |
nip_udp_sock_id_t | nip_udp_socket (struct nip_udp_sock_addr *addr, uint8_t flags) |
Register UDP socket with given address and flags. | |
void | nip_udp_close (nip_udp_sock_id_t sockid) |
Unregister previous socket registration. | |
void | nip_udp_disp_send (void) |
void | nip_udp_disp_receive (void) |
Dispatcher function to be called upon packet reception. | |
nip_error_t | nip_udp_init_send (nip_udp_sock_id_t sockid, nip_mem_size_t size) |
Initialize send buffer for new packet. | |
nip_error_t | nip_udp_write (nip_udp_sock_id_t sockid, uint8_t *buf, nip_mem_size_t size) |
Write bytes to socket. | |
nip_error_t | nip_udp_transmit (nip_udp_sock_id_t sockid, struct nip_udp_sock_addr *dest_addr, nip_net_if_id_t if_id) |
Transmit the packet from the current send buffer to the given destination. | |
nip_mem_size_t | nip_udp_recvfrom (nip_udp_sock_id_t sockid, uint8_t *buf, nip_mem_size_t size, struct nip_udp_sock_addr *from_addr) |
Attempt to receive new packet from UDP socket. | |
void | nip_udp_forward (nip_udp_sock_id_t sockid) |
Forwards current incoming buffer to outgoing queue. | |
void | nip_udp_cancel_send (nip_udp_sock_id_t sockid) |
Cancel transmission. | |
void | nip_udp_cancel_recv (nip_udp_sock_id_t sockid) |
Cancel current packet reception. | |
nip_mem_handle_t | nip_udp_buf_handle (nip_udp_sock_id_t sockid, nip_udp_buf_type_t buftype) |
Return handle to current Receive or Transmit buffer of UDP socket. | |
void * | nip_udp_data_ptr (nip_udp_sock_id_t sockid, nip_udp_buf_type_t buftype) |
Give direct access (pointer) to a socket buffer. | |
void | nip_udp_data_mem_ptr (nip_udp_sock_id_t sockid, nip_udp_buf_type_t buftype, nip_mem_ptr_t *res) |
Return mem-ptr to buffer data. | |
void | nip_udp_ptr_release (nip_udp_sock_id_t sockid, nip_udp_buf_type_t buftype) |
Release previously granted direct access. |
#define NIP_UDP_EOQ NIP_UDP_PACKET_QUEUE_SIZE |
End of queue.
To be used to invalidate queue pointer
Definition at line 80 of file udp.h.
Referenced by nip_udp_buf_handle(), nip_udp_cancel_recv(), nip_udp_close(), nip_udp_disp_receive(), nip_udp_forward(), nip_udp_recvfrom(), and nip_udp_socket().
#define NIP_UDP_FIXED_SOCKETS 4 |
Fixed number of UDP sockets to be used for fixed socket configuration.
Definition at line 44 of file udp.h.
Referenced by nip_udp_close(), nip_udp_disp_receive(), nip_udp_sock_ptr(), and nip_udp_socket().
#define NIP_UDP_MAX_SOCKETS 255 |
#define NIP_UDP_NO_SOCKET 0xFFFF |
Definition at line 72 of file udp.h.
Referenced by nip_dhcp_disp_check(), nip_mdns_disp_check(), nip_mdns_init(), and nip_udp_socket().
#define NIP_UDP_PACKET_QUEUE_SIZE 4 |
#define NIP_UDP_SOCK_FLG_LISTENING 0x04 |
bind socket to port? to be set by user
Definition at line 95 of file udp.h.
Referenced by nip_dhcp_disp_sock_init(), and nip_mdns_init().
#define NIP_UDP_SOCK_FLG_NON_BLOCK 0x02 |
non-blocking socket? to be set by user
Definition at line 94 of file udp.h.
Referenced by nip_dhcp_disp_sock_init(), nip_mdns_init(), and nip_udp_recvfrom().
#define NIP_UDP_SOCK_FLG_USED 0x01 |
socket in use?
Definition at line 93 of file udp.h.
Referenced by nip_udp_disp_receive(), nip_udp_sock_ptr(), and nip_udp_socket().
#define NIP_UDP_SOCKET_CHECK_INTVL 1 |
#define NIP_UDP_USE_FIXED_SOCKETS 1 |
typedef uint8_t nip_udp_queue_cnt_t |
typedef uint8_t nip_udp_queue_id_t |
typedef uint16_t nip_udp_sock_cnt_t |
typedef uint16_t nip_udp_sock_id_t |
enum nip_udp_buf_type_t |
nip_mem_handle_t nip_udp_buf_handle | ( | nip_udp_sock_id_t | sockid, | |
nip_udp_buf_type_t | buftype | |||
) |
Return handle to current Receive or Transmit buffer of UDP socket.
Definition at line 139 of file udp.c.
References nip_udp_buffer::buf, NIP_E_INVALID_BLOCK, NIP_E_INVALID_SOCK, nip_error, NIP_MEM_NULL, NIP_UDP_EOQ, NIP_UDP_RX_BUF, nip_udp_sock_ptr(), NULL, nip_udp_sock::rx_packet, nip_udp_sock::tx_packet, and udp_rx_queue.
Referenced by nip_udp_data_mem_ptr(), and nip_udp_ptr_release().
void nip_udp_cancel_recv | ( | nip_udp_sock_id_t | sockid | ) |
Cancel current packet reception.
Definition at line 462 of file udp.c.
References nip_udp_buffer::buf, nip_udp_buffer::next, nip_mem_free(), NIP_MEM_NULL, NIP_UDP_EOQ, nip_udp_sock_ptr(), nip_udp_sock::rx_packet, and udp_rx_queue.
Referenced by nip_dhcp_disp_check(), nip_mdns_disp_check(), nip_mdns_disp_check_queries(), and nip_udp_recvfrom().
void nip_udp_cancel_send | ( | nip_udp_sock_id_t | sockid | ) |
Cancel transmission.
The send buffer will be cleared and removed. For the next packet to be sent, nip_udp_init_send() will have to be called.
This function will usually be called by the application after nip_udp_write() failed before the packet was complete, which normally is a caused by insufficient memory. If the application does not want to wait in place for enough memory to be available and has no means to safe the send-status it will cancel the transmission and retry later.
Definition at line 286 of file udp.c.
References nip_mem_free(), NIP_MEM_NULL, nip_udp_sock_ptr(), and nip_udp_sock::tx_packet.
Referenced by nip_dhcp_disp_check(), nip_mdns_disp_check_queries(), and nip_mdns_tx_init().
void nip_udp_close | ( | nip_udp_sock_id_t | sockid | ) |
Unregister previous socket registration.
Definition at line 95 of file udp.c.
References nip_udp_buffer::buf, nip_mem_free(), NIP_MEM_NULL, NIP_UDP_EOQ, NIP_UDP_FIXED_SOCKETS, nip_udp_buffer::sockid, udp_rx_queue, and udp_sockets.
Referenced by nip_dhcp_disp_check(), and nip_mdns_interface_down().
void nip_udp_data_mem_ptr | ( | nip_udp_sock_id_t | sockid, | |
nip_udp_buf_type_t | buftype, | |||
nip_mem_ptr_t * | res | |||
) |
Return mem-ptr to buffer data.
The call failed, if res->id is NIP_MEM_NULL.
Definition at line 182 of file udp.c.
References nip_mem_ptr_t::id, NIP_MEM_NULL, nip_udp_buf_handle(), NULL, and nip_mem_ptr_t::ptr.
Referenced by nip_mdns_disp_check_cache(), nip_mdns_parse_packet(), and nip_udp_data_ptr().
void* nip_udp_data_ptr | ( | nip_udp_sock_id_t | sockid, | |
nip_udp_buf_type_t | buftype | |||
) |
Give direct access (pointer) to a socket buffer.
Normally the buffer space is administered by the UDP implementation and the application only has write access through the nip_udp_write() function. For optimization reasons it may be better for the application to have direct access, though.
The direct TX buffer access is intended to be used in combination with the nip_udp_write() function, which still has to be used to allocate enough memory. You may call nip_udp_write() without supplying a buffer pointer to allocate uninitialized memory.
If the requested buffer size could not be allocated NULL will returned and nip_error be set to NIP_E_OUT_OF_MEMORY.
Make sure you know how much memory has been allocated previously (using the nip_udp_write() function). You MUST NOT write beyond that mark or buffer overflows in the memory management system will occur.
sockid | id of socket to return pointer at buffer for |
Definition at line 221 of file udp.c.
References nip_mem_ptr(), and nip_udp_data_mem_ptr().
Referenced by nip_dhcp_disp_check(), nip_mdns_disp_check(), nip_mdns_disp_check_cache(), and nip_mdns_tx_init().
void nip_udp_disp_receive | ( | void | ) |
Dispatcher function to be called upon packet reception.
Definition at line 479 of file udp.c.
References nip_udp_buffer::addr, nip_udp_sock::addr, nip_udp_buffer::buf, nip_disp_params_t::common, nip_net_if_trans_params_t::dst_nw_addr, nip_udp_header::dst_port, nip_net_if_trans_t::flags, nip_udp_sock::flags, nip_net_if_trans_flags_t::free_payload, nip_net_if_trans_params_t::ip, nip_udp_sock_addr::ip, nip_udp_header::length, nip_udp_buffer::next, nip_disp_state_t::next, nip_disp, nip_mem_buf_used(), NIP_MEM_NULL, nip_mem_obtain_ptr(), nip_mem_release_block(), nip_memcmp, nip_memcpy(), NIP_MULTICAST_ENABLE, NIP_NET_IF_DONE, nip_reset_trans(), NIP_UDP_EOQ, NIP_UDP_FIXED_SOCKETS, NIP_UDP_SOCK_FLG_USED, ntohs, NULL, nip_net_if_trans_t::params, nip_net_if_trans_t::payload, nip_net_if_trans_t::payload_size, nip_udp_sock_addr::port, nip_udp_sock::rx_packet, nip_udp_buffer::size, nip_udp_buffer::sockid, nip_udp_sock::sockid, nip_net_if_trans_params_t::src_nw_addr, nip_udp_header::src_port, nip_net_if_trans_t::status, nip_disp_params_t::trans, udp_rx_queue, and udp_sockets.
void nip_udp_disp_send | ( | void | ) |
void nip_udp_forward | ( | nip_udp_sock_id_t | sockid | ) |
Forwards current incoming buffer to outgoing queue.
It will still have to be transmitted using nip_udp_transmit().
Definition at line 445 of file udp.c.
References nip_udp_buffer::buf, nip_udp_buffer::next, nip_mem_free(), NIP_MEM_NULL, NIP_UDP_EOQ, nip_udp_sock_ptr(), NULL, nip_udp_sock::rx_packet, nip_udp_sock::tx_packet, and udp_rx_queue.
Referenced by nip_dhcp_disp_check().
nip_error_t nip_udp_init_send | ( | nip_udp_sock_id_t | sockid, | |
nip_mem_size_t | size | |||
) |
Initialize send buffer for new packet.
Use this function in combination with nip_udp_write() or nip_udp_data_ptr() and nip_udp_transmit().
sockid | ID of socket to initialize send buffer for. | |
size | Size to be reserved (may be 0 for dynamic allocation) |
Call nip_udp_transmit() to send the buffer. nip_udp_data_ptr() will hand you a pointer to the buffer. See the description of nip_udp_data_ptr() for further instructions and warnings!
Definition at line 252 of file udp.c.
References NIP_E_INVALID_SOCK, NIP_E_OK, NIP_E_OUT_OF_MEMORY, nip_mem_alloc(), NIP_MEM_FLG_DELREAD, NIP_MEM_NULL, nip_mem_write(), nip_udp_sock_ptr(), NULL, and nip_udp_sock::tx_packet.
Referenced by nip_dhcp_disp_check(), and nip_mdns_tx_init().
void nip_udp_ptr_release | ( | nip_udp_sock_id_t | sockid, | |
nip_udp_buf_type_t | buftype | |||
) |
Release previously granted direct access.
This function has to be used in combination with nip_udp_data_ptr() as all all pointers obtained by the latter have to be released again.
Definition at line 172 of file udp.c.
References NIP_MEM_NULL, nip_mem_release_block(), and nip_udp_buf_handle().
Referenced by nip_dhcp_disp_check(), nip_mdns_disp_check(), nip_mdns_disp_check_cache(), and nip_mdns_tx_init().
nip_mem_size_t nip_udp_recvfrom | ( | nip_udp_sock_id_t | sockid, | |
uint8_t * | buf, | |||
nip_mem_size_t | size, | |||
struct nip_udp_sock_addr * | from_addr | |||
) |
Attempt to receive new packet from UDP socket.
The payload will be written to the supplied buffer pointer. If the datagram is to large to fit in the supplied buffer, it will be truncated at the buffer boundary, but not discarded. Subsequent calls to nip_udp_recvfrom() may be performed to read the remaining data.
sockid | id of socket to read from | |
*buf | pointer to buffer to write payload to | |
size | size of supplied buffer | |
*from_addr | pointer to structure where sender address information will be copied to. This pointer may be NULL if you're not interested in address information. |
Definition at line 394 of file udp.c.
References nip_udp_buffer::addr, nip_udp_buffer::buf, nip_udp_sock::flags, NIP_E_AGAIN, NIP_E_INVALID_SOCK, nip_error, nip_mem_read(), nip_memcpy(), nip_udp_cancel_recv(), NIP_UDP_EOQ, NIP_UDP_SOCK_FLG_NON_BLOCK, nip_udp_sock_ptr(), NULL, nip_udp_sock::rx_packet, nip_udp_buffer::size, and udp_rx_queue.
Referenced by nip_dhcp_disp_check(), and nip_mdns_disp_check().
nip_udp_sock_id_t nip_udp_socket | ( | struct nip_udp_sock_addr * | addr, | |
uint8_t | flags | |||
) |
Register UDP socket with given address and flags.
addr | structure to be filled with address and port. If all address bytes are set to Zero, the socket will listen on all interfaces. | |
flags | possible flags are:
|
Definition at line 64 of file udp.c.
References nip_udp_sock::addr, nip_udp_sock::flags, NIP_E_OUT_OF_RESSOURCES, nip_error, NIP_MEM_NULL, nip_memcpy(), NIP_UDP_EOQ, NIP_UDP_FIXED_SOCKETS, NIP_UDP_NO_SOCKET, NIP_UDP_SOCK_FLG_USED, NULL, nip_udp_sock::rx_packet, nip_udp_sock::sockid, nip_udp_sock::tx_packet, and udp_sockets.
Referenced by nip_dhcp_disp_sock_init(), and nip_mdns_init().
nip_error_t nip_udp_transmit | ( | nip_udp_sock_id_t | sockid, | |
struct nip_udp_sock_addr * | dest_addr, | |||
nip_net_if_id_t | if_id | |||
) |
Transmit the packet from the current send buffer to the given destination.
sockid | Socket ID | |
*dest_addr | pointer to destination address structure | |
net_if | ID of network interface to transmit message on. If you do not wish to specify the interface, put NIP_NET_NO_IF. |
Definition at line 322 of file udp.c.
References nip_udp_sock::addr, nip_udp_header::checksum, nip_udp_header::dst_port, nip_net_if_trans_t::flags, nip_net_if_trans_flags_t::free_payload, htons, nip_net_if_trans_params_t::ip, nip_udp_sock_addr::ip, nip_udp_header::length, NIP_DISP_CHECK_TRANS, nip_disp_notify, nip_dispatcher(), NIP_E_INVALID_SOCK, NIP_E_OK, nip_error, NIP_IP_PROTO_UDP, nip_ip_route(), nip_mem_buf_used(), NIP_MEM_NULL, nip_mem_obtain_ptr(), nip_mem_release_block(), NIP_NET_IF_RESOLV_ADDR, nip_udp_sock_ptr(), NULL, nip_net_if_trans_t::params, nip_net_if_trans_t::payload, nip_net_if_trans_t::payload_size, nip_udp_sock_addr::port, nip_net_if_trans_params_t::protocol, nip_net_if_trans_flags_t::reset_status, nip_udp_header::src_port, nip_net_if_trans_t::status, and nip_udp_sock::tx_packet.
Referenced by nip_dhcp_disp_check(), and nip_mdns_transmit().
nip_error_t nip_udp_write | ( | nip_udp_sock_id_t | sockid, | |
uint8_t * | buf, | |||
nip_mem_size_t | size | |||
) |
Write bytes to socket.
Transmission has to be trigged by nip_udp_transmit().
Definition at line 296 of file udp.c.
References nip_mem_write(), nip_udp_sock_ptr(), and nip_udp_sock::tx_packet.
Referenced by nip_dhcp_disp_check(), and nip_mdns_tx_init().