Permalink
Browse files

Add ssl echo server example to the example build system; remove dupli…

…cate
  • Loading branch information...
1 parent e18bade commit 3ae2773795fa1f110dd71fdaa2fd366b561189bc Nick Mathewson committed Aug 5, 2011
Showing with 25 additions and 139 deletions.
  1. +1 −0 .gitignore
  2. +2 −0 Makefile
  3. +2 −139 Ref6a_advanced_bufferevents.txt
  4. +20 −0 examples_R6a/Makefile
View
@@ -9,6 +9,7 @@
/examples_01/01_rot13_server_forking
/examples_01/01_sync_webclient
/examples_R6/R6_http_client
+/examples_R6a/R6a_ssl_server
/examples_R8/R8_echo_server
/examples_R9/R9_multilookup
/examples_R9/R9_dns_server
View
@@ -39,6 +39,7 @@ check: examples inline_examples
examples:
cd examples_01 && $(MAKE)
cd examples_R6 && $(MAKE)
+ cd examples_R6a && $(MAKE)
cd examples_R8 && $(MAKE)
cd examples_R9 && $(MAKE)
@@ -59,6 +60,7 @@ Ref3_eventloop.html: license.txt
Ref4_event.html: license.txt
Ref5_evutil.html: license.txt
Ref6_bufferevent.html: examples_R6/*.c license.txt
+Ref6a_advanced_bufferevent.html: examples_R6a/*.c license.txt
Ref7_evbuffer.html: license.txt
Ref8_listener.html: examples_R8/*.c license.txt
Ref9_dns.html: examples_R9/*.c license.txt
@@ -493,147 +493,10 @@ invoke appropriate callbacks. This is an advanced topic; you should
generally avoid it unless you really know what you're doing, especially since
many SSL versions have had known security issues related to renegotiation.
-.Example
+.Example: A simple SSL-based echo server
[code,C]
--------
-/* Simple echo server using OpenSSL bufferevents */
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
-#include <openssl/ssl.h>
-#include <openssl/err.h>
-#include <openssl/rand.h>
-
-#include <event.h>
-#include <event2/listener.h>
-#include <event2/bufferevent_ssl.h>
-
-static char * private_key =
- "-----BEGIN RSA PRIVATE KEY-----\n"
- "MIICXQIBAAKBgQC1XaK4b4MnyIzuDgphD1ySx60vgNuGU4ZAST4X23/Iz9yEwRBR\n"
- "b7CCfKbbRGFT0Y76SiWT/bWmTCpJcOxbrr+nr08YMN4vOweyHLN8UuFb/a054ONx\n"
- "W/YI2FzmXymn9ndulP53hlhTbiCbVXBMaF+JD6iiafSxoyk7fNp0yu8G4QIDAQAB\n"
- "AoGAOkzmQNl/1KsQOnoC9h5lCL3tOwb2CmERF9szfaHOmvPOlFInd7YqjFebn4KE\n"
- "stf7WRO6rq7w1ItJUKBfKj2rV6ZkyCsToPlSroRJhKz58UZrRxpzoAVNYf2qe6T8\n"
- "kn77l3PKDRS8aZvML/6IUBY+YLjA6VSAzWq6/RDlyt54zd0CQQDaUY7OqZjsLBmn\n"
- "VUEO+6etZPIkdafpVPIJ95gfibC+j4Y/yRKc9xGPnnOUAfDni639Vxj+iQgPUtPr\n"
- "HfkQPi7bAkEA1KtPtp0XWPv+crf1zNCw5QM0HUTkAyUEP/8LlzlQemMF+nc1m43s\n"
- "O8mSyo2j+zz7B2klg/DrQ10ufn1bqF638wJAOR4BWLwyUAexpn+9h6f7VHgGiddm\n"
- "WLtqn4Txfb7OSOzP2LxIbFyPcZ2o614eotV+bbttxJohS7EF1IuA7+j05QJBAL3B\n"
- "jYK3cFmpn0Pk+KEjpHLzBNEI4xobMUuY2lK4hSJusKrnKyH85EgEd8Hb1/EYXDQk\n"
- "kaDEmmalAhNC70GwIhkCQQCRN5geEhYzN+CaxVYPph99WetPYsTGN1qJPbpK+5n1\n"
- "p8dYihTLZ/c1QREQbGXcDgaQdTMvckc0PuO/Hhl+reLh\n"
- "-----END RSA PRIVATE KEY-----\n";
-
-static char * certificate =
- "-----BEGIN CERTIFICATE-----\n"
- "MIIDeDCCAuGgAwIBAgIJAKef6j1Xr5X0MA0GCSqGSIb3DQEBBQUAMIGFMQswCQYD\n"
- "VQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQg\n"
- "V2lkZ2l0cyBQdHkgTHRkMQ4wDAYDVQQLEwVTaW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Bc9Tx\n"
- "hZ4Y0qGytoq1Hl61WK5ebiUXtrzUeX37tP0kZw==\n"
- "-----END CERTIFICATE-----";
-
-
-static void
-ssl_readcb(struct bufferevent * bev, void * arg) {
- struct evbuffer * in = bufferevent_get_input(bev);
-
- printf("Recvd %zu bytes\n", evbuffer_get_length(in));
- printf("----- data ----\n");
- printf("%.*s\n", (int)evbuffer_get_length(in), evbuffer_pullup(in, -1));
-
- bufferevent_write_buffer(bev, in);
-}
-
-static void
-ssl_acceptcb(struct evconnlistener * serv, int sock, struct sockaddr * sa, int sa_len, void * arg) {
- struct event_base * evbase;
- struct bufferevent * bev;
- SSL_CTX * server_ctx;
- SSL * client_ctx;
-
- server_ctx = (SSL_CTX *)arg;
- client_ctx = SSL_new(server_ctx);
- evbase = evconnlistener_get_base(serv);
-
- bev = bufferevent_openssl_socket_new(evbase, sock, client_ctx,
- BUFFEREVENT_SSL_ACCEPTING,
- BEV_OPT_CLOSE_ON_FREE);
-
- bufferevent_enable(bev, EV_READ);
- bufferevent_setcb(bev, ssl_readcb, NULL, NULL, NULL);
-}
-
-static SSL_CTX *
-evssl_init(void) {
- EVP_PKEY * pkey;
- X509 * cx509;
- BIO * pkey_bio;
- BIO * cert_bio;
- SSL_CTX * server_ctx;
-
- SSL_load_error_strings();
- SSL_library_init();
- RAND_status();
-
- pkey_bio = BIO_new_mem_buf(private_key, -1);
- cert_bio = BIO_new_mem_buf(certificate, -1);
- pkey = PEM_read_bio_PrivateKey(pkey_bio, NULL, NULL, NULL);
- cx509 = PEM_read_bio_X509(cert_bio, NULL, NULL, NULL);
-
- server_ctx = SSL_CTX_new(SSLv23_server_method());
-
- SSL_CTX_use_certificate(server_ctx, cx509);
- SSL_CTX_use_PrivateKey(server_ctx, pkey);
- SSL_CTX_set_options(server_ctx, SSL_OP_NO_SSLv2);
- SSL_CTX_set_cipher_list(server_ctx, "RC4+RSA:HIGH:+MEDIUM:+LOW");
- SSL_CTX_set_session_cache_mode(server_ctx, SSL_SESS_CACHE_OFF);
-
- return server_ctx;
-}
-
-int
-main(int argc, char ** argv) {
- SSL_CTX * ctx;
- struct evconnlistener * listener;
- struct event_base * evbase;
- struct sockaddr_in sin;
-
-
- memset(&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- sin.sin_port = htons(9999);
- sin.sin_addr.s_addr = inet_addr("127.0.0.1");
-
- ctx = evssl_init();
- evbase = event_base_new();
- listener = evconnlistener_new_bind(evbase, ssl_acceptcb, (void *)ctx,
- LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, 1024,
- (struct sockaddr *)&sin, sizeof(sin));
-
- event_base_loop(evbase, 0);
-
- return 0;
-}
+include::examples_R6a/R6a_ssl_server.c[]
--------
View
@@ -0,0 +1,20 @@
+
+CC=gcc
+CFLAGS=-g -Wall $(LEBOOK_CFLAGS)
+
+EXAMPLE_BINARIES=R6a_ssl_server
+
+all: examples
+
+examples: $(EXAMPLE_BINARIES)
+
+R6a_ssl_server: R6a_ssl_server.o
+ $(CC) $(CFLAGS) R6a_ssl_server.o -o R6a_ssl_server -levent -levent_openssl -lssl -lcrypto
+
+.c.o:
+ $(CC) $(CFLAGS) -c $<
+
+clean:
+ rm -f *~
+ rm -f *.o
+ rm -f $(EXAMPLE_BINARIES)

0 comments on commit 3ae2773

Please sign in to comment.