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