Annotation of src/usr.bin/tftp/tftpsubs.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: tftpsubs.c,v 1.3 1994/12/08 09:51:31 jtc Exp $ */
! 2:
! 3: /*
! 4: * Copyright (c) 1983, 1993
! 5: * The Regents of the University of California. All rights reserved.
! 6: *
! 7: * Redistribution and use in source and binary forms, with or without
! 8: * modification, are permitted provided that the following conditions
! 9: * are met:
! 10: * 1. Redistributions of source code must retain the above copyright
! 11: * notice, this list of conditions and the following disclaimer.
! 12: * 2. Redistributions in binary form must reproduce the above copyright
! 13: * notice, this list of conditions and the following disclaimer in the
! 14: * documentation and/or other materials provided with the distribution.
! 15: * 3. All advertising materials mentioning features or use of this software
! 16: * must display the following acknowledgement:
! 17: * This product includes software developed by the University of
! 18: * California, Berkeley and its contributors.
! 19: * 4. Neither the name of the University nor the names of its contributors
! 20: * may be used to endorse or promote products derived from this software
! 21: * without specific prior written permission.
! 22: *
! 23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 33: * SUCH DAMAGE.
! 34: */
! 35:
! 36: #ifndef lint
! 37: #if 0
! 38: static char sccsid[] = "@(#)tftpsubs.c 8.1 (Berkeley) 6/6/93";
! 39: #endif
! 40: static char rcsid[] = "$NetBSD: tftpsubs.c,v 1.3 1994/12/08 09:51:31 jtc Exp $";
! 41: #endif /* not lint */
! 42:
! 43: /* Simple minded read-ahead/write-behind subroutines for tftp user and
! 44: server. Written originally with multiple buffers in mind, but current
! 45: implementation has two buffer logic wired in.
! 46:
! 47: Todo: add some sort of final error check so when the write-buffer
! 48: is finally flushed, the caller can detect if the disk filled up
! 49: (or had an i/o error) and return a nak to the other side.
! 50:
! 51: Jim Guyton 10/85
! 52: */
! 53:
! 54: #include <sys/types.h>
! 55: #include <sys/socket.h>
! 56: #include <sys/ioctl.h>
! 57: #include <netinet/in.h>
! 58: #include <arpa/tftp.h>
! 59:
! 60: #include <stdio.h>
! 61: #include <unistd.h>
! 62:
! 63: #include "tftpsubs.h"
! 64:
! 65: #define PKTSIZE SEGSIZE+4 /* should be moved to tftp.h */
! 66:
! 67: struct bf {
! 68: int counter; /* size of data in buffer, or flag */
! 69: char buf[PKTSIZE]; /* room for data packet */
! 70: } bfs[2];
! 71:
! 72: /* Values for bf.counter */
! 73: #define BF_ALLOC -3 /* alloc'd but not yet filled */
! 74: #define BF_FREE -2 /* free */
! 75: /* [-1 .. SEGSIZE] = size of data in the data buffer */
! 76:
! 77: static int nextone; /* index of next buffer to use */
! 78: static int current; /* index of buffer in use */
! 79:
! 80: /* control flags for crlf conversions */
! 81: int newline = 0; /* fillbuf: in middle of newline expansion */
! 82: int prevchar = -1; /* putbuf: previous char (cr check) */
! 83:
! 84: static struct tftphdr *rw_init();
! 85:
! 86: struct tftphdr *w_init() { return rw_init(0); } /* write-behind */
! 87: struct tftphdr *r_init() { return rw_init(1); } /* read-ahead */
! 88:
! 89: static struct tftphdr *
! 90: rw_init(x) /* init for either read-ahead or write-behind */
! 91: int x; /* zero for write-behind, one for read-head */
! 92: {
! 93: newline = 0; /* init crlf flag */
! 94: prevchar = -1;
! 95: bfs[0].counter = BF_ALLOC; /* pass out the first buffer */
! 96: current = 0;
! 97: bfs[1].counter = BF_FREE;
! 98: nextone = x; /* ahead or behind? */
! 99: return (struct tftphdr *)bfs[0].buf;
! 100: }
! 101:
! 102:
! 103: /* Have emptied current buffer by sending to net and getting ack.
! 104: Free it and return next buffer filled with data.
! 105: */
! 106: int
! 107: readit(file, dpp, convert)
! 108: FILE *file; /* file opened for read */
! 109: struct tftphdr **dpp;
! 110: int convert; /* if true, convert to ascii */
! 111: {
! 112: struct bf *b;
! 113:
! 114: bfs[current].counter = BF_FREE; /* free old one */
! 115: current = !current; /* "incr" current */
! 116:
! 117: b = &bfs[current]; /* look at new buffer */
! 118: if (b->counter == BF_FREE) /* if it's empty */
! 119: read_ahead(file, convert); /* fill it */
! 120: /* assert(b->counter != BF_FREE);*//* check */
! 121: *dpp = (struct tftphdr *)b->buf; /* set caller's ptr */
! 122: return b->counter;
! 123: }
! 124:
! 125: /*
! 126: * fill the input buffer, doing ascii conversions if requested
! 127: * conversions are lf -> cr,lf and cr -> cr, nul
! 128: */
! 129: void
! 130: read_ahead(file, convert)
! 131: FILE *file; /* file opened for read */
! 132: int convert; /* if true, convert to ascii */
! 133: {
! 134: register int i;
! 135: register char *p;
! 136: register int c;
! 137: struct bf *b;
! 138: struct tftphdr *dp;
! 139:
! 140: b = &bfs[nextone]; /* look at "next" buffer */
! 141: if (b->counter != BF_FREE) /* nop if not free */
! 142: return;
! 143: nextone = !nextone; /* "incr" next buffer ptr */
! 144:
! 145: dp = (struct tftphdr *)b->buf;
! 146:
! 147: if (convert == 0) {
! 148: b->counter = read(fileno(file), dp->th_data, SEGSIZE);
! 149: return;
! 150: }
! 151:
! 152: p = dp->th_data;
! 153: for (i = 0 ; i < SEGSIZE; i++) {
! 154: if (newline) {
! 155: if (prevchar == '\n')
! 156: c = '\n'; /* lf to cr,lf */
! 157: else c = '\0'; /* cr to cr,nul */
! 158: newline = 0;
! 159: }
! 160: else {
! 161: c = getc(file);
! 162: if (c == EOF) break;
! 163: if (c == '\n' || c == '\r') {
! 164: prevchar = c;
! 165: c = '\r';
! 166: newline = 1;
! 167: }
! 168: }
! 169: *p++ = c;
! 170: }
! 171: b->counter = (int)(p - dp->th_data);
! 172: }
! 173:
! 174: /* Update count associated with the buffer, get new buffer
! 175: from the queue. Calls write_behind only if next buffer not
! 176: available.
! 177: */
! 178: int
! 179: writeit(file, dpp, ct, convert)
! 180: FILE *file;
! 181: struct tftphdr **dpp;
! 182: int ct, convert;
! 183: {
! 184: bfs[current].counter = ct; /* set size of data to write */
! 185: current = !current; /* switch to other buffer */
! 186: if (bfs[current].counter != BF_FREE) /* if not free */
! 187: (void)write_behind(file, convert); /* flush it */
! 188: bfs[current].counter = BF_ALLOC; /* mark as alloc'd */
! 189: *dpp = (struct tftphdr *)bfs[current].buf;
! 190: return ct; /* this is a lie of course */
! 191: }
! 192:
! 193: /*
! 194: * Output a buffer to a file, converting from netascii if requested.
! 195: * CR,NUL -> CR and CR,LF => LF.
! 196: * Note spec is undefined if we get CR as last byte of file or a
! 197: * CR followed by anything else. In this case we leave it alone.
! 198: */
! 199: int
! 200: write_behind(file, convert)
! 201: FILE *file;
! 202: int convert;
! 203: {
! 204: char *buf;
! 205: int count;
! 206: register int ct;
! 207: register char *p;
! 208: register int c; /* current character */
! 209: struct bf *b;
! 210: struct tftphdr *dp;
! 211:
! 212: b = &bfs[nextone];
! 213: if (b->counter < -1) /* anything to flush? */
! 214: return 0; /* just nop if nothing to do */
! 215:
! 216: count = b->counter; /* remember byte count */
! 217: b->counter = BF_FREE; /* reset flag */
! 218: dp = (struct tftphdr *)b->buf;
! 219: nextone = !nextone; /* incr for next time */
! 220: buf = dp->th_data;
! 221:
! 222: if (count <= 0) return -1; /* nak logic? */
! 223:
! 224: if (convert == 0)
! 225: return write(fileno(file), buf, count);
! 226:
! 227: p = buf;
! 228: ct = count;
! 229: while (ct--) { /* loop over the buffer */
! 230: c = *p++; /* pick up a character */
! 231: if (prevchar == '\r') { /* if prev char was cr */
! 232: if (c == '\n') /* if have cr,lf then just */
! 233: fseek(file, -1, 1); /* smash lf on top of the cr */
! 234: else
! 235: if (c == '\0') /* if have cr,nul then */
! 236: goto skipit; /* just skip over the putc */
! 237: /* else just fall through and allow it */
! 238: }
! 239: putc(c, file);
! 240: skipit:
! 241: prevchar = c;
! 242: }
! 243: return count;
! 244: }
! 245:
! 246:
! 247: /* When an error has occurred, it is possible that the two sides
! 248: * are out of synch. Ie: that what I think is the other side's
! 249: * response to packet N is really their response to packet N-1.
! 250: *
! 251: * So, to try to prevent that, we flush all the input queued up
! 252: * for us on the network connection on our host.
! 253: *
! 254: * We return the number of packets we flushed (mostly for reporting
! 255: * when trace is active).
! 256: */
! 257:
! 258: int
! 259: synchnet(f)
! 260: int f; /* socket to flush */
! 261: {
! 262: int i, j = 0;
! 263: char rbuf[PKTSIZE];
! 264: struct sockaddr_in from;
! 265: int fromlen;
! 266:
! 267: while (1) {
! 268: (void) ioctl(f, FIONREAD, &i);
! 269: if (i) {
! 270: j++;
! 271: fromlen = sizeof from;
! 272: (void) recvfrom(f, rbuf, sizeof (rbuf), 0,
! 273: (struct sockaddr *)&from, &fromlen);
! 274: } else {
! 275: return(j);
! 276: }
! 277: }
! 278: }