Index: pjproject/trunk/pjnath/src/pjturn-srv/turn.h
===================================================================
--- pjproject/trunk/pjnath/src/pjturn-srv/turn.h	(revision 1812)
+++ pjproject/trunk/pjnath/src/pjturn-srv/turn.h	(revision 1850)
@@ -34,4 +34,6 @@
 #define PJTURN_NO_TIMEOUT	    ((long)0x7FFFFFFF)
 #define PJTURN_MAX_PKT_LEN	    3000
+#define PJTURN_PERM_TIMEOUT	    300
+#define PJTURN_CHANNEL_TIMEOUT	    600
 
 /** Transport types */
@@ -41,4 +43,8 @@
 };
 
+/** 
+ * Get transport type name string.
+ */
+PJ_DECL(const char*) pjturn_tp_type_name(int tp_type);
 
 /**
@@ -55,18 +61,9 @@
 	/** Transport/relay address */
 	pj_sockaddr	    addr;
-    } key;
-
-    /** Pool for this resource. */
-    pj_pool_t       *pool;
-
-    /** Mutex */
-    pj_lock_t	    *lock;
+    } hkey;
 
     /** Allocation who requested or reserved this resource. */
     pjturn_allocation *allocation;
 
-    /** Time when this resource times out */
-    pj_time_val	    timeout;
-
     /** Username used in credential */
     pj_str_t	    user;
@@ -75,6 +72,36 @@
     pj_str_t	    realm;
 
-    /** Transport/relay socket */
-    pj_sock_t	    sock;
+    /** Lifetime, in seconds. */
+    unsigned	    lifetime;
+
+    /** Relay/allocation expiration time */
+    pj_time_val	    expiry;
+
+    /** Timeout timer entry */
+    pj_timer_entry  timer;
+
+    /** Transport. */
+    struct {
+	/** Transport/relay socket */
+	pj_sock_t	    sock;
+
+	/** Transport/relay ioqueue */
+	pj_ioqueue_key_t    *key;
+
+	/** Read operation key. */
+	pj_ioqueue_op_key_t read_key;
+
+	/** The incoming packet buffer */
+	char		    rx_pkt[PJTURN_MAX_PKT_LEN];
+
+	/** Source address of the packet. */
+	pj_sockaddr	    src_addr;
+
+	/** Source address length */
+	int		    src_addr_len;
+
+	/** The outgoing packet buffer. This must be 3wbit aligned. */
+	char		    tx_pkt[PJTURN_MAX_PKT_LEN+4];
+    } tp;
 };
 
@@ -105,5 +132,5 @@
 
     /** Requested IP */
-    pj_sockaddr		addr;
+    char		addr[PJ_INET6_ADDRSTRLEN];
 
     /** Requested bandwidth */
@@ -128,9 +155,15 @@
 {
     /** Hash table key to identify client. */
-    pjturn_allocation_key key;
+    pjturn_allocation_key hkey;
 
     /** Pool for this allocation. */
     pj_pool_t		*pool;
 
+    /** Object name for logging identification */
+    char		*obj_name;
+
+    /** Client info (IP address and port) */
+    char		info[80];
+
     /** Mutex */
     pj_lock_t		*lock;
@@ -148,5 +181,28 @@
     pjturn_relay_res	*resv;
 
-};
+    /** Requested bandwidth */
+    unsigned		bandwidth;
+
+    /** STUN session for this client */
+    pj_stun_session	*sess;
+
+    /** Peer hash table (keyed by peer address) */
+    pj_hash_table_t	*peer_table;
+
+    /** Channel hash table (keyed by channel number) */
+    pj_hash_table_t	*ch_table;
+};
+
+
+/**
+ * This structure describes the hash table key to lookup TURN
+ * permission.
+ */
+typedef struct pjturn_permission_key
+{
+    /** Peer address. */
+    pj_sockaddr		peer_addr;
+
+} pjturn_permission_key;
 
 
@@ -157,22 +213,10 @@
 {
     /** Hash table key */
-    struct {
-	/** Transport type. */
-	pj_uint16_t		tp_type;
-
-	/** Transport socket. If TCP is used, the value will be the actual
-	 *  TCP socket. If UDP is used, the value will be the relay address
-	 */
-	pj_sock_t		sock;
-
-	/** Peer address. */
-	pj_sockaddr		peer_addr;
-    } key;
-
-    /** Pool for this permission. */
-    pj_pool_t	        *pool;
-
-    /** Mutex */
-    pj_lock_t		*lock;
+    pjturn_permission_key hkey;
+
+    /** Transport socket. If TCP is used, the value will be the actual
+     *  TCP socket. If UDP is used, the value will be the relay address
+     */
+    pj_sock_t		sock;
 
     /** TURN allocation that owns this permission/channel */
@@ -184,14 +228,36 @@
     pj_uint16_t		channel;
 
-    /** Permission timeout. */
-    pj_time_val		timeout;
-};
-
-/**
- * Handle incoming packet.
- */
-PJ_DECL(void) pjturn_allocation_on_rx_pkt(pjturn_allocation *alloc,
-					  pjturn_pkt *pkt);
-
+    /** Permission expiration time. */
+    pj_time_val		expiry;
+};
+
+/**
+ * Create new allocation.
+ */
+PJ_DECL(pj_status_t) pjturn_allocation_create(pjturn_listener *listener,
+					      const pj_sockaddr_t *src_addr,
+					      unsigned src_addr_len,
+					      const pj_stun_msg *msg,
+					      const pjturn_allocation_req *req,
+					      pjturn_allocation **p_alloc);
+/**
+ * Destroy allocation.
+ */
+PJ_DECL(void) pjturn_allocation_destroy(pjturn_allocation *alloc);
+
+/**
+ * Create relay.
+ */
+PJ_DECL(pj_status_t) pjturn_allocation_create_relay(pjturn_srv *srv,
+					            pjturn_allocation *alloc,
+						    const pj_stun_msg *msg,
+						    const pjturn_allocation_req *req,
+						    pjturn_relay_res *relay);
+
+/**
+ * Handle incoming packet from client.
+ */
+PJ_DECL(void) pjturn_allocation_on_rx_client_pkt(pjturn_allocation *alloc,
+						 pjturn_pkt *pkt);
 
 /****************************************************************************/
@@ -251,5 +317,5 @@
     pjturn_listener	    *listener;
 
-    /** Packet buffer. */
+    /** Packet buffer (must be 32bit aligned). */
     pj_uint8_t		    pkt[PJTURN_MAX_PKT_LEN];
 
@@ -358,9 +424,4 @@
 	pj_hash_table_t *res;
 
-	/** Permission hash table, indexed by transport type, socket handle,
-	 *  and peer address.
-	 */
-	pj_hash_table_t *peer;
-
     } tables;
 
@@ -408,4 +469,16 @@
 
 /**
+ * Register an allocation.
+ */
+PJ_DECL(pj_status_t) pjturn_srv_register_allocation(pjturn_srv *srv,
+						    pjturn_allocation *alloc);
+
+/**
+ * Unregister an allocation.
+ */
+PJ_DECL(pj_status_t) pjturn_srv_unregister_allocation(pjturn_srv *srv,
+						      pjturn_allocation *alloc);
+
+/**
  * This callback is called by UDP listener on incoming packet.
  */
