tftp.c revision 1.27
1/* $NetBSD: tftp.c,v 1.27 2006/09/29 14:42:59 christos Exp $ */ 2 3/* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33#ifndef lint 34#if 0 35static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93"; 36#else 37__RCSID("$NetBSD: tftp.c,v 1.27 2006/09/29 14:42:59 christos Exp $"); 38#endif 39#endif /* not lint */ 40 41/* Many bug fixes are from Jim Guyton <guyton@rand-unix> */ 42 43/* 44 * TFTP User Program -- Protocol Machines 45 */ 46#include <sys/types.h> 47#include <sys/param.h> 48#include <sys/socket.h> 49#include <sys/stat.h> 50#include <sys/time.h> 51 52#include <netinet/in.h> 53 54#include <arpa/tftp.h> 55#include <arpa/inet.h> 56 57#include <err.h> 58#include <errno.h> 59#include <setjmp.h> 60#include <signal.h> 61#include <stdio.h> 62#include <stdlib.h> 63#include <string.h> 64#include <unistd.h> 65#include <netdb.h> 66 67#include "extern.h" 68#include "tftpsubs.h" 69 70char ackbuf[PKTSIZE]; 71int timeout; 72jmp_buf toplevel; 73jmp_buf timeoutbuf; 74 75static void nak __P((int, struct sockaddr *)); 76static int makerequest __P((int, const char *, struct tftphdr *, const char *, off_t)); 77static void printstats __P((const char *, unsigned long)); 78static void startclock __P((void)); 79static void stopclock __P((void)); 80static void timer __P((int)); 81static void tpacket __P((const char *, struct tftphdr *, int)); 82static int cmpport __P((struct sockaddr *, struct sockaddr *)); 83 84static void get_options(struct tftphdr *, int); 85static int tftp_igmp_join(void); 86static void tftp_igmp_leave(int); 87 88static void 89get_options(struct tftphdr *ap, int size) 90{ 91 unsigned long val; 92 char *opt, *endp, *nextopt, *valp; 93 int l; 94 95 size -= 2; /* skip over opcode */ 96 opt = ap->th_stuff; 97 endp = opt + size - 1; 98 *endp = '\0'; 99 100 while (opt < endp) { 101 int ismulticast; 102 l = strlen(opt) + 1; 103 valp = opt + l; 104 ismulticast = !strcasecmp(opt, "multicast"); 105 if (valp < endp) { 106 val = strtoul(valp, NULL, 10); 107 l = strlen(valp) + 1; 108 nextopt = valp + l; 109 if (!ismulticast) { 110 if (val == ULONG_MAX && errno == ERANGE) { 111 /* Report illegal value */ 112 opt = nextopt; 113 continue; 114 } 115 } 116 } else { 117 /* Badly formed OACK */ 118 break; 119 } 120 if (strcmp(opt, "tsize") == 0) { 121 /* cool, but we'll ignore it */ 122 } else if (strcmp(opt, "timeout") == 0) { 123 if (val >= 1 && val <= 255) { 124 rexmtval = val; 125 } else { 126 /* Report error? */ 127 } 128 } else if (strcmp(opt, "blksize") == 0) { 129 if (val >= 8 && val <= MAXSEGSIZE) { 130 blksize = val; 131 } else { 132 /* Report error? */ 133 } 134 } else if (ismulticast) { 135 char multicast[24]; 136 char *pmulticast; 137 char *addr; 138 139 strlcpy(multicast, valp, sizeof(multicast)); 140 pmulticast = multicast; 141 addr = strsep(&pmulticast, ","); 142 if (multicast == NULL) 143 continue; /* Report error? */ 144 mcport = atoi(strsep(&pmulticast, ",")); 145 if (multicast == NULL) 146 continue; /* Report error? */ 147 mcmasterslave = atoi(multicast); 148 mcaddr = inet_addr(addr); 149 if (mcaddr == INADDR_NONE) 150 continue; /* Report error? */ 151 } else { 152 /* unknown option */ 153 } 154 opt = nextopt; 155 } 156} 157 158static int 159tftp_igmp_join(void) 160{ 161 struct ip_mreq req; 162 struct sockaddr_in s; 163 int fd, rv; 164 165 memset(&req, 0, sizeof(struct ip_mreq)); 166 req.imr_multiaddr.s_addr = mcaddr; 167 req.imr_interface.s_addr = INADDR_ANY; 168 169 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); 170 if (fd < 0) { 171 perror("socket"); 172 return fd; 173 } 174 175 memset(&s, 0, sizeof(struct sockaddr_in)); 176 s.sin_family = AF_INET; 177 s.sin_port = htons(mcport); 178 s.sin_len = sizeof(struct sockaddr_in); 179 rv = bind(fd, (struct sockaddr *)&s, sizeof(struct sockaddr_in)); 180 if (rv < 0) { 181 perror("bind"); 182 close(fd); 183 return rv; 184 } 185 186 rv = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &req, 187 sizeof(struct ip_mreq)); 188 if (rv < 0) { 189 perror("setsockopt"); 190 close(fd); 191 return rv; 192 } 193 194 return fd; 195} 196 197static void 198tftp_igmp_leave(int fd) 199{ 200 struct ip_mreq req; 201 int rv; 202 203 memset(&req, 0, sizeof(struct ip_mreq)); 204 req.imr_multiaddr.s_addr = mcaddr; 205 req.imr_interface.s_addr = INADDR_ANY; 206 207 rv = setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &req, 208 sizeof(struct ip_mreq)); 209 if (rv < 0) 210 perror("setsockopt"); 211 212 close(fd); 213 214 return; 215} 216 217/* 218 * Send the requested file. 219 */ 220void 221sendfile(fd, name, mode) 222 int fd; 223 char *name; 224 char *mode; 225{ 226 struct tftphdr *ap; /* data and ack packets */ 227 struct tftphdr *dp; 228 int j, n; 229 volatile unsigned int block; 230 volatile int size, convert; 231 volatile unsigned long amount; 232 struct sockaddr_storage from; 233 struct stat sbuf; 234 off_t filesize=0; 235 socklen_t fromlen; 236 FILE *file; 237 struct sockaddr_storage peer; 238 struct sockaddr_storage serv; /* valid server port number */ 239 240 startclock(); /* start stat's clock */ 241 dp = r_init(); /* reset fillbuf/read-ahead code */ 242 ap = (struct tftphdr *)(void *)ackbuf; 243 if (tsize) { 244 if (fstat(fd, &sbuf) == 0) { 245 filesize = sbuf.st_size; 246 } else { 247 filesize = -1ULL; 248 } 249 } 250 file = fdopen(fd, "r"); 251 convert = !strcmp(mode, "netascii"); 252 block = 0; 253 amount = 0; 254 (void)memcpy(&peer, &peeraddr, (size_t)peeraddr.ss_len); 255 (void)memset(&serv, 0, sizeof(serv)); 256 257 (void)signal(SIGALRM, timer); 258 do { 259 if (block == 0) 260 size = makerequest(WRQ, name, dp, mode, filesize) - 4; 261 else { 262 /* size = read(fd, dp->th_data, SEGSIZE); */ 263 size = readit(file, &dp, blksize, convert); 264 if (size < 0) { 265 nak(errno + 100, (struct sockaddr *)(void *)&peer); 266 break; 267 } 268 dp->th_opcode = htons((u_short)DATA); 269 dp->th_block = htons((u_short)block); 270 } 271 timeout = 0; 272 (void) setjmp(timeoutbuf); 273send_data: 274 if (trace) 275 tpacket("sent", dp, size + 4); 276 n = sendto(f, dp, (socklen_t)(size + 4), 0, 277 (struct sockaddr *)(void *)&peer, (socklen_t)peer.ss_len); 278 if (n != size + 4) { 279 warn("sendto"); 280 goto abort; 281 } 282 if (block) 283 read_ahead(file, blksize, convert); 284 for ( ; ; ) { 285 (void)alarm(rexmtval); 286 do { 287 int curf; 288 fromlen = sizeof(from); 289 if (mcaddr != INADDR_NONE) 290 curf = mf; 291 else 292 curf = f; 293 n = recvfrom(curf, ackbuf, sizeof(ackbuf), 0, 294 (struct sockaddr *)(void *)&from, &fromlen); 295 } while (n <= 0); 296 (void)alarm(0); 297 if (n < 0) { 298 warn("recvfrom"); 299 goto abort; 300 } 301 if (!serv.ss_family) 302 serv = from; 303 else if (!cmpport((struct sockaddr *)(void *)&serv, 304 (struct sockaddr *)(void *)&from)) { 305 warn("server port mismatch"); 306 goto abort; 307 } 308 peer = from; 309 if (trace) 310 tpacket("received", ap, n); 311 /* should verify packet came from server */ 312 ap->th_opcode = ntohs(ap->th_opcode); 313 if (ap->th_opcode == ERROR) { 314 (void)printf("Error code %d: %s\n", ap->th_code, 315 ap->th_msg); 316 goto abort; 317 } 318 if (ap->th_opcode == ACK) { 319 ap->th_block = ntohs(ap->th_block); 320 321 if (ap->th_block == 0) { 322 /* 323 * If the extended options are enabled, 324 * the server just refused 'em all. 325 * The only one that _really_ 326 * matters is blksize, but we'll 327 * clear timeout and mcaddr, too. 328 */ 329 blksize = def_blksize; 330 rexmtval = def_rexmtval; 331 mcaddr = INADDR_NONE; 332 } 333 if (ap->th_block == block) { 334 break; 335 } 336 /* On an error, try to synchronize 337 * both sides. 338 */ 339 j = synchnet(f, blksize+4); 340 if (j && trace) { 341 (void)printf("discarded %d packets\n", 342 j); 343 } 344 if (ap->th_block == (block-1)) { 345 goto send_data; 346 } 347 } 348 if (ap->th_opcode == OACK) { 349 if (block == 0) { 350 blksize = def_blksize; 351 rexmtval = def_rexmtval; 352 mcaddr = INADDR_NONE; 353 get_options(ap, n); 354 break; 355 } 356 } 357 } 358 if (block > 0) 359 amount += size; 360 block++; 361 } while (size == blksize || block == 1); 362abort: 363 (void)fclose(file); 364 stopclock(); 365 if (amount > 0) 366 printstats("Sent", amount); 367} 368 369/* 370 * Receive a file. 371 */ 372void 373recvfile(fd, name, mode) 374 int fd; 375 char *name; 376 char *mode; 377{ 378 struct tftphdr *ap; 379 struct tftphdr *dp; 380 int j, n, oack=0; 381 volatile unsigned int block; 382 volatile int size, firsttrip; 383 volatile unsigned long amount; 384 struct sockaddr_storage from; 385 socklen_t fromlen; 386 size_t readlen; 387 FILE *file; 388 volatile int convert; /* true if converting crlf -> lf */ 389 struct sockaddr_storage peer; 390 struct sockaddr_storage serv; /* valid server port number */ 391 392 startclock(); 393 dp = w_init(); 394 ap = (struct tftphdr *)(void *)ackbuf; 395 file = fdopen(fd, "w"); 396 convert = !strcmp(mode, "netascii"); 397 block = 1; 398 firsttrip = 1; 399 amount = 0; 400 (void)memcpy(&peer, &peeraddr, (size_t)peeraddr.ss_len); 401 (void)memset(&serv, 0, sizeof(serv)); 402 403 (void)signal(SIGALRM, timer); 404 do { 405 if (firsttrip) { 406 size = makerequest(RRQ, name, ap, mode, (off_t)0); 407 readlen = PKTSIZE; 408 firsttrip = 0; 409 } else { 410 ap->th_opcode = htons((u_short)ACK); 411 ap->th_block = htons((u_short)(block)); 412 readlen = blksize+4; 413 size = 4; 414 block++; 415 } 416 timeout = 0; 417 (void) setjmp(timeoutbuf); 418send_ack: 419 if (trace) 420 tpacket("sent", ap, size); 421 if (sendto(f, ackbuf, (socklen_t)size, 0, 422 (struct sockaddr *)(void *)&peer, 423 (socklen_t)peer.ss_len) != size) { 424 (void)alarm(0); 425 warn("sendto"); 426 goto abort; 427 } 428skip_ack: 429 if (write_behind(file, convert) == -1) 430 goto abort; 431 for ( ; ; ) { 432 (void)alarm(rexmtval); 433 do { 434 int readfd; 435 if (mf > 0) 436 readfd = mf; 437 else 438 readfd = f; 439 fromlen = sizeof(from); 440 n = recvfrom(readfd, dp, readlen, 0, 441 (struct sockaddr *)(void *)&from, &fromlen); 442 } while (n <= 0); 443 (void)alarm(0); 444 if (n < 0) { 445 warn("recvfrom"); 446 goto abort; 447 } 448 if (!serv.ss_family) 449 serv = from; 450 else if (!cmpport((struct sockaddr *)(void *)&serv, 451 (struct sockaddr *)(void *)&from)) { 452 warn("server port mismatch"); 453 goto abort; 454 } 455 peer = from; 456 if (trace) 457 tpacket("received", dp, n); 458 /* should verify client address */ 459 dp->th_opcode = ntohs(dp->th_opcode); 460 if (dp->th_opcode == ERROR) { 461 (void)printf("Error code %d: %s\n", dp->th_code, 462 dp->th_msg); 463 goto abort; 464 } 465 if (dp->th_opcode == DATA) { 466 dp->th_block = ntohs(dp->th_block); 467 468 if (dp->th_block == 1 && !oack) { 469 /* no OACK, revert to defaults */ 470 blksize = def_blksize; 471 rexmtval = def_rexmtval; 472 } 473 if (dp->th_block == block) { 474 break; /* have next packet */ 475 } 476 /* On an error, try to synchronize 477 * both sides. 478 */ 479 j = synchnet(f, blksize); 480 if (j && trace) { 481 (void)printf("discarded %d packets\n", j); 482 } 483 if (dp->th_block == (block-1)) { 484 goto send_ack; /* resend ack */ 485 } 486 } 487 if (dp->th_opcode == OACK) { 488 if (block == 1) { 489 oack = 1; 490 blksize = def_blksize; 491 rexmtval = def_rexmtval; 492 get_options(dp, n); 493 ap->th_opcode = htons(ACK); 494 ap->th_block = 0; 495 readlen = blksize+4; 496 size = 4; 497 if (mcaddr != INADDR_NONE) { 498 mf = tftp_igmp_join(); 499 if (mf < 0) 500 goto abort; 501 if (mcmasterslave == 0) 502 goto skip_ack; 503 } 504 goto send_ack; 505 } 506 } 507 } 508 /* size = write(fd, dp->th_data, n - 4); */ 509 size = writeit(file, &dp, n - 4, convert); 510 if (size < 0) { 511 nak(errno + 100, (struct sockaddr *)(void *)&peer); 512 break; 513 } 514 amount += size; 515 } while (size == blksize); 516abort: /* ok to ack, since user */ 517 ap->th_opcode = htons((u_short)ACK); /* has seen err msg */ 518 ap->th_block = htons((u_short)block); 519 if (mcaddr != INADDR_NONE && mf >= 0) { 520 tftp_igmp_leave(mf); 521 mf = -1; 522 } 523 (void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)(void *)&peer, 524 (socklen_t)peer.ss_len); 525 /* 526 * flush last buffer 527 * We do not check for failure because last buffer 528 * can be empty, thus returning an error. 529 * XXX maybe we should fix 'write_behind' instead. 530 */ 531 (void)write_behind(file, convert); 532 (void)fclose(file); 533 stopclock(); 534 if (amount > 0) 535 printstats("Received", amount); 536} 537 538static int 539makerequest(request, name, tp, mode, filesize) 540 int request; 541 const char *name; 542 struct tftphdr *tp; 543 const char *mode; 544 off_t filesize; 545{ 546 char *cp; 547 548 tp->th_opcode = htons((u_short)request); 549#ifndef __SVR4 550 cp = tp->th_stuff; 551#else 552 cp = (void *)&tp->th_stuff; 553#endif 554 (void)strcpy(cp, name); 555 cp += strlen(name); 556 *cp++ = '\0'; 557 (void)strcpy(cp, mode); 558 cp += strlen(mode); 559 *cp++ = '\0'; 560 if (tsize) { 561 (void)strcpy(cp, "tsize"); 562 cp += strlen(cp); 563 *cp++ = '\0'; 564 (void)sprintf(cp, "%lu", (unsigned long) filesize); 565 cp += strlen(cp); 566 *cp++ = '\0'; 567 } 568 if (tout) { 569 (void)strcpy(cp, "timeout"); 570 cp += strlen(cp); 571 *cp++ = '\0'; 572 (void)sprintf(cp, "%d", rexmtval); 573 cp += strlen(cp); 574 *cp++ = '\0'; 575 } 576 if (blksize != SEGSIZE) { 577 (void)strcpy(cp, "blksize"); 578 cp += strlen(cp); 579 *cp++ = '\0'; 580 (void)sprintf(cp, "%zd", blksize); 581 cp += strlen(cp); 582 *cp++ = '\0'; 583 } 584 return (cp - (char *)(void *)tp); 585} 586 587const struct errmsg { 588 int e_code; 589 const char *e_msg; 590} errmsgs[] = { 591 { EUNDEF, "Undefined error code" }, 592 { ENOTFOUND, "File not found" }, 593 { EACCESS, "Access violation" }, 594 { ENOSPACE, "Disk full or allocation exceeded" }, 595 { EBADOP, "Illegal TFTP operation" }, 596 { EBADID, "Unknown transfer ID" }, 597 { EEXISTS, "File already exists" }, 598 { ENOUSER, "No such user" }, 599 { EOPTNEG, "Option negotiation failed" }, 600 { -1, 0 } 601}; 602 603/* 604 * Send a nak packet (error message). 605 * Error code passed in is one of the 606 * standard TFTP codes, or a UNIX errno 607 * offset by 100. 608 */ 609static void 610nak(error, peer) 611 int error; 612 struct sockaddr *peer; 613{ 614 const struct errmsg *pe; 615 struct tftphdr *tp; 616 int length; 617 size_t msglen; 618 619 tp = (struct tftphdr *)(void *)ackbuf; 620 tp->th_opcode = htons((u_short)ERROR); 621 msglen = sizeof(ackbuf) - (&tp->th_msg[0] - ackbuf); 622 for (pe = errmsgs; pe->e_code >= 0; pe++) 623 if (pe->e_code == error) 624 break; 625 if (pe->e_code < 0) { 626 tp->th_code = EUNDEF; 627 (void)strlcpy(tp->th_msg, strerror(error - 100), msglen); 628 } else { 629 tp->th_code = htons((u_short)error); 630 (void)strlcpy(tp->th_msg, pe->e_msg, msglen); 631 } 632 length = strlen(tp->th_msg); 633 msglen = &tp->th_msg[length + 1] - ackbuf; 634 if (trace) 635 tpacket("sent", tp, (int)msglen); 636 if (sendto(f, ackbuf, msglen, 0, peer, (socklen_t)peer->sa_len) != msglen) 637 warn("nak"); 638} 639 640static void 641tpacket(s, tp, n) 642 const char *s; 643 struct tftphdr *tp; 644 int n; 645{ 646 static const char *opcodes[] = 647 { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" }; 648 char *cp, *file, *endp, *opt = NULL; 649 const char *spc; 650 u_short op = ntohs(tp->th_opcode); 651 int i, o; 652 653 if (op < RRQ || op > OACK) 654 (void)printf("%s opcode=%x ", s, op); 655 else 656 (void)printf("%s %s ", s, opcodes[op]); 657 switch (op) { 658 659 case RRQ: 660 case WRQ: 661 n -= 2; 662#ifndef __SVR4 663 cp = tp->th_stuff; 664#else 665 cp = (void *) &tp->th_stuff; 666#endif 667 endp = cp + n - 1; 668 if (*endp != '\0') { /* Shouldn't happen, but... */ 669 *endp = '\0'; 670 } 671 file = cp; 672 cp = strchr(cp, '\0') + 1; 673 (void)printf("<file=%s, mode=%s", file, cp); 674 cp = strchr(cp, '\0') + 1; 675 o = 0; 676 while (cp < endp) { 677 i = strlen(cp) + 1; 678 if (o) { 679 (void)printf(", %s=%s", opt, cp); 680 } else { 681 opt = cp; 682 } 683 o = (o+1) % 2; 684 cp += i; 685 } 686 (void)printf(">\n"); 687 break; 688 689 case DATA: 690 (void)printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4); 691 break; 692 693 case ACK: 694 (void)printf("<block=%d>\n", ntohs(tp->th_block)); 695 break; 696 697 case ERROR: 698 (void)printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg); 699 break; 700 701 case OACK: 702 o = 0; 703 n -= 2; 704 cp = tp->th_stuff; 705 endp = cp + n - 1; 706 if (*endp != '\0') { /* Shouldn't happen, but... */ 707 *endp = '\0'; 708 } 709 (void)printf("<"); 710 spc = ""; 711 while (cp < endp) { 712 i = strlen(cp) + 1; 713 if (o) { 714 (void)printf("%s%s=%s", spc, opt, cp); 715 spc = ", "; 716 } else { 717 opt = cp; 718 } 719 o = (o+1) % 2; 720 cp += i; 721 } 722 (void)printf(">\n"); 723 break; 724 } 725} 726 727struct timeval tstart; 728struct timeval tstop; 729 730static void 731startclock() 732{ 733 734 (void)gettimeofday(&tstart, NULL); 735} 736 737static void 738stopclock() 739{ 740 741 (void)gettimeofday(&tstop, NULL); 742} 743 744static void 745printstats(direction, amount) 746 const char *direction; 747 unsigned long amount; 748{ 749 double delta; 750 751 /* compute delta in 1/10's second units */ 752 delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) - 753 ((tstart.tv_sec*10.)+(tstart.tv_usec/100000)); 754 delta = delta/10.; /* back to seconds */ 755 (void)printf("%s %ld bytes in %.1f seconds", direction, amount, delta); 756 if (verbose) 757 (void)printf(" [%.0f bits/sec]", (amount*8.)/delta); 758 (void)putchar('\n'); 759} 760 761static void 762/*ARGSUSED*/ 763timer(sig) 764 int sig; 765{ 766 767 timeout += rexmtval; 768 if (timeout >= maxtimeout) { 769 (void)printf("Transfer timed out.\n"); 770 longjmp(toplevel, -1); 771 } 772 longjmp(timeoutbuf, 1); 773} 774 775static int 776cmpport(sa, sb) 777 struct sockaddr *sa; 778 struct sockaddr *sb; 779{ 780 char a[NI_MAXSERV], b[NI_MAXSERV]; 781 782 if (getnameinfo(sa, (socklen_t)sa->sa_len, NULL, 0, a, sizeof(a), NI_NUMERICSERV)) 783 return 0; 784 if (getnameinfo(sb, (socklen_t)sb->sa_len, NULL, 0, b, sizeof(b), NI_NUMERICSERV)) 785 return 0; 786 if (strcmp(a, b) != 0) 787 return 0; 788 789 return 1; 790} 791