diff --git a/CREDITS b/CREDITS
index 72cd9cfaf575512617fe1dc51b49245db852d477..1d47d81a7c82823c1854fdd5e6ea29b05ad95f1d 100755
--- a/CREDITS
+++ b/CREDITS
@@ -64,6 +64,8 @@ Josh Roberson - chan_zap reload support, Advanced Voicemail Features, other misc
 William Waites - syslog support, SIP NAT traversal for SIP-UA. ww@styx.org
 Rich Murphey - Porting to FreeBSD, NetBSD, OpenBSD, and Darwin.
 	rich@whiteoaklabs.com  http://whiteoaklabs.com
+Simon Lockhart - Porting to Solaris (based on work of Logan ???)	
+	simon@slimey.org
 
 === OTHER CONTRIBUTIONS ===
 John Todd - Monkey sounds and associated teletorture prompt
diff --git a/Makefile b/Makefile
index fe1b8083e607a0f9c74665bd4f46896359b8f6bd..bc3dc1a56add10d4ccb83a48ac74bd12d73049b2 100755
--- a/Makefile
+++ b/Makefile
@@ -55,6 +55,12 @@ endif
 
 PWD=$(shell pwd)
 
+GREP=grep
+ifeq (${OSARCH},SunOS)
+GREP=/usr/xpg4/bin/grep
+M4=/usr/local/bin/m4
+endif
+
 ######### More GSM codec optimization
 ######### Uncomment to enable MMXTM optimizations for x86 architecture CPU's
 ######### which support MMX instructions.  This should be newer pentiums,
@@ -136,7 +142,7 @@ ifneq ($(PROC),ultrasparc)
 CFLAGS+=$(shell if $(CC) -march=$(PROC) -S -o /dev/null -xc /dev/null >/dev/null 2>&1; then echo "-march=$(PROC)"; fi)
 endif
 
-CFLAGS+=$(shell if uname -m | grep -q ppc; then echo "-fsigned-char"; fi)
+CFLAGS+=$(shell if uname -m | $(GREP) -q ppc; then echo "-fsigned-char"; fi)
 CFLAGS+=$(shell if [ -f /usr/include/osp/osp.h ]; then echo "-DOSP_SUPPORT -I/usr/include/osp" ; fi)
 
 ifeq (${OSARCH},FreeBSD)
@@ -155,6 +161,10 @@ endif
 ifeq (${OSARCH},OpenBSD)
 CFLAGS+=-pthread
 endif
+ifeq (${OSARCH},SunOS)
+CFLAGS+=-Wcast-align -DSOLARIS
+INCLUDE+=-Iinclude/solaris-compat -I/usr/local/ssl/include
+endif
 
 #Uncomment this to use the older DSP routines
 #CFLAGS+=-DOLD_DSP_ROUTINES
@@ -207,6 +217,9 @@ endif
 ifeq (${OSARCH},OpenBSD)
 LIBS=-lcrypto -lpthread -lm -lncurses
 endif
+ifeq (${OSARCH},SunOS)
+LIBS+=-lpthread -ldl -lnsl -lsocket -lresolv -L/usr/local/ssl/lib
+endif
 LIBS+=-lssl
 OBJS=io.o sched.o logger.o frame.o loader.o config.o channel.o \
 	translate.o file.o say.o pbx.o cli.o md5.o term.o \
@@ -223,6 +236,11 @@ else
 ASTLINK=-Wl,-E 
 SOLINK=-shared -Xlinker -x
 endif
+ifeq (${OSARCH},SunOS)
+OBJS+=strcompat.o
+ASTLINK=
+SOLINK=-shared -fpic -L/usr/local/ssl/lib
+endif
 
 CC=gcc
 INSTALL=install
@@ -260,7 +278,7 @@ endif
 .PHONY: _version
 
 _version: 
-	if [ -d CVS ] && ! [ -f .version ]; then echo $(ASTERISKVERSION) > .version; fi 
+	if [ -d CVS ] && [ ! -f .version ]; then echo $(ASTERISKVERSION) > .version; fi 
 
 .version: _version
 
@@ -311,7 +329,7 @@ clean:
 	rm -f *.o *.so asterisk .depend
 	rm -f build.h 
 	rm -f ast_expr.c
-	@if [ -e editline/Makefile ]; then $(MAKE) -C editline distclean ; fi
+	@if [ -f editline/Makefile ]; then $(MAKE) -C editline distclean ; fi
 	@if [ -d mpg123-0.59r ]; then make -C mpg123-0.59r clean; fi
 	$(MAKE) -C db1-ast clean
 	$(MAKE) -C stdtime clean
@@ -320,7 +338,7 @@ datafiles: all
 	sh mkpkgconfig $(DESTDIR)/usr/lib/pkgconfig
 	mkdir -p $(DESTDIR)$(ASTVARLIBDIR)/sounds/digits
 	for x in sounds/digits/*.gsm; do \
-		if grep -q "^%`basename $$x`%" sounds.txt; then \
+		if $(GREP) -q "^%`basename $$x`%" sounds.txt; then \
 			install -m 644 $$x $(DESTDIR)$(ASTVARLIBDIR)/sounds/digits ; \
 		else \
 			echo "No description for $$x"; \
@@ -329,7 +347,7 @@ datafiles: all
 	done
 	mkdir -p $(DESTDIR)$(ASTVARLIBDIR)/sounds/letters
 	for x in sounds/letters/*.gsm; do \
-		if grep -q "^%`basename $$x`%" sounds.txt; then \
+		if $(GREP) -q "^%`basename $$x`%" sounds.txt; then \
 			install -m 644 $$x $(DESTDIR)$(ASTVARLIBDIR)/sounds/letters ; \
 		else \
 			echo "No description for $$x"; \
@@ -338,7 +356,7 @@ datafiles: all
 	done
 	mkdir -p $(DESTDIR)$(ASTVARLIBDIR)/sounds/phonetic
 	for x in sounds/phonetic/*.gsm; do \
-		if grep -q "^%`basename $$x`%" sounds.txt; then \
+		if $(GREP) -q "^%`basename $$x`%" sounds.txt; then \
 			install -m 644 $$x $(DESTDIR)$(ASTVARLIBDIR)/sounds/phonetic ; \
 		else \
 			echo "No description for $$x"; \
@@ -346,7 +364,7 @@ datafiles: all
 		fi; \
 	done
 	for x in sounds/vm-* sounds/transfer* sounds/pbx-* sounds/ss-* sounds/beep* sounds/dir-* sounds/conf-* sounds/agent-* sounds/invalid* sounds/tt-* sounds/auth-* sounds/privacy-* sounds/queue-*; do \
-		if grep -q "^%`basename $$x`%" sounds.txt; then \
+		if $(GREP) -q "^%`basename $$x`%" sounds.txt; then \
 			install -m 644 $$x $(DESTDIR)$(ASTVARLIBDIR)/sounds ; \
 		else \
 			echo "No description for $$x"; \
@@ -476,7 +494,7 @@ samples: all datafiles adsi
 		echo "Skipping asterisk.conf creation"; \
 	fi
 	for x in sounds/demo-*; do \
-		if grep -q "^%`basename $$x`%" sounds.txt; then \
+		if $(GREP) -q "^%`basename $$x`%" sounds.txt; then \
 			install -m 644 $$x $(DESTDIR)$(ASTVARLIBDIR)/sounds ; \
 		else \
 			echo "No description for $$x"; \
diff --git a/acl.c b/acl.c
index c7eb34270888302d470e4aec866b943a07b5347c..9169659a93868a4e92fe2818e75e31a06b0c682e 100755
--- a/acl.c
+++ b/acl.c
@@ -38,6 +38,11 @@
 AST_MUTEX_DEFINE_STATIC(routeseq_lock);
 #endif
 
+#if defined (SOLARIS)
+#include <sys/sockio.h>
+#endif
+
+
 
 struct ast_ha {
 	/* Host access rule */
diff --git a/aesopt.h b/aesopt.h
index 94e3e47f152788e1ce109f4cbf9e41cfbacdc645..f476de09e8940d85273f9d378df0da922e3d5454 100755
--- a/aesopt.h
+++ b/aesopt.h
@@ -156,6 +156,8 @@
 #  include <sys/endian.h>
 #elif defined( BSD ) && ( BSD >= 199103 ) || defined(__APPLE__)
 #  include <machine/endian.h>
+#elif defined ( SOLARIS )
+#  include <solaris-compat/compat.h>
 #elif defined( __GNUC__ ) || defined( __GNU_LIBRARY__ )
 #  include <endian.h>
 #if !defined(__APPLE__)
diff --git a/agi/Makefile b/agi/Makefile
index 2f87df5f87add7223012c50a895e35a4879135df..0d8a35922104890f46d797be1cf6805bf7856628 100755
--- a/agi/Makefile
+++ b/agi/Makefile
@@ -15,6 +15,11 @@ AGIS=agi-test.agi eagi-test eagi-sphinx-test
 
 CFLAGS+=
 
+LIBS=
+ifeq ($(OSARCH),SunOS)
+LIBS=-lsocket -lnsl ../strcompat.o
+endif
+
 all: depend $(AGIS)
 
 install: all
@@ -22,10 +27,10 @@ install: all
 	for x in $(AGIS); do $(INSTALL) -m 755 $$x $(DESTDIR)$(AGI_DIR) ; done
 
 eagi-test: eagi-test.o
-	$(CC) $(CFLAGS) -o eagi-test eagi-test.o
+	$(CC) $(CFLAGS) -o eagi-test eagi-test.o $(LIBS)
 
 eagi-sphinx-test: eagi-sphinx-test.o
-	$(CC) $(CFLAGS) -o eagi-sphinx-test eagi-sphinx-test.o
+	$(CC) $(CFLAGS) -o eagi-sphinx-test eagi-sphinx-test.o $(LIBS) 
 
 clean:
 	rm -f *.so *.o look .depend eagi-test eagi-sphinx-test
diff --git a/agi/eagi-sphinx-test.c b/agi/eagi-sphinx-test.c
index 3015b730b98f47ef4086d78ff3e376cf9f5c6c3e..72bd93fd47c691510b0ba8248c5320393a46ef0d 100755
--- a/agi/eagi-sphinx-test.c
+++ b/agi/eagi-sphinx-test.c
@@ -17,6 +17,9 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <netdb.h>
+#ifdef SOLARIS
+#include <solaris-compat/compat.h>
+#endif
 
 #define AUDIO_FILENO (STDERR_FILENO + 1)
 
diff --git a/agi/eagi-test.c b/agi/eagi-test.c
index aa498cb88aafdf0663f09b34a8570938f056a9cd..360fd42229d358acb86424900cf4cfa66ed43db2 100755
--- a/agi/eagi-test.c
+++ b/agi/eagi-test.c
@@ -11,6 +11,9 @@
 #include <errno.h>
 #include <string.h>
 #include <sys/select.h>
