forked from eclipse/tinydtls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.c
107 lines (88 loc) · 2.72 KB
/
peer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*******************************************************************************
*
* Copyright (c) 2011, 2012, 2013, 2014, 2015 Olaf Bergmann (TZI) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Olaf Bergmann - initial API and implementation
* Hauke Mehrtens - memory optimization, ECC integration
*
*******************************************************************************/
#include "global.h"
#include "peer.h"
#include "dtls_debug.h"
#if !(defined (WITH_CONTIKI)) && !(defined (RIOT_VERSION))
void peer_init(void)
{
}
static inline dtls_peer_t *
dtls_malloc_peer(void) {
return (dtls_peer_t *)malloc(sizeof(dtls_peer_t));
}
void
dtls_free_peer(dtls_peer_t *peer) {
dtls_handshake_free(peer->handshake_params);
dtls_security_free(peer->security_params[0]);
dtls_security_free(peer->security_params[1]);
free(peer);
}
#elif defined (WITH_CONTIKI) /* WITH_CONTIKI */
#include "memb.h"
MEMB(peer_storage, dtls_peer_t, DTLS_PEER_MAX);
void
peer_init(void) {
memb_init(&peer_storage);
}
static inline dtls_peer_t *
dtls_malloc_peer(void) {
return memb_alloc(&peer_storage);
}
void
dtls_free_peer(dtls_peer_t *peer) {
dtls_handshake_free(peer->handshake_params);
dtls_security_free(peer->security_params[0]);
dtls_security_free(peer->security_params[1]);
memb_free(&peer_storage, peer);
}
#elif defined (RIOT_VERSION)
# include <memarray.h>
dtls_peer_t peer_storage_data[DTLS_PEER_MAX];
memarray_t peer_storage;
void
peer_init(void) {
memarray_init(&peer_storage, peer_storage_data, sizeof(dtls_peer_t), DTLS_PEER_MAX);
}
static inline dtls_peer_t *
dtls_malloc_peer(void) {
return memarray_alloc(&peer_storage);
}
void
dtls_free_peer(dtls_peer_t *peer) {
dtls_handshake_free(peer->handshake_params);
dtls_security_free(peer->security_params[0]);
dtls_security_free(peer->security_params[1]);
memarray_free(&peer_storage, peer);
}
#endif /* WITH_CONTIKI */
dtls_peer_t *
dtls_new_peer(const session_t *session) {
dtls_peer_t *peer;
peer = dtls_malloc_peer();
if (peer) {
memset(peer, 0, sizeof(dtls_peer_t));
memcpy(&peer->session, session, sizeof(session_t));
peer->security_params[0] = dtls_security_new();
if (!peer->security_params[0]) {
dtls_free_peer(peer);
return NULL;
}
dtls_dsrv_log_addr(DTLS_LOG_DEBUG, "dtls_new_peer", session);
}
return peer;
}