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