Changeset 5284


Ignore:
Timestamp:
May 10, 2016 5:13:57 AM (8 years ago)
Author:
nanang
Message:

Close #1915: Added API pjsip_udp_transport_start2() that can create both IPv4 and IPv6 SIP UDP transports.

Location:
pjproject/trunk/pjsip
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip/include/pjsip/sip_transport_udp.h

    r3553 r5284  
    2727 
    2828#include <pjsip/sip_transport.h> 
     29#include <pj/sock_qos.h> 
    2930 
    3031PJ_BEGIN_DECL 
     
    5859    PJSIP_UDP_TRANSPORT_DESTROY_SOCKET  = 2 
    5960}; 
     61 
     62 
     63/** 
     64 * Settings to be specified when creating the UDP transport. Application  
     65 * should initialize this structure with its default values by calling  
     66 * pjsip_udp_transport_cfg_default(). 
     67 */ 
     68typedef struct pjsip_udp_transport_cfg 
     69{ 
     70    /** 
     71     * Address family to use. Valid values are pj_AF_INET() and 
     72     * pj_AF_INET6(). Default is pj_AF_INET(). 
     73     */ 
     74    int                 af; 
     75 
     76    /** 
     77     * Address to bind the socket to. 
     78     */ 
     79    pj_sockaddr         bind_addr; 
     80 
     81    /** 
     82     * Optional published address, which is the address to be 
     83     * advertised as the address of this SIP transport.  
     84     * By default the bound address will be used as the published address. 
     85     */ 
     86    pjsip_host_port     addr_name; 
     87 
     88    /** 
     89     * Number of simultaneous asynchronous accept() operations to be  
     90     * supported. It is recommended that the number here corresponds to  
     91     * the number of processors in the system (or the number of SIP 
     92     * worker threads). 
     93     * 
     94     * Default: 1 
     95     */ 
     96    unsigned            async_cnt; 
     97 
     98    /** 
     99     * QoS traffic type to be set on this transport. When application wants 
     100     * to apply QoS tagging to the transport, it's preferable to set this 
     101     * field rather than \a qos_param fields since this is more portable. 
     102     * 
     103     * Default is QoS not set. 
     104     */ 
     105    pj_qos_type         qos_type; 
     106 
     107    /** 
     108     * Set the low level QoS parameters to the transport. This is a lower 
     109     * level operation than setting the \a qos_type field and may not be 
     110     * supported on all platforms. 
     111     * 
     112     * Default is QoS not set. 
     113     */ 
     114    pj_qos_params       qos_params; 
     115 
     116    /** 
     117     * Specify options to be set on the transport.  
     118     * 
     119     * By default there is no options. 
     120     *  
     121     */ 
     122    pj_sockopt_params   sockopt_params; 
     123 
     124} pjsip_udp_transport_cfg; 
     125 
     126 
     127/** 
     128 * Initialize pjsip_udp_transport_cfg structure with default values for 
     129 * the specifed address family. 
     130 * 
     131 * @param cfg           The structure to initialize. 
     132 * @param af            Address family to be used. 
     133 */ 
     134PJ_DECL(void) pjsip_udp_transport_cfg_default(pjsip_udp_transport_cfg *cfg, 
     135                                              int af); 
     136 
     137 
     138/** 
     139 * Start UDP IPv4/IPv6 transport. 
     140 * 
     141 * @param endpt         The SIP endpoint. 
     142 * @param cfg           UDP transport settings. Application should initialize 
     143 *                      this setting with #pjsip_udp_transport_cfg_default(). 
     144 * @param p_transport   Pointer to receive the transport. 
     145 * 
     146 * @return              PJ_SUCCESS when the transport has been successfully 
     147 *                      started and registered to transport manager, or 
     148 *                      the appropriate error code. 
     149 */ 
     150PJ_DECL(pj_status_t) pjsip_udp_transport_start2( 
     151                                        pjsip_endpoint *endpt, 
     152                                        const pjsip_udp_transport_cfg *cfg, 
     153                                        pjsip_transport **p_transport); 
    60154 
    61155 
  • pjproject/trunk/pjsip/src/pjsip/sip_transport_udp.c

    r5230 r5284  
    870870} 
    871871 
     872 
     873/* 
     874 * Initialize pjsip_udp_transport_cfg structure with default values. 
     875 */ 
     876PJ_DEF(void) pjsip_udp_transport_cfg_default(pjsip_udp_transport_cfg *cfg, 
     877                                             int af) 
     878{ 
     879    pj_bzero(cfg, sizeof(*cfg)); 
     880    cfg->af = af; 
     881    pj_sockaddr_init(cfg->af, &cfg->bind_addr, NULL, 0); 
     882    cfg->async_cnt = 1; 
     883} 
     884 
     885 
     886/* 
     887 * pjsip_udp_transport_start2() 
     888 * 
     889 * Create a UDP socket in the specified address and start a transport. 
     890 */ 
     891PJ_DEF(pj_status_t) pjsip_udp_transport_start2( 
     892                                        pjsip_endpoint *endpt, 
     893                                        const pjsip_udp_transport_cfg *cfg, 
     894                                        pjsip_transport **p_transport) 
     895{ 
     896    pj_sock_t sock; 
     897    pj_status_t status; 
     898    pjsip_host_port addr_name; 
     899    char addr_buf[PJ_INET6_ADDRSTRLEN]; 
     900    pjsip_transport_type_e transport_type; 
     901    pj_uint16_t af; 
     902    int addr_len; 
     903 
     904    PJ_ASSERT_RETURN(endpt && cfg && cfg->async_cnt, PJ_EINVAL); 
     905 
     906    if (cfg->bind_addr.addr.sa_family == pj_AF_INET()) { 
     907        af = pj_AF_INET(); 
     908        transport_type = PJSIP_TRANSPORT_UDP; 
     909        addr_len = sizeof(pj_sockaddr_in); 
     910    } else { 
     911        af = pj_AF_INET6(); 
     912        transport_type = PJSIP_TRANSPORT_UDP6; 
     913        addr_len = sizeof(pj_sockaddr_in6); 
     914    } 
     915 
     916    status = create_socket(af, &cfg->bind_addr, addr_len, &sock); 
     917    if (status != PJ_SUCCESS) 
     918        return status; 
     919 
     920    /* Apply QoS, if specified */ 
     921    pj_sock_apply_qos2(sock, cfg->qos_type, &cfg->qos_params, 
     922                       2, THIS_FILE, "SIP UDP transport"); 
     923 
     924    /* Apply sockopt, if specified */ 
     925    if (cfg->sockopt_params.cnt) 
     926        pj_sock_setsockopt_params(sock, &cfg->sockopt_params); 
     927 
     928    if (cfg->addr_name.host.slen == 0) { 
     929        /* Address name is not specified. 
     930         * Build a name based on bound address. 
     931         */ 
     932        status = get_published_name(sock, addr_buf, sizeof(addr_buf), 
     933                                    &addr_name); 
     934        if (status != PJ_SUCCESS) { 
     935            pj_sock_close(sock); 
     936            return status; 
     937        } 
     938    } else { 
     939        addr_name = cfg->addr_name; 
     940    } 
     941 
     942    return pjsip_udp_transport_attach2(endpt, transport_type, sock, 
     943                                       &addr_name, cfg->async_cnt, 
     944                                       p_transport); 
     945} 
     946 
    872947/* 
    873948 * pjsip_udp_transport_start() 
     
    881956                                               pjsip_transport **p_transport) 
    882957{ 
    883     pj_sock_t sock; 
    884     pj_status_t status; 
    885     char addr_buf[PJ_INET6_ADDRSTRLEN]; 
    886     pjsip_host_port bound_name; 
    887  
    888     PJ_ASSERT_RETURN(endpt && async_cnt, PJ_EINVAL); 
    889  
    890     status = create_socket(pj_AF_INET(), local_a, sizeof(pj_sockaddr_in),  
    891                            &sock); 
    892     if (status != PJ_SUCCESS) 
    893         return status; 
    894  
    895     if (a_name == NULL) { 
    896         /* Address name is not specified.  
    897          * Build a name based on bound address. 
    898          */ 
    899         status = get_published_name(sock, addr_buf, sizeof(addr_buf),  
    900                                     &bound_name); 
    901         if (status != PJ_SUCCESS) { 
    902             pj_sock_close(sock); 
    903             return status; 
    904         } 
    905  
    906         a_name = &bound_name; 
    907     } 
    908  
    909     return pjsip_udp_transport_attach( endpt, sock, a_name, async_cnt,  
    910                                        p_transport ); 
    911 } 
    912  
     958    pjsip_udp_transport_cfg cfg; 
     959 
     960    pjsip_udp_transport_cfg_default(&cfg, pj_AF_INET()); 
     961    if (local_a) 
     962        pj_sockaddr_cp(&cfg.bind_addr, local_a); 
     963    if (a_name) 
     964        cfg.addr_name = *a_name; 
     965    cfg.async_cnt = async_cnt; 
     966 
     967    return pjsip_udp_transport_start2(endpt, &cfg, p_transport); 
     968} 
    913969 
    914970/* 
     
    923979                                               pjsip_transport **p_transport) 
    924980{ 
    925     pj_sock_t sock; 
    926     pj_status_t status; 
    927     char addr_buf[PJ_INET6_ADDRSTRLEN]; 
    928     pjsip_host_port bound_name; 
    929  
    930     PJ_ASSERT_RETURN(endpt && async_cnt, PJ_EINVAL); 
    931  
    932     status = create_socket(pj_AF_INET6(), local_a, sizeof(pj_sockaddr_in6),  
    933                            &sock); 
    934     if (status != PJ_SUCCESS) 
    935         return status; 
    936  
    937     if (a_name == NULL) { 
    938         /* Address name is not specified.  
    939          * Build a name based on bound address. 
    940          */ 
    941         status = get_published_name(sock, addr_buf, sizeof(addr_buf),  
    942                                     &bound_name); 
    943         if (status != PJ_SUCCESS) { 
    944             pj_sock_close(sock); 
    945             return status; 
    946         } 
    947  
    948         a_name = &bound_name; 
    949     } 
    950  
    951     return pjsip_udp_transport_attach2(endpt, PJSIP_TRANSPORT_UDP6, 
    952                                        sock, a_name, async_cnt, p_transport); 
     981    pjsip_udp_transport_cfg cfg; 
     982 
     983    pjsip_udp_transport_cfg_default(&cfg, pj_AF_INET6()); 
     984    if (local_a) 
     985        pj_sockaddr_cp(&cfg.bind_addr, local_a); 
     986    if (a_name) 
     987        cfg.addr_name = *a_name; 
     988    cfg.async_cnt = async_cnt; 
     989 
     990    return pjsip_udp_transport_start2(endpt, &cfg, p_transport); 
    953991} 
    954992 
Note: See TracChangeset for help on using the changeset viewer.