Blame


1 5e11c00c 2021-03-02 op #include "telescope.h"
2 5e11c00c 2021-03-02 op
3 5e11c00c 2021-03-02 op #include <sys/socket.h>
4 5e11c00c 2021-03-02 op
5 5e11c00c 2021-03-02 op #include <err.h>
6 5e11c00c 2021-03-02 op #include <errno.h>
7 5e11c00c 2021-03-02 op #include <signal.h>
8 5e11c00c 2021-03-02 op #include <stdio.h>
9 5e11c00c 2021-03-02 op #include <stdlib.h>
10 5e11c00c 2021-03-02 op #include <string.h>
11 5e11c00c 2021-03-02 op #include <unistd.h>
12 5e11c00c 2021-03-02 op
13 35e1f40a 2021-03-14 op struct event netev, fsev;
14 5e11c00c 2021-03-02 op struct tabshead tabshead;
15 5e11c00c 2021-03-02 op
16 c07ac996 2021-03-12 op /* the first is also the fallback one */
17 c07ac996 2021-03-12 op static struct proto protos[] = {
18 4d3785b1 2021-03-09 op { "gemini:", load_gemini_url },
19 4d3785b1 2021-03-09 op { "about:", load_about_url },
20 4d3785b1 2021-03-09 op { NULL, NULL },
21 4d3785b1 2021-03-09 op };
22 4d3785b1 2021-03-09 op
23 35e1f40a 2021-03-14 op static struct imsgbuf *netibuf, *fsibuf;
24 5e11c00c 2021-03-02 op
25 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
26 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
27 2051e653 2021-03-13 op static void handle_imsg_err(struct imsg*, size_t);
28 2051e653 2021-03-13 op static void handle_imsg_check_cert(struct imsg*, size_t);
29 2051e653 2021-03-13 op static void handle_imsg_got_code(struct imsg*, size_t);
30 2051e653 2021-03-13 op static void handle_imsg_got_meta(struct imsg*, size_t);
31 2051e653 2021-03-13 op static void handle_imsg_buf(struct imsg*, size_t);
32 2051e653 2021-03-13 op static void handle_imsg_eof(struct imsg*, size_t);
33 740f578b 2021-03-15 op static void handle_imsg_bookmark_ok(struct imsg*, size_t);
34 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
35 2051e653 2021-03-13 op static void load_page_from_str(struct tab*, const char*);
36 2051e653 2021-03-13 op static void do_load_url(struct tab*, const char*);
37 5e11c00c 2021-03-02 op
38 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
39 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
40 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
41 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
42 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
43 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
44 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
45 740f578b 2021-03-15 op [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
46 5e11c00c 2021-03-02 op };
47 5e11c00c 2021-03-02 op
48 cbcc75fb 2021-03-17 op static struct ohash certs;
49 cbcc75fb 2021-03-17 op
50 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
51 5e11c00c 2021-03-02 op die(void)
52 5e11c00c 2021-03-02 op {
53 5e11c00c 2021-03-02 op abort(); /* TODO */
54 5e11c00c 2021-03-02 op }
55 5e11c00c 2021-03-02 op
56 5e11c00c 2021-03-02 op static struct tab *
57 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
58 5e11c00c 2021-03-02 op {
59 5e11c00c 2021-03-02 op struct tab *t;
60 5e11c00c 2021-03-02 op
61 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
62 5e11c00c 2021-03-02 op if (t->id == id)
63 5e11c00c 2021-03-02 op return t;
64 5e11c00c 2021-03-02 op }
65 5e11c00c 2021-03-02 op
66 5e11c00c 2021-03-02 op die();
67 5e11c00c 2021-03-02 op }
68 5e11c00c 2021-03-02 op
69 5e11c00c 2021-03-02 op static void
70 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
71 5e11c00c 2021-03-02 op {
72 3a9b9365 2021-03-09 op struct tab *tab;
73 3a9b9365 2021-03-09 op char *page;
74 3a9b9365 2021-03-09 op
75 3a9b9365 2021-03-09 op tab = tab_by_id(imsg->hdr.peerid);
76 3a9b9365 2021-03-09 op
77 3a9b9365 2021-03-09 op page = imsg->data;
78 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
79 3a9b9365 2021-03-09 op
80 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
81 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
82 3a9b9365 2021-03-09 op die();
83 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
84 3a9b9365 2021-03-09 op free(page);
85 5e11c00c 2021-03-02 op }
86 5e11c00c 2021-03-02 op
87 5e11c00c 2021-03-02 op static void
88 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
89 5e11c00c 2021-03-02 op {
90 cbcc75fb 2021-03-17 op const char *hash;
91 cbcc75fb 2021-03-17 op int tofu_res;
92 cbcc75fb 2021-03-17 op struct tofu_entry *e;
93 cbcc75fb 2021-03-17 op struct tab *tab;
94 5e11c00c 2021-03-02 op
95 cbcc75fb 2021-03-17 op hash = imsg->data;
96 cbcc75fb 2021-03-17 op if (hash[datalen-1] != '\0')
97 cbcc75fb 2021-03-17 op abort();
98 cbcc75fb 2021-03-17 op
99 10346511 2021-03-17 op tab = tab_by_id(imsg->hdr.peerid);
100 cbcc75fb 2021-03-17 op
101 cbcc75fb 2021-03-17 op if ((e = telescope_lookup_tofu(&certs, tab->url.host)) == NULL) {
102 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
103 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
104 cbcc75fb 2021-03-17 op * the certs! */
105 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
106 cbcc75fb 2021-03-17 op abort();
107 cbcc75fb 2021-03-17 op
108 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
109 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
110 cbcc75fb 2021-03-17 op abort();
111 cbcc75fb 2021-03-17 op strlcpy(e->domain, tab->url.host, sizeof(e->domain));
112 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
113 cbcc75fb 2021-03-17 op telescope_ohash_insert(&certs, e);
114 cbcc75fb 2021-03-17 op } else
115 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
116 cbcc75fb 2021-03-17 op
117 cbcc75fb 2021-03-17 op if (tofu_res)
118 cbcc75fb 2021-03-17 op tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
119 cbcc75fb 2021-03-17 op else {
120 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
121 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
122 cbcc75fb 2021-03-17 op }
123 10346511 2021-03-17 op imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
124 10346511 2021-03-17 op &tofu_res, sizeof(tofu_res));
125 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
126 5e11c00c 2021-03-02 op }
127 5e11c00c 2021-03-02 op
128 5cd2ebb1 2021-03-11 op static inline int
129 5cd2ebb1 2021-03-11 op normalize_code(int n)
130 5cd2ebb1 2021-03-11 op {
131 5cd2ebb1 2021-03-11 op if (n < 20) {
132 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
133 5cd2ebb1 2021-03-11 op return n;
134 5cd2ebb1 2021-03-11 op return 10;
135 5cd2ebb1 2021-03-11 op } else if (n < 30) {
136 5cd2ebb1 2021-03-11 op return 20;
137 5cd2ebb1 2021-03-11 op } else if (n < 40) {
138 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
139 5cd2ebb1 2021-03-11 op return n;
140 5cd2ebb1 2021-03-11 op return 30;
141 5cd2ebb1 2021-03-11 op } else if (n < 50) {
142 5cd2ebb1 2021-03-11 op if (n <= 44)
143 5cd2ebb1 2021-03-11 op return n;
144 5cd2ebb1 2021-03-11 op return 40;
145 5cd2ebb1 2021-03-11 op } else if (n < 60) {
146 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
147 5cd2ebb1 2021-03-11 op return n;
148 5cd2ebb1 2021-03-11 op return 50;
149 5cd2ebb1 2021-03-11 op } else if (n < 70) {
150 5cd2ebb1 2021-03-11 op if (n <= 62)
151 5cd2ebb1 2021-03-11 op return n;
152 5cd2ebb1 2021-03-11 op return 60;
153 5cd2ebb1 2021-03-11 op } else
154 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
155 5cd2ebb1 2021-03-11 op }
156 5cd2ebb1 2021-03-11 op
157 5e11c00c 2021-03-02 op static void
158 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
159 5e11c00c 2021-03-02 op {
160 0972d8b2 2021-03-02 op struct tab *tab;
161 5e11c00c 2021-03-02 op
162 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
163 0972d8b2 2021-03-02 op
164 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
165 5e11c00c 2021-03-02 op die();
166 5e11c00c 2021-03-02 op
167 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
168 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
169 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
170 0972d8b2 2021-03-02 op tab->redirect_count = 0;
171 5e11c00c 2021-03-02 op }
172 5e11c00c 2021-03-02 op
173 5e11c00c 2021-03-02 op static void
174 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
175 5e11c00c 2021-03-02 op {
176 0972d8b2 2021-03-02 op struct tab *tab;
177 0972d8b2 2021-03-02 op
178 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
179 0972d8b2 2021-03-02 op
180 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
181 0972d8b2 2021-03-02 op die();
182 5cd2ebb1 2021-03-11 op
183 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
184 0972d8b2 2021-03-02 op
185 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
186 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
187 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
188 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
189 5cd2ebb1 2021-03-11 op ui_require_input(tab, tab->code == 11);
190 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
191 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
192 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
193 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
194 c07ac996 2021-03-12 op } else {
195 c07ac996 2021-03-12 op load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
196 c07ac996 2021-03-12 op }
197 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
198 3a9b9365 2021-03-09 op tab->redirect_count++;
199 0972d8b2 2021-03-02 op
200 3a9b9365 2021-03-09 op /* TODO: make customizable? */
201 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
202 3a9b9365 2021-03-09 op load_page_from_str(tab,
203 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
204 5cd2ebb1 2021-03-11 op } else
205 2051e653 2021-03-13 op do_load_url(tab, tab->meta);
206 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
207 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
208 3a9b9365 2021-03-09 op }
209 5e11c00c 2021-03-02 op }
210 5e11c00c 2021-03-02 op
211 5e11c00c 2021-03-02 op static void
212 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
213 5e11c00c 2021-03-02 op {
214 0972d8b2 2021-03-02 op struct tab *tab;
215 5e11c00c 2021-03-02 op
216 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
217 5e11c00c 2021-03-02 op
218 0972d8b2 2021-03-02 op if (!tab->page.parse(&tab->page, imsg->data, datalen))
219 5e11c00c 2021-03-02 op die();
220 5e11c00c 2021-03-02 op
221 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
222 5e11c00c 2021-03-02 op }
223 5e11c00c 2021-03-02 op
224 5e11c00c 2021-03-02 op static void
225 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
226 5e11c00c 2021-03-02 op {
227 a5c3e03d 2021-03-02 op struct tab *t;
228 a5c3e03d 2021-03-02 op
229 a5c3e03d 2021-03-02 op t = tab_by_id(imsg->hdr.peerid);
230 a5c3e03d 2021-03-02 op if (!t->page.free(&t->page))
231 a5c3e03d 2021-03-02 op die();
232 a5c3e03d 2021-03-02 op
233 a5c3e03d 2021-03-02 op ui_on_tab_refresh(t);
234 8af5e5ed 2021-03-08 op ui_on_tab_loaded(t);
235 5e11c00c 2021-03-02 op }
236 5e11c00c 2021-03-02 op
237 5e11c00c 2021-03-02 op static void
238 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
239 740f578b 2021-03-15 op {
240 740f578b 2021-03-15 op int res;
241 740f578b 2021-03-15 op
242 740f578b 2021-03-15 op if (datalen != sizeof(res))
243 740f578b 2021-03-15 op die();
244 740f578b 2021-03-15 op
245 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
246 740f578b 2021-03-15 op if (res == 0)
247 740f578b 2021-03-15 op ui_notify("Added to bookmarks!");
248 740f578b 2021-03-15 op else
249 740f578b 2021-03-15 op ui_notify("Failed to add to bookmarks: %s",
250 740f578b 2021-03-15 op strerror(res));
251 740f578b 2021-03-15 op }
252 740f578b 2021-03-15 op
253 740f578b 2021-03-15 op static void
254 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
255 5e11c00c 2021-03-02 op {
256 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
257 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
258 5e11c00c 2021-03-02 op }
259 5e11c00c 2021-03-02 op
260 0972d8b2 2021-03-02 op static void
261 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
262 0972d8b2 2021-03-02 op {
263 0972d8b2 2021-03-02 op gemtext_initparser(&tab->page);
264 3a9b9365 2021-03-09 op if (!tab->page.parse(&tab->page, page, strlen(page)))
265 0972d8b2 2021-03-02 op die();
266 0972d8b2 2021-03-02 op if (!tab->page.free(&tab->page))
267 0972d8b2 2021-03-02 op die();
268 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
269 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
270 0972d8b2 2021-03-02 op }
271 0972d8b2 2021-03-02 op
272 bcb0b073 2021-03-07 op void
273 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
274 0972d8b2 2021-03-02 op {
275 2051e653 2021-03-13 op char *m;
276 2051e653 2021-03-13 op size_t len;
277 8af5e5ed 2021-03-08 op
278 10346511 2021-03-17 op tab->trust = TS_VERIFIED;
279 2051e653 2021-03-13 op
280 4d3785b1 2021-03-09 op memset(&tab->url, 0, sizeof(tab->url));
281 4d3785b1 2021-03-09 op
282 4d3785b1 2021-03-09 op m = strchr(url, ':');
283 4d3785b1 2021-03-09 op strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
284 4d3785b1 2021-03-09 op strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
285 097ff997 2021-03-09 op
286 2051e653 2021-03-13 op len = sizeof(tab->hist_cur->h)-1;
287 2051e653 2021-03-13 op strlcpy(tab->hist_cur->h, url, len);
288 4d3785b1 2021-03-09 op
289 35e1f40a 2021-03-14 op gemtext_initparser(&tab->page);
290 35e1f40a 2021-03-14 op
291 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
292 35e1f40a 2021-03-14 op tab->hist_cur->h, len+1);
293 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
294 4d3785b1 2021-03-09 op }
295 0972d8b2 2021-03-02 op
296 4d3785b1 2021-03-09 op void
297 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
298 4d3785b1 2021-03-09 op {
299 4d3785b1 2021-03-09 op const char *err;
300 4d3785b1 2021-03-09 op char *p;
301 2051e653 2021-03-13 op size_t len;
302 754622a2 2021-03-15 op
303 754622a2 2021-03-15 op len = sizeof(tab->hist_cur->h)-1;
304 3a9b9365 2021-03-09 op
305 4d3785b1 2021-03-09 op if (has_prefix(url, "gemini:")) {
306 4d3785b1 2021-03-09 op if (!url_parse(url, &tab->url, &err))
307 3a9b9365 2021-03-09 op goto err;
308 4d3785b1 2021-03-09 op } else {
309 4d3785b1 2021-03-09 op if (!url_resolve_from(&tab->url, url, &err))
310 4d3785b1 2021-03-09 op goto err;
311 3a9b9365 2021-03-09 op }
312 3a9b9365 2021-03-09 op
313 2051e653 2021-03-13 op url_unparse(&tab->url, tab->hist_cur->h, len);
314 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
315 2051e653 2021-03-13 op tab->hist_cur->h, len+1);
316 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
317 3a9b9365 2021-03-09 op return;
318 3a9b9365 2021-03-09 op
319 3a9b9365 2021-03-09 op err:
320 4d3785b1 2021-03-09 op if (asprintf(&p, "#error loading %s\n>%s\n",
321 4d3785b1 2021-03-09 op url, err) == -1)
322 3a9b9365 2021-03-09 op die();
323 2051e653 2021-03-13 op strlcpy(tab->hist_cur->h, url, len);
324 4d3785b1 2021-03-09 op load_page_from_str(tab, p);
325 4d3785b1 2021-03-09 op free(p);
326 0972d8b2 2021-03-02 op }
327 0972d8b2 2021-03-02 op
328 2051e653 2021-03-13 op static void
329 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
330 4d3785b1 2021-03-09 op {
331 4d3785b1 2021-03-09 op struct proto *p;
332 4d3785b1 2021-03-09 op
333 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
334 10346511 2021-03-17 op
335 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
336 4d3785b1 2021-03-09 op if (has_prefix(url, p->schema)) {
337 4d3785b1 2021-03-09 op p->loadfn(tab, url);
338 4d3785b1 2021-03-09 op return;
339 4d3785b1 2021-03-09 op }
340 4d3785b1 2021-03-09 op }
341 4d3785b1 2021-03-09 op
342 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
343 4d3785b1 2021-03-09 op }
344 4d3785b1 2021-03-09 op
345 9ad4627d 2021-03-10 op void
346 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
347 2051e653 2021-03-13 op {
348 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
349 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
350 2051e653 2021-03-13 op
351 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
352 2051e653 2021-03-13 op event_loopbreak();
353 2051e653 2021-03-13 op return;
354 2051e653 2021-03-13 op }
355 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
356 2051e653 2021-03-13 op do_load_url(tab, url);
357 2051e653 2021-03-13 op }
358 2051e653 2021-03-13 op
359 2051e653 2021-03-13 op int
360 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
361 2051e653 2021-03-13 op {
362 2051e653 2021-03-13 op struct hist *h;
363 2051e653 2021-03-13 op
364 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
365 2051e653 2021-03-13 op return 0;
366 2051e653 2021-03-13 op tab->hist_cur = h;
367 2051e653 2021-03-13 op do_load_url(tab, h->h);
368 2051e653 2021-03-13 op return 1;
369 2051e653 2021-03-13 op }
370 2051e653 2021-03-13 op
371 2051e653 2021-03-13 op int
372 2051e653 2021-03-13 op load_next_page(struct tab *tab)
373 2051e653 2021-03-13 op {
374 2051e653 2021-03-13 op struct hist *h;
375 2051e653 2021-03-13 op
376 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
377 2051e653 2021-03-13 op return 0;
378 2051e653 2021-03-13 op tab->hist_cur = h;
379 2051e653 2021-03-13 op do_load_url(tab, h->h);
380 2051e653 2021-03-13 op return 1;
381 2051e653 2021-03-13 op }
382 2051e653 2021-03-13 op
383 2051e653 2021-03-13 op void
384 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
385 9ad4627d 2021-03-10 op {
386 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
387 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
388 9ad4627d 2021-03-10 op }
389 9ad4627d 2021-03-10 op
390 740f578b 2021-03-15 op void
391 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
392 740f578b 2021-03-15 op {
393 740f578b 2021-03-15 op imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
394 740f578b 2021-03-15 op imsg_flush(fsibuf);
395 740f578b 2021-03-15 op }
396 740f578b 2021-03-15 op
397 5e11c00c 2021-03-02 op int
398 5e11c00c 2021-03-02 op main(void)
399 5e11c00c 2021-03-02 op {
400 35e1f40a 2021-03-14 op struct imsgbuf network_ibuf, fs_ibuf;
401 35e1f40a 2021-03-14 op int net_fds[2], fs_fds[2];
402 35e1f40a 2021-03-14 op pid_t pid;
403 5e11c00c 2021-03-02 op
404 35e1f40a 2021-03-14 op pid = getpid();
405 35e1f40a 2021-03-14 op
406 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
407 5e11c00c 2021-03-02 op
408 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
409 5e11c00c 2021-03-02 op err(1, "socketpair");
410 5e11c00c 2021-03-02 op
411 5e11c00c 2021-03-02 op switch (fork()) {
412 5e11c00c 2021-03-02 op case -1:
413 5e11c00c 2021-03-02 op err(1, "fork");
414 5e11c00c 2021-03-02 op case 0:
415 5e11c00c 2021-03-02 op /* child */
416 35e1f40a 2021-03-14 op setproctitle("(%d) fs", pid);
417 35e1f40a 2021-03-14 op close(fs_fds[0]);
418 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[1]);
419 35e1f40a 2021-03-14 op exit(fs_main(&fs_ibuf));
420 35e1f40a 2021-03-14 op default:
421 35e1f40a 2021-03-14 op close(fs_fds[1]);
422 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[0]);
423 35e1f40a 2021-03-14 op fsibuf = &fs_ibuf;
424 5e11c00c 2021-03-02 op }
425 b1d4d01b 2021-03-14 op
426 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
427 35e1f40a 2021-03-14 op err(1, "socketpair");
428 5e11c00c 2021-03-02 op
429 35e1f40a 2021-03-14 op switch (fork()) {
430 35e1f40a 2021-03-14 op case -1:
431 35e1f40a 2021-03-14 op err(1, "fork");
432 35e1f40a 2021-03-14 op case 0:
433 35e1f40a 2021-03-14 op /* child */
434 35e1f40a 2021-03-14 op setproctitle("(%d) client", pid);
435 35e1f40a 2021-03-14 op close(net_fds[0]);
436 35e1f40a 2021-03-14 op close(fs_fds[0]);
437 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[1]);
438 35e1f40a 2021-03-14 op exit(client_main(&network_ibuf));
439 35e1f40a 2021-03-14 op default:
440 35e1f40a 2021-03-14 op close(net_fds[1]);
441 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[0]);
442 35e1f40a 2021-03-14 op netibuf = &network_ibuf;
443 35e1f40a 2021-03-14 op }
444 5e11c00c 2021-03-02 op
445 35e1f40a 2021-03-14 op setproctitle("(%d) ui", pid);
446 35e1f40a 2021-03-14 op
447 cbcc75fb 2021-03-17 op telescope_ohash_init(&certs, 5, offsetof(struct tofu_entry, domain));
448 cbcc75fb 2021-03-17 op
449 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
450 5e11c00c 2021-03-02 op
451 5e11c00c 2021-03-02 op event_init();
452 5e11c00c 2021-03-02 op
453 1304bbdd 2021-03-15 op event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
454 35e1f40a 2021-03-14 op event_add(&netev, NULL);
455 5e11c00c 2021-03-02 op
456 1304bbdd 2021-03-15 op event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
457 35e1f40a 2021-03-14 op event_add(&fsev, NULL);
458 35e1f40a 2021-03-14 op
459 5e11c00c 2021-03-02 op ui_init();
460 5e11c00c 2021-03-02 op
461 b1d4d01b 2021-03-14 op sandbox_ui_process();
462 b1d4d01b 2021-03-14 op
463 5e11c00c 2021-03-02 op event_dispatch();
464 5e11c00c 2021-03-02 op
465 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
466 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
467 5e11c00c 2021-03-02 op
468 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
469 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
470 35e1f40a 2021-03-14 op
471 5e11c00c 2021-03-02 op ui_end();
472 5e11c00c 2021-03-02 op
473 5e11c00c 2021-03-02 op return 0;
474 5e11c00c 2021-03-02 op }