udp.h File Reference

#include "nip_init.h"
#include "mem.h"

Include dependency graph for udp.h:

This graph shows which files directly or indirectly include this file:

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 Documentation

#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 maximum number of UDP sockets for dynamic socket array.

Definition at line 39 of file udp.h.

#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

Each entry in packet queue has cost of sizeof(struct nip_udp_buffer).

Definition at line 62 of file udp.h.

#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

Any socket will not be checked for changes twice within the interval, unless its timer has been reset.

Definition at line 57 of file udp.h.

#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.

Definition at line 51 of file udp.h.


Typedef Documentation

typedef uint8_t nip_udp_queue_cnt_t

Definition at line 84 of file udp.h.

typedef uint8_t nip_udp_queue_id_t

Definition at line 83 of file udp.h.

typedef uint16_t nip_udp_sock_cnt_t

Definition at line 71 of file udp.h.

typedef uint16_t nip_udp_sock_id_t

Definition at line 70 of file udp.h.


Enumeration Type Documentation

Enumerator:
NIP_UDP_RX_BUF 
NIP_UDP_TX_BUF 

Definition at line 97 of file udp.h.


Function Documentation

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().

Here is the call graph for this function:

Here is the caller graph for this function:

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().

Here is the call graph for this function:

Here is the caller graph for this function:

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().

Here is the call graph for this function:

Here is the caller graph for this function:

void nip_udp_close ( nip_udp_sock_id_t  sockid  ) 

Unregister previous socket registration.

Todo:
implement not fixed sockets

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().

Here is the call graph for this function:

Here is the caller graph for this function:

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().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Warning:
The buffer will be locked until nip_udp_ptr_release() has been called which will affect memory management and packet transmission. You have to call nip_udp_ptr_release() before transmitting the packet.

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.

Parameters:
sockid id of socket to return pointer at buffer for
Returns:
pointer to buffer or NULL. In the latter case nip_error NIP_E_INVALID_SOCK for an invalid sockid or NIP_E_INVALID_BLOCK if the buffer had not been initialized, or NIP_E_BLOCK_LOCKED if ressource was locked.

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().

Here is the call graph for this function:

Here is the caller graph for this function:

void nip_udp_disp_receive ( void   ) 

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().

Here is the call graph for this function:

Here is the caller graph for this function:

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().

Parameters:
sockid ID of socket to initialize send buffer for.
size Size to be reserved (may be 0 for dynamic allocation)
The socket buffer will be initialized without data, use nip_udp_write() to fill the buffer. You should provide the buffer size, if known in advance, to ensure there is enough memory available to buffer your packet. If you do not provide the buffer size it will be automatically increased whenever nip_udp_write() is being called, which may then fail if not enough memory is available.

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!

Warning:
Any previous buffer that has not been scheduled for transmission will be overwritten.
Returns:
NIP_E_OK, NIP_E_OUT_OF_MEMORY, of NIP_E_INVALID_SOCK

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().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Warning:
Do not use the given direct-access pointer after calling this function.

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().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Parameters:
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.
Returns:
total size of available buffer. If the return value is smaller than the provided buffer it indicates how much of the buffer has been filled, if it is larger it indicates that the packet has been truncated. If 0 is returned the nip_error variable will be set to one of the following values:
  • NIP_E_AGAIN no data available on non-blocking socket
  • NIP_E_INVALID_SOCK invalid socket or socket closed while waiting for data

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().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Parameters:
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:
  • NIP_UDP_SOCK_FLG_NON_BLOCK
  • NIP_UDP_SOCK_FLG_LISTENING
Returns:
NIP_UDP_NO_SOCKET on error. Global nip_error variable will be set to NIP_E_OUT_OF_RESSOURCES in that case.

Todo:
reserve socket in global list

Todo:
implement not fixed sockets

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().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Warning:
Never perform transmit calls from within the dispatcher on blocking sockets!!! That will most likely lead to deadlock as nip_udp_transmit() will prevent the dispatcher from finishing the transmission until it has finished the transmission, which would happen in the not so near future near infinity. ;)
Parameters:
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.
Returns:
  • NIP_E_OK packet has been added to outgoing packet queue
  • NIP_E_DEV_BUSY outgoing queue of interface is full, try again
  • NIP_E_NO_ROUTE_TO_HOST no interface will deliver to destination address

Todo:
add checksum

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().

Here is the call graph for this function:

Here is the caller graph for this function:

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().

Here is the call graph for this function:

Here is the caller graph for this function:


Generated on Thu Jul 10 01:09:51 2008 for NIP by  doxygen 1.5.5