diff options
Diffstat (limited to 'src/examples/https_server_example.c')
-rw-r--r-- | src/examples/https_server_example.c | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/src/examples/https_server_example.c b/src/examples/https_server_example.c new file mode 100644 index 00000000..709f8502 --- /dev/null +++ b/src/examples/https_server_example.c | |||
@@ -0,0 +1,182 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | (C) 2007 Christian Grothoff | ||
4 | |||
5 | libmicrohttpd is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | libmicrohttpd is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with libmicrohttpd; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file https_server_example.c | ||
23 | * @brief a simple echo server using TLS. echo input from client until 'exit' message is received. | ||
24 | * @author LV-426 | ||
25 | */ | ||
26 | |||
27 | #include "config.h" | ||
28 | #include <microhttpd.h> | ||
29 | #include "internal.h" | ||
30 | |||
31 | #include <stdlib.h> | ||
32 | #ifndef MINGW | ||
33 | #include <unistd.h> | ||
34 | #endif | ||
35 | #include <string.h> | ||
36 | #include <stdio.h> | ||
37 | #include <gnutls/gnutls.h> | ||
38 | |||
39 | #define DH_BITS 1024 | ||
40 | #define MAX_BUF 1024 | ||
41 | /* server credintials */ | ||
42 | gnutls_anon_server_credentials_t anoncred; | ||
43 | |||
44 | /* server Diffie-Hellman parameters */ | ||
45 | static gnutls_dh_params_t dh_params; | ||
46 | |||
47 | |||
48 | /* Generate Diffie Hellman parameters - for use with DHE kx algorithms. */ | ||
49 | static int | ||
50 | generate_dh_params (void) | ||
51 | { | ||
52 | |||
53 | gnutls_dh_params_init (&dh_params); | ||
54 | gnutls_dh_params_generate2 (dh_params, DH_BITS); | ||
55 | return 0; | ||
56 | } | ||
57 | |||
58 | gnutls_session_t | ||
59 | initialize_tls_session (void) | ||
60 | { | ||
61 | gnutls_session_t session; | ||
62 | |||
63 | gnutls_init (&session, GNUTLS_SERVER); | ||
64 | |||
65 | gnutls_priority_set_direct (session, "NORMAL:+ANON-DH", NULL); | ||
66 | |||
67 | gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred); | ||
68 | |||
69 | gnutls_dh_set_prime_bits (session, DH_BITS); | ||
70 | |||
71 | return session; | ||
72 | } | ||
73 | |||
74 | /* Accept Policy Callback */ | ||
75 | static int | ||
76 | TLS_echo (void *cls, | ||
77 | struct MHD_Connection *connection, | ||
78 | const char *url, | ||
79 | const char *method, | ||
80 | const char *upload_data, | ||
81 | const char *version, unsigned int *upload_data_size, void **ptr) | ||
82 | { | ||
83 | gnutls_session_t session; | ||
84 | static int aptr; | ||
85 | struct MHD_Response *response; | ||
86 | char buffer[MAX_BUF + 1]; | ||
87 | int ret; | ||
88 | |||
89 | printf ("accepted connection from %d\n", connection->addr->sin_addr); | ||
90 | |||
91 | session = initialize_tls_session (); | ||
92 | |||
93 | gnutls_transport_set_ptr (session, connection->socket_fd); | ||
94 | |||
95 | ret = gnutls_handshake (session); | ||
96 | if (ret < 0) | ||
97 | { | ||
98 | /* set connection as closed */ | ||
99 | connection->socket_fd = 1; | ||
100 | gnutls_deinit (session); | ||
101 | fprintf (stderr, "*** Handshake has failed (%s)\n\n", | ||
102 | gnutls_strerror (ret)); | ||
103 | return MHD_NO; | ||
104 | } | ||
105 | |||
106 | printf ("TLS Handshake completed\n"); | ||
107 | connection->state = MHDS_HANDSHAKE_COMPLETE; | ||
108 | |||
109 | /* simple echo loop. message encryption/decryption is acheived through 'gnutls_record_send' | ||
110 | * & gnutls_record_recv calls. */ | ||
111 | for (;;) | ||
112 | { | ||
113 | memset (buffer, 0, MAX_BUF + 1); | ||
114 | ret = gnutls_record_recv (session, buffer, MAX_BUF); | ||
115 | |||
116 | if (ret < 0) | ||
117 | { | ||
118 | fprintf (stderr, "\n*** Received corrupted " | ||
119 | "data(%d). Closing the connection.\n\n", ret); | ||
120 | break; | ||
121 | } | ||
122 | else if (ret >= 0) | ||
123 | { | ||
124 | if (strcmp (buffer, "exit") == 0) | ||
125 | { | ||
126 | printf ("\n- Peer has closed the GNUTLS connection\n"); | ||
127 | break; | ||
128 | } | ||
129 | else | ||
130 | { | ||
131 | /* echo data back to the client */ | ||
132 | gnutls_record_send (session, buffer, strlen (buffer)); | ||
133 | } | ||
134 | } | ||
135 | } | ||
136 | printf ("\n"); | ||
137 | |||
138 | /* mark connection as closed */ | ||
139 | connection->socket_fd = -1; | ||
140 | |||
141 | gnutls_deinit (session); | ||
142 | |||
143 | return ret; | ||
144 | } | ||
145 | |||
146 | int | ||
147 | main (int argc, char *const *argv) | ||
148 | { | ||
149 | struct MHD_Daemon *daemon; | ||
150 | struct MHD_Daemon *TLS_daemon; | ||
151 | |||
152 | /* look for HTTPS port argument */ | ||
153 | if (argc < 4) | ||
154 | { | ||
155 | printf ("Usage : %s HTTP-PORT SECONDS-TO-RUN HTTPS-PORT\n", argv[0]); | ||
156 | return 1; | ||
157 | } | ||
158 | |||
159 | gnutls_global_init (); | ||
160 | |||
161 | gnutls_anon_allocate_server_credentials (&anoncred); | ||
162 | |||
163 | generate_dh_params (); | ||
164 | |||
165 | gnutls_anon_set_server_dh_params (anoncred, dh_params); | ||
166 | |||
167 | TLS_daemon = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | ||
168 | | MHD_USE_DEBUG | MHD_USE_SSL, | ||
169 | atoi (argv[3]), NULL, NULL, &TLS_echo, NULL, | ||
170 | MHD_OPTION_END); | ||
171 | |||
172 | if (TLS_daemon == NULL) | ||
173 | return 1; | ||
174 | sleep (atoi (argv[2])); | ||
175 | |||
176 | MHD_stop_daemon (daemon); | ||
177 | |||
178 | gnutls_anon_free_server_credentials (anoncred); | ||
179 | |||
180 | gnutls_global_deinit (); | ||
181 | return 0; | ||
182 | } | ||