example: make ustream-ssl optional (load via dlopen)
[project/uclient.git] / uclient-example.c
1 /*
2  * uclient - ustream based protocol client library
3  *
4  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <dlfcn.h>
22
23 #include <libubox/blobmsg.h>
24
25 #include "uclient.h"
26
27 #ifdef __APPLE__
28 #define LIB_EXT "dylib"
29 #else
30 #define LIB_EXT "so"
31 #endif
32
33 static struct ustream_ssl_ctx *ssl_ctx;
34 static const struct ustream_ssl_ops *ssl_ops;
35
36 static void example_header_done(struct uclient *cl)
37 {
38         struct blob_attr *cur;
39         char local[INET6_ADDRSTRLEN], remote[INET6_ADDRSTRLEN];
40         int local_port, remote_port;
41         int rem;
42
43         uclient_get_addr(local, &local_port, &cl->local_addr);
44         uclient_get_addr(remote, &remote_port, &cl->remote_addr);
45
46         fprintf(stderr, "Connected: %s:%d -> %s:%d\n",
47                 local, local_port, remote, remote_port);
48
49         printf("Headers (%d): \n", cl->status_code);
50         blobmsg_for_each_attr(cur, cl->meta, rem) {
51                 printf("%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
52         }
53
54         printf("Contents:\n");
55 }
56
57 static void example_read_data(struct uclient *cl)
58 {
59         char buf[256];
60         int len;
61
62         while (1) {
63                 len = uclient_read(cl, buf, sizeof(buf));
64                 if (!len)
65                         return;
66
67                 write(STDOUT_FILENO, buf, len);
68         }
69 }
70
71 static void example_request_sm(struct uclient *cl)
72 {
73         static int i = 0;
74
75         switch (i++) {
76         case 0:
77                 uclient_connect(cl);
78                 uclient_http_set_request_type(cl, "HEAD");
79                 uclient_request(cl);
80                 break;
81         case 1:
82                 uclient_connect(cl);
83                 uclient_http_set_request_type(cl, "GET");
84                 uclient_request(cl);
85                 break;
86         default:
87                 uloop_end();
88                 break;
89         };
90 }
91
92 static void example_eof(struct uclient *cl)
93 {
94         static int retries;
95
96         if (retries < 10 && uclient_http_redirect(cl)) {
97                 retries++;
98                 return;
99         }
100
101         retries = 0;
102         example_request_sm(cl);
103 }
104
105 static void example_error(struct uclient *cl, int code)
106 {
107         fprintf(stderr, "Error %d!\n", code);
108         example_request_sm(cl);
109 }
110
111 static const struct uclient_cb cb = {
112         .header_done = example_header_done,
113         .data_read = example_read_data,
114         .data_eof = example_eof,
115         .error = example_error,
116 };
117
118 static int usage(const char *progname)
119 {
120         fprintf(stderr,
121                 "Usage: %s [options] <hostname> <port>\n"
122                 "Options:\n"
123                 "       -c <cert>:         Load CA certificates from file <cert>\n"
124                 "       -C:                Skip certificate CN verification against hostname\n"
125                 "\n", progname);
126         return 1;
127 }
128
129
130 static void init_ustream_ssl(void)
131 {
132         void *dlh;
133
134         dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL);
135         if (!dlh)
136                 return;
137
138         ssl_ops = dlsym(dlh, "ustream_ssl_ops");
139         if (!ssl_ops)
140                 return;
141
142         ssl_ctx = ssl_ops->context_new(false);
143 }
144
145 static int no_ssl(const char *progname)
146 {
147         fprintf(stderr, "%s: SSL support not available, please install ustream-ssl\n", progname);
148         return 1;
149 }
150
151
152 int main(int argc, char **argv)
153 {
154         const char *progname = argv[0];
155         struct uclient *cl;
156         bool verify = true;
157         int ch;
158
159         init_ustream_ssl();
160
161         while ((ch = getopt(argc, argv, "Cc:")) != -1) {
162                 switch(ch) {
163                 case 'c':
164                         if (ssl_ctx)
165                                 ssl_ops->context_add_ca_crt_file(ssl_ctx, optarg);
166                         break;
167                 case 'C':
168                         verify = false;
169                         break;
170                 default:
171                         return usage(progname);
172                 }
173         }
174
175         argv += optind;
176         argc -= optind;
177
178         if (argc != 1)
179                 return usage(progname);
180
181         if (!strncmp(argv[0], "https", 5) && !ssl_ctx)
182                 return no_ssl(progname);
183
184         uloop_init();
185
186         cl = uclient_new(argv[0], NULL, &cb);
187         if (!cl) {
188                 fprintf(stderr, "Failed to allocate uclient context\n");
189                 return 1;
190         }
191
192         if (ssl_ctx)
193                 uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
194
195         example_request_sm(cl);
196         uloop_run();
197         uloop_done();
198
199         uclient_free(cl);
200
201         if (ssl_ctx)
202                 ssl_ops->context_free(ssl_ctx);
203
204         return 0;
205 }