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