[asterisk-scf-commits] asterisk-scf/release/pjproject.git branch "veesix" created.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Mon Nov 22 15:45:47 CST 2010


branch "veesix" has been created
        at  9e646bf5feb08626df79ac200bfedba4d9a426cc (commit)

- Log -----------------------------------------------------------------
commit 9e646bf5feb08626df79ac200bfedba4d9a426cc
Author: Mark Michelson <mmichelson at digium.com>
Date:   Mon Nov 22 15:47:06 2010 -0600

    Initial addition of IPv6 support for TCP transport.
    
    I went the route of retaining a single API call for creating a TCP
    transport, but modified the argument such that it takes a generic
    pj_sockaddr instead of a pj_sockaddr_in. Most of the underlying
    structures and functions were already set up to use this type of
    structure. The majority of work here was eliminating assumptions
    in the code regarding the address size.
    
    It compiles, but it has not been tested.

diff --git a/pjsip/include/pjsip/sip_transport_tcp.h b/pjsip/include/pjsip/sip_transport_tcp.h
index d2fc653..32c8ca9 100644
--- a/pjsip/include/pjsip/sip_transport_tcp.h
+++ b/pjsip/include/pjsip/sip_transport_tcp.h
@@ -139,7 +139,7 @@ PJ_DECL(void) pjsip_tcp_transport_cfg_default(pjsip_tcp_transport_cfg *cfg,
  *			the appropriate error code.
  */
 PJ_DECL(pj_status_t) pjsip_tcp_transport_start(pjsip_endpoint *endpt,
-					       const pj_sockaddr_in *local,
+					       const pj_sockaddr *local,
 					       unsigned async_cnt,
 					       pjsip_tpfactory **p_factory);
 
@@ -174,7 +174,7 @@ PJ_DECL(pj_status_t) pjsip_tcp_transport_start(pjsip_endpoint *endpt,
  *			the appropriate error code.
  */
 PJ_DECL(pj_status_t) pjsip_tcp_transport_start2(pjsip_endpoint *endpt,
-					        const pj_sockaddr_in *local,
+					        const pj_sockaddr *local,
 					        const pjsip_host_port *a_name,
 					        unsigned async_cnt,
 					        pjsip_tpfactory **p_factory);
diff --git a/pjsip/src/pjsip/sip_transport_tcp.c b/pjsip/src/pjsip/sip_transport_tcp.c
index a5d21ec..45bb608 100644
--- a/pjsip/src/pjsip/sip_transport_tcp.c
+++ b/pjsip/src/pjsip/sip_transport_tcp.c
@@ -140,8 +140,8 @@ static pj_status_t lis_create_transport(pjsip_tpfactory *factory,
 static pj_status_t tcp_create(struct tcp_listener *listener,
 			      pj_pool_t *pool,
 			      pj_sock_t sock, pj_bool_t is_server,
-			      const pj_sockaddr_in *local,
-			      const pj_sockaddr_in *remote,
+			      const pj_sockaddr *local,
+			      const pj_sockaddr *remote,
 			      struct tcp_transport **p_tcp);
 
 
@@ -158,7 +158,7 @@ static void tcp_perror(const char *sender, const char *title,
 
 static void sockaddr_to_host_port( pj_pool_t *pool,
 				   pjsip_host_port *host_port,
-				   const pj_sockaddr_in *addr )
+				   const pj_sockaddr *addr )
 {
     host_port->host.ptr = (char*) pj_pool_alloc(pool, PJ_INET6_ADDRSTRLEN+4);
     pj_sockaddr_print(addr, host_port->host.ptr, PJ_INET6_ADDRSTRLEN+4, 2);
@@ -252,7 +252,7 @@ PJ_DEF(pj_status_t) pjsip_tcp_transport_start3(
 				  (pj_uint16_t)cfg->addr_name.port);
 	if (status != PJ_SUCCESS || !pj_sockaddr_has_addr(&tmp) ||
 	    (cfg->af==pj_AF_INET() && 
-	     tmp.ipv4.sin_addr.s_addr==PJ_INADDR_NONE)) 
+	     tmp.ipv4.sin_addr.s_addr==PJ_INADDR_NONE))
 	{
 	    /* Invalid address */
 	    return PJ_EINVAL;
@@ -338,7 +338,7 @@ PJ_DEF(pj_status_t) pjsip_tcp_transport_start3(
 	/* Save the address name */
 	sockaddr_to_host_port(listener->factory.pool, 
 			      &listener->factory.addr_name, 
-			      (pj_sockaddr_in*)listener_addr);
+			      listener_addr);
     }
 
     /* If port is zero, get the bound port */
@@ -413,13 +413,14 @@ on_error:
  * TCP listener.
  */
 PJ_DEF(pj_status_t) pjsip_tcp_transport_start2(pjsip_endpoint *endpt,
-					       const pj_sockaddr_in *local,
+					       const pj_sockaddr *local,
 					       const pjsip_host_port *a_name,
 					       unsigned async_cnt,
 					       pjsip_tpfactory **p_factory)
 {
     pjsip_tcp_transport_cfg cfg;
 
+    /* XXX Change family here... */
     pjsip_tcp_transport_cfg_default(&cfg, pj_AF_INET());
 
     if (local)
@@ -442,7 +443,7 @@ PJ_DEF(pj_status_t) pjsip_tcp_transport_start2(pjsip_endpoint *endpt,
  * TCP listener.
  */
 PJ_DEF(pj_status_t) pjsip_tcp_transport_start( pjsip_endpoint *endpt,
-					       const pj_sockaddr_in *local,
+					       const pj_sockaddr *local,
 					       unsigned async_cnt,
 					       pjsip_tpfactory **p_factory)
 {
@@ -535,8 +536,8 @@ static void tcp_keep_alive_timer(pj_timer_heap_t *th, pj_timer_entry *e);
 static pj_status_t tcp_create( struct tcp_listener *listener,
 			       pj_pool_t *pool,
 			       pj_sock_t sock, pj_bool_t is_server,
-			       const pj_sockaddr_in *local,
-			       const pj_sockaddr_in *remote,
+			       const pj_sockaddr *local,
+			       const pj_sockaddr *remote,
 			       struct tcp_transport **p_tcp)
 {
     struct tcp_transport *tcp;
@@ -545,6 +546,7 @@ static pj_status_t tcp_create( struct tcp_listener *listener,
     pj_activesock_cb tcp_callback;
     const pj_str_t ka_pkt = PJSIP_TCP_KEEP_ALIVE_DATA;
     pj_status_t status;
+    char remote_addr_str[PJ_INET6_ADDRSTRLEN];
     
 
     PJ_ASSERT_RETURN(sock != PJ_INVALID_SOCKET, PJ_EINVAL);
@@ -580,17 +582,17 @@ static pj_status_t tcp_create( struct tcp_listener *listener,
     }
 
     tcp->base.key.type = PJSIP_TRANSPORT_TCP;
-    pj_memcpy(&tcp->base.key.rem_addr, remote, sizeof(pj_sockaddr_in));
+    pj_memcpy(&tcp->base.key.rem_addr, remote, pj_sockaddr_get_len(remote));
     tcp->base.type_name = "tcp";
     tcp->base.flag = pjsip_transport_get_flag_from_type(PJSIP_TRANSPORT_TCP);
 
     tcp->base.info = (char*) pj_pool_alloc(pool, 64);
     pj_ansi_snprintf(tcp->base.info, 64, "TCP to %s:%d",
-		     pj_inet_ntoa(remote->sin_addr), 
-		     (int)pj_ntohs(remote->sin_port));
+                     pj_inet_ntop2(remote->addr.sa_family, remote, remote_addr_str, sizeof(remote_addr_str)),
+                     (int)pj_sockaddr_get_port(remote));
 
-    tcp->base.addr_len = sizeof(pj_sockaddr_in);
-    pj_memcpy(&tcp->base.local_addr, local, sizeof(pj_sockaddr_in));
+    tcp->base.addr_len = pj_sockaddr_get_len(local);
+    pj_memcpy(&tcp->base.local_addr, local, pj_sockaddr_get_len(local));
     sockaddr_to_host_port(pool, &tcp->base.local_name, local);
     sockaddr_to_host_port(pool, &tcp->base.remote_name, remote);
     tcp->base.dir = is_server? PJSIP_TP_DIR_INCOMING : PJSIP_TP_DIR_OUTGOING;
@@ -791,9 +793,10 @@ static pj_status_t tcp_start_read(struct tcp_transport *tcp)
 {
     pj_pool_t *pool;
     pj_ssize_t size;
-    pj_sockaddr_in *rem_addr;
+    pj_sockaddr *rem_addr;
     void *readbuf[1];
     pj_status_t status;
+    char rem_addr_str[PJ_INET6_ADDRSTRLEN];
 
     /* Init rdata */
     pool = pjsip_endpt_create_pool(tcp->base.endpt,
@@ -814,11 +817,11 @@ static pj_status_t tcp_start_read(struct tcp_transport *tcp)
 			   sizeof(pj_ioqueue_op_key_t));
 
     tcp->rdata.pkt_info.src_addr = tcp->base.key.rem_addr;
-    tcp->rdata.pkt_info.src_addr_len = sizeof(pj_sockaddr_in);
-    rem_addr = (pj_sockaddr_in*) &tcp->base.key.rem_addr;
+    tcp->rdata.pkt_info.src_addr_len = pj_sockaddr_get_len(&tcp->base.key.rem_addr);
+    rem_addr = &tcp->base.key.rem_addr;
     pj_ansi_strcpy(tcp->rdata.pkt_info.src_name,
-		   pj_inet_ntoa(rem_addr->sin_addr));
-    tcp->rdata.pkt_info.src_port = pj_ntohs(rem_addr->sin_port);
+           pj_inet_ntop2(rem_addr->addr.sa_family, rem_addr, rem_addr_str, sizeof(rem_addr_str)));
+    tcp->rdata.pkt_info.src_port = pj_sockaddr_get_port(rem_addr);
 
     size = sizeof(tcp->rdata.pkt_info.packet);
     readbuf[0] = tcp->rdata.pkt_info.packet;
@@ -835,6 +838,7 @@ static pj_status_t tcp_start_read(struct tcp_transport *tcp)
 }
 
 
+/* XXX Come back to this function when you're thinking more clearly */
 /* This callback is called by transport manager for the TCP factory
  * to create outgoing transport to the specified destination.
  */
@@ -848,23 +852,37 @@ static pj_status_t lis_create_transport(pjsip_tpfactory *factory,
     struct tcp_listener *listener;
     struct tcp_transport *tcp;
     pj_sock_t sock;
-    pj_sockaddr_in local_addr;
+    pj_sockaddr local_addr;
     pj_status_t status;
+    pj_uint16_t rem_family = rem_addr->addr.sa_family;
 
     /* Sanity checks */
     PJ_ASSERT_RETURN(factory && mgr && endpt && rem_addr &&
 		     addr_len && p_transport, PJ_EINVAL);
 
     /* Check that address is a sockaddr_in */
-    PJ_ASSERT_RETURN(rem_addr->addr.sa_family == pj_AF_INET() &&
-		     addr_len == sizeof(pj_sockaddr_in), PJ_EINVAL);
+    PJ_ASSERT_RETURN((rem_family == pj_AF_INET() &&
+		     addr_len == sizeof(pj_sockaddr_in)) ||
+                     (rem_family == pj_AF_INET6() &&
+                     addr_len == sizeof(pj_sockaddr_in6)), PJ_EINVAL);
+
+    pj_bzero(&local_addr, sizeof(pj_sockaddr));
+
+    if (rem_family == pj_AF_INET())
+    {
+        local_addr.addr.sa_family = local_addr.ipv4.sin_family = pj_AF_INET();
+    }
+    else
+    {
+        local_addr.addr.sa_family = local_addr.ipv6.sin6_family = pj_AF_INET6();
+    }
 
 
     listener = (struct tcp_listener*)factory;
 
     
     /* Create socket */
-    status = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, &sock);
+    status = pj_sock_socket(rem_family, pj_SOCK_STREAM(), 0, &sock);
     if (status != PJ_SUCCESS)
 	return status;
 
@@ -875,14 +893,14 @@ static pj_status_t lis_create_transport(pjsip_tpfactory *factory,
 				"outgoing SIP TCP socket");
 
     /* Bind to any port */
-    status = pj_sock_bind_in(sock, 0, 0);
+    status = pj_sock_bind(sock, &local_addr, pj_sockaddr_get_len(&local_addr));
     if (status != PJ_SUCCESS) {
 	pj_sock_close(sock);
 	return status;
     }
 
     /* Get the local port */
-    addr_len = sizeof(pj_sockaddr_in);
+    addr_len = pj_sockaddr_get_len(&local_addr);
     status = pj_sock_getsockname(sock, &local_addr, &addr_len);
     if (status != PJ_SUCCESS) {
 	pj_sock_close(sock);
@@ -890,12 +908,11 @@ static pj_status_t lis_create_transport(pjsip_tpfactory *factory,
     }
 
     /* Initially set the address from the listener's address */
-    local_addr.sin_addr.s_addr = 
-	((pj_sockaddr_in*)&listener->factory.local_addr)->sin_addr.s_addr;
+    pj_sockaddr_copy_addr(&local_addr, &listener->factory.local_addr);
 
     /* Create the transport descriptor */
     status = tcp_create(listener, NULL, sock, PJ_FALSE, &local_addr, 
-			(pj_sockaddr_in*)rem_addr, &tcp);
+			rem_addr, &tcp);
     if (status != PJ_SUCCESS)
 	return status;
 
@@ -903,7 +920,7 @@ static pj_status_t lis_create_transport(pjsip_tpfactory *factory,
     /* Start asynchronous connect() operation */
     tcp->has_pending_connect = PJ_TRUE;
     status = pj_activesock_start_connect(tcp->asock, tcp->base.pool, rem_addr,
-					 sizeof(pj_sockaddr_in));
+					 pj_sockaddr_get_len(rem_addr));
     if (status == PJ_SUCCESS) {
 	on_connect_complete(tcp->asock, PJ_SUCCESS);
     } else if (status != PJ_EPENDING) {
@@ -915,18 +932,18 @@ static pj_status_t lis_create_transport(pjsip_tpfactory *factory,
 	/* Update (again) local address, just in case local address currently
 	 * set is different now that asynchronous connect() is started.
 	 */
-	addr_len = sizeof(pj_sockaddr_in);
+	addr_len = pj_sockaddr_get_len(&local_addr);
 	if (pj_sock_getsockname(sock, &local_addr, &addr_len)==PJ_SUCCESS) {
-	    pj_sockaddr_in *tp_addr = (pj_sockaddr_in*)&tcp->base.local_addr;
+	    pj_sockaddr *tp_addr = &tcp->base.local_addr;
 
 	    /* Some systems (like old Win32 perhaps) may not set local address
 	     * properly before socket is fully connected.
 	     */
-	    if (tp_addr->sin_addr.s_addr != local_addr.sin_addr.s_addr &&
-		local_addr.sin_addr.s_addr != 0) 
+	    if (memcmp(pj_sockaddr_get_addr(tp_addr), pj_sockaddr_get_addr(&local_addr), pj_sockaddr_get_len(&local_addr)) &&
+		pj_sockaddr_get_port(&local_addr) != 0)
 	    {
-		tp_addr->sin_addr.s_addr = local_addr.sin_addr.s_addr;
-		tp_addr->sin_port = local_addr.sin_port;
+                pj_sockaddr_copy_addr(tp_addr, &local_addr);
+                pj_sockaddr_set_port(tp_addr, pj_sockaddr_get_port(&local_addr));
 		sockaddr_to_host_port(tcp->base.pool, &tcp->base.local_name,
 				      &local_addr);
 	    }
@@ -990,8 +1007,8 @@ static pj_bool_t on_accept_complete(pj_activesock_t *asock,
      * Create TCP transport for the new socket.
      */
     status = tcp_create( listener, NULL, sock, PJ_TRUE,
-			 (const pj_sockaddr_in*)&listener->factory.local_addr,
-			 (const pj_sockaddr_in*)src_addr, &tcp);
+			 &listener->factory.local_addr,
+			 src_addr, &tcp);
     if (status == PJ_SUCCESS) {
 	status = tcp_start_read(tcp);
 	if (status != PJ_SUCCESS) {
@@ -1095,7 +1112,7 @@ static pj_status_t tcp_send_msg(pjsip_transport *transport,
     PJ_ASSERT_RETURN(tdata->op_key.tdata == NULL, PJSIP_EPENDINGTX);
     
     /* Check the address is supported */
-    PJ_ASSERT_RETURN(rem_addr && addr_len==sizeof(pj_sockaddr_in), PJ_EINVAL);
+    PJ_ASSERT_RETURN(rem_addr && addr_len == pj_sockaddr_get_len(rem_addr), PJ_EINVAL);
 
 
 
@@ -1269,7 +1286,7 @@ static pj_bool_t on_connect_complete(pj_activesock_t *asock,
 				     pj_status_t status)
 {
     struct tcp_transport *tcp;
-    pj_sockaddr_in addr;
+    pj_sockaddr addr;
     int addrlen;
     pjsip_tp_state_callback state_cb;
 
@@ -1314,15 +1331,15 @@ static pj_bool_t on_connect_complete(pj_activesock_t *asock,
      * set is different now that the socket is connected (could happen
      * on some systems, like old Win32 probably?).
      */
-    addrlen = sizeof(pj_sockaddr_in);
+    addrlen = pj_sockaddr_get_len(&tcp->base.local_addr);
     if (pj_sock_getsockname(tcp->sock, &addr, &addrlen)==PJ_SUCCESS) {
-	pj_sockaddr_in *tp_addr = (pj_sockaddr_in*)&tcp->base.local_addr;
+	pj_sockaddr *tp_addr = &tcp->base.local_addr;
 
 	if (pj_sockaddr_has_addr(&addr) &&
-	    tp_addr->sin_addr.s_addr != addr.sin_addr.s_addr) 
+	    memcmp(pj_sockaddr_get_addr(tp_addr), pj_sockaddr_get_addr(&addr), pj_sockaddr_get_len(&addr))) 
 	{
-	    tp_addr->sin_addr.s_addr = addr.sin_addr.s_addr;
-	    tp_addr->sin_port = addr.sin_port;
+            pj_sockaddr_copy_addr(tp_addr, &addr);
+            pj_sockaddr_set_port(tp_addr, pj_sockaddr_get_port(&addr));
 	    sockaddr_to_host_port(tcp->base.pool, &tcp->base.local_name,
 				  tp_addr);
 	}

-----------------------------------------------------------------------


-- 
asterisk-scf/release/pjproject.git



More information about the asterisk-scf-commits mailing list