Index: /pjproject/trunk/pjnath/include/pjnath/config.h
===================================================================
--- /pjproject/trunk/pjnath/include/pjnath/config.h	(revision 1653)
+++ /pjproject/trunk/pjnath/include/pjnath/config.h	(revision 1654)
@@ -256,4 +256,55 @@
 
 
+/** ICE session pool initial size. */
+#ifndef PJNATH_POOL_LEN_ICE_SESS
+#   define PJNATH_POOL_LEN_ICE_SESS		    512
+#endif
+
+/** ICE session pool increment size */
+#ifndef PJNATH_POOL_INC_ICE_SESS
+#   define PJNATH_POOL_INC_ICE_SESS		    512
+#endif
+
+/** ICE stream transport pool initial size. */
+#ifndef PJNATH_POOL_LEN_ICE_STRANS
+#   define PJNATH_POOL_LEN_ICE_STRANS		    1000
+#endif
+
+/** ICE stream transport pool increment size */
+#ifndef PJNATH_POOL_INC_ICE_STRANS
+#   define PJNATH_POOL_INC_ICE_STRANS		    512
+#endif
+
+/** NAT detect pool initial size */
+#ifndef PJNATH_POOL_LEN_NATCK
+#   define PJNATH_POOL_LEN_NATCK		    512
+#endif
+
+/** NAT detect pool increment size */
+#ifndef PJNATH_POOL_INC_NATCK
+#   define PJNATH_POOL_INC_NATCK		    512
+#endif
+
+/** STUN session pool initial size */
+#ifndef PJNATH_POOL_LEN_STUN_SESS
+#   define PJNATH_POOL_LEN_STUN_SESS		    1000
+#endif
+
+/** STUN session pool increment size */
+#ifndef PJNATH_POOL_INC_STUN_SESS
+#   define PJNATH_POOL_INC_STUN_SESS		    1000
+#endif
+
+/** STUN session transmit data pool initial size */
+#ifndef PJNATH_POOL_LEN_STUN_TDATA
+#   define PJNATH_POOL_LEN_STUN_TDATA		    1000
+#endif
+
+/** STUN session transmit data pool increment size */
+#ifndef PJNATH_POOL_INC_STUN_TDATA
+#   define PJNATH_POOL_INC_STUN_TDATA		    1000
+#endif
+
+
 /**
  * @}
Index: /pjproject/trunk/pjnath/include/pjnath/ice_session.h
===================================================================
--- /pjproject/trunk/pjnath/include/pjnath/ice_session.h	(revision 1653)
+++ /pjproject/trunk/pjnath/include/pjnath/ice_session.h	(revision 1654)
@@ -26,4 +26,5 @@
 #include <pjnath/types.h>
 #include <pjnath/stun_session.h>
+#include <pjnath/errno.h>
 #include <pj/sock.h>
 #include <pj/timer.h>
@@ -561,4 +562,10 @@
     /* Valid list */
     pj_ice_sess_checklist valid_list;		    /**< Valid list.	    */
+    
+    /* Temporary buffer for misc stuffs to avoid using stack too much */
+    union {
+    	char txt[128];
+	char errmsg[PJ_ERR_MSG_SIZE];
+    } tmp;
 };
 
Index: /pjproject/trunk/pjnath/src/pjnath/ice_session.c
===================================================================
--- /pjproject/trunk/pjnath/src/pjnath/ice_session.c	(revision 1653)
+++ /pjproject/trunk/pjnath/src/pjnath/ice_session.c	(revision 1654)
@@ -18,5 +18,4 @@
  */
 #include <pjnath/ice_session.h>
-#include <pjnath/errno.h>
 #include <pj/addr_resolv.h>
 #include <pj/array.h>
@@ -262,5 +261,6 @@
 	name = "ice%p";
 
-    pool = pj_pool_create(stun_cfg->pf, name, 4000, 4000, NULL);
+    pool = pj_pool_create(stun_cfg->pf, name, PJNATH_POOL_LEN_ICE_SESS, 
+			  PJNATH_POOL_INC_ICE_SESS, NULL);
     ice = PJ_POOL_ZALLOC_T(pool, pj_ice_sess);
     ice->pool = pool;
