- (djm) Replacement for inet_ntoa for Irix (which breaks on gcc)
 - (djm) Fix strerror replacement for old SunOS. Based on patch from
   Charles Levert <charles@comm.polymtl.ca>
 - (djm) Seperate arc4random into seperate file and use OpenSSL's RC4
   implementation.
diff --git a/ChangeLog b/ChangeLog
index 9e9140d..0e122cf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+20000816
+ - (djm) Replacement for inet_ntoa for Irix (which breaks on gcc)
+ - (djm) Fix strerror replacement for old SunOS. Based on patch from 
+   Charles Levert <charles@comm.polymtl.ca>
+ - (djm) Seperate arc4random into seperate file and use OpenSSL's RC4 
+   implementation.
+
 20000815
  - (djm) More SunOS 4.1.x fixes from Nate Itkin <nitkin@europa.com>
  - (djm) Avoid failures on Irix when ssh is not setuid. Fix from
diff --git a/Makefile.in b/Makefile.in
index 518b4c8..0aaaa68 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -36,7 +36,7 @@
 
 LIBSSH_OBJS=atomicio.o authfd.o authfile.o aux.o bufaux.o buffer.o canohost.o channels.o cipher.o compat.o compress.o crc32.o deattack.o dispatch.o dsa.o fingerprint.o hmac.o hostfile.o key.o kex.o log.o match.o mpaux.o nchan.o packet.o radix.o entropy.o readpass.o rsa.o tildexpand.o ttymodes.o uidswap.o uuencode.o xmalloc.o 
 
-LIBOPENBSD_COMPAT_OBJS=bsd-base64.o bsd-bindresvport.o bsd-daemon.o bsd-inet_aton.o bsd-misc.o bsd-mktemp.o bsd-rresvport.o bsd-setenv.o bsd-sigaction.o bsd-snprintf.o bsd-strlcat.o bsd-strlcpy.o bsd-strsep.o fake-getaddrinfo.o fake-getnameinfo.o next-posix.o
+LIBOPENBSD_COMPAT_OBJS=bsd-arc4random.o bsd-base64.o bsd-bindresvport.o bsd-daemon.o bsd-inet_aton.o bsd-inet_ntoa.o bsd-misc.o bsd-mktemp.o bsd-rresvport.o bsd-setenv.o bsd-sigaction.o bsd-snprintf.o bsd-strlcat.o bsd-strlcpy.o bsd-strsep.o fake-getaddrinfo.o fake-getnameinfo.o next-posix.o
 
 SSHOBJS= ssh.o sshconnect.o sshconnect1.o sshconnect2.o log-client.o readconf.o clientloop.o
 
diff --git a/acconfig.h b/acconfig.h
index 358390b..6c25c8f 100644
--- a/acconfig.h
+++ b/acconfig.h
@@ -6,9 +6,15 @@
 
 @TOP@
 
+/* Define if you system's inet_ntoa is busted (e.g. Irix gcc issue) */
+#undef BROKEN_INET_NTOA
+
 /* Define if your system defines sys_errlist[] */
 #undef HAVE_SYS_ERRLIST
 
+/* Define if your system defines sys_nerr */
+#undef HAVE_SYS_NERR
+
 /* Define if your system choked on IP TOS setting */
 #undef IP_TOS_IS_BROKEN
 
