summary refs log tree commit diff
path: root/net.c
blob: 6fbfa970f1bbd7ec9710bb9726af06eac5a12343 (plain)
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
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>

#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>

#include "net.h"
#include "err.h"
#include "doc.h"

int net_addr(const char *url, struct addr *adr, enum protocol prot_default) {
	char *prot_mark;
	if ((prot_mark = strstr(url, "://"))) {
		static struct {
			const char *str;
			enum protocol prot;
		} prot_str_tbl[] = {
			{ "gopher", PROT_GOPHER },
			{ "gemini", PROT_GEMINI },
			{ "file", PROT_FILE },
		};
		size_t n = prot_mark - url;
		adr->prot = PROT_UNKNOWN;
		for (size_t i = 0; i < sizeof prot_str_tbl / sizeof *prot_str_tbl; i++) {
			if (!strncmp(prot_str_tbl[i].str, url, n)) {
				adr->prot = prot_str_tbl[i].prot;
				break;
			}
		}
		url = prot_mark + 3;
	} else {
		adr->prot = prot_default;
	}

	if (adr->prot == PROT_FILE) {
		adr->host_len = 0;
		size_t n = strlen(url);
		if (n >= PATH_MAX) return -1;
		adr->path_len = n;
		memcpy(adr->path, url, n + 1);
	} else {
		adr->host_len = 0;
		while (*url && *url != '/' && adr->host_len < HOST_MAX) {
			adr->host[adr->host_len++] = *url++;
		}
		adr->host[adr->host_len] = 0;

		char *colon = strchr(adr->host, ':');
		if (colon) {
			errno = 0;
			int port = strtoul(colon, NULL, 10);
			if (errno) {
				return -1;
			} else {
				adr->port = port;
			}
			*colon = 0;
			adr->host_len = colon - adr->host;
		} else {
			switch (adr->prot) {
			case PROT_GOPHER:
				adr->port = 70;
				break;
			default:
				adr->port = -1;
				break;
			}
		}

		if (*url && *url != '/') {
			perr("hostname too long");
			return -1;
		}
		if (*url) url++;
		adr->path_len = 0;
		while (*url && adr->path_len < HOST_MAX) {
			adr->path[adr->path_len++] = *url++;
		}
		adr->path[adr->path_len] = 0;
		if (*url) {
			perr("path too long");
			return -1;
		}
	}

	return 0;
}

static long
net_hosttoaddr(const char *hostname)
{
	struct hostent *h;
	struct in_addr **al;
	h = gethostbyname(hostname);
	if (!h) {
		perr("host not found");
		return -1;
	}
	al = (struct in_addr **)h->h_addr_list;
	if (!al || !(*al))
		perr("empty host address");
	return (*al)->s_addr;
}

static int
net_conntohost(const char *hostname, int port)
{
	struct sockaddr_in sai;
	int s;
	s = socket(AF_INET, SOCK_STREAM, 0);
	if (!s)
		return -1;
	sai.sin_family = AF_INET;
	sai.sin_port = htons(port);
	sai.sin_addr.s_addr = net_hosttoaddr(hostname);
	if (connect(s, (struct sockaddr *)&sai, sizeof(sai)))
		return -1;
	return s;
}

static int fetch_file(const struct addr *adr, struct buf *buf, enum doc_type *doct) {
	FILE *f = fopen(adr->path, "r/o");
	if (!f) {
		perr("file not found");
		return -1;
	}
	buf_init(buf, 1024);
	char b[256];
	while (fgets(b, sizeof b, f)) {
		buf_cat(buf, b, strlen(b));
	}
	buf_catc(buf, 0);
	fclose(f);
	*doct = DOC_PLAIN;
	return 0;
}

static int fetch_gopher(const struct addr *adr, struct buf *buf, enum doc_type *doct) {
	const char *sel = adr->path;
	size_t sel_len = adr->path_len;
	if (sel_len > 0 && sel[0] == '/') {
		sel_len--;
		sel++;
	}
	if (sel_len > 0) {
		switch (*sel) {
		case '0':
			*doct = DOC_PLAIN;
			break;
		case '1':
			*doct = DOC_GOPHERMAP;
			break;
		default:
			perr("invalid item type");
			return -1;
		}
		sel++;
		sel_len--;
	} else {
		*doct = DOC_GOPHERMAP;
	}
	int s = net_conntohost(adr->host, adr->port);
	if (s < 0) {
		return -1;
	}
	if (write(s, sel, sel_len) < 0
	 || write(s, "\r\n", 2) < 0) {
		perr("write failure");
		return -1;
	}
	buf_init(buf, 64);
	char inbuf[256];
	for (;;) {
		ssize_t n = read(s, inbuf, sizeof inbuf);
		if (n < 0) {
			perr("read error");
			buf_free(buf);
			close(s);
			return -1;
		}
		if (!n) break;
		buf_cat(buf, inbuf, n);
	}
	close(s);
	return 0;
}

int net_fetch(const struct addr *adr, struct buf *buf, enum doc_type *doct) {
	switch (adr->prot) {
	case PROT_FILE:
		return fetch_file(adr, buf, doct);
	case PROT_GOPHER:
		return fetch_gopher(adr, buf, doct);
	default:
		perr("unsupported protocol");
		return -1;
	}
}