ssh->close_expected = TRUE;
static void ssh2_msg_channel_request(Ssh ssh, struct Packet *pktin){    unsigned localid;    char *type;    int typelen, want_reply;    int reply = SSH2_MSG_CHANNEL_FAILURE; /* default */    struct ssh_channel *c;    struct Packet *pktout;    localid = ssh_pkt_getuint32(pktin);    ssh_pkt_getstring(pktin, &type, &typelen);    want_reply = ssh2_pkt_getbool(pktin);    /*     * First, check that the channel exists. Otherwise,     * we can instantly disconnect with a rude message.     */    c = find234(ssh->channels, &localid, ssh_channelfind);    if (!c) {	char buf[80];	sprintf(buf, "Received channel request for nonexistent"		" channel %d", localid);	logevent(buf);	pktout = ssh2_pkt_init(SSH2_MSG_DISCONNECT);	ssh2_pkt_adduint32(pktout, SSH2_DISCONNECT_BY_APPLICATION);	ssh2_pkt_addstring(pktout, buf);	ssh2_pkt_addstring(pktout, "en");	/* language tag */	ssh2_pkt_send_noqueue(ssh, pktout);	connection_fatal(ssh->frontend, "%s", buf);	ssh_closing((Plug)ssh, NULL, 0, 0);	return;    }    /*     * Having got the channel number, we now look at     * the request type string to see if it's something     * we recognise.     */    if (c == ssh->mainchan) {	/*	 * We recognise "exit-status" and "exit-signal" on	 * the primary channel.	 */	if (typelen == 11 &&	    !memcmp(type, "exit-status", 11)) {	    ssh->exitcode = ssh_pkt_getuint32(pktin);	    logeventf(ssh, "Server sent command exit status %d",		      ssh->exitcode);	    reply = SSH2_MSG_CHANNEL_SUCCESS;	} else if (typelen == 11 &&		   !memcmp(type, "exit-signal", 11)) {	    int is_plausible = TRUE, is_int = FALSE;	    char *fmt_sig = "", *fmt_msg = "";	    char *msg;	    int msglen = 0, core = FALSE;	    /* ICK: older versions of OpenSSH (e.g. 3.4p1)	     * provide an `int' for the signal, despite its	     * having been a `string' in the drafts since at	     * least 2001. (Fixed in session.c 1.147.) Try to	     * infer which we can safely parse it as. */	    {		unsigned char *p = pktin->body +		    pktin->savedpos;		long len = pktin->length - pktin->savedpos;		unsigned long num = GET_32BIT(p); /* what is it? */		/* If it's 0, it hardly matters; assume string */		if (num == 0) {		    is_int = FALSE;		} else {		    int maybe_int = FALSE, maybe_str = FALSE;#define CHECK_HYPOTHESIS(offset, result) \    do { \	long q = offset; \	if (q >= 0 && q+4 <= len) { \	    q = q + 4 + GET_32BIT(p+q); \	    if (q >= 0 && q+4 <= len && \		    (q = q + 4 + GET_32BIT(p+q)) && q == len) \		result = TRUE; \	} \    } while(0)		    CHECK_HYPOTHESIS(4+1, maybe_int);		    CHECK_HYPOTHESIS(4+num+1, maybe_str);#undef CHECK_HYPOTHESIS		    if (maybe_int && !maybe_str)			is_int = TRUE;		    else if (!maybe_int && maybe_str)			is_int = FALSE;		    else			/* Crikey. Either or neither. Panic. */			is_plausible = FALSE;		}	    }	    if (is_plausible) {		if (is_int) {		    /* Old non-standard OpenSSH. */		    int signum = ssh_pkt_getuint32(pktin);		    fmt_sig = dupprintf(" %d", signum);		} else {		    /* As per the drafts. */		    char *sig;		    int siglen;		    ssh_pkt_getstring(pktin, &sig, &siglen);		    /* Signal name isn't supposed to be blank, but		     * let's cope gracefully if it is. */		    if (siglen) {			fmt_sig = dupprintf(" \"%.*s\"",					    siglen, sig);		    }		}		core = ssh2_pkt_getbool(pktin);		ssh_pkt_getstring(pktin, &msg, &msglen);		if (msglen) {		    fmt_msg = dupprintf(" (\"%.*s\")", msglen, msg);		}		/* ignore lang tag */	    } /* else don't attempt to parse */	    logeventf(ssh, "Server exited on signal%s%s%s",		      fmt_sig, core ? " (core dumped)" : "",		      fmt_msg);	    if (*fmt_sig) sfree(fmt_sig);	    if (*fmt_msg) sfree(fmt_msg);	    reply = SSH2_MSG_CHANNEL_SUCCESS;	}    } else {	/*	 * This is a channel request we don't know	 * about, so we now either ignore the request	 * or respond with CHANNEL_FAILURE, depending	 * on want_reply.	 */	reply = SSH2_MSG_CHANNEL_FAILURE;    }    if (want_reply) {	pktout = ssh2_pkt_init(reply);	ssh2_pkt_adduint32(pktout, c->remoteid);	ssh2_pkt_send(ssh, pktout);    }}