/* * Convert text to IPv4/IPv6 address. */ PJ_DEF(pj_status_t) pj_inet_pton(int af, const pj_str_t *src, void *dst) { char tempaddr[PJ_INET6_ADDRSTRLEN]; PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL); PJ_ASSERT_RETURN(src && src->slen && dst, PJ_EINVAL); /* Initialize output with PJ_IN_ADDR_NONE for IPv4 (to be * compatible with pj_inet_aton() */ if (af==PJ_AF_INET) { ((pj_in_addr*)dst)->s_addr = PJ_INADDR_NONE; } /* Caution: * this function might be called with cp->slen >= 46 * (i.e. when called with hostname to check if it's an IP addr). */ if (src->slen >= PJ_INET6_ADDRSTRLEN) { return PJ_ENAMETOOLONG; } pj_memcpy(tempaddr, src->ptr, src->slen); tempaddr[src->slen] = '\0'; wchar_t tempaddr16[PJ_INET6_ADDRSTRLEN]; pj_ansi_to_unicode(tempaddr, pj_ansi_strlen(tempaddr), tempaddr16, sizeof(tempaddr16)); TBuf<PJ_INET6_ADDRSTRLEN> ip_addr((const TText*)tempaddr16); TInetAddr addr; addr.Init(KAfInet6); if (addr.Input(ip_addr) == KErrNone) { if (af==PJ_AF_INET) { /* Success (Symbian IP address is in host byte order) */ pj_uint32_t ip = pj_htonl(addr.Address()); pj_memcpy(dst, &ip, 4); } else if (af==PJ_AF_INET6) { const TIp6Addr & ip6 = addr.Ip6Address(); pj_memcpy(dst, ip6.u.iAddr8, 16); } else { pj_assert(!"Unexpected!"); return PJ_EBUG; } return PJ_SUCCESS; } else { /* Error */ return PJ_EINVAL; } }
static int connect_test() { RSocketServ rSockServ; RSocket rSock; TInetAddr inetAddr; TRequestStatus reqStatus; char buffer[16]; TPtrC8 data((const TUint8*)buffer, (TInt)sizeof(buffer)); int rc; rc = rSockServ.Connect(); if (rc != KErrNone) return rc; rc = rSock.Open(rSockServ, KAfInet, KSockDatagram, KProtocolInetUdp); if (rc != KErrNone) { rSockServ.Close(); return rc; } inetAddr.Init(KAfInet); inetAddr.Input(_L("127.0.0.1")); inetAddr.SetPort(80); rSock.Connect(inetAddr, reqStatus); User::WaitForRequest(reqStatus); if (reqStatus != KErrNone) { rSock.Close(); rSockServ.Close(); return rc; } rSock.Send(data, 0, reqStatus); User::WaitForRequest(reqStatus); if (reqStatus!=KErrNone) { rSock.Close(); rSockServ.Close(); return rc; } rSock.Close(); rSockServ.Close(); return KErrNone; }
/* * This function converts the Internet host address cp from the standard * numbers-and-dots notation into binary data and stores it in the structure * that inp points to. */ PJ_DEF(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp) { enum { MAXIPLEN = PJ_INET_ADDRSTRLEN }; /* Initialize output with PJ_INADDR_NONE. * Some apps relies on this instead of the return value * (and anyway the return value is quite confusing!) */ inp->s_addr = PJ_INADDR_NONE; /* Caution: * this function might be called with cp->slen >= 16 * (i.e. when called with hostname to check if it's an IP addr). */ PJ_ASSERT_RETURN(cp && cp->slen && inp, 0); if (cp->slen >= 16) { return 0; } char tempaddr8[MAXIPLEN]; pj_memcpy(tempaddr8, cp->ptr, cp->slen); tempaddr8[cp->slen] = '\0'; wchar_t tempaddr16[MAXIPLEN]; pj_ansi_to_unicode(tempaddr8, pj_ansi_strlen(tempaddr8), tempaddr16, sizeof(tempaddr16)); TBuf<MAXIPLEN> ip_addr((const TText*)tempaddr16); TInetAddr addr; addr.Init(KAfInet); if (addr.Input(ip_addr) == KErrNone) { /* Success (Symbian IP address is in host byte order) */ inp->s_addr = pj_htonl(addr.Address()); return 1; } else { /* Error */ return 0; } }