mirror of
https://github.com/eclipse/tinydtls.git
synced 2025-10-23 01:48:51 +08:00
session: add utility function for interacting with the session_t object
These functions were mostly added to make it easier to provide a ruby FFI for tinydtls. This is also the reason why `dtls_session_addr` dynamically allocates memory for a `struct sockaddr` instead of receiving a pointer to a memory location for a `struct sockaddr` as an argument. Change-Id: I91c3ee0ab03f5e8ed50e8e309c08b2436536496a Signed-off-by: Sören Tempel <tempel@uni-bremen.de> Signed-off-by: Olaf Bergmann <bergmann@tzi.org>
This commit is contained in:

committed by
Olaf Bergmann

parent
b9c58a1f23
commit
ef90034f32
46
session.c
46
session.c
@@ -71,6 +71,52 @@ dtls_session_init(session_t *sess) {
|
||||
sess->size = sizeof(sess->addr);
|
||||
}
|
||||
|
||||
/* These functions are primarly needed for the tinydtls ruby gem.
|
||||
*
|
||||
* They are not implemented on Contiki and RIOT because these operating
|
||||
* system don't supply malloc(3). This could be fixed by fixed by using
|
||||
* memory pools on these operating system as in `peer.c`. However, the
|
||||
* downside of this approach is that the memory pools reserve memory
|
||||
* even if the `dtls_new_session` isn't used and usually memory for the
|
||||
* `session_t` type is already resevered in the `peer_t` struct.
|
||||
* Therefore it would introduces quite some overhead on these
|
||||
* constrained platforms.
|
||||
*
|
||||
* In the long run we probably want to create two seperate memory pools
|
||||
* for sessions and peers and store a pointer to a session in the peer
|
||||
* struct.
|
||||
*/
|
||||
#if !(defined (WITH_CONTIKI)) && !(defined (RIOT_VERSION))
|
||||
session_t*
|
||||
dtls_new_session(struct sockaddr *addr, socklen_t addrlen) {
|
||||
session_t *sess;
|
||||
|
||||
sess = malloc(sizeof(session_t));
|
||||
if (!sess)
|
||||
return NULL;
|
||||
dtls_session_init(sess);
|
||||
|
||||
sess->size = addrlen;
|
||||
memcpy(&sess->addr.sa, addr, sess->size);
|
||||
|
||||
return sess;
|
||||
}
|
||||
|
||||
void
|
||||
dtls_free_session(session_t *sess) {
|
||||
free(sess);
|
||||
}
|
||||
#endif
|
||||
|
||||
struct sockaddr*
|
||||
dtls_session_addr(session_t *sess, socklen_t *addrlen) {
|
||||
if (!sess)
|
||||
return NULL;
|
||||
|
||||
*addrlen = sess->size;
|
||||
return &sess->addr.sa;
|
||||
}
|
||||
|
||||
int
|
||||
dtls_session_equals(const session_t *a, const session_t *b) {
|
||||
assert(a); assert(b);
|
||||
|
29
session.h
29
session.h
@@ -66,6 +66,35 @@ typedef struct {
|
||||
*/
|
||||
void dtls_session_init(session_t *sess);
|
||||
|
||||
#if !(defined (WITH_CONTIKI)) && !(defined (RIOT_VERSION))
|
||||
/**
|
||||
* Creates a new ::session_t for the given address.
|
||||
*
|
||||
* @param addr Address which should be stored in the ::session_t.
|
||||
* @param addrlen Length of the @p addr.
|
||||
* @return The new session or @c NULL on error.
|
||||
*/
|
||||
session_t* dtls_new_session(struct sockaddr *addr, socklen_t addrlen);
|
||||
|
||||
/**
|
||||
* Frees memory allocated for a session using ::dtls_new_session.
|
||||
*
|
||||
* @param sess Pointer to a session for which allocated memory should be
|
||||
* freed.
|
||||
*/
|
||||
void dtls_free_session(session_t *sess);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Extracts the address of the given ::session_t.
|
||||
*
|
||||
* @param sess Session to extract address for.
|
||||
* @param addrlen Pointer to memory location where the address
|
||||
* length should be stored.
|
||||
* @return The address or @c NULL if @p sess was @c NULL.
|
||||
*/
|
||||
struct sockaddr* dtls_session_addr(session_t *sess, socklen_t *addrlen);
|
||||
|
||||
/**
|
||||
* Compares the given session objects. This function returns @c 0
|
||||
* when @p a and @p b differ, @c 1 otherwise.
|
||||
|
Reference in New Issue
Block a user