083580b9a888e0b7a0b1315a9c7a91f068eff1a3
[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         L_CA_CERTIFICATE,
155 };
156
157 static const struct option longopts[] = {
158         [L_NO_CHECK_CERTIFICATE] = { "no-check-certificate", no_argument },
159         [L_CA_CERTIFICATE] = { "ca-certificate", required_argument },
160         {}
161 };
162
163 int main(int argc, char **argv)
164 {
165         const char *progname = argv[0];
166         struct uclient *cl;
167         bool verify = true;
168         int ch;
169         int longopt_idx = 0;
170
171         init_ustream_ssl();
172
173         while ((ch = getopt_long(argc, argv, "", longopts, &longopt_idx)) != -1) {
174                 switch(ch) {
175                 case 0:
176                         switch (longopt_idx) {
177                         case L_NO_CHECK_CERTIFICATE:
178                                 verify = false;
179                                 break;
180                         case L_CA_CERTIFICATE:
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                         break;
188                 default:
189                         return usage(progname);
190                 }
191         }
192
193         argv += optind;
194         argc -= optind;
195
196         if (argc != 1)
197                 return usage(progname);
198
199         if (!strncmp(argv[0], "https", 5) && !ssl_ctx)
200                 return no_ssl(progname);
201
202         uloop_init();
203
204         cl = uclient_new(argv[0], NULL, &cb);
205         if (!cl) {
206                 fprintf(stderr, "Failed to allocate uclient context\n");
207                 return 1;
208         }
209
210         if (ssl_ctx)
211                 uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
212
213         example_request_sm(cl);
214         uloop_run();
215         uloop_done();
216
217         uclient_free(cl);
218
219         if (ssl_ctx)
220                 ssl_ops->context_free(ssl_ctx);
221
222         return 0;
223 }