if (!pasvcmd) goto bad;
initconn(){	char *p, *a;	int result = ERROR, len, tmpno = 0;	int on = 1;	int error;	u_int addr[16], port[2];	u_int af, hal, pal;	char *pasvcmd = NULL;	if (myctladdr.su_family == AF_INET6	 && (IN6_IS_ADDR_LINKLOCAL(&myctladdr.su_sin6.sin6_addr)	  || IN6_IS_ADDR_SITELOCAL(&myctladdr.su_sin6.sin6_addr))) {		warnx("use of scoped address can be troublesome");	}reinit:	if (passivemode) {		data_addr = myctladdr;		data = socket(data_addr.su_family, SOCK_STREAM, 0);		if (data < 0) {			warn("socket");			return (1);		}		if ((options & SO_DEBUG) &&		    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,			       sizeof(on)) < 0)			warn("setsockopt (ignored)");		switch (data_addr.su_family) {		case AF_INET:			if (epsv4 && !epsv4bad) {				result = command(pasvcmd = "EPSV");				if (code / 10 == 22 && code != 229) {					fputs("wrong server: return code must be 229\n",						ttyout);					result = COMPLETE + 1;				}				if (result != COMPLETE) {					epsv4bad = 1;					if (debug) {						fputs("disabling epsv4 for this connection\n",						    ttyout);					}				}			}			if (result != COMPLETE)				result = command(pasvcmd = "PASV");			break;		case AF_INET6:			result = command(pasvcmd = "EPSV");			if (code / 10 == 22 && code != 229) {				fputs("wrong server: return code must be 229\n",					ttyout);				result = COMPLETE + 1;			}			if (result != COMPLETE)				result = command(pasvcmd = "LPSV");			break;		default:			result = COMPLETE + 1;			break;		}		if (result != COMPLETE) {			if (activefallback) {				(void)close(data);				data = -1;				passivemode = 0;				activefallback = 0;				goto reinit;			}			fputs("Passive mode refused.\n", ttyout);			goto bad;		}#define pack2(var, off) \	(((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0))#define pack4(var, off) \	(((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \	 ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0))		/*		 * What we've got at this point is a string of comma separated		 * one-byte unsigned integer values, separated by commas.		 */		if (strcmp(pasvcmd, "PASV") == 0) {			if (data_addr.su_family != AF_INET) {				fputs("Passive mode AF mismatch. Shouldn't happen!\n", ttyout);				error = 1;				goto bad;			}			if (code / 10 == 22 && code != 227) {				fputs("wrong server: return code must be 227\n",					ttyout);				error = 1;				goto bad;			}			error = sscanf(pasv, "%u,%u,%u,%u,%u,%u",					&addr[0], &addr[1], &addr[2], &addr[3],					&port[0], &port[1]);			if (error != 6) {				fputs("Passive mode address scan failure. Shouldn't happen!\n", ttyout);				error = 1;				goto bad;			}			error = 0;			memset(&data_addr, 0, sizeof(data_addr));			data_addr.su_family = AF_INET;			data_addr.su_len = sizeof(struct sockaddr_in);			data_addr.su_sin.sin_addr.s_addr =				htonl(pack4(addr, 0));			data_addr.su_port = htons(pack2(port, 0));		} else if (strcmp(pasvcmd, "LPSV") == 0) {			if (code / 10 == 22 && code != 228) {				fputs("wrong server: return code must be 228\n",					ttyout);				error = 1;				goto bad;			}			switch (data_addr.su_family) {			case AF_INET:				error = sscanf(pasv,"%u,%u,%u,%u,%u,%u,%u,%u,%u",					&af, &hal,					&addr[0], &addr[1], &addr[2], &addr[3],					&pal, &port[0], &port[1]);				if (error != 9) {					fputs("Passive mode address scan failure. Shouldn't happen!\n", ttyout);					error = 1;					goto bad;				}				if (af != 4 || hal != 4 || pal != 2) {					fputs("Passive mode AF mismatch. Shouldn't happen!\n", ttyout);					error = 1;					goto bad;				}				error = 0;				memset(&data_addr, 0, sizeof(data_addr));				data_addr.su_family = AF_INET;				data_addr.su_len = sizeof(struct sockaddr_in);				data_addr.su_sin.sin_addr.s_addr =					htonl(pack4(addr, 0));				data_addr.su_port = htons(pack2(port, 0));				break;			case AF_INET6:				error = sscanf(pasv,"%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",					&af, &hal,					&addr[0], &addr[1], &addr[2], &addr[3],					&addr[4], &addr[5], &addr[6], &addr[7],					&addr[8], &addr[9], &addr[10],					&addr[11], &addr[12], &addr[13],					&addr[14], &addr[15],					&pal, &port[0], &port[1]);				if (error != 21) {					fputs("Passive mode address scan failure. Shouldn't happen!\n", ttyout);					error = 1;					goto bad;				}				if (af != 6 || hal != 16 || pal != 2) {					fputs("Passive mode AF mismatch. Shouldn't happen!\n", ttyout);					error = 1;					goto bad;				}				error = 0;				memset(&data_addr, 0, sizeof(data_addr));				data_addr.su_family = AF_INET6;				data_addr.su_len = sizeof(struct sockaddr_in6);			    {				u_int32_t *p32;				p32 = (u_int32_t *)&data_addr.su_sin6.sin6_addr;				p32[0] = htonl(pack4(addr, 0));				p32[1] = htonl(pack4(addr, 4));				p32[2] = htonl(pack4(addr, 8));				p32[3] = htonl(pack4(addr, 12));			    }				data_addr.su_port = htons(pack2(port, 0));				break;			default:				error = 1;			}		} else if (strcmp(pasvcmd, "EPSV") == 0) {			char delim[4];			port[0] = 0;			if (code / 10 == 22 && code != 229) {				fputs("wrong server: return code must be 229\n",					ttyout);				error = 1;				goto bad;			}			if (sscanf(pasv, "%c%c%c%d%c", &delim[0],					&delim[1], &delim[2], &port[1],					&delim[3]) != 5) {				fputs("parse error!\n", ttyout);				error = 1;				goto bad;			}			if (delim[0] != delim[1] || delim[0] != delim[2]			 || delim[0] != delim[3]) {				fputs("parse error!\n", ttyout);				error = 1;				goto bad;			}			data_addr = hisctladdr;			data_addr.su_port = htons(port[1]);		} else			goto bad;		while (connect(data, (struct sockaddr *)&data_addr,			    data_addr.su_len) < 0) {			if (errno == EINTR)				continue;			if (activefallback) {				(void)close(data);				data = -1;				passivemode = 0;				activefallback = 0;				goto reinit;			}			warn("connect");			goto bad;		}#if defined(IPPROTO_IP) && defined(IP_TOS)		if (data_addr.su_family == AF_INET) {			on = IPTOS_THROUGHPUT;			if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,				       sizeof(int)) < 0)				warn("setsockopt TOS (ignored)");		}#endif		return (0);	}noport:	data_addr = myctladdr;	if (sendport)		data_addr.su_port = 0;	/* let system pick one */	if (data != -1)		(void)close(data);	data = socket(data_addr.su_family, SOCK_STREAM, 0);	if (data < 0) {		warn("socket");		if (tmpno)			sendport = 1;		return (1);	}	if (!sendport)		if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,				sizeof(on)) < 0) {			warn("setsockopt (reuse address)");			goto bad;		}	if (bind(data, (struct sockaddr *)&data_addr, data_addr.su_len) < 0) {		warn("bind");		goto bad;	}	if (options & SO_DEBUG &&	    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,			sizeof(on)) < 0)		warn("setsockopt (ignored)");	len = sizeof(data_addr);	if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {		warn("getsockname");		goto bad;	}	if (listen(data, 1) < 0)		warn("listen");#define	UC(b)	(((int)b)&0xff)	if (sendport) {		char hname[INET6_ADDRSTRLEN];		int af;		switch (data_addr.su_family) {		case AF_INET:			if (!epsv4 || epsv4bad) {				result = COMPLETE +1;				break;			}			/*FALLTHROUGH*/		case AF_INET6:			af = (data_addr.su_family == AF_INET) ? 1 : 2;			if (getnameinfo((struct sockaddr *)&data_addr,					data_addr.su_len, hname, sizeof(hname),					NULL, 0, NI_NUMERICHOST)) {				result = ERROR;			} else {				result = command("EPRT |%d|%s|%d|",					af, hname, ntohs(data_addr.su_port));				if (result != COMPLETE) {					epsv4bad = 1;					if (debug) {						fputs("disabling epsv4 for this connection\n",						    ttyout);					}				}			}			break;		default:			result = COMPLETE + 1;			break;		}		if (result == COMPLETE)			goto skip_port;		switch (data_addr.su_family) {		case AF_INET:			a = (char *)&data_addr.su_sin.sin_addr;			p = (char *)&data_addr.su_port;			result = command("PORT %d,%d,%d,%d,%d,%d",				 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),				 UC(p[0]), UC(p[1]));			break;		case AF_INET6:			a = (char *)&data_addr.su_sin6.sin6_addr;			p = (char *)&data_addr.su_port;			result = command("LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",				 6, 16,				 UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),				 UC(a[4]),UC(a[5]),UC(a[6]),UC(a[7]),				 UC(a[8]),UC(a[9]),UC(a[10]),UC(a[11]),				 UC(a[12]),UC(a[13]),UC(a[14]),UC(a[15]),				 2, UC(p[0]), UC(p[1]));			break;		default:			result = COMPLETE + 1; /* xxx */		}	skip_port:				if (result == ERROR && sendport == -1) {			sendport = 0;			tmpno = 1;			goto noport;		}		return (result != COMPLETE);	}	if (tmpno)		sendport = 1;#if defined(IPPROTO_IP) && defined(IP_TOS)	if (data_addr.su_family == AF_INET) {		on = IPTOS_THROUGHPUT;		if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,			       sizeof(int)) < 0)			warn("setsockopt TOS (ignored)");	}#endif	return (0);bad:	(void)close(data), data = -1;	if (tmpno)		sendport = 1;	return (1);}