1 /* netq.h -- Simple packet queue
3 * Copyright (C) 2010--2012 Olaf Bergmann <bergmann@tzi.org>
5 * This file is part of the library tinyDTLS. Please see the file
6 * LICENSE for terms of use.
15 #include "dtls_time.h"
18 * \defgroup netq Network Packet Queue
19 * The netq utility functions implement an ordered queue of data packets
20 * to send over the network and can also be used to queue received packets
26 #if defined(DTLS_ECC) || defined(DTLS_X509)
27 #define NETQ_MAXCNT 5 /**< maximum number of elements in netq structure */
28 #elif defined(DTLS_PSK)
29 #define NETQ_MAXCNT 3 /**< maximum number of elements in netq structure */
34 * Datagrams in the netq_t structure have a fixed maximum size of
35 * DTLS_MAX_BUF to simplify memory management on constrained nodes. */
36 typedef unsigned char netq_packet_t[DTLS_MAX_BUF];
38 typedef struct netq_t {
41 clock_time_t t; /**< when to send PDU for the next time */
42 unsigned int timeout; /**< randomized timeout value */
44 dtls_peer_t *peer; /**< remote address */
47 unsigned char retransmit_cnt; /**< retransmission counter, will be removed when zero */
49 size_t length; /**< actual length of data */
51 unsigned char data[]; /**< the datagram to send */
53 netq_packet_t data; /**< the datagram to send */
58 static inline void netq_init()
65 * Adds a node to the given queue, ordered by their time-stamp t.
66 * This function returns @c 0 on error, or non-zero if @p node has
67 * been added successfully.
69 * @param queue A pointer to the queue head where @p node will be added.
70 * @param node The new item to add.
71 * @return @c 0 on error, or non-zero if the new item was added.
73 int netq_insert_node(list_t queue, netq_t *node);
75 /** Destroys specified node and releases any memory that was allocated
76 * for the associated datagram. */
77 void netq_node_free(netq_t *node);
79 /** Removes all items from given queue and frees the allocated storage */
80 void netq_delete_all(list_t queue);
82 /** Creates a new node suitable for adding to a netq_t queue. */
83 netq_t *netq_node_new(size_t size);
86 * Returns a pointer to the first item in given queue or NULL if
89 netq_t *netq_head(list_t queue);
91 netq_t *netq_next(netq_t *p);
92 void netq_remove(list_t queue, netq_t *p);
95 * Removes the first item in given queue and returns a pointer to the
96 * removed element. If queue is empty when netq_pop_first() is called,
97 * this function returns NULL.
99 netq_t *netq_pop_first(list_t queue);
103 #endif /* _DTLS_NETQ_H_ */