From 0af1d2bb99bc9068a06d65511af68bd10f731ba2 Mon Sep 17 00:00:00 2001 From: Liong Sauw Ming Date: Tue, 26 Apr 2011 03:07:24 +0000 Subject: Fixed #1246: Use CFHost for pj_getaddrinfo() on iOS * Replace the fix for ticket #1104 with this fix * Modify pjturn-client/client_main's shutdown() function which conflicts with an existing function Re-run configure-iphone to use this fix automatically. git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@3543 74dad513-b988-da41-8d7b-12977e46ad98 --- pjlib/include/pj/compat/os_auto.h.in | 4 +-- pjlib/src/pj/addr_resolv_sock.c | 60 +++++++++++++++++++++++++++++++++++- pjlib/src/pj/sock_bsd.c | 17 ---------- 3 files changed, 61 insertions(+), 20 deletions(-) (limited to 'pjlib') diff --git a/pjlib/include/pj/compat/os_auto.h.in b/pjlib/include/pj/compat/os_auto.h.in index 97501a55..1dc88028 100644 --- a/pjlib/include/pj/compat/os_auto.h.in +++ b/pjlib/include/pj/compat/os_auto.h.in @@ -175,9 +175,9 @@ # include "TargetConditionals.h" # if TARGET_OS_IPHONE # include "Availability.h" + /* Use CFHost API for pj_getaddrinfo() (see ticket #1246) */ +# define PJ_GETADDRINFO_USE_CFHOST 1 # ifdef __IPHONE_4_0 - /* Append ".local" suffix to the system's hostname? (see ticket #1104) */ -# define PJ_GETHOSTNAME_APPEND_LOCAL_SUFFIX 1 /* Is multitasking support available? (see ticket #1107) */ # define PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT 1 /* Enable activesock TCP background mode support */ diff --git a/pjlib/src/pj/addr_resolv_sock.c b/pjlib/src/pj/addr_resolv_sock.c index eee2c9f8..659eb994 100644 --- a/pjlib/src/pj/addr_resolv_sock.c +++ b/pjlib/src/pj/addr_resolv_sock.c @@ -24,6 +24,10 @@ #include #include +#if defined(PJ_GETADDRINFO_USE_CFHOST) && PJ_GETADDRINFO_USE_CFHOST!=0 +# include +# include +#endif PJ_DEF(pj_status_t) pj_gethostbyname(const pj_str_t *hostname, pj_hostent *phe) { @@ -62,10 +66,16 @@ PJ_DEF(pj_status_t) pj_getaddrinfo(int af, const pj_str_t *nodename, { #if defined(PJ_SOCK_HAS_GETADDRINFO) && PJ_SOCK_HAS_GETADDRINFO!=0 char nodecopy[PJ_MAX_HOSTNAME]; - struct addrinfo hint, *res, *orig_res; pj_bool_t has_addr = PJ_FALSE; unsigned i; +#if defined(PJ_GETADDRINFO_USE_CFHOST) && PJ_GETADDRINFO_USE_CFHOST!=0 + CFStringRef hostname; + CFHostRef hostRef; + pj_status_t status = PJ_SUCCESS; +#else int rc; + struct addrinfo hint, *res, *orig_res; +#endif PJ_ASSERT_RETURN(nodename && count && *count && ai, PJ_EINVAL); PJ_ASSERT_RETURN(nodename->ptr && nodename->slen, PJ_EINVAL); @@ -105,6 +115,53 @@ PJ_DEF(pj_status_t) pj_getaddrinfo(int af, const pj_str_t *nodename, pj_memcpy(nodecopy, nodename->ptr, nodename->slen); nodecopy[nodename->slen] = '\0'; +#if defined(PJ_GETADDRINFO_USE_CFHOST) && PJ_GETADDRINFO_USE_CFHOST!=0 + hostname = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, nodecopy, + kCFStringEncodingASCII, + kCFAllocatorNull); + hostRef = CFHostCreateWithName(kCFAllocatorDefault, hostname); + if (CFHostStartInfoResolution(hostRef, kCFHostAddresses, nil)) { + CFArrayRef addrRef = CFHostGetAddressing(hostRef, nil); + i = 0; + if (addrRef != nil) { + CFIndex idx, naddr; + + naddr = CFArrayGetCount(addrRef); + for (idx = 0; idx < naddr && i < *count; idx++) { + struct sockaddr *addr; + + addr = (struct sockaddr *) + CFDataGetBytePtr(CFArrayGetValueAtIndex(addrRef, idx)); + /* This should not happen. */ + pj_assert(addr); + + /* Ignore unwanted address families */ + if (af!=PJ_AF_UNSPEC && addr->sa_family != af) + continue; + + /* Store canonical name */ + pj_ansi_strcpy(ai[i].ai_canonname, nodecopy); + + /* Store address */ + PJ_ASSERT_ON_FAIL(sizeof(*addr) <= sizeof(pj_sockaddr), + continue); + pj_memcpy(&ai[i].ai_addr, addr, sizeof(*addr)); + PJ_SOCKADDR_RESET_LEN(&ai[i].ai_addr); + + i++; + } + } + + *count = i; + } else { + status = PJ_ERESOLVE; + } + + CFRelease(hostRef); + CFRelease(hostname); + + return status; +#else /* Call getaddrinfo() */ pj_bzero(&hint, sizeof(hint)); hint.ai_family = af; @@ -145,6 +202,7 @@ PJ_DEF(pj_status_t) pj_getaddrinfo(int af, const pj_str_t *nodename, /* Done */ return PJ_SUCCESS; +#endif #else /* PJ_SOCK_HAS_GETADDRINFO */ pj_bool_t has_addr = PJ_FALSE; diff --git a/pjlib/src/pj/sock_bsd.c b/pjlib/src/pj/sock_bsd.c index f39beca5..d89a5281 100644 --- a/pjlib/src/pj/sock_bsd.c +++ b/pjlib/src/pj/sock_bsd.c @@ -456,23 +456,6 @@ PJ_DEF(const pj_str_t*) pj_gethostname(void) hostname.slen = 0; } else { hostname.slen = strlen(buf); -#if defined(PJ_GETHOSTNAME_APPEND_LOCAL_SUFFIX) && \ - PJ_GETHOSTNAME_APPEND_LOCAL_SUFFIX!=0 - { - const pj_str_t suffix = {".local", 6}; - - if (hostname.slen < suffix.slen || - pj_ansi_strnicmp(hostname.ptr + hostname.slen - - suffix.slen, suffix.ptr, suffix.slen)) - { - if (hostname.slen + suffix.slen + 1 < sizeof(buf)) - pj_strcat(&hostname, &suffix); - else - hostname.slen = 0; - hostname.ptr[hostname.slen] = '\0'; - } - } -#endif } } return &hostname; -- cgit v1.2.3