diff options
Diffstat (limited to 'c/wget.c')
| -rw-r--r-- | c/wget.c | 41 |
1 files changed, 19 insertions, 22 deletions
@@ -1,39 +1,36 @@ -#include "sock.h" -/* wget [A.B.C.D] - fetch http://TARGET/ over the kernel TCP socket layer and - print it. No SLIP/IP/TCP here: connect() handshakes in the kernel, send()/ - recv() drive the stream. Default target 10.0.0.1:80 (the SLIP peer/host). */ +#include "resolve.h" +/* wget HOST|IP - fetch http://HOST/ over the kernel TCP socket layer and print + it. Resolves a hostname via DNS first (resolve.h), then connect()/send()/ + recv() over TCP - all the SLIP/IP/UDP/TCP lives in the kernel. */ -static unsigned char buf[220]; /* first + large so dst[] lands arg-safe */ +static unsigned char buf[220]; /* first + large so namebuf/dst land arg-safe */ +static unsigned char namebuf[64]; static unsigned char dst[4]; -static unsigned char parse_ip(char *s, unsigned char *out) { - unsigned char i, v; - for (i = 0; i < 4; i++) { - if (*s < '0' || *s > '9') return 0; - v = 0; - while (*s >= '0' && *s <= '9') { v = (unsigned char)(v * 10 + (*s - '0')); s++; } - out[i] = v; - if (i < 3) { if (*s != '.') return 0; s++; } - } - return 1; -} - void main(void) { char *arg, *r; unsigned char s, n, i; arg = getargs(); - if (!arg || !*arg) { dst[0] = 10; dst[1] = 0; dst[2] = 0; dst[3] = 1; } - else if (!parse_ip(arg, dst)) { puts("wget: bad address"); nl(); sexit(1); } + if (!arg || !*arg) { puts("usage: wget HOST|IP"); nl(); sexit(1); } + for (i = 0; arg[i] && i < 63; i++) namebuf[i] = (unsigned char)arg[i]; + namebuf[i] = 0; + + if (!resolve((char *)namebuf, dst)) { + puts("wget: cannot resolve "); puts((char *)namebuf); nl(); sexit(1); + } + puts("connecting "); for (i = 0; i < 4; i++) { putu(dst[i]); if (i < 3) putc('.'); } nl(); s = net_socket(SOCK_TCP); if (s == 0xFF) { puts("wget: no socket"); nl(); sexit(1); } net_bind(s, 40001); - puts("connecting"); nl(); if (net_connect(s, dst, 80) == 0xFF) { puts("wget: connect failed"); nl(); net_close(s); sexit(1); } - r = "GET / HTTP/1.0\r\nHost: gbos\r\n\r\n"; + /* GET / HTTP/1.0 with the requested host as the Host header */ + r = "GET / HTTP/1.0\r\nHost: "; n = 0; - while (r[n]) { buf[n] = r[n]; n++; } + while (r[n]) { buf[n] = (unsigned char)r[n]; n++; } + for (i = 0; namebuf[i]; i++) buf[n++] = namebuf[i]; + buf[n++] = '\r'; buf[n++] = '\n'; buf[n++] = '\r'; buf[n++] = '\n'; net_send(s, buf, n); for (;;) { |
