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