e0e90153d45f01cb13d337b12e726205c823c43a
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.
23 #include <libubox/blobmsg.h>
28 #define LIB_EXT "dylib"
33 static struct ustream_ssl_ctx
*ssl_ctx
;
34 static const struct ustream_ssl_ops
*ssl_ops
;
36 static void example_header_done(struct uclient
*cl
)
38 struct blob_attr
*cur
;
39 char local
[INET6_ADDRSTRLEN
], remote
[INET6_ADDRSTRLEN
];
40 int local_port
, remote_port
;
43 uclient_get_addr(local
, &local_port
, &cl
->local_addr
);
44 uclient_get_addr(remote
, &remote_port
, &cl
->remote_addr
);
46 fprintf(stderr
, "Connected: %s:%d -> %s:%d\n",
47 local
, local_port
, remote
, remote_port
);
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
));
54 printf("Contents:\n");
57 static void example_read_data(struct uclient
*cl
)
63 len
= uclient_read(cl
, buf
, sizeof(buf
));
67 write(STDOUT_FILENO
, buf
, len
);
71 static void example_request_sm(struct uclient
*cl
)
78 uclient_http_set_request_type(cl
, "HEAD");
83 uclient_http_set_request_type(cl
, "GET");
92 static void example_eof(struct uclient
*cl
)
96 if (retries
< 10 && uclient_http_redirect(cl
)) {
102 example_request_sm(cl
);
105 static void example_error(struct uclient
*cl
, int code
)
107 fprintf(stderr
, "Error %d!\n", code
);
108 example_request_sm(cl
);
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
,
118 static int usage(const char *progname
)
121 "Usage: %s [options] <hostname> <port>\n"
123 " -c <cert>: Load CA certificates from file <cert>\n"
124 " -C: Skip certificate CN verification against hostname\n"
130 static void init_ustream_ssl(void)
134 dlh
= dlopen("libustream-ssl." LIB_EXT
, RTLD_LAZY
| RTLD_LOCAL
);
138 ssl_ops
= dlsym(dlh
, "ustream_ssl_ops");
142 ssl_ctx
= ssl_ops
->context_new(false);
145 static int no_ssl(const char *progname
)
147 fprintf(stderr
, "%s: SSL support not available, please install ustream-ssl\n", progname
);
152 int main(int argc
, char **argv
)
154 const char *progname
= argv
[0];
161 while ((ch
= getopt(argc
, argv
, "Cc:")) != -1) {
165 ssl_ops
->context_add_ca_crt_file(ssl_ctx
, optarg
);
171 return usage(progname
);
179 return usage(progname
);
181 if (!strncmp(argv
[0], "https", 5) && !ssl_ctx
)
182 return no_ssl(progname
);
186 cl
= uclient_new(argv
[0], NULL
, &cb
);
188 fprintf(stderr
, "Failed to allocate uclient context\n");
193 uclient_http_set_ssl_ctx(cl
, ssl_ops
, ssl_ctx
, verify
);
195 example_request_sm(cl
);
202 ssl_ops
->context_free(ssl_ctx
);