diff --git a/Makefile b/Makefile --- a/Makefile +++ b/Makefile @@ -41,13 +41,13 @@ SRCS+= loadfile.c loadfile_aout.c loadfile_ecoff.c loadfile_elf32.c \ .if (${SA_INCLUDE_NET} == "yes") # network routines -SRCS+= arp.c ether.c ether_sprintf.c ip_cksum.c net.c netif.c rpc.c udp.c +SRCS+= arp.c ether.c ether_sprintf.c ip_cksum.c net.c netif.c rpc.c udp.c ip.c tcp.c # network info services: SRCS+= bootp.c rarp.c bootparam.c # boot filesystems -SRCS+= nfs.c tftp.c +SRCS+= nfs.c tftp.c http.c .endif SRCS+= ffsv1.c ffsv2.c ufs_ls.c diff --git a/bootp.c b/bootp.c --- a/bootp.c +++ b/bootp.c @@ -96,11 +96,11 @@ bootp(int sock) struct iodesc *d; struct bootp *bp; struct { - u_char header[HEADER_SIZE]; + u_char header[UDP_TOTAL_HEADER_SIZE]; struct bootp wbootp; } wbuf; struct { - u_char header[HEADER_SIZE]; + u_char header[UDP_TOTAL_HEADER_SIZE]; struct bootp rbootp; } rbuf; #ifdef SUPPORT_DHCP diff --git a/dev_net.c b/dev_net.c --- a/dev_net.c +++ b/dev_net.c @@ -242,7 +242,6 @@ net_getparams(int sock) if (debug) printf("client name: %s\n", hostname); #endif - /* * Ignore the gateway from whoami (unreliable). * Use the "gateway" parameter instead. diff --git a/ether.c b/ether.c --- a/ether.c +++ b/ether.c @@ -118,7 +118,6 @@ readether(struct iodesc *d, void *pkt, size_t len, time_t tleft, return -1; } *etype = ntohs(eh->ether_type); - n -= sizeof(*eh); return n; } diff --git a/http.c b/http.c --- /dev/null +++ b/http.c @@ -0,0 +1,325 @@ +/* $NetBSD$ */ + +/* + * Copyright (c) 2010 Zoltan Arnold NAGY + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef _STANDALONE +#include +#else +#include +#endif + +#include "stand.h" +#include "net.h" +#include "http.h" + +#define STRNDUP(dst, src, x) \ + dst = (char *)alloc(x + 1); \ + memset(dst, 0, x + 1); \ + memcpy(dst, src, x); + +#define STRDUP(dst, src) \ + dst = (char *)alloc(strlen(src) + 1); \ + memset(dst, 0, strlen(src) + 1); \ + memcpy(dst, src, strlen(src)); + +#define APPEND(dst, text, ...) \ + sprintf(dst, "%s"text, dst, __VA_ARGS__) + +extern struct in_addr servip; +extern struct in_addr gateip; + +struct http_handle { + struct iodesc *desc; + char *auth; + char *path; + char *cmd; + int currpos; +}; + +static void *buffer; +static int port = 0; + +#define BUFFERSIZE 4096 +#define SPORT 9921 + +static int +get(struct open_file * f, size_t startpos, void *dst, size_t size) +{ + struct http_handle *h = (struct http_handle *) f->f_fsdata; + h->desc->myport = htons(SPORT + port + (getsecs() & 0x3ff)); + tcp_connect(h->desc); + memset(h->cmd, 0, 256); + sprintf(h->cmd, "GET %s HTTP/1.0\r\n", h->path); + if (h->auth != NULL) + APPEND(h->cmd, "Authorization: Basic %s\r\n", h->auth); + APPEND(h->cmd, "Range: bytes=%u-%u\r\n", startpos, startpos + size - 1); + APPEND(h->cmd, "Connection: close%s", "\r\n\r\n"); + sendtcp(h->desc, h->cmd, strlen(h->cmd)); + + struct { + u_char ethernet[ETHERNET_HEADER_SIZE + IP_HEADER_SIZE + + TCP_HEADER_SIZE]; + u_char payload[1600]; + } r; + + int hasread = 0; + int stripHeader = 1; + while (1) { + ssize_t res = readtcp(h->desc, &(r.payload), 1600, 1); + if (res == -2) + break; + else if (res == -1) { + continue; + } else { + char *x = (char *) (&r.payload); + if (stripHeader == 1) { + int i = 0; + for (i = 3; i < res - 3; i++) + if ((*(x + i) == '\n') && + (*(x + i - 1) == '\r') && + (*(x + i - 2) == '\n') && + (*(x + i - 3) == '\r')) { + x += i + 1; + res -= i + 1; + break; + } + stripHeader = 0; + } + memcpy(dst + hasread, x, res); + hasread += res; + } + } + port++; + return hasread; +} + +const char *base64table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + +/* + * chops a 24 bit long triple into four 6-byte parts, + * then encodes these parts using base64table + * + * if the input is less than 24 bit, then the last + * partial 6-bit part is padded with zeroes on the right, + * and '=' is substituted for the missing parts. + */ +static void +encode_triplet(const char *src, size_t length, char *dst) +{ + if (length >= 3) { + *dst = base64table[(src[0] & 0xFC) >> 2]; + *(dst + 1) = base64table[((src[0] & 3) << 4) | (src[1] >> 4)]; + *(dst + 2) = base64table[((src[1] & 15) << 2) | (src[2] >> 6)]; + *(dst + 3) = base64table[src[2] & 63]; + } else if (length == 2) { + *dst = base64table[(src[0] & 0xFC) >> 2]; + *(dst + 1) = base64table[((src[0] & 3) << 4) | (src[1] >> 4)]; + *(dst + 2) = base64table[((src[1] & 15) << 2)]; + *(dst + 3) = '='; + } else if (length == 1) { + *dst = base64table[(src[0] & 0xFC) >> 2]; + *(dst + 1) = base64table[((src[0] & 3) << 4)]; + *(dst + 2) = '='; + *(dst + 3) = '='; + } +} +/* + * encodes the source string into base64 + * the caller must have preallocated dst + * see RFC 4648 for details + */ +static void +encode(const char *src, char *dst) +{ + int length = strlen(src); + while (length > 0) { + encode_triplet(src, length, dst); + src += 3; + dst += 4; + length -= 3; + } +} + +/* + * checks if the supplied address is a valid IP address +*/ +static int +validip(const char *ip) +{ + struct in_addr addr; + return (inet_aton(ip, &addr) != 0); +} + +/* + * Extract information from the specified URL, and + * supply that to the handle. + * + * The general format is: + * http://username:password@host/path + * + * Currently, only IP addresses are accepted as hosts. + * + * The username+password is encoded into base64, and stored + * into the "auth" field of the handle. + */ +static int +parseurl(const char *url, struct http_handle * h) +{ + /* the minimal valid url is 1.1.1.1 */ + if ((url == NULL) || (strlen(url) < 7)) + return 0; + + /* the http:// designation can be omitted */ + if (strncmp(url, "http://", 7) == 0) + url += 7; + + char *fsep = strchr(url, '/'); + if (fsep == NULL) { + if (!validip(url)) + return -1; + inet_aton(url, &h->desc->destip); + STRDUP(h->path, "/"); + return 0; + } + /* + * the username+password+host should appear + * before the first / + */ + char *serverinfo; + STRNDUP(serverinfo, url, fsep - url); + + /* check if uname+password is available */ + char *isep = strchr(serverinfo, '@'); + if (isep != NULL) { + char *authinfo; + STRNDUP(authinfo, serverinfo, isep - serverinfo); + char *upsep = strchr(authinfo, ':'); + if (upsep == NULL) + return -1; + + h->auth = (char*)alloc(2048); + memset(h->auth, 0, 2048); + printf("encoding '%s'\n", authinfo); + encode(authinfo, h->auth); + serverinfo += (isep + 1 - serverinfo); + if (!validip(serverinfo)) + return -1; + + } else if (!validip(serverinfo)) + return -1; + printf("serverinfo: '%s'\n", serverinfo); + inet_aton(serverinfo, &h->desc->destip); + h->path = fsep; + printf("path: '%s'\n", h->path); + return 0; +} + +int +http_open(const char *path, struct open_file * f) +{ + struct http_handle *h = alloc(sizeof(struct http_handle)); + memset(h, 0, sizeof(struct http_handle)); + + h->desc = socktodesc(*(int *) (f->f_devdata)); + + h->desc->myport = htons(SPORT + (getsecs() & 0x3ff)); + h->desc->destport = htons(80); + h->cmd = (char *) alloc(256); + memset(h->cmd, 0, 256); + + if (parseurl(path, h) != 0) + return -1; + + h->currpos = 0; + f->f_fsdata = h; + buffer = (char *) alloc(BUFFERSIZE); + memset(buffer, 0, BUFFERSIZE); + + return 0; +} + +int +http_read(struct open_file * f, void *addr, size_t size, size_t * resid) +{ + struct http_handle *h = (struct http_handle *) (f->f_fsdata); + while (1) { + int res = get(f, h->currpos, addr, size); + if (res == 0) + continue; + h->currpos += res; + break; + } + if (resid) + *resid = 0; + return 0; +} + +int +http_close(struct open_file * f) +{ + return 0; +} + +int +http_write(struct open_file * f, void *start, size_t size, size_t * resid) +{ + return EROFS; +} + +int +http_stat(struct open_file * f, struct stat * sb) +{ + return 0; +} +off_t +http_seek(struct open_file * f, off_t offset, int where) +{ + struct http_handle *h = (struct http_handle *) (f->f_fsdata); + printf("http_seek called\n"); + switch (where) { + case SEEK_SET: + h->currpos = offset; + break; + case SEEK_CUR: + break; + default: + errno = EOFFSET; + return -1; + } + return offset; +} diff --git a/ip.c b/ip.c --- /dev/null +++ b/ip.c @@ -0,0 +1,191 @@ +/* $NetBSD$ */ + +/* + * Copyright (c) 1992 Regents of the University of California. + * Copyright (c) 2010 Zoltan Arnold NAGY + * All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Lawrence Berkeley Laboratory and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _STANDALONE +#include +#else +#include +#endif + +#include "stand.h" +#include "net.h" + +/* + * sends an IP packet, if it's alredy constructed +*/ +static ssize_t +_sendip(struct iodesc * d, struct ip * ip, size_t len) +{ + u_char *ea; + + if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 || + netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask)) { + ea = arpwhohas(d, ip->ip_dst); + } else { + ea = arpwhohas(d, gateip); + } + + return sendether(d, ip, len, ea, ETHERTYPE_IP); +} + +/* + * fills out the IP header + * Caller must leave room for ethernet, ip and udp headers in front! +*/ +ssize_t +sendip(struct iodesc * d, void *pkt, size_t len, u_int8_t proto) +{ + ssize_t cc; + struct ip *ip; + + ip = (struct ip *) pkt - 1; + len += sizeof(*ip); + + (void) memset(ip, 0, sizeof(*ip)); + + ip->ip_v = IPVERSION; + ip->ip_hl = sizeof(*ip) >> 2; + ip->ip_len = htons(len); + ip->ip_p = proto; + ip->ip_ttl = IPDEFTTL; + ip->ip_src = d->myip; + ip->ip_dst = d->destip; + ip->ip_sum = ip_cksum(ip, sizeof(*ip)); + + cc = _sendip(d, ip, len); + + if (cc == -1) + return -1; + if ((size_t) cc != len) + panic("sendip: bad write (%d != %d)", cc, len); + return (cc - (sizeof(*ip))); +} + +/* + * reads an IP packet + * WARNING: the old version stripped the IP options, if there were + * any. Because we have absolutely no idea if the upper layer needs + * these or not, it's best to leave them there. + * + * The size returned is the size indicated in the header. + */ +ssize_t +readip(struct iodesc * d, void *pkt, size_t len, time_t tleft, u_int8_t proto) +{ + ssize_t n; + size_t hlen; + struct ip *ip; + u_int16_t etype; + + ip = (struct ip *) pkt - 1; + + n = readether(d, ip, len + sizeof(*ip), tleft, &etype); + if (n == -1 || (size_t) n < sizeof(*ip)) + return -1; + + if (etype == ETHERTYPE_ARP) { + struct arphdr *ah = (void *) ip; + if (ah->ar_op == htons(ARPOP_REQUEST)) { + /* Send ARP reply */ + arp_reply(d, ah); + } + return -1; + } + + if (etype != ETHERTYPE_IP) { +#ifdef NET_DEBUG + if (debug) + printf("readip: not IP. ether_type=%x\n", etype); +#endif + return -1; + } + + /* Check ip header */ + if (ip->ip_v != IPVERSION || + ip->ip_p != proto) { /* half char */ +#ifdef NET_DEBUG + if (debug) { + printf("readip: wrong IP version or wrong proto " + "ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p); + } +#endif + return -1; + } + + hlen = ip->ip_hl << 2; + if (hlen < sizeof(*ip) || ip_cksum(ip, hlen) != 0) { +#ifdef NET_DEBUG + if (debug) + printf("readip: short hdr or bad cksum.\n"); +#endif + return -1; + } + if (n < ntohs(ip->ip_len)) { +#ifdef NET_DEBUG + if (debug) + printf("readip: bad length %d < %d.\n", + (int) n, ntohs(ip->ip_len)); +#endif + return -1; + } + if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) { +#ifdef NET_DEBUG + if (debug) { + printf("readip: bad saddr %s != ", inet_ntoa(d->myip)); + printf("%s\n", inet_ntoa(ip->ip_dst)); + } +#endif + return -1; + } + + return (ntohs(ip->ip_len) - 20); +} diff --git a/loadfile.c b/loadfile.c --- a/loadfile.c +++ b/loadfile.c @@ -140,6 +140,8 @@ fdloadfile(int fd, u_long *marks, int flags) if (lseek(fd, 0, SEEK_SET) == (off_t)-1) goto err; nr = read(fd, &hdr, sizeof(hdr)); + printf("tada\n"); + printf("nr: %d\n",nr); if (nr == -1) { WARN(("read header failed")); goto err; @@ -152,6 +154,7 @@ fdloadfile(int fd, u_long *marks, int flags) #ifdef BOOT_ECOFF if (!ECOFF_BADMAG(&hdr.coff)) { + printf("coff\n"); rval = loadfile_coff(fd, &hdr.coff, marks, flags); } else #endif @@ -159,6 +162,7 @@ fdloadfile(int fd, u_long *marks, int flags) if (memcmp(hdr.elf32.e_ident, ELFMAG, SELFMAG) == 0 && hdr.elf32.e_ident[EI_CLASS] == ELFCLASS32) { netbsd_elf_class = ELFCLASS32; + printf("elf32\n"); rval = loadfile_elf32(fd, &hdr.elf32, marks, flags); } else #endif @@ -166,6 +170,7 @@ fdloadfile(int fd, u_long *marks, int flags) if (memcmp(hdr.elf64.e_ident, ELFMAG, SELFMAG) == 0 && hdr.elf64.e_ident[EI_CLASS] == ELFCLASS64) { netbsd_elf_class = ELFCLASS64; + printf("elf64\n"); rval = loadfile_elf64(fd, &hdr.elf64, marks, flags); } else #endif diff --git a/net.h b/net.h --- a/net.h +++ b/net.h @@ -64,14 +64,27 @@ #define RECV_SIZE 1536 /* XXX delete this */ /* - * How much room to leave for headers: + * How much room to leave for headers in UDP packets: * 14: struct ether_header * 20: struct ip * 8: struct udphdr * That's 42 but let's pad it out to 48 bytes. */ -#define ETHER_SIZE 14 -#define HEADER_SIZE 48 +#define ETHERNET_HEADER_SIZE 14 +#define IP_HEADER_SIZE 20 +#define UDP_HEADER_SIZE 8 + +#define UDP_TOTAL_HEADER_SIZE (ETHERNET_HEADER_SIZE + IP_HEADER_SIZE + UDP_HEADER_SIZE) + +/* + * How much room to leave for headers in TCP packets: + * 14: struct ether_header + * 20: struct ip + * 20: struct udphdr + */ +#define TCP_HEADER_SIZE 20 + +#define TCP_TOTAL_HEADER_SIZE (ETHERNET_HEADER_SIZE + IP_HEADER_SIZE + TCP_HEADER_SIZE) extern u_char bcea[6]; extern char rootpath[FNAME_SIZE]; @@ -95,8 +108,16 @@ int rarp_getipaddress __P((int)); ssize_t sendether __P((struct iodesc *, void *, size_t, u_char *, int)); ssize_t readether __P((struct iodesc *, void *, size_t, time_t, u_int16_t *)); +ssize_t sendip __P((struct iodesc *, void *, size_t, u_int8_t)); +ssize_t readip __P((struct iodesc *, void *, size_t, time_t, u_int8_t)); + ssize_t sendudp __P((struct iodesc *, void *, size_t)); ssize_t readudp __P((struct iodesc *, void *, size_t, time_t)); + +int tcp_connect __P((struct iodesc *)); +ssize_t sendtcp __P((struct iodesc *, void *, size_t)); +ssize_t readtcp __P((struct iodesc *, void *, size_t, time_t)); + ssize_t sendrecv __P((struct iodesc *, ssize_t (*)(struct iodesc *, void *, size_t), void *, size_t, diff --git a/rarp.c b/rarp.c --- a/rarp.c +++ b/rarp.c @@ -88,14 +88,14 @@ rarp_getipaddress(int sock) struct iodesc *d; struct ether_arp *ap; struct { - u_char header[ETHER_SIZE]; + u_char header[ETHERNET_HEADER_SIZE]; struct { struct ether_arp arp; u_char pad[18]; /* 60 - sizeof(arp) */ } data; } wbuf; struct { - u_char header[ETHER_SIZE]; + u_char header[ETHERNET_HEADER_SIZE]; struct { struct ether_arp arp; u_char pad[24]; /* extra space */ diff --git a/tcp.c b/tcp.c --- /dev/null +++ b/tcp.c @@ -0,0 +1,225 @@ +/* $NetBSD$ */ + +/* + * Copyright (c) 2010 Zoltan Arnold NAGY + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#ifdef _STANDALONE +#include +#else +#include +#endif + +#include +#include + +#include +#include +#include +#include +#include + +#include "stand.h" +#include "net.h" + +/* + * very minimal TCP handling code. + * proper ACK handling should be done! + */ + +/* + 0 - CLOSED + 1 - ESTABLISHED +*/ +static int connection_state = 0; + +/* Adds a checksum to a tcp header */ +static void +add_checksum(struct iodesc * desc, struct tcphdr * th, void *payload, size_t len) +{ + struct { + u_int32_t src; + u_int32_t dst; + u_int32_t lenproto; + struct tcphdr th; + u_char payload[len]; + } psh; + psh.src = desc->myip.s_addr; + psh.dst = desc->destip.s_addr; + psh.lenproto = htons(20 + len + IPPROTO_TCP); + th->th_sum = 0; + memcpy(&psh.th, th, sizeof(struct tcphdr)); + if (len > 0) + memcpy(&psh.payload, payload, len); + th->th_sum = ip_cksum((void *)&psh, + 3 * sizeof(u_int32_t) + sizeof(struct tcphdr) + len); +} + +static u_int32_t seq = 123; +static u_int32_t ack = 0; + +/* + * Does the usual 3-way handshake: + * - sends a SYN packet + * - receives a SYN+ACK packet + * - send an ACK packet + */ +int +tcp_connect(struct iodesc * desc) +{ + struct { + u_char headers[ETHERNET_HEADER_SIZE + IP_HEADER_SIZE]; + struct tcphdr th; + } w; + + struct { + u_char headers[ETHERNET_HEADER_SIZE + IP_HEADER_SIZE]; + struct tcphdr th; + } r; + + memset(&w, 0, (sizeof(u_char)*(ETHERNET_HEADER_SIZE + IP_HEADER_SIZE)) + + sizeof(struct tcphdr)); + memset(&r, 0, (sizeof(u_char)*(ETHERNET_HEADER_SIZE + IP_HEADER_SIZE)) + + sizeof(struct tcphdr)); + + w.th.th_sport = desc->myport; + w.th.th_dport = desc->destport; + seq = 123; + ack = 0; + w.th.th_seq = htonl(seq); + w.th.th_ack = htonl(ack); + w.th.th_off = 5; + w.th.th_flags = TH_SYN; + w.th.th_win = htons(8196); + add_checksum(desc, &w.th, NULL, 0); + + sendip(desc, &w.th, sizeof(struct tcphdr), IPPROTO_TCP); + while (1) { + ssize_t res = readip(desc, &r.th, sizeof(struct tcphdr), 1, + IPPROTO_TCP); + if (res == -1) + continue; + break; + } + + if ((r.th.th_flags & TH_SYN) && (r.th.th_flags & TH_ACK)) { + w.th.th_flags = TH_ACK; + w.th.th_seq = htonl(++seq); + ack = ntohl(r.th.th_seq) + 1; + w.th.th_ack = htonl(ack); + add_checksum(desc, &w.th, NULL, 0); + connection_state = 1; + sendip(desc, &w.th, sizeof(struct tcphdr), IPPROTO_TCP); + } + return 0; +} + +/* + * sends out a tcp packet with the specific payload + * all host and port informations are gathered from + * the preset iodesc +*/ +int +sendtcp(struct iodesc * desc, void *payload, size_t len) +{ + if (connection_state == 0) + return -2; + + struct { + u_char headers[ETHERNET_HEADER_SIZE + IP_HEADER_SIZE]; + struct tcphdr th; + u_char payload[len]; + } w; + memset(&w, 0, (sizeof(u_char) * (ETHERNET_HEADER_SIZE + IP_HEADER_SIZE)) + + sizeof(struct tcphdr) + len); + memcpy(&w.payload, payload, len); + w.th.th_sport = desc->myport; + w.th.th_dport = desc->destport; + w.th.th_seq = htonl(seq); + w.th.th_ack = htonl(ack); + w.th.th_off = 5; + w.th.th_flags = TH_PUSH | TH_ACK; + w.th.th_win = htons(8196); + add_checksum(desc, &w.th, payload, len); + seq += len; + sendip(desc, &w.th, sizeof(struct tcphdr) + len, IPPROTO_TCP); + return 0; +} + +/* + * reads a tcp packet; the caller should have allocated enough space + * to hold the ethernet + ip + tcp headers + payload + * the iodesc should be setup before calling this method + */ +int +readtcp(struct iodesc * desc, void *pkt, size_t len, time_t tleft) +{ + struct tcphdr *th = (struct tcphdr *) pkt - 1; + + if (connection_state == 0) + return -2; + ssize_t n = readip(desc, th, len + sizeof(struct tcphdr), tleft, + IPPROTO_TCP); + if (n == -1 || (size_t) n < sizeof(struct tcphdr)) + return -1; + n -= sizeof(struct tcphdr); + if (th->th_flags & TH_ACK) + ack += n; + + struct { + u_char headers[ETHERNET_HEADER_SIZE + IP_HEADER_SIZE]; + struct tcphdr th; + } w; + memset(&w, 0, (sizeof(u_char) * (ETHERNET_HEADER_SIZE + IP_HEADER_SIZE)) + + sizeof(struct tcphdr)); + + w.th.th_sport = desc->myport; + w.th.th_dport = desc->destport; + w.th.th_off = 5; + if (th->th_flags & TH_FIN) { + ack += 1; + w.th.th_flags = TH_ACK | TH_FIN; + connection_state = 0; + } else { + w.th.th_flags = TH_ACK; + } + w.th.th_seq = htonl(seq); + w.th.th_ack = htonl(ack); + w.th.th_win = htons(8196); + + add_checksum(desc, &w.th, NULL, 0); + sendip(desc, &w.th, sizeof(struct tcphdr), IPPROTO_TCP); + if (connection_state == 0) { + while (1) { + if (readip(desc, &w.th, sizeof(struct tcphdr), + 1, IPPROTO_TCP) != -1) + break; + } + } + return n; +} diff --git a/tftp.c b/tftp.c --- a/tftp.c +++ b/tftp.c @@ -65,6 +65,8 @@ static int tftpport = 2000; #define RSPACE 520 /* max data packet, rounded up */ +#define DEBUG + struct tftp_handle { struct iodesc *iodesc; int currblock; /* contents of lastdata */ @@ -73,7 +75,7 @@ struct tftp_handle { int off; const char *path; /* saved for re-requests */ struct { - u_char header[HEADER_SIZE]; + u_char header[UDP_TOTAL_HEADER_SIZE]; struct tftphdr t; u_char space[RSPACE]; } lastdata; @@ -153,7 +155,7 @@ static int tftp_makereq(struct tftp_handle *h) { struct { - u_char header[HEADER_SIZE]; + u_char header[UDP_TOTAL_HEADER_SIZE]; struct tftphdr t; u_char space[FNAME_SIZE + 6]; } wbuf; @@ -196,7 +198,7 @@ static int tftp_getnextblock(struct tftp_handle *h) { struct { - u_char header[HEADER_SIZE]; + u_char header[UDP_TOTAL_HEADER_SIZE]; struct tftphdr t; } wbuf; char *wtail; @@ -229,7 +231,7 @@ static void tftp_terminate(struct tftp_handle *h) { struct { - u_char header[HEADER_SIZE]; + u_char header[UDP_TOTAL_HEADER_SIZE]; struct tftphdr t; } wbuf; char *wtail; @@ -303,7 +305,6 @@ tftp_read(struct open_file *f, void *addr, size_t size, size_t *resid) while (tftpfile->currblock < needblock) { int res; - res = tftp_getnextblock(tftpfile); if (res) { /* no answer */ #ifdef DEBUG @@ -315,7 +316,6 @@ tftp_read(struct open_file *f, void *addr, size_t size, size_t *resid) if (tftpfile->islastblock) break; } - if (tftpfile->currblock == needblock) { size_t offinblock, inbuffer; @@ -338,8 +338,9 @@ tftp_read(struct open_file *f, void *addr, size_t size, size_t *resid) tftpfile->off += count; size -= count; - if ((tftpfile->islastblock) && (count == inbuffer)) + if ((tftpfile->islastblock) && (count == inbuffer)) { break; /* EOF */ + } } else { #ifdef DEBUG printf("tftp: block %d not found\n", needblock); @@ -351,6 +352,7 @@ tftp_read(struct open_file *f, void *addr, size_t size, size_t *resid) if (resid) *resid = size; + return 0; } @@ -424,6 +426,7 @@ tftp_stat(struct open_file *f, struct stat *sb) sb->st_uid = 0; sb->st_gid = 0; sb->st_size = tftp_size_of_file(tftpfile); + printf("stat returning with %d\n", (int)sb->st_size); return 0; } @@ -435,14 +438,17 @@ tftp_seek(struct open_file *f, off_t offset, int where) switch (where) { case SEEK_SET: + printf("SEEK_SET called with offset: %u, current: %u, diff: %u\n", (int)offset, tftpfile->off, abs(tftpfile->off-offset)); tftpfile->off = offset; break; case SEEK_CUR: + printf("SEEK_CUR called with offset: %u, current: %u, diff: %u\n", (int)offset, tftpfile->off, abs(tftpfile->off-offset)); tftpfile->off += offset; break; default: errno = EOFFSET; return -1; } + printf("seek returning %d\n", tftpfile->off); return tftpfile->off; } diff --git a/udp.c b/udp.c --- a/udp.c +++ b/udp.c @@ -67,9 +67,7 @@ ssize_t sendudp(struct iodesc *d, void *pkt, size_t len) { ssize_t cc; - struct ip *ip; struct udphdr *uh; - u_char *ea; #ifdef NET_DEBUG if (debug) { @@ -84,51 +82,20 @@ sendudp(struct iodesc *d, void *pkt, size_t len) #endif uh = (struct udphdr *)pkt - 1; - ip = (struct ip *)uh - 1; - len += sizeof(*ip) + sizeof(*uh); + len += sizeof(*uh); - (void)memset(ip, 0, sizeof(*ip) + sizeof(*uh)); - - ip->ip_v = IPVERSION; /* half-char */ - ip->ip_hl = sizeof(*ip) >> 2; /* half-char */ - ip->ip_len = htons(len); - ip->ip_p = IPPROTO_UDP; /* char */ - ip->ip_ttl = IPDEFTTL; /* char */ - ip->ip_src = d->myip; - ip->ip_dst = d->destip; - ip->ip_sum = ip_cksum(ip, sizeof(*ip)); /* short, but special */ + (void)memset(uh, 0, sizeof(*uh)); uh->uh_sport = d->myport; uh->uh_dport = d->destport; - uh->uh_ulen = htons(len - sizeof(*ip)); - -#ifndef UDP_NO_CKSUM - { - struct udpiphdr *ui; - struct ip tip; + uh->uh_ulen = htons(len); - /* Calculate checksum (must save and restore ip header) */ - tip = *ip; - ui = (struct udpiphdr *)ip; - (void)memset(ui->ui_x1, 0, sizeof(ui->ui_x1)); - ui->ui_len = uh->uh_ulen; - uh->uh_sum = ip_cksum(ui, len); - *ip = tip; - } -#endif - - if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 || - netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask)) - ea = arpwhohas(d, ip->ip_dst); - else - ea = arpwhohas(d, gateip); - - cc = sendether(d, ip, len, ea, ETHERTYPE_IP); + cc = sendip(d, uh, len, IPPROTO_UDP); if (cc == -1) return -1; if ((size_t)cc != len) panic("sendudp: bad write (%d != %d)", cc, len); - return (cc - (sizeof(*ip) + sizeof(*uh))); + return (cc - sizeof(*uh)); } /* @@ -139,87 +106,13 @@ ssize_t readudp(struct iodesc *d, void *pkt, size_t len, time_t tleft) { ssize_t n; - size_t hlen; - struct ip *ip; struct udphdr *uh; - u_int16_t etype; /* host order */ - -#ifdef NET_DEBUG - if (debug) - printf("readudp: called\n"); -#endif uh = (struct udphdr *)pkt - 1; - ip = (struct ip *)uh - 1; - - n = readether(d, ip, len + sizeof(*ip) + sizeof(*uh), tleft, &etype); - if (n == -1 || (size_t)n < sizeof(*ip) + sizeof(*uh)) - return -1; - - /* Ethernet address checks now in readether() */ - - /* Need to respond to ARP requests. */ - if (etype == ETHERTYPE_ARP) { - struct arphdr *ah = (void *)ip; - if (ah->ar_op == htons(ARPOP_REQUEST)) { - /* Send ARP reply */ - arp_reply(d, ah); - } - return -1; - } - - if (etype != ETHERTYPE_IP) { -#ifdef NET_DEBUG - if (debug) - printf("readudp: not IP. ether_type=%x\n", etype); -#endif - return -1; - } - - /* Check ip header */ - if (ip->ip_v != IPVERSION || - ip->ip_p != IPPROTO_UDP) { /* half char */ -#ifdef NET_DEBUG - if (debug) { - printf("readudp: IP version or not UDP. " - "ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p); - } -#endif + n = readip(d, uh, len + sizeof(*uh), tleft, IPPROTO_UDP); + if (n == -1 || (size_t)n < sizeof(*uh)) return -1; - } - - hlen = ip->ip_hl << 2; - if (hlen < sizeof(*ip) || ip_cksum(ip, hlen) != 0) { -#ifdef NET_DEBUG - if (debug) - printf("readudp: short hdr or bad cksum.\n"); -#endif - return -1; - } - if (n < ntohs(ip->ip_len)) { -#ifdef NET_DEBUG - if (debug) - printf("readudp: bad length %d < %d.\n", - (int)n, ntohs(ip->ip_len)); -#endif - return -1; - } - if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) { -#ifdef NET_DEBUG - if (debug) { - printf("readudp: bad saddr %s != ", inet_ntoa(d->myip)); - printf("%s\n", inet_ntoa(ip->ip_dst)); - } -#endif - return -1; - } - /* If there were ip options, make them go away */ - if (hlen != sizeof(*ip)) { - (void)memcpy(uh, ((u_char *)ip) + hlen, len - hlen); - ip->ip_len = htons(sizeof(*ip)); - n -= hlen - sizeof(*ip); - } if (uh->uh_dport != d->myport) { #ifdef NET_DEBUG if (debug) @@ -229,33 +122,6 @@ readudp(struct iodesc *d, void *pkt, size_t len, time_t tleft) return -1; } -#ifndef UDP_NO_CKSUM - if (uh->uh_sum) { - struct udpiphdr *ui; - struct ip tip; - - n = ntohs(uh->uh_ulen) + sizeof(*ip); - if (n > RECV_SIZE - ETHER_SIZE) { - printf("readudp: huge packet, udp len %d\n", (int)n); - return -1; - } - - /* Check checksum (must save and restore ip header) */ - tip = *ip; - ui = (struct udpiphdr *)ip; - (void)memset(ui->ui_x1, 0, sizeof(ui->ui_x1)); - ui->ui_len = uh->uh_ulen; - if (ip_cksum(ui, n) != 0) { -#ifdef NET_DEBUG - if (debug) - printf("readudp: bad cksum\n"); -#endif - *ip = tip; - return -1; - } - *ip = tip; - } -#endif if (ntohs(uh->uh_ulen) < sizeof(*uh)) { #ifdef NET_DEBUG if (debug) @@ -265,6 +131,6 @@ readudp(struct iodesc *d, void *pkt, size_t len, time_t tleft) return -1; } - n -= sizeof(*ip) + sizeof(*uh); + n -= sizeof(*uh); return n; }