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