aboutsummaryrefslogtreecommitdiffstats
path: root/sample
diff options
context:
space:
mode:
authorHaibo Huang <hhb@google.com>2020-07-14 18:31:56 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2020-07-14 18:31:56 +0000
commit884d7a39094e590c9dc58f865b068f40694c6a49 (patch)
tree53915fc3eafdda183ab94c84e85ad7d7107afc6a /sample
parentc4d7c4bdab4c5587b42073d60fb6aca6e335d76e (diff)
parent023ea039b9fada42e0cbbefcd75858a855a6130a (diff)
downloadplatform_external_libevent-master.tar.gz
platform_external_libevent-master.tar.bz2
platform_external_libevent-master.zip
Upgrade libevent to release-2.1.12-stable am: f0077b80a0 am: 023ea039b9HEADmaster
Original change: https://android-review.googlesource.com/c/platform/external/libevent/+/1360893 Change-Id: Iee716ec96a55737a04463c7b84c929c2e6418c54
Diffstat (limited to 'sample')
-rw-r--r--sample/dns-example.c4
-rw-r--r--sample/event-read-fifo.c4
-rw-r--r--sample/hello-world.c3
-rw-r--r--sample/http-connect.c88
-rw-r--r--sample/http-server.c28
-rw-r--r--sample/https-client.c44
-rw-r--r--sample/include.am3
-rw-r--r--sample/signal-test.c25
-rw-r--r--sample/time-test.c4
9 files changed, 129 insertions, 74 deletions
diff --git a/sample/dns-example.c b/sample/dns-example.c
index 21a75de..2d07c38 100644
--- a/sample/dns-example.c
+++ b/sample/dns-example.c
@@ -225,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;
}
}
diff --git a/sample/event-read-fifo.c b/sample/event-read-fifo.c
index 27b0b53..a17b9bd 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 2023cd6..a13e06a 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 af2c86a..53f816d 100644
--- a/sample/http-connect.c
+++ b/sample/http-connect.c
@@ -23,15 +23,44 @@ 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;
struct evbuffer *evbuf;
- struct evhttp_connection *evcon;
VERIFY(req);
- evcon = evhttp_request_get_connection(req);
- VERIFY(evcon);
evbuf = evhttp_request_get_input_buffer(req);
len = evbuffer_get_length(evbuf);
@@ -41,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;
@@ -75,28 +104,8 @@ int main(int argc, const char **argv)
return 1;
}
- {
- VERIFY(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,
@@ -105,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 cedb2af..049aabc 100644
--- a/sample/http-server.c
+++ b/sample/http-server.c
@@ -99,14 +99,14 @@ static const struct table_entry {
{ NULL, NULL },
};
-struct options
-{
+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' */
@@ -182,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;
@@ -222,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;
@@ -346,12 +346,13 @@ done:
static void
print_usage(FILE *out, const char *prog, int exit_code)
{
- fprintf(out, "Syntax: [ OPTS ] %s <docroot>\n", prog);
- fprintf(out, " -p - port\n");
- fprintf(out, " -U - bind to unix socket\n");
- fprintf(out, " -u - unlink unix socket before bind\n");
- fprintf(out, " -I - IOCP\n");
- fprintf(out, " -v - verbosity, enables libevent debug logging too\n");
+ 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
@@ -374,9 +375,10 @@ parse_opts(int argc, char **argv)
}
}
- if (optind >= argc || (argc-optind) > 1) {
+ if (optind >= argc || (argc - optind) > 1) {
print_usage(stdout, argv[0], 1);
}
+ o.docroot = argv[optind];
return o;
}
@@ -504,7 +506,7 @@ 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);
if (o.unixsock) {
#ifdef EVENT__HAVE_STRUCT_SOCKADDR_UN
diff --git a/sample/https-client.c b/sample/https-client.c
index 58e449b..5136ace 100644
--- a/sample/https-client.c
+++ b/sample/https-client.c
@@ -118,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)
{
@@ -181,6 +180,35 @@ 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
@@ -335,17 +363,22 @@ main(int argc, char **argv)
goto error;
}
-#ifndef _WIN32
- /* TODO: Add certificate loading on Windows as well */
-
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");
@@ -376,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();
diff --git a/sample/include.am b/sample/include.am
index cc003b7..b6894d4 100644
--- a/sample/include.am
+++ b/sample/include.am
@@ -25,6 +25,9 @@ sample_https_client_SOURCES = \
sample/hostcheck.c \
sample/openssl_hostname_validation.c
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 \
diff --git a/sample/signal-test.c b/sample/signal-test.c
index 1866835..4aef420 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 8d0fd91..671a1d2 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);