diff --git a/bsd-arc4random.c b/bsd-arc4random.c
new file mode 100644
index 0000000..d01a515
--- /dev/null
+++ b/bsd-arc4random.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 1999-2000 Damien Miller.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *      This product includes software developed by Markus Friedl.
+ * 4. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "includes.h"
+#include <openssl/rand.h>
+#include <openssl/rc4.h>
+
+#ifndef HAVE_ARC4RANDOM
+
+static int rc4_ready = 0;
+static RC4_KEY rc4;
+
+unsigned int arc4random(void)
+{
+	unsigned int r = 0;
+
+	if (!rc4_ready)
+		arc4random_stir();
+	
+	RC4(&rc4, sizeof(r), (unsigned char *)&r, (unsigned char *)&r);
+	
+	return(r);
+}
+
+void arc4random_stir(void)
+{
+	unsigned char rand_buf[32];
+	
+	memset(&rc4, 0, sizeof(rc4));
+
+	seed_rng();
+	RAND_bytes(rand_buf, sizeof(rand_buf));
+	
+	RC4_set_key(&rc4, sizeof(rand_buf), rand_buf);
+
+	memset(rand_buf, 0, sizeof(rand_buf));
+}
+#endif /* !HAVE_ARC4RANDOM */
diff --git a/bsd-arc4random.h b/bsd-arc4random.h
new file mode 100644
index 0000000..c6ccd35
--- /dev/null
+++ b/bsd-arc4random.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 1999-2000 Damien Miller.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *      This product includes software developed by Markus Friedl.
+ * 4. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _BSD_ARC4RANDOM_H
+#define _BSD_ARC4RANDOM_H
+
+#include "config.h"
+
+#ifndef HAVE_ARC4RANDOM
+unsigned int arc4random(void);
+void arc4random_stir(void);
+#endif /* !HAVE_ARC4RANDOM */
+
+#endif /* _BSD_ARC4RANDOM_H */
diff --git a/bsd-inet_ntoa.c b/bsd-inet_ntoa.c
new file mode 100644
index 0000000..fe5539b
--- /dev/null
+++ b/bsd-inet_ntoa.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 1983, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by the University of
+ *	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#if defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA)
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char rcsid[] = "$OpenBSD: inet_ntoa.c,v 1.2 1996/08/19 08:29:16 tholo Exp $";
+#endif /* LIBC_SCCS and not lint */
+
+/*
+ * Convert network-format internet address
+ * to base 256 d.d.d.d representation.
+ */
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdio.h>
+#include "bsd-inet_ntoa.h"
+
+char *inet_ntoa(struct in_addr in)
+{
+	static char b[18];
+	register char *p;
+
+	p = (char *)&in;
+#define	UC(b)	(((int)b)&0xff)
+	(void)snprintf(b, sizeof(b),
+	    "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
+	return (b);
+}
+
+#endif /* defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA) */
diff --git a/bsd-inet_ntoa.h b/bsd-inet_ntoa.h
new file mode 100644
index 0000000..e3d48e4
--- /dev/null
+++ b/bsd-inet_ntoa.h
@@ -0,0 +1,10 @@
+#ifndef _BSD_INET_NTOA_H
+#define _BSD_INET_NTOA_H
+
+#include "config.h"
+
+#if defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA)
+char *inet_ntoa(struct in_addr in);
+#endif /* defined(BROKEN_INET_NTOA) || !defined(HAVE_INET_NTOA) */
+
+#endif /* _BSD_INET_NTOA_H */
diff --git a/bsd-misc.c b/bsd-misc.c
index e6b529e..67614eb 100644
--- a/bsd-misc.c
+++ b/bsd-misc.c
@@ -27,107 +27,9 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "config.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <unistd.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#ifdef HAVE_STDDEF_H
-#include <stddef.h>
-#endif
-
+#include "includes.h"
 #include "xmalloc.h"
 #include "ssh.h"
-#include "bsd-misc.h"
-#include "entropy.h"
-
-#include <openssl/rand.h>
-
-#ifndef HAVE_ARC4RANDOM
-
-typedef struct
-{
-	unsigned int s[256];
-	int i;
-	int j;
-} rc4_t;
-
-void rc4_key(rc4_t *r, unsigned char *key, int len);
-void rc4_getbytes(rc4_t *r, unsigned char *buffer, int len);
-
-static rc4_t *rc4 = NULL;
-
-void rc4_key(rc4_t *r, unsigned char *key, int len)
-{
-	int t;
-	
-	for(r->i = 0; r->i < 256; r->i++)
-		r->s[r->i] = r->i;
-
-	r->j = 0;
-	for(r->i = 0; r->i < 256; r->i++)
-	{
-		r->j = (r->j + r->s[r->i] + key[r->i % len]) % 256;
-		t = r->s[r->i];
-		r->s[r->i] = r->s[r->j];
-		r->s[r->j] = t;
-	}
-	r->i = r->j = 0;
-}
-
-void rc4_getbytes(rc4_t *r, unsigned char *buffer, int len)
-{
-	int t;
-	int c;
-
-	c = 0;	
-	while(c < len)
-	{
-		r->i = (r->i + 1) % 256;
-		r->j = (r->j + r->s[r->i]) % 256;
-		t = r->s[r->i];
-		r->s[r->i] = r->s[r->j];
-		r->s[r->j] = t;
-
-		t = (r->s[r->i] + r->s[r->j]) % 256;
-		
-		buffer[c] = r->s[t];
-		c++;
-	}
-}
-
-unsigned int arc4random(void)
-{
-	unsigned int r;
-
-	if (rc4 == NULL)
-		arc4random_stir();
-	
-	rc4_getbytes(rc4, (unsigned char *)&r, sizeof(r));
-	
-	return(r);
-}
-
-void arc4random_stir(void)
-{
-	unsigned char rand_buf[32];
-	
-	if (rc4 == NULL)
-		rc4 = xmalloc(sizeof(*rc4));
-
-	seed_rng();
-	RAND_bytes(rand_buf, sizeof(rand_buf));
-	
-	rc4_key(rc4, rand_buf, sizeof(rand_buf));
-	memset(rand_buf, 0, sizeof(rand_buf));
-}
-#endif /* !HAVE_ARC4RANDOM */
 
 #ifndef HAVE_SETPROCTITLE
 void setproctitle(const char *fmt, ...)
@@ -158,9 +60,15 @@
 }
 #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
 
-#if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST)
-const char *strerror(void)
+#if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
+const char *strerror(int e)
 {
-	return(sys_errlist[errno]);
+	extern int sys_nerr;
+	extern char *sys_errlist[];
+	
+	if ((e >= 0) || (e < sys_nerr))
+		return("unlisted error");
+	else
+		return(sys_errlist[e]);
 }
