moved ca-certs.
[gnutls.git] / lib / gnutls_dtls.h
blobcd15f89643a2089139104dbd346889f79fc03bef
1 /*
2 * Copyright (C) 2009-2012 Free Software Foundation, Inc.
4 * Author: Jonathan Bastien-Filiatrault
6 * This file is part of GNUTLS.
8 * The GNUTLS library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #ifndef DTLS_H
24 # define DTLS_H
26 #include <config.h>
27 #include <gnutls_int.h>
28 #include <gnutls_buffers.h>
29 #include <gnutls_mbuffers.h>
30 #include <gnutls_constate.h>
31 #include <timespec.h>
33 int _dtls_transmit(gnutls_session_t session);
34 int _dtls_retransmit(gnutls_session_t session);
35 int _dtls_record_check(struct record_parameters_st *rp, uint64 * _seq);
36 void _dtls_reset_hsk_state(gnutls_session_t session);
38 #define MAX_DTLS_TIMEOUT 60000
40 unsigned int _dtls_timespec_sub_ms (struct timespec *a, struct timespec *b);
42 #define RETURN_DTLS_EAGAIN_OR_TIMEOUT(session, r) { \
43 struct timespec now; \
44 unsigned int diff; \
45 gettime(&now); \
47 diff = _dtls_timespec_sub_ms(&now, &session->internals.dtls.handshake_start_time); \
48 if (diff > session->internals.dtls.total_timeout_ms) \
49 { \
50 _gnutls_dtls_log("Session timeout: %u ms\n", diff); \
51 return gnutls_assert_val(GNUTLS_E_TIMEDOUT); \
52 } \
53 else \
54 { \
55 int rr; \
56 if (r != GNUTLS_E_INTERRUPTED) rr = GNUTLS_E_AGAIN; \
57 else rr = r; \
58 if (session->internals.dtls.blocking != 0) \
59 millisleep(50); \
60 return gnutls_assert_val(rr); \
61 } \
65 int _dtls_wait_and_retransmit(gnutls_session_t session);
67 /* returns true or false depending on whether we need to
68 * handle asynchronously handshake data.
70 inline static int _dtls_is_async(gnutls_session_t session)
72 if ((session->security_parameters.entity == GNUTLS_SERVER && session->internals.resumed == RESUME_FALSE) ||
73 (session->security_parameters.entity == GNUTLS_CLIENT && session->internals.resumed == RESUME_TRUE))
74 return 1;
75 else
76 return 0;
79 inline static void _dtls_async_timer_init(gnutls_session_t session)
81 if (_dtls_is_async(session))
83 _gnutls_dtls_log ("DTLS[%p]: Initializing timer for handshake state.\n", session);
84 session->internals.dtls.async_term = gnutls_time(0) + MAX_DTLS_TIMEOUT/1000;
86 else
88 _dtls_reset_hsk_state(session);
89 _gnutls_handshake_io_buffer_clear (session);
90 _gnutls_epoch_gc(session);
91 session->internals.dtls.async_term = 0;
95 void _dtls_async_timer_delete(gnutls_session_t session);
97 /* Checks whether it is time to terminate the timer
99 inline static void _dtls_async_timer_check(gnutls_session_t session)
101 if (!IS_DTLS(session))
102 return;
104 if (session->internals.dtls.async_term != 0)
106 time_t now = time(0);
108 /* check if we need to expire the queued handshake data */
109 if (now > session->internals.dtls.async_term)
111 _dtls_async_timer_delete(session);
116 /* Returns non-zero if the async timer is active */
117 inline static int _dtls_async_timer_active(gnutls_session_t session)
119 if (!IS_DTLS(session))
120 return 0;
122 return session->internals.dtls.async_term;
125 #endif