+#ifdef SOLARIS
+#include <solaris-compat/compat.h>
+#endif
 
 #define AUDIO_FILENO (STDERR_FILENO + 1)
 
diff --git a/apps/Makefile b/apps/Makefile
index 08e24de11938639b04187fb0250537207ad6fce0..48dfdedd7f52c52f006be48a0bf1113d1e27036b 100755
--- a/apps/Makefile
+++ b/apps/Makefile
@@ -33,8 +33,10 @@ APPS=app_dial.so app_playback.so app_voicemail.so app_directory.so app_mp3.so\
      app_dumpchan.so app_waitforsilence.so
 
 ifneq (${OSARCH},Darwin)
+ifneq (${OSARCH},SunOS)
 APPS+=app_intercom.so
 endif
+endif
 
 #
 # Obsolete things...
diff --git a/apps/app_nbscat.c b/apps/app_nbscat.c
index 75951de51c99f83c4f8a1bc617c1f9a7b1cc8788..f8b4b37b92845d5a32b7de8a62ef24fe6bff43a3 100755
--- a/apps/app_nbscat.c
+++ b/apps/app_nbscat.c
@@ -31,6 +31,10 @@
 #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
 #define NBSCAT "/usr/bin/nbscat8k"
 
+#ifndef AF_LOCAL
+#define AF_LOCAL AF_UNIX
+#endif
+
 static char *tdesc = "Silly NBS Stream Application";
 
 static char *app = "NBScat";
diff --git a/apps/app_qcall.c b/apps/app_qcall.c
index f5721e167ce8cfd7408daafcb49ccdcf3d657b88..204e491a05e8c2d269e7d896603db52ccbf8ed01 100755
--- a/apps/app_qcall.c
+++ b/apps/app_qcall.c
@@ -95,6 +95,23 @@ LOCAL_USER_DECL;
 #define	OURCLID	"2564286275"		/* The callerid to be displayed when calling */
 #endif
 
+#ifdef SOLARIS
+int flock(int fd, int type)
+{
+	struct flock lock;
+
+        lock.l_type = type;
+        lock.l_whence = SEEK_SET;
+        lock.l_start = 0;
+        lock.l_len = 0;
+
+	return fcntl(f, F_SETLK, &lock);
+}
+
+#define LOCK_EX F_WRLCK
+#define LOCK_UN F_UNLCK
+#endif
+
 static void *qcall_do(void *arg);
 
 static void *qcall(void *ignore)
diff --git a/apps/app_sms.c b/apps/app_sms.c
index faba221d6c1cfe72f3df7b52ff207dc997fec24f..42412ddf51e9cb69093b7e4e23e52f6503bbb701 100755
--- a/apps/app_sms.c
+++ b/apps/app_sms.c
@@ -1077,6 +1077,10 @@ sms_handleincoming (sms_t * h)
    return 0;                    /* no error */
 }
 
