diff -u openssh-3.9p1/buffer.c openssh-3.9p1-hpn10/buffer.c
--- openssh-3.9p1/buffer.c	2003-11-21 06:56:47.000000000 -0600
+++ openssh-3.9p1-hpn10/buffer.c	2005-05-26 13:50:35.000000000 -0500
@@ -78,7 +78,7 @@
 	u_int newlen;
 	void *p;
 
-	if (len > 0x100000)
+	if (len > 0xa00000)
 		fatal("buffer_append_space: len %u not supported", len);
 
 	/* If the buffer is empty, start using it from the beginning. */
@@ -107,7 +107,7 @@
 	/* Increase the size of the buffer and retry. */
 
 	newlen = buffer->alloc + len + 32768;
-	if (newlen > 0xa00000)
+	if (newlen > MAXBUFSZ)
 		fatal("buffer_append_space: alloc %u not supported",
 		    newlen);
 	buffer->buf = xrealloc(buffer->buf, newlen);
diff -u openssh-3.9p1/buffer.h openssh-3.9p1-hpn10/buffer.h
--- openssh-3.9p1/buffer.h	2002-03-04 19:53:04.000000000 -0600
+++ openssh-3.9p1-hpn10/buffer.h	2005-05-26 13:50:35.000000000 -0500
@@ -16,6 +16,8 @@
 #ifndef BUFFER_H
 #define BUFFER_H
 
+#define MAXBUFSZ (2<<29)-1
+
 typedef struct {
 	u_char	*buf;		/* Buffer for data. */
 	u_int	 alloc;		/* Number of bytes allocated for data. */
diff -u openssh-3.9p1/channels.c openssh-3.9p1-hpn10/channels.c
--- openssh-3.9p1/channels.c	2004-08-13 06:18:01.000000000 -0500
+++ openssh-3.9p1-hpn10/channels.c	2005-05-26 13:50:35.000000000 -0500
@@ -257,6 +257,7 @@
 	c->local_window_max = window;
 	c->local_consumed = 0;
 	c->local_maxpacket = maxpack;
+	c->dynamic_window = 0;
 	c->remote_id = -1;
 	c->remote_name = xstrdup(remote_name);
 	c->remote_window = 0;
@@ -711,6 +712,9 @@
 channel_pre_open(Channel *c, fd_set * readset, fd_set * writeset)
 {
 	u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
+	
+       /* buffer at most 32 packets worth of input data */
+       limit = MIN(limit, MAXBUFSZ); 
 
 	if (c->istate == CHAN_INPUT_OPEN &&
 	    limit > 0 &&
@@ -1528,14 +1532,29 @@
 	    !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
 	    c->local_window < c->local_window_max/2 &&
 	    c->local_consumed > 0) {
+		u_int32_t tcpwinsz = 0;
+		socklen_t optsz = sizeof(tcpwinsz);
+		int ret = -1;
+		u_int32_t addition = 0;
+		if (c->dynamic_window) {
+			ret = getsockopt(packet_get_connection_in(), 
+				SOL_SOCKET, SO_RCVBUF, &tcpwinsz, &optsz);
+			if ((ret == 0) && tcpwinsz > MAXBUFSZ) 
+				tcpwinsz = MAXBUFSZ;
+		}
+		if (c->dynamic_window && (ret == 0) && 
+		    (tcpwinsz > c->local_window_max)) {
+			addition = tcpwinsz - c->local_window_max;
+			c->local_window_max += addition;
+		}
 		packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
 		packet_put_int(c->remote_id);
-		packet_put_int(c->local_consumed);
+		packet_put_int(c->local_consumed + addition);
 		packet_send();
 		debug2("channel %d: window %d sent adjust %d",
 		    c->self, c->local_window,
 		    c->local_consumed);
-		c->local_window += c->local_consumed;
+		c->local_window += c->local_consumed + addition;
 		c->local_consumed = 0;
 	}
 	return 1;
diff -u openssh-3.9p1/channels.h openssh-3.9p1-hpn10/channels.h
--- openssh-3.9p1/channels.h	2004-08-13 06:18:01.000000000 -0500
+++ openssh-3.9p1-hpn10/channels.h	2005-05-26 13:50:35.000000000 -0500
@@ -98,6 +98,7 @@
 	u_int	local_window_max;
 	u_int	local_consumed;
 	u_int	local_maxpacket;
+	int	dynamic_window;
 	int     extended_usage;
 	int	single_connection;
 
@@ -118,11 +119,11 @@
 
 /* default window/packet sizes for tcp/x11-fwd-channel */
 #define CHAN_SES_PACKET_DEFAULT	(32*1024)
-#define CHAN_SES_WINDOW_DEFAULT	(4*CHAN_SES_PACKET_DEFAULT)
+#define CHAN_SES_WINDOW_DEFAULT	(0xa00000/2)
 #define CHAN_TCP_PACKET_DEFAULT	(32*1024)
-#define CHAN_TCP_WINDOW_DEFAULT	(4*CHAN_TCP_PACKET_DEFAULT)
+#define CHAN_TCP_WINDOW_DEFAULT	(0xa00000/2)
 #define CHAN_X11_PACKET_DEFAULT	(16*1024)
-#define CHAN_X11_WINDOW_DEFAULT	(4*CHAN_X11_PACKET_DEFAULT)
+#define CHAN_X11_WINDOW_DEFAULT	(0xa00000/2)
 
 /* possible input states */
 #define CHAN_INPUT_OPEN			0
diff -u openssh-3.9p1/compat.c openssh-3.9p1-hpn10/compat.c
--- openssh-3.9p1/compat.c	2003-11-03 03:09:03.000000000 -0600
+++ openssh-3.9p1-hpn10/compat.c	2005-05-26 13:50:35.000000000 -0500
@@ -158,6 +158,14 @@
 		    strlen(check[i].pat), 0) == 1) {
 			debug("match: %s pat %s", version, check[i].pat);
 			datafellows = check[i].bugs;
+			/* Check to see if the remote side is OpenSSH and not HPN */
+			if(strstr(version,"OpenSSH") != NULL)
+			{
+				if (strstr(version,"hpn") == NULL)
+				{
+					datafellows |= SSH_BUG_LARGEWINDOW;
+				}
+			}
 			return;
 		}
 	}
