Index: pjproject/trunk/pjlib-util/src/pjlib-util/stun_msg.c
===================================================================
--- pjproject/trunk/pjlib-util/src/pjlib-util/stun_msg.c	(revision 1003)
+++ pjproject/trunk/pjlib-util/src/pjlib-util/stun_msg.c	(revision 1021)
@@ -518,6 +518,6 @@
 				    int attr_type,
 				    pj_bool_t xor_ed,
+				    const pj_sockaddr_t *addr,
 				    unsigned addr_len,
-				    const pj_sockaddr_t *addr,
 				    pj_stun_generic_ip_addr_attr **p_attr)
 {
@@ -560,6 +560,6 @@
 				     int attr_type, 
 				     pj_bool_t xor_ed,
-				     unsigned addr_len,
-				     const pj_sockaddr_t *addr)
+				     const pj_sockaddr_t *addr,
+				     unsigned addr_len)
 {
     pj_stun_generic_ip_addr_attr *attr;
@@ -567,5 +567,5 @@
 
     status = pj_stun_generic_ip_addr_attr_create(pool, attr_type, xor_ed,
-					         addr_len, addr, &attr);
+					         addr, addr_len, &attr);
     if (status != PJ_SUCCESS)
 	return status;
@@ -1435,15 +1435,13 @@
 PJ_INLINE(pj_uint16_t) GET_VAL16(const pj_uint8_t *pdu, unsigned pos)
 {
-    pj_uint16_t val = (pj_uint16_t) ((pdu[pos] << 8) + pdu[pos+1]);
-    return pj_ntohs(val);
+    return (pj_uint16_t) ((pdu[pos] << 8) + pdu[pos+1]);
 }
 
 PJ_INLINE(pj_uint32_t) GET_VAL32(const pj_uint8_t *pdu, unsigned pos)
 {
-    pj_uint32_t val = (pdu[pos+0] << 24) +
-		      (pdu[pos+1] << 16) +
-		      (pdu[pos+2] <<  8) +
-		      (pdu[pos+3]);
-    return pj_ntohl(val);
+    return (pdu[pos+0] << 24) +
+	   (pdu[pos+1] << 16) +
+	   (pdu[pos+2] <<  8) +
+	   (pdu[pos+3]);
 }
 
@@ -1466,32 +1464,32 @@
 	return PJLIB_UTIL_ESTUNINMSGTYPE;
 
+    /* Check the PDU length */
+    msg_len = GET_VAL16(pdu, 2);
+    if ((msg_len + 20 > pdu_len) || 
+	((options & PJ_STUN_IS_DATAGRAM) && msg_len + 20 != pdu_len))
+    {
+	return PJLIB_UTIL_ESTUNINMSGLEN;
+    }
+
     /* If magic is set, then there is great possibility that this is
      * a STUN message.
      */
-    if (GET_VAL32(pdu, 4) != PJ_STUN_MAGIC)
-	return PJLIB_UTIL_ESTUNNOTMAGIC;
-
-    /* Check the PDU length */
-    msg_len = GET_VAL16(pdu, 2);
-    if ((msg_len > pdu_len) || 
-	((options & PJ_STUN_IS_DATAGRAM) && msg_len != pdu_len))
-    {
-	return PJLIB_UTIL_ESTUNINMSGLEN;
-    }
-
-    /* Check if FINGERPRINT attribute is present */
-    if (GET_VAL16(pdu, msg_len + 20) == PJ_STUN_ATTR_FINGERPRINT) {
-	pj_uint16_t attr_len = GET_VAL16(pdu, msg_len + 22);
-	pj_uint32_t fingerprint = GET_VAL32(pdu, msg_len + 24);
-	pj_uint32_t crc;
-
-	if (attr_len != 4)
-	    return PJLIB_UTIL_ESTUNINATTRLEN;
-
-	crc = pj_crc32_calc(pdu, msg_len + 20);
-	crc ^= STUN_XOR_FINGERPRINT;
-
-	if (crc != fingerprint)
-	    return PJLIB_UTIL_ESTUNFINGERPRINT;
+    if (GET_VAL32(pdu, 4) == PJ_STUN_MAGIC) {
+
+	/* Check if FINGERPRINT attribute is present */
+	if (GET_VAL16(pdu, msg_len + 20) == PJ_STUN_ATTR_FINGERPRINT) {
+	    pj_uint16_t attr_len = GET_VAL16(pdu, msg_len + 22);
+	    pj_uint32_t fingerprint = GET_VAL32(pdu, msg_len + 24);
+	    pj_uint32_t crc;
+
+	    if (attr_len != 4)
+		return PJLIB_UTIL_ESTUNINATTRLEN;
+
+	    crc = pj_crc32_calc(pdu, msg_len + 20);
+	    crc ^= STUN_XOR_FINGERPRINT;
+
+	    if (crc != fingerprint)
+		return PJLIB_UTIL_ESTUNFINGERPRINT;
+	}
     }
 
@@ -1820,5 +1818,5 @@
  * Print the message structure to a buffer.
  */
-PJ_DEF(pj_status_t) pj_stun_msg_encode(const pj_stun_msg *msg,
+PJ_DEF(pj_status_t) pj_stun_msg_encode(pj_stun_msg *msg,
 				       pj_uint8_t *buf, unsigned buf_size,
 				       unsigned options,
@@ -1834,5 +1832,5 @@
     unsigned printed;
     pj_status_t status;
-    unsigned i, length;
+    unsigned i;
 
 
@@ -1899,16 +1897,16 @@
      */
     if (amsg_integrity && afingerprint) {
-	length = (pj_uint16_t)((buf - start) - 20 + 24 + 8);
+	msg->hdr.length = (pj_uint16_t)((buf - start) - 20 + 24 + 8);
     } else if (amsg_integrity) {
-	length = (pj_uint16_t)((buf - start) - 20 + 24);
+	msg->hdr.length = (pj_uint16_t)((buf - start) - 20 + 24);
     } else if (afingerprint) {
-	length = (pj_uint16_t)((buf - start) - 20 + 8);
+	msg->hdr.length = (pj_uint16_t)((buf - start) - 20 + 8);
     } else {
-	length = (pj_uint16_t)((buf - start) - 20);
+	msg->hdr.length = (pj_uint16_t)((buf - start) - 20);
     }
 
     /* hdr->length = pj_htons(length); */
-    *(buf+2) = (pj_uint8_t)((length >> 8) & 0x00FF);
-    *(buf+3) = (pj_uint8_t)(length & 0x00FF);
+    start[2] = (pj_uint8_t)((msg->hdr.length >> 8) & 0x00FF);
+    start[3] = (pj_uint8_t)(msg->hdr.length & 0x00FF);
 
     /* Calculate message integrity, if present */
@@ -2089,5 +2087,5 @@
 	*p_response = NULL;
 
-    if (PJ_STUN_IS_REQUEST(msg->hdr.type))
+    if (!PJ_STUN_IS_REQUEST(msg->hdr.type))
 	p_response = NULL;
 
