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