Make MTU configurable.

Use static or stack storage for coap_packet_t.
This commit is contained in:
jcmichelou committed 2017-06-26 17:34:01 +02:00
1 parent 6e97dbafcf
commit b2fe4a64de
29 files changed
+303 -165

No files matched your search

+2 -2
View File
@@ -22,9 +22,9 @@
#ifndef COAP_MAX_BLOCK_SZX
/**
* The largest value for the SZX component in a Block option. Note that
* 1 << (COAP_MAX_BLOCK_SZX + 4) should not exceed COAP_MAX_PDU_SIZE.
* 1 << (COAP_MAX_BLOCK_SZX + 4) should not exceed COAP_DEFAULT_PDU_SIZE.
*/
#define COAP_MAX_BLOCK_SZX 4
#define COAP_MAX_BLOCK_SZX 6
#endif /* COAP_MAX_BLOCK_SZX */
/**
+26 -24
View File
@@ -20,13 +20,6 @@
* @{
*/
/**
* The maximum expected size of a DTLS record. This constant is used
* to allocate storage in the sendqueue for a DTLS session in case the
* data cannot be sent immediately.
*/
#define COAP_DTLS_MAX_PACKET_SIZE COAP_MAX_PDU_SIZE
/** Returns 1 if support for DTLS is enabled, or 0 otherwise. */
int coap_dtls_is_supported(void);
@@ -44,10 +37,10 @@ int coap_dtls_get_log_level(void);
* @return A DTLS context object or NULL on error;
*/
void *
coap_dtls_new_context( struct coap_context_t *coap_context );
coap_dtls_new_context(struct coap_context_t *coap_context);
/** Releases the storage allocated for @p dtls_context. */
void coap_dtls_free_context( void *dtls_context );
void coap_dtls_free_context(void *dtls_context);
/**
* Create a new client-side session. This should send a HELLO to the server.
@@ -55,7 +48,7 @@ void coap_dtls_free_context( void *dtls_context );
* @param session The CoAP session
* @return Opaque handle to underlying TLS library object containing security parameters for the session.
*/
void *coap_dtls_new_client_session( coap_session_t *session );
void *coap_dtls_new_client_session(coap_session_t *session);
/**
* Create a new server-side session.
@@ -65,14 +58,14 @@ void *coap_dtls_new_client_session( coap_session_t *session );
* @param session The CoAP session
* @return Opaque handle to underlying TLS library object containing security parameters for the session.
*/
void *coap_dtls_new_server_session( coap_session_t *session );
void *coap_dtls_new_server_session(coap_session_t *session);
/**
* Terminates the DTLS session (may send an ALERT if necessary) then frees the underlying TLS library object containing security parameters for the session.
*
* @param session The CoAP session
*/
void coap_dtls_free_session( coap_session_t *session );
void coap_dtls_free_session(coap_session_t *session);
/**
* Send data to a DTLS peer.
@@ -81,9 +74,9 @@ void coap_dtls_free_session( coap_session_t *session );
* @param pdu The CoAP PDU
* @return 0 if this would be blocking, -1 if there is an error or the number of cleartext bytes sent
*/
int coap_dtls_send( coap_session_t *session,
const uint8_t *data,
size_t data_len );
int coap_dtls_send(coap_session_t *session,
const uint8_t *data,
size_t data_len);
/**
* Do all pending retransmits and get next timeout
@@ -91,7 +84,7 @@ int coap_dtls_send( coap_session_t *session,
* @param dtls_context The DTLS context
* @return <0 If not implemented, i.e. each session has its own timeout, 0 if no timeout, >0 Number of milliseconds until the next timeout.
*/
int coap_dtls_get_context_timeout( void *dtls_context );
int coap_dtls_get_context_timeout(void *dtls_context);
/**
* Get next timeout for this session.
@@ -99,14 +92,14 @@ int coap_dtls_get_context_timeout( void *dtls_context );
* @param session The CoAP session
* @return <0 If no event is pending, >=0 Number of milliseconds until the next timeout.
*/
int coap_dtls_get_timeout( coap_session_t *session );
int coap_dtls_get_timeout(coap_session_t *session);
/**
* Handle a DTLS timeout expiration.
*
* @param session The CoAP session
*/
void coap_dtls_handle_timeout( coap_session_t *session );
void coap_dtls_handle_timeout(coap_session_t *session);
/**
* Handling incoming data from a DTLS peer.
@@ -116,9 +109,9 @@ void coap_dtls_handle_timeout( coap_session_t *session );
* @param data_len Encrypted datagram size
* @return result of coap_handle_message on the decrypted CoAP PDU or -1 for error.
*/
int coap_dtls_receive( coap_session_t *session,
const uint8_t *data,
size_t data_len);
int coap_dtls_receive(coap_session_t *session,
const uint8_t *data,
size_t data_len);
/**
* Handling client HELLO messages from a new candiate peer.
@@ -129,9 +122,18 @@ int coap_dtls_receive( coap_session_t *session,
* @param data_len Encrypted datagram size
* @return 0 if a cookie verification message has been sent, 1 if the HELLO contains a valid cookie and a server session should be created, -1 if the message is invalid.
*/
int coap_dtls_hello( coap_session_t *session,
const uint8_t *data,
size_t data_len );
int coap_dtls_hello(coap_session_t *session,
const uint8_t *data,
size_t data_len);
/**
* Get DTLS overhead over cleartext PDUs.
*
* @param session The CoAP session
* @return maximum number of bytes added by DTLS layer.
*/
unsigned int coap_dtls_get_overhead(coap_session_t *session);
/** @} */
+7 -10
View File
@@ -15,6 +15,10 @@
#include "address.h"
#ifndef COAP_RXBUFFER_SIZE
#define COAP_RXBUFFER_SIZE 1472
#endif /* COAP_RXBUFFER_SIZE */
#ifdef _WIN32
typedef SOCKET coap_fd_t;
#define coap_closesocket closesocket
@@ -107,24 +111,17 @@ ssize_t coap_network_send( coap_socket_t *sock, const struct coap_session_t *ses
* error, @p *packet is set to NULL.
*
* @param sock Socket to read data from
* @param packet A result parameter where a pointer to the received packet
* structure is stored. The caller must call coap_free_packet to
* release the storage used by this packet.
* @param packet Received packet metadata and payload. src and dst should be preset.
*
* @return The number of bytes received on success, or a value less than
* zero on error.
*/
ssize_t coap_network_read( coap_socket_t *sock, struct coap_packet_t **packet );
ssize_t coap_network_read( coap_socket_t *sock, struct coap_packet_t *packet );
#ifndef coap_mcast_interface
# define coap_mcast_interface(Local) 0
#endif
/**
* Releases the storage allocated for @p packet.
*/
void coap_free_packet(struct coap_packet_t *packet);
/**
* Given a packet, set msg and msg_len to an address and length of the packet's
* data in memory.
@@ -164,7 +161,7 @@ struct coap_packet_t {
coap_address_t dst; /**< the packet's destination address */
int ifindex; /**< the interface index */
size_t length; /**< length of payload */
unsigned char payload[]; /**< payload */
unsigned char payload[COAP_RXBUFFER_SIZE]; /**< payload */
};
#endif
typedef struct coap_packet_t coap_packet_t;
+33 -3
View File
@@ -55,9 +55,11 @@ typedef struct coap_session_t {
coap_proto_t proto; /**< protocol used */
coap_session_type_t type; /**< client or server side socket */
coap_session_state_t state; /**< current state of relationaship with peer */
int ref; /**< reference count from queues */
coap_address_t local_addr; /**< local address and port */
uint8_t ref; /**< reference count from queues */
uint16_t mtu; /**< path mtu */
uint16_t tls_overhead; /**< overhead of TLS layer */
coap_address_t remote_addr; /**< remote address and port */
coap_address_t local_addr; /**< local address and port */
int ifindex; /**< interface index */
coap_socket_t sock; /**< socket object for the session, if any */
struct coap_endpoint_t *endpoint; /**< session's endpoint */
@@ -109,6 +111,23 @@ void coap_session_reset(coap_session_t *session);
*/
void coap_session_connected(coap_session_t *session);
/**
* Set the session MTU. This is the maximum message size that can be sent,
* excluding IP and UDP overhead.
*
* @param session The CoAP session.
* @param mtu maximum message size
*/
void coap_session_set_mtu(coap_session_t *session, unsigned mtu);
/**
* Get maximum acceptable PDU size
*
* @param session The CoAP session.
* @return maximum PDU size
*/
unsigned int coap_session_max_pdu_size(coap_session_t *session);
/**
* Creates a new client session to the designated server.
* @param ctx The CoAP context.
@@ -179,8 +198,9 @@ coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, int retransmit_
*/
typedef struct coap_endpoint_t {
struct coap_endpoint_t *next;
struct coap_context_t *context; /**< endpoint's context */
struct coap_context_t *context; /**< endpoint's context */
coap_proto_t proto; /**< protocol used on this interface */
uint16_t default_mtu; /**< default mtu for this interface */
coap_socket_t sock; /**< socket object for the interface, if any */
coap_address_t bind_addr; /**< local interface address */
coap_session_t *sessions; /**< list of active sessions */
@@ -197,8 +217,18 @@ typedef struct coap_endpoint_t {
coap_endpoint_t *coap_new_endpoint(struct coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto);
/**
* Set the endpoint's default MTU. This is the maximum message size that can be
* sent, excluding IP and UDP overhead.
*
* @param session The CoAP session.
* @param mtu maximum message size
*/
void coap_endpoint_set_default_mtu(coap_endpoint_t *ep, unsigned mtu);
void coap_free_endpoint(coap_endpoint_t *ep);
/**
* Get endpoint description.
*
+5
View File
@@ -35,6 +35,10 @@
#define MEMP_NUM_COAPPDU MEMP_NUM_COAPNODE
#endif
#ifndef MEMP_NUM_COAPSESSION
#define MEMP_NUM_COAPSESSION 2
#endif
#ifndef MEMP_NUM_COAP_SUBSCRIPTION
#define MEMP_NUM_COAP_SUBSCRIPTION 4
#endif
@@ -52,6 +56,7 @@ LWIP_MEMPOOL(COAP_ENDPOINT, MEMP_NUM_COAPENDPOINT, sizeof(coap_endpoint_t), "COA
LWIP_MEMPOOL(COAP_PACKET, MEMP_NUM_COAPPACKET, sizeof(coap_packet_t), "COAP_PACKET")
LWIP_MEMPOOL(COAP_NODE, MEMP_NUM_COAPNODE, sizeof(coap_queue_t), "COAP_NODE")
LWIP_MEMPOOL(COAP_PDU, MEMP_NUM_COAPPDU, sizeof(coap_pdu_t), "COAP_PDU")
LWIP_MEMPOOL(COAP_SESSION, MEMP_NUM_COAPSESSION, sizeof(coap_session_t), "COAP_SESSION")
LWIP_MEMPOOL(COAP_subscription, MEMP_NUM_COAP_SUBSCRIPTION, sizeof(coap_subscription_t), "COAP_subscription")
LWIP_MEMPOOL(COAP_RESOURCE, MEMP_NUM_COAPRESOURCE, sizeof(coap_resource_t), "COAP_RESOURCE")
LWIP_MEMPOOL(COAP_RESOURCEATTR, MEMP_NUM_COAPRESOURCEATTR, sizeof(coap_attr_t), "COAP_RESOURCEATTR")
+1 -1
View File
@@ -38,7 +38,7 @@ typedef enum {
COAP_PDU_BUF,
COAP_RESOURCE,
COAP_RESOURCEATTR,
COAP_DTLS_SESSION
COAP_SESSION
} coap_memory_tag_t;
#ifndef WITH_LWIP
+3 -1
View File
@@ -129,7 +129,7 @@ typedef struct coap_context_t {
ssize_t (*network_send)(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen);
ssize_t (*network_read)(coap_socket_t *sock, struct coap_packet_t **packet);
ssize_t (*network_read)(coap_socket_t *sock, struct coap_packet_t *packet);
size_t(*get_client_psk)(const coap_session_t *session, const uint8_t *hint, size_t hint_len, uint8_t *identity, size_t *identity_len, size_t max_identity_len, uint8_t *psk, size_t max_psk_len);
size_t(*get_server_psk)(const coap_session_t *session, const uint8_t *identity, size_t identity_len, uint8_t *psk, size_t max_psk_len);
@@ -527,11 +527,13 @@ int coap_option_check_critical(coap_context_t *ctx,
* must be either sent with coap_sent() or released by coap_delete_pdu().
*
* @param context The current coap context to use.
* @param session The CoAP session.
* @param request The request for @c .well-known/core .
*
* @return A new 2.05 response for @c .well-known/core or NULL on error.
*/
coap_pdu_t *coap_wellknown_response(coap_context_t *context,
coap_session_t *session,
coap_pdu_t *request);
#endif /* _COAP_NET_H_ */
+1 -1
View File
@@ -371,7 +371,7 @@ unsigned short coap_opt_delta(const coap_opt_t *opt);
/** @deprecated { Use coap_opt_encode() instead. } */
#define COAP_OPT_SETDELTA(opt,val) \
coap_opt_encode((opt), COAP_MAX_PDU_SIZE, (val), NULL, 0)
coap_opt_encode((opt), COAP_DEFAULT_PDU_SIZE, (val), NULL, 0)
/**
* Returns the length of the given option. @p opt must point to an option jump
+4 -7
View File
@@ -24,9 +24,9 @@
#define COAP_DEFAULT_PORT 5683 /* CoAP default UDP port */
#define COAPS_DEFAULT_PORT 5684 /* CoAP default UDP port for secure transmission */
#define COAP_DEFAULT_MAX_AGE 60 /* default maximum object lifetime in seconds */
#ifndef COAP_MAX_PDU_SIZE
#define COAP_MAX_PDU_SIZE 1400 /* maximum size of a CoAP PDU */
#endif /* COAP_MAX_PDU_SIZE */
#ifndef COAP_DEFAULT_PDU_SIZE
#define COAP_DEFAULT_PDU_SIZE 1152 /* default maximum size of a CoAP PDU */
#endif /* COAP_DEFAULT_PDU_SIZE */
#define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */
#define COAP_DEFAULT_SCHEME "coap" /* the default scheme for CoAP URIs */
@@ -320,14 +320,11 @@ void coap_pdu_clear(coap_pdu_t *pdu, size_t size);
/**
* Creates a new CoAP PDU.
* The object is created on the heap and must be released using
* coap_delete_pdu() unless coap_send() is called in which case
* coap_send() will take care of calling coap_delete_pdu().
*
* @deprecated This function allocates the maximum storage for each
* PDU. Use coap_pdu_init() instead.
*/
coap_pdu_t *coap_new_pdu(void);
coap_pdu_t *coap_new_pdu(struct coap_session_t *session);
/**
* Dispose of an CoAP PDU and frees associated storage.