if (!cmd) cmd = ssh->cfg.remote_cmd;
static void do_ssh1_connection(Ssh ssh, unsigned char *in, int inlen,			       struct Packet *pktin){    crBegin(ssh->do_ssh1_connection_crstate);    ssh->packet_dispatch[SSH1_SMSG_STDOUT_DATA] = 	ssh->packet_dispatch[SSH1_SMSG_STDERR_DATA] =	ssh1_smsg_stdout_stderr_data;    ssh->packet_dispatch[SSH1_MSG_CHANNEL_OPEN_CONFIRMATION] =	ssh1_msg_channel_open_confirmation;    ssh->packet_dispatch[SSH1_MSG_CHANNEL_OPEN_FAILURE] =	ssh1_msg_channel_open_failure;    ssh->packet_dispatch[SSH1_MSG_CHANNEL_CLOSE] =	ssh->packet_dispatch[SSH1_MSG_CHANNEL_CLOSE_CONFIRMATION] =	ssh1_msg_channel_close;    ssh->packet_dispatch[SSH1_MSG_CHANNEL_DATA] = ssh1_msg_channel_data;    ssh->packet_dispatch[SSH1_SMSG_EXIT_STATUS] = ssh1_smsg_exit_status;    if (ssh->cfg.agentfwd && agent_exists()) {	logevent("Requesting agent forwarding");	send_packet(ssh, SSH1_CMSG_AGENT_REQUEST_FORWARDING, PKT_END);	do {	    crReturnV;	} while (!pktin);	if (pktin->type != SSH1_SMSG_SUCCESS	    && pktin->type != SSH1_SMSG_FAILURE) {	    bombout(("Protocol confusion"));	    crStopV;	} else if (pktin->type == SSH1_SMSG_FAILURE) {	    logevent("Agent forwarding refused");	} else {	    logevent("Agent forwarding enabled");	    ssh->agentfwd_enabled = TRUE;	    ssh->packet_dispatch[SSH1_SMSG_AGENT_OPEN] = ssh1_smsg_agent_open;	}    }    if (ssh->cfg.x11_forward) {	char proto[20], data[64];	logevent("Requesting X11 forwarding");	ssh->x11auth = x11_invent_auth(proto, sizeof(proto),				       data, sizeof(data), ssh->cfg.x11_auth);        x11_get_real_auth(ssh->x11auth, ssh->cfg.x11_display);	if (ssh->v1_local_protoflags & SSH1_PROTOFLAG_SCREEN_NUMBER) {	    send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING,			PKT_STR, proto, PKT_STR, data,			PKT_INT, x11_get_screen_number(ssh->cfg.x11_display),			PKT_END);	} else {	    send_packet(ssh, SSH1_CMSG_X11_REQUEST_FORWARDING,			PKT_STR, proto, PKT_STR, data, PKT_END);	}	do {	    crReturnV;	} while (!pktin);	if (pktin->type != SSH1_SMSG_SUCCESS	    && pktin->type != SSH1_SMSG_FAILURE) {	    bombout(("Protocol confusion"));	    crStopV;	} else if (pktin->type == SSH1_SMSG_FAILURE) {	    logevent("X11 forwarding refused");	} else {	    logevent("X11 forwarding enabled");	    ssh->X11_fwd_enabled = TRUE;	    ssh->packet_dispatch[SSH1_SMSG_X11_OPEN] = ssh1_smsg_x11_open;	}    }    ssh_setup_portfwd(ssh, &ssh->cfg);    ssh->packet_dispatch[SSH1_MSG_PORT_OPEN] = ssh1_msg_port_open;    if (!ssh->cfg.nopty) {	/* Unpick the terminal-speed string. */	/* XXX perhaps we should allow no speeds to be sent. */	ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */	sscanf(ssh->cfg.termspeed, "%d,%d", &ssh->ospeed, &ssh->ispeed);	/* Send the pty request. */	send_packet(ssh, SSH1_CMSG_REQUEST_PTY,		    PKT_STR, ssh->cfg.termtype,		    PKT_INT, ssh->term_height,		    PKT_INT, ssh->term_width,		    PKT_INT, 0, PKT_INT, 0, /* width,height in pixels */		    PKT_CHAR, 192, PKT_INT, ssh->ispeed, /* TTY_OP_ISPEED */		    PKT_CHAR, 193, PKT_INT, ssh->ospeed, /* TTY_OP_OSPEED */		    PKT_CHAR, 0, PKT_END);	ssh->state = SSH_STATE_INTERMED;	do {	    crReturnV;	} while (!pktin);	if (pktin->type != SSH1_SMSG_SUCCESS	    && pktin->type != SSH1_SMSG_FAILURE) {	    bombout(("Protocol confusion"));	    crStopV;	} else if (pktin->type == SSH1_SMSG_FAILURE) {	    c_write_str(ssh, "Server refused to allocate pty\r\n");	    ssh->editing = ssh->echoing = 1;	}	logeventf(ssh, "Allocated pty (ospeed %dbps, ispeed %dbps)",		  ssh->ospeed, ssh->ispeed);    } else {	ssh->editing = ssh->echoing = 1;    }    if (ssh->cfg.compression) {	send_packet(ssh, SSH1_CMSG_REQUEST_COMPRESSION, PKT_INT, 6, PKT_END);	do {	    crReturnV;	} while (!pktin);	if (pktin->type != SSH1_SMSG_SUCCESS	    && pktin->type != SSH1_SMSG_FAILURE) {	    bombout(("Protocol confusion"));	    crStopV;	} else if (pktin->type == SSH1_SMSG_FAILURE) {	    c_write_str(ssh, "Server refused to compress\r\n");	}	logevent("Started compression");	ssh->v1_compressing = TRUE;	ssh->cs_comp_ctx = zlib_compress_init();	logevent("Initialised zlib (RFC1950) compression");	ssh->sc_comp_ctx = zlib_decompress_init();	logevent("Initialised zlib (RFC1950) decompression");    }    /*     * Start the shell or command.     *      * Special case: if the first-choice command is an SSH2     * subsystem (hence not usable here) and the second choice     * exists, we fall straight back to that.     */    {	char *cmd = ssh->cfg.remote_cmd_ptr;		if (ssh->cfg.ssh_subsys && ssh->cfg.remote_cmd_ptr2) {	    cmd = ssh->cfg.remote_cmd_ptr2;	    ssh->fallback_cmd = TRUE;	}	if (*cmd)	    send_packet(ssh, SSH1_CMSG_EXEC_CMD, PKT_STR, cmd, PKT_END);	else	    send_packet(ssh, SSH1_CMSG_EXEC_SHELL, PKT_END);	logevent("Started session");    }    ssh->state = SSH_STATE_SESSION;    if (ssh->size_needed)	ssh_size(ssh, ssh->term_width, ssh->term_height);    if (ssh->eof_needed)	ssh_special(ssh, TS_EOF);    if (ssh->ldisc)	ldisc_send(ssh->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */    ssh->send_ok = 1;    ssh->channels = newtree234(ssh_channelcmp);    while (1) {	/*	 * By this point, most incoming packets are already being	 * handled by the dispatch table, and we need only pay	 * attention to the unusual ones.	 */	crReturnV;	if (pktin) {	    if (pktin->type == SSH1_SMSG_SUCCESS) {		/* may be from EXEC_SHELL on some servers */	    } else if (pktin->type == SSH1_SMSG_FAILURE) {		/* may be from EXEC_SHELL on some servers		 * if no pty is available or in other odd cases. Ignore */	    } else {		bombout(("Strange packet received: type %d", pktin->type));		crStopV;	    }	} else {	    while (inlen > 0) {		int len = min(inlen, 512);		send_packet(ssh, SSH1_CMSG_STDIN_DATA, PKTT_DATA,			    PKT_INT, len, PKT_DATA, in, len,			    PKTT_OTHER, PKT_END);		in += len;		inlen -= len;	    }	}    }    crFinishV;}