41fd943f96ef5180a510c3f9e55683ae180ff86d
[platform/upstream/iotivity.git] / extlibs / tinydtls / peer.h
1 /* dtls -- a very basic DTLS implementation
2  *
3  * Copyright (C) 2011--2013 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 /**
27  * @file peer.h
28  * @brief information about peers in a DTLS session
29  */
30
31 #ifndef _DTLS_PEER_H_
32 #define _DTLS_PEER_H_
33
34 #include <sys/types.h>
35
36 #include "tinydtls.h"
37 #include "global.h"
38 #include "session.h"
39
40 #include "state.h"
41 #include "crypto.h"
42
43 #ifndef WITH_CONTIKI
44 #include "uthash.h"
45 #endif /* WITH_CONTIKI */
46
47 typedef enum { DTLS_CLIENT=0, DTLS_SERVER } dtls_peer_type;
48
49 /** 
50  * Holds security parameters, local state and the transport address
51  * for each peer. */
52 typedef struct dtls_peer_t {
53 #ifndef WITH_CONTIKI
54   UT_hash_handle hh;
55 #else /* WITH_CONTIKI */
56   struct dtls_peer_t *next;
57 #endif /* WITH_CONTIKI */
58
59   session_t session;         /**< peer address and local interface */
60
61   dtls_peer_type role;       /**< denotes if this host is DTLS_CLIENT or DTLS_SERVER */
62   dtls_state_t state;        /**< DTLS engine state */
63
64   dtls_security_parameters_t *security_params[2];
65   dtls_handshake_parameters_t *handshake_params;
66 } dtls_peer_t;
67
68 INLINE_API dtls_security_parameters_t *dtls_security_params_epoch(dtls_peer_t *peer, uint16_t epoch)
69 {
70   if (peer->security_params[0] && peer->security_params[0]->epoch == epoch) {
71     return peer->security_params[0];
72   } else if (peer->security_params[1] && peer->security_params[1]->epoch == epoch) {
73     return peer->security_params[1];
74   } else {
75     return NULL;
76   }
77 }
78
79 INLINE_API dtls_security_parameters_t *dtls_security_params(dtls_peer_t *peer)
80 {
81   return peer->security_params[0];
82 }
83
84 INLINE_API dtls_security_parameters_t *dtls_security_params_next(dtls_peer_t *peer)
85 {
86   if (peer->security_params[1])
87     dtls_security_free(peer->security_params[1]);
88
89   peer->security_params[1] = dtls_security_new();
90   if (!peer->security_params[1]) {
91     return NULL;
92   }
93   peer->security_params[1]->epoch = peer->security_params[0]->epoch + 1;
94   return peer->security_params[1];
95 }
96
97 INLINE_API void dtls_security_params_free_other(dtls_peer_t *peer)
98 {
99   dtls_security_parameters_t * security0 = peer->security_params[0];
100   dtls_security_parameters_t * security1 = peer->security_params[1];
101
102   if (!security0 || !security1 || security0->epoch < security1->epoch)
103     return;
104
105   dtls_security_free(security1);
106   peer->security_params[1] = NULL;
107 }
108
109 INLINE_API void dtls_security_params_switch(dtls_peer_t *peer)
110 {
111   dtls_security_parameters_t * security = peer->security_params[1];
112
113   peer->security_params[1] = peer->security_params[0];
114   peer->security_params[0] = security;
115 }
116
117 void peer_init();
118
119 /**
120  * Creates a new peer for given @p session. The current configuration
121  * is initialized with the cipher suite TLS_NULL_WITH_NULL_NULL (i.e.
122  * no security at all). This function returns a pointer to the new
123  * peer or NULL on error. The caller is responsible for releasing the
124  * storage allocated for this peer using dtls_free_peer().
125  *
126  * @param session  The remote peer's address and local interface index.
127  * @return A pointer to a newly created and initialized peer object
128  * or NULL on error.
129  */
130 dtls_peer_t *dtls_new_peer(const session_t *session);
131
132 /** Releases the storage allocated to @p peer. */
133 void dtls_free_peer(dtls_peer_t *peer);
134
135 /** Returns the current state of @p peer. */
136 INLINE_API dtls_state_t dtls_peer_state(const dtls_peer_t *peer) {
137   return peer->state;
138 }
139
140 /**
141  * Checks if given @p peer is connected. This function returns
142  * @c 1 if connected, or @c 0 otherwise.
143  */
144 INLINE_API int dtls_peer_is_connected(const dtls_peer_t *peer) {
145   return peer->state == DTLS_STATE_CONNECTED;
146 }
147
148 #endif /* _DTLS_PEER_H_ */