diff -u openssh-3.9p1/compat.h openssh-3.9p1-hpn10/compat.h
--- openssh-3.9p1/compat.h	2004-07-17 01:12:08.000000000 -0500
+++ openssh-3.9p1-hpn10/compat.h	2005-05-26 13:50:35.000000000 -0500
@@ -55,6 +55,7 @@
 #define SSH_BUG_EXTEOF		0x00200000
 #define SSH_BUG_PROBE		0x00400000
 #define SSH_BUG_FIRSTKEX	0x00800000
+#define SSH_BUG_LARGEWINDOW     0x01000000
 
 void     enable_compat13(void);
 void     enable_compat20(void);
Common subdirectories: openssh-3.9p1/contrib and openssh-3.9p1-hpn10/contrib
Common subdirectories: openssh-3.9p1/openbsd-compat and openssh-3.9p1-hpn10/openbsd-compat
diff -u openssh-3.9p1/readconf.h openssh-3.9p1-hpn10/readconf.h
--- openssh-3.9p1/readconf.h	2004-07-17 01:12:08.000000000 -0500
+++ openssh-3.9p1-hpn10/readconf.h	2005-05-26 13:50:35.000000000 -0500
@@ -56,6 +56,7 @@
 	int     compression_level;	/* Compression level 1 (fast) to 9
 					 * (best). */
 	int     tcp_keep_alive;	/* Set SO_KEEPALIVE. */
+        int     tcp_rcv_buf; /* user switch to set tcp recv buffer */
 	LogLevel log_level;	/* Level for logging. */
 
 	int     port;		/* Port to connect. */
Common subdirectories: openssh-3.9p1/regress and openssh-3.9p1-hpn10/regress
Common subdirectories: openssh-3.9p1/scard and openssh-3.9p1-hpn10/scard
diff -u openssh-3.9p1/scp.c openssh-3.9p1-hpn10/scp.c
--- openssh-3.9p1/scp.c	2004-08-13 06:19:38.000000000 -0500
+++ openssh-3.9p1-hpn10/scp.c	2005-05-26 13:50:35.000000000 -0500
@@ -227,7 +227,7 @@
 	addargs(&args, "-oClearAllForwardings yes");
 
 	fflag = tflag = 0;
-	while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
+	while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q1246S:o:F:w:")) != -1)
 		switch (ch) {
 		/* User-visible flags. */
 		case '1':
@@ -288,6 +288,9 @@
 			setmode(0, O_BINARY);
 #endif
 			break;
+		case 'w':
+		  addargs(&args, "-w%s", optarg);
+		  break;
 		default:
 			usage();
 		}
