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