aboutsummaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorlv-426 <oxcafebaby@yahoo.com>2008-05-24 13:59:21 +0000
committerlv-426 <oxcafebaby@yahoo.com>2008-05-24 13:59:21 +0000
commitc51d65ffce7b0542a15afa4d7bb252df6a970b5c (patch)
tree82dcb0f2fb0f3e9b115e77bae74c42a1186b39f5 /src/examples
parenta0cc83b7c2325dfceec0851c2046148059c84619 (diff)
downloadlibmicrohttpd-c51d65ffce7b0542a15afa4d7bb252df6a970b5c.tar.gz
libmicrohttpd-c51d65ffce7b0542a15afa4d7bb252df6a970b5c.zip
added :
* configure.ac support for --with-gnutls=PFX * TLS connection handler in daemon.c * simple TLS echo client server example
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/Makefile.am32
-rw-r--r--src/examples/https_echo_client_example.c153
-rw-r--r--src/examples/https_server_example.c182
3 files changed, 364 insertions, 3 deletions
diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am
index 3d693b23..340f7824 100644
--- a/src/examples/Makefile.am
+++ b/src/examples/Makefile.am
@@ -1,10 +1,16 @@
1SUBDIRS = . 1SUBDIRS = .
2 2
3INCLUDES = -I$(top_srcdir)/src/include 3AM_CPPFLAGS = -I$(top_srcdir)/src/include
4 4
5# example programs 5# example programs
6 6
7noinst_PROGRAMS = minimal_example querystring_example fileserver_example fileserver_example_external_select 7noinst_PROGRAMS = \
8https_server_example \
9https_echo_client_example \
10minimal_example \
11querystring_example \
12fileserver_example \
13fileserver_example_external_select
8 14
9minimal_example_SOURCES = \ 15minimal_example_SOURCES = \
10 minimal_example.c 16 minimal_example.c
@@ -21,9 +27,29 @@ fileserver_example_SOURCES = \
21fileserver_example_LDADD = \ 27fileserver_example_LDADD = \
22 $(top_builddir)/src/daemon/libmicrohttpd.la 28 $(top_builddir)/src/daemon/libmicrohttpd.la
23 29
24
25fileserver_example_external_select_SOURCES = \ 30fileserver_example_external_select_SOURCES = \
26 fileserver_example_external_select.c 31 fileserver_example_external_select.c
27fileserver_example_external_select_LDADD = \ 32fileserver_example_external_select_LDADD = \
28 $(top_builddir)/src/daemon/libmicrohttpd.la 33 $(top_builddir)/src/daemon/libmicrohttpd.la
29 34
35https_server_example_CPPFLAGS = \
36 $(GNUTLS_CPPFLAGS) \
37 -I$(top_srcdir)/src/daemon
38https_server_example_SOURCES = \
39 https_server_example.c
40https_server_example_LDADD = \
41 $(top_builddir)/src/daemon/libmicrohttpd.la
42https_server_example_LDFLAGS = \
43 -L$(GNUTLS_LIB_PATH) \
44 -lgnutls
45
46https_echo_client_example_CPPFLAGS = \
47 $(GNUTLS_CPPFLAGS) \
48 -I$(top_srcdir)/src/daemon
49https_echo_client_example_SOURCES = \
50 https_echo_client_example.c
51https_echo_client_example_LDADD = \
52 $(top_builddir)/src/daemon/libmicrohttpd.la
53https_echo_client_example_LDFLAGS = \
54 -L$(GNUTLS_LIB_PATH) \
55 -lgnutls
diff --git a/src/examples/https_echo_client_example.c b/src/examples/https_echo_client_example.c
new file mode 100644
index 00000000..04e26c74
--- /dev/null
+++ b/src/examples/https_echo_client_example.c
@@ -0,0 +1,153 @@
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_echo_client.c
23 * @brief a simple echo client to use in conjuction with the echo TLS server.
24 * @author LV-426
25 */
26
27#if HAVE_CONFIG_H
28# include <config.h>
29#endif
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <sys/types.h>
35#include <sys/socket.h>
36#include <arpa/inet.h>
37#include <unistd.h>
38#include <gnutls/gnutls.h>
39
40#define MAX_BUF 1024
41#define SA struct sockaddr
42#define MSG "GET / HTTP/1.0\r\n\r\n"
43
44extern int tcp_connect (void);
45extern void tcp_close (int sd);
46
47int
48main (int argc, char **argv)
49{
50 int ret, sd, ii, err;
51 gnutls_session_t session;
52 char buffer[MAX_BUF + 1];
53 gnutls_anon_client_credentials_t anoncred;
54
55 struct sockaddr_in servaddr4;
56 const struct sockaddr *servaddr;
57 struct sockaddr_in sa;
58 socklen_t addrlen;
59
60 if (argc < 2)
61 {
62 printf ("Usage : %s SERVER-PORT\n", argv[0]);
63 return 1;
64 }
65
66 gnutls_global_init ();
67
68 gnutls_anon_allocate_client_credentials (&anoncred);
69
70 /* Initialize TLS session */
71 gnutls_init (&session, GNUTLS_CLIENT);
72
73 /* Use default priorities */
74 gnutls_priority_set_direct (session, "PERFORMANCE:+ANON-DH:!ARCFOUR-128",
75 NULL);
76
77 /* put the anonymous credentials to the current session */
78 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
79
80 sd = socket (AF_INET, SOCK_STREAM, 0);
81 memset (&sa, '\0', sizeof (sa));
82 sa.sin_family = AF_INET;
83 sa.sin_port = htons (atoi (argv[1]));
84 inet_pton (AF_INET, "127.0.0.1", &sa.sin_addr);
85
86 /* connect to the peer */
87 err = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
88 if (err < 0)
89 {
90 fprintf (stderr, "Connect error\n");
91 exit (1);
92 }
93
94 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) sd);
95
96 /* Perform the TLS handshake */
97 ret = gnutls_handshake (session);
98
99 if (ret < 0)
100 {
101 fprintf (stderr, "*** Handshake failed\n");
102 gnutls_perror (ret);
103 goto end;
104 }
105 else
106 {
107 printf ("- Handshake was completed\n");
108 }
109
110 for (;;)
111 {
112 /**/ scanf ("%s", buffer);
113
114 if (strcmp (buffer, "exit") == 0)
115 {
116 gnutls_record_send (session, buffer, strlen (MSG));
117 break;
118 }
119 gnutls_record_send (session, buffer, strlen (MSG));
120
121 ret = gnutls_record_recv (session, buffer, MAX_BUF);
122 if (ret == 0)
123 {
124 printf ("- Peer has closed the TLS connection\n");
125 goto end;
126 }
127 else if (ret < 0)
128 {
129 fprintf (stderr, "*** Error: %s\n", gnutls_strerror (ret));
130 break;
131 }
132
133 printf ("- Received %d bytes: ", ret);
134 for (ii = 0; ii < ret; ii++)
135 {
136 fputc (buffer[ii], stdout);
137 }
138 fputs ("\n", stdout);
139 }
140
141end:
142
143 shutdown (sd, SHUT_RDWR);
144 close (sd);
145
146 gnutls_deinit (session);
147
148 gnutls_anon_free_client_credentials (anoncred);
149
150 gnutls_global_deinit ();
151
152 return 0;
153}
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 */
42gnutls_anon_server_credentials_t anoncred;
43
44/* server Diffie-Hellman parameters */
45static gnutls_dh_params_t dh_params;
46
47
48/* Generate Diffie Hellman parameters - for use with DHE kx algorithms. */
49static int
50generate_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
58gnutls_session_t
59initialize_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 */
75static int
76TLS_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
146int
147main (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}