@@ -501,7 +504,7 @@
 	BUF *bp;
 	off_t i, amt, result, statbytes;
 	int fd, haderr, indx;
-	char *last, *name, buf[2048];
+	char *last, *name, buf[16384];
 	int len;
 
 	for (indx = 0; indx < argc; ++indx) {
@@ -561,7 +564,11 @@
 		(void) atomicio(vwrite, remout, buf, strlen(buf));
 		if (response() < 0)
 			goto next;
-		if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
+		/* this change decreases the number of read/write syscalls*/
+		/* when scp acts as data source. this is the critical change*/
+		/* buf can actually remain at 2k but increasing both to 16k*/
+		/* seemed to make sense*/
+		if ((bp = allocbuf(&buffer, fd, sizeof(buf))) == NULL) {
 next:			(void) close(fd);
 			continue;
 		}
@@ -721,7 +728,7 @@
 	int amt, count, exists, first, mask, mode, ofd, omode;
 	off_t size, statbytes;
 	int setimes, targisdir, wrerrno = 0;
-	char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
+	char ch, *cp, *np, *targ, *why, *vect[1], buf[16384];
 	struct timeval tv[2];
 
 #define	atime	tv[0]
@@ -882,7 +889,7 @@
 			continue;
 		}
 		(void) atomicio(vwrite, remout, "", 1);
-		if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
+		if ((bp = allocbuf(&buffer, ofd, sizeof(buf))) == NULL) {
 			(void) close(ofd);
 			continue;
 		}
@@ -892,8 +899,8 @@
 		statbytes = 0;
 		if (showprogress)
 			start_progress_meter(curfile, size, &statbytes);
-		for (count = i = 0; i < size; i += 4096) {
-			amt = 4096;
+		for (count = i = 0; i < size; i += sizeof(buf)) {
+			amt = sizeof(buf);
 			if (i + amt > size)
 				amt = size - i;
 			count += amt;
@@ -910,7 +917,7 @@
 			} while (amt > 0);
 
 			if (limit_rate)
-				bwlimit(4096);
+				bwlimit(sizeof(buf));
 
 			if (count == bp->cnt) {
 				/* Keep reading so we stay sync'd up. */
@@ -1026,7 +1033,7 @@
 {
 	(void) fprintf(stderr,
 	    "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
-	    "           [-l limit] [-o ssh_option] [-P port] [-S program]\n"
+	    "           [-l limit] [-o ssh_option] [-P port] [-w buffer size] [-S program]\n"
 	    "           [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
 	exit(1);
 }
diff -u openssh-3.9p1/serverloop.c openssh-3.9p1-hpn10/serverloop.c
--- openssh-3.9p1/serverloop.c	2004-08-13 06:18:01.000000000 -0500
+++ openssh-3.9p1-hpn10/serverloop.c	2005-05-26 13:50:35.000000000 -0500
@@ -895,6 +895,8 @@
 	c = channel_new("session", SSH_CHANNEL_LARVAL,
 	    -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT,
 	    0, "server-session", 1);
+	if (!(datafellows & SSH_BUG_LARGEWINDOW))
+		c->dynamic_window = 1;
 	if (session_open(the_authctxt, c->self) != 1) {
 		debug("session open failed, free channel %d", c->self);
 		channel_free(c);
diff -u openssh-3.9p1/ssh.c openssh-3.9p1-hpn10/ssh.c
--- openssh-3.9p1/ssh.c	2004-08-15 02:23:34.000000000 -0500
+++ openssh-3.9p1-hpn10/ssh.c	2005-05-26 14:17:47.525451976 -0500
@@ -157,7 +157,7 @@
 "usage: ssh [-1246AaCfghkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]\n"
 "           [-D port] [-e escape_char] [-F configfile] [-i identity_file]\n"
 "           [-L port:host:hostport] [-l login_name] [-m mac_spec] [-o option]\n"
-"           [-p port] [-R port:host:hostport] [-S ctl] [user@]hostname [command]\n"
+"           [-p port] [-R port:host:hostport] [-S ctl] [-w receive buffer size] [user@]hostname [command]\n"
 	);
 	exit(1);
 }
@@ -234,9 +234,12 @@
 	/* Parse command-line arguments. */
 	host = NULL;
 
+	/* need to set options.tcp_rcv_buf to 0 */
+	options.tcp_rcv_buf = 0;
+
 again:
 	while ((opt = getopt(ac, av,
-	    "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:MNPR:S:TVXY")) != -1) {
+	    "1246ab:c:e:fgi:kl:m:no:p:qstvw:xACD:F:I:L:MNPR:S:TVXY")) != -1) {
 		switch (opt) {
 		case '1':
 			options.protocol = SSH_PROTO_1;
@@ -455,6 +458,9 @@
 		case 'F':
 			config = optarg;
 			break;
+		case 'w':
+		        options.tcp_rcv_buf = atoi(optarg);
+		        break;
 		default:
 			usage();
 		}
@@ -1134,6 +1140,7 @@
 	window = CHAN_SES_WINDOW_DEFAULT;
 	packetmax = CHAN_SES_PACKET_DEFAULT;
 	if (tty_flag) {
+		window = 4*CHAN_SES_PACKET_DEFAULT;
 		window >>= 1;
 		packetmax >>= 1;
 	}
@@ -1141,7 +1148,9 @@
 	    "session", SSH_CHANNEL_OPENING, in, out, err,
 	    window, packetmax, CHAN_EXTENDED_WRITE,
 	    "client-session", /*nonblock*/0);
