3 #include <sys/socket.h>
13 struct event netev, fsev;
14 struct tabshead tabshead;
16 /* the first is also the fallback one */
17 static struct proto protos[] = {
18 { "gemini:", load_gemini_url },
19 { "about:", load_about_url },
23 static struct imsgbuf *netibuf, *fsibuf;
25 static void die(void) __attribute__((__noreturn__));
26 static struct tab *tab_by_id(uint32_t);
27 static void handle_imsg_err(struct imsg*, size_t);
28 static void handle_imsg_check_cert(struct imsg*, size_t);
29 static void handle_imsg_got_code(struct imsg*, size_t);
30 static void handle_imsg_got_meta(struct imsg*, size_t);
31 static void handle_imsg_buf(struct imsg*, size_t);
32 static void handle_imsg_eof(struct imsg*, size_t);
33 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
34 static void handle_dispatch_imsg(int, short, void*);
35 static void load_page_from_str(struct tab*, const char*);
36 static void do_load_url(struct tab*, const char*);
38 static imsg_handlerfn *handlers[] = {
39 [IMSG_ERR] = handle_imsg_err,
40 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
41 [IMSG_GOT_CODE] = handle_imsg_got_code,
42 [IMSG_GOT_META] = handle_imsg_got_meta,
43 [IMSG_BUF] = handle_imsg_buf,
44 [IMSG_EOF] = handle_imsg_eof,
45 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
48 static struct ohash certs;
50 static void __attribute__((__noreturn__))
57 tab_by_id(uint32_t id)
61 TAILQ_FOREACH(t, &tabshead, tabs) {
70 handle_imsg_err(struct imsg *imsg, size_t datalen)
75 tab = tab_by_id(imsg->hdr.peerid);
78 page[datalen-1] = '\0';
80 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
81 tab->hist_cur->h, page) == -1)
83 load_page_from_str(tab, page);
88 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
96 if (hash[datalen-1] != '\0')
99 tab = tab_by_id(imsg->hdr.peerid);
101 if ((e = telescope_lookup_tofu(&certs, tab->url.host)) == NULL) {
102 /* TODO: an update in libressl/libretls changed
103 * significantly. Find a better approach at storing
105 if (datalen > sizeof(e->hash))
108 tofu_res = 1; /* trust on first use */
109 if ((e = calloc(1, sizeof(*e))) == NULL)
111 strlcpy(e->domain, tab->url.host, sizeof(e->domain));
112 strlcpy(e->hash, hash, sizeof(e->hash));
113 telescope_ohash_insert(&certs, e);
115 tofu_res = !strcmp(hash, e->hash);
118 tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
120 tab->trust = TS_UNTRUSTED;
121 load_page_from_str(tab, "# Certificate mismatch\n");
123 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
124 &tofu_res, sizeof(tofu_res));
129 normalize_code(int n)
132 if (n == 10 || n == 11)
138 if (n == 30 || n == 31)
146 if (n <= 53 || n == 59)
154 return MALFORMED_RESPONSE;
158 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
162 tab = tab_by_id(imsg->hdr.peerid);
164 if (sizeof(tab->code) != datalen)
167 memcpy(&tab->code, imsg->data, sizeof(tab->code));
168 tab->code = normalize_code(tab->code);
169 if (tab->code != 30 && tab->code != 31)
170 tab->redirect_count = 0;
174 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
178 tab = tab_by_id(imsg->hdr.peerid);
180 if (sizeof(tab->meta) <= datalen)
183 memcpy(tab->meta, imsg->data, datalen);
185 if (tab->code < 10) { /* internal errors */
186 load_page_from_str(tab, err_pages[tab->code]);
187 } else if (tab->code < 20) { /* 1x */
188 load_page_from_str(tab, err_pages[tab->code]);
189 ui_require_input(tab, tab->code == 11);
190 } else if (tab->code == 20) {
191 if (setup_parser_for(tab)) {
192 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
195 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
197 } else if (tab->code < 40) { /* 3x */
198 tab->redirect_count++;
200 /* TODO: make customizable? */
201 if (tab->redirect_count > 5) {
202 load_page_from_str(tab,
203 err_pages[TOO_MUCH_REDIRECTS]);
205 do_load_url(tab, tab->meta);
206 } else { /* 4x, 5x & 6x */
207 load_page_from_str(tab, err_pages[tab->code]);
212 handle_imsg_buf(struct imsg *imsg, size_t datalen)
216 tab = tab_by_id(imsg->hdr.peerid);
218 if (!tab->page.parse(&tab->page, imsg->data, datalen))
221 ui_on_tab_refresh(tab);
225 handle_imsg_eof(struct imsg *imsg, size_t datalen)
229 t = tab_by_id(imsg->hdr.peerid);
230 if (!t->page.free(&t->page))
233 ui_on_tab_refresh(t);
238 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
242 if (datalen != sizeof(res))
245 memcpy(&res, imsg->data, sizeof(res));
247 ui_notify("Added to bookmarks!");
249 ui_notify("Failed to add to bookmarks: %s",
254 handle_dispatch_imsg(int fd, short ev, void *d)
256 struct imsgbuf *ibuf = d;
257 dispatch_imsg(ibuf, handlers, sizeof(handlers));
261 load_page_from_str(struct tab *tab, const char *page)
263 gemtext_initparser(&tab->page);
264 if (!tab->page.parse(&tab->page, page, strlen(page)))
266 if (!tab->page.free(&tab->page))
268 ui_on_tab_refresh(tab);
269 ui_on_tab_loaded(tab);
273 load_about_url(struct tab *tab, const char *url)
278 tab->trust = TS_VERIFIED;
280 memset(&tab->url, 0, sizeof(tab->url));
282 m = strchr(url, ':');
283 strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
284 strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
286 len = sizeof(tab->hist_cur->h)-1;
287 strlcpy(tab->hist_cur->h, url, len);
289 gemtext_initparser(&tab->page);
291 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
292 tab->hist_cur->h, len+1);
297 load_gemini_url(struct tab *tab, const char *url)
303 len = sizeof(tab->hist_cur->h)-1;
305 if (has_prefix(url, "gemini:")) {
306 if (!url_parse(url, &tab->url, &err))
309 if (!url_resolve_from(&tab->url, url, &err))
313 url_unparse(&tab->url, tab->hist_cur->h, len);
314 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
315 tab->hist_cur->h, len+1);
320 if (asprintf(&p, "#error loading %s\n>%s\n",
323 strlcpy(tab->hist_cur->h, url, len);
324 load_page_from_str(tab, p);
329 do_load_url(struct tab *tab, const char *url)
333 tab->trust = TS_UNKNOWN;
335 for (p = protos; p->schema != NULL; ++p) {
336 if (has_prefix(url, p->schema)) {
342 protos[0].loadfn(tab, url);
346 load_url(struct tab *tab, const char *url)
348 if (tab->hist_cur != NULL)
349 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
351 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
355 hist_push(&tab->hist, tab->hist_cur);
356 do_load_url(tab, url);
360 load_previous_page(struct tab *tab)
364 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
367 do_load_url(tab, h->h);
372 load_next_page(struct tab *tab)
376 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
379 do_load_url(tab, h->h);
384 stop_tab(struct tab *tab)
386 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
391 add_to_bookmarks(const char *str)
393 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
400 struct imsgbuf network_ibuf, fs_ibuf;
401 int net_fds[2], fs_fds[2];
406 signal(SIGCHLD, SIG_IGN);
408 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
409 err(1, "socketpair");
416 setproctitle("(%d) fs", pid);
418 imsg_init(&fs_ibuf, fs_fds[1]);
419 exit(fs_main(&fs_ibuf));
422 imsg_init(&fs_ibuf, fs_fds[0]);
426 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
427 err(1, "socketpair");
434 setproctitle("(%d) client", pid);
437 imsg_init(&network_ibuf, net_fds[1]);
438 exit(client_main(&network_ibuf));
441 imsg_init(&network_ibuf, net_fds[0]);
442 netibuf = &network_ibuf;
445 setproctitle("(%d) ui", pid);
447 telescope_ohash_init(&certs, 5, offsetof(struct tofu_entry, domain));
449 TAILQ_INIT(&tabshead);
453 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
454 event_add(&netev, NULL);
456 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
457 event_add(&fsev, NULL);
461 sandbox_ui_process();
465 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
468 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);