| Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * |
| 9 | * Trivial file transfer protocol server. |
| 10 | * |
| 11 | * This code includes many modifications by Jim Guyton <guyton@rand-unix> |
| 12 | * |
| 13 | * This source file was started based on netkit-tftpd 0.17 |
| 14 | * Heavily modified for curl's test suite |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Copyright (c) 1983 Regents of the University of California. |
| 19 | * All rights reserved. |
| 20 | * |
| 21 | * Redistribution and use in source and binary forms, with or without |
| 22 | * modification, are permitted provided that the following conditions |
| 23 | * are met: |
| 24 | * 1. Redistributions of source code must retain the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer. |
| 26 | * 2. Redistributions in binary form must reproduce the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer in the |
| 28 | * documentation and/or other materials provided with the distribution. |
| 29 | * 3. All advertising materials mentioning features or use of this software |
| 30 | * must display the following acknowledgement: |
| 31 | * This product includes software developed by the University of |
| 32 | * California, Berkeley and its contributors. |
| 33 | * 4. Neither the name of the University nor the names of its contributors |
| 34 | * may be used to endorse or promote products derived from this software |
| 35 | * without specific prior written permission. |
| 36 | * |
| 37 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 38 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 39 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 40 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 41 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 42 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 43 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 45 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 46 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 47 | * SUCH DAMAGE. |
| 48 | */ |
| 49 | |
| 50 | #define CURL_NO_OLDIES |
| 51 | |
| 52 | #include "setup.h" /* portability help from the lib directory */ |
| 53 | |
| 54 | #ifdef HAVE_SYS_IOCTL_H |
| 55 | #include <sys/ioctl.h> |
| 56 | #endif |
| 57 | #ifdef HAVE_SIGNAL_H |
| 58 | #include <signal.h> |
| 59 | #endif |
| 60 | #ifdef HAVE_FCNTL_H |
| 61 | #include <fcntl.h> |
| 62 | #endif |
| 63 | #ifdef HAVE_SYS_SOCKET_H |
| 64 | #include <sys/socket.h> |
| 65 | #endif |
| 66 | #ifdef HAVE_NETINET_IN_H |
| 67 | #include <netinet/in.h> |
| 68 | #endif |
| 69 | #ifdef HAVE_ARPA_INET_H |
| 70 | #include <arpa/inet.h> |
| 71 | #endif |
| 72 | #ifdef HAVE_ARPA_TFTP_H |
| 73 | #include <arpa/tftp.h> |
| 74 | #else |
| 75 | #include "tftp.h" |
| 76 | #endif |
| 77 | #ifdef HAVE_NETDB_H |
| 78 | #include <netdb.h> |
| 79 | #endif |
| 80 | #ifdef HAVE_SYS_FILIO_H |
| 81 | /* FIONREAD on Solaris 7 */ |
| 82 | #include <sys/filio.h> |
| 83 | #endif |
| 84 | |
| 85 | #include <setjmp.h> |
| 86 | #ifdef HAVE_UNISTD_H |
| 87 | #include <unistd.h> |
| 88 | #endif |
| 89 | #ifdef HAVE_PWD_H |
| 90 | #include <pwd.h> |
| 91 | #endif |
| 92 | |
| 93 | #define ENABLE_CURLX_PRINTF |
| 94 | /* make the curlx header define all printf() functions to use the curlx_* |
| 95 | versions instead */ |
| 96 | #include "curlx.h" /* from the private lib dir */ |
| 97 | #include "getpart.h" |
| 98 | #include "util.h" |
| 99 | |
| 100 | /* include memdebug.h last */ |
| 101 | #include "memdebug.h" |
| 102 | |
| 103 | /***************************************************************************** |
| 104 | * STRUCT DECLARATIONS AND DEFINES * |
| 105 | *****************************************************************************/ |
| 106 | |
| 107 | #ifndef PKTSIZE |
| 108 | #define PKTSIZE (SEGSIZE + 4) /* SEGSIZE defined in arpa/tftp.h */ |
| 109 | #endif |
| 110 | |
| 111 | struct testcase { |
| 112 | char *buffer; /* holds the file data to send to the client */ |
| 113 | size_t bufsize; /* size of the data in buffer */ |
| 114 | char *rptr; /* read pointer into the buffer */ |
| 115 | size_t rcount; /* amount of data left to read of the file */ |
| 116 | long num; /* test case number */ |
| 117 | int ofile; /* file descriptor for output file when uploading to us */ |
| 118 | }; |
| 119 | |
| 120 | struct formats { |
| 121 | const char *f_mode; |
| 122 | int f_convert; |
| 123 | }; |
| 124 | |
| 125 | struct errmsg { |
| 126 | int e_code; |
| 127 | const char *e_msg; |
| 128 | }; |
| 129 | |
| 130 | /* |
| 131 | * bf.counter values in range [-1 .. SEGSIZE] represents size of data in the |
| 132 | * bf.buf buffer. Additionally it can also hold flags BF_ALLOC or BF_FREE. |
| 133 | */ |
| 134 | |
| 135 | struct bf { |
| 136 | int counter; /* size of data in buffer, or flag */ |
| 137 | char buf[PKTSIZE]; /* room for data packet */ |
| 138 | }; |
| 139 | |
| 140 | #define BF_ALLOC -3 /* alloc'd but not yet filled */ |
| 141 | #define BF_FREE -2 /* free */ |
| 142 | |
| 143 | #define opcode_RRQ 1 |
| 144 | #define opcode_WRQ 2 |
| 145 | #define opcode_DATA 3 |
| 146 | #define opcode_ACK 4 |
| 147 | #define opcode_ERROR 5 |
| 148 | |
| 149 | #define TIMEOUT 5 |
| 150 | |
| 151 | #undef MIN |
| 152 | #define MIN(x,y) ((x)<(y)?(x):(y)) |
| 153 | |
| 154 | #ifndef DEFAULT_LOGFILE |
| 155 | #define DEFAULT_LOGFILE "log/tftpd.log" |
| 156 | #endif |
| 157 | |
| 158 | #define REQUEST_DUMP "log/server.input" |
| 159 | |
| 160 | #define DEFAULT_PORT 8999 /* UDP */ |
| 161 | |
| 162 | /***************************************************************************** |
| 163 | * GLOBAL VARIABLES * |
| 164 | *****************************************************************************/ |
| 165 | |
| 166 | static struct errmsg errmsgs[] = { |
| 167 | { EUNDEF, "Undefined error code" }, |
| 168 | { ENOTFOUND, "File not found" }, |
| 169 | { EACCESS, "Access violation" }, |
| 170 | { ENOSPACE, "Disk full or allocation exceeded" }, |
| 171 | { EBADOP, "Illegal TFTP operation" }, |
| 172 | { EBADID, "Unknown transfer ID" }, |
| 173 | { EEXISTS, "File already exists" }, |
| 174 | { ENOUSER, "No such user" }, |
| 175 | { -1, 0 } |
| 176 | }; |
| 177 | |
| 178 | static struct formats formata[] = { |
| 179 | { "netascii", 1 }, |
| 180 | { "octet", 0 }, |
| 181 | { NULL, 0 } |
| 182 | }; |
| 183 | |
| 184 | static struct bf bfs[2]; |
| 185 | |
| 186 | static int nextone; /* index of next buffer to use */ |
| 187 | static int current; /* index of buffer in use */ |
| 188 | |
| 189 | /* control flags for crlf conversions */ |
| 190 | static int newline = 0; /* fillbuf: in middle of newline expansion */ |
| 191 | static int prevchar = -1; /* putbuf: previous char (cr check) */ |
| 192 | |
| 193 | static char buf[PKTSIZE]; |
| 194 | static char ackbuf[PKTSIZE]; |
| 195 | |
| 196 | static struct sockaddr_in from; |
| 197 | static curl_socklen_t fromlen; |
| 198 | |
| 199 | static curl_socket_t peer = CURL_SOCKET_BAD; |
| 200 | |
| 201 | static int timeout; |
| 202 | static int maxtimeout = 5 * TIMEOUT; |
| 203 | |
| 204 | static unsigned short sendblock; /* block count used by sendtftp() */ |
| 205 | static struct tftphdr *sdp; /* data buffer used by sendtftp() */ |
| 206 | static struct tftphdr *sap; /* ack buffer used by sendtftp() */ |
| 207 | |
| 208 | static unsigned short recvblock; /* block count used by recvtftp() */ |
| 209 | static struct tftphdr *rdp; /* data buffer used by recvtftp() */ |
| 210 | static struct tftphdr *rap; /* ack buffer used by recvtftp() */ |
| 211 | |
| 212 | #ifdef ENABLE_IPV6 |
| 213 | static bool use_ipv6 = FALSE; |
| 214 | #endif |
| 215 | static const char *ipv_inuse = "IPv4"; |
| 216 | |
| 217 | const char *serverlogfile = DEFAULT_LOGFILE; |
| 218 | static char *pidname= (char *)".tftpd.pid"; |
| 219 | static int serverlogslocked = 0; |
| 220 | static int wrotepidfile = 0; |
| 221 | |
| 222 | #ifdef HAVE_SIGSETJMP |
| 223 | static sigjmp_buf timeoutbuf; |
| 224 | #endif |
| 225 | |
| 226 | #if defined(HAVE_ALARM) && defined(SIGALRM) |
| 227 | static int rexmtval = TIMEOUT; |
| 228 | #endif |
| 229 | |
| 230 | /* do-nothing macro replacement for systems which lack siginterrupt() */ |
| 231 | |
| 232 | #ifndef HAVE_SIGINTERRUPT |
| 233 | #define siginterrupt(x,y) do {} while(0) |
| 234 | #endif |
| 235 | |
| 236 | /* vars used to keep around previous signal handlers */ |
| 237 | |
| 238 | typedef RETSIGTYPE (*SIGHANDLER_T)(int); |
| 239 | |
| 240 | #ifdef SIGHUP |
| 241 | static SIGHANDLER_T old_sighup_handler = SIG_ERR; |
| 242 | #endif |
| 243 | |
| 244 | #ifdef SIGPIPE |
| 245 | static SIGHANDLER_T old_sigpipe_handler = SIG_ERR; |
| 246 | #endif |
| 247 | |
| 248 | #ifdef SIGINT |
| 249 | static SIGHANDLER_T old_sigint_handler = SIG_ERR; |
| 250 | #endif |
| 251 | |
| 252 | #ifdef SIGTERM |
| 253 | static SIGHANDLER_T old_sigterm_handler = SIG_ERR; |
| 254 | #endif |
| 255 | |
| 256 | /* var which if set indicates that the program should finish execution */ |
| 257 | |
| 258 | SIG_ATOMIC_T got_exit_signal = 0; |
| 259 | |
| 260 | /* if next is set indicates the first signal handled in exit_signal_handler */ |
| 261 | |
| 262 | static volatile int exit_signal = 0; |
| 263 | |
| 264 | /***************************************************************************** |
| 265 | * FUNCTION PROTOTYPES * |
| 266 | *****************************************************************************/ |
| 267 | |
| 268 | static struct tftphdr *rw_init(int); |
| 269 | |
| 270 | static struct tftphdr *w_init(void); |
| 271 | |
| 272 | static struct tftphdr *r_init(void); |
| 273 | |
| 274 | static int readit(struct testcase *test, |
| 275 | struct tftphdr **dpp, |
| 276 | int convert); |
| 277 | |
| 278 | static int writeit(struct testcase *test, |
| 279 | struct tftphdr **dpp, |
| 280 | int ct, |
| 281 | int convert); |
| 282 | |
| 283 | static void read_ahead(struct testcase *test, int convert); |
| 284 | |
| 285 | static ssize_t write_behind(struct testcase *test, int convert); |
| 286 | |
| 287 | static int synchnet(curl_socket_t); |
| 288 | |
| 289 | static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size); |
| 290 | |
| 291 | static int validate_access(struct testcase *test, const char *fname, int mode); |
| 292 | |
| 293 | static void sendtftp(struct testcase *test, struct formats *pf); |
| 294 | |
| 295 | static void recvtftp(struct testcase *test, struct formats *pf); |
| 296 | |
| 297 | static void nak(int error); |
| 298 | |
| 299 | #if defined(HAVE_ALARM) && defined(SIGALRM) |
| 300 | |
| 301 | static void mysignal(int sig, void (*handler)(int)); |
| 302 | |
| 303 | static void timer(int signum); |
| 304 | |
| 305 | static void justtimeout(int signum); |
| 306 | |
| 307 | #endif /* HAVE_ALARM && SIGALRM */ |
| 308 | |
| 309 | static RETSIGTYPE exit_signal_handler(int signum); |
| 310 | |
| 311 | static void install_signal_handlers(void); |
| 312 | |
| 313 | static void restore_signal_handlers(void); |
| 314 | |
| 315 | /***************************************************************************** |
| 316 | * FUNCTION IMPLEMENTATIONS * |
| 317 | *****************************************************************************/ |
| 318 | |
| 319 | #if defined(HAVE_ALARM) && defined(SIGALRM) |
| 320 | |
| 321 | /* |
| 322 | * Like signal(), but with well-defined semantics. |
| 323 | */ |
| 324 | static void mysignal(int sig, void (*handler)(int)) |
| 325 | { |
| 326 | struct sigaction sa; |
| 327 | memset(&sa, 0, sizeof(sa)); |
| 328 | sa.sa_handler = handler; |
| 329 | sigaction(sig, &sa, NULL); |
| 330 | } |
| 331 | |
| 332 | static void timer(int signum) |
| 333 | { |
| 334 | (void)signum; |
| 335 | |
| 336 | logmsg("alarm!"); |
| 337 | |
| 338 | timeout += rexmtval; |
| 339 | if(timeout >= maxtimeout) { |
| 340 | if(wrotepidfile) { |
| 341 | wrotepidfile = 0; |
| 342 | unlink(pidname); |
| 343 | } |
| 344 | if(serverlogslocked) { |
| 345 | serverlogslocked = 0; |
| 346 | clear_advisor_read_lock(SERVERLOGS_LOCK); |
| 347 | } |
| 348 | exit(1); |
| 349 | } |
| 350 | #ifdef HAVE_SIGSETJMP |
| 351 | siglongjmp(timeoutbuf, 1); |
| 352 | #endif |
| 353 | } |
| 354 | |
| 355 | static void justtimeout(int signum) |
| 356 | { |
| 357 | (void)signum; |
| 358 | } |
| 359 | |
| 360 | #endif /* HAVE_ALARM && SIGALRM */ |
| 361 | |
| 362 | /* signal handler that will be triggered to indicate that the program |
| 363 | should finish its execution in a controlled manner as soon as possible. |
| 364 | The first time this is called it will set got_exit_signal to one and |
| 365 | store in exit_signal the signal that triggered its execution. */ |
| 366 | |
| 367 | static RETSIGTYPE exit_signal_handler(int signum) |
| 368 | { |
| 369 | int old_errno = ERRNO; |
| 370 | if(got_exit_signal == 0) { |
| 371 | got_exit_signal = 1; |
| 372 | exit_signal = signum; |
| 373 | } |
| 374 | (void)signal(signum, exit_signal_handler); |
| 375 | SET_ERRNO(old_errno); |
| 376 | } |
| 377 | |
| 378 | static void install_signal_handlers(void) |
| 379 | { |
| 380 | #ifdef SIGHUP |
| 381 | /* ignore SIGHUP signal */ |
| 382 | if((old_sighup_handler = signal(SIGHUP, SIG_IGN)) == SIG_ERR) |
| 383 | logmsg("cannot install SIGHUP handler: %s", strerror(ERRNO)); |
| 384 | #endif |
| 385 | #ifdef SIGPIPE |
| 386 | /* ignore SIGPIPE signal */ |
| 387 | if((old_sigpipe_handler = signal(SIGPIPE, SIG_IGN)) == SIG_ERR) |
| 388 | logmsg("cannot install SIGPIPE handler: %s", strerror(ERRNO)); |
| 389 | #endif |
| 390 | #ifdef SIGINT |
| 391 | /* handle SIGINT signal with our exit_signal_handler */ |
| 392 | if((old_sigint_handler = signal(SIGINT, exit_signal_handler)) == SIG_ERR) |
| 393 | logmsg("cannot install SIGINT handler: %s", strerror(ERRNO)); |
| 394 | else |
| 395 | siginterrupt(SIGINT, 1); |
| 396 | #endif |
| 397 | #ifdef SIGTERM |
| 398 | /* handle SIGTERM signal with our exit_signal_handler */ |
| 399 | if((old_sigterm_handler = signal(SIGTERM, exit_signal_handler)) == SIG_ERR) |
| 400 | logmsg("cannot install SIGTERM handler: %s", strerror(ERRNO)); |
| 401 | else |
| 402 | siginterrupt(SIGTERM, 1); |
| 403 | #endif |
| 404 | } |
| 405 | |
| 406 | static void restore_signal_handlers(void) |
| 407 | { |
| 408 | #ifdef SIGHUP |
| 409 | if(SIG_ERR != old_sighup_handler) |
| 410 | (void)signal(SIGHUP, old_sighup_handler); |
| 411 | #endif |
| 412 | #ifdef SIGPIPE |
| 413 | if(SIG_ERR != old_sigpipe_handler) |
| 414 | (void)signal(SIGPIPE, old_sigpipe_handler); |
| 415 | #endif |
| 416 | #ifdef SIGINT |
| 417 | if(SIG_ERR != old_sigint_handler) |
| 418 | (void)signal(SIGINT, old_sigint_handler); |
| 419 | #endif |
| 420 | #ifdef SIGTERM |
| 421 | if(SIG_ERR != old_sigterm_handler) |
| 422 | (void)signal(SIGTERM, old_sigterm_handler); |
| 423 | #endif |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * init for either read-ahead or write-behind. |
| 428 | * zero for write-behind, one for read-head. |
| 429 | */ |
| 430 | static struct tftphdr *rw_init(int x) |
| 431 | { |
| 432 | newline = 0; /* init crlf flag */ |
| 433 | prevchar = -1; |
| 434 | bfs[0].counter = BF_ALLOC; /* pass out the first buffer */ |
| 435 | current = 0; |
| 436 | bfs[1].counter = BF_FREE; |
| 437 | nextone = x; /* ahead or behind? */ |
| 438 | return (struct tftphdr *)bfs[0].buf; |
| 439 | } |
| 440 | |
| 441 | static struct tftphdr *w_init(void) |
| 442 | { |
| 443 | return rw_init(0); /* write-behind */ |
| 444 | } |
| 445 | |
| 446 | static struct tftphdr *r_init(void) |
| 447 | { |
| 448 | return rw_init(1); /* read-ahead */ |
| 449 | } |
| 450 | |
| 451 | /* Have emptied current buffer by sending to net and getting ack. |
| 452 | Free it and return next buffer filled with data. |
| 453 | */ |
| 454 | static int readit(struct testcase *test, struct tftphdr **dpp, |
| 455 | int convert /* if true, convert to ascii */) |
| 456 | { |
| 457 | struct bf *b; |
| 458 | |
| 459 | bfs[current].counter = BF_FREE; /* free old one */ |
| 460 | current = !current; /* "incr" current */ |
| 461 | |
| 462 | b = &bfs[current]; /* look at new buffer */ |
| 463 | if (b->counter == BF_FREE) /* if it's empty */ |
| 464 | read_ahead(test, convert); /* fill it */ |
| 465 | |
| 466 | *dpp = (struct tftphdr *)b->buf; /* set caller's ptr */ |
| 467 | return b->counter; |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * fill the input buffer, doing ascii conversions if requested |
| 472 | * conversions are lf -> cr,lf and cr -> cr, nul |
| 473 | */ |
| 474 | static void read_ahead(struct testcase *test, |
| 475 | int convert /* if true, convert to ascii */) |
| 476 | { |
| 477 | int i; |
| 478 | char *p; |
| 479 | int c; |
| 480 | struct bf *b; |
| 481 | struct tftphdr *dp; |
| 482 | |
| 483 | b = &bfs[nextone]; /* look at "next" buffer */ |
| 484 | if (b->counter != BF_FREE) /* nop if not free */ |
| 485 | return; |
| 486 | nextone = !nextone; /* "incr" next buffer ptr */ |
| 487 | |
| 488 | dp = (struct tftphdr *)b->buf; |
| 489 | |
| 490 | if (convert == 0) { |
| 491 | /* The former file reading code did this: |
| 492 | b->counter = read(fileno(file), dp->th_data, SEGSIZE); */ |
| 493 | size_t copy_n = MIN(SEGSIZE, test->rcount); |
| 494 | memcpy(dp->th_data, test->rptr, copy_n); |
| 495 | |
| 496 | /* decrease amount, advance pointer */ |
| 497 | test->rcount -= copy_n; |
| 498 | test->rptr += copy_n; |
| 499 | b->counter = (int)copy_n; |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | p = dp->th_data; |
| 504 | for (i = 0 ; i < SEGSIZE; i++) { |
| 505 | if (newline) { |
| 506 | if (prevchar == '\n') |
| 507 | c = '\n'; /* lf to cr,lf */ |
| 508 | else |
| 509 | c = '\0'; /* cr to cr,nul */ |
| 510 | newline = 0; |
| 511 | } |
| 512 | else { |
| 513 | if(test->rcount) { |
| 514 | c=test->rptr[0]; |
| 515 | test->rptr++; |
| 516 | test->rcount--; |
| 517 | } |
| 518 | else |
| 519 | break; |
| 520 | if (c == '\n' || c == '\r') { |
| 521 | prevchar = c; |
| 522 | c = '\r'; |
| 523 | newline = 1; |
| 524 | } |
| 525 | } |
| 526 | *p++ = (char)c; |
| 527 | } |
| 528 | b->counter = (int)(p - dp->th_data); |
| 529 | } |
| 530 | |
| 531 | /* Update count associated with the buffer, get new buffer from the queue. |
| 532 | Calls write_behind only if next buffer not available. |
| 533 | */ |
| 534 | static int writeit(struct testcase *test, struct tftphdr **dpp, |
| 535 | int ct, int convert) |
| 536 | { |
| 537 | bfs[current].counter = ct; /* set size of data to write */ |
| 538 | current = !current; /* switch to other buffer */ |
| 539 | if (bfs[current].counter != BF_FREE) /* if not free */ |
| 540 | write_behind(test, convert); /* flush it */ |
| 541 | bfs[current].counter = BF_ALLOC; /* mark as alloc'd */ |
| 542 | *dpp = (struct tftphdr *)bfs[current].buf; |
| 543 | return ct; /* this is a lie of course */ |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | * Output a buffer to a file, converting from netascii if requested. |
| 548 | * CR,NUL -> CR and CR,LF => LF. |
| 549 | * Note spec is undefined if we get CR as last byte of file or a |
| 550 | * CR followed by anything else. In this case we leave it alone. |
| 551 | */ |
| 552 | static ssize_t write_behind(struct testcase *test, int convert) |
| 553 | { |
| 554 | char *writebuf; |
| 555 | int count; |
| 556 | int ct; |
| 557 | char *p; |
| 558 | int c; /* current character */ |
| 559 | struct bf *b; |
| 560 | struct tftphdr *dp; |
| 561 | |
| 562 | b = &bfs[nextone]; |
| 563 | if (b->counter < -1) /* anything to flush? */ |
| 564 | return 0; /* just nop if nothing to do */ |
| 565 | |
| 566 | if(!test->ofile) { |
| 567 | char outfile[256]; |
| 568 | snprintf(outfile, sizeof(outfile), "log/upload.%ld", test->num); |
| 569 | test->ofile=open(outfile, O_CREAT|O_RDWR, 0777); |
| 570 | if(test->ofile == -1) { |
| 571 | logmsg("Couldn't create and/or open file %s for upload!", outfile); |
| 572 | return -1; /* failure! */ |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | count = b->counter; /* remember byte count */ |
| 577 | b->counter = BF_FREE; /* reset flag */ |
| 578 | dp = (struct tftphdr *)b->buf; |
| 579 | nextone = !nextone; /* incr for next time */ |
| 580 | writebuf = dp->th_data; |
| 581 | |
| 582 | if (count <= 0) |
| 583 | return -1; /* nak logic? */ |
| 584 | |
| 585 | if (convert == 0) |
| 586 | return write(test->ofile, writebuf, count); |
| 587 | |
| 588 | p = writebuf; |
| 589 | ct = count; |
| 590 | while (ct--) { /* loop over the buffer */ |
| 591 | c = *p++; /* pick up a character */ |
| 592 | if (prevchar == '\r') { /* if prev char was cr */ |
| 593 | if (c == '\n') /* if have cr,lf then just */ |
| 594 | lseek(test->ofile, -1, SEEK_CUR); /* smash lf on top of the cr */ |
| 595 | else |
| 596 | if (c == '\0') /* if have cr,nul then */ |
| 597 | goto skipit; /* just skip over the putc */ |
| 598 | /* else just fall through and allow it */ |
| 599 | } |
| 600 | /* formerly |
| 601 | putc(c, file); */ |
| 602 | write(test->ofile, &c, 1); |
| 603 | skipit: |
| 604 | prevchar = c; |
| 605 | } |
| 606 | return count; |
| 607 | } |
| 608 | |
| 609 | /* When an error has occurred, it is possible that the two sides are out of |
| 610 | * synch. Ie: that what I think is the other side's response to packet N is |
| 611 | * really their response to packet N-1. |
| 612 | * |
| 613 | * So, to try to prevent that, we flush all the input queued up for us on the |
| 614 | * network connection on our host. |
| 615 | * |
| 616 | * We return the number of packets we flushed (mostly for reporting when trace |
| 617 | * is active). |
| 618 | */ |
| 619 | |
| 620 | static int synchnet(curl_socket_t f /* socket to flush */) |
| 621 | { |
| 622 | |
| 623 | #if defined(HAVE_IOCTLSOCKET) |
| 624 | unsigned long i; |
| 625 | #else |
| 626 | int i; |
| 627 | #endif |
| 628 | int j = 0; |
| 629 | char rbuf[PKTSIZE]; |
| 630 | struct sockaddr_in fromaddr; |
| 631 | curl_socklen_t fromaddrlen; |
| 632 | |
| 633 | for (;;) { |
| 634 | #if defined(HAVE_IOCTLSOCKET) |
| 635 | (void) ioctlsocket(f, FIONREAD, &i); |
| 636 | #else |
| 637 | (void) ioctl(f, FIONREAD, &i); |
| 638 | #endif |
| 639 | if (i) { |
| 640 | j++; |
| 641 | fromaddrlen = sizeof(fromaddr); |
| 642 | (void)recvfrom(f, rbuf, sizeof(rbuf), 0, |
| 643 | (struct sockaddr *)&fromaddr, &fromaddrlen); |
| 644 | } |
| 645 | else |
| 646 | break; |
| 647 | } |
| 648 | return j; |
| 649 | } |
| 650 | |
| 651 | int main(int argc, char **argv) |
| 652 | { |
| 653 | struct sockaddr_in me; |
| 654 | #ifdef ENABLE_IPV6 |
| 655 | struct sockaddr_in6 me6; |
| 656 | #endif /* ENABLE_IPV6 */ |
| 657 | |
| 658 | struct tftphdr *tp; |
| 659 | ssize_t n = 0; |
| 660 | int arg = 1; |
| 661 | unsigned short port = DEFAULT_PORT; |
| 662 | curl_socket_t sock = CURL_SOCKET_BAD; |
| 663 | int flag; |
| 664 | int rc; |
| 665 | int error; |
| 666 | long pid; |
| 667 | struct testcase test; |
| 668 | int result = 0; |
| 669 | |
| 670 | memset(&test, 0, sizeof(test)); |
| 671 | |
| 672 | while(argc>arg) { |
| 673 | if(!strcmp("--version", argv[arg])) { |
| 674 | printf("tftpd IPv4%s\n", |
| 675 | #ifdef ENABLE_IPV6 |
| 676 | "/IPv6" |
| 677 | #else |
| 678 | "" |
| 679 | #endif |
| 680 | ); |
| 681 | return 0; |
| 682 | } |
| 683 | else if(!strcmp("--pidfile", argv[arg])) { |
| 684 | arg++; |
| 685 | if(argc>arg) |
| 686 | pidname = argv[arg++]; |
| 687 | } |
| 688 | else if(!strcmp("--logfile", argv[arg])) { |
| 689 | arg++; |
| 690 | if(argc>arg) |
| 691 | serverlogfile = argv[arg++]; |
| 692 | } |
| 693 | else if(!strcmp("--ipv4", argv[arg])) { |
| 694 | #ifdef ENABLE_IPV6 |
| 695 | ipv_inuse = "IPv4"; |
| 696 | use_ipv6 = FALSE; |
| 697 | #endif |
| 698 | arg++; |
| 699 | } |
| 700 | else if(!strcmp("--ipv6", argv[arg])) { |
| 701 | #ifdef ENABLE_IPV6 |
| 702 | ipv_inuse = "IPv6"; |
| 703 | use_ipv6 = TRUE; |
| 704 | #endif |
| 705 | arg++; |
| 706 | } |
| 707 | else if(!strcmp("--port", argv[arg])) { |
| 708 | arg++; |
| 709 | if(argc>arg) { |
| 710 | char *endptr; |
| 711 | unsigned long ulnum = strtoul(argv[arg], &endptr, 10); |
| 712 | if((endptr != argv[arg] + strlen(argv[arg])) || |
| 713 | (ulnum < 1025UL) || (ulnum > 65535UL)) { |
| 714 | fprintf(stderr, "tftpd: invalid --port argument (%s)\n", |
| 715 | argv[arg]); |
| 716 | return 0; |
| 717 | } |
| 718 | port = curlx_ultous(ulnum); |
| 719 | arg++; |
| 720 | } |
| 721 | } |
| 722 | else if(!strcmp("--srcdir", argv[arg])) { |
| 723 | arg++; |
| 724 | if(argc>arg) { |
| 725 | path = argv[arg]; |
| 726 | arg++; |
| 727 | } |
| 728 | } |
| 729 | else { |
| 730 | puts("Usage: tftpd [option]\n" |
| 731 | " --version\n" |
| 732 | " --logfile [file]\n" |
| 733 | " --pidfile [file]\n" |
| 734 | " --ipv4\n" |
| 735 | " --ipv6\n" |
| 736 | " --port [port]\n" |
| 737 | " --srcdir [path]"); |
| 738 | return 0; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | #ifdef WIN32 |
| 743 | win32_init(); |
| 744 | atexit(win32_cleanup); |
| 745 | #endif |
| 746 | |
| 747 | install_signal_handlers(); |
| 748 | |
| 749 | pid = (long)getpid(); |
| 750 | |
| 751 | #ifdef ENABLE_IPV6 |
| 752 | if(!use_ipv6) |
| 753 | #endif |
| 754 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 755 | #ifdef ENABLE_IPV6 |
| 756 | else |
| 757 | sock = socket(AF_INET6, SOCK_DGRAM, 0); |
| 758 | #endif |
| 759 | |
| 760 | if(CURL_SOCKET_BAD == sock) { |
| 761 | error = SOCKERRNO; |
| 762 | logmsg("Error creating socket: (%d) %s", |
| 763 | error, strerror(error)); |
| 764 | result = 1; |
| 765 | goto tftpd_cleanup; |
| 766 | } |
| 767 | |
| 768 | flag = 1; |
| 769 | if (0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, |
| 770 | (void *)&flag, sizeof(flag))) { |
| 771 | error = SOCKERRNO; |
| 772 | logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s", |
| 773 | error, strerror(error)); |
| 774 | result = 1; |
| 775 | goto tftpd_cleanup; |
| 776 | } |
| 777 | |
| 778 | #ifdef ENABLE_IPV6 |
| 779 | if(!use_ipv6) { |
| 780 | #endif |
| 781 | memset(&me, 0, sizeof(me)); |
| 782 | me.sin_family = AF_INET; |
| 783 | me.sin_addr.s_addr = INADDR_ANY; |
| 784 | me.sin_port = htons(port); |
| 785 | rc = bind(sock, (struct sockaddr *) &me, sizeof(me)); |
| 786 | #ifdef ENABLE_IPV6 |
| 787 | } |
| 788 | else { |
| 789 | memset(&me6, 0, sizeof(me6)); |
| 790 | me6.sin6_family = AF_INET6; |
| 791 | me6.sin6_addr = in6addr_any; |
| 792 | me6.sin6_port = htons(port); |
| 793 | rc = bind(sock, (struct sockaddr *) &me6, sizeof(me6)); |
| 794 | } |
| 795 | #endif /* ENABLE_IPV6 */ |
| 796 | if(0 != rc) { |
| 797 | error = SOCKERRNO; |
| 798 | logmsg("Error binding socket on port %hu: (%d) %s", |
| 799 | port, error, strerror(error)); |
| 800 | result = 1; |
| 801 | goto tftpd_cleanup; |
| 802 | } |
| 803 | |
| 804 | wrotepidfile = write_pidfile(pidname); |
| 805 | if(!wrotepidfile) { |
| 806 | result = 1; |
| 807 | goto tftpd_cleanup; |
| 808 | } |
| 809 | |
| 810 | logmsg("Running %s version on port UDP/%d", ipv_inuse, (int)port); |
| 811 | |
| 812 | for (;;) { |
| 813 | fromlen = sizeof(from); |
| 814 | n = (ssize_t)recvfrom(sock, buf, sizeof(buf), 0, |
| 815 | (struct sockaddr *)&from, &fromlen); |
| 816 | if(got_exit_signal) |
| 817 | break; |
| 818 | if (n < 0) { |
| 819 | logmsg("recvfrom"); |
| 820 | result = 3; |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | set_advisor_read_lock(SERVERLOGS_LOCK); |
| 825 | serverlogslocked = 1; |
| 826 | |
| 827 | from.sin_family = AF_INET; |
| 828 | |
| 829 | peer = socket(AF_INET, SOCK_DGRAM, 0); |
| 830 | if(CURL_SOCKET_BAD == peer) { |
| 831 | logmsg("socket"); |
| 832 | result = 2; |
| 833 | break; |
| 834 | } |
| 835 | |
| 836 | if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) { |
| 837 | logmsg("connect: fail"); |
| 838 | result = 1; |
| 839 | break; |
| 840 | } |
| 841 | maxtimeout = 5*TIMEOUT; |
| 842 | |
| 843 | tp = (struct tftphdr *)buf; |
| 844 | tp->th_opcode = ntohs(tp->th_opcode); |
| 845 | if (tp->th_opcode == opcode_RRQ || tp->th_opcode == opcode_WRQ) { |
| 846 | memset(&test, 0, sizeof(test)); |
| 847 | if (do_tftp(&test, tp, n) < 0) |
| 848 | break; |
| 849 | if(test.buffer) |
| 850 | free(test.buffer); |
| 851 | } |
| 852 | sclose(peer); |
| 853 | peer = CURL_SOCKET_BAD; |
| 854 | |
| 855 | if(test.ofile > 0) { |
| 856 | close(test.ofile); |
| 857 | test.ofile = 0; |
| 858 | } |
| 859 | |
| 860 | if(got_exit_signal) |
| 861 | break; |
| 862 | |
| 863 | if(serverlogslocked) { |
| 864 | serverlogslocked = 0; |
| 865 | clear_advisor_read_lock(SERVERLOGS_LOCK); |
| 866 | } |
| 867 | |
| 868 | logmsg("end of one transfer"); |
| 869 | |
| 870 | } |
| 871 | |
| 872 | tftpd_cleanup: |
| 873 | |
| 874 | if(test.ofile > 0) |
| 875 | close(test.ofile); |
| 876 | |
| 877 | if((peer != sock) && (peer != CURL_SOCKET_BAD)) |
| 878 | sclose(peer); |
| 879 | |
| 880 | if(sock != CURL_SOCKET_BAD) |
| 881 | sclose(sock); |
| 882 | |
| 883 | if(got_exit_signal) |
| 884 | logmsg("signalled to die"); |
| 885 | |
| 886 | if(wrotepidfile) |
| 887 | unlink(pidname); |
| 888 | |
| 889 | if(serverlogslocked) { |
| 890 | serverlogslocked = 0; |
| 891 | clear_advisor_read_lock(SERVERLOGS_LOCK); |
| 892 | } |
| 893 | |
| 894 | restore_signal_handlers(); |
| 895 | |
| 896 | if(got_exit_signal) { |
| 897 | logmsg("========> %s tftpd (port: %d pid: %ld) exits with signal (%d)", |
| 898 | ipv_inuse, (int)port, pid, exit_signal); |
| 899 | /* |
| 900 | * To properly set the return status of the process we |
| 901 | * must raise the same signal SIGINT or SIGTERM that we |
| 902 | * caught and let the old handler take care of it. |
| 903 | */ |
| 904 | raise(exit_signal); |
| 905 | } |
| 906 | |
| 907 | logmsg("========> tftpd quits"); |
| 908 | return result; |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | * Handle initial connection protocol. |
| 913 | */ |
| 914 | static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size) |
| 915 | { |
| 916 | char *cp; |
| 917 | int first = 1, ecode; |
| 918 | struct formats *pf; |
| 919 | char *filename, *mode = NULL; |
| 920 | int error; |
| 921 | FILE *server; |
| 922 | |
| 923 | /* Open request dump file. */ |
| 924 | server = fopen(REQUEST_DUMP, "ab"); |
| 925 | if(!server) { |
| 926 | error = ERRNO; |
| 927 | logmsg("fopen() failed with error: %d %s", error, strerror(error)); |
| 928 | logmsg("Error opening file: %s", REQUEST_DUMP); |
| 929 | return -1; |
| 930 | } |
| 931 | |
| 932 | /* store input protocol */ |
| 933 | fprintf(server, "opcode: %x\n", tp->th_opcode); |
| 934 | |
| 935 | cp = (char *)&tp->th_stuff; |
| 936 | filename = cp; |
| 937 | again: |
| 938 | while (cp < buf + size) { |
| 939 | if (*cp == '\0') |
| 940 | break; |
| 941 | cp++; |
| 942 | } |
| 943 | if (*cp) { |
| 944 | nak(EBADOP); |
| 945 | fclose(server); |
| 946 | return 3; |
| 947 | } |
| 948 | if (first) { |
| 949 | mode = ++cp; |
| 950 | first = 0; |
| 951 | goto again; |
| 952 | } |
| 953 | /* store input protocol */ |
| 954 | fprintf(server, "filename: %s\n", filename); |
| 955 | |
| 956 | for (cp = mode; cp && *cp; cp++) |
| 957 | if(ISUPPER(*cp)) |
| 958 | *cp = (char)tolower((int)*cp); |
| 959 | |
| 960 | /* store input protocol */ |
| 961 | fprintf(server, "mode: %s\n", mode); |
| 962 | fclose(server); |
| 963 | |
| 964 | for (pf = formata; pf->f_mode; pf++) |
| 965 | if (strcmp(pf->f_mode, mode) == 0) |
| 966 | break; |
| 967 | if (!pf->f_mode) { |
| 968 | nak(EBADOP); |
| 969 | return 2; |
| 970 | } |
| 971 | ecode = validate_access(test, filename, tp->th_opcode); |
| 972 | if (ecode) { |
| 973 | nak(ecode); |
| 974 | return 1; |
| 975 | } |
| 976 | if (tp->th_opcode == opcode_WRQ) |
| 977 | recvtftp(test, pf); |
| 978 | else |
| 979 | sendtftp(test, pf); |
| 980 | |
| 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | /* |
| 985 | * Validate file access. |
| 986 | */ |
| 987 | static int validate_access(struct testcase *test, |
| 988 | const char *filename, int mode) |
| 989 | { |
| 990 | char *ptr; |
| 991 | long testno, partno; |
| 992 | int error; |
| 993 | char partbuf[80]="data"; |
| 994 | |
| 995 | logmsg("trying to get file: %s mode %x", filename, mode); |
| 996 | |
| 997 | if(!strncmp("verifiedserver", filename, 14)) { |
| 998 | char weare[128]; |
| 999 | size_t count = sprintf(weare, "WE ROOLZ: %ld\r\n", (long)getpid()); |
| 1000 | |
| 1001 | logmsg("Are-we-friendly question received"); |
| 1002 | test->buffer = strdup(weare); |
| 1003 | test->rptr = test->buffer; /* set read pointer */ |
| 1004 | test->bufsize = count; /* set total count */ |
| 1005 | test->rcount = count; /* set data left to read */ |
| 1006 | return 0; /* fine */ |
| 1007 | } |
| 1008 | |
| 1009 | /* find the last slash */ |
| 1010 | ptr = strrchr(filename, '/'); |
| 1011 | |
| 1012 | if(ptr) { |
| 1013 | char *file; |
| 1014 | |
| 1015 | ptr++; /* skip the slash */ |
| 1016 | |
| 1017 | /* skip all non-numericals following the slash */ |
| 1018 | while(*ptr && !ISDIGIT(*ptr)) |
| 1019 | ptr++; |
| 1020 | |
| 1021 | /* get the number */ |
| 1022 | testno = strtol(ptr, &ptr, 10); |
| 1023 | |
| 1024 | if(testno > 10000) { |
| 1025 | partno = testno % 10000; |
| 1026 | testno /= 10000; |
| 1027 | } |
| 1028 | else |
| 1029 | partno = 0; |
| 1030 | |
| 1031 | |
| 1032 | logmsg("requested test number %ld part %ld", testno, partno); |
| 1033 | |
| 1034 | test->num = testno; |
| 1035 | |
| 1036 | file = test2file(testno); |
| 1037 | |
| 1038 | if(0 != partno) |
| 1039 | sprintf(partbuf, "data%ld", partno); |
| 1040 | |
| 1041 | if(file) { |
| 1042 | FILE *stream=fopen(file, "rb"); |
| 1043 | if(!stream) { |
| 1044 | error = ERRNO; |
| 1045 | logmsg("fopen() failed with error: %d %s", error, strerror(error)); |
| 1046 | logmsg("Error opening file: %s", file); |
| 1047 | logmsg("Couldn't open test file: %s", file); |
| 1048 | return EACCESS; |
| 1049 | } |
| 1050 | else { |
| 1051 | size_t count; |
| 1052 | error = getpart(&test->buffer, &count, "reply", partbuf, stream); |
| 1053 | fclose(stream); |
| 1054 | if(error) { |
| 1055 | logmsg("getpart() failed with error: %d", error); |
| 1056 | return EACCESS; |
| 1057 | } |
| 1058 | if(test->buffer) { |
| 1059 | test->rptr = test->buffer; /* set read pointer */ |
| 1060 | test->bufsize = count; /* set total count */ |
| 1061 | test->rcount = count; /* set data left to read */ |
| 1062 | } |
| 1063 | else |
| 1064 | return EACCESS; |
| 1065 | } |
| 1066 | |
| 1067 | } |
| 1068 | else |
| 1069 | return EACCESS; |
| 1070 | } |
| 1071 | else { |
| 1072 | logmsg("no slash found in path"); |
| 1073 | return EACCESS; /* failure */ |
| 1074 | } |
| 1075 | |
| 1076 | logmsg("file opened and all is good"); |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
| 1080 | /* |
| 1081 | * Send the requested file. |
| 1082 | */ |
| 1083 | static void sendtftp(struct testcase *test, struct formats *pf) |
| 1084 | { |
| 1085 | int size; |
| 1086 | ssize_t n; |
| 1087 | sendblock = 1; |
| 1088 | #if defined(HAVE_ALARM) && defined(SIGALRM) |
| 1089 | mysignal(SIGALRM, timer); |
| 1090 | #endif |
| 1091 | sdp = r_init(); |
| 1092 | sap = (struct tftphdr *)ackbuf; |
| 1093 | do { |
| 1094 | size = readit(test, &sdp, pf->f_convert); |
| 1095 | if (size < 0) { |
| 1096 | nak(ERRNO + 100); |
| 1097 | return; |
| 1098 | } |
| 1099 | sdp->th_opcode = htons((u_short)opcode_DATA); |
| 1100 | sdp->th_block = htons((u_short)sendblock); |
| 1101 | timeout = 0; |
| 1102 | #ifdef HAVE_SIGSETJMP |
| 1103 | (void) sigsetjmp(timeoutbuf, 1); |
| 1104 | #endif |
| 1105 | send_data: |
| 1106 | if (swrite(peer, sdp, size + 4) != size + 4) { |
| 1107 | logmsg("write"); |
| 1108 | return; |
| 1109 | } |
| 1110 | read_ahead(test, pf->f_convert); |
| 1111 | for ( ; ; ) { |
| 1112 | #ifdef HAVE_ALARM |
| 1113 | alarm(rexmtval); /* read the ack */ |
| 1114 | #endif |
| 1115 | n = sread(peer, ackbuf, sizeof (ackbuf)); |
| 1116 | #ifdef HAVE_ALARM |
| 1117 | alarm(0); |
| 1118 | #endif |
| 1119 | if(got_exit_signal) |
| 1120 | return; |
| 1121 | if (n < 0) { |
| 1122 | logmsg("read: fail"); |
| 1123 | return; |
| 1124 | } |
| 1125 | sap->th_opcode = ntohs((u_short)sap->th_opcode); |
| 1126 | sap->th_block = ntohs((u_short)sap->th_block); |
| 1127 | |
| 1128 | if (sap->th_opcode == opcode_ERROR) { |
| 1129 | logmsg("got ERROR"); |
| 1130 | return; |
| 1131 | } |
| 1132 | |
| 1133 | if (sap->th_opcode == opcode_ACK) { |
| 1134 | if (sap->th_block == sendblock) { |
| 1135 | break; |
| 1136 | } |
| 1137 | /* Re-synchronize with the other side */ |
| 1138 | (void) synchnet(peer); |
| 1139 | if (sap->th_block == (sendblock-1)) { |
| 1140 | goto send_data; |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | } |
| 1145 | sendblock++; |
| 1146 | } while (size == SEGSIZE); |
| 1147 | } |
| 1148 | |
| 1149 | /* |
| 1150 | * Receive a file. |
| 1151 | */ |
| 1152 | static void recvtftp(struct testcase *test, struct formats *pf) |
| 1153 | { |
| 1154 | ssize_t n, size; |
| 1155 | recvblock = 0; |
| 1156 | #if defined(HAVE_ALARM) && defined(SIGALRM) |
| 1157 | mysignal(SIGALRM, timer); |
| 1158 | #endif |
| 1159 | rdp = w_init(); |
| 1160 | rap = (struct tftphdr *)ackbuf; |
| 1161 | do { |
| 1162 | timeout = 0; |
| 1163 | rap->th_opcode = htons((u_short)opcode_ACK); |
| 1164 | rap->th_block = htons((u_short)recvblock); |
| 1165 | recvblock++; |
| 1166 | #ifdef HAVE_SIGSETJMP |
| 1167 | (void) sigsetjmp(timeoutbuf, 1); |
| 1168 | #endif |
| 1169 | send_ack: |
| 1170 | if (swrite(peer, ackbuf, 4) != 4) { |
| 1171 | logmsg("write: fail\n"); |
| 1172 | goto abort; |
| 1173 | } |
| 1174 | write_behind(test, pf->f_convert); |
| 1175 | for ( ; ; ) { |
| 1176 | #ifdef HAVE_ALARM |
| 1177 | alarm(rexmtval); |
| 1178 | #endif |
| 1179 | n = sread(peer, rdp, PKTSIZE); |
| 1180 | #ifdef HAVE_ALARM |
| 1181 | alarm(0); |
| 1182 | #endif |
| 1183 | if(got_exit_signal) |
| 1184 | goto abort; |
| 1185 | if (n < 0) { /* really? */ |
| 1186 | logmsg("read: fail\n"); |
| 1187 | goto abort; |
| 1188 | } |
| 1189 | rdp->th_opcode = ntohs((u_short)rdp->th_opcode); |
| 1190 | rdp->th_block = ntohs((u_short)rdp->th_block); |
| 1191 | if (rdp->th_opcode == opcode_ERROR) |
| 1192 | goto abort; |
| 1193 | if (rdp->th_opcode == opcode_DATA) { |
| 1194 | if (rdp->th_block == recvblock) { |
| 1195 | break; /* normal */ |
| 1196 | } |
| 1197 | /* Re-synchronize with the other side */ |
| 1198 | (void) synchnet(peer); |
| 1199 | if (rdp->th_block == (recvblock-1)) |
| 1200 | goto send_ack; /* rexmit */ |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | size = writeit(test, &rdp, (int)(n - 4), pf->f_convert); |
| 1205 | if (size != (n-4)) { /* ahem */ |
| 1206 | if (size < 0) |
| 1207 | nak(ERRNO + 100); |
| 1208 | else |
| 1209 | nak(ENOSPACE); |
| 1210 | goto abort; |
| 1211 | } |
| 1212 | } while (size == SEGSIZE); |
| 1213 | write_behind(test, pf->f_convert); |
| 1214 | |
| 1215 | rap->th_opcode = htons((u_short)opcode_ACK); /* send the "final" ack */ |
| 1216 | rap->th_block = htons((u_short)recvblock); |
| 1217 | (void) swrite(peer, ackbuf, 4); |
| 1218 | #if defined(HAVE_ALARM) && defined(SIGALRM) |
| 1219 | mysignal(SIGALRM, justtimeout); /* just abort read on timeout */ |
| 1220 | alarm(rexmtval); |
| 1221 | #endif |
| 1222 | n = sread(peer, buf, sizeof(buf)); /* normally times out and quits */ |
| 1223 | #ifdef HAVE_ALARM |
| 1224 | alarm(0); |
| 1225 | #endif |
| 1226 | if(got_exit_signal) |
| 1227 | goto abort; |
| 1228 | if (n >= 4 && /* if read some data */ |
| 1229 | rdp->th_opcode == opcode_DATA && /* and got a data block */ |
| 1230 | recvblock == rdp->th_block) { /* then my last ack was lost */ |
| 1231 | (void) swrite(peer, ackbuf, 4); /* resend final ack */ |
| 1232 | } |
| 1233 | abort: |
| 1234 | return; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
| 1238 | * Send a nak packet (error message). Error code passed in is one of the |
| 1239 | * standard TFTP codes, or a UNIX errno offset by 100. |
| 1240 | */ |
| 1241 | static void nak(int error) |
| 1242 | { |
| 1243 | struct tftphdr *tp; |
| 1244 | int length; |
| 1245 | struct errmsg *pe; |
| 1246 | |
| 1247 | tp = (struct tftphdr *)buf; |
| 1248 | tp->th_opcode = htons((u_short)opcode_ERROR); |
| 1249 | tp->th_code = htons((u_short)error); |
| 1250 | for (pe = errmsgs; pe->e_code >= 0; pe++) |
| 1251 | if (pe->e_code == error) |
| 1252 | break; |
| 1253 | if (pe->e_code < 0) { |
| 1254 | pe->e_msg = strerror(error - 100); |
| 1255 | tp->th_code = EUNDEF; /* set 'undef' errorcode */ |
| 1256 | } |
| 1257 | strcpy(tp->th_msg, pe->e_msg); |
| 1258 | length = (int)strlen(pe->e_msg); |
| 1259 | tp->th_msg[length] = '\0'; |
| 1260 | length += 5; |
| 1261 | if (swrite(peer, buf, length) != length) |
| 1262 | logmsg("nak: fail\n"); |
| 1263 | } |