mbed port of tinydtls
peer.c@1:bc8a649bad13, 2013-10-11 (annotated)
- Committer:
- ashleymills
- Date:
- Fri Oct 11 08:46:21 2013 +0000
- Revision:
- 1:bc8a649bad13
- Parent:
- 0:04990d454f45
Cleaned up all the debug stuff I added finding the hash table bug.
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 | #define __DEBUG__ 4 |
ashleymills | 0:04990d454f45 | 27 | |
ashleymills | 0:04990d454f45 | 28 | #ifndef __MODULE__ |
ashleymills | 0:04990d454f45 | 29 | #define __MODULE__ "peer.c" |
ashleymills | 0:04990d454f45 | 30 | #endif |
ashleymills | 0:04990d454f45 | 31 | |
ashleymills | 0:04990d454f45 | 32 | #include "peer.h" |
ashleymills | 0:04990d454f45 | 33 | #include "debug.h" |
ashleymills | 0:04990d454f45 | 34 | #include "dbg.h" |
ashleymills | 0:04990d454f45 | 35 | |
ashleymills | 0:04990d454f45 | 36 | #ifndef NDEBUG |
ashleymills | 0:04990d454f45 | 37 | #include <stdio.h> |
ashleymills | 0:04990d454f45 | 38 | |
ashleymills | 0:04990d454f45 | 39 | extern size_t dsrv_print_addr(const session_t *addr, unsigned char *buf, |
ashleymills | 0:04990d454f45 | 40 | size_t len); |
ashleymills | 0:04990d454f45 | 41 | #endif /* NDEBUG */ |
ashleymills | 0:04990d454f45 | 42 | |
ashleymills | 0:04990d454f45 | 43 | #ifndef WITH_CONTIKI |
ashleymills | 0:04990d454f45 | 44 | void peer_init() |
ashleymills | 0:04990d454f45 | 45 | { |
ashleymills | 0:04990d454f45 | 46 | } |
ashleymills | 0:04990d454f45 | 47 | |
ashleymills | 0:04990d454f45 | 48 | static inline dtls_peer_t * |
ashleymills | 0:04990d454f45 | 49 | dtls_malloc_peer() { |
ashleymills | 0:04990d454f45 | 50 | return (dtls_peer_t *)malloc(sizeof(dtls_peer_t)); |
ashleymills | 0:04990d454f45 | 51 | } |
ashleymills | 0:04990d454f45 | 52 | |
ashleymills | 0:04990d454f45 | 53 | void |
ashleymills | 0:04990d454f45 | 54 | dtls_free_peer(dtls_peer_t *peer) { |
ashleymills | 0:04990d454f45 | 55 | free(peer); |
ashleymills | 0:04990d454f45 | 56 | } |
ashleymills | 0:04990d454f45 | 57 | #else /* WITH_CONTIKI */ |
ashleymills | 0:04990d454f45 | 58 | PROCESS(dtls_retransmit_process, "DTLS retransmit process"); |
ashleymills | 0:04990d454f45 | 59 | |
ashleymills | 0:04990d454f45 | 60 | #include "memb.h" |
ashleymills | 0:04990d454f45 | 61 | MEMB(peer_storage, dtls_peer_t, DTLS_PEER_MAX); |
ashleymills | 0:04990d454f45 | 62 | |
ashleymills | 0:04990d454f45 | 63 | void |
ashleymills | 0:04990d454f45 | 64 | peer_init() { |
ashleymills | 0:04990d454f45 | 65 | memb_init(&peer_storage); |
ashleymills | 0:04990d454f45 | 66 | } |
ashleymills | 0:04990d454f45 | 67 | |
ashleymills | 0:04990d454f45 | 68 | static inline dtls_peer_t * |
ashleymills | 0:04990d454f45 | 69 | dtls_malloc_peer() { |
ashleymills | 0:04990d454f45 | 70 | return memb_alloc(&peer_storage); |
ashleymills | 0:04990d454f45 | 71 | } |
ashleymills | 0:04990d454f45 | 72 | |
ashleymills | 0:04990d454f45 | 73 | void |
ashleymills | 0:04990d454f45 | 74 | dtls_free_peer(dtls_peer_t *peer) { |
ashleymills | 0:04990d454f45 | 75 | memb_free(&peer_storage, peer); |
ashleymills | 0:04990d454f45 | 76 | } |
ashleymills | 0:04990d454f45 | 77 | #endif /* WITH_CONTIKI */ |
ashleymills | 0:04990d454f45 | 78 | |
ashleymills | 0:04990d454f45 | 79 | dtls_peer_t * |
ashleymills | 0:04990d454f45 | 80 | dtls_new_peer(const session_t *session) { |
ashleymills | 0:04990d454f45 | 81 | DBG("Entered dtls_new_peer"); |
ashleymills | 0:04990d454f45 | 82 | dtls_peer_t *peer; |
ashleymills | 0:04990d454f45 | 83 | |
ashleymills | 0:04990d454f45 | 84 | peer = dtls_malloc_peer(); |
ashleymills | 0:04990d454f45 | 85 | if (peer) { |
ashleymills | 0:04990d454f45 | 86 | memset(peer, 0, sizeof(dtls_peer_t)); |
ashleymills | 0:04990d454f45 | 87 | memcpy(&peer->session, session, sizeof(session_t)); |
ashleymills | 0:04990d454f45 | 88 | |
ashleymills | 0:04990d454f45 | 89 | #ifndef NDEBUG |
ashleymills | 0:04990d454f45 | 90 | if (dtls_get_log_level() >= LOG_DEBUG) { |
ashleymills | 0:04990d454f45 | 91 | unsigned char addrbuf[72]; |
ashleymills | 0:04990d454f45 | 92 | dsrv_print_addr(session, addrbuf, sizeof(addrbuf)); |
ashleymills | 0:04990d454f45 | 93 | DBG("Created peer for: \"%s\"", addrbuf); |
ashleymills | 0:04990d454f45 | 94 | } |
ashleymills | 0:04990d454f45 | 95 | #endif |
ashleymills | 0:04990d454f45 | 96 | /* initially allow the NULL cipher */ |
ashleymills | 0:04990d454f45 | 97 | CURRENT_CONFIG(peer)->cipher = TLS_NULL_WITH_NULL_NULL; |
ashleymills | 0:04990d454f45 | 98 | |
ashleymills | 0:04990d454f45 | 99 | /* initialize the handshake hash wrt. the hard-coded DTLS version */ |
ashleymills | 0:04990d454f45 | 100 | DBG("DTLSv12: initialize HASH_SHA256"); |
ashleymills | 0:04990d454f45 | 101 | /* TLS 1.2: PRF(secret, label, seed) = P_<hash>(secret, label + seed) */ |
ashleymills | 0:04990d454f45 | 102 | /* FIXME: we use the default SHA256 here, might need to support other |
ashleymills | 0:04990d454f45 | 103 | hash functions as well */ |
ashleymills | 0:04990d454f45 | 104 | dtls_hash_init(&peer->hs_state.hs_hash); |
ashleymills | 0:04990d454f45 | 105 | } |
ashleymills | 0:04990d454f45 | 106 | DBG("New peer created @ %u",peer); |
ashleymills | 0:04990d454f45 | 107 | |
ashleymills | 0:04990d454f45 | 108 | return peer; |
ashleymills | 0:04990d454f45 | 109 | } |
ashleymills | 0:04990d454f45 | 110 |