126093Sminshall /* 233821Sbostic * Copyright (c) 1983 Regents of the University of California. 333821Sbostic * All rights reserved. 433821Sbostic * 542770Sbostic * %sccs.include.redist.c% 626093Sminshall */ 726093Sminshall 826093Sminshall #ifndef lint 9*46771Sbostic static char sccsid[] = "@(#)tftpsubs.c 5.6 (Berkeley) 02/28/91"; 1033821Sbostic #endif /* not lint */ 1126093Sminshall 1226093Sminshall /* Simple minded read-ahead/write-behind subroutines for tftp user and 1326093Sminshall server. Written originally with multiple buffers in mind, but current 1426093Sminshall implementation has two buffer logic wired in. 1526093Sminshall 1626093Sminshall Todo: add some sort of final error check so when the write-buffer 1726093Sminshall is finally flushed, the caller can detect if the disk filled up 1826093Sminshall (or had an i/o error) and return a nak to the other side. 1926093Sminshall 2026093Sminshall Jim Guyton 10/85 2126093Sminshall */ 2226093Sminshall 2326093Sminshall #include <sys/types.h> 2426093Sminshall #include <sys/socket.h> 2526114Sminshall #include <sys/ioctl.h> 2626093Sminshall #include <netinet/in.h> 2726093Sminshall #include <arpa/tftp.h> 2826093Sminshall #include <stdio.h> 2926093Sminshall 3026093Sminshall #define PKTSIZE SEGSIZE+4 /* should be moved to tftp.h */ 3126093Sminshall 3226093Sminshall struct bf { 3326093Sminshall int counter; /* size of data in buffer, or flag */ 3426093Sminshall char buf[PKTSIZE]; /* room for data packet */ 3526093Sminshall } bfs[2]; 3626093Sminshall 3726093Sminshall /* Values for bf.counter */ 3826093Sminshall #define BF_ALLOC -3 /* alloc'd but not yet filled */ 3926093Sminshall #define BF_FREE -2 /* free */ 4026093Sminshall /* [-1 .. SEGSIZE] = size of data in the data buffer */ 4126093Sminshall 4226093Sminshall static int nextone; /* index of next buffer to use */ 4326093Sminshall static int current; /* index of buffer in use */ 4426093Sminshall 4526093Sminshall /* control flags for crlf conversions */ 4626093Sminshall int newline = 0; /* fillbuf: in middle of newline expansion */ 4726093Sminshall int prevchar = -1; /* putbuf: previous char (cr check) */ 4826093Sminshall 4926093Sminshall struct tftphdr *rw_init(); 5026093Sminshall 5126093Sminshall struct tftphdr *w_init() { return rw_init(0); } /* write-behind */ 5226093Sminshall struct tftphdr *r_init() { return rw_init(1); } /* read-ahead */ 5326093Sminshall 5426093Sminshall struct tftphdr * 5526093Sminshall rw_init(x) /* init for either read-ahead or write-behind */ 5626093Sminshall int x; /* zero for write-behind, one for read-head */ 5726093Sminshall { 5826093Sminshall newline = 0; /* init crlf flag */ 5926093Sminshall prevchar = -1; 6026093Sminshall bfs[0].counter = BF_ALLOC; /* pass out the first buffer */ 6126093Sminshall current = 0; 6226093Sminshall bfs[1].counter = BF_FREE; 6326093Sminshall nextone = x; /* ahead or behind? */ 6426093Sminshall return (struct tftphdr *)bfs[0].buf; 6526093Sminshall } 6626093Sminshall 6726093Sminshall 6826093Sminshall /* Have emptied current buffer by sending to net and getting ack. 6926093Sminshall Free it and return next buffer filled with data. 7026093Sminshall */ 7126093Sminshall readit(file, dpp, convert) 7226093Sminshall FILE *file; /* file opened for read */ 7326093Sminshall struct tftphdr **dpp; 7426093Sminshall int convert; /* if true, convert to ascii */ 7526093Sminshall { 7626093Sminshall struct bf *b; 7726093Sminshall 7826093Sminshall bfs[current].counter = BF_FREE; /* free old one */ 7926093Sminshall current = !current; /* "incr" current */ 8026093Sminshall 8126093Sminshall b = &bfs[current]; /* look at new buffer */ 8226093Sminshall if (b->counter == BF_FREE) /* if it's empty */ 8326093Sminshall read_ahead(file, convert); /* fill it */ 8426093Sminshall /* assert(b->counter != BF_FREE); /* check */ 8526093Sminshall *dpp = (struct tftphdr *)b->buf; /* set caller's ptr */ 8626093Sminshall return b->counter; 8726093Sminshall } 8826093Sminshall 8926093Sminshall /* 9026093Sminshall * fill the input buffer, doing ascii conversions if requested 9126093Sminshall * conversions are lf -> cr,lf and cr -> cr, nul 9226093Sminshall */ 9326093Sminshall read_ahead(file, convert) 9426093Sminshall FILE *file; /* file opened for read */ 9526093Sminshall int convert; /* if true, convert to ascii */ 9626093Sminshall { 9726093Sminshall register int i; 9826093Sminshall register char *p; 9926093Sminshall register int c; 10026093Sminshall struct bf *b; 10126093Sminshall struct tftphdr *dp; 10226093Sminshall 10326093Sminshall b = &bfs[nextone]; /* look at "next" buffer */ 10426093Sminshall if (b->counter != BF_FREE) /* nop if not free */ 10526093Sminshall return; 10626093Sminshall nextone = !nextone; /* "incr" next buffer ptr */ 10726093Sminshall 10826093Sminshall dp = (struct tftphdr *)b->buf; 10926093Sminshall 11026093Sminshall if (convert == 0) { 11126093Sminshall b->counter = read(fileno(file), dp->th_data, SEGSIZE); 11226093Sminshall return; 11326093Sminshall } 11426093Sminshall 11526093Sminshall p = dp->th_data; 11626093Sminshall for (i = 0 ; i < SEGSIZE; i++) { 11726093Sminshall if (newline) { 11826093Sminshall if (prevchar == '\n') 11926093Sminshall c = '\n'; /* lf to cr,lf */ 12026093Sminshall else c = '\0'; /* cr to cr,nul */ 12126093Sminshall newline = 0; 12226093Sminshall } 12326093Sminshall else { 12426093Sminshall c = getc(file); 12526093Sminshall if (c == EOF) break; 12626093Sminshall if (c == '\n' || c == '\r') { 12726093Sminshall prevchar = c; 12826093Sminshall c = '\r'; 12926093Sminshall newline = 1; 13026093Sminshall } 13126093Sminshall } 13226093Sminshall *p++ = c; 13326093Sminshall } 13426093Sminshall b->counter = (int)(p - dp->th_data); 13526093Sminshall } 13626093Sminshall 13726093Sminshall /* Update count associated with the buffer, get new buffer 13826093Sminshall from the queue. Calls write_behind only if next buffer not 13926093Sminshall available. 14026093Sminshall */ 14126093Sminshall writeit(file, dpp, ct, convert) 14226093Sminshall FILE *file; 14326093Sminshall struct tftphdr **dpp; 14426093Sminshall int convert; 14526093Sminshall { 14626093Sminshall bfs[current].counter = ct; /* set size of data to write */ 14726093Sminshall current = !current; /* switch to other buffer */ 14826093Sminshall if (bfs[current].counter != BF_FREE) /* if not free */ 14926093Sminshall write_behind(file, convert); /* flush it */ 15026093Sminshall bfs[current].counter = BF_ALLOC; /* mark as alloc'd */ 15126093Sminshall *dpp = (struct tftphdr *)bfs[current].buf; 15226093Sminshall return ct; /* this is a lie of course */ 15326093Sminshall } 15426093Sminshall 15526093Sminshall /* 15626093Sminshall * Output a buffer to a file, converting from netascii if requested. 15726093Sminshall * CR,NUL -> CR and CR,LF => LF. 15826093Sminshall * Note spec is undefined if we get CR as last byte of file or a 15926093Sminshall * CR followed by anything else. In this case we leave it alone. 16026093Sminshall */ 16126093Sminshall write_behind(file, convert) 16226093Sminshall FILE *file; 16326093Sminshall int convert; 16426093Sminshall { 16526093Sminshall char *buf; 16626093Sminshall int count; 16726093Sminshall register int ct; 16826093Sminshall register char *p; 16926093Sminshall register int c; /* current character */ 17026093Sminshall struct bf *b; 17126093Sminshall struct tftphdr *dp; 17226093Sminshall 17326093Sminshall b = &bfs[nextone]; 17426093Sminshall if (b->counter < -1) /* anything to flush? */ 17526093Sminshall return 0; /* just nop if nothing to do */ 17626093Sminshall 17726093Sminshall count = b->counter; /* remember byte count */ 17826093Sminshall b->counter = BF_FREE; /* reset flag */ 17926093Sminshall dp = (struct tftphdr *)b->buf; 18026093Sminshall nextone = !nextone; /* incr for next time */ 18126093Sminshall buf = dp->th_data; 18226093Sminshall 18326093Sminshall if (count <= 0) return -1; /* nak logic? */ 18426093Sminshall 18526093Sminshall if (convert == 0) 18626093Sminshall return write(fileno(file), buf, count); 18726093Sminshall 18826093Sminshall p = buf; 18926093Sminshall ct = count; 19026093Sminshall while (ct--) { /* loop over the buffer */ 19126093Sminshall c = *p++; /* pick up a character */ 19226093Sminshall if (prevchar == '\r') { /* if prev char was cr */ 19326093Sminshall if (c == '\n') /* if have cr,lf then just */ 19426093Sminshall fseek(file, -1, 1); /* smash lf on top of the cr */ 19526093Sminshall else 19626093Sminshall if (c == '\0') /* if have cr,nul then */ 19726093Sminshall goto skipit; /* just skip over the putc */ 19826093Sminshall /* else just fall through and allow it */ 19926093Sminshall } 20026093Sminshall putc(c, file); 20126093Sminshall skipit: 20226093Sminshall prevchar = c; 20326093Sminshall } 20426093Sminshall return count; 20526093Sminshall } 20626093Sminshall 20726093Sminshall 20826114Sminshall /* When an error has occurred, it is possible that the two sides 20926114Sminshall * are out of synch. Ie: that what I think is the other side's 21026114Sminshall * response to packet N is really their response to packet N-1. 21126114Sminshall * 21226114Sminshall * So, to try to prevent that, we flush all the input queued up 21326114Sminshall * for us on the network connection on our host. 21426114Sminshall * 21526114Sminshall * We return the number of packets we flushed (mostly for reporting 21626114Sminshall * when trace is active). 21726114Sminshall */ 21826114Sminshall 21926114Sminshall int 22026114Sminshall synchnet(f) 22126114Sminshall int f; /* socket to flush */ 22226114Sminshall { 22326114Sminshall int i, j = 0; 22426114Sminshall char rbuf[PKTSIZE]; 22526114Sminshall struct sockaddr_in from; 22626114Sminshall int fromlen; 22726114Sminshall 22826114Sminshall while (1) { 22926114Sminshall (void) ioctl(f, FIONREAD, &i); 23026114Sminshall if (i) { 23126114Sminshall j++; 23226114Sminshall fromlen = sizeof from; 23326114Sminshall (void) recvfrom(f, rbuf, sizeof (rbuf), 0, 234*46771Sbostic (struct sockaddr *)&from, &fromlen); 23526114Sminshall } else { 23626114Sminshall return(j); 23726114Sminshall } 23826114Sminshall } 23926114Sminshall } 240