69ba79a53abebcfdd7f1412b810d738b1ab46711
2 * uclient - ustream based protocol client library
4 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
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.
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.
24 #include <libubox/blobmsg.h>
29 #define LIB_EXT "dylib"
34 static struct ustream_ssl_ctx
*ssl_ctx
;
35 static const struct ustream_ssl_ops
*ssl_ops
;
37 static void example_header_done(struct uclient
*cl
)
39 struct blob_attr
*cur
;
40 char local
[INET6_ADDRSTRLEN
], remote
[INET6_ADDRSTRLEN
];
41 int local_port
, remote_port
;
44 uclient_get_addr(local
, &local_port
, &cl
->local_addr
);
45 uclient_get_addr(remote
, &remote_port
, &cl
->remote_addr
);
47 fprintf(stderr
, "Connected: %s:%d -> %s:%d\n",
48 local
, local_port
, remote
, remote_port
);
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
));
55 printf("Contents:\n");
58 static void example_read_data(struct uclient
*cl
)
64 len
= uclient_read(cl
, buf
, sizeof(buf
));
68 write(STDOUT_FILENO
, buf
, len
);
72 static void example_request_sm(struct uclient
*cl
)
79 uclient_http_set_request_type(cl
, "HEAD");
84 uclient_http_set_request_type(cl
, "GET");
93 static void example_eof(struct uclient
*cl
)
97 if (retries
< 10 && uclient_http_redirect(cl
)) {
103 example_request_sm(cl
);
106 static void example_error(struct uclient
*cl
, int code
)
108 fprintf(stderr
, "Error %d!\n", code
);
109 example_request_sm(cl
);
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
,
119 static int usage(const char *progname
)
122 "Usage: %s [options] <hostname> <port>\n"
124 " -c <cert>: Load CA certificates from file <cert>\n"
125 " -C: Skip certificate CN verification against hostname\n"
131 static void init_ustream_ssl(void)
135 dlh
= dlopen("libustream-ssl." LIB_EXT
, RTLD_LAZY
| RTLD_LOCAL
);
139 ssl_ops
= dlsym(dlh
, "ustream_ssl_ops");
143 ssl_ctx
= ssl_ops
->context_new(false);
146 static int no_ssl(const char *progname
)
148 fprintf(stderr
, "%s: SSL support not available, please install ustream-ssl\n", progname
);
153 L_NO_CHECK_CERTIFICATE
,
156 static const struct option longopts
[] = {
157 [L_NO_CHECK_CERTIFICATE
] = { "no-check-certificate", no_argument
}
160 int main(int argc
, char **argv
)
162 const char *progname
= argv
[0];
170 while ((ch
= getopt_long(argc
, argv
, "c:", longopts
, &longopt_idx
)) != -1) {
173 switch (longopt_idx
) {
174 case L_NO_CHECK_CERTIFICATE
:
178 return usage(progname
);
182 ssl_ops
->context_add_ca_crt_file(ssl_ctx
, optarg
);
185 return usage(progname
);
193 return usage(progname
);
195 if (!strncmp(argv
[0], "https", 5) && !ssl_ctx
)
196 return no_ssl(progname
);
200 cl
= uclient_new(argv
[0], NULL
, &cb
);
202 fprintf(stderr
, "Failed to allocate uclient context\n");
207 uclient_http_set_ssl_ctx(cl
, ssl_ops
, ssl_ctx
, verify
);
209 example_request_sm(cl
);
216 ssl_ops
->context_free(ssl_ctx
);