-#endif /* !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) */
+#endif
diff --git a/bsd-misc.h b/bsd-misc.h
index 76b4e1a..aea285c 100644
--- a/bsd-misc.h
+++ b/bsd-misc.h
@@ -32,11 +32,6 @@
 
 #include "config.h"
 
-#ifndef HAVE_ARC4RANDOM
-unsigned int arc4random(void);
-void arc4random_stir(void);
-#endif /* !HAVE_ARC4RANDOM */
-
 #ifndef HAVE_SETPROCTITLE
 void setproctitle(const char *fmt, ...);
 #endif /* !HAVE_SETPROCTITLE */
@@ -58,8 +53,8 @@
 int seteuid(uid_t euid);
 #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
 
-#if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST)
-const char *strerror(void);
-#endif /* !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) */
+#if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
+const char *strerror(int e);
+#endif 
 
 #endif /* _BSD_MISC_H */
diff --git a/configure.in b/configure.in
index 7ad0d01..974d0df 100644
--- a/configure.in
+++ b/configure.in
@@ -87,6 +87,7 @@
 	MANTYPE='$(CATMAN)'
 	no_libsocket=1
 	no_libnsl=1
+	AC_DEFINE(BROKEN_INET_NTOA)
 	;;
 *-*-irix6*)
 	CFLAGS="$CFLAGS -I/usr/local/include"
@@ -97,6 +98,7 @@
 	AC_DEFINE(WITH_IRIX_AUDIT)
 	no_libsocket=1
 	no_libnsl=1
+	AC_DEFINE(BROKEN_INET_NTOA)
 	;;
 *-*-linux*)
 	no_dev_ptmx=1
@@ -224,7 +226,7 @@
 AC_CHECK_HEADERS(bstring.h endian.h floatingpoint.h lastlog.h limits.h login.h maillock.h netdb.h netgroup.h netinet/in_systm.h paths.h poll.h pty.h shadow.h security/pam_appl.h sys/bitypes.h sys/bsdtty.h sys/cdefs.h sys/poll.h sys/select.h sys/stat.h sys/stropts.h sys/sysmacros.h sys/time.h sys/ttcompat.h stddef.h time.h ttyent.h usersec.h util.h utmp.h utmpx.h)
 
 # Checks for library functions.
-AC_CHECK_FUNCS(arc4random atexit b64_ntop bcopy bindresvport_af clock freeaddrinfo gai_strerror getaddrinfo getnameinfo getrusage getttyent inet_aton innetgr md5_crypt memmove mkdtemp on_exit openpty rresvport_af setenv seteuid setlogin setproctitle setreuid sigaction sigvec snprintf strerror strlcat strlcpy strsep vsnprintf vhangup _getpty __b64_ntop)
+AC_CHECK_FUNCS(arc4random atexit b64_ntop bcopy bindresvport_af clock freeaddrinfo gai_strerror getaddrinfo getnameinfo getrusage getttyent inet_aton inet_ntoa innetgr md5_crypt memmove mkdtemp on_exit openpty rresvport_af setenv seteuid setlogin setproctitle setreuid sigaction sigvec snprintf strerror strlcat strlcpy strsep vsnprintf vhangup _getpty __b64_ntop)
 dnl    checks for time functions
 AC_CHECK_FUNCS(gettimeofday time)
 dnl    checks for libutil functions
@@ -741,6 +743,18 @@
 fi
 
 
+AC_CACHE_CHECK([if libc defines sys_nerr], ac_cv_libc_defines_sys_nerr, [
+	AC_TRY_LINK([], 
+		[ extern int sys_nerr; printf("%i", sys_nerr);], 
+		[ ac_cv_libc_defines_sys_nerr="yes" ],
+		[ ac_cv_libc_defines_sys_nerr="no" ]
+	)
+])
+if test "x$ac_cv_libc_defines_sys_nerr" = "xyes" ; then
+	AC_DEFINE(HAVE_SYS_NERR)
+fi
+
+
 # Looking for programs, paths and files
 AC_ARG_WITH(rsh,
 	[  --with-rsh=PATH         Specify path to remote shell program ],
diff --git a/openbsd-compat.h b/openbsd-compat.h
index 3802265..521579b 100644
--- a/openbsd-compat.h
+++ b/openbsd-compat.h
@@ -4,6 +4,7 @@
 #include "config.h"
 
 /* BSD function replacements */
+#include "bsd-arc4random.h"
 #include "bsd-bindresvport.h"
 #include "bsd-rresvport.h"
 #include "bsd-misc.h"
@@ -15,6 +16,7 @@
 #include "bsd-base64.h"
 #include "bsd-sigaction.h"
 #include "bsd-inet_aton.h"
+#include "bsd-inet_ntoa.h"
 #include "bsd-strsep.h"
 
 /* rfc2553 socket API replacements */