-
+	if (!tty_flag && (!(datafellows & SSH_BUG_LARGEWINDOW))) {
+		c->dynamic_window = 1;
+	}
 	debug3("ssh_session2_open: channel_new: %d", c->self);
 
 	channel_send_open(c->self);
diff -u openssh-3.9p1/sshconnect.c openssh-3.9p1-hpn10/sshconnect.c
--- openssh-3.9p1/sshconnect.c	2004-06-21 21:56:02.000000000 -0500
+++ openssh-3.9p1-hpn10/sshconnect.c	2005-05-26 13:50:35.000000000 -0500
@@ -186,13 +186,58 @@
 			    strerror(errno));
 		else
 			debug("Allocated local port %d.", p);
+
+		
+		/* tuning needs to happen after the socket is */
+		/* created but before the connection happens */
+		/* so winscale is negotiated properly -cjr */
+		
+		/* Set tcp receive buffer if requested */
+		if (options.tcp_rcv_buf) 
+		  {
+		    if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, 
+				   (void *)&options.tcp_rcv_buf, 
+				   sizeof(options.tcp_rcv_buf)) >= 0)
+		      {             
+			debug("setsockopt SO_RCVBUF: %.100s", strerror(errno));
+		      } 
+		    else 
+		      {
+			/* coudln't set the socket size to use spec. */
+			/* should default to system param and continue */
+			/* warn the user though - cjr */
+			error("Couldn't set socket receive buffer as requested. Continuing anyway.");
+		      }
+		  }
 		return sock;
 	}
 	sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
 	if (sock < 0)
 		error("socket: %.100s", strerror(errno));
-
-	/* Bind the socket to an alternative local IP address */
+	
+	/* tuning needs to happen after the socket is */
+	/* created but before the connection happens */
+	/* so winscale is negotiated properly -cjr */
+	
+	/* Set tcp receive buffer if requested */
+	if (options.tcp_rcv_buf) 
+	  {
+	    if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, 
+			   (void *)&options.tcp_rcv_buf, 
+			   sizeof(options.tcp_rcv_buf)) >= 0)
+	      {             
+		debug("setsockopt SO_RCVBUF: %.100s", strerror(errno));
+	      }
+	    else 
+	      {
+		/* coudln't set the socket size to use spec. */
+		/* should default to system param and continue */
+		/* warn the user though - cjr */
+		error("Couldn't set socket receive buffer as requested. Continuing anyway.");
+	      }
+	  }
+	
+       	/* Bind the socket to an alternative local IP address */
 	if (options.bind_address == NULL)
 		return sock;
 
diff -u openssh-3.9p1/version.h openssh-3.9p1-hpn10/version.h
--- openssh-3.9p1/version.h	2004-08-17 07:47:41.000000000 -0500
+++ openssh-3.9p1-hpn10/version.h	2005-05-26 13:50:35.000000000 -0500
@@ -1,3 +1,3 @@
 /* $OpenBSD: version.h,v 1.42 2004/08/16 08:17:01 markus Exp $ */
 
-#define SSH_VERSION	"OpenSSH_3.9p1"
+#define SSH_VERSION	"OpenSSH_3.9p1-hpn"
