Changeset 1588


Ignore:
Timestamp:
Nov 21, 2007 2:10:46 PM (16 years ago)
Author:
bennylp
Message:

Added pj_inet_pton() and pj_inet_ntop() to Symbian (part of ticket #415 for IPv6 support)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/src/pj/sock_symbian.cpp

    r1525 r1588  
    3131 * Address families. 
    3232 */ 
     33const pj_uint16_t PJ_AF_UNSPEC  = KAFUnspec; 
    3334const pj_uint16_t PJ_AF_UNIX    = 0xFFFF; 
    3435const pj_uint16_t PJ_AF_INET    = KAfInet; 
     
    331332        return 0; 
    332333    } 
     334} 
     335 
     336/* 
     337 * Convert text to IPv4/IPv6 address. 
     338 */ 
     339PJ_DEF(pj_status_t) pj_inet_pton(int af, const pj_str_t *src, void *dst) 
     340{ 
     341    char tempaddr[PJ_INET6_ADDRSTRLEN]; 
     342 
     343    PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL); 
     344    PJ_ASSERT_RETURN(src && src->slen && dst, PJ_EINVAL); 
     345 
     346    /* Initialize output with PJ_IN_ADDR_NONE for IPv4 (to be  
     347     * compatible with pj_inet_aton() 
     348     */ 
     349    if (af==PJ_AF_INET) { 
     350        ((pj_in_addr*)dst)->s_addr = PJ_INADDR_NONE; 
     351    } 
     352 
     353    /* Caution: 
     354     *  this function might be called with cp->slen >= 46 
     355     *  (i.e. when called with hostname to check if it's an IP addr). 
     356     */ 
     357    if (src->slen >= PJ_INET6_ADDRSTRLEN) { 
     358        return PJ_ENAMETOOLONG; 
     359    } 
     360 
     361    pj_memcpy(tempaddr, src->ptr, src->slen); 
     362    tempaddr[src->slen] = '\0'; 
     363 
     364 
     365    wchar_t tempaddr16[PJ_INET6_ADDRSTRLEN]; 
     366    pj_ansi_to_unicode(tempaddr, pj_ansi_strlen(tempaddr), 
     367                       tempaddr16, sizeof(tempaddr16)); 
     368 
     369    TBuf<PJ_INET6_ADDRSTRLEN> ip_addr((const TText*)tempaddr16); 
     370 
     371    TInetAddr addr; 
     372    addr.Init(KAfInet6); 
     373    if (addr.Input(ip_addr) == KErrNone) { 
     374        if (af==PJ_AF_INET) { 
     375            /* Success (Symbian IP address is in host byte order) */ 
     376            pj_uint32_t ip = pj_htonl(addr.Address()); 
     377            pj_memcpy(dst, &ip, 4); 
     378        } else if (af==PJ_AF_INET6) { 
     379            const TIp6Addr & ip6 = addr.Ip6Address(); 
     380            pj_memcpy(dst, ip6.u.iAddr8, 16); 
     381        } else { 
     382            pj_assert(!"Unexpected!"); 
     383            return PJ_EBUG; 
     384        } 
     385        return PJ_SUCCESS; 
     386    } else { 
     387        /* Error */ 
     388        return PJ_EINVAL; 
     389    } 
     390} 
     391 
     392/* 
     393 * Convert IPv4/IPv6 address to text. 
     394 */ 
     395PJ_DEF(pj_status_t) pj_inet_ntop(int af, const void *src, 
     396                                 char *dst, int size) 
     397 
     398{ 
     399    PJ_ASSERT_RETURN(src && dst && size, PJ_EINVAL); 
     400 
     401    *dst = '\0'; 
     402 
     403    if (af==PJ_AF_INET) { 
     404 
     405        TBuf<PJ_INET_ADDRSTRLEN> str16; 
     406        pj_in_addr inaddr; 
     407 
     408        if (size <= PJ_INET_ADDRSTRLEN) 
     409            return PJ_ETOOSMALL; 
     410 
     411        pj_memcpy(&inaddr, src, 4); 
     412 
     413        /* Symbian IP address is in host byte order */ 
     414        TInetAddr temp_addr((TUint32)pj_ntohl(inaddr.s_addr), (TUint)0); 
     415        temp_addr.Output(str16); 
     416  
     417        pj_unicode_to_ansi((const wchar_t*)str16.PtrZ(), str16.Length(), 
     418                           dst, size); 
     419        return PJ_SUCCESS; 
     420 
     421    } else if (af==PJ_AF_INET6) { 
     422        TBuf<PJ_INET6_ADDRSTRLEN> str16; 
     423 
     424        if (size <= PJ_INET6_ADDRSTRLEN) 
     425            return PJ_ETOOSMALL; 
     426 
     427        TIp6Addr ip6; 
     428        pj_memcpy(ip6.u.iAddr8, src, 16); 
     429 
     430        TInetAddr temp_addr(ip6, (TUint)0); 
     431        temp_addr.Output(str16); 
     432  
     433        pj_unicode_to_ansi((const wchar_t*)str16.PtrZ(), str16.Length(), 
     434                           dst, size); 
     435        return PJ_SUCCESS; 
     436 
     437    } else { 
     438        pj_assert(!"Unsupport address family"); 
     439        return PJ_EINVAL; 
     440    } 
     441 
    333442} 
    334443 
Note: See TracChangeset for help on using the changeset viewer.