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.
21 int flag2, flag3, bflag, cflag, hflag, Nflag, Vflag, vflag;
24 main(int argc, char **argv)
27 struct tls_config *conf;
29 char iribuf[GEMINI_URL_LEN], buf[GEMINI_URL_LEN];
30 const char *parse_err = "unknown error", *port = "1965";
38 while ((ch = getopt(argc, argv, "23cbH:hNVv")) != -1) {
68 fprintf(stderr, "USAGE: %s [-23cbhNVv] [-H hostname]\n",
76 if ((bflag + cflag + hflag + Vflag) > 1)
77 errx(1, "only one of bchr flags can be used.");
79 if (flag2 + flag3 > 1)
80 errx(1, "only -2 or -3 can be specified at the same time.");
83 errx(1, "missing IRI");
85 if (strlcpy(iribuf, argv[0], sizeof(iribuf)) >= sizeof(iribuf))
86 errx(1, "request too long: %s", argv[0]);
87 if (strlcpy(buf, argv[0], sizeof(buf)) >= sizeof(iribuf))
88 errx(1, "request too long: %s", argv[0]);
89 if (strlcat(buf, "\r\n", sizeof(buf)) >= sizeof(buf))
90 errx(1, "request too long: %s", argv[0]);
92 if (!parse_iri(iribuf, &iri, &parse_err))
93 errx(1, "invalid IRI: %s", parse_err);
98 if ((conf = tls_config_new()) == NULL)
99 errx(1, "tls_config_new");
101 tls_config_insecure_noverifycert(conf);
103 tls_config_insecure_noverifyname(conf);
105 if (flag2 && tls_config_set_protocols(conf, TLS_PROTOCOL_TLSv1_2) == -1)
106 errx(1, "cannot set TLSv1.2");
107 if (flag3 && tls_config_set_protocols(conf, TLS_PROTOCOL_TLSv1_3) == -1)
108 errx(1, "cannot set TLSv1.3");
110 if ((ctx = tls_client()) == NULL)
111 errx(1, "tls_client creation failed");
113 if (tls_configure(ctx, conf) == -1)
114 errx(1, "tls_configure: %s", tls_error(ctx));
116 if (*iri.port != '\0')
119 if (hostname == NULL)
122 if (tls_connect_servername(ctx, iri.host, port, hostname) == -1)
123 errx(1, "tls_connect: %s", tls_error(ctx));
125 for (handshake = 0; !handshake;) {
126 switch (tls_handshake(ctx)) {
136 if (tls_write(ctx, buf, strlen(buf)) == -1)
137 errx(1, "tls_write: %s", tls_error(ctx));
140 switch (len = tls_read(ctx, buf, sizeof(buf))) {
144 case TLS_WANT_POLLIN:
145 case TLS_WANT_POLLOUT:
151 if ((t = strchr(buf, '\r')) != NULL)
153 else if ((t = strchr(buf, '\n')) != NULL)
169 t = strchr(buf, '\r');
171 t = strchr(buf, '\n');
174 write(1, buf, t - buf);