Home | History | Annotate | Line # | Download | only in ftp
ftp.c revision 1.126.2.9
      1  1.126.2.9      tron /*	$NetBSD: ftp.c,v 1.126.2.9 2005/07/24 10:33:09 tron Exp $	*/
      2       1.77     lukem 
      3       1.77     lukem /*-
      4  1.126.2.1      tron  * Copyright (c) 1996-2005 The NetBSD Foundation, Inc.
      5       1.77     lukem  * All rights reserved.
      6       1.77     lukem  *
      7       1.77     lukem  * This code is derived from software contributed to The NetBSD Foundation
      8       1.77     lukem  * by Luke Mewburn.
      9       1.77     lukem  *
     10       1.77     lukem  * Redistribution and use in source and binary forms, with or without
     11       1.77     lukem  * modification, are permitted provided that the following conditions
     12       1.77     lukem  * are met:
     13       1.77     lukem  * 1. Redistributions of source code must retain the above copyright
     14       1.77     lukem  *    notice, this list of conditions and the following disclaimer.
     15       1.77     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.77     lukem  *    notice, this list of conditions and the following disclaimer in the
     17       1.77     lukem  *    documentation and/or other materials provided with the distribution.
     18       1.77     lukem  * 3. All advertising materials mentioning features or use of this software
     19       1.77     lukem  *    must display the following acknowledgement:
     20       1.77     lukem  *	This product includes software developed by the NetBSD
     21       1.77     lukem  *	Foundation, Inc. and its contributors.
     22       1.77     lukem  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.77     lukem  *    contributors may be used to endorse or promote products derived
     24       1.77     lukem  *    from this software without specific prior written permission.
     25       1.77     lukem  *
     26       1.77     lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.77     lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.77     lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.77     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.77     lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.77     lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.77     lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.77     lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.77     lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.77     lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.77     lukem  * POSSIBILITY OF SUCH DAMAGE.
     37       1.77     lukem  */
     38       1.48    itojun 
     39       1.48    itojun /*
     40       1.72     lukem  * Copyright (c) 1985, 1989, 1993, 1994
     41       1.72     lukem  *	The Regents of the University of California.  All rights reserved.
     42       1.72     lukem  *
     43       1.48    itojun  * Redistribution and use in source and binary forms, with or without
     44       1.48    itojun  * modification, are permitted provided that the following conditions
     45       1.48    itojun  * are met:
     46       1.48    itojun  * 1. Redistributions of source code must retain the above copyright
     47       1.48    itojun  *    notice, this list of conditions and the following disclaimer.
     48       1.48    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     49       1.48    itojun  *    notice, this list of conditions and the following disclaimer in the
     50       1.48    itojun  *    documentation and/or other materials provided with the distribution.
     51      1.122       agc  * 3. Neither the name of the University nor the names of its contributors
     52       1.48    itojun  *    may be used to endorse or promote products derived from this software
     53       1.48    itojun  *    without specific prior written permission.
     54       1.72     lukem  *
     55       1.72     lukem  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56       1.48    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57       1.48    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58       1.72     lukem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59       1.48    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60       1.48    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61       1.48    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62       1.48    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63       1.48    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64       1.48    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65       1.48    itojun  * SUCH DAMAGE.
     66       1.48    itojun  */
     67       1.12       tls 
     68        1.1       cgd /*
     69       1.72     lukem  * Copyright (C) 1997 and 1998 WIDE Project.
     70       1.72     lukem  * All rights reserved.
     71  1.126.2.2      tron  *
     72        1.1       cgd  * Redistribution and use in source and binary forms, with or without
     73        1.1       cgd  * modification, are permitted provided that the following conditions
     74        1.1       cgd  * are met:
     75        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     76        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     77        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     78        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     79        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     80       1.72     lukem  * 3. Neither the name of the project nor the names of its contributors
     81        1.1       cgd  *    may be used to endorse or promote products derived from this software
     82        1.1       cgd  *    without specific prior written permission.
     83  1.126.2.2      tron  *
     84       1.72     lukem  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     85        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     86        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     87       1.72     lukem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     88        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     89        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     90        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     91        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     92        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     93        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     94        1.1       cgd  * SUCH DAMAGE.
     95        1.1       cgd  */
     96        1.1       cgd 
     97       1.26     lukem #include <sys/cdefs.h>
     98        1.1       cgd #ifndef lint
     99       1.12       tls #if 0
    100       1.12       tls static char sccsid[] = "@(#)ftp.c	8.6 (Berkeley) 10/27/94";
    101       1.12       tls #else
    102  1.126.2.9      tron __RCSID("$NetBSD: ftp.c,v 1.126.2.9 2005/07/24 10:33:09 tron Exp $");
    103       1.12       tls #endif
    104        1.1       cgd #endif /* not lint */
    105        1.1       cgd 
    106       1.21     lukem #include <sys/types.h>
    107        1.1       cgd #include <sys/stat.h>
    108        1.1       cgd #include <sys/socket.h>
    109       1.33  christos #include <sys/time.h>
    110        1.1       cgd 
    111        1.1       cgd #include <netinet/in.h>
    112        1.1       cgd #include <netinet/in_systm.h>
    113        1.1       cgd #include <netinet/ip.h>
    114        1.6       cgd #include <arpa/inet.h>
    115        1.1       cgd #include <arpa/ftp.h>
    116        1.1       cgd #include <arpa/telnet.h>
    117        1.1       cgd 
    118        1.6       cgd #include <ctype.h>
    119        1.6       cgd #include <err.h>
    120        1.1       cgd #include <errno.h>
    121  1.126.2.1      tron #include <fcntl.h>
    122        1.1       cgd #include <netdb.h>
    123        1.6       cgd #include <stdio.h>
    124        1.6       cgd #include <stdlib.h>
    125        1.6       cgd #include <string.h>
    126       1.32    kleink #include <time.h>
    127        1.6       cgd #include <unistd.h>
    128       1.23     lukem #include <stdarg.h>
    129        1.1       cgd 
    130        1.1       cgd #include "ftp_var.h"
    131       1.48    itojun 
    132      1.126     lukem volatile sig_atomic_t	abrtflag;
    133      1.126     lukem volatile sig_atomic_t	timeoutflag;
    134      1.126     lukem 
    135       1.83     lukem sigjmp_buf	ptabort;
    136        1.6       cgd int	ptabflg;
    137        1.1       cgd int	ptflag = 0;
    138       1.73     lukem char	pasv[BUFSIZ];	/* passive port for proxy data connection */
    139        1.1       cgd 
    140       1.95     lukem static int empty(FILE *, FILE *, int);
    141       1.45  christos 
    142      1.104     lukem struct sockinet {
    143      1.104     lukem 	union sockunion {
    144      1.104     lukem 		struct sockaddr_in  su_sin;
    145       1.62     lukem #ifdef INET6
    146      1.104     lukem 		struct sockaddr_in6 su_sin6;
    147       1.62     lukem #endif
    148      1.104     lukem 	} si_su;
    149      1.107     lukem #if !HAVE_SOCKADDR_SA_LEN
    150      1.104     lukem 	int	si_len;
    151       1.62     lukem #endif
    152       1.48    itojun };
    153       1.52  christos 
    154      1.107     lukem #if !HAVE_SOCKADDR_SA_LEN
    155      1.104     lukem # define su_len		si_len
    156      1.104     lukem #else
    157      1.104     lukem # define su_len		si_su.su_sin.sin_len
    158      1.104     lukem #endif
    159      1.104     lukem #define su_family	si_su.su_sin.sin_family
    160      1.104     lukem #define su_port		si_su.su_sin.sin_port
    161       1.48    itojun 
    162      1.104     lukem struct sockinet myctladdr, hisctladdr, data_addr;
    163        1.1       cgd 
    164        1.1       cgd char *
    165       1.95     lukem hookup(char *host, char *port)
    166        1.1       cgd {
    167  1.126.2.6      tron 	int s = -1, error, portnum;
    168       1.49    itojun 	struct addrinfo hints, *res, *res0;
    169       1.58  christos 	char hbuf[MAXHOSTNAMELEN];
    170       1.14     lukem 	static char hostnamebuf[MAXHOSTNAMELEN];
    171       1.49    itojun 	char *cause = "unknown";
    172  1.126.2.6      tron 	socklen_t len;
    173  1.126.2.4       snj 	int on = 1;
    174      1.108     lukem 
    175       1.48    itojun 	memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
    176      1.106     lukem 	memset((char *)&myctladdr, 0, sizeof (myctladdr));
    177       1.48    itojun 	memset(&hints, 0, sizeof(hints));
    178      1.108     lukem 	portnum = parseport(port, FTP_PORT);
    179       1.48    itojun 	hints.ai_flags = AI_CANONNAME;
    180      1.116     lukem 	hints.ai_family = family;
    181       1.48    itojun 	hints.ai_socktype = SOCK_STREAM;
    182       1.48    itojun 	hints.ai_protocol = 0;
    183      1.108     lukem 	error = getaddrinfo(host, NULL, &hints, &res0);
    184       1.48    itojun 	if (error) {
    185  1.126.2.8      tron 		warnx("%s: %s", host, gai_strerror(error));
    186       1.48    itojun 		code = -1;
    187       1.48    itojun 		return (0);
    188       1.48    itojun 	}
    189        1.1       cgd 
    190       1.49    itojun 	if (res0->ai_canonname)
    191       1.68     lukem 		(void)strlcpy(hostnamebuf, res0->ai_canonname,
    192       1.68     lukem 		    sizeof(hostnamebuf));
    193       1.48    itojun 	else
    194       1.68     lukem 		(void)strlcpy(hostnamebuf, host, sizeof(hostnamebuf));
    195        1.1       cgd 	hostname = hostnamebuf;
    196  1.126.2.2      tron 
    197       1.49    itojun 	for (res = res0; res; res = res->ai_next) {
    198       1.96    itojun 		/*
    199       1.96    itojun 		 * make sure that ai_addr is NOT an IPv4 mapped address.
    200       1.96    itojun 		 * IPv4 mapped address complicates too many things in FTP
    201       1.96    itojun 		 * protocol handling, as FTP protocol is defined differently
    202       1.96    itojun 		 * between IPv4 and IPv6.
    203       1.97    itojun 		 *
    204       1.97    itojun 		 * This may not be the best way to handle this situation,
    205       1.97    itojun 		 * since the semantics of IPv4 mapped address is defined in
    206       1.97    itojun 		 * the kernel.  There are configurations where we should use
    207       1.97    itojun 		 * IPv4 mapped address as native IPv6 address, not as
    208       1.97    itojun 		 * "an IPv6 address that embeds IPv4 address" (namely, SIIT).
    209       1.97    itojun 		 *
    210       1.97    itojun 		 * More complete solution would be to have an additional
    211       1.97    itojun 		 * getsockopt to grab "real" peername/sockname.  "real"
    212       1.97    itojun 		 * peername/sockname will be AF_INET if IPv4 mapped address
    213       1.97    itojun 		 * is used to embed IPv4 address, and will be AF_INET6 if
    214       1.97    itojun 		 * we use it as native.  What a mess!
    215       1.96    itojun 		 */
    216       1.96    itojun 		ai_unmapped(res);
    217  1.126.2.7      tron 		if (verbose && res0->ai_next) {
    218  1.126.2.7      tron 				/* if we have multiple possibilities */
    219      1.112    itojun 			if (getnameinfo(res->ai_addr, res->ai_addrlen,
    220      1.112    itojun 			    hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
    221      1.112    itojun 				strlcpy(hbuf, "?", sizeof(hbuf));
    222       1.49    itojun 			fprintf(ttyout, "Trying %s...\n", hbuf);
    223       1.49    itojun 		}
    224      1.108     lukem 		((struct sockaddr_in *)res->ai_addr)->sin_port = htons(portnum);
    225      1.109     lukem 		s = socket(res->ai_family, SOCK_STREAM, res->ai_protocol);
    226       1.49    itojun 		if (s < 0) {
    227       1.49    itojun 			cause = "socket";
    228       1.49    itojun 			continue;
    229       1.49    itojun 		}
    230      1.125     lukem 		error = xconnect(s, res->ai_addr, res->ai_addrlen);
    231       1.49    itojun 		if (error) {
    232       1.49    itojun 			/* this "if" clause is to prevent print warning twice */
    233       1.49    itojun 			if (res->ai_next) {
    234      1.112    itojun 				if (getnameinfo(res->ai_addr, res->ai_addrlen,
    235      1.106     lukem 				    hbuf, sizeof(hbuf), NULL, 0,
    236      1.112    itojun 				    NI_NUMERICHOST))
    237      1.112    itojun 					strlcpy(hbuf, "?", sizeof(hbuf));
    238       1.49    itojun 				warn("connect to address %s", hbuf);
    239        1.1       cgd 			}
    240       1.49    itojun 			cause = "connect";
    241       1.49    itojun 			close(s);
    242       1.49    itojun 			s = -1;
    243        1.1       cgd 			continue;
    244        1.1       cgd 		}
    245       1.49    itojun 
    246       1.49    itojun 		/* finally we got one */
    247       1.49    itojun 		break;
    248       1.49    itojun 	}
    249       1.49    itojun 	if (s < 0) {
    250      1.101    itojun 		warn("%s", cause);
    251        1.1       cgd 		code = -1;
    252       1.50    itojun 		freeaddrinfo(res0);
    253       1.49    itojun 		return 0;
    254        1.1       cgd 	}
    255      1.104     lukem 	memcpy(&hisctladdr.si_su, res->ai_addr, res->ai_addrlen);
    256      1.104     lukem 	hisctladdr.su_len = res->ai_addrlen;
    257       1.49    itojun 	freeaddrinfo(res0);
    258       1.49    itojun 	res0 = res = NULL;
    259       1.58  christos 
    260      1.105     lukem 	len = hisctladdr.su_len;
    261  1.126.2.6      tron 	if (getsockname(s, (struct sockaddr *)&myctladdr.si_su, &len) == -1) {
    262        1.6       cgd 		warn("getsockname");
    263        1.1       cgd 		code = -1;
    264        1.1       cgd 		goto bad;
    265        1.1       cgd 	}
    266      1.105     lukem 	myctladdr.su_len = len;
    267       1.58  christos 
    268       1.92     lukem #ifdef IPTOS_LOWDELAY
    269      1.104     lukem 	if (hisctladdr.su_family == AF_INET) {
    270       1.53  christos 		int tos = IPTOS_LOWDELAY;
    271  1.126.2.4       snj 		if (setsockopt(s, IPPROTO_IP, IP_TOS,
    272  1.126.2.5       snj 				(void *)&tos, sizeof(tos)) == -1) {
    273  1.126.2.9      tron 				DWARN("setsockopt %s (ignored)",
    274  1.126.2.4       snj 				    "IPTOS_LOWDELAY");
    275  1.126.2.4       snj 		}
    276       1.48    itojun 	}
    277       1.92     lukem #endif
    278        1.1       cgd 	cin = fdopen(s, "r");
    279        1.1       cgd 	cout = fdopen(s, "w");
    280        1.1       cgd 	if (cin == NULL || cout == NULL) {
    281        1.6       cgd 		warnx("fdopen failed.");
    282        1.1       cgd 		if (cin)
    283       1.23     lukem 			(void)fclose(cin);
    284        1.1       cgd 		if (cout)
    285       1.23     lukem 			(void)fclose(cout);
    286        1.1       cgd 		code = -1;
    287        1.1       cgd 		goto bad;
    288        1.1       cgd 	}
    289        1.1       cgd 	if (verbose)
    290       1.35     lukem 		fprintf(ttyout, "Connected to %s.\n", hostname);
    291       1.87     lukem 	if (getreply(0) > 2) {	/* read startup message from server */
    292        1.1       cgd 		if (cin)
    293       1.23     lukem 			(void)fclose(cin);
    294        1.1       cgd 		if (cout)
    295       1.23     lukem 			(void)fclose(cout);
    296        1.1       cgd 		code = -1;
    297        1.1       cgd 		goto bad;
    298        1.1       cgd 	}
    299        1.1       cgd 
    300  1.126.2.4       snj 	if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE,
    301  1.126.2.4       snj 			(void *)&on, sizeof(on)) == -1) {
    302  1.126.2.9      tron 		DWARN("setsockopt %s (ignored)", "SO_OOBINLINE");
    303        1.1       cgd 	}
    304        1.1       cgd 
    305        1.1       cgd 	return (hostname);
    306      1.102     lukem  bad:
    307       1.23     lukem 	(void)close(s);
    308       1.31     lukem 	return (NULL);
    309        1.1       cgd }
    310        1.1       cgd 
    311        1.1       cgd void
    312       1.95     lukem cmdabort(int notused)
    313        1.1       cgd {
    314       1.81     lukem 	int oerrno = errno;
    315        1.1       cgd 
    316      1.126     lukem 	sigint_raised = 1;
    317       1.22     lukem 	alarmtimer(0);
    318       1.84     lukem 	if (fromatty)
    319       1.84     lukem 		write(fileno(ttyout), "\n", 1);
    320        1.1       cgd 	abrtflag++;
    321        1.1       cgd 	if (ptflag)
    322       1.81     lukem 		siglongjmp(ptabort, 1);
    323       1.81     lukem 	errno = oerrno;
    324        1.1       cgd }
    325        1.1       cgd 
    326       1.76     lukem void
    327       1.95     lukem cmdtimeout(int notused)
    328       1.76     lukem {
    329       1.81     lukem 	int oerrno = errno;
    330       1.76     lukem 
    331       1.76     lukem 	alarmtimer(0);
    332       1.84     lukem 	if (fromatty)
    333       1.84     lukem 		write(fileno(ttyout), "\n", 1);
    334       1.76     lukem 	timeoutflag++;
    335       1.76     lukem 	if (ptflag)
    336       1.81     lukem 		siglongjmp(ptabort, 1);
    337       1.81     lukem 	errno = oerrno;
    338       1.76     lukem }
    339       1.76     lukem 
    340        1.1       cgd /*VARARGS*/
    341        1.6       cgd int
    342       1.23     lukem command(const char *fmt, ...)
    343        1.1       cgd {
    344        1.1       cgd 	va_list ap;
    345        1.1       cgd 	int r;
    346       1.90     lukem 	sigfunc oldsigint;
    347        1.1       cgd 
    348  1.126.2.9      tron #ifndef NO_DEBUG
    349        1.1       cgd 	if (debug) {
    350       1.35     lukem 		fputs("---> ", ttyout);
    351       1.23     lukem 		va_start(ap, fmt);
    352        1.1       cgd 		if (strncmp("PASS ", fmt, 5) == 0)
    353       1.35     lukem 			fputs("PASS XXXX", ttyout);
    354       1.14     lukem 		else if (strncmp("ACCT ", fmt, 5) == 0)
    355       1.35     lukem 			fputs("ACCT XXXX", ttyout);
    356       1.14     lukem 		else
    357       1.35     lukem 			vfprintf(ttyout, fmt, ap);
    358        1.1       cgd 		va_end(ap);
    359       1.35     lukem 		putc('\n', ttyout);
    360        1.1       cgd 	}
    361  1.126.2.9      tron #endif
    362        1.1       cgd 	if (cout == NULL) {
    363       1.23     lukem 		warnx("No control connection for command.");
    364        1.1       cgd 		code = -1;
    365        1.1       cgd 		return (0);
    366        1.1       cgd 	}
    367       1.76     lukem 
    368       1.76     lukem 	abrtflag = 0;
    369       1.76     lukem 
    370       1.76     lukem 	oldsigint = xsignal(SIGINT, cmdabort);
    371       1.76     lukem 
    372       1.23     lukem 	va_start(ap, fmt);
    373        1.1       cgd 	vfprintf(cout, fmt, ap);
    374        1.1       cgd 	va_end(ap);
    375       1.23     lukem 	fputs("\r\n", cout);
    376       1.23     lukem 	(void)fflush(cout);
    377        1.1       cgd 	cpend = 1;
    378        1.1       cgd 	r = getreply(!strcmp(fmt, "QUIT"));
    379       1.76     lukem 	if (abrtflag && oldsigint != SIG_IGN)
    380       1.76     lukem 		(*oldsigint)(SIGINT);
    381       1.76     lukem 	(void)xsignal(SIGINT, oldsigint);
    382        1.6       cgd 	return (r);
    383        1.1       cgd }
    384        1.1       cgd 
    385        1.6       cgd int
    386       1.95     lukem getreply(int expecteof)
    387        1.1       cgd {
    388       1.16     lukem 	char current_line[BUFSIZ];	/* last line of previous reply */
    389       1.16     lukem 	int c, n, line;
    390        1.6       cgd 	int dig;
    391        1.1       cgd 	int originalcode = 0, continuation = 0;
    392       1.90     lukem 	sigfunc oldsigint, oldsigalrm;
    393        1.1       cgd 	int pflag = 0;
    394        1.6       cgd 	char *cp, *pt = pasv;
    395        1.1       cgd 
    396       1.76     lukem 	abrtflag = 0;
    397       1.76     lukem 	timeoutflag = 0;
    398       1.76     lukem 
    399       1.76     lukem 	oldsigint = xsignal(SIGINT, cmdabort);
    400       1.76     lukem 	oldsigalrm = xsignal(SIGALRM, cmdtimeout);
    401       1.76     lukem 
    402       1.16     lukem 	for (line = 0 ;; line++) {
    403        1.1       cgd 		dig = n = code = 0;
    404       1.16     lukem 		cp = current_line;
    405       1.76     lukem 		while (alarmtimer(60),((c = getc(cin)) != '\n')) {
    406        1.1       cgd 			if (c == IAC) {     /* handle telnet commands */
    407        1.1       cgd 				switch (c = getc(cin)) {
    408        1.1       cgd 				case WILL:
    409        1.1       cgd 				case WONT:
    410        1.1       cgd 					c = getc(cin);
    411        1.1       cgd 					fprintf(cout, "%c%c%c", IAC, DONT, c);
    412       1.23     lukem 					(void)fflush(cout);
    413        1.1       cgd 					break;
    414        1.1       cgd 				case DO:
    415        1.1       cgd 				case DONT:
    416        1.1       cgd 					c = getc(cin);
    417        1.1       cgd 					fprintf(cout, "%c%c%c", IAC, WONT, c);
    418       1.23     lukem 					(void)fflush(cout);
    419        1.1       cgd 					break;
    420        1.1       cgd 				default:
    421        1.1       cgd 					break;
    422        1.1       cgd 				}
    423        1.1       cgd 				continue;
    424        1.1       cgd 			}
    425        1.1       cgd 			dig++;
    426        1.1       cgd 			if (c == EOF) {
    427       1.76     lukem 				/*
    428       1.76     lukem 				 * these will get trashed by pswitch()
    429       1.76     lukem 				 * in lostpeer()
    430       1.76     lukem 				 */
    431       1.76     lukem 				int reply_timeoutflag = timeoutflag;
    432       1.76     lukem 				int reply_abrtflag = abrtflag;
    433       1.76     lukem 
    434       1.85     lukem 				alarmtimer(0);
    435       1.85     lukem 				if (expecteof && feof(cin)) {
    436       1.76     lukem 					(void)xsignal(SIGINT, oldsigint);
    437       1.76     lukem 					(void)xsignal(SIGALRM, oldsigalrm);
    438        1.1       cgd 					code = 221;
    439        1.1       cgd 					return (0);
    440        1.1       cgd 				}
    441       1.85     lukem 				cpend = 0;
    442       1.85     lukem 				lostpeer(0);
    443        1.1       cgd 				if (verbose) {
    444       1.76     lukem 					if (reply_timeoutflag)
    445       1.76     lukem 						fputs(
    446       1.85     lukem     "421 Service not available, remote server timed out. Connection closed\n",
    447       1.85     lukem 						    ttyout);
    448       1.76     lukem 					else if (reply_abrtflag)
    449       1.76     lukem 						fputs(
    450       1.85     lukem     "421 Service not available, user interrupt. Connection closed.\n",
    451       1.85     lukem 						    ttyout);
    452       1.76     lukem 					else
    453       1.76     lukem 						fputs(
    454       1.76     lukem     "421 Service not available, remote server has closed connection.\n",
    455       1.85     lukem 						    ttyout);
    456       1.76     lukem 					(void)fflush(ttyout);
    457        1.1       cgd 				}
    458        1.1       cgd 				code = 421;
    459       1.76     lukem 				(void)xsignal(SIGINT, oldsigint);
    460       1.76     lukem 				(void)xsignal(SIGALRM, oldsigalrm);
    461        1.6       cgd 				return (4);
    462        1.1       cgd 			}
    463        1.1       cgd 			if (c != '\r' && (verbose > 0 ||
    464       1.35     lukem 			    ((verbose > -1 && n == '5' && dig > 4) &&
    465       1.35     lukem 			    (((!n && c < '5') || (n && n < '5'))
    466       1.35     lukem 			     || !retry_connect)))) {
    467        1.1       cgd 				if (proxflag &&
    468       1.16     lukem 				   (dig == 1 || (dig == 5 && verbose == 0)))
    469       1.35     lukem 					fprintf(ttyout, "%s:", hostname);
    470       1.35     lukem 				(void)putc(c, ttyout);
    471        1.1       cgd 			}
    472        1.1       cgd 			if (dig < 4 && isdigit(c))
    473        1.1       cgd 				code = code * 10 + (c - '0');
    474       1.48    itojun 			if (!pflag && (code == 227 || code == 228))
    475        1.1       cgd 				pflag = 1;
    476       1.48    itojun 			else if (!pflag && code == 229)
    477       1.48    itojun 				pflag = 100;
    478        1.1       cgd 			if (dig > 4 && pflag == 1 && isdigit(c))
    479        1.1       cgd 				pflag = 2;
    480        1.1       cgd 			if (pflag == 2) {
    481      1.118    itojun 				if (c != '\r' && c != ')') {
    482      1.118    itojun 					if (pt < &pasv[sizeof(pasv) - 1])
    483      1.118    itojun 						*pt++ = c;
    484      1.118    itojun 				} else {
    485        1.1       cgd 					*pt = '\0';
    486        1.1       cgd 					pflag = 3;
    487        1.1       cgd 				}
    488        1.1       cgd 			}
    489       1.48    itojun 			if (pflag == 100 && c == '(')
    490       1.48    itojun 				pflag = 2;
    491        1.1       cgd 			if (dig == 4 && c == '-') {
    492        1.1       cgd 				if (continuation)
    493        1.1       cgd 					code = 0;
    494        1.1       cgd 				continuation++;
    495        1.1       cgd 			}
    496        1.1       cgd 			if (n == 0)
    497        1.1       cgd 				n = c;
    498       1.16     lukem 			if (cp < &current_line[sizeof(current_line) - 1])
    499        1.1       cgd 				*cp++ = c;
    500        1.1       cgd 		}
    501       1.35     lukem 		if (verbose > 0 || ((verbose > -1 && n == '5') &&
    502       1.35     lukem 		    (n < '5' || !retry_connect))) {
    503       1.35     lukem 			(void)putc(c, ttyout);
    504       1.35     lukem 			(void)fflush (ttyout);
    505        1.1       cgd 		}
    506      1.102     lukem 		if (cp[-1] == '\r')
    507      1.102     lukem 			cp[-1] = '\0';
    508      1.102     lukem 		*cp = '\0';
    509      1.102     lukem 		if (line == 0)
    510      1.102     lukem 			(void)strlcpy(reply_string, current_line,
    511      1.102     lukem 			    sizeof(reply_string));
    512      1.102     lukem 		if (line > 0 && code == 0 && reply_callback != NULL)
    513      1.102     lukem 			(*reply_callback)(current_line);
    514        1.1       cgd 		if (continuation && code != originalcode) {
    515        1.1       cgd 			if (originalcode == 0)
    516        1.1       cgd 				originalcode = code;
    517        1.1       cgd 			continue;
    518        1.1       cgd 		}
    519        1.1       cgd 		if (n != '1')
    520        1.1       cgd 			cpend = 0;
    521       1.85     lukem 		alarmtimer(0);
    522       1.76     lukem 		(void)xsignal(SIGINT, oldsigint);
    523       1.76     lukem 		(void)xsignal(SIGALRM, oldsigalrm);
    524        1.1       cgd 		if (code == 421 || originalcode == 421)
    525       1.85     lukem 			lostpeer(0);
    526       1.76     lukem 		if (abrtflag && oldsigint != cmdabort && oldsigint != SIG_IGN)
    527       1.76     lukem 			(*oldsigint)(SIGINT);
    528       1.76     lukem 		if (timeoutflag && oldsigalrm != cmdtimeout &&
    529       1.76     lukem 		    oldsigalrm != SIG_IGN)
    530       1.76     lukem 			(*oldsigalrm)(SIGINT);
    531        1.1       cgd 		return (n - '0');
    532        1.1       cgd 	}
    533        1.1       cgd }
    534        1.1       cgd 
    535       1.45  christos static int
    536       1.95     lukem empty(FILE *cin, FILE *din, int sec)
    537        1.1       cgd {
    538  1.126.2.1      tron 	int		nr, nfd;
    539  1.126.2.1      tron 	struct pollfd	pfd[2];
    540       1.45  christos 
    541  1.126.2.1      tron 	nfd = 0;
    542       1.45  christos 	if (cin) {
    543      1.124     lukem 		pfd[nfd].fd = fileno(cin);
    544      1.124     lukem 		pfd[nfd++].events = POLLIN;
    545       1.45  christos 	}
    546       1.45  christos 
    547       1.45  christos 	if (din) {
    548      1.124     lukem 		pfd[nfd].fd = fileno(din);
    549      1.124     lukem 		pfd[nfd++].events = POLLIN;
    550       1.45  christos 	}
    551       1.45  christos 
    552  1.126.2.1      tron 	if ((nr = xpoll(pfd, nfd, sec * 1000)) <= 0)
    553       1.45  christos 		return nr;
    554       1.45  christos 
    555       1.45  christos 	nr = 0;
    556       1.45  christos 	nfd = 0;
    557       1.45  christos 	if (cin)
    558       1.45  christos 		nr |= (pfd[nfd++].revents & POLLIN) ? 1 : 0;
    559       1.45  christos 	if (din)
    560       1.45  christos 		nr |= (pfd[nfd++].revents & POLLIN) ? 2 : 0;
    561       1.45  christos 	return nr;
    562        1.1       cgd }
    563        1.1       cgd 
    564       1.83     lukem sigjmp_buf	xferabort;
    565        1.1       cgd 
    566        1.1       cgd void
    567       1.95     lukem abortxfer(int notused)
    568        1.1       cgd {
    569       1.81     lukem 	char msgbuf[100];
    570  1.126.2.6      tron 	size_t len;
    571        1.1       cgd 
    572      1.126     lukem 	sigint_raised = 1;
    573       1.22     lukem 	alarmtimer(0);
    574        1.1       cgd 	mflag = 0;
    575        1.1       cgd 	abrtflag = 0;
    576       1.81     lukem 	switch (direction[0]) {
    577       1.81     lukem 	case 'r':
    578       1.81     lukem 		strlcpy(msgbuf, "\nreceive", sizeof(msgbuf));
    579       1.81     lukem 		break;
    580       1.81     lukem 	case 's':
    581       1.81     lukem 		strlcpy(msgbuf, "\nsend", sizeof(msgbuf));
    582       1.81     lukem 		break;
    583       1.81     lukem 	default:
    584       1.85     lukem 		errx(1, "abortxfer called with unknown direction `%s'",
    585       1.81     lukem 		    direction);
    586       1.81     lukem 	}
    587       1.85     lukem 	len = strlcat(msgbuf, " aborted. Waiting for remote to finish abort.\n",
    588       1.81     lukem 	    sizeof(msgbuf));
    589       1.81     lukem 	write(fileno(ttyout), msgbuf, len);
    590       1.81     lukem 	siglongjmp(xferabort, 1);
    591        1.1       cgd }
    592        1.1       cgd 
    593        1.6       cgd void
    594       1.95     lukem sendrequest(const char *cmd, const char *local, const char *remote,
    595       1.95     lukem 	    int printnames)
    596        1.1       cgd {
    597        1.1       cgd 	struct stat st;
    598        1.6       cgd 	int c, d;
    599       1.26     lukem 	FILE *fin, *dout;
    600       1.95     lukem 	int (*closefunc)(FILE *);
    601       1.90     lukem 	sigfunc oldintr, oldintp;
    602       1.26     lukem 	volatile off_t hashbytes;
    603       1.64     lukem 	char *lmode, *bufp;
    604       1.64     lukem 	static size_t bufsize;
    605       1.64     lukem 	static char *buf;
    606       1.23     lukem 	int oprogress;
    607        1.1       cgd 
    608       1.35     lukem #ifdef __GNUC__			/* to shut up gcc warnings */
    609       1.26     lukem 	(void)&fin;
    610       1.26     lukem 	(void)&dout;
    611       1.26     lukem 	(void)&closefunc;
    612       1.26     lukem 	(void)&oldintr;
    613       1.26     lukem 	(void)&oldintp;
    614       1.26     lukem 	(void)&lmode;
    615       1.26     lukem #endif
    616       1.26     lukem 
    617       1.16     lukem 	hashbytes = mark;
    618       1.14     lukem 	direction = "sent";
    619       1.26     lukem 	dout = NULL;
    620       1.14     lukem 	bytes = 0;
    621       1.16     lukem 	filesize = -1;
    622       1.23     lukem 	oprogress = progress;
    623        1.1       cgd 	if (verbose && printnames) {
    624        1.1       cgd 		if (local && *local != '-')
    625       1.35     lukem 			fprintf(ttyout, "local: %s ", local);
    626        1.1       cgd 		if (remote)
    627       1.35     lukem 			fprintf(ttyout, "remote: %s\n", remote);
    628        1.1       cgd 	}
    629        1.1       cgd 	if (proxy) {
    630        1.1       cgd 		proxtrans(cmd, local, remote);
    631        1.1       cgd 		return;
    632        1.1       cgd 	}
    633        1.1       cgd 	if (curtype != type)
    634        1.1       cgd 		changetype(type, 0);
    635        1.1       cgd 	closefunc = NULL;
    636        1.1       cgd 	oldintr = NULL;
    637        1.1       cgd 	oldintp = NULL;
    638        1.1       cgd 	lmode = "w";
    639       1.81     lukem 	if (sigsetjmp(xferabort, 1)) {
    640       1.85     lukem 		while (cpend)
    641       1.23     lukem 			(void)getreply(0);
    642       1.79     lukem 		code = -1;
    643       1.30     lukem 		goto cleanupsend;
    644        1.1       cgd 	}
    645       1.80     lukem 	(void)xsignal(SIGQUIT, psummary);
    646       1.81     lukem 	oldintr = xsignal(SIGINT, abortxfer);
    647       1.23     lukem 	if (strcmp(local, "-") == 0) {
    648        1.1       cgd 		fin = stdin;
    649       1.23     lukem 		progress = 0;
    650       1.23     lukem 	} else if (*local == '|') {
    651       1.67     lukem 		oldintp = xsignal(SIGPIPE, SIG_IGN);
    652        1.1       cgd 		fin = popen(local + 1, "r");
    653        1.1       cgd 		if (fin == NULL) {
    654        1.6       cgd 			warn("%s", local + 1);
    655       1.79     lukem 			code = -1;
    656       1.30     lukem 			goto cleanupsend;
    657        1.1       cgd 		}
    658       1.23     lukem 		progress = 0;
    659        1.1       cgd 		closefunc = pclose;
    660        1.1       cgd 	} else {
    661        1.1       cgd 		fin = fopen(local, "r");
    662        1.1       cgd 		if (fin == NULL) {
    663        1.6       cgd 			warn("local: %s", local);
    664       1.79     lukem 			code = -1;
    665       1.30     lukem 			goto cleanupsend;
    666        1.1       cgd 		}
    667        1.1       cgd 		closefunc = fclose;
    668       1.29   mycroft 		if (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode)) {
    669       1.35     lukem 			fprintf(ttyout, "%s: not a plain file.\n", local);
    670       1.79     lukem 			code = -1;
    671       1.30     lukem 			goto cleanupsend;
    672        1.1       cgd 		}
    673       1.16     lukem 		filesize = st.st_size;
    674        1.1       cgd 	}
    675        1.1       cgd 	if (initconn()) {
    676       1.79     lukem 		code = -1;
    677       1.30     lukem 		goto cleanupsend;
    678        1.1       cgd 	}
    679       1.81     lukem 	if (sigsetjmp(xferabort, 1))
    680        1.1       cgd 		goto abort;
    681        1.1       cgd 
    682        1.1       cgd 	if (restart_point &&
    683        1.1       cgd 	    (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
    684        1.6       cgd 		int rc;
    685        1.6       cgd 
    686       1.21     lukem 		rc = -1;
    687        1.6       cgd 		switch (curtype) {
    688        1.6       cgd 		case TYPE_A:
    689      1.115     lukem 			rc = fseeko(fin, restart_point, SEEK_SET);
    690        1.6       cgd 			break;
    691        1.6       cgd 		case TYPE_I:
    692        1.6       cgd 		case TYPE_L:
    693        1.6       cgd 			rc = lseek(fileno(fin), restart_point, SEEK_SET);
    694        1.6       cgd 			break;
    695        1.6       cgd 		}
    696        1.6       cgd 		if (rc < 0) {
    697        1.6       cgd 			warn("local: %s", local);
    698       1.30     lukem 			goto cleanupsend;
    699        1.1       cgd 		}
    700      1.107     lukem 		if (command("REST " LLF, (LLT)restart_point) != CONTINUE)
    701       1.30     lukem 			goto cleanupsend;
    702      1.119     lukem 		lmode = "r+";
    703        1.1       cgd 	}
    704        1.1       cgd 	if (remote) {
    705       1.85     lukem 		if (command("%s %s", cmd, remote) != PRELIM)
    706       1.30     lukem 			goto cleanupsend;
    707       1.85     lukem 	} else {
    708       1.85     lukem 		if (command("%s", cmd) != PRELIM)
    709       1.30     lukem 			goto cleanupsend;
    710       1.85     lukem 	}
    711      1.121     lukem 	dirchange = 1;
    712        1.1       cgd 	dout = dataconn(lmode);
    713        1.1       cgd 	if (dout == NULL)
    714        1.1       cgd 		goto abort;
    715       1.64     lukem 
    716       1.64     lukem 	if (sndbuf_size > bufsize) {
    717       1.64     lukem 		if (buf)
    718       1.64     lukem 			(void)free(buf);
    719       1.64     lukem 		bufsize = sndbuf_size;
    720       1.64     lukem 		buf = xmalloc(bufsize);
    721       1.64     lukem 	}
    722       1.64     lukem 
    723       1.16     lukem 	progressmeter(-1);
    724       1.67     lukem 	oldintp = xsignal(SIGPIPE, SIG_IGN);
    725       1.64     lukem 
    726        1.1       cgd 	switch (curtype) {
    727        1.1       cgd 
    728        1.1       cgd 	case TYPE_I:
    729        1.1       cgd 	case TYPE_L:
    730       1.89     lukem 		if (rate_put) {		/* rate limited */
    731       1.89     lukem 			while (1) {
    732       1.89     lukem 				struct timeval then, now, td;
    733       1.89     lukem 				off_t bufrem;
    734       1.46     lukem 
    735       1.62     lukem 				(void)gettimeofday(&then, NULL);
    736       1.89     lukem 				errno = c = d = 0;
    737       1.89     lukem 				bufrem = rate_put;
    738       1.89     lukem 				while (bufrem > 0) {
    739       1.89     lukem 					if ((c = read(fileno(fin), buf,
    740       1.89     lukem 					    MIN(bufsize, bufrem))) <= 0)
    741       1.89     lukem 						goto senddone;
    742       1.89     lukem 					bytes += c;
    743       1.89     lukem 					bufrem -= c;
    744       1.89     lukem 					for (bufp = buf; c > 0;
    745       1.89     lukem 					    c -= d, bufp += d)
    746       1.89     lukem 						if ((d = write(fileno(dout),
    747       1.89     lukem 						    bufp, c)) <= 0)
    748       1.89     lukem 							break;
    749       1.89     lukem 					if (d < 0)
    750       1.89     lukem 						goto senddone;
    751       1.89     lukem 					if (hash &&
    752       1.89     lukem 					    (!progress || filesize < 0) ) {
    753       1.89     lukem 						while (bytes >= hashbytes) {
    754       1.89     lukem 							(void)putc('#', ttyout);
    755       1.89     lukem 							hashbytes += mark;
    756       1.89     lukem 						}
    757       1.89     lukem 						(void)fflush(ttyout);
    758       1.89     lukem 					}
    759       1.89     lukem 				}
    760       1.89     lukem 				while (1) {
    761       1.89     lukem 					(void)gettimeofday(&now, NULL);
    762       1.89     lukem 					timersub(&now, &then, &td);
    763       1.89     lukem 					if (td.tv_sec > 0)
    764       1.89     lukem 						break;
    765       1.89     lukem 					usleep(1000000 - td.tv_usec);
    766       1.89     lukem 				}
    767       1.89     lukem 			}
    768      1.102     lukem 		} else {		/* simpler/faster; no rate limit */
    769       1.89     lukem 			while (1) {
    770       1.89     lukem 				errno = c = d = 0;
    771       1.89     lukem 				if ((c = read(fileno(fin), buf, bufsize)) <= 0)
    772       1.46     lukem 					goto senddone;
    773       1.46     lukem 				bytes += c;
    774       1.46     lukem 				for (bufp = buf; c > 0; c -= d, bufp += d)
    775       1.46     lukem 					if ((d = write(fileno(dout), bufp, c))
    776       1.46     lukem 					    <= 0)
    777       1.46     lukem 						break;
    778       1.46     lukem 				if (d < 0)
    779       1.46     lukem 					goto senddone;
    780       1.46     lukem 				if (hash && (!progress || filesize < 0) ) {
    781       1.46     lukem 					while (bytes >= hashbytes) {
    782       1.46     lukem 						(void)putc('#', ttyout);
    783       1.46     lukem 						hashbytes += mark;
    784       1.46     lukem 					}
    785       1.46     lukem 					(void)fflush(ttyout);
    786       1.46     lukem 				}
    787       1.46     lukem 			}
    788        1.1       cgd 		}
    789       1.46     lukem  senddone:
    790       1.22     lukem 		if (hash && (!progress || filesize < 0) && bytes > 0) {
    791       1.14     lukem 			if (bytes < mark)
    792       1.35     lukem 				(void)putc('#', ttyout);
    793       1.35     lukem 			(void)putc('\n', ttyout);
    794        1.1       cgd 		}
    795        1.1       cgd 		if (c < 0)
    796        1.6       cgd 			warn("local: %s", local);
    797        1.1       cgd 		if (d < 0) {
    798       1.14     lukem 			if (errno != EPIPE)
    799        1.6       cgd 				warn("netout");
    800        1.1       cgd 			bytes = -1;
    801        1.1       cgd 		}
    802        1.1       cgd 		break;
    803        1.1       cgd 
    804        1.1       cgd 	case TYPE_A:
    805        1.1       cgd 		while ((c = getc(fin)) != EOF) {
    806        1.1       cgd 			if (c == '\n') {
    807       1.22     lukem 				while (hash && (!progress || filesize < 0) &&
    808       1.16     lukem 				    (bytes >= hashbytes)) {
    809       1.35     lukem 					(void)putc('#', ttyout);
    810       1.35     lukem 					(void)fflush(ttyout);
    811       1.14     lukem 					hashbytes += mark;
    812        1.1       cgd 				}
    813        1.1       cgd 				if (ferror(dout))
    814        1.1       cgd 					break;
    815       1.23     lukem 				(void)putc('\r', dout);
    816        1.1       cgd 				bytes++;
    817        1.1       cgd 			}
    818       1.23     lukem 			(void)putc(c, dout);
    819        1.1       cgd 			bytes++;
    820       1.16     lukem #if 0	/* this violates RFC */
    821       1.16     lukem 			if (c == '\r') {
    822       1.16     lukem 				(void)putc('\0', dout);
    823       1.16     lukem 				bytes++;
    824       1.16     lukem 			}
    825       1.16     lukem #endif
    826        1.1       cgd 		}
    827       1.22     lukem 		if (hash && (!progress || filesize < 0)) {
    828        1.1       cgd 			if (bytes < hashbytes)
    829       1.35     lukem 				(void)putc('#', ttyout);
    830       1.35     lukem 			(void)putc('\n', ttyout);
    831        1.1       cgd 		}
    832        1.1       cgd 		if (ferror(fin))
    833        1.6       cgd 			warn("local: %s", local);
    834        1.1       cgd 		if (ferror(dout)) {
    835        1.1       cgd 			if (errno != EPIPE)
    836        1.6       cgd 				warn("netout");
    837        1.1       cgd 			bytes = -1;
    838        1.1       cgd 		}
    839        1.1       cgd 		break;
    840        1.1       cgd 	}
    841       1.78     lukem 
    842       1.22     lukem 	progressmeter(1);
    843       1.78     lukem 	if (closefunc != NULL) {
    844        1.1       cgd 		(*closefunc)(fin);
    845       1.78     lukem 		fin = NULL;
    846       1.78     lukem 	}
    847       1.79     lukem 	(void)fclose(dout);
    848       1.79     lukem 	dout = NULL;
    849       1.23     lukem 	(void)getreply(0);
    850        1.1       cgd 	if (bytes > 0)
    851       1.16     lukem 		ptransfer(0);
    852       1.30     lukem 	goto cleanupsend;
    853       1.78     lukem 
    854      1.102     lukem  abort:
    855       1.80     lukem 	(void)xsignal(SIGINT, oldintr);
    856       1.80     lukem 	oldintr = NULL;
    857       1.78     lukem 	if (!cpend) {
    858       1.79     lukem 		code = -1;
    859       1.78     lukem 		goto cleanupsend;
    860       1.78     lukem 	}
    861       1.80     lukem 	if (data >= 0) {
    862       1.80     lukem 		(void)close(data);
    863       1.80     lukem 		data = -1;
    864       1.80     lukem 	}
    865       1.80     lukem 	if (dout) {
    866       1.80     lukem 		(void)fclose(dout);
    867       1.80     lukem 		dout = NULL;
    868       1.80     lukem 	}
    869       1.78     lukem 	(void)getreply(0);
    870       1.79     lukem 	code = -1;
    871       1.78     lukem 	if (bytes > 0)
    872       1.78     lukem 		ptransfer(0);
    873       1.78     lukem 
    874      1.102     lukem  cleanupsend:
    875       1.81     lukem 	if (oldintr)
    876       1.81     lukem 		(void)xsignal(SIGINT, oldintr);
    877       1.81     lukem 	if (oldintp)
    878       1.81     lukem 		(void)xsignal(SIGPIPE, oldintp);
    879       1.80     lukem 	if (data >= 0) {
    880       1.80     lukem 		(void)close(data);
    881       1.80     lukem 		data = -1;
    882       1.80     lukem 	}
    883       1.78     lukem 	if (closefunc != NULL && fin != NULL)
    884       1.78     lukem 		(*closefunc)(fin);
    885       1.78     lukem 	if (dout)
    886       1.78     lukem 		(void)fclose(dout);
    887       1.30     lukem 	progress = oprogress;
    888       1.30     lukem 	restart_point = 0;
    889       1.78     lukem 	bytes = 0;
    890        1.1       cgd }
    891        1.1       cgd 
    892        1.6       cgd void
    893       1.95     lukem recvrequest(const char *cmd, const char *local, const char *remote,
    894       1.95     lukem 	    const char *lmode, int printnames, int ignorespecial)
    895        1.1       cgd {
    896       1.26     lukem 	FILE *fout, *din;
    897       1.95     lukem 	int (*closefunc)(FILE *);
    898       1.90     lukem 	sigfunc oldintr, oldintp;
    899       1.26     lukem 	int c, d;
    900       1.26     lukem 	volatile int is_retr, tcrflag, bare_lfs;
    901       1.47     lukem 	static size_t bufsize;
    902       1.47     lukem 	static char *buf;
    903       1.26     lukem 	volatile off_t hashbytes;
    904        1.1       cgd 	struct stat st;
    905       1.15     lukem 	time_t mtime;
    906       1.16     lukem 	struct timeval tval[2];
    907       1.23     lukem 	int oprogress;
    908       1.24     lukem 	int opreserve;
    909        1.1       cgd 
    910       1.35     lukem #ifdef __GNUC__			/* to shut up gcc warnings */
    911       1.26     lukem 	(void)&local;
    912       1.26     lukem 	(void)&fout;
    913       1.26     lukem 	(void)&din;
    914       1.26     lukem 	(void)&closefunc;
    915       1.26     lukem 	(void)&oldintr;
    916       1.26     lukem 	(void)&oldintp;
    917       1.26     lukem #endif
    918       1.26     lukem 
    919       1.26     lukem 	fout = NULL;
    920       1.26     lukem 	din = NULL;
    921       1.16     lukem 	hashbytes = mark;
    922       1.14     lukem 	direction = "received";
    923       1.14     lukem 	bytes = 0;
    924       1.26     lukem 	bare_lfs = 0;
    925       1.16     lukem 	filesize = -1;
    926       1.23     lukem 	oprogress = progress;
    927       1.24     lukem 	opreserve = preserve;
    928       1.30     lukem 	is_retr = (strcmp(cmd, "RETR") == 0);
    929        1.1       cgd 	if (is_retr && verbose && printnames) {
    930       1.27     lukem 		if (local && (ignorespecial || *local != '-'))
    931       1.35     lukem 			fprintf(ttyout, "local: %s ", local);
    932        1.1       cgd 		if (remote)
    933       1.35     lukem 			fprintf(ttyout, "remote: %s\n", remote);
    934        1.1       cgd 	}
    935        1.1       cgd 	if (proxy && is_retr) {
    936        1.1       cgd 		proxtrans(cmd, local, remote);
    937        1.1       cgd 		return;
    938        1.1       cgd 	}
    939        1.1       cgd 	closefunc = NULL;
    940        1.1       cgd 	oldintr = NULL;
    941        1.1       cgd 	oldintp = NULL;
    942        1.1       cgd 	tcrflag = !crflag && is_retr;
    943       1.81     lukem 	if (sigsetjmp(xferabort, 1)) {
    944       1.85     lukem 		while (cpend)
    945       1.23     lukem 			(void)getreply(0);
    946       1.79     lukem 		code = -1;
    947       1.78     lukem 		goto cleanuprecv;
    948        1.1       cgd 	}
    949       1.80     lukem 	(void)xsignal(SIGQUIT, psummary);
    950       1.81     lukem 	oldintr = xsignal(SIGINT, abortxfer);
    951       1.27     lukem 	if (ignorespecial || (strcmp(local, "-") && *local != '|')) {
    952       1.28     lukem 		if (access(local, W_OK) < 0) {
    953        1.6       cgd 			char *dir = strrchr(local, '/');
    954        1.1       cgd 
    955        1.1       cgd 			if (errno != ENOENT && errno != EACCES) {
    956        1.6       cgd 				warn("local: %s", local);
    957       1.79     lukem 				code = -1;
    958       1.78     lukem 				goto cleanuprecv;
    959        1.1       cgd 			}
    960        1.1       cgd 			if (dir != NULL)
    961        1.1       cgd 				*dir = 0;
    962       1.46     lukem 			d = access(dir == local ? "/" :
    963       1.46     lukem 			    dir ? local : ".", W_OK);
    964        1.1       cgd 			if (dir != NULL)
    965        1.1       cgd 				*dir = '/';
    966        1.1       cgd 			if (d < 0) {
    967        1.6       cgd 				warn("local: %s", local);
    968       1.79     lukem 				code = -1;
    969       1.78     lukem 				goto cleanuprecv;
    970        1.1       cgd 			}
    971        1.1       cgd 			if (!runique && errno == EACCES &&
    972       1.35     lukem 			    chmod(local, (S_IRUSR|S_IWUSR)) < 0) {
    973        1.6       cgd 				warn("local: %s", local);
    974       1.79     lukem 				code = -1;
    975       1.78     lukem 				goto cleanuprecv;
    976        1.1       cgd 			}
    977        1.1       cgd 			if (runique && errno == EACCES &&
    978        1.1       cgd 			   (local = gunique(local)) == NULL) {
    979       1.79     lukem 				code = -1;
    980       1.78     lukem 				goto cleanuprecv;
    981        1.1       cgd 			}
    982        1.1       cgd 		}
    983        1.1       cgd 		else if (runique && (local = gunique(local)) == NULL) {
    984       1.79     lukem 			code = -1;
    985       1.78     lukem 			goto cleanuprecv;
    986        1.1       cgd 		}
    987        1.1       cgd 	}
    988        1.1       cgd 	if (!is_retr) {
    989        1.1       cgd 		if (curtype != TYPE_A)
    990        1.1       cgd 			changetype(TYPE_A, 0);
    991       1.16     lukem 	} else {
    992       1.16     lukem 		if (curtype != type)
    993       1.16     lukem 			changetype(type, 0);
    994       1.19     lukem 		filesize = remotesize(remote, 0);
    995       1.85     lukem 		if (code == 421 || code == -1)
    996       1.85     lukem 			goto cleanuprecv;
    997       1.16     lukem 	}
    998        1.1       cgd 	if (initconn()) {
    999       1.79     lukem 		code = -1;
   1000       1.78     lukem 		goto cleanuprecv;
   1001        1.1       cgd 	}
   1002       1.81     lukem 	if (sigsetjmp(xferabort, 1))
   1003        1.1       cgd 		goto abort;
   1004        1.1       cgd 	if (is_retr && restart_point &&
   1005      1.107     lukem 	    command("REST " LLF, (LLT) restart_point) != CONTINUE)
   1006       1.78     lukem 		goto cleanuprecv;
   1007       1.88     lukem 	if (! EMPTYSTRING(remote)) {
   1008       1.85     lukem 		if (command("%s %s", cmd, remote) != PRELIM)
   1009       1.78     lukem 			goto cleanuprecv;
   1010        1.1       cgd 	} else {
   1011       1.85     lukem 		if (command("%s", cmd) != PRELIM)
   1012       1.78     lukem 			goto cleanuprecv;
   1013        1.1       cgd 	}
   1014        1.1       cgd 	din = dataconn("r");
   1015        1.1       cgd 	if (din == NULL)
   1016        1.1       cgd 		goto abort;
   1017       1.27     lukem 	if (!ignorespecial && strcmp(local, "-") == 0) {
   1018        1.1       cgd 		fout = stdout;
   1019       1.23     lukem 		progress = 0;
   1020       1.24     lukem 		preserve = 0;
   1021       1.27     lukem 	} else if (!ignorespecial && *local == '|') {
   1022       1.67     lukem 		oldintp = xsignal(SIGPIPE, SIG_IGN);
   1023        1.1       cgd 		fout = popen(local + 1, "w");
   1024        1.1       cgd 		if (fout == NULL) {
   1025        1.6       cgd 			warn("%s", local+1);
   1026        1.1       cgd 			goto abort;
   1027        1.1       cgd 		}
   1028       1.23     lukem 		progress = 0;
   1029       1.24     lukem 		preserve = 0;
   1030        1.1       cgd 		closefunc = pclose;
   1031        1.1       cgd 	} else {
   1032        1.1       cgd 		fout = fopen(local, lmode);
   1033        1.1       cgd 		if (fout == NULL) {
   1034        1.6       cgd 			warn("local: %s", local);
   1035        1.1       cgd 			goto abort;
   1036        1.1       cgd 		}
   1037        1.1       cgd 		closefunc = fclose;
   1038        1.1       cgd 	}
   1039       1.46     lukem 
   1040       1.64     lukem 	if (fstat(fileno(fout), &st) != -1 && !S_ISREG(st.st_mode)) {
   1041       1.64     lukem 		progress = 0;
   1042       1.64     lukem 		preserve = 0;
   1043       1.64     lukem 	}
   1044       1.64     lukem 	if (rcvbuf_size > bufsize) {
   1045        1.1       cgd 		if (buf)
   1046       1.23     lukem 			(void)free(buf);
   1047       1.64     lukem 		bufsize = rcvbuf_size;
   1048       1.37     lukem 		buf = xmalloc(bufsize);
   1049        1.1       cgd 	}
   1050       1.64     lukem 
   1051       1.16     lukem 	progressmeter(-1);
   1052       1.64     lukem 
   1053        1.1       cgd 	switch (curtype) {
   1054        1.1       cgd 
   1055        1.1       cgd 	case TYPE_I:
   1056        1.1       cgd 	case TYPE_L:
   1057       1.30     lukem 		if (is_retr && restart_point &&
   1058        1.6       cgd 		    lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
   1059        1.6       cgd 			warn("local: %s", local);
   1060       1.78     lukem 			goto cleanuprecv;
   1061        1.1       cgd 		}
   1062       1.89     lukem 		if (rate_get) {		/* rate limiting */
   1063       1.89     lukem 			while (1) {
   1064       1.89     lukem 				struct timeval then, now, td;
   1065       1.89     lukem 				off_t bufrem;
   1066       1.46     lukem 
   1067       1.62     lukem 				(void)gettimeofday(&then, NULL);
   1068       1.89     lukem 				errno = c = d = 0;
   1069       1.89     lukem 				for (bufrem = rate_get; bufrem > 0; ) {
   1070       1.89     lukem 					if ((c = read(fileno(din), buf,
   1071       1.89     lukem 					    MIN(bufsize, bufrem))) <= 0)
   1072       1.89     lukem 						goto recvdone;
   1073       1.89     lukem 					bytes += c;
   1074       1.89     lukem 					bufrem -=c;
   1075       1.89     lukem 					if ((d = write(fileno(fout), buf, c))
   1076       1.89     lukem 					    != c)
   1077       1.89     lukem 						goto recvdone;
   1078       1.89     lukem 					if (hash &&
   1079       1.89     lukem 					    (!progress || filesize < 0)) {
   1080       1.89     lukem 						while (bytes >= hashbytes) {
   1081       1.89     lukem 							(void)putc('#', ttyout);
   1082       1.89     lukem 							hashbytes += mark;
   1083       1.89     lukem 						}
   1084       1.89     lukem 						(void)fflush(ttyout);
   1085       1.89     lukem 					}
   1086       1.89     lukem 				}
   1087       1.89     lukem 					/* sleep until time is up */
   1088       1.89     lukem 				while (1) {
   1089       1.89     lukem 					(void)gettimeofday(&now, NULL);
   1090       1.89     lukem 					timersub(&now, &then, &td);
   1091       1.89     lukem 					if (td.tv_sec > 0)
   1092       1.89     lukem 						break;
   1093       1.89     lukem 					usleep(1000000 - td.tv_usec);
   1094       1.89     lukem 				}
   1095       1.89     lukem 			}
   1096       1.89     lukem 		} else {		/* faster code (no limiting) */
   1097       1.89     lukem 			while (1) {
   1098       1.89     lukem 				errno = c = d = 0;
   1099       1.89     lukem 				if ((c = read(fileno(din), buf, bufsize)) <= 0)
   1100       1.46     lukem 					goto recvdone;
   1101       1.46     lukem 				bytes += c;
   1102       1.46     lukem 				if ((d = write(fileno(fout), buf, c)) != c)
   1103       1.46     lukem 					goto recvdone;
   1104       1.46     lukem 				if (hash && (!progress || filesize < 0)) {
   1105       1.46     lukem 					while (bytes >= hashbytes) {
   1106       1.46     lukem 						(void)putc('#', ttyout);
   1107       1.46     lukem 						hashbytes += mark;
   1108       1.46     lukem 					}
   1109       1.46     lukem 					(void)fflush(ttyout);
   1110        1.1       cgd 				}
   1111        1.1       cgd 			}
   1112        1.1       cgd 		}
   1113       1.46     lukem  recvdone:
   1114       1.22     lukem 		if (hash && (!progress || filesize < 0) && bytes > 0) {
   1115       1.14     lukem 			if (bytes < mark)
   1116       1.35     lukem 				(void)putc('#', ttyout);
   1117       1.35     lukem 			(void)putc('\n', ttyout);
   1118        1.1       cgd 		}
   1119        1.1       cgd 		if (c < 0) {
   1120        1.1       cgd 			if (errno != EPIPE)
   1121        1.6       cgd 				warn("netin");
   1122        1.1       cgd 			bytes = -1;
   1123        1.1       cgd 		}
   1124        1.1       cgd 		if (d < c) {
   1125        1.1       cgd 			if (d < 0)
   1126        1.6       cgd 				warn("local: %s", local);
   1127        1.1       cgd 			else
   1128        1.6       cgd 				warnx("%s: short write", local);
   1129        1.1       cgd 		}
   1130        1.1       cgd 		break;
   1131        1.1       cgd 
   1132        1.1       cgd 	case TYPE_A:
   1133       1.30     lukem 		if (is_retr && restart_point) {
   1134       1.30     lukem 			int ch;
   1135      1.115     lukem 			off_t i;
   1136        1.1       cgd 
   1137      1.115     lukem 			if (fseeko(fout, (off_t)0, SEEK_SET) < 0)
   1138        1.1       cgd 				goto done;
   1139      1.115     lukem 			for (i = 0; i++ < restart_point;) {
   1140        1.1       cgd 				if ((ch = getc(fout)) == EOF)
   1141        1.1       cgd 					goto done;
   1142        1.1       cgd 				if (ch == '\n')
   1143        1.1       cgd 					i++;
   1144        1.1       cgd 			}
   1145      1.115     lukem 			if (fseeko(fout, (off_t)0, SEEK_CUR) < 0) {
   1146      1.102     lukem  done:
   1147        1.6       cgd 				warn("local: %s", local);
   1148       1.78     lukem 				goto cleanuprecv;
   1149        1.1       cgd 			}
   1150        1.1       cgd 		}
   1151        1.1       cgd 		while ((c = getc(din)) != EOF) {
   1152        1.1       cgd 			if (c == '\n')
   1153        1.1       cgd 				bare_lfs++;
   1154        1.1       cgd 			while (c == '\r') {
   1155       1.22     lukem 				while (hash && (!progress || filesize < 0) &&
   1156       1.16     lukem 				    (bytes >= hashbytes)) {
   1157       1.35     lukem 					(void)putc('#', ttyout);
   1158       1.35     lukem 					(void)fflush(ttyout);
   1159       1.14     lukem 					hashbytes += mark;
   1160        1.1       cgd 				}
   1161        1.1       cgd 				bytes++;
   1162        1.1       cgd 				if ((c = getc(din)) != '\n' || tcrflag) {
   1163        1.1       cgd 					if (ferror(fout))
   1164        1.1       cgd 						goto break2;
   1165       1.23     lukem 					(void)putc('\r', fout);
   1166        1.1       cgd 					if (c == '\0') {
   1167        1.1       cgd 						bytes++;
   1168        1.1       cgd 						goto contin2;
   1169        1.1       cgd 					}
   1170        1.1       cgd 					if (c == EOF)
   1171        1.1       cgd 						goto contin2;
   1172        1.1       cgd 				}
   1173        1.1       cgd 			}
   1174       1.23     lukem 			(void)putc(c, fout);
   1175        1.1       cgd 			bytes++;
   1176        1.1       cgd 	contin2:	;
   1177        1.1       cgd 		}
   1178      1.114     lukem  break2:
   1179       1.22     lukem 		if (hash && (!progress || filesize < 0)) {
   1180        1.1       cgd 			if (bytes < hashbytes)
   1181       1.35     lukem 				(void)putc('#', ttyout);
   1182       1.35     lukem 			(void)putc('\n', ttyout);
   1183        1.1       cgd 		}
   1184        1.1       cgd 		if (ferror(din)) {
   1185        1.1       cgd 			if (errno != EPIPE)
   1186        1.6       cgd 				warn("netin");
   1187        1.1       cgd 			bytes = -1;
   1188        1.1       cgd 		}
   1189        1.1       cgd 		if (ferror(fout))
   1190        1.6       cgd 			warn("local: %s", local);
   1191        1.1       cgd 		break;
   1192        1.1       cgd 	}
   1193       1.78     lukem 
   1194       1.22     lukem 	progressmeter(1);
   1195       1.78     lukem 	if (closefunc != NULL) {
   1196        1.1       cgd 		(*closefunc)(fout);
   1197       1.78     lukem 		fout = NULL;
   1198       1.78     lukem 	}
   1199       1.79     lukem 	(void)fclose(din);
   1200       1.79     lukem 	din = NULL;
   1201       1.23     lukem 	(void)getreply(0);
   1202       1.43     lukem 	if (bare_lfs) {
   1203       1.43     lukem 		fprintf(ttyout,
   1204       1.43     lukem 		    "WARNING! %d bare linefeeds received in ASCII mode.\n",
   1205       1.43     lukem 		    bare_lfs);
   1206       1.43     lukem 		fputs("File may not have transferred correctly.\n", ttyout);
   1207       1.43     lukem 	}
   1208       1.19     lukem 	if (bytes >= 0 && is_retr) {
   1209       1.19     lukem 		if (bytes > 0)
   1210       1.19     lukem 			ptransfer(0);
   1211       1.15     lukem 		if (preserve && (closefunc == fclose)) {
   1212       1.19     lukem 			mtime = remotemodtime(remote, 0);
   1213       1.15     lukem 			if (mtime != -1) {
   1214       1.31     lukem 				(void)gettimeofday(&tval[0], NULL);
   1215       1.16     lukem 				tval[1].tv_sec = mtime;
   1216       1.16     lukem 				tval[1].tv_usec = 0;
   1217       1.16     lukem 				if (utimes(local, tval) == -1) {
   1218       1.35     lukem 					fprintf(ttyout,
   1219       1.23     lukem 				"Can't change modification time on %s to %s",
   1220       1.23     lukem 					    local, asctime(localtime(&mtime)));
   1221       1.15     lukem 				}
   1222       1.15     lukem 			}
   1223       1.15     lukem 		}
   1224       1.15     lukem 	}
   1225       1.78     lukem 	goto cleanuprecv;
   1226       1.23     lukem 
   1227      1.102     lukem  abort:
   1228       1.78     lukem 			/*
   1229       1.78     lukem 			 * abort using RFC 959 recommended IP,SYNC sequence
   1230       1.78     lukem 			 */
   1231       1.82     lukem 	if (! sigsetjmp(xferabort, 1)) {
   1232       1.82     lukem 			/* this is the first call */
   1233       1.82     lukem 		(void)xsignal(SIGINT, abort_squared);
   1234       1.82     lukem 		if (!cpend) {
   1235       1.82     lukem 			code = -1;
   1236       1.82     lukem 			goto cleanuprecv;
   1237       1.82     lukem 		}
   1238       1.82     lukem 		abort_remote(din);
   1239       1.78     lukem 	}
   1240       1.80     lukem 	code = -1;
   1241       1.78     lukem 	if (bytes > 0)
   1242       1.78     lukem 		ptransfer(0);
   1243        1.1       cgd 
   1244      1.102     lukem  cleanuprecv:
   1245       1.81     lukem 	if (oldintr)
   1246       1.81     lukem 		(void)xsignal(SIGINT, oldintr);
   1247       1.81     lukem 	if (oldintp)
   1248       1.81     lukem 		(void)xsignal(SIGPIPE, oldintp);
   1249       1.80     lukem 	if (data >= 0) {
   1250       1.80     lukem 		(void)close(data);
   1251       1.80     lukem 		data = -1;
   1252       1.80     lukem 	}
   1253       1.78     lukem 	if (closefunc != NULL && fout != NULL)
   1254       1.78     lukem 		(*closefunc)(fout);
   1255       1.78     lukem 	if (din)
   1256       1.78     lukem 		(void)fclose(din);
   1257       1.78     lukem 	progress = oprogress;
   1258       1.78     lukem 	preserve = opreserve;
   1259       1.78     lukem 	bytes = 0;
   1260        1.1       cgd }
   1261        1.1       cgd 
   1262        1.1       cgd /*
   1263        1.1       cgd  * Need to start a listen on the data channel before we send the command,
   1264        1.1       cgd  * otherwise the server's connect may fail.
   1265        1.1       cgd  */
   1266        1.6       cgd int
   1267       1.95     lukem initconn(void)
   1268        1.1       cgd {
   1269        1.6       cgd 	char *p, *a;
   1270  1.126.2.6      tron 	int result, tmpno = 0;
   1271        1.1       cgd 	int on = 1;
   1272       1.48    itojun 	int error;
   1273  1.126.2.7      tron 	unsigned int addr[16], port[2];
   1274  1.126.2.7      tron 	unsigned int af, hal, pal;
   1275  1.126.2.6      tron 	socklen_t len;
   1276       1.48    itojun 	char *pasvcmd = NULL;
   1277        1.8       cgd 
   1278       1.62     lukem #ifdef INET6
   1279  1.126.2.9      tron #ifndef NO_DEBUG
   1280       1.93    itojun 	if (myctladdr.su_family == AF_INET6 && debug &&
   1281      1.104     lukem 	    (IN6_IS_ADDR_LINKLOCAL(&myctladdr.si_su.su_sin6.sin6_addr) ||
   1282      1.104     lukem 	     IN6_IS_ADDR_SITELOCAL(&myctladdr.si_su.su_sin6.sin6_addr))) {
   1283       1.59    itojun 		warnx("use of scoped address can be troublesome");
   1284       1.59    itojun 	}
   1285       1.62     lukem #endif
   1286  1.126.2.9      tron #endif
   1287      1.102     lukem  reinit:
   1288        1.8       cgd 	if (passivemode) {
   1289       1.48    itojun 		data_addr = myctladdr;
   1290       1.48    itojun 		data = socket(data_addr.su_family, SOCK_STREAM, 0);
   1291        1.8       cgd 		if (data < 0) {
   1292       1.19     lukem 			warn("socket");
   1293       1.21     lukem 			return (1);
   1294        1.8       cgd 		}
   1295        1.8       cgd 		if ((options & SO_DEBUG) &&
   1296  1.126.2.4       snj 		    setsockopt(data, SOL_SOCKET, SO_DEBUG,
   1297  1.126.2.4       snj 				(void *)&on, sizeof(on)) == -1) {
   1298  1.126.2.9      tron 			DWARN("setsockopt %s (ignored)", "SO_DEBUG");
   1299  1.126.2.4       snj 		}
   1300       1.54    itojun 		result = COMPLETE + 1;
   1301       1.48    itojun 		switch (data_addr.su_family) {
   1302       1.48    itojun 		case AF_INET:
   1303       1.74     lukem 			if (epsv4 && !epsv4bad) {
   1304  1.126.2.2      tron 				pasvcmd = "EPSV";
   1305      1.110        is 				result = command("EPSV");
   1306       1.85     lukem 				if (!connected)
   1307       1.85     lukem 					return (1);
   1308       1.55    itojun 				/*
   1309       1.55    itojun 				 * this code is to be friendly with broken
   1310       1.55    itojun 				 * BSDI ftpd
   1311       1.55    itojun 				 */
   1312       1.56    itojun 				if (code / 10 == 22 && code != 229) {
   1313       1.55    itojun 					fputs(
   1314       1.55    itojun "wrong server: return code must be 229\n",
   1315       1.55    itojun 						ttyout);
   1316       1.55    itojun 					result = COMPLETE + 1;
   1317       1.55    itojun 				}
   1318       1.74     lukem 				if (result != COMPLETE) {
   1319       1.74     lukem 					epsv4bad = 1;
   1320  1.126.2.9      tron 					DPRINTF("disabling epsv4 for this "
   1321  1.126.2.9      tron 					    "connection\n");
   1322       1.74     lukem 				}
   1323       1.55    itojun 			}
   1324       1.85     lukem 			if (result != COMPLETE) {
   1325  1.126.2.2      tron 				pasvcmd = "PASV";
   1326      1.110        is 				result = command("PASV");
   1327       1.85     lukem 				if (!connected)
   1328       1.85     lukem 					return (1);
   1329       1.85     lukem 			}
   1330       1.48    itojun 			break;
   1331       1.62     lukem #ifdef INET6
   1332       1.48    itojun 		case AF_INET6:
   1333  1.126.2.2      tron 			pasvcmd = "EPSV";
   1334      1.110        is 			result = command("EPSV");
   1335       1.85     lukem 			if (!connected)
   1336       1.85     lukem 				return (1);
   1337       1.55    itojun 			/* this code is to be friendly with broken BSDI ftpd */
   1338       1.56    itojun 			if (code / 10 == 22 && code != 229) {
   1339       1.55    itojun 				fputs(
   1340       1.55    itojun "wrong server: return code must be 229\n",
   1341       1.55    itojun 					ttyout);
   1342       1.55    itojun 				result = COMPLETE + 1;
   1343       1.55    itojun 			}
   1344      1.110        is 			if (result != COMPLETE) {
   1345      1.110        is 				pasvcmd = "LPSV";
   1346      1.110        is 				result = command("LPSV");
   1347      1.110        is 			}
   1348       1.85     lukem 			if (!connected)
   1349       1.85     lukem 				return (1);
   1350       1.48    itojun 			break;
   1351       1.62     lukem #endif
   1352       1.48    itojun 		default:
   1353       1.57    itojun 			result = COMPLETE + 1;
   1354       1.54    itojun 			break;
   1355       1.48    itojun 		}
   1356       1.48    itojun 		if (result != COMPLETE) {
   1357       1.35     lukem 			if (activefallback) {
   1358       1.35     lukem 				(void)close(data);
   1359       1.35     lukem 				data = -1;
   1360       1.35     lukem 				passivemode = 0;
   1361       1.85     lukem #if 0
   1362       1.35     lukem 				activefallback = 0;
   1363       1.85     lukem #endif
   1364       1.35     lukem 				goto reinit;
   1365       1.35     lukem 			}
   1366       1.35     lukem 			fputs("Passive mode refused.\n", ttyout);
   1367        1.8       cgd 			goto bad;
   1368        1.8       cgd 		}
   1369        1.8       cgd 
   1370       1.85     lukem #define	pack2(var, off) \
   1371       1.48    itojun 	(((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0))
   1372       1.85     lukem #define	pack4(var, off) \
   1373       1.48    itojun 	(((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \
   1374       1.48    itojun 	 ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0))
   1375      1.100     lukem #define	UC(b)	(((int)b)&0xff)
   1376       1.48    itojun 
   1377        1.8       cgd 		/*
   1378       1.48    itojun 		 * What we've got at this point is a string of comma separated
   1379       1.48    itojun 		 * one-byte unsigned integer values, separated by commas.
   1380        1.8       cgd 		 */
   1381       1.55    itojun 		if (strcmp(pasvcmd, "PASV") == 0) {
   1382       1.55    itojun 			if (data_addr.su_family != AF_INET) {
   1383       1.55    itojun 				fputs(
   1384      1.105     lukem     "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
   1385       1.55    itojun 				error = 1;
   1386       1.55    itojun 				goto bad;
   1387       1.55    itojun 			}
   1388       1.56    itojun 			if (code / 10 == 22 && code != 227) {
   1389       1.55    itojun 				fputs("wrong server: return code must be 227\n",
   1390       1.55    itojun 					ttyout);
   1391       1.55    itojun 				error = 1;
   1392       1.55    itojun 				goto bad;
   1393       1.55    itojun 			}
   1394       1.55    itojun 			error = sscanf(pasv, "%u,%u,%u,%u,%u,%u",
   1395       1.55    itojun 					&addr[0], &addr[1], &addr[2], &addr[3],
   1396       1.55    itojun 					&port[0], &port[1]);
   1397       1.55    itojun 			if (error != 6) {
   1398       1.55    itojun 				fputs(
   1399       1.55    itojun "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
   1400       1.55    itojun 				error = 1;
   1401       1.55    itojun 				goto bad;
   1402       1.55    itojun 			}
   1403       1.55    itojun 			error = 0;
   1404       1.55    itojun 			memset(&data_addr, 0, sizeof(data_addr));
   1405       1.55    itojun 			data_addr.su_family = AF_INET;
   1406       1.55    itojun 			data_addr.su_len = sizeof(struct sockaddr_in);
   1407      1.104     lukem 			data_addr.si_su.su_sin.sin_addr.s_addr =
   1408      1.104     lukem 			    htonl(pack4(addr, 0));
   1409       1.55    itojun 			data_addr.su_port = htons(pack2(port, 0));
   1410       1.55    itojun 		} else if (strcmp(pasvcmd, "LPSV") == 0) {
   1411       1.56    itojun 			if (code / 10 == 22 && code != 228) {
   1412       1.55    itojun 				fputs("wrong server: return code must be 228\n",
   1413       1.55    itojun 					ttyout);
   1414       1.55    itojun 				error = 1;
   1415       1.55    itojun 				goto bad;
   1416       1.55    itojun 			}
   1417       1.48    itojun 			switch (data_addr.su_family) {
   1418       1.48    itojun 			case AF_INET:
   1419       1.55    itojun 				error = sscanf(pasv,
   1420       1.55    itojun "%u,%u,%u,%u,%u,%u,%u,%u,%u",
   1421       1.57    itojun 					&af, &hal,
   1422       1.55    itojun 					&addr[0], &addr[1], &addr[2], &addr[3],
   1423       1.55    itojun 					&pal, &port[0], &port[1]);
   1424       1.57    itojun 				if (error != 9) {
   1425       1.55    itojun 					fputs(
   1426       1.57    itojun "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
   1427       1.55    itojun 					error = 1;
   1428       1.55    itojun 					goto bad;
   1429       1.55    itojun 				}
   1430       1.57    itojun 				if (af != 4 || hal != 4 || pal != 2) {
   1431       1.48    itojun 					fputs(
   1432       1.57    itojun "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
   1433       1.48    itojun 					error = 1;
   1434       1.48    itojun 					goto bad;
   1435       1.48    itojun 				}
   1436       1.57    itojun 
   1437       1.48    itojun 				error = 0;
   1438       1.48    itojun 				memset(&data_addr, 0, sizeof(data_addr));
   1439       1.48    itojun 				data_addr.su_family = AF_INET;
   1440       1.48    itojun 				data_addr.su_len = sizeof(struct sockaddr_in);
   1441      1.104     lukem 				data_addr.si_su.su_sin.sin_addr.s_addr =
   1442      1.104     lukem 				    htonl(pack4(addr, 0));
   1443       1.48    itojun 				data_addr.su_port = htons(pack2(port, 0));
   1444       1.48    itojun 				break;
   1445       1.62     lukem #ifdef INET6
   1446       1.48    itojun 			case AF_INET6:
   1447       1.48    itojun 				error = sscanf(pasv,
   1448       1.48    itojun "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
   1449       1.48    itojun 					&af, &hal,
   1450       1.48    itojun 					&addr[0], &addr[1], &addr[2], &addr[3],
   1451       1.48    itojun 					&addr[4], &addr[5], &addr[6], &addr[7],
   1452       1.48    itojun 					&addr[8], &addr[9], &addr[10],
   1453       1.48    itojun 					&addr[11], &addr[12], &addr[13],
   1454       1.48    itojun 					&addr[14], &addr[15],
   1455       1.48    itojun 					&pal, &port[0], &port[1]);
   1456       1.57    itojun 				if (error != 21) {
   1457       1.48    itojun 					fputs(
   1458       1.57    itojun "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
   1459       1.48    itojun 					error = 1;
   1460       1.48    itojun 					goto bad;
   1461       1.48    itojun 				}
   1462       1.57    itojun 				if (af != 6 || hal != 16 || pal != 2) {
   1463       1.48    itojun 					fputs(
   1464       1.57    itojun "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
   1465       1.48    itojun 					error = 1;
   1466       1.48    itojun 					goto bad;
   1467       1.48    itojun 				}
   1468        1.8       cgd 
   1469       1.48    itojun 				error = 0;
   1470       1.48    itojun 				memset(&data_addr, 0, sizeof(data_addr));
   1471       1.48    itojun 				data_addr.su_family = AF_INET6;
   1472       1.48    itojun 				data_addr.su_len = sizeof(struct sockaddr_in6);
   1473       1.60    itojun 			    {
   1474      1.100     lukem 				int i;
   1475      1.100     lukem 				for (i = 0; i < sizeof(struct in6_addr); i++) {
   1476      1.104     lukem 					data_addr.si_su.su_sin6.sin6_addr.s6_addr[i] =
   1477      1.100     lukem 					    UC(addr[i]);
   1478      1.100     lukem 				}
   1479       1.60    itojun 			    }
   1480       1.48    itojun 				data_addr.su_port = htons(pack2(port, 0));
   1481       1.48    itojun 				break;
   1482       1.62     lukem #endif
   1483       1.48    itojun 			default:
   1484       1.48    itojun 				error = 1;
   1485       1.48    itojun 			}
   1486       1.48    itojun 		} else if (strcmp(pasvcmd, "EPSV") == 0) {
   1487       1.48    itojun 			char delim[4];
   1488       1.48    itojun 
   1489       1.48    itojun 			port[0] = 0;
   1490       1.56    itojun 			if (code / 10 == 22 && code != 229) {
   1491       1.55    itojun 				fputs("wrong server: return code must be 229\n",
   1492       1.55    itojun 					ttyout);
   1493       1.55    itojun 				error = 1;
   1494       1.55    itojun 				goto bad;
   1495       1.55    itojun 			}
   1496       1.48    itojun 			if (sscanf(pasv, "%c%c%c%d%c", &delim[0],
   1497       1.48    itojun 					&delim[1], &delim[2], &port[1],
   1498       1.48    itojun 					&delim[3]) != 5) {
   1499       1.57    itojun 				fputs("parse error!\n", ttyout);
   1500       1.57    itojun 				error = 1;
   1501       1.48    itojun 				goto bad;
   1502       1.48    itojun 			}
   1503       1.48    itojun 			if (delim[0] != delim[1] || delim[0] != delim[2]
   1504       1.48    itojun 			 || delim[0] != delim[3]) {
   1505       1.57    itojun 				fputs("parse error!\n", ttyout);
   1506       1.57    itojun 				error = 1;
   1507       1.48    itojun 				goto bad;
   1508       1.48    itojun 			}
   1509       1.48    itojun 			data_addr = hisctladdr;
   1510       1.48    itojun 			data_addr.su_port = htons(port[1]);
   1511       1.48    itojun 		} else
   1512        1.8       cgd 			goto bad;
   1513        1.8       cgd 
   1514      1.104     lukem 		while (xconnect(data, (struct sockaddr *)&data_addr.si_su,
   1515       1.48    itojun 			    data_addr.su_len) < 0) {
   1516       1.39     lukem 			if (activefallback) {
   1517       1.39     lukem 				(void)close(data);
   1518       1.39     lukem 				data = -1;
   1519       1.39     lukem 				passivemode = 0;
   1520       1.85     lukem #if 0
   1521       1.39     lukem 				activefallback = 0;
   1522       1.85     lukem #endif
   1523       1.39     lukem 				goto reinit;
   1524       1.39     lukem 			}
   1525  1.126.2.5       snj 			warn("connect for data channel");
   1526        1.8       cgd 			goto bad;
   1527        1.8       cgd 		}
   1528       1.92     lukem #ifdef IPTOS_THROUGHPUT
   1529       1.48    itojun 		if (data_addr.su_family == AF_INET) {
   1530       1.48    itojun 			on = IPTOS_THROUGHPUT;
   1531  1.126.2.4       snj 			if (setsockopt(data, IPPROTO_IP, IP_TOS,
   1532  1.126.2.5       snj 					(void *)&on, sizeof(on)) == -1) {
   1533  1.126.2.9      tron 				DWARN("setsockopt %s (ignored)",
   1534  1.126.2.9      tron 				    "IPTOS_THROUGHPUT");
   1535  1.126.2.4       snj 			}
   1536       1.48    itojun 		}
   1537       1.92     lukem #endif
   1538       1.21     lukem 		return (0);
   1539        1.8       cgd 	}
   1540        1.1       cgd 
   1541      1.102     lukem  noport:
   1542        1.1       cgd 	data_addr = myctladdr;
   1543        1.1       cgd 	if (sendport)
   1544       1.48    itojun 		data_addr.su_port = 0;	/* let system pick one */
   1545        1.1       cgd 	if (data != -1)
   1546       1.23     lukem 		(void)close(data);
   1547       1.48    itojun 	data = socket(data_addr.su_family, SOCK_STREAM, 0);
   1548        1.1       cgd 	if (data < 0) {
   1549        1.6       cgd 		warn("socket");
   1550        1.1       cgd 		if (tmpno)
   1551        1.1       cgd 			sendport = 1;
   1552        1.1       cgd 		return (1);
   1553        1.1       cgd 	}
   1554        1.1       cgd 	if (!sendport)
   1555  1.126.2.4       snj 		if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR,
   1556  1.126.2.4       snj 				(void *)&on, sizeof(on)) == -1) {
   1557  1.126.2.4       snj 			warn("setsockopt %s", "SO_REUSEADDR");
   1558        1.1       cgd 			goto bad;
   1559        1.1       cgd 		}
   1560      1.104     lukem 	if (bind(data, (struct sockaddr *)&data_addr.si_su,
   1561      1.104     lukem 	    data_addr.su_len) < 0) {
   1562        1.6       cgd 		warn("bind");
   1563        1.1       cgd 		goto bad;
   1564        1.1       cgd 	}
   1565  1.126.2.4       snj 	if ((options & SO_DEBUG) &&
   1566  1.126.2.4       snj 	    setsockopt(data, SOL_SOCKET, SO_DEBUG,
   1567  1.126.2.4       snj 			(void *)&on, sizeof(on)) == -1) {
   1568  1.126.2.9      tron 		DWARN("setsockopt %s (ignored)", "SO_DEBUG");
   1569  1.126.2.4       snj 	}
   1570      1.105     lukem 	len = sizeof(data_addr.si_su);
   1571      1.105     lukem 	memset((char *)&data_addr, 0, sizeof (data_addr));
   1572  1.126.2.6      tron 	if (getsockname(data, (struct sockaddr *)&data_addr.si_su, &len) == -1) {
   1573        1.6       cgd 		warn("getsockname");
   1574        1.1       cgd 		goto bad;
   1575        1.1       cgd 	}
   1576      1.105     lukem 	data_addr.su_len = len;
   1577       1.36   thorpej 	if (xlisten(data, 1) < 0)
   1578        1.6       cgd 		warn("listen");
   1579       1.48    itojun 
   1580        1.1       cgd 	if (sendport) {
   1581      1.112    itojun 		char hname[NI_MAXHOST], sname[NI_MAXSERV];
   1582       1.48    itojun 		int af;
   1583      1.112    itojun 		struct sockinet tmp;
   1584       1.48    itojun 
   1585       1.48    itojun 		switch (data_addr.su_family) {
   1586       1.48    itojun 		case AF_INET:
   1587       1.74     lukem 			if (!epsv4 || epsv4bad) {
   1588       1.54    itojun 				result = COMPLETE + 1;
   1589       1.54    itojun 				break;
   1590       1.54    itojun 			}
   1591       1.54    itojun 			/* FALLTHROUGH */
   1592       1.58  christos #ifdef INET6
   1593       1.48    itojun 		case AF_INET6:
   1594      1.111    itojun #endif
   1595       1.48    itojun 			af = (data_addr.su_family == AF_INET) ? 1 : 2;
   1596      1.112    itojun 			tmp = data_addr;
   1597      1.113    itojun #ifdef INET6
   1598      1.112    itojun 			if (tmp.su_family == AF_INET6)
   1599      1.112    itojun 				tmp.si_su.su_sin6.sin6_scope_id = 0;
   1600      1.113    itojun #endif
   1601      1.112    itojun 			if (getnameinfo((struct sockaddr *)&tmp.si_su,
   1602      1.112    itojun 			    tmp.su_len, hname, sizeof(hname), sname,
   1603      1.112    itojun 			    sizeof(sname), NI_NUMERICHOST | NI_NUMERICSERV)) {
   1604       1.48    itojun 				result = ERROR;
   1605       1.48    itojun 			} else {
   1606      1.112    itojun 				result = command("EPRT |%d|%s|%s|", af, hname,
   1607      1.112    itojun 				    sname);
   1608       1.85     lukem 				if (!connected)
   1609       1.85     lukem 					return (1);
   1610       1.74     lukem 				if (result != COMPLETE) {
   1611       1.74     lukem 					epsv4bad = 1;
   1612  1.126.2.9      tron 					DPRINTF("disabling epsv4 for this "
   1613  1.126.2.9      tron 					    "connection\n");
   1614       1.74     lukem 				}
   1615       1.48    itojun 			}
   1616       1.48    itojun 			break;
   1617       1.48    itojun 		default:
   1618       1.48    itojun 			result = COMPLETE + 1;
   1619       1.48    itojun 			break;
   1620       1.48    itojun 		}
   1621       1.48    itojun 		if (result == COMPLETE)
   1622       1.48    itojun 			goto skip_port;
   1623       1.48    itojun 
   1624       1.48    itojun 		switch (data_addr.su_family) {
   1625       1.48    itojun 		case AF_INET:
   1626      1.104     lukem 			a = (char *)&data_addr.si_su.su_sin.sin_addr;
   1627       1.48    itojun 			p = (char *)&data_addr.su_port;
   1628       1.48    itojun 			result = command("PORT %d,%d,%d,%d,%d,%d",
   1629       1.48    itojun 				 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
   1630       1.48    itojun 				 UC(p[0]), UC(p[1]));
   1631       1.48    itojun 			break;
   1632       1.58  christos #ifdef INET6
   1633       1.48    itojun 		case AF_INET6:
   1634      1.104     lukem 			a = (char *)&data_addr.si_su.su_sin6.sin6_addr;
   1635       1.48    itojun 			p = (char *)&data_addr.su_port;
   1636       1.48    itojun 			result = command(
   1637       1.85     lukem 	"LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
   1638       1.48    itojun 				 6, 16,
   1639       1.48    itojun 				 UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),
   1640       1.48    itojun 				 UC(a[4]),UC(a[5]),UC(a[6]),UC(a[7]),
   1641       1.48    itojun 				 UC(a[8]),UC(a[9]),UC(a[10]),UC(a[11]),
   1642       1.48    itojun 				 UC(a[12]),UC(a[13]),UC(a[14]),UC(a[15]),
   1643       1.48    itojun 				 2, UC(p[0]), UC(p[1]));
   1644       1.48    itojun 			break;
   1645       1.58  christos #endif
   1646       1.48    itojun 		default:
   1647       1.48    itojun 			result = COMPLETE + 1; /* xxx */
   1648       1.48    itojun 		}
   1649       1.85     lukem 		if (!connected)
   1650       1.85     lukem 			return (1);
   1651       1.48    itojun 	skip_port:
   1652  1.126.2.2      tron 
   1653        1.1       cgd 		if (result == ERROR && sendport == -1) {
   1654        1.1       cgd 			sendport = 0;
   1655        1.1       cgd 			tmpno = 1;
   1656        1.1       cgd 			goto noport;
   1657        1.1       cgd 		}
   1658        1.1       cgd 		return (result != COMPLETE);
   1659        1.1       cgd 	}
   1660        1.1       cgd 	if (tmpno)
   1661        1.1       cgd 		sendport = 1;
   1662       1.92     lukem #ifdef IPTOS_THROUGHPUT
   1663       1.48    itojun 	if (data_addr.su_family == AF_INET) {
   1664       1.48    itojun 		on = IPTOS_THROUGHPUT;
   1665  1.126.2.4       snj 		if (setsockopt(data, IPPROTO_IP, IP_TOS,
   1666  1.126.2.5       snj 				(void *)&on, sizeof(on)) == -1)
   1667  1.126.2.9      tron 			DWARN("setsockopt %s (ignored)", "IPTOS_THROUGHPUT");
   1668       1.48    itojun 	}
   1669       1.92     lukem #endif
   1670        1.1       cgd 	return (0);
   1671      1.102     lukem  bad:
   1672  1.126.2.1      tron 	(void)close(data);
   1673  1.126.2.1      tron 	data = -1;
   1674        1.1       cgd 	if (tmpno)
   1675        1.1       cgd 		sendport = 1;
   1676        1.1       cgd 	return (1);
   1677        1.1       cgd }
   1678        1.1       cgd 
   1679        1.1       cgd FILE *
   1680       1.95     lukem dataconn(const char *lmode)
   1681        1.1       cgd {
   1682  1.126.2.1      tron 	struct sockinet	from;
   1683  1.126.2.6      tron 	int		s, flags, rv, timeout;
   1684  1.126.2.1      tron 	struct timeval	endtime, now, td;
   1685  1.126.2.1      tron 	struct pollfd	pfd[1];
   1686  1.126.2.6      tron 	socklen_t	fromlen;
   1687        1.8       cgd 
   1688  1.126.2.1      tron 	if (passivemode)	/* passive data connection */
   1689        1.8       cgd 		return (fdopen(data, lmode));
   1690        1.1       cgd 
   1691  1.126.2.1      tron 				/* active mode data connection */
   1692  1.126.2.1      tron 
   1693  1.126.2.1      tron 	if ((flags = fcntl(data, F_GETFL, 0)) == -1)
   1694  1.126.2.1      tron 		goto dataconn_failed;		/* get current socket flags  */
   1695  1.126.2.1      tron 	if (fcntl(data, F_SETFL, flags | O_NONBLOCK) == -1)
   1696  1.126.2.1      tron 		goto dataconn_failed;		/* set non-blocking connect */
   1697  1.126.2.1      tron 
   1698  1.126.2.1      tron 		/* NOTE: we now must restore socket flags on successful exit */
   1699  1.126.2.1      tron 
   1700  1.126.2.1      tron 				/* limit time waiting on listening socket */
   1701  1.126.2.1      tron 	pfd[0].fd = data;
   1702  1.126.2.1      tron 	pfd[0].events = POLLIN;
   1703  1.126.2.1      tron 	(void)gettimeofday(&endtime, NULL);	/* determine end time */
   1704  1.126.2.1      tron 	endtime.tv_sec += (quit_time > 0) ? quit_time: 60;
   1705  1.126.2.1      tron 						/* without -q, default to 60s */
   1706  1.126.2.1      tron 	do {
   1707  1.126.2.1      tron 		(void)gettimeofday(&now, NULL);
   1708  1.126.2.1      tron 		timersub(&endtime, &now, &td);
   1709  1.126.2.1      tron 		timeout = td.tv_sec * 1000 + td.tv_usec/1000;
   1710  1.126.2.1      tron 		if (timeout < 0)
   1711  1.126.2.1      tron 			timeout = 0;
   1712  1.126.2.1      tron 		rv = xpoll(pfd, 1, timeout);
   1713  1.126.2.1      tron 	} while (rv == -1 && errno == EINTR);	/* loop until poll ! EINTR */
   1714  1.126.2.1      tron 	if (rv == -1) {
   1715  1.126.2.1      tron 		warn("poll waiting before accept");
   1716  1.126.2.1      tron 		goto dataconn_failed;
   1717  1.126.2.1      tron 	}
   1718  1.126.2.1      tron 	if (rv == 0) {
   1719  1.126.2.3      tron 		warn("poll timeout waiting before accept");
   1720  1.126.2.1      tron 		goto dataconn_failed;
   1721  1.126.2.1      tron 	}
   1722  1.126.2.1      tron 
   1723  1.126.2.1      tron 				/* (non-blocking) accept the connection */
   1724  1.126.2.1      tron 	fromlen = myctladdr.su_len;
   1725  1.126.2.1      tron 	do {
   1726  1.126.2.1      tron 		s = accept(data, (struct sockaddr *) &from.si_su, &fromlen);
   1727  1.126.2.1      tron 	} while (s == -1 && errno == EINTR);	/* loop until accept ! EINTR */
   1728  1.126.2.1      tron 	if (s == -1) {
   1729        1.6       cgd 		warn("accept");
   1730  1.126.2.1      tron 		goto dataconn_failed;
   1731        1.1       cgd 	}
   1732  1.126.2.1      tron 
   1733       1.23     lukem 	(void)close(data);
   1734        1.1       cgd 	data = s;
   1735  1.126.2.1      tron 	if (fcntl(data, F_SETFL, flags) == -1)	/* restore socket flags */
   1736  1.126.2.1      tron 		goto dataconn_failed;
   1737  1.126.2.1      tron 
   1738       1.92     lukem #ifdef IPTOS_THROUGHPUT
   1739       1.48    itojun 	if (from.su_family == AF_INET) {
   1740       1.48    itojun 		int tos = IPTOS_THROUGHPUT;
   1741  1.126.2.4       snj 		if (setsockopt(s, IPPROTO_IP, IP_TOS,
   1742  1.126.2.5       snj 				(void *)&tos, sizeof(tos)) == -1) {
   1743  1.126.2.9      tron 			DWARN("setsockopt %s (ignored)", "IPTOS_THROUGHPUT");
   1744       1.48    itojun 		}
   1745       1.48    itojun 	}
   1746       1.92     lukem #endif
   1747        1.1       cgd 	return (fdopen(data, lmode));
   1748  1.126.2.2      tron 
   1749  1.126.2.1      tron  dataconn_failed:
   1750  1.126.2.1      tron 	(void)close(data);
   1751  1.126.2.1      tron 	data = -1;
   1752  1.126.2.1      tron 	return (NULL);
   1753       1.14     lukem }
   1754       1.14     lukem 
   1755       1.14     lukem void
   1756       1.95     lukem psabort(int notused)
   1757        1.1       cgd {
   1758       1.81     lukem 	int oerrno = errno;
   1759        1.1       cgd 
   1760      1.126     lukem 	sigint_raised = 1;
   1761       1.22     lukem 	alarmtimer(0);
   1762        1.1       cgd 	abrtflag++;
   1763       1.81     lukem 	errno = oerrno;
   1764        1.1       cgd }
   1765        1.1       cgd 
   1766        1.6       cgd void
   1767       1.95     lukem pswitch(int flag)
   1768        1.1       cgd {
   1769       1.90     lukem 	sigfunc oldintr;
   1770        1.1       cgd 	static struct comvars {
   1771        1.1       cgd 		int connect;
   1772        1.1       cgd 		char name[MAXHOSTNAMELEN];
   1773      1.104     lukem 		struct sockinet mctl;
   1774      1.104     lukem 		struct sockinet hctl;
   1775        1.1       cgd 		FILE *in;
   1776        1.1       cgd 		FILE *out;
   1777        1.1       cgd 		int tpe;
   1778        1.1       cgd 		int curtpe;
   1779        1.1       cgd 		int cpnd;
   1780        1.1       cgd 		int sunqe;
   1781        1.1       cgd 		int runqe;
   1782        1.1       cgd 		int mcse;
   1783        1.1       cgd 		int ntflg;
   1784        1.1       cgd 		char nti[17];
   1785        1.1       cgd 		char nto[17];
   1786        1.1       cgd 		int mapflg;
   1787        1.1       cgd 		char mi[MAXPATHLEN];
   1788        1.1       cgd 		char mo[MAXPATHLEN];
   1789        1.1       cgd 	} proxstruct, tmpstruct;
   1790        1.1       cgd 	struct comvars *ip, *op;
   1791        1.1       cgd 
   1792        1.1       cgd 	abrtflag = 0;
   1793       1.67     lukem 	oldintr = xsignal(SIGINT, psabort);
   1794        1.1       cgd 	if (flag) {
   1795        1.1       cgd 		if (proxy)
   1796        1.1       cgd 			return;
   1797        1.1       cgd 		ip = &tmpstruct;
   1798        1.1       cgd 		op = &proxstruct;
   1799        1.1       cgd 		proxy++;
   1800        1.1       cgd 	} else {
   1801        1.1       cgd 		if (!proxy)
   1802        1.1       cgd 			return;
   1803        1.1       cgd 		ip = &proxstruct;
   1804        1.1       cgd 		op = &tmpstruct;
   1805        1.1       cgd 		proxy = 0;
   1806        1.1       cgd 	}
   1807        1.1       cgd 	ip->connect = connected;
   1808        1.1       cgd 	connected = op->connect;
   1809       1.68     lukem 	if (hostname)
   1810       1.68     lukem 		(void)strlcpy(ip->name, hostname, sizeof(ip->name));
   1811       1.68     lukem 	else
   1812       1.23     lukem 		ip->name[0] = '\0';
   1813        1.1       cgd 	hostname = op->name;
   1814        1.1       cgd 	ip->hctl = hisctladdr;
   1815        1.1       cgd 	hisctladdr = op->hctl;
   1816        1.1       cgd 	ip->mctl = myctladdr;
   1817        1.1       cgd 	myctladdr = op->mctl;
   1818        1.1       cgd 	ip->in = cin;
   1819        1.1       cgd 	cin = op->in;
   1820        1.1       cgd 	ip->out = cout;
   1821        1.1       cgd 	cout = op->out;
   1822        1.1       cgd 	ip->tpe = type;
   1823        1.1       cgd 	type = op->tpe;
   1824        1.1       cgd 	ip->curtpe = curtype;
   1825        1.1       cgd 	curtype = op->curtpe;
   1826        1.1       cgd 	ip->cpnd = cpend;
   1827        1.1       cgd 	cpend = op->cpnd;
   1828        1.1       cgd 	ip->sunqe = sunique;
   1829        1.1       cgd 	sunique = op->sunqe;
   1830        1.1       cgd 	ip->runqe = runique;
   1831        1.1       cgd 	runique = op->runqe;
   1832        1.1       cgd 	ip->mcse = mcase;
   1833        1.1       cgd 	mcase = op->mcse;
   1834        1.1       cgd 	ip->ntflg = ntflag;
   1835        1.1       cgd 	ntflag = op->ntflg;
   1836       1.68     lukem 	(void)strlcpy(ip->nti, ntin, sizeof(ip->nti));
   1837       1.68     lukem 	(void)strlcpy(ntin, op->nti, sizeof(ntin));
   1838       1.68     lukem 	(void)strlcpy(ip->nto, ntout, sizeof(ip->nto));
   1839       1.68     lukem 	(void)strlcpy(ntout, op->nto, sizeof(ntout));
   1840        1.1       cgd 	ip->mapflg = mapflag;
   1841        1.1       cgd 	mapflag = op->mapflg;
   1842       1.68     lukem 	(void)strlcpy(ip->mi, mapin, sizeof(ip->mi));
   1843       1.68     lukem 	(void)strlcpy(mapin, op->mi, sizeof(mapin));
   1844       1.68     lukem 	(void)strlcpy(ip->mo, mapout, sizeof(ip->mo));
   1845       1.68     lukem 	(void)strlcpy(mapout, op->mo, sizeof(mapout));
   1846       1.67     lukem 	(void)xsignal(SIGINT, oldintr);
   1847        1.1       cgd 	if (abrtflag) {
   1848        1.1       cgd 		abrtflag = 0;
   1849        1.1       cgd 		(*oldintr)(SIGINT);
   1850        1.1       cgd 	}
   1851        1.1       cgd }
   1852        1.1       cgd 
   1853        1.1       cgd void
   1854       1.95     lukem abortpt(int notused)
   1855        1.1       cgd {
   1856        1.6       cgd 
   1857      1.126     lukem 	sigint_raised = 1;
   1858       1.22     lukem 	alarmtimer(0);
   1859       1.84     lukem 	if (fromatty)
   1860       1.84     lukem 		write(fileno(ttyout), "\n", 1);
   1861        1.1       cgd 	ptabflg++;
   1862        1.1       cgd 	mflag = 0;
   1863        1.1       cgd 	abrtflag = 0;
   1864       1.81     lukem 	siglongjmp(ptabort, 1);
   1865        1.1       cgd }
   1866        1.1       cgd 
   1867        1.6       cgd void
   1868       1.95     lukem proxtrans(const char *cmd, const char *local, const char *remote)
   1869        1.1       cgd {
   1870       1.90     lukem 	sigfunc oldintr;
   1871       1.26     lukem 	int prox_type, nfnd;
   1872       1.26     lukem 	volatile int secndflag;
   1873        1.1       cgd 	char *cmd2;
   1874        1.1       cgd 
   1875       1.35     lukem #ifdef __GNUC__			/* to shut up gcc warnings */
   1876       1.26     lukem 	(void)&oldintr;
   1877       1.26     lukem 	(void)&cmd2;
   1878       1.26     lukem #endif
   1879       1.26     lukem 
   1880       1.26     lukem 	oldintr = NULL;
   1881       1.26     lukem 	secndflag = 0;
   1882        1.1       cgd 	if (strcmp(cmd, "RETR"))
   1883        1.1       cgd 		cmd2 = "RETR";
   1884        1.1       cgd 	else
   1885        1.1       cgd 		cmd2 = runique ? "STOU" : "STOR";
   1886        1.1       cgd 	if ((prox_type = type) == 0) {
   1887        1.1       cgd 		if (unix_server && unix_proxy)
   1888        1.1       cgd 			prox_type = TYPE_I;
   1889        1.1       cgd 		else
   1890        1.1       cgd 			prox_type = TYPE_A;
   1891        1.1       cgd 	}
   1892        1.1       cgd 	if (curtype != prox_type)
   1893        1.1       cgd 		changetype(prox_type, 1);
   1894        1.1       cgd 	if (command("PASV") != COMPLETE) {
   1895       1.35     lukem 		fputs("proxy server does not support third party transfers.\n",
   1896       1.35     lukem 		    ttyout);
   1897        1.1       cgd 		return;
   1898        1.1       cgd 	}
   1899        1.1       cgd 	pswitch(0);
   1900        1.1       cgd 	if (!connected) {
   1901       1.35     lukem 		fputs("No primary connection.\n", ttyout);
   1902        1.1       cgd 		pswitch(1);
   1903        1.1       cgd 		code = -1;
   1904        1.1       cgd 		return;
   1905        1.1       cgd 	}
   1906        1.1       cgd 	if (curtype != prox_type)
   1907        1.1       cgd 		changetype(prox_type, 1);
   1908        1.1       cgd 	if (command("PORT %s", pasv) != COMPLETE) {
   1909        1.1       cgd 		pswitch(1);
   1910        1.1       cgd 		return;
   1911        1.1       cgd 	}
   1912       1.81     lukem 	if (sigsetjmp(ptabort, 1))
   1913        1.1       cgd 		goto abort;
   1914       1.67     lukem 	oldintr = xsignal(SIGINT, abortpt);
   1915       1.41     lukem 	if ((restart_point &&
   1916      1.107     lukem 	    (command("REST " LLF, (LLT) restart_point) != CONTINUE))
   1917      1.103     lukem 	    || (command("%s %s", cmd, remote) != PRELIM)) {
   1918       1.67     lukem 		(void)xsignal(SIGINT, oldintr);
   1919        1.1       cgd 		pswitch(1);
   1920        1.1       cgd 		return;
   1921        1.1       cgd 	}
   1922        1.1       cgd 	sleep(2);
   1923        1.1       cgd 	pswitch(1);
   1924        1.1       cgd 	secndflag++;
   1925       1.41     lukem 	if ((restart_point &&
   1926      1.107     lukem 	    (command("REST " LLF, (LLT) restart_point) != CONTINUE))
   1927      1.103     lukem 	    || (command("%s %s", cmd2, local) != PRELIM))
   1928        1.1       cgd 		goto abort;
   1929        1.1       cgd 	ptflag++;
   1930       1.23     lukem 	(void)getreply(0);
   1931        1.1       cgd 	pswitch(0);
   1932       1.23     lukem 	(void)getreply(0);
   1933       1.67     lukem 	(void)xsignal(SIGINT, oldintr);
   1934        1.1       cgd 	pswitch(1);
   1935        1.1       cgd 	ptflag = 0;
   1936       1.35     lukem 	fprintf(ttyout, "local: %s remote: %s\n", local, remote);
   1937        1.1       cgd 	return;
   1938      1.102     lukem  abort:
   1939       1.82     lukem 	if (sigsetjmp(xferabort, 1)) {
   1940       1.82     lukem 		(void)xsignal(SIGINT, oldintr);
   1941       1.82     lukem 		return;
   1942       1.82     lukem 	}
   1943       1.82     lukem 	(void)xsignal(SIGINT, abort_squared);
   1944        1.1       cgd 	ptflag = 0;
   1945        1.1       cgd 	if (strcmp(cmd, "RETR") && !proxy)
   1946        1.1       cgd 		pswitch(1);
   1947        1.1       cgd 	else if (!strcmp(cmd, "RETR") && proxy)
   1948        1.1       cgd 		pswitch(0);
   1949        1.1       cgd 	if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
   1950        1.1       cgd 		if (command("%s %s", cmd2, local) != PRELIM) {
   1951        1.1       cgd 			pswitch(0);
   1952        1.1       cgd 			if (cpend)
   1953       1.31     lukem 				abort_remote(NULL);
   1954        1.1       cgd 		}
   1955        1.1       cgd 		pswitch(1);
   1956        1.1       cgd 		if (ptabflg)
   1957        1.1       cgd 			code = -1;
   1958       1.67     lukem 		(void)xsignal(SIGINT, oldintr);
   1959        1.1       cgd 		return;
   1960        1.1       cgd 	}
   1961        1.1       cgd 	if (cpend)
   1962       1.31     lukem 		abort_remote(NULL);
   1963        1.1       cgd 	pswitch(!proxy);
   1964        1.1       cgd 	if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
   1965        1.1       cgd 		if (command("%s %s", cmd2, local) != PRELIM) {
   1966        1.1       cgd 			pswitch(0);
   1967        1.1       cgd 			if (cpend)
   1968       1.31     lukem 				abort_remote(NULL);
   1969        1.1       cgd 			pswitch(1);
   1970        1.1       cgd 			if (ptabflg)
   1971        1.1       cgd 				code = -1;
   1972       1.67     lukem 			(void)xsignal(SIGINT, oldintr);
   1973        1.1       cgd 			return;
   1974        1.1       cgd 		}
   1975        1.1       cgd 	}
   1976        1.1       cgd 	if (cpend)
   1977       1.31     lukem 		abort_remote(NULL);
   1978        1.1       cgd 	pswitch(!proxy);
   1979        1.1       cgd 	if (cpend) {
   1980       1.45  christos 		if ((nfnd = empty(cin, NULL, 10)) <= 0) {
   1981       1.85     lukem 			if (nfnd < 0)
   1982        1.6       cgd 				warn("abort");
   1983        1.1       cgd 			if (ptabflg)
   1984        1.1       cgd 				code = -1;
   1985       1.85     lukem 			lostpeer(0);
   1986        1.1       cgd 		}
   1987       1.23     lukem 		(void)getreply(0);
   1988       1.23     lukem 		(void)getreply(0);
   1989        1.1       cgd 	}
   1990        1.1       cgd 	if (proxy)
   1991        1.1       cgd 		pswitch(0);
   1992        1.1       cgd 	pswitch(1);
   1993        1.1       cgd 	if (ptabflg)
   1994        1.1       cgd 		code = -1;
   1995       1.67     lukem 	(void)xsignal(SIGINT, oldintr);
   1996        1.1       cgd }
   1997        1.1       cgd 
   1998        1.6       cgd void
   1999       1.95     lukem reset(int argc, char *argv[])
   2000        1.1       cgd {
   2001        1.1       cgd 	int nfnd = 1;
   2002        1.1       cgd 
   2003       1.85     lukem 	if (argc == 0 && argv != NULL) {
   2004  1.126.2.9      tron 		UPRINTF("usage: %s\n", argv[0]);
   2005       1.85     lukem 		code = -1;
   2006       1.85     lukem 		return;
   2007       1.85     lukem 	}
   2008        1.1       cgd 	while (nfnd > 0) {
   2009       1.45  christos 		if ((nfnd = empty(cin, NULL, 0)) < 0) {
   2010        1.6       cgd 			warn("reset");
   2011        1.1       cgd 			code = -1;
   2012       1.85     lukem 			lostpeer(0);
   2013       1.85     lukem 		} else if (nfnd)
   2014       1.23     lukem 			(void)getreply(0);
   2015        1.1       cgd 	}
   2016        1.1       cgd }
   2017        1.1       cgd 
   2018        1.1       cgd char *
   2019       1.95     lukem gunique(const char *local)
   2020        1.1       cgd {
   2021        1.1       cgd 	static char new[MAXPATHLEN];
   2022        1.6       cgd 	char *cp = strrchr(local, '/');
   2023       1.68     lukem 	int d, count=0, len;
   2024        1.1       cgd 	char ext = '1';
   2025        1.1       cgd 
   2026        1.1       cgd 	if (cp)
   2027        1.1       cgd 		*cp = '\0';
   2028       1.28     lukem 	d = access(cp == local ? "/" : cp ? local : ".", W_OK);
   2029        1.1       cgd 	if (cp)
   2030        1.1       cgd 		*cp = '/';
   2031        1.1       cgd 	if (d < 0) {
   2032        1.6       cgd 		warn("local: %s", local);
   2033       1.31     lukem 		return (NULL);
   2034        1.1       cgd 	}
   2035       1.68     lukem 	len = strlcpy(new, local, sizeof(new));
   2036       1.68     lukem 	cp = &new[len];
   2037  1.126.2.2      tron 	*cp++ = '.';
   2038        1.1       cgd 	while (!d) {
   2039        1.1       cgd 		if (++count == 100) {
   2040       1.35     lukem 			fputs("runique: can't find unique file name.\n",
   2041       1.35     lukem 			    ttyout);
   2042       1.31     lukem 			return (NULL);
   2043        1.1       cgd 		}
   2044        1.1       cgd 		*cp++ = ext;
   2045        1.1       cgd 		*cp = '\0';
   2046        1.1       cgd 		if (ext == '9')
   2047        1.1       cgd 			ext = '0';
   2048        1.1       cgd 		else
   2049        1.1       cgd 			ext++;
   2050       1.28     lukem 		if ((d = access(new, F_OK)) < 0)
   2051        1.1       cgd 			break;
   2052        1.1       cgd 		if (ext != '0')
   2053        1.1       cgd 			cp--;
   2054        1.1       cgd 		else if (*(cp - 2) == '.')
   2055        1.1       cgd 			*(cp - 1) = '1';
   2056        1.1       cgd 		else {
   2057        1.1       cgd 			*(cp - 2) = *(cp - 2) + 1;
   2058        1.1       cgd 			cp--;
   2059        1.1       cgd 		}
   2060        1.1       cgd 	}
   2061        1.6       cgd 	return (new);
   2062       1.82     lukem }
   2063       1.82     lukem 
   2064       1.82     lukem /*
   2065       1.82     lukem  * abort_squared --
   2066       1.82     lukem  *	aborts abort_remote(). lostpeer() is called because if the user is
   2067       1.82     lukem  *	too impatient to wait or there's another problem then ftp really
   2068       1.82     lukem  *	needs to get back to a known state.
   2069       1.82     lukem  */
   2070       1.82     lukem void
   2071       1.95     lukem abort_squared(int dummy)
   2072       1.82     lukem {
   2073       1.82     lukem 	char msgbuf[100];
   2074  1.126.2.6      tron 	size_t len;
   2075       1.82     lukem 
   2076      1.126     lukem 	sigint_raised = 1;
   2077       1.82     lukem 	alarmtimer(0);
   2078       1.82     lukem 	len = strlcpy(msgbuf, "\nremote abort aborted; closing connection.\n",
   2079       1.82     lukem 	    sizeof(msgbuf));
   2080       1.82     lukem 	write(fileno(ttyout), msgbuf, len);
   2081       1.85     lukem 	lostpeer(0);
   2082       1.82     lukem 	siglongjmp(xferabort, 1);
   2083        1.1       cgd }
   2084        1.1       cgd 
   2085        1.6       cgd void
   2086       1.95     lukem abort_remote(FILE *din)
   2087        1.1       cgd {
   2088       1.65     lukem 	char buf[BUFSIZ];
   2089        1.1       cgd 	int nfnd;
   2090        1.1       cgd 
   2091       1.23     lukem 	if (cout == NULL) {
   2092       1.23     lukem 		warnx("Lost control connection for abort.");
   2093       1.23     lukem 		if (ptabflg)
   2094       1.23     lukem 			code = -1;
   2095       1.85     lukem 		lostpeer(0);
   2096       1.23     lukem 		return;
   2097       1.23     lukem 	}
   2098        1.1       cgd 	/*
   2099        1.1       cgd 	 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
   2100        1.1       cgd 	 * after urgent byte rather than before as is protocol now
   2101        1.1       cgd 	 */
   2102       1.63     lukem 	buf[0] = IAC;
   2103       1.63     lukem 	buf[1] = IP;
   2104       1.63     lukem 	buf[2] = IAC;
   2105        1.1       cgd 	if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
   2106        1.6       cgd 		warn("abort");
   2107       1.15     lukem 	fprintf(cout, "%cABOR\r\n", DM);
   2108       1.23     lukem 	(void)fflush(cout);
   2109       1.45  christos 	if ((nfnd = empty(cin, din, 10)) <= 0) {
   2110       1.85     lukem 		if (nfnd < 0)
   2111        1.6       cgd 			warn("abort");
   2112        1.1       cgd 		if (ptabflg)
   2113        1.1       cgd 			code = -1;
   2114       1.85     lukem 		lostpeer(0);
   2115        1.1       cgd 	}
   2116       1.45  christos 	if (din && (nfnd & 2)) {
   2117        1.1       cgd 		while (read(fileno(din), buf, BUFSIZ) > 0)
   2118       1.45  christos 			continue;
   2119        1.1       cgd 	}
   2120        1.1       cgd 	if (getreply(0) == ERROR && code == 552) {
   2121        1.1       cgd 		/* 552 needed for nic style abort */
   2122       1.23     lukem 		(void)getreply(0);
   2123        1.1       cgd 	}
   2124       1.23     lukem 	(void)getreply(0);
   2125       1.96    itojun }
   2126       1.96    itojun 
   2127       1.96    itojun void
   2128       1.96    itojun ai_unmapped(struct addrinfo *ai)
   2129       1.96    itojun {
   2130       1.98     lukem #ifdef INET6
   2131       1.96    itojun 	struct sockaddr_in6 *sin6;
   2132       1.96    itojun 	struct sockaddr_in sin;
   2133  1.126.2.6      tron 	socklen_t len;
   2134       1.96    itojun 
   2135       1.96    itojun 	if (ai->ai_family != AF_INET6)
   2136       1.96    itojun 		return;
   2137       1.96    itojun 	if (ai->ai_addrlen != sizeof(struct sockaddr_in6) ||
   2138       1.96    itojun 	    sizeof(sin) > ai->ai_addrlen)
   2139       1.96    itojun 		return;
   2140       1.96    itojun 	sin6 = (struct sockaddr_in6 *)ai->ai_addr;
   2141       1.96    itojun 	if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
   2142       1.96    itojun 		return;
   2143       1.96    itojun 
   2144       1.96    itojun 	memset(&sin, 0, sizeof(sin));
   2145       1.96    itojun 	sin.sin_family = AF_INET;
   2146       1.99     lukem 	len = sizeof(struct sockaddr_in);
   2147       1.96    itojun 	memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
   2148       1.96    itojun 	    sizeof(sin.sin_addr));
   2149       1.96    itojun 	sin.sin_port = sin6->sin6_port;
   2150       1.96    itojun 
   2151       1.96    itojun 	ai->ai_family = AF_INET;
   2152      1.107     lukem #if HAVE_SOCKADDR_SA_LEN
   2153       1.99     lukem 	sin.sin_len = len;
   2154       1.99     lukem #endif
   2155       1.99     lukem 	memcpy(ai->ai_addr, &sin, len);
   2156       1.99     lukem 	ai->ai_addrlen = len;
   2157       1.98     lukem #endif
   2158        1.1       cgd }
   2159  1.126.2.9      tron 
   2160  1.126.2.9      tron #ifdef NO_USAGE
   2161  1.126.2.9      tron void
   2162  1.126.2.9      tron xusage(void)
   2163  1.126.2.9      tron {
   2164  1.126.2.9      tron 	fputs("Usage error\n", ttyout);
   2165  1.126.2.9      tron }
   2166  1.126.2.9      tron #endif
   2167