mbed port of tinydtls
peer.h@0:04990d454f45, 2013-10-10 (annotated)
- Committer:
- ashleymills
- Date:
- Thu Oct 10 21:38:07 2013 +0000
- Revision:
- 0:04990d454f45
It now works. Found nasty gotcha with non-std sockaddr_in
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:04990d454f45 | 1 | /* dtls -- a very basic DTLS implementation |
ashleymills | 0:04990d454f45 | 2 | * |
ashleymills | 0:04990d454f45 | 3 | * Copyright (C) 2011--2013 Olaf Bergmann <bergmann@tzi.org> |
ashleymills | 0:04990d454f45 | 4 | * |
ashleymills | 0:04990d454f45 | 5 | * Permission is hereby granted, free of charge, to any person |
ashleymills | 0:04990d454f45 | 6 | * obtaining a copy of this software and associated documentation |
ashleymills | 0:04990d454f45 | 7 | * files (the "Software"), to deal in the Software without |
ashleymills | 0:04990d454f45 | 8 | * restriction, including without limitation the rights to use, copy, |
ashleymills | 0:04990d454f45 | 9 | * modify, merge, publish, distribute, sublicense, and/or sell copies |
ashleymills | 0:04990d454f45 | 10 | * of the Software, and to permit persons to whom the Software is |
ashleymills | 0:04990d454f45 | 11 | * furnished to do so, subject to the following conditions: |
ashleymills | 0:04990d454f45 | 12 | * |
ashleymills | 0:04990d454f45 | 13 | * The above copyright notice and this permission notice shall be |
ashleymills | 0:04990d454f45 | 14 | * included in all copies or substantial portions of the Software. |
ashleymills | 0:04990d454f45 | 15 | * |
ashleymills | 0:04990d454f45 | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
ashleymills | 0:04990d454f45 | 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
ashleymills | 0:04990d454f45 | 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ashleymills | 0:04990d454f45 | 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
ashleymills | 0:04990d454f45 | 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
ashleymills | 0:04990d454f45 | 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
ashleymills | 0:04990d454f45 | 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
ashleymills | 0:04990d454f45 | 23 | * SOFTWARE. |
ashleymills | 0:04990d454f45 | 24 | */ |
ashleymills | 0:04990d454f45 | 25 | |
ashleymills | 0:04990d454f45 | 26 | /** |
ashleymills | 0:04990d454f45 | 27 | * @file peer.h |
ashleymills | 0:04990d454f45 | 28 | * @brief information about peers in a DTLS session |
ashleymills | 0:04990d454f45 | 29 | */ |
ashleymills | 0:04990d454f45 | 30 | |
ashleymills | 0:04990d454f45 | 31 | #ifndef _PEER_H_ |
ashleymills | 0:04990d454f45 | 32 | #define _PEER_H_ |
ashleymills | 0:04990d454f45 | 33 | |
ashleymills | 0:04990d454f45 | 34 | #include "config.h" |
ashleymills | 0:04990d454f45 | 35 | #include "global.h" |
ashleymills | 0:04990d454f45 | 36 | |
ashleymills | 0:04990d454f45 | 37 | #include "state.h" |
ashleymills | 0:04990d454f45 | 38 | #include "crypto.h" |
ashleymills | 0:04990d454f45 | 39 | |
ashleymills | 0:04990d454f45 | 40 | #ifndef WITH_CONTIKI |
ashleymills | 0:04990d454f45 | 41 | #include "uthash.h" |
ashleymills | 0:04990d454f45 | 42 | #endif /* WITH_CONTIKI */ |
ashleymills | 0:04990d454f45 | 43 | |
ashleymills | 0:04990d454f45 | 44 | /** |
ashleymills | 0:04990d454f45 | 45 | * Holds security parameters, local state and the transport address |
ashleymills | 0:04990d454f45 | 46 | * for each peer. */ |
ashleymills | 0:04990d454f45 | 47 | typedef struct dtls_peer_t { |
ashleymills | 0:04990d454f45 | 48 | #ifndef WITH_CONTIKI |
ashleymills | 0:04990d454f45 | 49 | UT_hash_handle hh; |
ashleymills | 0:04990d454f45 | 50 | #else /* WITH_CONTIKI */ |
ashleymills | 0:04990d454f45 | 51 | struct dtls_peer_t *next; |
ashleymills | 0:04990d454f45 | 52 | #endif /* WITH_CONTIKI */ |
ashleymills | 0:04990d454f45 | 53 | |
ashleymills | 0:04990d454f45 | 54 | session_t session; /**< peer address and local interface */ |
ashleymills | 0:04990d454f45 | 55 | |
ashleymills | 0:04990d454f45 | 56 | dtls_state_t state; /**< DTLS engine state */ |
ashleymills | 0:04990d454f45 | 57 | uint16 epoch; /**< counter for cipher state changes*/ |
ashleymills | 0:04990d454f45 | 58 | uint48 rseq; /**< sequence number of last record sent */ |
ashleymills | 0:04990d454f45 | 59 | |
ashleymills | 0:04990d454f45 | 60 | dtls_hs_state_t hs_state; /**< handshake protocol status */ |
ashleymills | 0:04990d454f45 | 61 | |
ashleymills | 0:04990d454f45 | 62 | dtls_security_parameters_t security_params[2]; |
ashleymills | 0:04990d454f45 | 63 | int config; /**< denotes which security params are in effect */ |
ashleymills | 0:04990d454f45 | 64 | /* FIXME: check if we can use epoch for this */ |
ashleymills | 0:04990d454f45 | 65 | } dtls_peer_t; |
ashleymills | 0:04990d454f45 | 66 | |
ashleymills | 0:04990d454f45 | 67 | /** |
ashleymills | 0:04990d454f45 | 68 | * Creates a new peer for given @p session. The current configuration |
ashleymills | 0:04990d454f45 | 69 | * is initialized with the cipher suite TLS_NULL_WITH_NULL_NULL (i.e. |
ashleymills | 0:04990d454f45 | 70 | * no security at all). This function returns a pointer to the new |
ashleymills | 0:04990d454f45 | 71 | * peer or NULL on error. The caller is responsible for releasing the |
ashleymills | 0:04990d454f45 | 72 | * storage allocated for this peer using dtls_free_peer(). |
ashleymills | 0:04990d454f45 | 73 | * |
ashleymills | 0:04990d454f45 | 74 | * @param session The remote peer's address and local interface index. |
ashleymills | 0:04990d454f45 | 75 | * @return A pointer to a newly created and initialized peer object |
ashleymills | 0:04990d454f45 | 76 | * or NULL on error. |
ashleymills | 0:04990d454f45 | 77 | */ |
ashleymills | 0:04990d454f45 | 78 | dtls_peer_t *dtls_new_peer(const session_t *session); |
ashleymills | 0:04990d454f45 | 79 | |
ashleymills | 0:04990d454f45 | 80 | /** Releases the storage allocated to @p peer. */ |
ashleymills | 0:04990d454f45 | 81 | void dtls_free_peer(dtls_peer_t *peer); |
ashleymills | 0:04990d454f45 | 82 | |
ashleymills | 0:04990d454f45 | 83 | /** Returns the current state of @p peer. */ |
ashleymills | 0:04990d454f45 | 84 | static inline dtls_state_t dtls_peer_state(const dtls_peer_t *peer) { |
ashleymills | 0:04990d454f45 | 85 | return peer->state; |
ashleymills | 0:04990d454f45 | 86 | } |
ashleymills | 0:04990d454f45 | 87 | |
ashleymills | 0:04990d454f45 | 88 | /** |
ashleymills | 0:04990d454f45 | 89 | * Checks if given @p peer is connected. This function returns |
ashleymills | 0:04990d454f45 | 90 | * @c 1 if connected, or @c 0 otherwise. |
ashleymills | 0:04990d454f45 | 91 | */ |
ashleymills | 0:04990d454f45 | 92 | static inline int dtls_peer_is_connected(const dtls_peer_t *peer) { |
ashleymills | 0:04990d454f45 | 93 | return peer->state == DTLS_STATE_CONNECTED; |
ashleymills | 0:04990d454f45 | 94 | } |
ashleymills | 0:04990d454f45 | 95 | |
ashleymills | 0:04990d454f45 | 96 | #define CURRENT_CONFIG(Peer) (&(Peer)->security_params[(Peer)->config]) |
ashleymills | 0:04990d454f45 | 97 | #define OTHER_CONFIG(Peer) (&(Peer)->security_params[!((Peer)->config & 0x01)]) |
ashleymills | 0:04990d454f45 | 98 | |
ashleymills | 0:04990d454f45 | 99 | #define SWITCH_CONFIG(Peer) ((Peer)->config = !((Peer)->config & 0x01)) |
ashleymills | 0:04990d454f45 | 100 | |
ashleymills | 0:04990d454f45 | 101 | #endif /* _PEER_H_ */ |