aboutsummaryrefslogtreecommitdiff
path: root/sample
diff options
context:
space:
mode:
Diffstat (limited to 'sample')
-rw-r--r--sample/dns-example.c17
-rw-r--r--sample/event-read-fifo.c4
-rw-r--r--sample/hello-world.c3
-rw-r--r--sample/http-connect.c86
-rw-r--r--sample/http-server.c292
-rw-r--r--sample/https-client.c88
-rw-r--r--sample/include.am11
-rw-r--r--sample/le-proxy.c16
-rw-r--r--sample/openssl_hostname_validation.c3
-rw-r--r--sample/signal-test.c25
-rw-r--r--sample/time-test.c7
11 files changed, 405 insertions, 147 deletions
diff --git a/sample/dns-example.c b/sample/dns-example.c
index fb705664aa81..2d07c3874bca 100644
--- a/sample/dns-example.c
+++ b/sample/dns-example.c
@@ -78,6 +78,8 @@ gai_callback(int err, struct evutil_addrinfo *ai, void *arg)
{
const char *name = arg;
int i;
+ struct evutil_addrinfo *first_ai = ai;
+
if (err) {
printf("%s: %s\n", name, evutil_gai_strerror(err));
}
@@ -99,6 +101,9 @@ gai_callback(int err, struct evutil_addrinfo *ai, void *arg)
printf("[%d] %s: %s\n",i,name,buf);
}
}
+
+ if (first_ai)
+ evutil_freeaddrinfo(first_ai);
}
static void
@@ -154,19 +159,19 @@ main(int c, char **v) {
const char *ns;
};
struct options o;
- char opt;
+ int opt;
struct event_base *event_base = NULL;
struct evdns_base *evdns_base = NULL;
memset(&o, 0, sizeof(o));
-
+
if (c < 2) {
fprintf(stderr, "syntax: %s [-x] [-v] [-c resolv.conf] [-s ns] hostname\n", v[0]);
fprintf(stderr, "syntax: %s [-T]\n", v[0]);
return 1;
}
- while ((opt = getopt(c, v, "xvc:Ts:")) != -1) {
+ while ((opt = getopt(c, v, "xvc:Ts:g")) != -1) {
switch (opt) {
case 'x': o.reverse = 1; break;
case 'v': ++verbose; break;
@@ -220,8 +225,8 @@ main(int c, char **v) {
res = evdns_base_resolv_conf_parse(evdns_base,
DNS_OPTION_NAMESERVERS, o.resolv_conf);
- if (res < 0) {
- fprintf(stderr, "Couldn't configure nameservers");
+ if (res) {
+ fprintf(stderr, "Couldn't configure nameservers\n");
return 1;
}
}
@@ -252,6 +257,8 @@ main(int c, char **v) {
}
fflush(stdout);
event_base_dispatch(event_base);
+ evdns_base_free(evdns_base, 1);
+ event_base_free(event_base);
return 0;
}
diff --git a/sample/event-read-fifo.c b/sample/event-read-fifo.c
index 27b0b530d5c1..a17b9bd9ae45 100644
--- a/sample/event-read-fifo.c
+++ b/sample/event-read-fifo.c
@@ -129,10 +129,10 @@ main(int argc, char **argv)
fprintf(stderr, "Write data to %s\n", fifo);
#endif
- /* Initalize the event library */
+ /* Initialize the event library */
base = event_base_new();
- /* Initalize one event */
+ /* Initialize one event */
#ifdef _WIN32
evfifo = event_new(base, (evutil_socket_t)socket, EV_READ|EV_PERSIST, fifo_read,
event_self_cbarg());
diff --git a/sample/hello-world.c b/sample/hello-world.c
index 2023cd6c6aa6..a13e06af612d 100644
--- a/sample/hello-world.c
+++ b/sample/hello-world.c
@@ -42,7 +42,7 @@ main(int argc, char **argv)
struct evconnlistener *listener;
struct event *signal_event;
- struct sockaddr_in sin;
+ struct sockaddr_in sin = {0};
#ifdef _WIN32
WSADATA wsa_data;
WSAStartup(0x0201, &wsa_data);
@@ -54,7 +54,6 @@ main(int argc, char **argv)
return 1;
}
- memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
diff --git a/sample/http-connect.c b/sample/http-connect.c
index a44d001ac71f..53f816d3ae59 100644
--- a/sample/http-connect.c
+++ b/sample/http-connect.c
@@ -11,6 +11,7 @@
#define VERIFY(cond) do { \
if (!(cond)) { \
fprintf(stderr, "[error] %s\n", #cond); \
+ exit(EXIT_FAILURE); \
} \
} while (0); \
@@ -22,6 +23,38 @@ struct connect_base
struct evhttp_uri *location;
};
+static struct evhttp_uri* uri_parse(const char *str)
+{
+ struct evhttp_uri *uri;
+ VERIFY(uri = evhttp_uri_parse(str));
+ VERIFY(evhttp_uri_get_host(uri));
+ VERIFY(evhttp_uri_get_port(uri) > 0);
+ return uri;
+}
+static char* uri_path(struct evhttp_uri *uri, char buffer[URL_MAX])
+{
+ struct evhttp_uri *path;
+
+ VERIFY(evhttp_uri_join(uri, buffer, URL_MAX));
+
+ path = evhttp_uri_parse(buffer);
+ evhttp_uri_set_scheme(path, NULL);
+ evhttp_uri_set_userinfo(path, 0);
+ evhttp_uri_set_host(path, NULL);
+ evhttp_uri_set_port(path, -1);
+ VERIFY(evhttp_uri_join(path, buffer, URL_MAX));
+ return buffer;
+}
+static char* uri_hostport(struct evhttp_uri *uri, char buffer[URL_MAX])
+{
+ VERIFY(evhttp_uri_join(uri, buffer, URL_MAX));
+ VERIFY(evhttp_uri_get_host(uri));
+ VERIFY(evhttp_uri_get_port(uri) > 0);
+ evutil_snprintf(buffer, URL_MAX, "%s:%d",
+ evhttp_uri_get_host(uri), evhttp_uri_get_port(uri));
+ return buffer;
+}
+
static void get_cb(struct evhttp_request *req, void *arg)
{
ev_ssize_t len;
@@ -37,26 +70,26 @@ static void get_cb(struct evhttp_request *req, void *arg)
static void connect_cb(struct evhttp_request *proxy_req, void *arg)
{
- char buffer[URL_MAX];
-
struct connect_base *base = arg;
struct evhttp_connection *evcon = base->evcon;
struct evhttp_uri *location = base->location;
+ struct evhttp_request *req;
+ char buffer[URL_MAX];
VERIFY(proxy_req);
- if (evcon) {
- struct evhttp_request *req = evhttp_request_new(get_cb, NULL);
- evhttp_add_header(req->output_headers, "Connection", "close");
- VERIFY(!evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
- evhttp_uri_join(location, buffer, URL_MAX)));
- }
+ VERIFY(evcon);
+
+ req = evhttp_request_new(get_cb, NULL);
+ evhttp_add_header(req->output_headers, "Connection", "close");
+ evhttp_add_header(req->output_headers, "Host", evhttp_uri_get_host(location));
+ VERIFY(!evhttp_make_request(evcon, req, EVHTTP_REQ_GET,
+ uri_path(location, buffer)));
}
int main(int argc, const char **argv)
{
- char buffer[URL_MAX];
+ char hostport[URL_MAX];
- struct evhttp_uri *host_port;
struct evhttp_uri *location;
struct evhttp_uri *proxy;
@@ -71,28 +104,8 @@ int main(int argc, const char **argv)
return 1;
}
- {
- proxy = evhttp_uri_parse(argv[1]);
- VERIFY(evhttp_uri_get_host(proxy));
- VERIFY(evhttp_uri_get_port(proxy) > 0);
- }
- {
- host_port = evhttp_uri_parse(argv[2]);
- evhttp_uri_set_scheme(host_port, NULL);
- evhttp_uri_set_userinfo(host_port, NULL);
- evhttp_uri_set_path(host_port, NULL);
- evhttp_uri_set_query(host_port, NULL);
- evhttp_uri_set_fragment(host_port, NULL);
- VERIFY(evhttp_uri_get_host(host_port));
- VERIFY(evhttp_uri_get_port(host_port) > 0);
- }
- {
- location = evhttp_uri_parse(argv[2]);
- evhttp_uri_set_scheme(location, NULL);
- evhttp_uri_set_userinfo(location, 0);
- evhttp_uri_set_host(location, NULL);
- evhttp_uri_set_port(location, -1);
- }
+ proxy = uri_parse(argv[1]);
+ location = uri_parse(argv[2]);
VERIFY(base = event_base_new());
VERIFY(evcon = evhttp_connection_base_new(base, NULL,
@@ -101,17 +114,18 @@ int main(int argc, const char **argv)
connect_base.location = location;
VERIFY(req = evhttp_request_new(connect_cb, &connect_base));
+ uri_hostport(location, hostport);
evhttp_add_header(req->output_headers, "Connection", "keep-alive");
evhttp_add_header(req->output_headers, "Proxy-Connection", "keep-alive");
- evutil_snprintf(buffer, URL_MAX, "%s:%d",
- evhttp_uri_get_host(host_port), evhttp_uri_get_port(host_port));
- evhttp_make_request(evcon, req, EVHTTP_REQ_CONNECT, buffer);
+ evhttp_add_header(req->output_headers, "Host", hostport);
+ evhttp_make_request(evcon, req, EVHTTP_REQ_CONNECT, hostport);
event_base_dispatch(base);
+
evhttp_connection_free(evcon);
event_base_free(base);
evhttp_uri_free(proxy);
- evhttp_uri_free(host_port);
evhttp_uri_free(location);
+
return 0;
}
diff --git a/sample/http-server.c b/sample/http-server.c
index 579feea6e3e1..049aabc4418e 100644
--- a/sample/http-server.c
+++ b/sample/http-server.c
@@ -20,26 +20,39 @@
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
+#include <getopt.h>
#include <io.h>
#include <fcntl.h>
#ifndef S_ISDIR
#define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
#endif
-#else
+#else /* !_WIN32 */
#include <sys/stat.h>
#include <sys/socket.h>
-#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
+#endif /* _WIN32 */
+#include <signal.h>
+
+#ifdef EVENT__HAVE_SYS_UN_H
+#include <sys/un.h>
+#endif
+#ifdef EVENT__HAVE_AFUNIX_H
+#include <afunix.h>
#endif
#include <event2/event.h>
#include <event2/http.h>
+#include <event2/listener.h>
#include <event2/buffer.h>
#include <event2/util.h>
#include <event2/keyvalq_struct.h>
+#ifdef _WIN32
+#include <event2/thread.h>
+#endif /* _WIN32 */
+
#ifdef EVENT__HAVE_NETINET_IN_H
#include <netinet/in.h>
# ifdef _XOPEN_SOURCE_EXTENDED
@@ -63,7 +76,7 @@
#ifndef O_RDONLY
#define O_RDONLY _O_RDONLY
#endif
-#endif
+#endif /* _WIN32 */
char uri_root[512];
@@ -86,6 +99,16 @@ static const struct table_entry {
{ NULL, NULL },
};
+struct options {
+ int port;
+ int iocp;
+ int verbose;
+
+ int unlink;
+ const char *unixsock;
+ const char *docroot;
+};
+
/* Try to guess a good content-type for 'path' */
static const char *
guess_content_type(const char *path)
@@ -159,7 +182,7 @@ static void
send_document_cb(struct evhttp_request *req, void *arg)
{
struct evbuffer *evb = NULL;
- const char *docroot = arg;
+ struct options *o = arg;
const char *uri = evhttp_request_get_uri(req);
struct evhttp_uri *decoded = NULL;
const char *path;
@@ -199,12 +222,12 @@ send_document_cb(struct evhttp_request *req, void *arg)
if (strstr(decoded_path, ".."))
goto err;
- len = strlen(decoded_path)+strlen(docroot)+2;
+ len = strlen(decoded_path)+strlen(o->docroot)+2;
if (!(whole_path = malloc(len))) {
perror("malloc");
goto err;
}
- evutil_snprintf(whole_path, len, "%s/%s", docroot, decoded_path);
+ evutil_snprintf(whole_path, len, "%s/%s", o->docroot, decoded_path);
if (stat(whole_path, &st)<0) {
goto err;
@@ -321,42 +344,161 @@ done:
}
static void
-syntax(void)
+print_usage(FILE *out, const char *prog, int exit_code)
+{
+ fprintf(out,
+ "Syntax: %s [ OPTS ] <docroot>\n"
+ " -p - port\n"
+ " -U - bind to unix socket\n"
+ " -u - unlink unix socket before bind\n"
+ " -I - IOCP\n"
+ " -v - verbosity, enables libevent debug logging too\n", prog);
+ exit(exit_code);
+}
+static struct options
+parse_opts(int argc, char **argv)
+{
+ struct options o;
+ int opt;
+
+ memset(&o, 0, sizeof(o));
+
+ while ((opt = getopt(argc, argv, "hp:U:uIv")) != -1) {
+ switch (opt) {
+ case 'p': o.port = atoi(optarg); break;
+ case 'U': o.unixsock = optarg; break;
+ case 'u': o.unlink = 1; break;
+ case 'I': o.iocp = 1; break;
+ case 'v': ++o.verbose; break;
+ case 'h': print_usage(stdout, argv[0], 0); break;
+ default : fprintf(stderr, "Unknown option %c\n", opt); break;
+ }
+ }
+
+ if (optind >= argc || (argc - optind) > 1) {
+ print_usage(stdout, argv[0], 1);
+ }
+ o.docroot = argv[optind];
+
+ return o;
+}
+
+static void
+do_term(int sig, short events, void *arg)
+{
+ struct event_base *base = arg;
+ event_base_loopbreak(base);
+ fprintf(stderr, "Got %i, Terminating\n", sig);
+}
+
+static int
+display_listen_sock(struct evhttp_bound_socket *handle)
{
- fprintf(stdout, "Syntax: http-server <docroot>\n");
+ struct sockaddr_storage ss;
+ evutil_socket_t fd;
+ ev_socklen_t socklen = sizeof(ss);
+ char addrbuf[128];
+ void *inaddr;
+ const char *addr;
+ int got_port = -1;
+
+ fd = evhttp_bound_socket_get_fd(handle);
+ memset(&ss, 0, sizeof(ss));
+ if (getsockname(fd, (struct sockaddr *)&ss, &socklen)) {
+ perror("getsockname() failed");
+ return 1;
+ }
+
+ if (ss.ss_family == AF_INET) {
+ got_port = ntohs(((struct sockaddr_in*)&ss)->sin_port);
+ inaddr = &((struct sockaddr_in*)&ss)->sin_addr;
+ } else if (ss.ss_family == AF_INET6) {
+ got_port = ntohs(((struct sockaddr_in6*)&ss)->sin6_port);
+ inaddr = &((struct sockaddr_in6*)&ss)->sin6_addr;
+ }
+#ifdef EVENT__HAVE_STRUCT_SOCKADDR_UN
+ else if (ss.ss_family == AF_UNIX) {
+ printf("Listening on <%s>\n", ((struct sockaddr_un*)&ss)->sun_path);
+ return 0;
+ }
+#endif
+ else {
+ fprintf(stderr, "Weird address family %d\n",
+ ss.ss_family);
+ return 1;
+ }
+
+ addr = evutil_inet_ntop(ss.ss_family, inaddr, addrbuf,
+ sizeof(addrbuf));
+ if (addr) {
+ printf("Listening on %s:%d\n", addr, got_port);
+ evutil_snprintf(uri_root, sizeof(uri_root),
+ "http://%s:%d",addr,got_port);
+ } else {
+ fprintf(stderr, "evutil_inet_ntop failed\n");
+ return 1;
+ }
+
+ return 0;
}
int
main(int argc, char **argv)
{
- struct event_base *base;
- struct evhttp *http;
- struct evhttp_bound_socket *handle;
+ struct event_config *cfg = NULL;
+ struct event_base *base = NULL;
+ struct evhttp *http = NULL;
+ struct evhttp_bound_socket *handle = NULL;
+ struct evconnlistener *lev = NULL;
+ struct event *term = NULL;
+ struct options o = parse_opts(argc, argv);
+ int ret = 0;
- ev_uint16_t port = 0;
#ifdef _WIN32
- WSADATA WSAData;
- WSAStartup(0x101, &WSAData);
+ {
+ WORD wVersionRequested;
+ WSADATA wsaData;
+ wVersionRequested = MAKEWORD(2, 2);
+ WSAStartup(wVersionRequested, &wsaData);
+ }
#else
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
- return (1);
+ if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
+ ret = 1;
+ goto err;
+ }
#endif
- if (argc < 2) {
- syntax();
- return 1;
+
+ setbuf(stdout, NULL);
+ setbuf(stderr, NULL);
+
+ /** Read env like in regress */
+ if (o.verbose || getenv("EVENT_DEBUG_LOGGING_ALL"))
+ event_enable_debug_logging(EVENT_DBG_ALL);
+
+ cfg = event_config_new();
+#ifdef _WIN32
+ if (o.iocp) {
+#ifdef EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED
+ evthread_use_windows_threads();
+ event_config_set_num_cpus_hint(cfg, 8);
+#endif
+ event_config_set_flag(cfg, EVENT_BASE_FLAG_STARTUP_IOCP);
}
+#endif
- base = event_base_new();
+ base = event_base_new_with_config(cfg);
if (!base) {
fprintf(stderr, "Couldn't create an event_base: exiting\n");
- return 1;
+ ret = 1;
}
+ event_config_free(cfg);
+ cfg = NULL;
/* Create a new evhttp object to handle requests. */
http = evhttp_new(base);
if (!http) {
fprintf(stderr, "couldn't create evhttp. Exiting.\n");
- return 1;
+ ret = 1;
}
/* The /dump URI will dump all requests to stdout and say 200 ok. */
@@ -364,55 +506,77 @@ main(int argc, char **argv)
/* We want to accept arbitrary requests, so we need to set a "generic"
* cb. We can also add callbacks for specific paths. */
- evhttp_set_gencb(http, send_document_cb, argv[1]);
+ evhttp_set_gencb(http, send_document_cb, &o);
- /* Now we tell the evhttp what port to listen on */
- handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", port);
- if (!handle) {
- fprintf(stderr, "couldn't bind to port %d. Exiting.\n",
- (int)port);
- return 1;
- }
+ if (o.unixsock) {
+#ifdef EVENT__HAVE_STRUCT_SOCKADDR_UN
+ struct sockaddr_un addr;
- {
- /* Extract and display the address we're listening on. */
- struct sockaddr_storage ss;
- evutil_socket_t fd;
- ev_socklen_t socklen = sizeof(ss);
- char addrbuf[128];
- void *inaddr;
- const char *addr;
- int got_port = -1;
- fd = evhttp_bound_socket_get_fd(handle);
- memset(&ss, 0, sizeof(ss));
- if (getsockname(fd, (struct sockaddr *)&ss, &socklen)) {
- perror("getsockname() failed");
- return 1;
+ if (o.unlink && (unlink(o.unixsock) && errno != ENOENT)) {
+ perror(o.unixsock);
+ ret = 1;
+ goto err;
+ }
+
+ addr.sun_family = AF_UNIX;
+ strcpy(addr.sun_path, o.unixsock);
+
+ lev = evconnlistener_new_bind(base, NULL, NULL,
+ LEV_OPT_CLOSE_ON_FREE, -1,
+ (struct sockaddr *)&addr, sizeof(addr));
+ if (!lev) {
+ perror("Cannot create listener");
+ ret = 1;
+ goto err;
}
- if (ss.ss_family == AF_INET) {
- got_port = ntohs(((struct sockaddr_in*)&ss)->sin_port);
- inaddr = &((struct sockaddr_in*)&ss)->sin_addr;
- } else if (ss.ss_family == AF_INET6) {
- got_port = ntohs(((struct sockaddr_in6*)&ss)->sin6_port);
- inaddr = &((struct sockaddr_in6*)&ss)->sin6_addr;
- } else {
- fprintf(stderr, "Weird address family %d\n",
- ss.ss_family);
- return 1;
+
+ handle = evhttp_bind_listener(http, lev);
+ if (!handle) {
+ fprintf(stderr, "couldn't bind to %s. Exiting.\n", o.unixsock);
+ ret = 1;
+ goto err;
}
- addr = evutil_inet_ntop(ss.ss_family, inaddr, addrbuf,
- sizeof(addrbuf));
- if (addr) {
- printf("Listening on %s:%d\n", addr, got_port);
- evutil_snprintf(uri_root, sizeof(uri_root),
- "http://%s:%d",addr,got_port);
- } else {
- fprintf(stderr, "evutil_inet_ntop failed\n");
- return 1;
+#else /* !EVENT__HAVE_STRUCT_SOCKADDR_UN */
+ fprintf(stderr, "-U is not supported on this platform. Exiting.\n");
+ ret = 1;
+ goto err;
+#endif /* EVENT__HAVE_STRUCT_SOCKADDR_UN */
+ }
+ else {
+ handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", o.port);
+ if (!handle) {
+ fprintf(stderr, "couldn't bind to port %d. Exiting.\n", o.port);
+ ret = 1;
+ goto err;
}
}
+ if (display_listen_sock(handle)) {
+ ret = 1;
+ goto err;
+ }
+
+ term = evsignal_new(base, SIGINT, do_term, base);
+ if (!term)
+ goto err;
+ if (event_add(term, NULL))
+ goto err;
+
event_base_dispatch(base);
- return 0;
+#ifdef _WIN32
+ WSACleanup();
+#endif
+
+err:
+ if (cfg)
+ event_config_free(cfg);
+ if (http)
+ evhttp_free(http);
+ if (term)
+ event_free(term);
+ if (base)
+ event_base_free(base);
+
+ return ret;
}
diff --git a/sample/https-client.c b/sample/https-client.c
index 748395656b2e..5136acebd79a 100644
--- a/sample/https-client.c
+++ b/sample/https-client.c
@@ -45,7 +45,6 @@
#include "openssl_hostname_validation.h"
-static struct event_base *base;
static int ignore_cert = 0;
static void
@@ -54,7 +53,7 @@ http_request_done(struct evhttp_request *req, void *ctx)
char buffer[256];
int nread;
- if (req == NULL) {
+ if (!req || !evhttp_request_get_response_code(req)) {
/* If req is NULL, it means an error occurred, but
* sadly we are mostly left guessing what the error
* might have been. We'll do our best... */
@@ -119,7 +118,6 @@ err_openssl(const char *func)
exit(1);
}
-#ifndef _WIN32
/* See http://archives.seul.org/libevent/users/Jan-2013/msg00039.html */
static int cert_verify_callback(X509_STORE_CTX *x509_ctx, void *arg)
{
@@ -182,16 +180,45 @@ static int cert_verify_callback(X509_STORE_CTX *x509_ctx, void *arg)
return 0;
}
}
+
+#ifdef _WIN32
+static int
+add_cert_for_store(X509_STORE *store, const char *name)
+{
+ HCERTSTORE sys_store = NULL;
+ PCCERT_CONTEXT ctx = NULL;
+ int r = 0;
+
+ sys_store = CertOpenSystemStore(0, name);
+ if (!sys_store) {
+ err("failed to open system certificate store");
+ return -1;
+ }
+ while ((ctx = CertEnumCertificatesInStore(sys_store, ctx))) {
+ X509 *x509 = d2i_X509(NULL, (unsigned char const **)&ctx->pbCertEncoded,
+ ctx->cbCertEncoded);
+ if (x509) {
+ X509_STORE_add_cert(store, x509);
+ X509_free(x509);
+ } else {
+ r = -1;
+ err_openssl("d2i_X509");
+ break;
+ }
+ }
+ CertCloseStore(sys_store, 0);
+ return r;
+}
#endif
int
main(int argc, char **argv)
{
int r;
-
+ struct event_base *base = NULL;
struct evhttp_uri *http_uri = NULL;
const char *url = NULL, *data_file = NULL;
- const char *crt = "/etc/ssl/certs/ca-certificates.crt";
+ const char *crt = NULL;
const char *scheme, *host, *path, *query;
char uri[256];
int port;
@@ -312,7 +339,8 @@ main(int argc, char **argv)
}
uri[sizeof(uri) - 1] = '\0';
-#if OPENSSL_VERSION_NUMBER < 0x10100000L
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
+ (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
// Initialize OpenSSL
SSL_library_init();
ERR_load_crypto_strings();
@@ -335,14 +363,27 @@ main(int argc, char **argv)
goto error;
}
-#ifndef _WIN32
- /* TODO: Add certificate loading on Windows as well */
-
- /* Attempt to use the system's trusted root certificates.
- * (This path is only valid for Debian-based systems.) */
- if (1 != SSL_CTX_load_verify_locations(ssl_ctx, crt, NULL)) {
- err_openssl("SSL_CTX_load_verify_locations");
- goto error;
+ if (crt == NULL) {
+ X509_STORE *store;
+ /* Attempt to use the system's trusted root certificates. */
+ store = SSL_CTX_get_cert_store(ssl_ctx);
+#ifdef _WIN32
+ if (add_cert_for_store(store, "CA") < 0 ||
+ add_cert_for_store(store, "AuthRoot") < 0 ||
+ add_cert_for_store(store, "ROOT") < 0) {
+ goto error;
+ }
+#else // _WIN32
+ if (X509_STORE_set_default_paths(store) != 1) {
+ err_openssl("X509_STORE_set_default_paths");
+ goto error;
+ }
+#endif // _WIN32
+ } else {
+ if (SSL_CTX_load_verify_locations(ssl_ctx, crt, NULL) != 1) {
+ err_openssl("SSL_CTX_load_verify_locations");
+ goto error;
+ }
}
/* Ask OpenSSL to verify the server certificate. Note that this
* does NOT include verifying that the hostname is correct.
@@ -368,9 +409,6 @@ main(int argc, char **argv)
* "wrapping" OpenSSL's routine, not replacing it. */
SSL_CTX_set_cert_verify_callback(ssl_ctx, cert_verify_callback,
(void *) host);
-#else // _WIN32
- (void)crt;
-#endif // _WIN32
// Create event base
base = event_base_new();
@@ -474,25 +512,29 @@ cleanup:
evhttp_connection_free(evcon);
if (http_uri)
evhttp_uri_free(http_uri);
- event_base_free(base);
+ if (base)
+ event_base_free(base);
if (ssl_ctx)
SSL_CTX_free(ssl_ctx);
if (type == HTTP && ssl)
SSL_free(ssl);
-#if OPENSSL_VERSION_NUMBER < 0x10100000L
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
+ (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
EVP_cleanup();
ERR_free_strings();
-#ifdef EVENT__HAVE_ERR_REMOVE_THREAD_STATE
- ERR_remove_thread_state(NULL);
-#else
+#if OPENSSL_VERSION_NUMBER < 0x10000000L
ERR_remove_state(0);
+#else
+ ERR_remove_thread_state(NULL);
#endif
+
CRYPTO_cleanup_all_ex_data();
sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
-#endif /*OPENSSL_VERSION_NUMBER < 0x10100000L */
+#endif /* (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
+ (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L) */
#ifdef _WIN32
WSACleanup();
diff --git a/sample/include.am b/sample/include.am
index d1a7242f7caa..b6894d46e5fe 100644
--- a/sample/include.am
+++ b/sample/include.am
@@ -16,16 +16,19 @@ SAMPLES = \
if OPENSSL
SAMPLES += sample/le-proxy
sample_le_proxy_SOURCES = sample/le-proxy.c
-sample_le_proxy_LDADD = libevent.la libevent_openssl.la ${OPENSSL_LIBS} ${OPENSSL_LIBADD}
-sample_le_proxy_INCLUDES = $(OPENSSL_INCS)
+sample_le_proxy_LDADD = libevent.la libevent_openssl.la $(OPENSSL_LIBS) $(OPENSSL_LIBADD)
+sample_le_proxy_CPPFLAGS = $(AM_CPPFLAGS) $(OPENSSL_INCS)
SAMPLES += sample/https-client
sample_https_client_SOURCES = \
sample/https-client.c \
sample/hostcheck.c \
sample/openssl_hostname_validation.c
-sample_https_client_LDADD = libevent.la libevent_openssl.la ${OPENSSL_LIBS} ${OPENSSL_LIBADD}
-sample_https_client_INCLUDES = $(OPENSSL_INCS)
+sample_https_client_LDADD = libevent.la libevent_openssl.la $(OPENSSL_LIBS) $(OPENSSL_LIBADD)
+if BUILD_WIN32
+sample_https_client_LDADD += -lcrypt32
+endif
+sample_https_client_CPPFLAGS = $(AM_CPPFLAGS) $(OPENSSL_INCS)
noinst_HEADERS += \
sample/hostcheck.h \
sample/openssl_hostname_validation.h
diff --git a/sample/le-proxy.c b/sample/le-proxy.c
index 8d9b529ee68d..13e0e2aea918 100644
--- a/sample/le-proxy.c
+++ b/sample/le-proxy.c
@@ -30,6 +30,7 @@
#include <event2/listener.h>
#include <event2/util.h>
+#include "util-internal.h"
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/rand.h>
@@ -194,6 +195,7 @@ accept_cb(struct evconnlistener *listener, evutil_socket_t fd,
perror("Bufferevent_openssl_new");
bufferevent_free(b_out);
bufferevent_free(b_in);
+ return;
}
b_out = b_ssl;
}
@@ -214,6 +216,13 @@ main(int argc, char **argv)
int use_ssl = 0;
struct evconnlistener *listener;
+#ifdef _WIN32
+ WORD wVersionRequested;
+ WSADATA wsaData;
+ wVersionRequested = MAKEWORD(2, 2);
+ (void) WSAStartup(wVersionRequested, &wsaData);
+#endif
+
if (argc < 3)
syntax();
@@ -259,7 +268,8 @@ main(int argc, char **argv)
if (use_ssl) {
int r;
-#if OPENSSL_VERSION_NUMBER < 0x10100000L
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
+ (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
SSL_library_init();
ERR_load_crypto_strings();
SSL_load_error_strings();
@@ -287,5 +297,9 @@ main(int argc, char **argv)
evconnlistener_free(listener);
event_base_free(base);
+#ifdef _WIN32
+ WSACleanup();
+#endif
+
return 0;
}
diff --git a/sample/openssl_hostname_validation.c b/sample/openssl_hostname_validation.c
index 40312f2e9613..4036ccbaabc9 100644
--- a/sample/openssl_hostname_validation.c
+++ b/sample/openssl_hostname_validation.c
@@ -48,7 +48,8 @@ SOFTWARE.
#define HOSTNAME_MAX_SIZE 255
-#if OPENSSL_VERSION_NUMBER < 0x10100000L
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
+ (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
#define ASN1_STRING_get0_data ASN1_STRING_data
#endif
diff --git a/sample/signal-test.c b/sample/signal-test.c
index 18668350b868..4aef42051574 100644
--- a/sample/signal-test.c
+++ b/sample/signal-test.c
@@ -44,8 +44,9 @@ signal_cb(evutil_socket_t fd, short event, void *arg)
int
main(int argc, char **argv)
{
- struct event *signal_int;
+ struct event *signal_int = NULL;
struct event_base* base;
+ int ret = 0;
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
@@ -55,18 +56,28 @@ main(int argc, char **argv)
(void) WSAStartup(wVersionRequested, &wsaData);
#endif
- /* Initalize the event library */
+ /* Initialize the event library */
base = event_base_new();
+ if (!base) {
+ ret = 1;
+ goto out;
+ }
- /* Initalize one event */
+ /* Initialize one event */
signal_int = evsignal_new(base, SIGINT, signal_cb, event_self_cbarg());
-
+ if (!signal_int) {
+ ret = 2;
+ goto out;
+ }
event_add(signal_int, NULL);
event_base_dispatch(base);
- event_free(signal_int);
- event_base_free(base);
- return (0);
+out:
+ if (signal_int)
+ event_free(signal_int);
+ if (base)
+ event_base_free(base);
+ return ret;
}
diff --git a/sample/time-test.c b/sample/time-test.c
index c94c18a5004e..671a1d2108e2 100644
--- a/sample/time-test.c
+++ b/sample/time-test.c
@@ -88,10 +88,10 @@ main(int argc, char **argv)
flags = 0;
}
- /* Initalize the event library */
+ /* Initialize the event library */
base = event_base_new();
- /* Initalize one event */
+ /* Initialize one event */
event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);
evutil_timerclear(&tv);
@@ -100,6 +100,9 @@ main(int argc, char **argv)
evutil_gettimeofday(&lasttime, NULL);
+ setbuf(stdout, NULL);
+ setbuf(stderr, NULL);
+
event_base_dispatch(base);
return (0);