add support for querying local/remote address
[project/uclient.git] / uclient-example.c
index 9bfd241..a54aa88 100644 (file)
@@ -1,14 +1,42 @@
-#include <libubox/blobmsg.h>
+/*
+ * uclient - ustream based protocol client library
+ *
+ * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
 #include <unistd.h>
 #include <stdio.h>
 
+#include <libubox/blobmsg.h>
+
 #include "uclient.h"
 
+
 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));
@@ -121,7 +149,7 @@ int main(int argc, char **argv)
 
        uloop_init();
 
-       cl = uclient_new(argv[0], &cb);
+       cl = uclient_new(argv[0], NULL, &cb);
        if (!cl) {
                fprintf(stderr, "Failed to allocate uclient context\n");
                return 1;