summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Fietkau <nbd@openwrt.org>2014-05-04 13:59:45 +0200
committerFelix Fietkau <nbd@openwrt.org>2014-05-04 13:59:45 +0200
commitbd38e778dc28dbf3e5f0a249689fad2b2c6b7243 (patch)
tree468d8080bf8b170c22fc6f4bb281b7a9fc877727
parent3ddb765bd513a70d128810b65c3a45f81690782e (diff)
downloaduclient-bd38e778dc28dbf3e5f0a249689fad2b2c6b7243.tar.gz
example: make ustream-ssl optional (load via dlopen)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
-rw-r--r--CMakeLists.txt2
-rw-r--r--uclient-example.c47
2 files changed, 43 insertions, 6 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f7fd051..103b992 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -16,7 +16,7 @@ ADD_LIBRARY(uclient SHARED uclient.c uclient-http.c uclient-utils.c)
TARGET_LINK_LIBRARIES(uclient ubox dl)
ADD_EXECUTABLE(uclient-example uclient-example.c)
-TARGET_LINK_LIBRARIES(uclient-example uclient ustream-ssl)
+TARGET_LINK_LIBRARIES(uclient-example uclient)
INSTALL(FILES uclient.h uclient-utils.h
DESTINATION include/libubox
diff --git a/uclient-example.c b/uclient-example.c
index 8c9d3e6..e0e9015 100644
--- a/uclient-example.c
+++ b/uclient-example.c
@@ -18,11 +18,20 @@
#include <unistd.h>
#include <stdio.h>
+#include <dlfcn.h>
#include <libubox/blobmsg.h>
#include "uclient.h"
+#ifdef __APPLE__
+#define LIB_EXT "dylib"
+#else
+#define LIB_EXT "so"
+#endif
+
+static struct ustream_ssl_ctx *ssl_ctx;
+static const struct ustream_ssl_ops *ssl_ops;
static void example_header_done(struct uclient *cl)
{
@@ -118,20 +127,42 @@ static int usage(const char *progname)
}
+static void init_ustream_ssl(void)
+{
+ void *dlh;
+
+ dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL);
+ if (!dlh)
+ return;
+
+ ssl_ops = dlsym(dlh, "ustream_ssl_ops");
+ if (!ssl_ops)
+ return;
+
+ ssl_ctx = ssl_ops->context_new(false);
+}
+
+static int no_ssl(const char *progname)
+{
+ fprintf(stderr, "%s: SSL support not available, please install ustream-ssl\n", progname);
+ return 1;
+}
+
+
int main(int argc, char **argv)
{
- struct ustream_ssl_ctx *ctx;
const char *progname = argv[0];
struct uclient *cl;
bool verify = true;
int ch;
- ctx = ustream_ssl_context_new(false);
+ init_ustream_ssl();
while ((ch = getopt(argc, argv, "Cc:")) != -1) {
switch(ch) {
case 'c':
- ustream_ssl_context_add_ca_crt_file(ctx, optarg);
+ if (ssl_ctx)
+ ssl_ops->context_add_ca_crt_file(ssl_ctx, optarg);
break;
case 'C':
verify = false;
@@ -147,6 +178,9 @@ int main(int argc, char **argv)
if (argc != 1)
return usage(progname);
+ if (!strncmp(argv[0], "https", 5) && !ssl_ctx)
+ return no_ssl(progname);
+
uloop_init();
cl = uclient_new(argv[0], NULL, &cb);
@@ -155,14 +189,17 @@ int main(int argc, char **argv)
return 1;
}
- uclient_http_set_ssl_ctx(cl, &ustream_ssl_ops, ctx, verify);
+ if (ssl_ctx)
+ uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
+
example_request_sm(cl);
uloop_run();
uloop_done();
uclient_free(cl);
- ustream_ssl_context_free(ctx);
+ if (ssl_ctx)
+ ssl_ops->context_free(ssl_ctx);
return 0;
}