@@ -558,5 +558,4 @@
     pj_ice_sess_cand *lcand;
     pj_status_t status = PJ_SUCCESS;
-    char tmp[128];
 
     PJ_ASSERT_RETURN(ice && comp_id && 
@@ -585,5 +584,5 @@
 
 
-    pj_ansi_strcpy(tmp, pj_inet_ntoa(lcand->addr.ipv4.sin_addr));
+    pj_ansi_strcpy(ice->tmp.txt, pj_inet_ntoa(lcand->addr.ipv4.sin_addr));
     LOG4((ice->obj_name, 
 	 "Candidate %d added: comp_id=%d, type=%s, foundation=%.*s, "
@@ -594,5 +593,5 @@
 	 (int)lcand->foundation.slen,
 	 lcand->foundation.ptr,
-	 tmp, 
+	 ice->tmp.txt, 
 	 (int)pj_ntohs(lcand->addr.ipv4.sin_port),
 	 pj_inet_ntoa(lcand->base_addr.ipv4.sin_addr),
@@ -734,6 +733,8 @@
     const pj_ice_sess_cand *lcand = check->lcand;
     const pj_ice_sess_cand *rcand = check->rcand;
-    char laddr[CHECK_NAME_LEN];
+    char laddr[PJ_INET6_ADDRSTRLEN];
     int len;
+
+    PJ_CHECK_STACK();
 
     pj_ansi_strcpy(laddr, pj_inet_ntoa(lcand->addr.ipv4.sin_addr));
@@ -761,9 +762,8 @@
 }
 
-static void dump_checklist(const char *title, const pj_ice_sess *ice, 
+static void dump_checklist(const char *title, pj_ice_sess *ice, 
 			   const pj_ice_sess_checklist *clist)
 {
     unsigned i;
-    char buffer[CHECK_NAME_LEN];
 
     LOG4((ice->obj_name, "%s", title));
@@ -771,5 +771,5 @@
 	const pj_ice_sess_check *c = &clist->checks[i];
 	LOG4((ice->obj_name, " %s (%s, state=%s)",
-	     dump_check(buffer, sizeof(buffer), clist, c),
+	     dump_check(ice->tmp.txt, sizeof(ice->tmp.txt), clist, c),
 	     (c->nominated ? "nominated" : "not nominated"), 
 	     check_state_name[c->state]));
@@ -785,10 +785,8 @@
 			    pj_status_t err_code)
 {
-    char buf[CHECK_NAME_LEN];
-
     pj_assert(check->state < PJ_ICE_SESS_CHECK_STATE_SUCCEEDED);
 
     LOG5((ice->obj_name, "Check %s: state changed from %s to %s",
-	 dump_check(buf, sizeof(buf), &ice->clist, check),
+	 dump_check(ice->tmp.txt, sizeof(ice->tmp.txt), &ice->clist, check),
 	 check_state_name[check->state],
 	 check_state_name[st]));
@@ -938,9 +936,7 @@
 	    if (reason != NULL) {
 		/* Found duplicate, remove it */
-		char buf[CHECK_NAME_LEN];
-
 		LOG5((ice->obj_name, "Check %s pruned (%s)",
-		      dump_check(buf, sizeof(buf), &ice->clist, 
-			         &clist->checks[j]),
+		      dump_check(ice->tmp.txt, sizeof(ice->tmp.txt), 
+				 &ice->clist, &clist->checks[j]),
 		      reason));
 
@@ -976,6 +972,4 @@
 {
     if (!ice->is_complete) {
-	char errmsg[PJ_ERR_MSG_SIZE];
-
 	ice->is_complete = PJ_TRUE;
 	ice->ice_status = status;
@@ -983,5 +977,6 @@
 	/* Log message */
 	LOG4((ice->obj_name, "ICE process complete, status=%s", 
-	     pj_strerror(status, errmsg, sizeof(errmsg)).ptr));
+	     pj_strerror(status, ice->tmp.errmsg, 
+			 sizeof(ice->tmp.errmsg)).ptr));
 
 	dump_checklist("Valid list", ice, &ice->valid_list);
@@ -1057,5 +1052,4 @@
     if (check->err_code==PJ_SUCCESS && check->nominated) {
 	pj_ice_sess_comp *comp;
-	char buf[CHECK_NAME_LEN];
 
 	LOG5((ice->obj_name, "Check %d is successful and nominated",
@@ -1075,5 +1069,6 @@
 		    LOG5((ice->obj_name, 
 			 "Check %s to be failed because state is %s",
-			 dump_check(buf, sizeof(buf), &ice->clist, c), 
+			 dump_check(ice->tmp.txt, sizeof(ice->tmp.txt), 
+				    &ice->clist, c), 
 			 check_state_name[c->state]));
 		    check_set_state(ice, c, PJ_ICE_SESS_CHECK_STATE_FAILED,
@@ -1088,5 +1083,6 @@
 			LOG5((ice->obj_name, 
 			     "Cancelling check %s (In Progress)",
-			     dump_check(buf, sizeof(buf), &ice->clist, c)));
+			     dump_check(ice->tmp.txt, sizeof(ice->tmp.txt), 
+					&ice->clist, c)));
 			pj_stun_session_cancel_req(comp->stun_sess, 
 						   c->tdata, PJ_FALSE, 0);
@@ -1369,5 +1365,4 @@
     const pj_ice_sess_cand *rcand;
     pj_uint32_t prio;
-    char buffer[128];
     pj_status_t status;
 
@@ -1379,5 +1374,5 @@
     LOG5((ice->obj_name, 
 	 "Sending connectivity check for check %s", 
-	 dump_check(buffer, sizeof(buffer), clist, check)));
+	 dump_check(ice->tmp.txt, sizeof(ice->tmp.txt), clist, check)));
 
     /* Create request */
@@ -1554,7 +1549,9 @@
     pj_ice_sess_checklist *clist;
     const pj_ice_sess_cand *cand0;
-    const pj_str_t *flist[PJ_ICE_MAX_CAND];
+    const pj_str_t *flist[PJ_ICE_MAX_CAND]; // XXX
     pj_ice_rx_check *rcheck;
     unsigned i, flist_cnt = 0;
+    pj_time_val delay;
+    pj_status_t status;
 
     PJ_ASSERT_RETURN(ice, PJ_EINVAL);
@@ -1625,5 +1622,17 @@
 
     /* Start periodic check */
-    return start_periodic_check(ice->stun_cfg.timer_heap, &clist->timer);
+    /* We could start it immediately like below, but lets schedule timer 
+     * instead to reduce stack usage:
+     * return start_periodic_check(ice->stun_cfg.timer_heap, &clist->timer);
+     */
+    clist->timer.id = PJ_TRUE;
+    delay.sec = delay.msec = 0;
+    status = pj_timer_heap_schedule(ice->stun_cfg.timer_heap, 
+				    &clist->timer, &delay);
+    if (status != PJ_SUCCESS) {
+	clist->timer.id = PJ_FALSE;
+    }
+
+    return status;
 }
 
@@ -1662,5 +1671,4 @@
     pj_ice_sess_checklist *clist;
     pj_stun_xor_mapped_addr_attr *xaddr;
-    char buffer[CHECK_NAME_LEN];
     unsigned i;
 
@@ -1731,5 +1739,6 @@
 	LOG4((ice->obj_name, 
 	     "Check %s%s: connectivity check FAILED: %s",
-	     dump_check(buffer, sizeof(buffer), &ice->clist, check),
+	     dump_check(ice->tmp.txt, sizeof(ice->tmp.txt), 
+			&ice->clist, check),
 	     (check->nominated ? " (nominated)" : " (not nominated)"),
 	     errmsg));
@@ -1754,5 +1763,6 @@
 	LOG4((ice->obj_name, 
 	     "Check %s%s: connectivity check FAILED: source address mismatch",
-	     dump_check(buffer, sizeof(buffer), &ice->clist, check),
+	     dump_check(ice->tmp.txt, sizeof(ice->tmp.txt), 
+			&ice->clist, check),
 	     (check->nominated ? " (nominated)" : " (not nominated)")));
 	check_set_state(ice, check, PJ_ICE_SESS_CHECK_STATE_FAILED, status);
@@ -1780,5 +1790,6 @@
     LOG4((ice->obj_name, 
 	 "Check %s%s: connectivity check SUCCESS",
-	 dump_check(buffer, sizeof(buffer), &ice->clist, check),
+	 dump_check(ice->tmp.txt, sizeof(ice->tmp.txt), 
+		    &ice->clist, check),
 	 (check->nominated ? " (nominated)" : " (not nominated)")));
 
@@ -2358,8 +2369,7 @@
 					   NULL, src_addr, src_addr_len);
 	if (status != PJ_SUCCESS) {
-	    char errmsg[PJ_ERR_MSG_SIZE];
-	    pj_strerror(status, errmsg, sizeof(errmsg));
+	    pj_strerror(status, ice->tmp.errmsg, sizeof(ice->tmp.errmsg));
 	    LOG4((ice->obj_name, "Error processing incoming message: %s",
-		  errmsg));
+		  ice->tmp.errmsg));
 	}
     } else {
Index: /pjproject/trunk/pjnath/src/pjnath/ice_strans.c
===================================================================
--- /pjproject/trunk/pjnath/src/pjnath/ice_strans.c	(revision 1653)
+++ /pjproject/trunk/pjnath/src/pjnath/ice_strans.c	(revision 1654)
@@ -98,5 +98,6 @@
 	name = "icstr%p";
 
-    pool = pj_pool_create(stun_cfg->pf, name, 1000, 512, NULL);
+    pool = pj_pool_create(stun_cfg->pf, name, PJNATH_POOL_LEN_ICE_STRANS, 
+			  PJNATH_POOL_INC_ICE_STRANS, NULL);
     ice_st = PJ_POOL_ZALLOC_T(pool, pj_ice_strans);
     ice_st->pool = pool;
Index: /pjproject/trunk/pjnath/src/pjnath/nat_detect.c
===================================================================
--- /pjproject/trunk/pjnath/src/pjnath/nat_detect.c	(revision 1653)
+++ /pjproject/trunk/pjnath/src/pjnath/nat_detect.c	(revision 1654)
@@ -221,5 +221,6 @@
      * Init NAT detection session.
      */
-    pool = pj_pool_create(stun_cfg->pf, "natck%p", 512, 512, NULL);
+    pool = pj_pool_create(stun_cfg->pf, "natck%p", PJNATH_POOL_LEN_NATCK, 
+			  PJNATH_POOL_INC_NATCK, NULL);
     if (!pool)
 	return PJ_ENOMEM;
Index: /pjproject/trunk/pjnath/src/pjnath/stun_msg.c
===================================================================
--- /pjproject/trunk/pjnath/src/pjnath/stun_msg.c	(revision 1653)
+++ /pjproject/trunk/pjnath/src/pjnath/stun_msg.c	(revision 1654)
@@ -708,4 +708,6 @@
     pj_uint32_t val;
 
+    PJ_CHECK_STACK();
+    
     /* Create the attribute */
     attr = PJ_POOL_ZALLOC_T(pool, pj_stun_sockaddr_attr);
@@ -772,4 +774,6 @@
 	return PJ_ETOOSMALL;
 
+    PJ_CHECK_STACK();
+    
     /* Copy and convert headers to network byte order */
     PUTVAL16H(buf, 0, ca->hdr.type);
@@ -900,4 +904,6 @@
 	(const pj_stun_string_attr*)a;
 
+    PJ_CHECK_STACK();
+    
     /* Calculated total attr_len (add padding if necessary) */
     *printed = (ca->value.slen + ATTR_HDR_LEN + 3) & (~3);
@@ -1081,4 +1087,6 @@
     const pj_stun_uint_attr *ca = (const pj_stun_uint_attr*)a;
 
+    PJ_CHECK_STACK();
+    
     if (len < 8) 
 	return PJ_ETOOSMALL;
@@ -1164,4 +1172,6 @@
     const pj_stun_uint64_attr *ca = (const pj_stun_uint64_attr*)a;
 
+    PJ_CHECK_STACK();
+    
     if (len < 12) 
 	return PJ_ETOOSMALL;
@@ -1243,4 +1253,6 @@
     const pj_stun_msgint_attr *ca = (const pj_stun_msgint_attr*)a;
 
+    PJ_CHECK_STACK();
+    
     if (len < 24) 
 	return PJ_ETOOSMALL;
@@ -1347,4 +1359,6 @@
 	(const pj_stun_errcode_attr*)a;
 
+    PJ_CHECK_STACK();
+    
     if (len < ATTR_HDR_LEN + 4 + (unsigned)ca->reason.slen) 
 	return PJ_ETOOSMALL;
@@ -1462,4 +1476,6 @@
     unsigned i;
 
+    PJ_CHECK_STACK();
+    
     /* Check that buffer is enough */
     if (len < ATTR_HDR_LEN + (ca->attr_count << 1))
@@ -1562,4 +1578,6 @@
     const pj_stun_binary_attr *ca = (const pj_stun_binary_attr*)a;
 
+    PJ_CHECK_STACK();
+    
     /* Calculated total attr_len (add padding if necessary) */
     *printed = (ca->length + ATTR_HDR_LEN + 3) & (~3);
Index: /pjproject/trunk/pjnath/src/pjnath/stun_msg_dump.c
===================================================================
--- /pjproject/trunk/pjnath/src/pjnath/stun_msg_dump.c	(revision 1653)
+++ /pjproject/trunk/pjnath/src/pjnath/stun_msg_dump.c	(revision 1654)
@@ -20,6 +20,6 @@
 #include <pjnath/errno.h>
 #include <pj/assert.h>
+#include <pj/os.h>
 #include <pj/string.h>
-
 
 #if PJ_LOG_MAX_LEVEL > 0
@@ -237,4 +237,6 @@
     PJ_ASSERT_RETURN(msg && buffer && length, NULL);
 
+    PJ_CHECK_STACK();
+    
     p = buffer;
     end = buffer + length;
Index: /pjproject/trunk/pjnath/src/pjnath/stun_session.c
===================================================================
--- /pjproject/trunk/pjnath/src/pjnath/stun_session.c	(revision 1653)
+++ /pjproject/trunk/pjnath/src/pjnath/stun_session.c	(revision 1654)
@@ -46,6 +46,6 @@
 #define LOG_ERR_(sess,title,rc) pjnath_perror(sess->pool->obj_name,title,rc)
 
-#define TDATA_POOL_SIZE		    1024
-#define TDATA_POOL_INC		    1024
+#define TDATA_POOL_SIZE		    PJNATH_POOL_LEN_STUN_TDATA
+#define TDATA_POOL_INC		    PJNATH_POOL_INC_STUN_TDATA
 
 
@@ -384,7 +384,8 @@
 
     if (name==NULL)
-	name = "sess%p";
-
-    pool = pj_pool_create(cfg->pf, name, 4000, 4000, NULL);
+	name = "stuse%p";
+
+    pool = pj_pool_create(cfg->pf, name, PJNATH_POOL_LEN_STUN_SESS, 
+			  PJNATH_POOL_INC_STUN_SESS, NULL);
     PJ_ASSERT_RETURN(pool, PJ_ENOMEM);
 
@@ -996,5 +997,7 @@
     PJ_ASSERT_RETURN(sess && packet && pkt_size, PJ_EINVAL);
 
-    tmp_pool = pj_pool_create(sess->cfg->pf, "tmpstun", 1024, 1024, NULL);
+    tmp_pool = pj_pool_create(sess->cfg->pf, "tmpstun", 
+			      PJNATH_POOL_LEN_STUN_TDATA, 
+			      PJNATH_POOL_INC_STUN_TDATA, NULL);
     if (!tmp_pool)
 	return PJ_ENOMEM;
