example: make ustream-ssl optional (load via dlopen)
[project/uclient.git] / uclient-example.c
index 2e88b28..e0e9015 100644 (file)
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
-#include <libubox/blobmsg.h>
+
 #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)
 {
        struct blob_attr *cur;
+       char local[INET6_ADDRSTRLEN], remote[INET6_ADDRSTRLEN];
+       int local_port, remote_port;
        int rem;
 
+       uclient_get_addr(local, &local_port, &cl->local_addr);
+       uclient_get_addr(remote, &remote_port, &cl->remote_addr);
+
+       fprintf(stderr, "Connected: %s:%d -> %s:%d\n",
+               local, local_port, remote, remote_port);
+
        printf("Headers (%d): \n", cl->status_code);
        blobmsg_for_each_attr(cur, cl->meta, rem) {
                printf("%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
@@ -107,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;
@@ -136,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);
@@ -144,14 +189,17 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       uclient_http_set_ssl_ctx(cl, 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;
 }