GNU libmicrohttpd  0.9.29
connection_https.c
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  Copyright (C) 2007, 2008, 2010 Daniel Pittman and Christian Grothoff
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 */
20 
29 #include "internal.h"
30 #include "connection.h"
31 #include "connection_https.h"
32 #include "memorypool.h"
33 #include "response.h"
34 #include "mhd_mono_clock.h"
35 #include <gnutls/gnutls.h>
36 
37 
46 static int
47 run_tls_handshake (struct MHD_Connection *connection)
48 {
49  int ret;
50 
51  if (MHD_TLS_CONNECTION_INIT == connection->state)
52  {
53  ret = gnutls_handshake (connection->tls_session);
54  if (ret == GNUTLS_E_SUCCESS)
55  {
56  /* set connection state to enable HTTP processing */
57  connection->state = MHD_CONNECTION_INIT;
58  MHD_update_last_activity_ (connection);
59  return MHD_NO;
60  }
61  if ( (GNUTLS_E_AGAIN == ret) ||
62  (GNUTLS_E_INTERRUPTED == ret) )
63  {
64  /* handshake not done */
65  return MHD_YES;
66  }
67  /* handshake failed */
68 #ifdef HAVE_MESSAGES
69  MHD_DLOG (connection->daemon,
70  _("Error: received handshake message out of context\n"));
71 #endif
72  MHD_connection_close_ (connection,
74  return MHD_YES;
75  }
76  return MHD_NO;
77 }
78 
79 
96 static int
98 {
99  if (MHD_YES == run_tls_handshake (connection))
100  return MHD_YES;
101  return MHD_connection_handle_read (connection);
102 }
103 
104 
113 static int
115 {
116  if (MHD_YES == run_tls_handshake (connection))
117  return MHD_YES;
118  return MHD_connection_handle_write (connection);
119 }
120 
121 
132 static int
134 {
135  time_t timeout;
136 
137 #if DEBUG_STATES
138  MHD_DLOG (connection->daemon,
139  _("In function %s handling connection at state: %s\n"),
140  __FUNCTION__,
141  MHD_state_to_string (connection->state));
142 #endif
143  if (connection->suspended)
144  return MHD_connection_handle_idle (connection);
145  switch (connection->state)
146  {
147  /* on newly created connections we might reach here before any reply has been received */
149  break;
150  /* close connection if necessary */
152  return MHD_connection_handle_idle (connection);
153  default:
154  return MHD_connection_handle_idle (connection);
155  }
156  timeout = connection->connection_timeout;
157  if ( (timeout != 0) &&
158  (timeout < (MHD_monotonic_sec_counter() - connection->last_activity)))
159  MHD_connection_close_ (connection,
161 #ifdef EPOLL_SUPPORT
162  return MHD_connection_epoll_update_ (connection);
163 #else
164  return MHD_YES;
165 #endif
166 }
167 
168 
175 void
177 {
181 }
182 
183 
190 int
192 {
193  if (connection->tls_closed)
194  return MHD_NO;
195 
196  connection->tls_closed = true;
197  return (GNUTLS_E_SUCCESS == gnutls_bye(connection->tls_session, GNUTLS_SHUT_WR)) ?
198  MHD_YES : MHD_NO;
199 }
200 
201 /* end of connection_https.c */
enum MHD_CONNECTION_STATE state
Definition: internal.h:890
int MHD_connection_handle_write(struct MHD_Connection *connection)
Definition: connection.c:2630
Methods for managing connections.
void MHD_connection_close_(struct MHD_Connection *connection, enum MHD_RequestTerminationCode termination_code)
Definition: connection.c:592
#define MHD_YES
Definition: microhttpd.h:134
Methods for managing response objects.
void MHD_update_last_activity_(struct MHD_Connection *connection)
Definition: connection.c:2522
struct MHD_Daemon * daemon
Definition: internal.h:641
int(* idle_handler)(struct MHD_Connection *connection)
Definition: internal.h:942
static int MHD_tls_connection_handle_write(struct MHD_Connection *connection)
int MHD_connection_handle_read(struct MHD_Connection *connection)
Definition: connection.c:2560
void MHD_set_https_callbacks(struct MHD_Connection *connection)
time_t connection_timeout
Definition: internal.h:829
Methods for managing connections.
static int MHD_tls_connection_handle_read(struct MHD_Connection *connection)
int(* read_handler)(struct MHD_Connection *connection)
Definition: internal.h:930
internal shared structures
int MHD_tls_connection_shutdown(struct MHD_Connection *connection)
internal monotonic clock functions implementations
int(* write_handler)(struct MHD_Connection *connection)
Definition: internal.h:936
static int run_tls_handshake(struct MHD_Connection *connection)
time_t last_activity
Definition: internal.h:823
int MHD_connection_handle_idle(struct MHD_Connection *connection)
Definition: connection.c:2895
time_t MHD_monotonic_sec_counter(void)
#define _(String)
Definition: mhd_options.h:42
bool suspended
Definition: internal.h:996
static int MHD_tls_connection_handle_idle(struct MHD_Connection *connection)
#define MHD_NO
Definition: microhttpd.h:139
memory pool; mostly used for efficient (de)allocation for each connection and bounding memory use for...