1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)tftpsubs.c 8.1 (Berkeley) 06/06/93"; 10 #endif /* not lint */ 11 12 /* Simple minded read-ahead/write-behind subroutines for tftp user and 13 server. Written originally with multiple buffers in mind, but current 14 implementation has two buffer logic wired in. 15 16 Todo: add some sort of final error check so when the write-buffer 17 is finally flushed, the caller can detect if the disk filled up 18 (or had an i/o error) and return a nak to the other side. 19 20 Jim Guyton 10/85 21 */ 22 23 #include <sys/types.h> 24 #include <sys/socket.h> 25 #include <sys/ioctl.h> 26 #include <netinet/in.h> 27 #include <arpa/tftp.h> 28 29 #include <stdio.h> 30 #include <unistd.h> 31 32 #include "tftpsubs.h" 33 34 #define PKTSIZE SEGSIZE+4 /* should be moved to tftp.h */ 35 36 struct bf { 37 int counter; /* size of data in buffer, or flag */ 38 char buf[PKTSIZE]; /* room for data packet */ 39 } bfs[2]; 40 41 /* Values for bf.counter */ 42 #define BF_ALLOC -3 /* alloc'd but not yet filled */ 43 #define BF_FREE -2 /* free */ 44 /* [-1 .. SEGSIZE] = size of data in the data buffer */ 45 46 static int nextone; /* index of next buffer to use */ 47 static int current; /* index of buffer in use */ 48 49 /* control flags for crlf conversions */ 50 int newline = 0; /* fillbuf: in middle of newline expansion */ 51 int prevchar = -1; /* putbuf: previous char (cr check) */ 52 53 static struct tftphdr *rw_init(); 54 55 struct tftphdr *w_init() { return rw_init(0); } /* write-behind */ 56 struct tftphdr *r_init() { return rw_init(1); } /* read-ahead */ 57 58 static struct tftphdr * 59 rw_init(x) /* init for either read-ahead or write-behind */ 60 int x; /* zero for write-behind, one for read-head */ 61 { 62 newline = 0; /* init crlf flag */ 63 prevchar = -1; 64 bfs[0].counter = BF_ALLOC; /* pass out the first buffer */ 65 current = 0; 66 bfs[1].counter = BF_FREE; 67 nextone = x; /* ahead or behind? */ 68 return (struct tftphdr *)bfs[0].buf; 69 } 70 71 72 /* Have emptied current buffer by sending to net and getting ack. 73 Free it and return next buffer filled with data. 74 */ 75 int 76 readit(file, dpp, convert) 77 FILE *file; /* file opened for read */ 78 struct tftphdr **dpp; 79 int convert; /* if true, convert to ascii */ 80 { 81 struct bf *b; 82 83 bfs[current].counter = BF_FREE; /* free old one */ 84 current = !current; /* "incr" current */ 85 86 b = &bfs[current]; /* look at new buffer */ 87 if (b->counter == BF_FREE) /* if it's empty */ 88 read_ahead(file, convert); /* fill it */ 89 /* assert(b->counter != BF_FREE);*//* check */ 90 *dpp = (struct tftphdr *)b->buf; /* set caller's ptr */ 91 return b->counter; 92 } 93 94 /* 95 * fill the input buffer, doing ascii conversions if requested 96 * conversions are lf -> cr,lf and cr -> cr, nul 97 */ 98 void 99 read_ahead(file, convert) 100 FILE *file; /* file opened for read */ 101 int convert; /* if true, convert to ascii */ 102 { 103 register int i; 104 register char *p; 105 register int c; 106 struct bf *b; 107 struct tftphdr *dp; 108 109 b = &bfs[nextone]; /* look at "next" buffer */ 110 if (b->counter != BF_FREE) /* nop if not free */ 111 return; 112 nextone = !nextone; /* "incr" next buffer ptr */ 113 114 dp = (struct tftphdr *)b->buf; 115 116 if (convert == 0) { 117 b->counter = read(fileno(file), dp->th_data, SEGSIZE); 118 return; 119 } 120 121 p = dp->th_data; 122 for (i = 0 ; i < SEGSIZE; i++) { 123 if (newline) { 124 if (prevchar == '\n') 125 c = '\n'; /* lf to cr,lf */ 126 else c = '\0'; /* cr to cr,nul */ 127 newline = 0; 128 } 129 else { 130 c = getc(file); 131 if (c == EOF) break; 132 if (c == '\n' || c == '\r') { 133 prevchar = c; 134 c = '\r'; 135 newline = 1; 136 } 137 } 138 *p++ = c; 139 } 140 b->counter = (int)(p - dp->th_data); 141 } 142 143 /* Update count associated with the buffer, get new buffer 144 from the queue. Calls write_behind only if next buffer not 145 available. 146 */ 147 int 148 writeit(file, dpp, ct, convert) 149 FILE *file; 150 struct tftphdr **dpp; 151 int ct, convert; 152 { 153 bfs[current].counter = ct; /* set size of data to write */ 154 current = !current; /* switch to other buffer */ 155 if (bfs[current].counter != BF_FREE) /* if not free */ 156 (void)write_behind(file, convert); /* flush it */ 157 bfs[current].counter = BF_ALLOC; /* mark as alloc'd */ 158 *dpp = (struct tftphdr *)bfs[current].buf; 159 return ct; /* this is a lie of course */ 160 } 161 162 /* 163 * Output a buffer to a file, converting from netascii if requested. 164 * CR,NUL -> CR and CR,LF => LF. 165 * Note spec is undefined if we get CR as last byte of file or a 166 * CR followed by anything else. In this case we leave it alone. 167 */ 168 int 169 write_behind(file, convert) 170 FILE *file; 171 int convert; 172 { 173 char *buf; 174 int count; 175 register int ct; 176 register char *p; 177 register int c; /* current character */ 178 struct bf *b; 179 struct tftphdr *dp; 180 181 b = &bfs[nextone]; 182 if (b->counter < -1) /* anything to flush? */ 183 return 0; /* just nop if nothing to do */ 184 185 count = b->counter; /* remember byte count */ 186 b->counter = BF_FREE; /* reset flag */ 187 dp = (struct tftphdr *)b->buf; 188 nextone = !nextone; /* incr for next time */ 189 buf = dp->th_data; 190 191 if (count <= 0) return -1; /* nak logic? */ 192 193 if (convert == 0) 194 return write(fileno(file), buf, count); 195 196 p = buf; 197 ct = count; 198 while (ct--) { /* loop over the buffer */ 199 c = *p++; /* pick up a character */ 200 if (prevchar == '\r') { /* if prev char was cr */ 201 if (c == '\n') /* if have cr,lf then just */ 202 fseek(file, -1, 1); /* smash lf on top of the cr */ 203 else 204 if (c == '\0') /* if have cr,nul then */ 205 goto skipit; /* just skip over the putc */ 206 /* else just fall through and allow it */ 207 } 208 putc(c, file); 209 skipit: 210 prevchar = c; 211 } 212 return count; 213 } 214 215 216 /* When an error has occurred, it is possible that the two sides 217 * are out of synch. Ie: that what I think is the other side's 218 * response to packet N is really their response to packet N-1. 219 * 220 * So, to try to prevent that, we flush all the input queued up 221 * for us on the network connection on our host. 222 * 223 * We return the number of packets we flushed (mostly for reporting 224 * when trace is active). 225 */ 226 227 int 228 synchnet(f) 229 int f; /* socket to flush */ 230 { 231 int i, j = 0; 232 char rbuf[PKTSIZE]; 233 struct sockaddr_in from; 234 int fromlen; 235 236 while (1) { 237 (void) ioctl(f, FIONREAD, &i); 238 if (i) { 239 j++; 240 fromlen = sizeof from; 241 (void) recvfrom(f, rbuf, sizeof (rbuf), 0, 242 (struct sockaddr *)&from, &fromlen); 243 } else { 244 return(j); 245 } 246 } 247 } 248