diff options
author | Toomas Soome <tsoome@me.com> | 2018-02-16 22:00:03 +0200 |
---|---|---|
committer | Dan McDonald <danmcd@joyent.com> | 2018-05-14 13:00:24 -0400 |
commit | 890c86716e81b6b22639ff07791007b82c26e6c5 (patch) | |
tree | 077bf09019f64b60524767b89dd1f2eefbfae909 | |
parent | b8f90f25b92fc9ac25542c21076fda8ff8c6fa59 (diff) | |
download | illumos-joyent-890c86716e81b6b22639ff07791007b82c26e6c5.tar.gz |
9122 libstand: Consolidate tftp sendrecv into net.c sendrecv
Reviewed by: Yuri Pankov <yuripv@yuripv.net>
Reviewed by: Andy Fiddaman <omnios@citrus-it.co.uk>
Approved by: Dan McDonald <danmcd@joyent.com>
-rw-r--r-- | usr/src/boot/lib/libstand/arp.c | 6 | ||||
-rw-r--r-- | usr/src/boot/lib/libstand/bootp.c | 9 | ||||
-rw-r--r-- | usr/src/boot/lib/libstand/net.c | 6 | ||||
-rw-r--r-- | usr/src/boot/lib/libstand/net.h | 5 | ||||
-rw-r--r-- | usr/src/boot/lib/libstand/rarp.c | 7 | ||||
-rw-r--r-- | usr/src/boot/lib/libstand/rpc.c | 6 | ||||
-rw-r--r-- | usr/src/boot/lib/libstand/tftp.c | 103 |
7 files changed, 46 insertions, 96 deletions
diff --git a/usr/src/boot/lib/libstand/arp.c b/usr/src/boot/lib/libstand/arp.c index 22bd028df1..65e421d1bb 100644 --- a/usr/src/boot/lib/libstand/arp.c +++ b/usr/src/boot/lib/libstand/arp.c @@ -64,7 +64,7 @@ int arp_num = 1; /* Local forwards */ static ssize_t arpsend(struct iodesc *, void *, size_t); -static ssize_t arprecv(struct iodesc *, void **, void **, time_t); +static ssize_t arprecv(struct iodesc *, void **, void **, time_t, void *); /* Broadcast an ARP packet, asking who has addr on interface d */ u_char * @@ -117,7 +117,7 @@ arpwhohas(struct iodesc *d, struct in_addr addr) ah = NULL; i = sendrecv(d, arpsend, &wbuf.data, sizeof(wbuf.data), - arprecv, &pkt, (void **)&ah); + arprecv, &pkt, (void **)&ah, NULL); if (i == -1) { panic("arp: no response for %s\n", inet_ntoa(addr)); @@ -158,7 +158,7 @@ arpsend(struct iodesc *d, void *pkt, size_t len) * else -1 (and errno == 0) */ static ssize_t -arprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft) +arprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft, void *extra) { ssize_t n; struct ether_arp *ah; diff --git a/usr/src/boot/lib/libstand/bootp.c b/usr/src/boot/lib/libstand/bootp.c index 9afe9a301b..328a45f9b5 100644 --- a/usr/src/boot/lib/libstand/bootp.c +++ b/usr/src/boot/lib/libstand/bootp.c @@ -68,7 +68,7 @@ static char vm_cmu[4] = VM_CMU; /* Local forwards */ static ssize_t bootpsend(struct iodesc *, void *, size_t); -static ssize_t bootprecv(struct iodesc *, void **, void **, time_t); +static ssize_t bootprecv(struct iodesc *, void **, void **, time_t, void *); static int vend_rfc1048(u_char *, u_int); #ifdef BOOTP_VEND_CMU static void vend_cmu(u_char *); @@ -177,7 +177,7 @@ bootp(int sock) if(sendrecv(d, bootpsend, bp, sizeof(*bp), - bootprecv, &pkt, (void **)&rbootp) == -1) { + bootprecv, &pkt, (void **)&rbootp, NULL) == -1) { printf("bootp: no reply\n"); return; } @@ -203,7 +203,7 @@ bootp(int sock) free(pkt); if(sendrecv(d, bootpsend, bp, sizeof(*bp), - bootprecv, &pkt, (void **)&rbootp) == -1) { + bootprecv, &pkt, (void **)&rbootp, NULL) == -1) { printf("DHCPREQUEST failed\n"); return; } @@ -280,7 +280,8 @@ bootpsend(struct iodesc *d, void *pkt, size_t len) } static ssize_t -bootprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft) +bootprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft, + void *extra) { ssize_t n; struct bootp *bp; diff --git a/usr/src/boot/lib/libstand/net.c b/usr/src/boot/lib/libstand/net.c index ea4c75b233..d8d46c4ec4 100644 --- a/usr/src/boot/lib/libstand/net.c +++ b/usr/src/boot/lib/libstand/net.c @@ -71,8 +71,8 @@ ssize_t sendrecv(struct iodesc *d, ssize_t (*sproc)(struct iodesc *, void *, size_t), void *sbuf, size_t ssize, - ssize_t (*rproc)(struct iodesc *, void **, void**, time_t), - void **pkt, void **payload) + ssize_t (*rproc)(struct iodesc *, void **, void**, time_t, void *), + void **pkt, void **payload, void *recv_extra) { ssize_t cc; time_t t, tmo, tlast; @@ -115,7 +115,7 @@ sendrecv(struct iodesc *d, } /* Try to get a packet and process it. */ - cc = (*rproc)(d, pkt, payload, tleft); + cc = (*rproc)(d, pkt, payload, tleft, recv_extra); /* Return on data, EOF or real error. */ if (cc != -1 || (errno != 0 && errno != ETIMEDOUT)) return (cc); diff --git a/usr/src/boot/lib/libstand/net.h b/usr/src/boot/lib/libstand/net.h index 733206ec92..99081a634a 100644 --- a/usr/src/boot/lib/libstand/net.h +++ b/usr/src/boot/lib/libstand/net.h @@ -112,8 +112,9 @@ ssize_t readudp(struct iodesc *, void **, void **, time_t); ssize_t sendrecv(struct iodesc *, ssize_t (*)(struct iodesc *, void *, size_t), void *, size_t, - ssize_t (*)(struct iodesc *, void **, void **, time_t), - void **, void **); + ssize_t (*)(struct iodesc *, void **, void **, time_t, + void *), + void **, void **, void *); /* bootp/DHCP */ void bootp(int); diff --git a/usr/src/boot/lib/libstand/rarp.c b/usr/src/boot/lib/libstand/rarp.c index 120b2c377f..7781787015 100644 --- a/usr/src/boot/lib/libstand/rarp.c +++ b/usr/src/boot/lib/libstand/rarp.c @@ -53,7 +53,7 @@ static ssize_t rarpsend(struct iodesc *, void *, size_t); -static ssize_t rarprecv(struct iodesc *, void **, void **, time_t); +static ssize_t rarprecv(struct iodesc *, void **, void **, time_t, void *); /* * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826). @@ -98,7 +98,7 @@ rarp_getipaddress(int sock) if (sendrecv(d, rarpsend, &wbuf.data, sizeof(wbuf.data), - rarprecv, &pkt, (void *)&ap) < 0) { + rarprecv, &pkt, (void *)&ap, NULL) < 0) { printf("No response for RARP request\n"); return (-1); } @@ -142,7 +142,8 @@ rarpsend(struct iodesc *d, void *pkt, size_t len) * else -1 (and errno == 0) */ static ssize_t -rarprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft) +rarprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft, + void *extra) { ssize_t n; struct ether_arp *ap; diff --git a/usr/src/boot/lib/libstand/rpc.c b/usr/src/boot/lib/libstand/rpc.c index 0923739441..b843535552 100644 --- a/usr/src/boot/lib/libstand/rpc.c +++ b/usr/src/boot/lib/libstand/rpc.c @@ -96,7 +96,7 @@ struct rpc_reply { }; /* Local forwards */ -static ssize_t recvrpc(struct iodesc *, void **, void **, time_t); +static ssize_t recvrpc(struct iodesc *, void **, void **, time_t, void *); static int rpc_getport(struct iodesc *, n_long, n_long); int rpc_xid; @@ -166,7 +166,7 @@ rpc_call(struct iodesc *d, n_long prog, n_long vers, n_long proc, ptr = NULL; cc = sendrecv(d, sendudp, send_head, send_tail - send_head, - recvrpc, &ptr, (void **)&reply); + recvrpc, &ptr, (void **)&reply, NULL); #ifdef RPC_DEBUG if (debug) @@ -216,7 +216,7 @@ rpc_call(struct iodesc *d, n_long prog, n_long vers, n_long proc, * Remaining checks are done by callrpc */ static ssize_t -recvrpc(struct iodesc *d, void **pkt, void **payload, time_t tleft) +recvrpc(struct iodesc *d, void **pkt, void **payload, time_t tleft, void *extra) { void *ptr; struct rpc_reply *reply; diff --git a/usr/src/boot/lib/libstand/tftp.c b/usr/src/boot/lib/libstand/tftp.c index c1ce0e3b80..e235f8a537 100644 --- a/usr/src/boot/lib/libstand/tftp.c +++ b/usr/src/boot/lib/libstand/tftp.c @@ -60,7 +60,10 @@ #include "tftp.h" struct tftp_handle; +struct tftprecv_extra; +static ssize_t recvtftp(struct iodesc *d, void **pkt, void **payload, + time_t tleft, void *recv_extra); static int tftp_open(const char *path, struct open_file *f); static int tftp_close(struct open_file *f); static int tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len); @@ -68,11 +71,6 @@ static int tftp_read(struct open_file *f, void *buf, size_t size, size_t *resid) static off_t tftp_seek(struct open_file *f, off_t offset, int where); static int tftp_set_blksize(struct tftp_handle *h, const char *str); static int tftp_stat(struct open_file *f, struct stat *sb); -static ssize_t sendrecv_tftp(struct tftp_handle *h, - ssize_t (*sproc)(struct iodesc *, void *, size_t), - void *sbuf, size_t ssize, - ssize_t (*rproc)(struct tftp_handle *h, void **, void **, time_t, unsigned short *), - void **, void **, unsigned short *rtype); struct fs_ops tftp_fsops = { "tftp", @@ -116,6 +114,11 @@ struct tftp_handle { struct tftphdr *tftp_hdr; }; +struct tftprecv_extra { + struct tftp_handle *tftp_handle; + unsigned short rtype; /* Received type */ +}; + #define TFTP_MAX_ERRCODE EOPTNEG static const int tftperrors[TFTP_MAX_ERRCODE + 1] = { 0, /* ??? */ @@ -176,15 +179,18 @@ tftp_sendack(struct tftp_handle *h) } static ssize_t -recvtftp(struct tftp_handle *h, void **pkt, void **payload, time_t tleft, - unsigned short *rtype) +recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft, + void *recv_extra) { - struct iodesc *d = h->iodesc; + struct tftprecv_extra *extra; + struct tftp_handle *h; struct tftphdr *t; void *ptr = NULL; ssize_t len; errno = 0; + extra = (struct tftprecv_extra *)recv_extra; + h = extra->tftp_handle; len = readudp(d, &ptr, (void **)&t, tleft); @@ -193,7 +199,7 @@ recvtftp(struct tftp_handle *h, void **pkt, void **payload, time_t tleft, return (-1); } - *rtype = ntohs(t->th_opcode); + extra->rtype = ntohs(t->th_opcode); switch (ntohs(t->th_opcode)) { case DATA: { int got; @@ -280,6 +286,7 @@ tftp_makereq(struct tftp_handle *h) struct tftphdr t; u_char space[FNAME_SIZE + 6]; } __packed __aligned(4) wbuf; + struct tftprecv_extra recv_extra; char *wtail; int l; ssize_t res; @@ -287,7 +294,6 @@ tftp_makereq(struct tftp_handle *h) struct tftphdr *t; char *tftp_blksize = NULL; int blksize_l; - unsigned short rtype = 0; /* * Allow overriding default TFTP block size by setting @@ -332,8 +338,9 @@ tftp_makereq(struct tftp_handle *h) h->validsize = 0; pkt = NULL; - res = sendrecv_tftp(h, &sendudp, &wbuf.t, wtail - (char *) &wbuf.t, - &recvtftp, &pkt, (void **)&t, &rtype); + recv_extra.tftp_handle = h; + res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *) &wbuf.t, + &recvtftp, &pkt, (void **)&t, &recv_extra); if (res == -1) { free(pkt); return (errno); @@ -343,13 +350,13 @@ tftp_makereq(struct tftp_handle *h) h->pkt = pkt; h->tftp_hdr = t; - if (rtype == OACK) + if (recv_extra.rtype == OACK) return (tftp_getnextblock(h)); /* Server ignored our blksize request, revert to TFTP default. */ h->tftp_blksize = SEGSIZE; - switch (rtype) { + switch (recv_extra.rtype) { case DATA: { h->currblock = 1; h->validsize = res; @@ -375,11 +382,11 @@ tftp_getnextblock(struct tftp_handle *h) u_char header[HEADER_SIZE]; struct tftphdr t; } __packed __aligned(4) wbuf; + struct tftprecv_extra recv_extra; char *wtail; int res; void *pkt; struct tftphdr *t; - unsigned short rtype = 0; wbuf.t.th_opcode = htons((u_short) ACK); wtail = (char *) &wbuf.t.th_block; wbuf.t.th_block = htons((u_short) h->currblock); @@ -388,8 +395,9 @@ tftp_getnextblock(struct tftp_handle *h) h->iodesc->xid = h->currblock + 1; /* expected block */ pkt = NULL; - res = sendrecv_tftp(h, &sendudp, &wbuf.t, wtail - (char *) &wbuf.t, - &recvtftp, &pkt, (void **)&t, &rtype); + recv_extra.tftp_handle = h; + res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *) &wbuf.t, + &recvtftp, &pkt, (void **)&t, &recv_extra); if (res == -1) { /* 0 is OK! */ free(pkt); @@ -596,67 +604,6 @@ tftp_seek(struct open_file *f, off_t offset, int where) return (tftpfile->off); } -static ssize_t -sendrecv_tftp(struct tftp_handle *h, - ssize_t (*sproc)(struct iodesc *, void *, size_t), - void *sbuf, size_t ssize, - ssize_t (*rproc)(struct tftp_handle *, void **, void **, time_t, - unsigned short *), - void **pkt, void **payload, unsigned short *rtype) -{ - struct iodesc *d = h->iodesc; - ssize_t cc; - time_t t, t1, tleft; - -#ifdef TFTP_DEBUG - if (debug) - printf("sendrecv: called\n"); -#endif - - tleft = MINTMO; - t = t1 = getsecs(); - for (;;) { - if ((getsecs() - t) > MAXTMO) { - errno = ETIMEDOUT; - return -1; - } - - cc = (*sproc)(d, sbuf, ssize); - if (cc != -1 && cc < ssize) - panic("sendrecv: short write! (%zd < %zu)", - cc, ssize); - - if (cc == -1) { - /* Error on transmit; wait before retrying */ - while ((getsecs() - t1) < tleft); - t1 = getsecs(); - continue; - } - - t = t1 = getsecs(); -recvnext: - if ((getsecs() - t) > MAXTMO) { - errno = ETIMEDOUT; - return (-1); - } - /* Try to get a packet and process it. */ - cc = (*rproc)(h, pkt, payload, tleft, rtype); - /* Return on data, EOF or real error. */ - if (cc != -1 || (errno != 0 && errno != ETIMEDOUT)) - return (cc); - if ((getsecs() - t1) < tleft) { - goto recvnext; - } - - /* Timed out or didn't get the packet we're waiting for */ - tleft += MINTMO; - if (tleft > (2 * MINTMO)) { - tleft = (2 * MINTMO); - } - t1 = getsecs(); - } -} - static int tftp_set_blksize(struct tftp_handle *h, const char *str) { |