Ignore:
Timestamp:
Mar 24, 2007 5:37:25 PM (17 years ago)
Author:
bennylp
Message:

Added generic DNS SRV resolution in pjlib-util

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c

    r1101 r1102  
    653653} 
    654654 
     655 
     656static pj_status_t resolve_stun_server(pj_bool_t use_dns_srv); 
     657 
     658/* 
     659 * Callback function to receive notification from the resolver 
     660 * when the resolution process completes. 
     661 */ 
     662static void stun_dns_srv_resolver_cb(void *user_data, 
     663                                     pj_status_t status, 
     664                                     const pj_dns_srv_record *rec) 
     665{ 
     666    unsigned i; 
     667 
     668    PJ_UNUSED_ARG(user_data); 
     669 
     670    pjsua_var.stun_status = status; 
     671     
     672    if (status != PJ_SUCCESS) { 
     673        /* DNS SRV resolution failed. If stun_host is specified, resolve 
     674         * it with gethostbyname() 
     675         */ 
     676        if (pjsua_var.ua_cfg.stun_host.slen) { 
     677            pj_hostent he; 
     678 
     679            pjsua_var.stun_status = pj_gethostbyname(&pjsua_var.ua_cfg.stun_host, &he); 
     680 
     681            if (pjsua_var.stun_status == PJ_SUCCESS) { 
     682                pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0); 
     683                pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr; 
     684                pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)3478); 
     685 
     686                PJ_LOG(3,(THIS_FILE,  
     687                          "STUN server %.*s resolved, address is %s:%d", 
     688                          (int)pjsua_var.ua_cfg.stun_host.slen, 
     689                          pjsua_var.ua_cfg.stun_host.ptr, 
     690                          pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr), 
     691                          (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port))); 
     692            } 
     693        } else { 
     694            char errmsg[PJ_ERR_MSG_SIZE]; 
     695 
     696            pj_strerror(status, errmsg, sizeof(errmsg)); 
     697            PJ_LOG(1,(THIS_FILE,  
     698                     "DNS SRV resolution failed for STUN server %.*s: %s", 
     699                     (int)pjsua_var.ua_cfg.stun_domain.slen, 
     700                     pjsua_var.ua_cfg.stun_domain.ptr, 
     701                     errmsg)); 
     702        } 
     703        return; 
     704    } 
     705 
     706    pj_memcpy(&pjsua_var.stun_srv, &rec->entry[0].addr,  
     707              rec->entry[0].addr_len); 
     708 
     709    PJ_LOG(3,(THIS_FILE, "_stun._udp.%.*s resolved, found %d entry(s):", 
     710              (int)pjsua_var.ua_cfg.stun_domain.slen, 
     711              pjsua_var.ua_cfg.stun_domain.ptr, 
     712              rec->count)); 
     713 
     714    for (i=0; i<rec->count; ++i) { 
     715        PJ_LOG(3,(THIS_FILE,  
     716                  " %d: prio=%d, weight=%d  %s:%d",  
     717                  i, rec->entry[i].priority, rec->entry[i].weight, 
     718                  pj_inet_ntoa(rec->entry[i].addr.ipv4.sin_addr), 
     719                  (int)pj_ntohs(rec->entry[i].addr.ipv4.sin_port))); 
     720    } 
     721 
     722} 
     723 
    655724/* 
    656725 * Resolve STUN server. 
     
    665734 
    666735        /* Start STUN server resolution */ 
    667         /* For now just do DNS A resolution */ 
    668  
    669         if (pjsua_var.ua_cfg.stun_srv.slen == 0) { 
    670             pjsua_var.stun_status = PJ_SUCCESS; 
    671         } else { 
     736         
     737        pjsua_var.stun_status = PJ_EPENDING; 
     738 
     739        /* If stun_domain is specified, resolve STUN servers with DNS 
     740         * SRV resolution. 
     741         */ 
     742        if (pjsua_var.ua_cfg.stun_domain.slen) { 
     743            pj_str_t res_type; 
     744 
     745            /* Fail if resolver is not configured */ 
     746            if (pjsua_var.resolver == NULL) { 
     747                PJ_LOG(1,(THIS_FILE, "Nameserver must be configured when " 
     748                                     "stun_domain is specified")); 
     749                pjsua_var.stun_status = PJLIB_UTIL_EDNSNONS; 
     750                return PJLIB_UTIL_EDNSNONS; 
     751            } 
     752            res_type = pj_str("_stun._udp"); 
     753            pjsua_var.stun_status =  
     754                pj_dns_srv_resolve(&pjsua_var.ua_cfg.stun_domain, &res_type, 
     755                                   3478, pjsua_var.pool, pjsua_var.resolver, 
     756                                   0, NULL, stun_dns_srv_resolver_cb); 
     757            if (pjsua_var.stun_status != PJ_SUCCESS) { 
     758                pjsua_perror(THIS_FILE, "Error starting DNS SRV resolution",  
     759                             pjsua_var.stun_status); 
     760                return pjsua_var.stun_status; 
     761            } 
     762        } 
     763        /* Otherwise if stun_host is specified, resolve STUN server with 
     764         * gethostbyname(). 
     765         */ 
     766        else if (pjsua_var.ua_cfg.stun_host.slen) { 
    672767            pj_hostent he; 
    673768 
    674             pjsua_var.stun_status =  
    675                 pj_gethostbyname(&pjsua_var.ua_cfg.stun_srv, &he); 
     769            pjsua_var.stun_status = pj_gethostbyname(&pjsua_var.ua_cfg.stun_host, &he); 
    676770 
    677771            if (pjsua_var.stun_status == PJ_SUCCESS) { 
     
    680774                pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)3478); 
    681775 
    682                 PJ_LOG(4,(THIS_FILE,  
     776                PJ_LOG(3,(THIS_FILE,  
    683777                          "STUN server %.*s resolved, address is %s:%d", 
    684                           (int)pjsua_var.ua_cfg.stun_srv.slen, 
    685                           pjsua_var.ua_cfg.stun_srv.ptr, 
     778                          (int)pjsua_var.ua_cfg.stun_host.slen, 
     779                          pjsua_var.ua_cfg.stun_host.ptr, 
    686780                          pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr), 
    687781                          (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port))); 
    688782            } 
    689         } 
     783 
     784        } 
     785        /* Otherwise disable STUN. */ 
     786        else { 
     787            pjsua_var.stun_status = PJ_SUCCESS; 
     788        } 
     789 
     790 
    690791        return pjsua_var.stun_status; 
    691792 
     
    694795         * result. 
    695796         */ 
    696         pj_assert(!"Should not happen"); 
    697         return PJ_EBUG; 
     797        if (wait) { 
     798            while (pjsua_var.stun_status == PJ_EPENDING) 
     799                pjsua_handle_events(10); 
     800        } 
     801 
     802        return pjsua_var.stun_status; 
    698803 
    699804    } else { 
     
    9471052                                         p_pub_addr); 
    9481053        if (status != PJ_SUCCESS) { 
    949             pjsua_perror(THIS_FILE, "Error resolving with STUN", status); 
     1054            pjsua_perror(THIS_FILE, "Error contacting STUN server", status); 
    9501055            pj_sock_close(sock); 
    9511056            return status; 
Note: See TracChangeset for help on using the changeset viewer.