1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netdb.h>
#define ARENA_IMPL
#define STR_IMPL
#include "dynarr.h"
#include "arena.h"
#include "str.h"
/* address cache */
#define ADDR_MAP_MAX 128
#define ADDR_HOST_MAX 128
typedef struct {
char host[ADDR_HOST_MAX];
int hostn, port;
struct addrinfo *ai;
} AddrMap;
static AddrMap addr_mapv[ADDR_MAP_MAX];
static int addr_mapc = 0;
Str
uint_to_str(unsigned src, Arena *a)
{
Str r = { 0 };
do {
str_catc(&r, (src % 10) + '0', a);
src /= 10;
} while(src);
for (int i = 0, j = r.n - 1; i < j; i++, j--) {
char t = r.s[i];
r.s[i] = r.s[j];
r.s[j] = t;
}
return r;
}
struct addrinfo *
addr_get(Str host, int port, Arena *scratch)
{
if (host.n > ADDR_HOST_MAX) {
fprintf(stderr, "hostname too long\n");
return NULL;
}
/* TODO: maybe move fetched addr to front of table? */
for (int i = 0; i < addr_mapc; i++) {
AddrMap *m = &addr_mapv[i];
if (m->port != port) continue;
if (str_eql((Str) { m->host, m->hostn }, host))
return m->ai;
}
puts("lookup");
struct addrinfo *ai;
int e = getaddrinfo(str_to_cstr(host, scratch),
str_to_cstr(uint_to_str(port, scratch), scratch),
&(struct addrinfo) {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
.ai_protocol = IPPROTO_TCP,
.ai_flags = AI_NUMERICSERV
}, &ai);
if (e) {
fprintf(stderr, "bad address: %s\n", gai_strerror(e));
return NULL;
}
if (addr_mapc == ADDR_MAP_MAX) {
freeaddrinfo(addr_mapv[0].ai);
for (int i = 1; i < ADDR_MAP_MAX; i++)
addr_mapv[i-1] = addr_mapv[i];
addr_mapc--;
}
AddrMap *m = &addr_mapv[addr_mapc++];
memcpy(m->host, host.s, host.n);
m->hostn = host.n;
m->port = port;
m->ai = ai;
return ai;
}
void
addr_init(void)
{
/* do nothing */
}
void
addr_fini(void)
{
for (int i = 0; i < addr_mapc; i++)
freeaddrinfo(addr_mapv[i].ai);
}
int
addr_conn(Str host, int port, Arena *scratch)
{
struct addrinfo *ai = addr_get(host, port, scratch);
if (!ai)
return -1;
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1) {
fprintf(stderr, "couldn't create socket\n");
return -1;
}
if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
fprintf(stderr, "couldn't connect to host\n");
close(s);
return -1;
}
return s;
}
/* url parsing */
#define PROT_DEFAULT PROT_GOPHER
typedef enum {
PROT_UNKNOWN,
PROT_GOPHER,
PROT_FILE,
} Proto;
typedef enum {
GI_TEXT,
GI_MENU,
GI_SEARCH,
GI_UNSUPPORTED,
} GopherItemType;
typedef struct {
Str host, path;
Proto proto;
int port;
} Request;
int
parse_port(Str s) {
int x = 0;
for (int i = 0; i < s.n; i++) {
if (s.s[i] < '0' || s.s[i] > '9') return -1;
if (x >= INT_MAX / 10 - 1) return -1;
x = (x * 10) + s.s[i] - '0';
}
return x;
}
int
parse_request(Str url, Request *req)
{
int i;
static struct { Str s; Proto p; int port; } prot_tbl[] = {
{ Ss("gopher"), PROT_GOPHER, 70 },
{ Ss("file"), PROT_FILE, 0 },
};
/* protocol */
req->proto = PROT_UNKNOWN;
Cut cprot = str_cuts(url, S("://"));
if (cprot.tail.n > 0) {
for (i = 0; i < sizeof prot_tbl / sizeof *prot_tbl; i++) {
if (str_eql(cprot.head, prot_tbl[i].s)) {
req->proto = prot_tbl[i].p;
req->port = prot_tbl[i].port;
}
}
url = cprot.tail;
switch (req->proto) {
case PROT_UNKNOWN:
fprintf(stderr, "bad protocol\n");
return -1;
case PROT_FILE:
req->path = url;
return 0;
default:
break;
}
} else {
req->proto = PROT_DEFAULT;
}
/* host & port */
Cut chost = str_cut(url, '/');
Cut cport = str_cut(chost.head, ':');
req->path = chost.tail;
req->host = cport.head;
if (cport.tail.n > 0) {
req->port = parse_port(cport.tail);
if (req->port == -1) {
fprintf(stderr, "bad port\n");
return -1;
}
}
return 0;
}
/* documents */
typedef enum {
DOC_TEXT,
DOC_GOPHERMAP,
DOC_ERROR,
DOC_UNKNOWN
} DocType;
typedef struct DocLine {
struct DocLine *next, *prev;
Str s;
} DocLine;
typedef struct {
Arena arena;
DocType type;
Str src;
DocLine *head, *tail;
} Doc;
/* document parsing */
DocLine *
doc_push_line(Doc *d, Str s)
{
DocLine *n = new(&d->arena, DocLine);
n->s = s;
n->prev = d->tail;
if (d->tail) d->tail->next = n;
if (!d->head) d->head = n;
d->tail = n;
return n;
}
int
parse_text(Doc *d)
{
Str s = d->src;
while (s.n > 0) {
Cut c = str_cut(s, '\n');
doc_push_line(d, c.head);
s = c.tail;
}
return 0;
}
int
parse_doc(Doc *d)
{
switch (d->type) {
case DOC_TEXT:
return parse_text(d);
case DOC_GOPHERMAP:
doc_push_line(d, S("Gophermaps are unimplemented"));
return -1;
case DOC_ERROR:
doc_push_line(d, S("Error"));
return -1;
default:
break;
}
doc_push_line(d, S("Unknown document type"));
return -1;
}
/* document fetching */
ssize_t
write_str(int fd, Str s)
{
return write(fd, s.s, s.n);
}
Str
read_all(int fd, Arena *a)
{
DYNARR(char) r = { 0 };
for (;;) {
DA_AFIT(&r, a, r.n + 1024);
ssize_t n = read(fd, r.v + r.n, r.c_ - r.n);
if (n <= 0) break;
r.n += n;
}
return (Str) { r.v, r.n };
}
DocType
fetch_gopher(Str *buf, Request req, Arena *perm, Arena *scratch)
{
DocType t = DOC_UNKNOWN;
if (req.path.n == 0) {
t = DOC_GOPHERMAP;
} else {
switch (req.path.s[0]) {
case '0': t = DOC_TEXT; break;
case '1': t = DOC_GOPHERMAP; break;
default:
/* don't bother fetching */
*buf = S("");
return DOC_UNKNOWN;
}
req.path = str_skip(req.path, 1);
}
int s = addr_conn(req.host, req.port, scratch);
if (s == -1) return DOC_ERROR;
write_str(s, req.path);
write_str(s, S("\r\n"));
*buf = read_all(s, perm);
close(s);
return t;
}
DocType
fetch_file(Str *buf, Request req, Arena *perm, Arena *scratch)
{
int f = open(str_to_cstr(req.path, scratch), O_RDONLY);
if (f == -1) return DOC_ERROR;
*buf = read_all(f, perm);
close(f);
return DOC_TEXT;
}
DocType
fetch(Str *buf, Request req, Arena *perm, Arena *scratch)
{
switch (req.proto) {
case PROT_GOPHER:
return fetch_gopher(buf, req, perm, scratch);
case PROT_FILE:
return fetch_file(buf, req, perm, scratch);
default:
*buf = S("");
fprintf(stderr, "unknown type to fetch file\n");
return DOC_ERROR;
}
}
void
term_size(int *rows, int *cols)
{
struct winsize sz = { 0 };
ioctl(STDOUT_FILENO, TIOCGWINSZ, &sz);
if (rows) *rows = sz.ws_row;
if (cols) *cols = sz.ws_col;
}
int
main(void)
{
Arena scratch = { 0 };
Request req = { 0 };
addr_init();
printf("parse_request() -> %d\n", parse_request(S("gopher://tilde.town"), &req));
printf("host = %.*s\n", (int)req.host.n, req.host.s);
printf("path = %.*s\n", (int)req.path.n, req.path.s);
printf("prot = %d\n", req.proto);
printf("port = %d\n", req.port);
Str buf = { 0 };
DocType t = fetch(&buf, req, &scratch, &scratch);
printf("fetch() -> %d\n", t);
Doc doc = { .arena = scratch, .src = buf, .type = t };
printf("parse() -> %d\n", parse_doc(&doc));
DocLine *l = doc.head;
while (l) {
int ln;
term_size(&ln, NULL);
for (int i = 0; l && i < ln - 1; i++) {
printf("%.*s\n", (int)l->s.n, l->s.s);
l = l->next;
}
char buf[1024] = { 0 };
printf("* ");
fflush(stdout);
fgets(buf, 1023, stdin);
if (buf[0] == 'q') break;
}
puts("Goodbye!");
addr_fini();
return 0;
}
|