padlen = block_size - (len % block_size); if (padlen < 4) padlen += block_size; if (extra_pad) {  extra_pad  = roundup(extra_pad, block_size); pad = extra_pad - ((len + padlen) % extra_pad); debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)", pad, len, padlen, extra_pad); padlen += pad; extra_pad = 0;
packet_send2_wrapped();   if (type == SSH2_MSG_NEWKEYS) { rekeying = 0; while ((p = TAILQ_FIRST(&outgoing))) { type = p->type; debug("dequeue packet: %u", type); buffer_free(&outgoing_packet); memcpy(&outgoing_packet, &p->payload, sizeof(Buffer)); TAILQ_REMOVE(&outgoing, p, next); xfree(p); packet_send2_wrapped(); }
packet_send2(void){	u_char type, *cp, *macbuf = NULL;	u_char padlen, pad;	u_int packet_length = 0;	u_int i, len;	u_int32_t rand = 0;	Enc *enc   = NULL;	Mac *mac   = NULL;	Comp *comp = NULL;	int block_size;	if (newkeys[MODE_OUT] != NULL) {		enc  = &newkeys[MODE_OUT]->enc;		mac  = &newkeys[MODE_OUT]->mac;		comp = &newkeys[MODE_OUT]->comp;	}	block_size = enc ? enc->block_size : 8;	cp = buffer_ptr(&outgoing_packet);	type = cp[5];#ifdef PACKET_DEBUG	fprintf(stderr, "plain:     ");	buffer_dump(&outgoing_packet);#endif	if (comp && comp->enabled) {		len = buffer_len(&outgoing_packet);		/* skip header, compress only payload */		buffer_consume(&outgoing_packet, 5);		buffer_clear(&compression_buffer);		buffer_compress(&outgoing_packet, &compression_buffer);		buffer_clear(&outgoing_packet);		buffer_append(&outgoing_packet, "\0\0\0\0\0", 5);		buffer_append(&outgoing_packet, buffer_ptr(&compression_buffer),		    buffer_len(&compression_buffer));		DBG(debug("compression: raw %d compressed %d", len,		    buffer_len(&outgoing_packet)));	}	/* sizeof (packet_len + pad_len + payload) */	len = buffer_len(&outgoing_packet);	/*	 * calc size of padding, alloc space, get random data,	 * minimum padding is 4 bytes	 */	padlen = block_size - (len % block_size);	if (padlen < 4)		padlen += block_size;	if (extra_pad) {		/* will wrap if extra_pad+padlen > 255 */		extra_pad  = roundup(extra_pad, block_size);		pad = extra_pad - ((len + padlen) % extra_pad);		debug3("packet_send2: adding %d (len %d padlen %d extra_pad %d)",		    pad, len, padlen, extra_pad);		padlen += pad;		extra_pad = 0;	}	cp = buffer_append_space(&outgoing_packet, padlen);	if (enc && !send_context.plaintext) {		/* random padding */		for (i = 0; i < padlen; i++) {			if (i % 4 == 0)				rand = arc4random();			cp[i] = rand & 0xff;			rand >>= 8;		}	} else {		/* clear padding */		memset(cp, 0, padlen);	}	/* packet_length includes payload, padding and padding length field */	packet_length = buffer_len(&outgoing_packet) - 4;	cp = buffer_ptr(&outgoing_packet);	PUT_32BIT(cp, packet_length);	cp[4] = padlen;	DBG(debug("send: len %d (includes padlen %d)", packet_length+4, padlen));	/* compute MAC over seqnr and packet(length fields, payload, padding) */	if (mac && mac->enabled) {		macbuf = mac_compute(mac, send_seqnr,		    buffer_ptr(&outgoing_packet),		    buffer_len(&outgoing_packet));		DBG(debug("done calc MAC out #%d", send_seqnr));	}	/* encrypt packet and append to output buffer. */	cp = buffer_append_space(&output, buffer_len(&outgoing_packet));	cipher_crypt(&send_context, cp, buffer_ptr(&outgoing_packet),	    buffer_len(&outgoing_packet));	/* append unencrypted MAC */	if (mac && mac->enabled)		buffer_append(&output, (char *)macbuf, mac->mac_len);#ifdef PACKET_DEBUG	fprintf(stderr, "encrypted: ");	buffer_dump(&output);#endif	/* increment sequence number for outgoing packets */	if (++send_seqnr == 0)		log("outgoing seqnr wraps around");	buffer_clear(&outgoing_packet);	if (type == SSH2_MSG_NEWKEYS)		set_newkeys(MODE_OUT);}