tftp.c revision 1.26 1 /* $NetBSD: tftp.c,v 1.26 2006/07/21 17:49:00 jmcneill 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
35 static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93";
36 #else
37 __RCSID("$NetBSD: tftp.c,v 1.26 2006/07/21 17:49:00 jmcneill 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
70 char ackbuf[PKTSIZE];
71 int timeout;
72 jmp_buf toplevel;
73 jmp_buf timeoutbuf;
74
75 static void nak __P((int, struct sockaddr *));
76 static int makerequest __P((int, const char *, struct tftphdr *, const char *, off_t));
77 static void printstats __P((const char *, unsigned long));
78 static void startclock __P((void));
79 static void stopclock __P((void));
80 static void timer __P((int));
81 static void tpacket __P((const char *, struct tftphdr *, int));
82 static int cmpport __P((struct sockaddr *, struct sockaddr *));
83
84 static void get_options(struct tftphdr *, int);
85 static int tftp_igmp_join(void);
86 static void tftp_igmp_leave(int);
87
88 static void
89 get_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
158 static int
159 tftp_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
197 static void
198 tftp_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 */
220 void
221 sendfile(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);
273 send_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);
362 abort:
363 (void)fclose(file);
364 stopclock();
365 if (amount > 0)
366 printstats("Sent", amount);
367 }
368
369 /*
370 * Receive a file.
371 */
372 void
373 recvfile(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);
418 send_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 }
428 skip_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 (mcmasterslave == 0)
500 goto skip_ack;
501 }
502 goto send_ack;
503 }
504 }
505 }
506 /* size = write(fd, dp->th_data, n - 4); */
507 size = writeit(file, &dp, n - 4, convert);
508 if (size < 0) {
509 nak(errno + 100, (struct sockaddr *)(void *)&peer);
510 break;
511 }
512 amount += size;
513 } while (size == blksize);
514 abort: /* ok to ack, since user */
515 ap->th_opcode = htons((u_short)ACK); /* has seen err msg */
516 ap->th_block = htons((u_short)block);
517 if (mcaddr != INADDR_NONE) {
518 tftp_igmp_leave(mf);
519 mf = -1;
520 }
521 (void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)(void *)&peer,
522 (socklen_t)peer.ss_len);
523 /*
524 * flush last buffer
525 * We do not check for failure because last buffer
526 * can be empty, thus returning an error.
527 * XXX maybe we should fix 'write_behind' instead.
528 */
529 (void)write_behind(file, convert);
530 (void)fclose(file);
531 stopclock();
532 if (amount > 0)
533 printstats("Received", amount);
534 }
535
536 static int
537 makerequest(request, name, tp, mode, filesize)
538 int request;
539 const char *name;
540 struct tftphdr *tp;
541 const char *mode;
542 off_t filesize;
543 {
544 char *cp;
545
546 tp->th_opcode = htons((u_short)request);
547 #ifndef __SVR4
548 cp = tp->th_stuff;
549 #else
550 cp = (void *)&tp->th_stuff;
551 #endif
552 (void)strcpy(cp, name);
553 cp += strlen(name);
554 *cp++ = '\0';
555 (void)strcpy(cp, mode);
556 cp += strlen(mode);
557 *cp++ = '\0';
558 if (tsize) {
559 (void)strcpy(cp, "tsize");
560 cp += strlen(cp);
561 *cp++ = '\0';
562 (void)sprintf(cp, "%lu", (unsigned long) filesize);
563 cp += strlen(cp);
564 *cp++ = '\0';
565 }
566 if (tout) {
567 (void)strcpy(cp, "timeout");
568 cp += strlen(cp);
569 *cp++ = '\0';
570 (void)sprintf(cp, "%d", rexmtval);
571 cp += strlen(cp);
572 *cp++ = '\0';
573 }
574 if (blksize != SEGSIZE) {
575 (void)strcpy(cp, "blksize");
576 cp += strlen(cp);
577 *cp++ = '\0';
578 (void)sprintf(cp, "%zd", blksize);
579 cp += strlen(cp);
580 *cp++ = '\0';
581 }
582 return (cp - (char *)(void *)tp);
583 }
584
585 const struct errmsg {
586 int e_code;
587 const char *e_msg;
588 } errmsgs[] = {
589 { EUNDEF, "Undefined error code" },
590 { ENOTFOUND, "File not found" },
591 { EACCESS, "Access violation" },
592 { ENOSPACE, "Disk full or allocation exceeded" },
593 { EBADOP, "Illegal TFTP operation" },
594 { EBADID, "Unknown transfer ID" },
595 { EEXISTS, "File already exists" },
596 { ENOUSER, "No such user" },
597 { EOPTNEG, "Option negotiation failed" },
598 { -1, 0 }
599 };
600
601 /*
602 * Send a nak packet (error message).
603 * Error code passed in is one of the
604 * standard TFTP codes, or a UNIX errno
605 * offset by 100.
606 */
607 static void
608 nak(error, peer)
609 int error;
610 struct sockaddr *peer;
611 {
612 const struct errmsg *pe;
613 struct tftphdr *tp;
614 int length;
615 size_t msglen;
616
617 tp = (struct tftphdr *)(void *)ackbuf;
618 tp->th_opcode = htons((u_short)ERROR);
619 msglen = sizeof(ackbuf) - (&tp->th_msg[0] - ackbuf);
620 for (pe = errmsgs; pe->e_code >= 0; pe++)
621 if (pe->e_code == error)
622 break;
623 if (pe->e_code < 0) {
624 tp->th_code = EUNDEF;
625 (void)strlcpy(tp->th_msg, strerror(error - 100), msglen);
626 } else {
627 tp->th_code = htons((u_short)error);
628 (void)strlcpy(tp->th_msg, pe->e_msg, msglen);
629 }
630 length = strlen(tp->th_msg);
631 msglen = &tp->th_msg[length + 1] - ackbuf;
632 if (trace)
633 tpacket("sent", tp, (int)msglen);
634 if (sendto(f, ackbuf, msglen, 0, peer, (socklen_t)peer->sa_len) != msglen)
635 warn("nak");
636 }
637
638 static void
639 tpacket(s, tp, n)
640 const char *s;
641 struct tftphdr *tp;
642 int n;
643 {
644 static const char *opcodes[] =
645 { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" };
646 char *cp, *file, *endp, *opt = NULL;
647 const char *spc;
648 u_short op = ntohs(tp->th_opcode);
649 int i, o;
650
651 if (op < RRQ || op > OACK)
652 (void)printf("%s opcode=%x ", s, op);
653 else
654 (void)printf("%s %s ", s, opcodes[op]);
655 switch (op) {
656
657 case RRQ:
658 case WRQ:
659 n -= 2;
660 #ifndef __SVR4
661 cp = tp->th_stuff;
662 #else
663 cp = (void *) &tp->th_stuff;
664 #endif
665 endp = cp + n - 1;
666 if (*endp != '\0') { /* Shouldn't happen, but... */
667 *endp = '\0';
668 }
669 file = cp;
670 cp = strchr(cp, '\0') + 1;
671 (void)printf("<file=%s, mode=%s", file, cp);
672 cp = strchr(cp, '\0') + 1;
673 o = 0;
674 while (cp < endp) {
675 i = strlen(cp) + 1;
676 if (o) {
677 (void)printf(", %s=%s", opt, cp);
678 } else {
679 opt = cp;
680 }
681 o = (o+1) % 2;
682 cp += i;
683 }
684 (void)printf(">\n");
685 break;
686
687 case DATA:
688 (void)printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
689 break;
690
691 case ACK:
692 (void)printf("<block=%d>\n", ntohs(tp->th_block));
693 break;
694
695 case ERROR:
696 (void)printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
697 break;
698
699 case OACK:
700 o = 0;
701 n -= 2;
702 cp = tp->th_stuff;
703 endp = cp + n - 1;
704 if (*endp != '\0') { /* Shouldn't happen, but... */
705 *endp = '\0';
706 }
707 (void)printf("<");
708 spc = "";
709 while (cp < endp) {
710 i = strlen(cp) + 1;
711 if (o) {
712 (void)printf("%s%s=%s", spc, opt, cp);
713 spc = ", ";
714 } else {
715 opt = cp;
716 }
717 o = (o+1) % 2;
718 cp += i;
719 }
720 (void)printf(">\n");
721 break;
722 }
723 }
724
725 struct timeval tstart;
726 struct timeval tstop;
727
728 static void
729 startclock()
730 {
731
732 (void)gettimeofday(&tstart, NULL);
733 }
734
735 static void
736 stopclock()
737 {
738
739 (void)gettimeofday(&tstop, NULL);
740 }
741
742 static void
743 printstats(direction, amount)
744 const char *direction;
745 unsigned long amount;
746 {
747 double delta;
748
749 /* compute delta in 1/10's second units */
750 delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
751 ((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
752 delta = delta/10.; /* back to seconds */
753 (void)printf("%s %ld bytes in %.1f seconds", direction, amount, delta);
754 if (verbose)
755 (void)printf(" [%.0f bits/sec]", (amount*8.)/delta);
756 (void)putchar('\n');
757 }
758
759 static void
760 /*ARGSUSED*/
761 timer(sig)
762 int sig;
763 {
764
765 timeout += rexmtval;
766 if (timeout >= maxtimeout) {
767 (void)printf("Transfer timed out.\n");
768 longjmp(toplevel, -1);
769 }
770 longjmp(timeoutbuf, 1);
771 }
772
773 static int
774 cmpport(sa, sb)
775 struct sockaddr *sa;
776 struct sockaddr *sb;
777 {
778 char a[NI_MAXSERV], b[NI_MAXSERV];
779
780 if (getnameinfo(sa, (socklen_t)sa->sa_len, NULL, 0, a, sizeof(a), NI_NUMERICSERV))
781 return 0;
782 if (getnameinfo(sb, (socklen_t)sb->sa_len, NULL, 0, b, sizeof(b), NI_NUMERICSERV))
783 return 0;
784 if (strcmp(a, b) != 0)
785 return 0;
786
787 return 1;
788 }
789