+ } else if (pktin.type == SSH2_MSG_CHANNEL_OPEN) {
+ char *type;
+ int typelen;
+ char *error = NULL;
+ struct ssh_channel *c;
+ ssh2_pkt_getstring(&type, &typelen);
+ c = smalloc(sizeof(struct ssh_channel));
+
+ if (typelen == 3 && !memcmp(type, "x11", 3)) {
+ char *rh;
+ if (!ssh_X11_fwd_enabled)
+ error = "X11 forwarding is not enabled";
+ else if ( x11_init(&c->u.x11.s, cfg.x11_display, c, &rh) != NULL ) {
+ error = "Unable to open an X11 connection";
+ } else {
+ c->type = CHAN_X11;
+ }
+ } else {
+ error = "Unsupported channel type requested";
+ }
+
+ c->remoteid = ssh2_pkt_getuint32();
+ if (error) {
+ ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_FAILURE);
+ ssh2_pkt_adduint32(c->remoteid);
+ ssh2_pkt_adduint32(SSH2_OPEN_CONNECT_FAILED);
+ ssh2_pkt_addstring(error);
+ ssh2_pkt_addstring("en"); /* language tag */
+ ssh2_pkt_send();
+ sfree(c);
+ } else {
+ struct ssh_channel *d;
+ unsigned i;
+ enum234 e;
+
+ for (i=1, d = first234(ssh_channels, &e); d;
+ d = next234(&e)) {
+ if (d->localid > i)
+ break; /* found a free number */
+ i = d->localid + 1;
+ }
+ c->localid = i;
+ c->closes = 0;
+ c->v2.remwindow = ssh2_pkt_getuint32();
+ c->v2.remmaxpkt = ssh2_pkt_getuint32();
+ c->v2.outbuffer = NULL;
+ c->v2.outbuflen = c->v2.outbufsize = 0;
+ add234(ssh_channels, c);
+ ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
+ ssh2_pkt_adduint32(c->remoteid);
+ ssh2_pkt_adduint32(c->localid);
+ ssh2_pkt_adduint32(0x8000UL); /* our window size */
+ ssh2_pkt_adduint32(0x4000UL); /* our max pkt size */
+ ssh2_pkt_send();
+ }