strlcpy(channels[ch].path, host, sizeof(channels[ch].path)); channels[ch].host_port = port_to_connect; channels[ch].listening_port = listen_port;
if (c == NULL) { error("channel_request_forwarding: channel_new failed"); close(sock); continue; } strlcpy(c->path, host, sizeof(c->path)); c->host_port = port_to_connect; c->listening_port = listen_port;
channel_request_forwarding(    const char *listen_address, u_short listen_port,    const char *host_to_connect, u_short port_to_connect,    int gateway_ports, int remote_fwd){	int success, ch, sock, on = 1, ctype;	struct addrinfo hints, *ai, *aitop;	char ntop[NI_MAXHOST], strport[NI_MAXSERV];	const char *host;	struct linger linger;	success = 0;	if (remote_fwd) {		host = listen_address;		ctype = SSH_CHANNEL_RPORT_LISTENER;	} else {		host = host_to_connect;		ctype  =SSH_CHANNEL_PORT_LISTENER;	}	if (strlen(host) > sizeof(channels[0].path) - 1) {		error("Forward host name too long.");		return success;	}	/* XXX listen_address is currently ignored */	/*	 * getaddrinfo returns a loopback address if the hostname is	 * set to NULL and hints.ai_flags is not AI_PASSIVE	 */	memset(&hints, 0, sizeof(hints));	hints.ai_family = IPv4or6;	hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;	hints.ai_socktype = SOCK_STREAM;	snprintf(strport, sizeof strport, "%d", listen_port);	if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)		packet_disconnect("getaddrinfo: fatal error");	for (ai = aitop; ai; ai = ai->ai_next) {		if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)			continue;		if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),		    strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {			error("channel_request_forwarding: getnameinfo failed");			continue;		}		/* Create a port to listen for the host. */		sock = socket(ai->ai_family, SOCK_STREAM, 0);		if (sock < 0) {			/* this is no error since kernel may not support ipv6 */			verbose("socket: %.100s", strerror(errno));			continue;		}		/*		 * Set socket options.  We would like the socket to disappear		 * as soon as it has been closed for whatever reason.		 */		setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));		linger.l_onoff = 1;		linger.l_linger = 5;		setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));		debug("Local forwarding listening on %s port %s.", ntop, strport);		/* Bind the socket to the address. */		if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {			/* address can be in use ipv6 address is already bound */			verbose("bind: %.100s", strerror(errno));			close(sock);			continue;		}		/* Start listening for connections on the socket. */		if (listen(sock, 5) < 0) {			error("listen: %.100s", strerror(errno));			close(sock);			continue;		}		/* Allocate a channel number for the socket. */		ch = channel_new("port listener", ctype, sock, sock, -1,		    CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,		    0, xstrdup("port listener"), 1);		strlcpy(channels[ch].path, host, sizeof(channels[ch].path));		channels[ch].host_port = port_to_connect;		channels[ch].listening_port = listen_port;		success = 1;	}	if (success == 0)		error("channel_request_forwarding: cannot listen to port: %d",		    listen_port);	freeaddrinfo(aitop);	return success;}