2 497977d5 2021-01-23 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 497977d5 2021-01-23 op * Permission to use, copy, modify, and distribute this software for any
5 497977d5 2021-01-23 op * purpose with or without fee is hereby granted, provided that the above
6 497977d5 2021-01-23 op * copyright notice and this permission notice appear in all copies.
8 497977d5 2021-01-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 497977d5 2021-01-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 497977d5 2021-01-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 497977d5 2021-01-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 497977d5 2021-01-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 497977d5 2021-01-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 497977d5 2021-01-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 497977d5 2021-01-23 op #include <string.h>
19 497977d5 2021-01-23 op #include "gmid.h"
22 497977d5 2021-01-23 op main(int argc, char **argv)
24 497977d5 2021-01-23 op struct iri iri;
25 497977d5 2021-01-23 op struct tls_config *conf;
26 497977d5 2021-01-23 op struct tls *ctx;
27 9adde3d8 2021-01-23 op char iribuf[GEMINI_URL_LEN], buf[GEMINI_URL_LEN];
28 497977d5 2021-01-23 op const char *parse_err = "unknown error", *port = "1965";
30 497977d5 2021-01-23 op int ch, flag2, flag3, bflag, cflag, hflag, Nflag, Vflag;
33 497977d5 2021-01-23 op flag2 = flag3 = bflag = cflag = hflag = Nflag = Vflag = 0;
34 497977d5 2021-01-23 op while ((ch = getopt(argc, argv, "23cbhNV")) != -1) {
58 497977d5 2021-01-23 op fprintf(stderr, "USAGE: %s [-23cbhNV]", *argv);
62 497977d5 2021-01-23 op argc -= optind;
63 497977d5 2021-01-23 op argv += optind;
65 497977d5 2021-01-23 op if ((bflag + cflag + hflag + Vflag) > 1)
66 497977d5 2021-01-23 op errx(1, "only one of bchr flags can be used.");
68 497977d5 2021-01-23 op if (flag2 + flag3 > 1)
69 497977d5 2021-01-23 op errx(1, "only -2 or -3 can be specified at the same time.");
71 497977d5 2021-01-23 op if (argc != 1)
72 497977d5 2021-01-23 op errx(1, "missing IRI");
74 497977d5 2021-01-23 op if (strlcpy(iribuf, argv[0], sizeof(iribuf)) >= sizeof(iribuf))
75 497977d5 2021-01-23 op errx(1, "request too long: %s", argv[0]);
76 9adde3d8 2021-01-23 op if (strlcpy(buf, argv[0], sizeof(buf)) >= sizeof(iribuf))
77 497977d5 2021-01-23 op errx(1, "request too long: %s", argv[0]);
78 9adde3d8 2021-01-23 op if (strlcat(buf, "\r\n", sizeof(buf)) >= sizeof(buf))
79 497977d5 2021-01-23 op errx(1, "request too long: %s", argv[0]);
81 497977d5 2021-01-23 op if (!parse_iri(iribuf, &iri, &parse_err))
82 497977d5 2021-01-23 op errx(1, "invalid IRI: %s", parse_err);
85 497977d5 2021-01-23 op errx(0, "IRI: OK");
87 497977d5 2021-01-23 op if ((conf = tls_config_new()) == NULL)
88 497977d5 2021-01-23 op errx(1, "tls_config_new");
90 497977d5 2021-01-23 op tls_config_insecure_noverifycert(conf);
92 497977d5 2021-01-23 op tls_config_insecure_noverifyname(conf);
94 497977d5 2021-01-23 op if (flag2 && tls_config_set_protocols(conf, TLS_PROTOCOL_TLSv1_2) == -1)
95 497977d5 2021-01-23 op errx(1, "cannot set TLSv1.2");
96 497977d5 2021-01-23 op if (flag3 && tls_config_set_protocols(conf, TLS_PROTOCOL_TLSv1_3) == -1)
97 497977d5 2021-01-23 op errx(1, "cannot set TLSv1.3");
99 497977d5 2021-01-23 op if ((ctx = tls_client()) == NULL)
100 497977d5 2021-01-23 op errx(1, "tls_client creation failed");
102 497977d5 2021-01-23 op if (tls_configure(ctx, conf) == -1)
103 497977d5 2021-01-23 op errx(1, "tls_configure: %s", tls_error(ctx));
105 d760973a 2021-01-23 op if (*iri.port != '\0')
106 d760973a 2021-01-23 op port = iri.port;
107 d760973a 2021-01-23 op if (tls_connect(ctx, iri.host, port) == -1)
108 497977d5 2021-01-23 op errx(1, "tls_connect: %s", tls_error(ctx));
110 9adde3d8 2021-01-23 op tls_write(ctx, buf, strlen(buf));
111 9adde3d8 2021-01-23 op /* if (tls_write(ctx, buf, strlen(buf)) != -1) */
112 497977d5 2021-01-23 op /* errx(1, "tls_write: %s", tls_error(ctx)); */
115 f62aab51 2021-01-23 op switch (len = tls_read(ctx, buf, sizeof(buf))) {
119 f62aab51 2021-01-23 op case TLS_WANT_POLLIN:
120 f62aab51 2021-01-23 op case TLS_WANT_POLLOUT:
126 497977d5 2021-01-23 op if ((t = strchr(buf, '\r')) != NULL)
128 497977d5 2021-01-23 op else if ((t = strchr(buf, '\n')) != NULL)
132 497977d5 2021-01-23 op len -= t - buf;
133 497977d5 2021-01-23 op write(1, t, len);
138 497977d5 2021-01-23 op write(1, buf, 2);
139 497977d5 2021-01-23 op write(1, "\n", 1);
144 497977d5 2021-01-23 op t = strchr(buf, '\r');
145 497977d5 2021-01-23 op if (t == NULL)
146 497977d5 2021-01-23 op t = strchr(buf, '\n');
147 497977d5 2021-01-23 op if (t == NULL)
148 497977d5 2021-01-23 op t = &buf[len];
149 497977d5 2021-01-23 op write(1, buf, t - buf);
150 497977d5 2021-01-23 op write(1, "\n", 1);
154 497977d5 2021-01-23 op write(1, buf, len);
158 497977d5 2021-01-23 op tls_close(ctx);
159 497977d5 2021-01-23 op tls_free(ctx);