example: change -C to --no-check-certificate to match wget
[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 #include <getopt.h>
23
24 #include <libubox/blobmsg.h>
25
26 #include "uclient.h"
27
28 #ifdef __APPLE__
29 #define LIB_EXT "dylib"
30 #else
31 #define LIB_EXT "so"
32 #endif
33
34 static struct ustream_ssl_ctx *ssl_ctx;
35 static const struct ustream_ssl_ops *ssl_ops;
36
37 static void example_header_done(struct uclient *cl)
38 {
39         struct blob_attr *cur;
40         char local[INET6_ADDRSTRLEN], remote[INET6_ADDRSTRLEN];
41         int local_port, remote_port;
42         int rem;
43
44         uclient_get_addr(local, &local_port, &cl->local_addr);
45         uclient_get_addr(remote, &remote_port, &cl->remote_addr);
46
47         fprintf(stderr, "Connected: %s:%d -> %s:%d\n",
48                 local, local_port, remote, remote_port);
49
50         printf("Headers (%d): \n", cl->status_code);
51         blobmsg_for_each_attr(cur, cl->meta, rem) {
52                 printf("%s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
53         }
54
55         printf("Contents:\n");
56 }
57
58 static void example_read_data(struct uclient *cl)
59 {
60         char buf[256];
61         int len;
62
63         while (1) {
64                 len = uclient_read(cl, buf, sizeof(buf));
65                 if (!len)
66                         return;
67
68                 write(STDOUT_FILENO, buf, len);
69         }
70 }
71
72 static void example_request_sm(struct uclient *cl)
73 {
74         static int i = 0;
75
76         switch (i++) {
77         case 0:
78                 uclient_connect(cl);
79                 uclient_http_set_request_type(cl, "HEAD");
80                 uclient_request(cl);
81                 break;
82         case 1:
83                 uclient_connect(cl);
84                 uclient_http_set_request_type(cl, "GET");
85                 uclient_request(cl);
86                 break;
87         default:
88                 uloop_end();
89                 break;
90         };
91 }
92
93 static void example_eof(struct uclient *cl)
94 {
95         static int retries;
96
97         if (retries < 10 && uclient_http_redirect(cl)) {
98                 retries++;
99                 return;
100         }
101
102         retries = 0;
103         example_request_sm(cl);
104 }
105
106 static void example_error(struct uclient *cl, int code)
107 {
108         fprintf(stderr, "Error %d!\n", code);
109         example_request_sm(cl);
110 }
111
112 static const struct uclient_cb cb = {
113         .header_done = example_header_done,
114         .data_read = example_read_data,
115         .data_eof = example_eof,
116         .error = example_error,
117 };
118
119 static int usage(const char *progname)
120 {
121         fprintf(stderr,
122                 "Usage: %s [options] <hostname> <port>\n"
123                 "Options:\n"
124                 "       -c <cert>:         Load CA certificates from file <cert>\n"
125                 "       -C:                Skip certificate CN verification against hostname\n"
126                 "\n", progname);
127         return 1;
128 }
129
130
131 static void init_ustream_ssl(void)
132 {
133         void *dlh;
134
135         dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL);
136         if (!dlh)
137                 return;
138
139         ssl_ops = dlsym(dlh, "ustream_ssl_ops");
140         if (!ssl_ops)
141                 return;
142
143         ssl_ctx = ssl_ops->context_new(false);
144 }
145
146 static int no_ssl(const char *progname)
147 {
148         fprintf(stderr, "%s: SSL support not available, please install ustream-ssl\n", progname);
149         return 1;
150 }
151
152 enum {
153         L_NO_CHECK_CERTIFICATE,
154 };
155
156 static const struct option longopts[] = {
157         [L_NO_CHECK_CERTIFICATE] = { "no-check-certificate", no_argument }
158 };
159
160 int main(int argc, char **argv)
161 {
162         const char *progname = argv[0];
163         struct uclient *cl;
164         bool verify = true;
165         int ch;
166         int longopt_idx = 0;
167
168         init_ustream_ssl();
169
170         while ((ch = getopt_long(argc, argv, "c:", longopts, &longopt_idx)) != -1) {
171                 switch(ch) {
172                 case 0:
173                         switch (longopt_idx) {
174                         case L_NO_CHECK_CERTIFICATE:
175                                 verify = false;
176                                 break;
177                         default:
178                                 return usage(progname);
179                         }
180                 case 'c':
181                         if (ssl_ctx)
182                                 ssl_ops->context_add_ca_crt_file(ssl_ctx, optarg);
183                         break;
184                 default:
185                         return usage(progname);
186                 }
187         }
188
189         argv += optind;
190         argc -= optind;
191
192         if (argc != 1)
193                 return usage(progname);
194
195         if (!strncmp(argv[0], "https", 5) && !ssl_ctx)
196                 return no_ssl(progname);
197
198         uloop_init();
199
200         cl = uclient_new(argv[0], NULL, &cb);
201         if (!cl) {
202                 fprintf(stderr, "Failed to allocate uclient context\n");
203                 return 1;
204         }
205
206         if (ssl_ctx)
207                 uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
208
209         example_request_sm(cl);
210         uloop_run();
211         uloop_done();
212
213         uclient_free(cl);
214
215         if (ssl_ctx)
216                 ssl_ops->context_free(ssl_ctx);
217
218         return 0;
219 }