provide accurate value to select
[gnutls.git] / lib / gnutls_dtls.h
blobf8b1a721dcf700dd08713966348691864aceb98b
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 <timespec.h>
32 int _dtls_transmit(gnutls_session_t session);
33 int _dtls_retransmit(gnutls_session_t session);
34 int _dtls_record_check(struct record_parameters_st *rp, uint64 * _seq);
36 #define MAX_DTLS_TIMEOUT 60000
38 /* returns a-b in ms */
39 inline static unsigned int timespec_sub_ms(struct timespec *a, struct timespec *b)
41 return (a->tv_sec * 1000 + a->tv_nsec / (1000 * 1000) -
42 (b->tv_sec * 1000 + b->tv_nsec / (1000 * 1000)));
45 #define RETURN_DTLS_EAGAIN_OR_TIMEOUT(session, r) { \
46 struct timespec now; \
47 unsigned int diff; \
48 gettime(&now); \
50 diff = timespec_sub_ms(&now, &session->internals.dtls.handshake_start_time); \
51 if (diff > session->internals.dtls.total_timeout_ms) \
52 { \
53 _gnutls_dtls_log("Session timeout: %u ms\n", diff); \
54 return gnutls_assert_val(GNUTLS_E_TIMEDOUT); \
55 } \
56 else \
57 { \
58 int rr; \
59 if (r != GNUTLS_E_INTERRUPTED) rr = GNUTLS_E_AGAIN; \
60 else rr = r; \
61 if (session->internals.dtls.blocking != 0) \
62 millisleep(50); \
63 return gnutls_assert_val(rr); \
64 } \
68 int _dtls_wait_and_retransmit(gnutls_session_t session);
70 /* returns true or false depending on whether we need to
71 * handle asynchronously handshake data.
73 inline static int _dtls_is_async(gnutls_session_t session)
75 if ((session->security_parameters.entity == GNUTLS_SERVER && session->internals.resumed == RESUME_FALSE) ||
76 (session->security_parameters.entity == GNUTLS_CLIENT && session->internals.resumed == RESUME_TRUE))
77 return 1;
78 else
79 return 0;
82 inline static void _dtls_async_timer_init(gnutls_session_t session)
84 if (_dtls_is_async(session))
86 _gnutls_dtls_log ("DTLS[%p]: Initializing timer for handshake state.\n", session);
87 session->internals.dtls.async_term = gnutls_time(0) + MAX_DTLS_TIMEOUT/1000;
89 else
90 session->internals.dtls.async_term = 0;
93 inline static void _dtls_async_timer_delete(gnutls_session_t session)
95 if (session->internals.dtls.async_term != 0)
97 _gnutls_dtls_log ("DTLS[%p]: Deinitializing handshake state.\n", session);
98 _gnutls_handshake_io_buffer_clear (session);
99 session->internals.dtls.async_term = 0; /* turn off "timer" */
103 /* Checks whether it is time to terminate the timer
105 inline static void _dtls_async_timer_check(gnutls_session_t session)
107 if (!IS_DTLS(session))
108 return;
110 if (session->internals.dtls.async_term != 0)
112 time_t now = time(0);
114 /* check if we need to expire the queued handshake data */
115 if (now > session->internals.dtls.async_term)
117 _dtls_async_timer_delete(session);
122 /* Returns non-zero if the async timer is active */
123 inline static int _dtls_async_timer_active(gnutls_session_t session)
125 if (!IS_DTLS(session))
126 return 0;
128 return session->internals.dtls.async_term;
131 #endif