+#ifdef SOLARIS
+#define NAME_MAX 1024
+#endif
+
 static void
 sms_nextoutgoing (sms_t * h)
 {                               /* find and fill in next message, or send a REL if none waiting */
diff --git a/ast_expr.y b/ast_expr.y
index df08f68299608728487e2e85b3646d1de444058b..e90931f8791593cb7ee1a7c92f0e4b242fc268c7 100755
--- a/ast_expr.y
+++ b/ast_expr.y
@@ -13,7 +13,11 @@
 #include <string.h>
 #include <locale.h>
 #include <ctype.h>
+#ifndef SOLARIS
 #include <err.h>
+#else
+typedef uint64_t quad_t;
+#endif
 #include <errno.h>
 #include <regex.h>
 #include <limits.h>
@@ -39,6 +43,10 @@
 
 /* #define ast_log fprintf
 #define LOG_WARNING stderr */
+
+#ifdef SOLARIS
+#define __P(p) p
+#endif
   
 enum valtype {
 	integer, numeric_string, string
diff --git a/asterisk.c b/asterisk.c
index 4dcf27a81a69656fff0ee69762efc6fa6d34052a..204ef160ab57208ea2b3463f224d3795970bd9e5 100755
--- a/asterisk.c
+++ b/asterisk.c
@@ -53,10 +53,15 @@
 #include <grp.h>
 #include <pwd.h>
 
-#if  defined(__FreeBSD__) || defined( __NetBSD__ )
+#if  defined(__FreeBSD__) || defined( __NetBSD__ ) || defined(SOLARIS)
 #include <netdb.h>
 #endif
 
+#ifndef AF_LOCAL
+#define AF_LOCAL AF_UNIX
+#define PF_LOCAL PF_UNIX
+#endif
+
 #define AST_MAX_CONNECTS 128
 #define NUM_MSGS 64
 
@@ -315,7 +320,7 @@ static void *netconsole(void *vconsole)
 
 static void *listener(void *unused)
 {
-	struct sockaddr_un sun;
+	struct sockaddr_un sunaddr;
 	int s;
 	int len;
 	int x;
@@ -335,8 +340,8 @@ static void *listener(void *unused)
 				ast_log(LOG_WARNING, "poll returned error: %s\n", strerror(errno));
 			continue;
 		}
-		len = sizeof(sun);
-		s = accept(ast_socket, (struct sockaddr *)&sun, &len);
+		len = sizeof(sunaddr);
+		s = accept(ast_socket, (struct sockaddr *)&sunaddr, &len);
 		if (s < 0) {
 			if (errno != EINTR)
 				ast_log(LOG_WARNING, "Accept returned %d: %s\n", s, strerror(errno));
@@ -377,7 +382,7 @@ static void *listener(void *unused)
 
 static int ast_makesocket(void)
 {
-	struct sockaddr_un sun;
+	struct sockaddr_un sunaddr;
 	int res;
 	int x;
 	for (x=0;x<AST_MAX_CONNECTS;x++)	
@@ -388,10 +393,10 @@ static int ast_makesocket(void)
 		ast_log(LOG_WARNING, "Unable to create control socket: %s\n", strerror(errno));
 		return -1;
 	}		
-	memset(&sun, 0, sizeof(sun));
-	sun.sun_family = AF_LOCAL;
-	strncpy(sun.sun_path, (char *)ast_config_AST_SOCKET, sizeof(sun.sun_path)-1);
-	res = bind(ast_socket, (struct sockaddr *)&sun, sizeof(sun));
+	memset(&sunaddr, 0, sizeof(sunaddr));
+	sunaddr.sun_family = AF_LOCAL;
+	strncpy(sunaddr.sun_path, (char *)ast_config_AST_SOCKET, sizeof(sunaddr.sun_path)-1);
+	res = bind(ast_socket, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
 	if (res) {
 		ast_log(LOG_WARNING, "Unable to bind socket to %s: %s\n", (char *)ast_config_AST_SOCKET, strerror(errno));
 		close(ast_socket);
@@ -412,17 +417,17 @@ static int ast_makesocket(void)
 
 static int ast_tryconnect(void)
 {
-	struct sockaddr_un sun;
+	struct sockaddr_un sunaddr;
 	int res;
 	ast_consock = socket(PF_LOCAL, SOCK_STREAM, 0);
 	if (ast_consock < 0) {
 		ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
 		return 0;
 	}
-	memset(&sun, 0, sizeof(sun));
-	sun.sun_family = AF_LOCAL;
-	strncpy(sun.sun_path, (char *)ast_config_AST_SOCKET, sizeof(sun.sun_path)-1);
-	res = connect(ast_consock, (struct sockaddr *)&sun, sizeof(sun));
+	memset(&sunaddr, 0, sizeof(sunaddr));
+	sunaddr.sun_family = AF_LOCAL;
+	strncpy(sunaddr.sun_path, (char *)ast_config_AST_SOCKET, sizeof(sunaddr.sun_path)-1);
+	res = connect(ast_consock, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
 	if (res) {
 		close(ast_consock);
 		ast_consock = -1;
diff --git a/channels/Makefile b/channels/Makefile
index 9810cec95038ec74a6bcb32d6eddacd5ca4a67ca..5769fc0736508b6c6e602a76eeb9fbd196998d22 100755
--- a/channels/Makefile
+++ b/channels/Makefile
@@ -19,7 +19,7 @@ PROC=$(shell uname -m)
 
 CHANNEL_LIBS=chan_modem.so chan_sip.so \
 	     chan_modem_aopen.so \
-             chan_modem_bestdata.so chan_modem_i4l.so \
+             chan_modem_bestdata.so \
              chan_agent.so chan_mgcp.so chan_iax2.so \
 	     chan_local.so chan_skinny.so chan_features.so
 
@@ -52,7 +52,13 @@ H323LIB=-lh323_NetBSD_x86_r
 SOLINK+=-L/usr/local/lib
 endif
 ifneq (${OSARCH},Darwin)
-CHANNEL_LIBS+=chan_oss.so
+ifneq (${OSARCH},SunOS)
+CHANNEL_LIBS+=chan_oss.so chan_modem_i4l.so 
+endif
+endif
+
+ifeq (${OSARCH},SunOS)
+SOLINK+=-lrt
 endif
 
 CHANNEL_LIBS+=$(shell [ -f /usr/include/linux/ixjuser.h ] && echo chan_phone.so)
diff --git a/channels/chan_mgcp.c b/channels/chan_mgcp.c
index 72851dd3d0629688e4a5f480b08aa572095be9fe..98070483b0030aca1d03b8c66bf110666a2812c6 100755
--- a/channels/chan_mgcp.c
+++ b/channels/chan_mgcp.c
@@ -105,6 +105,10 @@
 #define MAX_EXPIREY     3600
 #define CANREINVITE	1
 
+#ifndef INADDR_NONE
+#define INADDR_NONE (in_addr_t)(-1)
+#endif
+
 static char *desc = "Media Gateway Control Protocol (MGCP)";
 static char *type = "MGCP";
 static char *tdesc = "Media Gateway Control Protocol (MGCP)";
diff --git a/channels/chan_modem.c b/channels/chan_modem.c
index 41d95540b69c7cc6456d0e2da3cc510e296bb4e8..9a1e2074d5f528a154da8df22c8bcdef8e4954ac 100755
--- a/channels/chan_modem.c
+++ b/channels/chan_modem.c
@@ -709,8 +709,19 @@ static void stty(struct ast_modem_pvt *p)
 		ast_log(LOG_WARNING, "Unable to get serial parameters on %s: %s\n", p->dev, strerror(errno));
 		return;
 	}
+#ifndef SOLARIS
 	cfmakeraw(&mode);
-	cfsetspeed(&mode, B115200);
+#else
+        mode.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
+                        |INLCR|IGNCR|ICRNL|IXON);
+        mode.c_oflag &= ~OPOST;
+        mode.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
+        mode.c_cflag &= ~(CSIZE|PARENB);
+        mode.c_cflag |= CS8;
+#endif
+
+	cfsetispeed(&mode, B115200);
+	cfsetospeed(&mode, B115200);
 	if (tcsetattr(p->fd, TCSANOW, &mode)) 
 		ast_log(LOG_WARNING, "Unable to set serial parameters on %s: %s\n", p->dev, strerror(errno));
 	
diff --git a/channels/chan_zap.c b/channels/chan_zap.c
index 25b99a315d46377931bf80d12984c8f2063296cb..7b3216b58357c7b1ea3fb7a6a881138d00da2327 100755
--- a/channels/chan_zap.c
+++ b/channels/chan_zap.c
@@ -42,7 +42,9 @@
 #include <sys/signal.h>
 #include <errno.h>
 #include <stdlib.h>
+#ifndef SOLARIS
 #include <stdint.h>
+#endif
 #include <unistd.h>
 #include <sys/ioctl.h>
 #ifdef __linux__
diff --git a/channels/iax2-parser.c b/channels/iax2-parser.c
index b66de41240382c879de39f006878f8d511bda33b..90d4822556406e095eb8dc62fe8aec2aaf83ecda 100755
--- a/channels/iax2-parser.c
+++ b/channels/iax2-parser.c
@@ -30,6 +30,22 @@ static int frames = 0;
 static int iframes = 0;
 static int oframes = 0;
 
+#ifdef SOLARIS
+static unsigned int get_uint32(unsigned char *p)
+{
+  return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
+}
+
+static unsigned short get_uint16(unsigned char *p)
+{
+  return (p[0] << 8) | p[1] ;
+}
+
+#else
+#define get_uint32(p) (*((unsigned int *)(p)))
+#define get_uint16(p) (*((unsigned short *)(p)))
+#endif
+
 static void internaloutput(const char *str)
 {
 	fputs(str, stdout);
@@ -67,7 +83,7 @@ static void dump_string(char *output, int maxlen, void *value, int len)
 static void dump_int(char *output, int maxlen, void *value, int len)
 {
 	if (len == (int)sizeof(unsigned int))
-		snprintf(output, maxlen, "%lu", (unsigned long)ntohl(*((unsigned int *)value)));
+		snprintf(output, maxlen, "%lu", (unsigned long)ntohl(get_uint32(value)));
 	else
 		snprintf(output, maxlen, "Invalid INT");
 }
@@ -75,7 +91,7 @@ static void dump_int(char *output, int maxlen, void *value, int len)
 static void dump_short(char *output, int maxlen, void *value, int len)
 {
 	if (len == (int)sizeof(unsigned short))
-		snprintf(output, maxlen, "%d", ntohs(*((unsigned short *)value)));
+		snprintf(output, maxlen, "%d", ntohs(get_uint16(value)));
 	else
 		snprintf(output, maxlen, "Invalid SHORT");
 }
@@ -105,8 +121,8 @@ static void dump_prov_flags(char *output, int maxlen, void *value, int len)
 {
 	char buf[256] = "";
 	if (len == (int)sizeof(unsigned int))
-		snprintf(output, maxlen, "%lu (%s)", (unsigned long)ntohl(*((unsigned int *)value)),
-			iax_provflags2str(buf, sizeof(buf), ntohl(*((unsigned int *)value))));
+		snprintf(output, maxlen, "%lu (%s)", (unsigned long)ntohl(get_uint32(value)),
+			iax_provflags2str(buf, sizeof(buf), ntohl(get_uint32(value))));
 	else
 		snprintf(output, maxlen, "Invalid INT");
 }
@@ -550,14 +566,14 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
 				snprintf(tmp, (int)sizeof(tmp), "Expecting capability to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
 				errorf(tmp);
 			} else
-				ies->capability = ntohl(*((unsigned int *)(data + 2)));
+				ies->capability = ntohl(get_uint32(data + 2));
 			break;
 		case IAX_IE_FORMAT:
 			if (len != (int)sizeof(unsigned int)) {
 				snprintf(tmp, (int)sizeof(tmp), "Expecting format to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
 				errorf(tmp);
 			} else
-				ies->format = ntohl(*((unsigned int *)(data + 2)));
+				ies->format = ntohl(get_uint32(data + 2));
 			break;
 		case IAX_IE_LANGUAGE:
 			ies->language = data + 2;
@@ -567,21 +583,21 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
 				snprintf(tmp, (int)sizeof(tmp),  "Expecting version to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
 				errorf(tmp);
 			} else
-				ies->version = ntohs(*((unsigned short *)(data + 2)));
+				ies->version = ntohs(get_uint16(data + 2));
 			break;
 		case IAX_IE_ADSICPE:
 			if (len != (int)sizeof(unsigned short)) {
 				snprintf(tmp, (int)sizeof(tmp), "Expecting adsicpe to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
 				errorf(tmp);
 			} else
-				ies->adsicpe = ntohs(*((unsigned short *)(data + 2)));
+				ies->adsicpe = ntohs(get_uint16(data + 2));
 			break;
 		case IAX_IE_SAMPLINGRATE:
 			if (len != (int)sizeof(unsigned short)) {
 				snprintf(tmp, (int)sizeof(tmp), "Expecting samplingrate to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
 				errorf(tmp);
 			} else
-				ies->samprate = ntohs(*((unsigned short *)(data + 2)));
+				ies->samprate = ntohs(get_uint16(data + 2));
 			break;
 		case IAX_IE_DNID:
 			ies->dnid = data + 2;
@@ -594,7 +610,7 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
 				snprintf(tmp, (int)sizeof(tmp), "Expecting authmethods to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
 				errorf(tmp);
 			} else
-				ies->authmethods = ntohs(*((unsigned short *)(data + 2)));
+				ies->authmethods = ntohs(get_uint16(data + 2));
 			break;
 		case IAX_IE_CHALLENGE:
 			ies->challenge = data + 2;
@@ -613,21 +629,21 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
 				snprintf(tmp, (int)sizeof(tmp),  "Expecting refresh to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
 				errorf(tmp);
 			} else
-				ies->refresh = ntohs(*((unsigned short *)(data + 2)));
+				ies->refresh = ntohs(get_uint16(data + 2));
 			break;
 		case IAX_IE_DPSTATUS:
 			if (len != (int)sizeof(unsigned short)) {
 				snprintf(tmp, (int)sizeof(tmp),  "Expecting dpstatus to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
 				errorf(tmp);
 			} else
-				ies->dpstatus = ntohs(*((unsigned short *)(data + 2)));
+				ies->dpstatus = ntohs(get_uint16(data + 2));
 			break;
 		case IAX_IE_CALLNO:
 			if (len != (int)sizeof(unsigned short)) {
 				snprintf(tmp, (int)sizeof(tmp),  "Expecting callno to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
 				errorf(tmp);
 			} else
-				ies->callno = ntohs(*((unsigned short *)(data + 2)));
+				ies->callno = ntohs(get_uint16(data + 2));
 			break;
 		case IAX_IE_CAUSE:
 			ies->cause = data + 2;
@@ -645,7 +661,7 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
 				snprintf(tmp, (int)sizeof(tmp), "Expecting msgcount to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
 				errorf(tmp);
 			} else
-				ies->msgcount = ntohs(*((unsigned short *)(data + 2)));	
+				ies->msgcount = ntohs(get_uint16(data + 2));	
 			break;
 		case IAX_IE_AUTOANSWER:
 			ies->autoanswer = 1;
@@ -658,21 +674,21 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
 				snprintf(tmp, (int)sizeof(tmp), "Expecting transferid to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
 				errorf(tmp);
 			} else
-				ies->transferid = ntohl(*((unsigned int *)(data + 2)));
+				ies->transferid = ntohl(get_uint32(data + 2));
 			break;
 		case IAX_IE_DATETIME:
 			if (len != (int)sizeof(unsigned int)) {
 				snprintf(tmp, (int)sizeof(tmp), "Expecting date/time to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
 				errorf(tmp);
 			} else
-				ies->datetime = ntohl(*((unsigned int *)(data + 2)));
+				ies->datetime = ntohl(get_uint32(data + 2));
 			break;
 		case IAX_IE_FIRMWAREVER:
 			if (len != (int)sizeof(unsigned short)) {
 				snprintf(tmp, (int)sizeof(tmp), "Expecting firmwarever to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
 				errorf(tmp);
 			} else
-				ies->firmwarever = ntohs(*((unsigned short *)(data + 2)));	
+				ies->firmwarever = ntohs(get_uint16(data + 2));	
 			break;
 		case IAX_IE_DEVICETYPE:
 			ies->devicetype = data + 2;
@@ -685,7 +701,7 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
 				snprintf(tmp, (int)sizeof(tmp), "Expected block desc to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len);
 				errorf(tmp);
 			} else
-				ies->fwdesc = ntohl(*((unsigned int *)(data + 2)));
+				ies->fwdesc = ntohl(get_uint32(data + 2));
 			break;
 		case IAX_IE_FWBLOCKDATA:
 			ies->fwdata = data + 2;
@@ -697,7 +713,7 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
 				errorf(tmp);
 			} else {
 				ies->provverpres = 1;
-				ies->provver = ntohl(*((unsigned int *)(data + 2)));
+				ies->provver = ntohl(get_uint32(data + 2));
 			}
 			break;
 		case IAX_IE_CALLINGPRES:
@@ -721,7 +737,7 @@ int iax_parse_ies(struct iax_ies *ies, unsigned char *data, int datalen)
 				snprintf(tmp, (int)sizeof(tmp), "Expecting callingtns to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len);
 				errorf(tmp);
 			} else
-				ies->calling_tns = ntohs(*((unsigned short *)(data + 2)));	
+				ies->calling_tns = ntohs(get_uint16(data + 2));	
 			break;
 		default:
 			snprintf(tmp, (int)sizeof(tmp), "Ignoring unknown information element '%s' (%d) of length %d\n", iax_ie2str(ie), ie, len);
diff --git a/cli.c b/cli.c
index 28d40cad0bb2c5f3bafa69c3eb876a5764b7c560..8a99c6e4b560ec4747e8b371dd9bcd08745e2e2f 100755
--- a/cli.c
+++ b/cli.c
@@ -47,7 +47,12 @@ void ast_cli(int fd, char *fmt, ...)
 
 	va_list ap;
 	va_start(ap, fmt);
+#ifdef SOLARIS
+        stuff = (char *)malloc(10240);
+        vsnprintf(stuff, 10240, fmt, ap);
+#else
 	res = vasprintf(&stuff, fmt, ap);
+#endif
 	va_end(ap);
 	if (res == -1) {
 		ast_log(LOG_ERROR, "Out of memory\n");
diff --git a/codecs/Makefile b/codecs/Makefile
index 82d8f458e09ec2c8bd9412e92a671c248bf480bd..07dc353434422f38fe5fa39577594a0e8ea44e81 100755
--- a/codecs/Makefile
+++ b/codecs/Makefile
@@ -41,8 +41,8 @@ all: depend $(CODECS)
 
 clean:
 	rm -f *.so *.o .depend
-	! [ -d g723.1 ] || $(MAKE) -C g723.1 clean
-	! [ -d g723.1b ] || $(MAKE) -C g723.1b clean
+	[ ! -d g723.1 ] || $(MAKE) -C g723.1 clean
+	[ ! -d g723.1b ] || $(MAKE) -C g723.1b clean
 	$(MAKE) -C gsm clean
 	$(MAKE) -C lpc10 clean
 	$(MAKE) -C ilbc clean
diff --git a/codecs/gsm/Makefile b/codecs/gsm/Makefile
index 86f25f11af2ef5893d588af3ace839eccdaa84ed..6b34233b205cb64043276dffd998003d5459400a 100755
--- a/codecs/gsm/Makefile
+++ b/codecs/gsm/Makefile
@@ -38,6 +38,7 @@ WAV49	= -DWAV49
 ######### probably require gcc. 
 
 ifneq (${OSARCH},Darwin)
+ifneq (${OSARCH},SunOS)
 ifneq (${PROC},x86_64)
 ifneq (${PROC},ultrasparc)
 ifneq ($(shell uname -m),ppc)
@@ -48,6 +49,7 @@ endif
 endif
 endif
 endif
+endif
 
 #The problem with sparc is the best stuff is in newer versions of gcc (post 3.0) only.
 #This works for even old (2.96) versions of gcc and provides a small boost either way.
diff --git a/codecs/lpc10/Makefile b/codecs/lpc10/Makefile
index 23d6886e840463be2fc3d099a1a5e189948e71de..0a2e03008d25e586251dedcfa23e3887ddd709b0 100755
--- a/codecs/lpc10/Makefile
+++ b/codecs/lpc10/Makefile
@@ -39,6 +39,7 @@ ifneq ($(PROC),alpha)
 ifeq ($(PROC),ultrasparc)
         CFLAGS+= -mtune=$(PROC) -mcpu=v8 -O3 -fomit-frame-pointer
 else
+ifneq ($(OSARCH),SunOS)
         CFLAGS+= -march=$(PROC)
 endif
 endif
@@ -46,6 +47,7 @@ endif
 endif
 endif
 endif
+endif
 
 LIB = $(LIB_TARGET_DIR)/liblpc10.a
 
diff --git a/db1-ast/Makefile b/db1-ast/Makefile
index f8192f795996e99e66486b114eed2a5e3de77f99..0aa9fdb0241806c62dc5f9ec24bd8bc85b2bf121 100755
--- a/db1-ast/Makefile
+++ b/db1-ast/Makefile
@@ -48,6 +48,11 @@ clean:
 OORG=	-O2
 CL=	$(CC) -Wall -c -D__DBINTERFACE_PRIVATE $(OORG) -I. -Iinclude
 
+OSTYPE=$(shell uname -s)
+ifeq ($(OSTYPE),SunOS)
+CL+=-I../include -I../include/solaris-compat -DSOLARIS
+endif
+
 db_dump185.o: db_dump185.c
 	$(CL) -o $@ $<
 %.o: hash/%.c
diff --git a/db1-ast/include/db.h b/db1-ast/include/db.h
index d1258d2a63c829e19431ab6d71f13067355ed7ff..a58724bdda8bd17485f1cb88f6806e8928106038 100755
--- a/db1-ast/include/db.h
+++ b/db1-ast/include/db.h
@@ -45,6 +45,10 @@
 #include <compat.h>
 #endif
 
+#ifdef SOLARIS
+#include "solaris-compat/compat.h"
+#endif
+
 #define	RET_ERROR	-1		/* Return values. */
 #define	RET_SUCCESS	 0
 #define	RET_SPECIAL	 1
@@ -53,10 +57,10 @@
 #define	__BIT_TYPES_DEFINED__
 #if (!defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__APPLE__))
 typedef	__signed char		   int8_t;
-typedef	unsigned char		 u_int8_t;
 typedef	short			  int16_t;
-typedef	unsigned short		u_int16_t;
 typedef	int			  int32_t;
+typedef	unsigned char		 u_int8_t;
+typedef	unsigned short		u_int16_t;
 typedef	unsigned int		u_int32_t;
 #ifdef WE_DONT_NEED_QUADS
 typedef	long long		  int64_t;
@@ -65,6 +69,12 @@ typedef	unsigned long long	u_int64_t;
 #endif /* __FreeBSD__ */
 #endif
 
+#ifdef SOLARIS
+#define	__P(p) p
+#define __BEGIN_DECLS
+#define __END_DECLS
+#endif
+
 #define	MAX_PAGE_NUMBER	0xffffffff	/* >= # of pages in a file */
 typedef u_int32_t	pgno_t;
 #define	MAX_PAGE_OFFSET	65535		/* >= # of bytes in a page */
diff --git a/editline/Makefile.in b/editline/Makefile.in
index a18efd78067760f1513055b951f5050c6a1af55a..67ae6ad4047b244fbcbb43c9997a1cc6d97fa1be 100755
--- a/editline/Makefile.in
+++ b/editline/Makefile.in
@@ -2,6 +2,8 @@
 # Generic Makefile for libedit.
 #
 
+OSTYPE=$(shell uname -s)
+
 SHELL = /bin/sh
 
 CC = @CC@
@@ -18,6 +20,10 @@ LIBS = @LIBS@
 INSTALL = @INSTALL@
 PREFIX = @prefix@
 
+ifeq ($(OSTYPE),SunOS)
+CFLAGS+=-DSOLARIS -I../include/solaris-compat
+endif
+
 # .c files.
 ACSRCS = @ACSRCS@
 BCSRCS = @BCSRCS@
diff --git a/editline/np/vis.c b/editline/np/vis.c
index 259f03bd68a52aacc5c2e333c5a0f1330c900829..97406cdf8c8521fc25f98260e8b02b3859737a25 100755
--- a/editline/np/vis.c
+++ b/editline/np/vis.c
@@ -67,6 +67,10 @@ __weak_alias(vis,_vis)
 #define BELL '\007'
 #endif
 
+#ifdef SOLARIS
+typedef unsigned int	u_int32_t;
+#endif
+
 #define isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
 #define iswhite(c)	(c == ' ' || c == '\t' || c == '\n')
 #define issafe(c)	(c == '\b' || c == BELL || c == '\r')
diff --git a/editline/sys.h b/editline/sys.h
index 9970f18f8f2a89651321390d2cb5837e10dfdbcb..a306d1df7b2d883eca33f2427071f1c15c180f20 100755
--- a/editline/sys.h
+++ b/editline/sys.h
@@ -96,10 +96,12 @@ typedef void (*sig_t)(int);
 /*
  * Broken hdrs.
  */
+#ifndef SOLARIS
 extern int	tgetent(const char *bp, char *name);
 extern int	tgetflag(const char *id);
 extern int	tgetnum(const char *id);
 extern char    *tgetstr(const char *id, char **area);
+#endif
 extern char    *tgoto(const char *cap, int col, int row);
 extern int	tputs(const char *str, int affcnt, int (*putc)(int));
 extern char    *getenv(const char *);
diff --git a/formats/format_g726.c b/formats/format_g726.c
index 172b4cd41d7d96c78e5d19e7ddc9261f47029501..16c45b7657f7b63153c01b803c688baf6e82efa1 100755
--- a/formats/format_g726.c
+++ b/formats/format_g726.c
@@ -27,8 +27,12 @@
 #ifdef __linux__
 #include <endian.h>
 #else
+#ifdef SOLARIS
+#include "solaris-compat/compat.h"
+#else
 #include <machine/endian.h>
 #endif
+#endif
 
 #define	RATE_40		0
 #define	RATE_32		1
diff --git a/formats/format_g729.c b/formats/format_g729.c
index be41781dddfa903c9aff3717169b1689628a91fb..7c268996b32285486b454eed1bda811f91e29ea9 100755
--- a/formats/format_g729.c
+++ b/formats/format_g729.c
@@ -28,8 +28,12 @@
 #ifdef __linux__
 #include <endian.h>
 #else
+#ifdef SOLARIS
+#include "solaris-compat/compat.h"
+#else
 #include <machine/endian.h>
 #endif
+#endif
 
 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
 
diff --git a/formats/format_gsm.c b/formats/format_gsm.c
index a51ee0262d8709067677bdbb2f2dab5b4bbf4c43..cb1a7ab0d384448e911e188821acbf1ebfddcda4 100755
--- a/formats/format_gsm.c
+++ b/formats/format_gsm.c
@@ -28,8 +28,12 @@
 #ifdef __linux__
 #include <endian.h>
 #else
+#ifdef SOLARIS
+#include "solaris-compat/compat.h"
+#else
 #include <machine/endian.h>
 #endif
+#endif
 #include "msgsm.h"
 
 /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
diff --git a/formats/format_h263.c b/formats/format_h263.c
index d2e2e76e53994be6ad622eae9197c7bdd9a09932..17805b2318ae326446c0f68ae020350316f82aa5 100755
--- a/formats/format_h263.c
+++ b/formats/format_h263.c
@@ -28,8 +28,12 @@
 #ifdef __linux__
 #include <endian.h>
 #else
+#ifdef SOLARIS
+#include "solaris-compat/compat.h"
+#else
 #include <machine/endian.h>
 #endif
+#endif
 
 /* Some Ideas for this code came from makeh263e.c by Jeffrey Chilton */
 
diff --git a/formats/format_ilbc.c b/formats/format_ilbc.c
index 5b47ddc673a922405ccdf17eda34f2727f652cd8..f92018cf25b5a466feddfe255f69471503709ca7 100755
--- a/formats/format_ilbc.c
+++ b/formats/format_ilbc.c
@@ -30,8 +30,12 @@
 #ifdef __linux__
 #include <endian.h>
 #else
+#ifdef SOLARIS
+#include "solaris-compat/compat.h"
+#else
 #include <machine/endian.h>
 #endif
+#endif
 
 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
 
diff --git a/formats/format_jpeg.c b/formats/format_jpeg.c
index 56c5ef8d8092b0cb6e893b76dc0713c0464233ab..b084030096d894c200d2b297432534efe2caaff8 100755
--- a/formats/format_jpeg.c
+++ b/formats/format_jpeg.c
@@ -30,8 +30,12 @@
 #ifdef __linux__
 #include <endian.h>
 #else
+#ifdef SOLARIS
+#include "solaris-compat/compat.h"
+#else
 #include <machine/endian.h>
 #endif
+#endif
 
 
 static char *desc = "JPEG (Joint Picture Experts Group) Image Format";
diff --git a/formats/format_pcm.c b/formats/format_pcm.c
index 3e037ca95bb9b1f26c7cc7ac6a9f90ce226b67b9..6d012717723ecb152b0a64b5a229210c73d22f60 100755
--- a/formats/format_pcm.c
+++ b/formats/format_pcm.c
@@ -28,8 +28,12 @@
 #ifdef __linux__
 #include <endian.h>
 #else
+#ifdef SOLARIS
+#include "solaris-compat/compat.h"
+#else
 #include <machine/endian.h>
 #endif
+#endif
 
 #define BUF_SIZE 160		/* 160 samples */
 
diff --git a/formats/format_pcm_alaw.c b/formats/format_pcm_alaw.c
index d828aedcc76e27fad460235dfcb1d95e7e05ce63..866cd236070a12543863d1939497408ad7a0bccf 100755
--- a/formats/format_pcm_alaw.c
+++ b/formats/format_pcm_alaw.c
@@ -30,8 +30,12 @@
 #ifdef __linux__
 #include <endian.h>
 #else
+#ifdef SOLARIS
+#include "solaris-compat/compat.h"
+#else
 #include <machine/endian.h>
 #endif
+#endif
 
 #define BUF_SIZE 160		/* 160 samples */
 
diff --git a/formats/format_sln.c b/formats/format_sln.c
index 0a52c10f0cde90d495bf0b9f0e88dc35dd10eeae..639905e01de8d82fd2ecc0afd6ba69f652be0ce9 100755
--- a/formats/format_sln.c
+++ b/formats/format_sln.c
@@ -26,8 +26,12 @@
 #ifdef __linux__
 #include <endian.h>
 #else
+#ifdef SOLARIS
+#include "solaris-compat/compat.h"
+#else
 #include <machine/endian.h>
 #endif
+#endif
 
 #define BUF_SIZE 320		/* 320 samples */
 
diff --git a/formats/format_vox.c b/formats/format_vox.c
index 78afc6598a31156c2063380ab2c0a77357e7d0c6..7601994fd528a93ec718046dee2abf74f58d2d1f 100755
--- a/formats/format_vox.c
+++ b/formats/format_vox.c
@@ -28,8 +28,12 @@
 #ifdef __linux__
 #include <endian.h>
 #else
+#ifdef SOLARIS
+#include "solaris-compat/compat.h"
+#else
 #include <machine/endian.h>
 #endif
+#endif
 
 #define BUF_SIZE 80		/* 160 samples */
 
diff --git a/formats/format_wav.c b/formats/format_wav.c
index 3755539136e88f0813725752cd890e8b70fd9c7a..be6053447354e0061e34df2af9072e01e3d14571 100755
--- a/formats/format_wav.c
+++ b/formats/format_wav.c
@@ -28,8 +28,12 @@
 #ifdef __linux__
 #include <endian.h>
 #else
+#ifdef SOLARIS
+#include "solaris-compat/compat.h"
+#else
 #include <machine/endian.h>
 #endif
+#endif
 
 /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
 
diff --git a/formats/format_wav_gsm.c b/formats/format_wav_gsm.c
index c46c819ddbe08c2d5ee9f81906fd759a8e544793..a34ae929da38807ee36779262abb757f8c1bfd7b 100755
--- a/formats/format_wav_gsm.c
+++ b/formats/format_wav_gsm.c
@@ -28,8 +28,12 @@
 #ifdef __linux__
 #include <endian.h>
 #else
+#ifdef SOLARIS
+#include "solaris-compat/compat.h"
+#else
 #include <machine/endian.h>
 #endif
+#endif
 #include "msgsm.h"
 
 /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
diff --git a/include/asterisk/channel.h b/include/asterisk/channel.h
index 7c129f73ccd7aeb950f76d670f01fda3eab87bd0..28c566364c991307d14ca4deff045771aae4295c 100755
--- a/include/asterisk/channel.h
+++ b/include/asterisk/channel.h
@@ -829,6 +829,19 @@ static inline int ast_fdisset(struct pollfd *pfds, int fd, int max, int *start)
 	return 0;
 }
 
+#ifdef SOLARIS
+static inline void timersub(struct timeval *tvend, struct timeval *tvstart, struct timeval *tvdiff)
+{
+	tvdiff->tv_sec = tvend->tv_sec - tvstart->tv_sec;
+	tvdiff->tv_usec = tvend->tv_usec - tvstart->tv_usec;
+	if (tvdiff->tv_usec < 0) {
+		tvdiff->tv_sec --;
+		tvdiff->tv_usec += 1000000;
+	}
+
+}
+#endif
+
 //! Waits for activity on a group of channels
 /*! 
  * \param nfds the maximum number of file descriptors in the sets
diff --git a/include/asterisk/frame.h b/include/asterisk/frame.h
index 784bea39c859a9ad84d25d5df35559e6e22594f0..1d59fee13e3a52c05da089a0e1dd22cde678e9c9 100755
--- a/include/asterisk/frame.h
+++ b/include/asterisk/frame.h
@@ -22,6 +22,10 @@ extern "C" {
 
 #include <sys/types.h>
 #include <sys/time.h>
+
+#ifdef SOLARIS
+#include "solaris-compat/compat.h"
+#endif
 	
 /*
  * Autodetect system endianess
diff --git a/include/asterisk/lock.h b/include/asterisk/lock.h
index f2066de38a1bf4b34e9f16a35ca20eac067a1c28..81a31affa7e4873d9451b03b1c7e134b2f835111 100755
--- a/include/asterisk/lock.h
+++ b/include/asterisk/lock.h
@@ -48,6 +48,10 @@
 #define AST_MUTEX_KIND			PTHREAD_MUTEX_RECURSIVE
 #endif /* PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP */
 
+#ifdef SOLARIS
+#define AST_MUTEX_INIT_W_CONSTRUCTORS
+#endif
+
 #ifdef DEBUG_THREADS
 
 #ifdef THREAD_CRASH
diff --git a/include/solaris-compat/compat.h b/include/solaris-compat/compat.h
new file mode 100755
index 0000000000000000000000000000000000000000..6b304b2baea76c85b06566abfa1b3538d4673585
--- /dev/null
+++ b/include/solaris-compat/compat.h
@@ -0,0 +1,34 @@
+#ifndef _SOLARIS_COMPAT_H
+#define _SOLARIS_COMPAT_H
+
+#define __BEGIN_DECLS
+#define __END_DECLS
+
+#ifndef __P
+#define __P(p) p
+#endif
+
+#define LITTLE_ENDIAN 1234
+#define BIG_ENDIAN 4321
+
+#define __LITTLE_ENDIAN 1234
+#define __BIG_ENDIAN 4321
+
+#ifdef __sparc__
+#define BYTE_ORDER BIG_ENDIAN
+#define __BYTE_ORDER BIG_ENDIAN
+#else
+#define BYTE_ORDER LITTLE_ENDIAN
+#define ____BYTE_ORDER BIG_ENDIAN
+#endif
+
+#ifndef __BIT_TYPES_DEFINED__
+#define __BIT_TYPES_DEFINED__
+typedef unsigned char	u_int8_t;
+typedef unsigned short	u_int16_t;
+typedef unsigned int	u_int32_t;
+#endif
+
+int setenv(const char *name, const char *value, int overwrite);
+
+#endif
diff --git a/include/solaris-compat/sys/cdefs.h b/include/solaris-compat/sys/cdefs.h
new file mode 100755
index 0000000000000000000000000000000000000000..40f76af87618c554f724d702dda7e1afdaae3022
--- /dev/null
+++ b/include/solaris-compat/sys/cdefs.h
@@ -0,0 +1,10 @@
+#ifndef __SYS_CDEFS_H_
+#define __SYS_CDEFS_H_
+
+#define __BEGIN_DECLS
+#define __END_DECLS
+
+#define __P(p) p
+
+
+#endif
diff --git a/include/solaris-compat/sys/queue.h b/include/solaris-compat/sys/queue.h
new file mode 100755
index 0000000000000000000000000000000000000000..ac273dfe381d7a6b8c38d44b2e4c2a86e7155d0c
--- /dev/null
+++ b/include/solaris-compat/sys/queue.h
@@ -0,0 +1,540 @@
+/*
+ * Copyright (c) 1991, 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.
+ *
+ *      @(#)queue.h     8.5 (Berkeley) 8/20/94
+ * $FreeBSD: src/sys/sys/queue.h,v 1.24.2.4 2000/05/05 01:41:41 archie Exp $
+ */
+
+#ifndef _SYS_QUEUE_H_
+#define _SYS_QUEUE_H_
+
+/*
+ * This file defines five types of data structures: singly-linked lists,
+ * singly-linked tail queues, lists, tail queues, and circular queues.
+ *
+ * A singly-linked list is headed by a single forward pointer. The elements
+ * are singly linked for minimum space and pointer manipulation overhead at
+ * the expense of O(n) removal for arbitrary elements. New elements can be
+ * added to the list after an existing element or at the head of the list.
+ * Elements being removed from the head of the list should use the explicit
+ * macro for this purpose for optimum efficiency. A singly-linked list may
+ * only be traversed in the forward direction.  Singly-linked lists are ideal
+ * for applications with large datasets and few or no removals or for
+ * implementing a LIFO queue.
+ *
+ * A singly-linked tail queue is headed by a pair of pointers, one to the
+ * head of the list and the other to the tail of the list. The elements are
+ * singly linked for minimum space and pointer manipulation overhead at the
+ * expense of O(n) removal for arbitrary elements. New elements can be added
+ * to the list after an existing element, at the head of the list, or at the
+ * end of the list. Elements being removed from the head of the tail queue
+ * should use the explicit macro for this purpose for optimum efficiency.
+ * A singly-linked tail queue may only be traversed in the forward direction.
+ * Singly-linked tail queues are ideal for applications with large datasets
+ * and few or no removals or for implementing a FIFO queue.
+ *
+ * A list is headed by a single forward pointer (or an array of forward
+ * pointers for a hash table header). The elements are doubly linked
+ * so that an arbitrary element can be removed without a need to
+ * traverse the list. New elements can be added to the list before
+ * or after an existing element or at the head of the list. A list
+ * may only be traversed in the forward direction.
+ *
+ * A tail queue is headed by a pair of pointers, one to the head of the
+ * list and the other to the tail of the list. The elements are doubly
+ * linked so that an arbitrary element can be removed without a need to
+ * traverse the list. New elements can be added to the list before or
+ * after an existing element, at the head of the list, or at the end of
+ * the list. A tail queue may be traversed in either direction.
+ *
+ * A circle queue is headed by a pair of pointers, one to the head of the
+ * list and the other to the tail of the list. The elements are doubly
+ * linked so that an arbitrary element can be removed without a need to
+ * traverse the list. New elements can be added to the list before or after
+ * an existing element, at the head of the list, or at the end of the list.
+ * A circle queue may be traversed in either direction, but has a more
+ * complex end of list detection.
+ *
+ * For details on the use of these macros, see the queue(3) manual page.
+ *
+ *
+ *                      SLIST   LIST    STAILQ  TAILQ   CIRCLEQ
+ * _HEAD                +       +       +       +       +
+ * _ENTRY               +       +       +       +       +
+ * _INIT                +       +       +       +       +
+ * _EMPTY               +       +       +       +       +
+ * _FIRST               +       +       +       +       +
+ * _NEXT                +       +       +       +       +
+ * _PREV                -       -       -       +       +
+ * _LAST                -       -       +       +       +
+ * _FOREACH             +       +       +       +       +
+ * _FOREACH_REVERSE     -       -       -       +       +
+ * _INSERT_HEAD         +       +       +       +       +
+ * _INSERT_BEFORE       -       +       -       +       +
+ * _INSERT_AFTER        +       +       +       +       +
+ * _INSERT_TAIL         -       -       +       +       +
+ * _REMOVE_HEAD         +       -       +       -       -
+ * _REMOVE              +       +       +       +       +
+ *
+ */
+
+/*
+ * Singly-linked List definitions.
+ */
+#define SLIST_HEAD(name, type)                                          \
+struct name {                                                           \
+        struct type *slh_first; /* first element */                     \
+}
+
+#define SLIST_HEAD_INITIALIZER(head)                                    \
+        { NULL }
+ 
+#define SLIST_ENTRY(type)                                               \
+struct {                                                                \
+        struct type *sle_next;  /* next element */                      \
+}
+ 
+/*
+ * Singly-linked List functions.
+ */
+#define SLIST_EMPTY(head)       ((head)->slh_first == NULL)
+
+#define SLIST_FIRST(head)       ((head)->slh_first)
+
+#define SLIST_FOREACH(var, head, field)                                 \
+        for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
+
+#define SLIST_INIT(head) {                                              \
+        (head)->slh_first = NULL;                                       \
+}
+
+#define SLIST_INSERT_AFTER(slistelm, elm, field) do {                   \
+        (elm)->field.sle_next = (slistelm)->field.sle_next;             \
+        (slistelm)->field.sle_next = (elm);                             \
+} while (0)
+
+#define SLIST_INSERT_HEAD(head, elm, field) do {                        \
+        (elm)->field.sle_next = (head)->slh_first;                      \
+        (head)->slh_first = (elm);                                      \
+} while (0)
+
+#define SLIST_NEXT(elm, field)  ((elm)->field.sle_next)
+
+#define SLIST_REMOVE_HEAD(head, field) do {                             \
+        (head)->slh_first = (head)->slh_first->field.sle_next;          \
+} while (0)
+
+#define SLIST_REMOVE(head, elm, type, field) do {                       \
+        if ((head)->slh_first == (elm)) {                               \
+                SLIST_REMOVE_HEAD((head), field);                       \
+        }                                                               \
+        else {                                                          \
+                struct type *curelm = (head)->slh_first;                \
+                while( curelm->field.sle_next != (elm) )                \
+                        curelm = curelm->field.sle_next;                \
+                curelm->field.sle_next =                                \
+                    curelm->field.sle_next->field.sle_next;             \
+        }                                                               \
+} while (0)
+
+/*
+ * Singly-linked Tail queue definitions.
+ */
+#define STAILQ_HEAD(name, type)                                         \
+struct name {                                                           \
+        struct type *stqh_first;/* first element */                     \
+        struct type **stqh_last;/* addr of last next element */         \
+}
+
+#define STAILQ_HEAD_INITIALIZER(head)                                   \
+        { NULL, &(head).stqh_first }
+
+#define STAILQ_ENTRY(type)                                              \
+struct {                                                                \
+        struct type *stqe_next; /* next element */                      \
+}
+
+/*
+ * Singly-linked Tail queue functions.
+ */
+#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
+
+#define STAILQ_INIT(head) do {                                          \
+        (head)->stqh_first = NULL;                                      \
+        (head)->stqh_last = &(head)->stqh_first;                        \
+} while (0)
+
+#define STAILQ_FIRST(head)      ((head)->stqh_first)
+#define STAILQ_LAST(head)       (*(head)->stqh_last)
+
+#define STAILQ_FOREACH(var, head, field)                                \
+        for((var) = (head)->stqh_first; (var); (var) = (var)->field.stqe_next)
+
+#define STAILQ_INSERT_HEAD(head, elm, field) do {                       \
+        if (((elm)->field.stqe_next = (head)->stqh_first) == NULL)      \
+                (head)->stqh_last = &(elm)->field.stqe_next;            \
+        (head)->stqh_first = (elm);                                     \
+} while (0)
+
+#define STAILQ_INSERT_TAIL(head, elm, field) do {                       \
+        (elm)->field.stqe_next = NULL;                                  \
+        *(head)->stqh_last = (elm);                                     \
+        (head)->stqh_last = &(elm)->field.stqe_next;                    \
+} while (0)
+
+#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {               \
+        if (((elm)->field.stqe_next = (tqelm)->field.stqe_next) == NULL)\
+                (head)->stqh_last = &(elm)->field.stqe_next;            \
+        (tqelm)->field.stqe_next = (elm);                               \
+} while (0)
+
+#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
+
+#define STAILQ_REMOVE_HEAD(head, field) do {                            \
+        if (((head)->stqh_first =                                       \
+             (head)->stqh_first->field.stqe_next) == NULL)              \
+                (head)->stqh_last = &(head)->stqh_first;                \
+} while (0)
+
+#define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do {                 \
+        if (((head)->stqh_first = (elm)->field.stqe_next) == NULL)      \
+                (head)->stqh_last = &(head)->stqh_first;                \
+} while (0)
+
+#define STAILQ_REMOVE(head, elm, type, field) do {                      \
+        if ((head)->stqh_first == (elm)) {                              \
+                STAILQ_REMOVE_HEAD(head, field);                        \
+        }                                                               \
+        else {                                                          \
+                struct type *curelm = (head)->stqh_first;               \
+                while( curelm->field.stqe_next != (elm) )               \
+                        curelm = curelm->field.stqe_next;               \
+                if((curelm->field.stqe_next =                           \
+                    curelm->field.stqe_next->field.stqe_next) == NULL)  \
+                        (head)->stqh_last = &(curelm)->field.stqe_next; \
+        }                                                               \
+} while (0)
+
+/*
+ * List definitions.
+ */
+#define LIST_HEAD(name, type)                                           \
+struct name {                                                           \
+        struct type *lh_first;  /* first element */                     \
+}
+
+#define LIST_HEAD_INITIALIZER(head)                                     \
+        { NULL }
+
+#define LIST_ENTRY(type)                                                \
+struct {                                                                \
+        struct type *le_next;   /* next element */                      \
+        struct type **le_prev;  /* address of previous next element */  \
+}
+
+/*
+ * List functions.
+ */
+
+#define LIST_EMPTY(head) ((head)->lh_first == NULL)
+
+#define LIST_FIRST(head)        ((head)->lh_first)
+
+#define LIST_FOREACH(var, head, field)                                  \
+        for((var) = (head)->lh_first; (var); (var) = (var)->field.le_next)
+
+#define LIST_INIT(head) do {                                            \
+        (head)->lh_first = NULL;                                        \
+} while (0)
+
+#define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
+        if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)  \
+                (listelm)->field.le_next->field.le_prev =               \
+                    &(elm)->field.le_next;                              \
+        (listelm)->field.le_next = (elm);                               \
+        (elm)->field.le_prev = &(listelm)->field.le_next;               \
+} while (0)
+
+#define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
+        (elm)->field.le_prev = (listelm)->field.le_prev;                \
+        (elm)->field.le_next = (listelm);                               \
+        *(listelm)->field.le_prev = (elm);                              \
+        (listelm)->field.le_prev = &(elm)->field.le_next;               \
+} while (0)
+
+#define LIST_INSERT_HEAD(head, elm, field) do {                         \
+        if (((elm)->field.le_next = (head)->lh_first) != NULL)          \
+                (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
+        (head)->lh_first = (elm);                                       \
+        (elm)->field.le_prev = &(head)->lh_first;                       \
+} while (0)
+
+#define LIST_NEXT(elm, field)   ((elm)->field.le_next)
+
+#define LIST_REMOVE(elm, field) do {                                    \
+        if ((elm)->field.le_next != NULL)                               \
+                (elm)->field.le_next->field.le_prev =                   \
+                    (elm)->field.le_prev;                               \
+        *(elm)->field.le_prev = (elm)->field.le_next;                   \
+} while (0)
+
+/*
+ * Tail queue definitions.
+ */
+#define TAILQ_HEAD(name, type)                                          \
+struct name {                                                           \
+        struct type *tqh_first; /* first element */                     \
+        struct type **tqh_last; /* addr of last next element */         \
+}
+
+#define TAILQ_HEAD_INITIALIZER(head)                                    \
+        { NULL, &(head).tqh_first }
+
+#define TAILQ_ENTRY(type)                                               \
+struct {                                                                \
+        struct type *tqe_next;  /* next element */                      \
+        struct type **tqe_prev; /* address of previous next element */  \
+}
+
+/*
+ * Tail queue functions.
+ */
+#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
+
+#define TAILQ_FOREACH(var, head, field)                                 \
+        for (var = TAILQ_FIRST(head); var; var = TAILQ_NEXT(var, field))
+
+#define TAILQ_FOREACH_REVERSE(var, head, headname, field)               \
+        for ((var) = TAILQ_LAST((head), headname);                      \
+             (var);                                                     \
+             (var) = TAILQ_PREV((var), headname, field))
+
+#define TAILQ_FIRST(head) ((head)->tqh_first)
+
+#define TAILQ_LAST(head, headname) \
+        (*(((struct headname *)((head)->tqh_last))->tqh_last))
+
+#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
+
+#define TAILQ_PREV(elm, headname, field) \
+        (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
+
+#define TAILQ_INIT(head) do {                                           \
+        (head)->tqh_first = NULL;                                       \
+        (head)->tqh_last = &(head)->tqh_first;                          \
+} while (0)
+
+#define TAILQ_INSERT_HEAD(head, elm, field) do {                        \
+        if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)        \
+                (head)->tqh_first->field.tqe_prev =                     \
+                    &(elm)->field.tqe_next;                             \
+        else                                                            \
+                (head)->tqh_last = &(elm)->field.tqe_next;              \
+        (head)->tqh_first = (elm);                                      \
+        (elm)->field.tqe_prev = &(head)->tqh_first;                     \
+} while (0)
+
+#define TAILQ_INSERT_TAIL(head, elm, field) do {                        \
+        (elm)->field.tqe_next = NULL;                                   \
+        (elm)->field.tqe_prev = (head)->tqh_last;                       \
+        *(head)->tqh_last = (elm);                                      \
+        (head)->tqh_last = &(elm)->field.tqe_next;                      \
+} while (0)
+
+#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {              \
+        if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
+                (elm)->field.tqe_next->field.tqe_prev =                 \
+                    &(elm)->field.tqe_next;                             \
+        else                                                            \
+                (head)->tqh_last = &(elm)->field.tqe_next;              \
+        (listelm)->field.tqe_next = (elm);                              \
+        (elm)->field.tqe_prev = &(listelm)->field.tqe_next;             \
+} while (0)
+
+#define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                   \
+        (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
+        (elm)->field.tqe_next = (listelm);                              \
+        *(listelm)->field.tqe_prev = (elm);                             \
+        (listelm)->field.tqe_prev = &(elm)->field.tqe_next;             \
+} while (0)
+
+#define TAILQ_REMOVE(head, elm, field) do {                             \
+        if (((elm)->field.tqe_next) != NULL)                            \
+                (elm)->field.tqe_next->field.tqe_prev =                 \
+                    (elm)->field.tqe_prev;                              \
+        else                                                            \
+                (head)->tqh_last = (elm)->field.tqe_prev;               \
+        *(elm)->field.tqe_prev = (elm)->field.tqe_next;                 \
+} while (0)
+
+/*
+ * Circular queue definitions.
+ */
+#define CIRCLEQ_HEAD(name, type)                                        \
+struct name {                                                           \
+        struct type *cqh_first;         /* first element */             \
+        struct type *cqh_last;          /* last element */              \
+}
+
+#define CIRCLEQ_ENTRY(type)                                             \
+struct {                                                                \
+        struct type *cqe_next;          /* next element */              \
+        struct type *cqe_prev;          /* previous element */          \
+}
+
+/*
+ * Circular queue functions.
+ */
+#define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
+
+#define CIRCLEQ_FIRST(head) ((head)->cqh_first)
+
+#define CIRCLEQ_FOREACH(var, head, field)                               \
+        for((var) = (head)->cqh_first;                                  \
+            (var) != (void *)(head);                                    \
+            (var) = (var)->field.cqe_next)
+
+#define CIRCLEQ_FOREACH_REVERSE(var, head, field)                       \
+        for((var) = (head)->cqh_last;                                   \
+            (var) != (void *)(head);                                    \
+            (var) = (var)->field.cqe_prev)
+
+#define CIRCLEQ_INIT(head) do {                                         \
+        (head)->cqh_first = (void *)(head);                             \
+        (head)->cqh_last = (void *)(head);                              \
+} while (0)
+
+#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {            \
+        (elm)->field.cqe_next = (listelm)->field.cqe_next;              \
+        (elm)->field.cqe_prev = (listelm);                              \
+        if ((listelm)->field.cqe_next == (void *)(head))                \
+                (head)->cqh_last = (elm);                               \
+        else                                                            \
+                (listelm)->field.cqe_next->field.cqe_prev = (elm);      \
+        (listelm)->field.cqe_next = (elm);                              \
+} while (0)
+
+#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {           \
+        (elm)->field.cqe_next = (listelm);                              \
+        (elm)->field.cqe_prev = (listelm)->field.cqe_prev;              \
+        if ((listelm)->field.cqe_prev == (void *)(head))                \
+                (head)->cqh_first = (elm);                              \
+        else                                                            \
+                (listelm)->field.cqe_prev->field.cqe_next = (elm);      \
+        (listelm)->field.cqe_prev = (elm);                              \
+} while (0)
+
+#define CIRCLEQ_INSERT_HEAD(head, elm, field) do {                      \
+        (elm)->field.cqe_next = (head)->cqh_first;                      \
+        (elm)->field.cqe_prev = (void *)(head);                         \
+        if ((head)->cqh_last == (void *)(head))                         \
+                (head)->cqh_last = (elm);                               \
+        else                                                            \
+                (head)->cqh_first->field.cqe_prev = (elm);              \
+        (head)->cqh_first = (elm);                                      \
+} while (0)
+
+#define CIRCLEQ_INSERT_TAIL(head, elm, field) do {                      \
+        (elm)->field.cqe_next = (void *)(head);                         \
+        (elm)->field.cqe_prev = (head)->cqh_last;                       \
+        if ((head)->cqh_first == (void *)(head))                        \
+                (head)->cqh_first = (elm);                              \
+        else                                                            \
+                (head)->cqh_last->field.cqe_next = (elm);               \
+        (head)->cqh_last = (elm);                                       \
+} while (0)
+
+#define CIRCLEQ_LAST(head) ((head)->cqh_last)
+
+#define CIRCLEQ_NEXT(elm,field) ((elm)->field.cqe_next)
+
+#define CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev)
+
+#define CIRCLEQ_REMOVE(head, elm, field) do {                           \
+        if ((elm)->field.cqe_next == (void *)(head))                    \
+                (head)->cqh_last = (elm)->field.cqe_prev;               \
+        else                                                            \
+                (elm)->field.cqe_next->field.cqe_prev =                 \
+                    (elm)->field.cqe_prev;                              \
+        if ((elm)->field.cqe_prev == (void *)(head))                    \
+                (head)->cqh_first = (elm)->field.cqe_next;              \
+        else                                                            \
+                (elm)->field.cqe_prev->field.cqe_next =                 \
+                    (elm)->field.cqe_next;                              \
+} while (0)
+
+#ifdef KERNEL
+
+/*
+ * XXX insque() and remque() are an old way of handling certain queues.
+ * They bogusly assumes that all queue heads look alike.
+ */
+
+struct quehead {
+        struct quehead *qh_link;
+        struct quehead *qh_rlink;
+};
+
+#ifdef  __GNUC__
+
+static __inline void
+insque(void *a, void *b)
+{
+        struct quehead *element = a, *head = b;
+
+        element->qh_link = head->qh_link;
+        element->qh_rlink = head;
+        head->qh_link = element;
+        element->qh_link->qh_rlink = element;
+}
+
+static __inline void
+remque(void *a)
+{
+        struct quehead *element = a;
+
+        element->qh_link->qh_rlink = element->qh_rlink;
+        element->qh_rlink->qh_link = element->qh_link;
+        element->qh_rlink = 0;
+}
+
+#else /* !__GNUC__ */
+
+void    insque __P((void *a, void *b));
+void    remque __P((void *a));
+
+#endif /* __GNUC__ */
+
+#endif /* KERNEL */
+
+#endif /* !_SYS_QUEUE_H_ */
+
diff --git a/logger.c b/logger.c
index e9e75b8310b0345061bdc581e3a761924d4a8c20..5fb0348afd17238cd0b5fce9d795249fe8033938 100755
--- a/logger.c
+++ b/logger.c
@@ -136,7 +136,9 @@ static struct logchannel *make_logchannel(char *channel, char *components, int l
 {
 	struct logchannel *chan;
 	char *facility;
+#ifndef SOLARIS
 	CODE *cptr;
+#endif
 
 	if (ast_strlen_zero(channel))
 		return NULL;
@@ -155,6 +157,8 @@ static struct logchannel *make_logchannel(char *channel, char *components, int l
 		    if(!facility++ || !facility) {
 			facility = "local0";
 		    }
+
+#ifndef SOLARIS
 		    /*
 		     * Walk through the list of facilitynames (defined in sys/syslog.h)
 		     * to see if we can find the one we have been given
@@ -168,6 +172,46 @@ static struct logchannel *make_logchannel(char *channel, char *components, int l
 			}
 			cptr++;
 		    }
+#else
+		    chan->facility = -1;
+		    if (!strcasecmp(facility, "kern")) 
+			chan->facility = LOG_KERN;
+		    else if (!strcasecmp(facility, "USER")) 
+			chan->facility = LOG_USER;
+		    else if (!strcasecmp(facility, "MAIL")) 
+			chan->facility = LOG_MAIL;
+		    else if (!strcasecmp(facility, "DAEMON")) 
+			chan->facility = LOG_DAEMON;
+		    else if (!strcasecmp(facility, "AUTH")) 
+			chan->facility = LOG_AUTH;
+		    else if (!strcasecmp(facility, "SYSLOG")) 
+			chan->facility = LOG_SYSLOG;
+		    else if (!strcasecmp(facility, "LPR")) 
+			chan->facility = LOG_LPR;
+		    else if (!strcasecmp(facility, "NEWS")) 
+			chan->facility = LOG_NEWS;
+		    else if (!strcasecmp(facility, "UUCP")) 
+			chan->facility = LOG_UUCP;
+		    else if (!strcasecmp(facility, "CRON")) 
+			chan->facility = LOG_CRON;
+		    else if (!strcasecmp(facility, "LOCAL0")) 
+			chan->facility = LOG_LOCAL0;
+		    else if (!strcasecmp(facility, "LOCAL1")) 
+			chan->facility = LOG_LOCAL1;
+		    else if (!strcasecmp(facility, "LOCAL2")) 
+			chan->facility = LOG_LOCAL2;
+		    else if (!strcasecmp(facility, "LOCAL3")) 
+			chan->facility = LOG_LOCAL3;
+		    else if (!strcasecmp(facility, "LOCAL4")) 
+			chan->facility = LOG_LOCAL4;
+		    else if (!strcasecmp(facility, "LOCAL5")) 
+			chan->facility = LOG_LOCAL5;
+		    else if (!strcasecmp(facility, "LOCAL6")) 
+			chan->facility = LOG_LOCAL6;
+		    else if (!strcasecmp(facility, "LOCAL7")) 
+			chan->facility = LOG_LOCAL7;
+#endif
+
 		    if (0 > chan->facility) {
 			fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
 			free(chan);
diff --git a/md5.c b/md5.c
index 17a00c6fcb0e8ba5c966a404dccca62eefb6c697..d39e0fe1a2019b88f9b49d68c5823bf8ecf3ce3c 100755
--- a/md5.c
+++ b/md5.c
@@ -8,6 +8,9 @@
 #  include <sys/endian.h>
 #elif defined( BSD ) && ( BSD >= 199103 ) || defined(__APPLE__)
 #  include <machine/endian.h>
+#elif defined( __sparc__ ) && defined( SOLARIS )
+#  define BIG_ENDIAN 4321
+#  define BYTE_ORDER BIG_ENDIAN
 #else
 #  include <endian.h>
 #endif
diff --git a/mkdep b/mkdep
index 94e6e482aa9c558b9d8309ba883a55b2b513bdce..3f30b51ac0db4a889b10b9f3b2674cf73484ed87 100755
--- a/mkdep
+++ b/mkdep
@@ -1,4 +1,4 @@
-#!/bin/sh -
+#!/bin/bash -
 #
 #	$OpenBSD: mkdep.gcc.sh,v 1.8 1998/09/02 06:40:07 deraadt Exp $
 #	$NetBSD: mkdep.gcc.sh,v 1.9 1994/12/23 07:34:59 jtc Exp $
diff --git a/mkpkgconfig b/mkpkgconfig
index a4f143bed1645b5ef7576a7288fe0855f2f80d3c..3b097a9fe678cf260fd15b8dd5864a2cd5686ea1 100755
--- a/mkpkgconfig
+++ b/mkpkgconfig
@@ -2,7 +2,7 @@
 PPATH=$1
 ## Make sure we were called from Makefile
 
-if [ "x$ASTERISKVERSIONNUM" == "x" ]; then
+if [ "x$ASTERISKVERSIONNUM" = "x" ]; then
   echo " ** Do not call this script directly"
   exit
 fi
diff --git a/pbx/pbx_dundi.c b/pbx/pbx_dundi.c
index 0bc2c7996f9c791ddd75cdabb4406078d7d97cf3..b68a1050add91dccfe897c9541f601886ac85713 100755
--- a/pbx/pbx_dundi.c
+++ b/pbx/pbx_dundi.c
@@ -39,7 +39,7 @@
 #include <sys/socket.h>
 #include <string.h>
 #include <errno.h>
-#if defined(__FreeBSD__) || defined(__NetBSD__)
+#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(SOLARIS)
 #include <sys/types.h>
 #include <netinet/in_systm.h>
 #endif
@@ -406,7 +406,7 @@ static void reset_global_eid(void)
 		close(s);
 	}
 #else
-#if defined(ifa_broadaddr)
+#if defined(ifa_broadaddr) && !defined(SOLARIS)
 	char eid_str[20];
 	struct ifaddrs *ifap;
 	
@@ -4518,14 +4518,14 @@ static int set_config(char *config_file, struct sockaddr_in* sin)
 				tos = IPTOS_THROUGHPUT;
 			else if (!strcasecmp(v->value, "reliability"))
 				tos = IPTOS_RELIABILITY;
-#if !defined(__NetBSD__)
+#if !defined(__NetBSD__) && !defined(SOLARIS)
 			else if (!strcasecmp(v->value, "mincost"))
 				tos = IPTOS_MINCOST;
 #endif
 			else if (!strcasecmp(v->value, "none"))
 				tos = 0;
 			else
-#if defined(__NetBSD__)
+#if defined(__NetBSD__) && !defined(SOLARIS)
 				ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
 #else
 				ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', or 'none'\n", v->lineno);
diff --git a/rtp.c b/rtp.c
index 55d79ca305cb7b142dfcfc2a0a2ee833d9baa20f..92dea1dcaa43a4a936784b3abee8ddd3d30940f8 100755
--- a/rtp.c
+++ b/rtp.c
@@ -1084,9 +1084,23 @@ int ast_rtp_senddigit(struct ast_rtp *rtp, char digit)
 	return 0;
 }
 
+#ifdef SOLARIS
+static void put_uint32(unsigned char *buf, int i)
+{
+  unsigned char *c = (unsigned char *)&i;
+
+  buf[0] = (i>>24) & 0xff;
+  buf[1] = (i>>16) & 0xff;
+  buf[2] = (i>>8)  & 0xff;
+  buf[3] = i       & 0xff;
+}
+#else
+#define put_uint32(p,v) ((*((unsigned int *)(p))) = (v))
+#endif
+
 static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec)
 {
-	unsigned int *rtpheader;
+	unsigned char *rtpheader;
 	char iabuf[INET_ADDRSTRLEN];
 	int hdrlen = 12;
 	int res;
@@ -1165,10 +1179,14 @@ static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec
 		}
 	}
 	/* Get a pointer to the header */
-	rtpheader = (unsigned int *)(f->data - hdrlen);
-	rtpheader[0] = htonl((2 << 30) | (codec << 16) | (rtp->seqno++) | (mark << 23));
-	rtpheader[1] = htonl(rtp->lastts);
-	rtpheader[2] = htonl(rtp->ssrc); 
+	rtpheader = (unsigned char *)(f->data - hdrlen);
+
+	put_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
+	put_uint32(rtpheader + 4, htonl(rtp->lastts));
+	put_uint32(rtpheader + 8, htonl(rtp->ssrc)); 
+
+	rtp->seqno++;
+
 	if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
 		res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
 		if (res <0) 
diff --git a/say.c b/say.c
index e3b3106dd3a31e2647a0408cd94112e364fe31f7..d8533e953a3e0be4ceb55257ff269798fd43559f 100755
--- a/say.c
+++ b/say.c
@@ -28,6 +28,10 @@
 #include "asterisk.h"
 #include <stdio.h>
 
+#ifdef SOLARIS
+#include <iso/limits_iso.h>
+#endif
+
 
 /* Forward declaration */
 static int wait_file(struct ast_channel *chan, const char *ints, const char *file, const char *lang);
diff --git a/stdtime/localtime.c b/stdtime/localtime.c
index 1f6309e3f430913c17a7314939a540a449882737..2f9d412fda9f576de0eb1412987ef0bc359b9f21 100755
--- a/stdtime/localtime.c
+++ b/stdtime/localtime.c
@@ -113,6 +113,12 @@ struct lsinfo {				/* leap second information */
 #define MY_TZNAME_MAX	255
 #endif /* !defined TZNAME_MAX */
 
+#ifdef SOLARIS
+#undef TM_ZONE
+#undef TM_GMTOFF 
+#endif
+
+
 struct state {
 	char	name[TZ_STRLEN_MAX + 1];
 	int		leapcnt;
@@ -1208,7 +1214,11 @@ const time_t * const	timep;
 char *buf;
 {
         struct tm tm;
+#ifdef SOLARIS
+	return asctime_r(localtime_r(timep, &tm), buf, 256);
+#else
 	return asctime_r(localtime_r(timep, &tm), buf);
+#endif
 }
 
 /*
diff --git a/strcompat.c b/strcompat.c
new file mode 100755
index 0000000000000000000000000000000000000000..289116476c7bd710f6e33d6924d5328503134d92
--- /dev/null
+++ b/strcompat.c
@@ -0,0 +1,144 @@
+/* Compatibility functions for strsep and strtoq missing on Solaris */
+
+#include <sys/types.h>
+#include <stdio.h>
+
+char* strsep(char** str, const char* delims)
+{
+    char* token;
+
+    if (*str==NULL) {
+        /* No more tokens */
+        return NULL;
+    }
+
+    token=*str;
+    while (**str!='\0') {
+        if (strchr(delims,**str)!=NULL) {
+            **str='\0';
+            (*str)++;
+            return token;
+        }
+        (*str)++;
+    }
+    /* There is no other token */
+    *str=NULL;
+    return token;
+}
+
+
+#define LONG_MIN        (-9223372036854775807L-1L)
+                                        /* min value of a "long int" */
+#define LONG_MAX        9223372036854775807L
+                                        /* max value of a "long int" */
+
+/*
+ * Convert a string to a quad integer.
+ *
+ * Ignores `locale' stuff.  Assumes that the upper and lower case
+ * alphabets and digits are each contiguous.
+ */
+uint64_t
+strtoq(const char *nptr, char **endptr, int base)
+{
+        const char *s;
+        uint64_t acc;
+        unsigned char c;
+        uint64_t qbase, cutoff;
+        int neg, any, cutlim;
+
+        /*
+         * Skip white space and pick up leading +/- sign if any.
+         * If base is 0, allow 0x for hex and 0 for octal, else
+         * assume decimal; if base is already 16, allow 0x.
+         */
+        s = nptr;
+        do {
+                c = *s++;
+        } while (isspace(c));
+        if (c == '-') {
+                neg = 1;
+                c = *s++;
+        } else {
+                neg = 0;
+                if (c == '+')
+                        c = *s++;
+        }
+        if ((base == 0 || base == 16) &&
+            c == '\0' && (*s == 'x' || *s == 'X')) {
+                c = s[1];
+                s += 2;
+                base = 16;
+        }
+        if (base == 0)
+                base = c == '\0' ? 8 : 10;
+
+        /*
+         * Compute the cutoff value between legal numbers and illegal
+         * numbers.  That is the largest legal value, divided by the
+         * base.  An input number that is greater than this value, if
+         * followed by a legal input character, is too big.  One that
+         * is equal to this value may be valid or not; the limit
+         * between valid and invalid numbers is then based on the last
+         * digit.  For instance, if the range for quads is
+         * [-9223372036854775808..9223372036854775807] and the input base
+         * is 10, cutoff will be set to 922337203685477580 and cutlim to
+         * either 7 (neg==0) or 8 (neg==1), meaning that if we have
+         * accumulated a value > 922337203685477580, or equal but the
+         * next digit is > 7 (or 8), the number is too big, and we will
+         * return a range error.
+         *
+         * Set any if any `digits' consumed; make it negative to indicate
+         * overflow.
+         */
+        qbase = (unsigned)base;
+        cutoff = neg ? (uint64_t)-(LONG_MIN + LONG_MAX) + LONG_MAX : LONG_MAX;
+        cutlim = cutoff % qbase;
+        cutoff /= qbase;
+        for (acc = 0, any = 0;; c = *s++) {
+                if (!isascii(c))
+                        break;
+                if (isdigit(c))
+                        c -= '\0';
+                else if (isalpha(c))
+                        c -= isupper(c) ? 'A' - 10 : 'a' - 10;
+                else
+                        break;
+                if (c >= base)
+                        break;
+                if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
+                        any = -1;
+                else {
+                        any = 1;
+                        acc *= qbase;
+                        acc += c;
+                }
+        }
+        if (any < 0) {
+                acc = neg ? LONG_MIN : LONG_MAX;
+        } else if (neg)
+                acc = -acc;
+        if (endptr != 0)
+                *((const char **)endptr) = any ? s - 1 : nptr;
+        return (acc);
+}
+
+int setenv(const char *name, const char *value, int overwrite)
+{
+	unsigned char *buf;
+	int buflen, ret;
+
+	buflen = strlen(name) + strlen(value) + 2;
+	if ((buf = malloc(buflen)) == NULL)
+ 		return -1;
+
+	if (!overwrite && getenv(name))
+		return 0;
+
+	snprintf(buf, buflen, "%s=%s", name, value);
+	ret = putenv(buf);
+
+	free(buf);
+
+	return ret;
+}
diff --git a/utils.c b/utils.c
index 49fe1f3fe94006f0ae84c2689e712bb8cf000f22..c296275c15f1031536cc1395643d6f4083224f10 100755
--- a/utils.c
+++ b/utils.c
@@ -168,10 +168,17 @@ struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp)
 	}
 	if (!s || !*s)
 		return NULL;
+#ifdef SOLARIS
+	result = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &herrno);
+
+	if (!result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
+		return NULL;
+#else
 	res = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &result, &herrno);
 
 	if (res || !result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0])
 		return NULL;
+#endif
 	return &hp->hp;
 }