andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: ** File: pripv6.c andre@0: ** Description: Support for various functions unique to IPv6 andre@0: */ andre@0: #include "primpl.h" andre@0: #include andre@0: andre@0: #if !defined(_PR_INET6) || defined(_PR_INET6_PROBE) andre@0: andre@0: static PRIOMethods ipv6_to_v4_tcpMethods; andre@0: static PRIOMethods ipv6_to_v4_udpMethods; andre@0: static PRDescIdentity _pr_ipv6_to_ipv4_id; andre@0: extern PRBool IsValidNetAddr(const PRNetAddr *addr); andre@0: extern PRIPv6Addr _pr_in6addr_any; andre@0: extern PRIPv6Addr _pr_in6addr_loopback; andre@0: andre@0: /* andre@0: * convert an IPv4-mapped IPv6 addr to an IPv4 addr andre@0: */ andre@0: static void _PR_ConvertToIpv4NetAddr(const PRNetAddr *src_v6addr, andre@0: PRNetAddr *dst_v4addr) andre@0: { andre@0: const PRUint8 *srcp; andre@0: andre@0: PR_ASSERT(PR_AF_INET6 == src_v6addr->ipv6.family); andre@0: andre@0: if (PR_IsNetAddrType(src_v6addr, PR_IpAddrV4Mapped)) { andre@0: srcp = src_v6addr->ipv6.ip.pr_s6_addr; andre@0: memcpy((char *) &dst_v4addr->inet.ip, srcp + 12, 4); andre@0: } else if (PR_IsNetAddrType(src_v6addr, PR_IpAddrAny)) { andre@0: dst_v4addr->inet.ip = htonl(INADDR_ANY); andre@0: } else if (PR_IsNetAddrType(src_v6addr, PR_IpAddrLoopback)) { andre@0: dst_v4addr->inet.ip = htonl(INADDR_LOOPBACK); andre@0: } andre@0: dst_v4addr->inet.family = PR_AF_INET; andre@0: dst_v4addr->inet.port = src_v6addr->ipv6.port; andre@0: } andre@0: andre@0: /* andre@0: * convert an IPv4 addr to an IPv4-mapped IPv6 addr andre@0: */ andre@0: static void _PR_ConvertToIpv6NetAddr(const PRNetAddr *src_v4addr, andre@0: PRNetAddr *dst_v6addr) andre@0: { andre@0: PRUint8 *dstp; andre@0: andre@0: PR_ASSERT(PR_AF_INET == src_v4addr->inet.family); andre@0: dst_v6addr->ipv6.family = PR_AF_INET6; andre@0: dst_v6addr->ipv6.port = src_v4addr->inet.port; andre@0: andre@0: if (htonl(INADDR_ANY) == src_v4addr->inet.ip) { andre@0: dst_v6addr->ipv6.ip = _pr_in6addr_any; andre@0: } else { andre@0: dstp = dst_v6addr->ipv6.ip.pr_s6_addr; andre@0: memset(dstp, 0, 10); andre@0: memset(dstp + 10, 0xff, 2); andre@0: memcpy(dstp + 12,(char *) &src_v4addr->inet.ip, 4); andre@0: } andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK Ipv6ToIpv4SocketBind(PRFileDesc *fd, andre@0: const PRNetAddr *addr) andre@0: { andre@0: PRNetAddr tmp_ipv4addr; andre@0: const PRNetAddr *tmp_addrp; andre@0: PRFileDesc *lo = fd->lower; andre@0: andre@0: if (PR_AF_INET6 != addr->raw.family) { andre@0: PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) || andre@0: PR_IsNetAddrType(addr, PR_IpAddrAny)) { andre@0: _PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr); andre@0: tmp_addrp = &tmp_ipv4addr; andre@0: } else { andre@0: PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: return((lo->methods->bind)(lo,tmp_addrp)); andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK Ipv6ToIpv4SocketConnect( andre@0: PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout) andre@0: { andre@0: PRNetAddr tmp_ipv4addr; andre@0: const PRNetAddr *tmp_addrp; andre@0: andre@0: if (PR_AF_INET6 != addr->raw.family) { andre@0: PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) || andre@0: PR_IsNetAddrType(addr, PR_IpAddrLoopback)) { andre@0: _PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr); andre@0: tmp_addrp = &tmp_ipv4addr; andre@0: } else { andre@0: PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: return (fd->lower->methods->connect)(fd->lower, tmp_addrp, timeout); andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketSendTo( andre@0: PRFileDesc *fd, const void *buf, PRInt32 amount, andre@0: PRIntn flags, const PRNetAddr *addr, PRIntervalTime timeout) andre@0: { andre@0: PRNetAddr tmp_ipv4addr; andre@0: const PRNetAddr *tmp_addrp; andre@0: andre@0: if (PR_AF_INET6 != addr->raw.family) { andre@0: PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) || andre@0: PR_IsNetAddrType(addr, PR_IpAddrLoopback)) { andre@0: _PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr); andre@0: tmp_addrp = &tmp_ipv4addr; andre@0: } else { andre@0: PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: return (fd->lower->methods->sendto)( andre@0: fd->lower, buf, amount, flags, tmp_addrp, timeout); andre@0: } andre@0: andre@0: static PRFileDesc* PR_CALLBACK Ipv6ToIpv4SocketAccept ( andre@0: PRFileDesc *fd, PRNetAddr *addr, PRIntervalTime timeout) andre@0: { andre@0: PRStatus rv; andre@0: PRFileDesc *newfd; andre@0: PRFileDesc *newstack; andre@0: PRNetAddr tmp_ipv4addr; andre@0: PRNetAddr *addrlower = NULL; andre@0: andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: newstack = PR_NEW(PRFileDesc); andre@0: if (NULL == newstack) andre@0: { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return NULL; andre@0: } andre@0: *newstack = *fd; /* make a copy of the accepting layer */ andre@0: andre@0: if (addr) andre@0: addrlower = &tmp_ipv4addr; andre@0: newfd = (fd->lower->methods->accept)(fd->lower, addrlower, timeout); andre@0: if (NULL == newfd) andre@0: { andre@0: PR_DELETE(newstack); andre@0: return NULL; andre@0: } andre@0: if (addr) andre@0: _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, addr); andre@0: andre@0: rv = PR_PushIOLayer(newfd, PR_TOP_IO_LAYER, newstack); andre@0: PR_ASSERT(PR_SUCCESS == rv); andre@0: return newfd; /* that's it */ andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketAcceptRead(PRFileDesc *sd, andre@0: PRFileDesc **nd, PRNetAddr **ipv6_raddr, void *buf, PRInt32 amount, andre@0: PRIntervalTime timeout) andre@0: { andre@0: PRInt32 nbytes; andre@0: PRStatus rv; andre@0: PRNetAddr tmp_ipv4addr; andre@0: PRFileDesc *newstack; andre@0: andre@0: PR_ASSERT(sd != NULL); andre@0: PR_ASSERT(sd->lower != NULL); andre@0: andre@0: newstack = PR_NEW(PRFileDesc); andre@0: if (NULL == newstack) andre@0: { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return -1; andre@0: } andre@0: *newstack = *sd; /* make a copy of the accepting layer */ andre@0: andre@0: nbytes = sd->lower->methods->acceptread( andre@0: sd->lower, nd, ipv6_raddr, buf, amount, timeout); andre@0: if (-1 == nbytes) andre@0: { andre@0: PR_DELETE(newstack); andre@0: return nbytes; andre@0: } andre@0: tmp_ipv4addr = **ipv6_raddr; /* copy */ andre@0: _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, *ipv6_raddr); andre@0: andre@0: /* this PR_PushIOLayer call cannot fail */ andre@0: rv = PR_PushIOLayer(*nd, PR_TOP_IO_LAYER, newstack); andre@0: PR_ASSERT(PR_SUCCESS == rv); andre@0: return nbytes; andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK Ipv6ToIpv4SocketGetName(PRFileDesc *fd, andre@0: PRNetAddr *ipv6addr) andre@0: { andre@0: PRStatus result; andre@0: PRNetAddr tmp_ipv4addr; andre@0: andre@0: result = (fd->lower->methods->getsockname)(fd->lower, &tmp_ipv4addr); andre@0: if (PR_SUCCESS == result) { andre@0: _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, ipv6addr); andre@0: PR_ASSERT(IsValidNetAddr(ipv6addr) == PR_TRUE); andre@0: } andre@0: return result; andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK Ipv6ToIpv4SocketGetPeerName(PRFileDesc *fd, andre@0: PRNetAddr *ipv6addr) andre@0: { andre@0: PRStatus result; andre@0: PRNetAddr tmp_ipv4addr; andre@0: andre@0: result = (fd->lower->methods->getpeername)(fd->lower, &tmp_ipv4addr); andre@0: if (PR_SUCCESS == result) { andre@0: _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, ipv6addr); andre@0: PR_ASSERT(IsValidNetAddr(ipv6addr) == PR_TRUE); andre@0: } andre@0: return result; andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketRecvFrom(PRFileDesc *fd, void *buf, andre@0: PRInt32 amount, PRIntn flags, PRNetAddr *ipv6addr, andre@0: PRIntervalTime timeout) andre@0: { andre@0: PRNetAddr tmp_ipv4addr; andre@0: PRInt32 result; andre@0: andre@0: result = (fd->lower->methods->recvfrom)( andre@0: fd->lower, buf, amount, flags, &tmp_ipv4addr, timeout); andre@0: if (-1 != result) { andre@0: _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, ipv6addr); andre@0: PR_ASSERT(IsValidNetAddr(ipv6addr) == PR_TRUE); andre@0: } andre@0: return result; andre@0: } andre@0: andre@0: #if defined(_PR_INET6_PROBE) andre@0: static PRBool ipv6_is_present; andre@0: extern PRBool _pr_test_ipv6_socket(void); andre@0: andre@0: #if !defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYNAME) andre@0: extern PRStatus _pr_find_getipnodebyname(void); andre@0: #endif andre@0: andre@0: #if !defined(_PR_INET6) && defined(_PR_HAVE_GETADDRINFO) andre@0: extern PRStatus _pr_find_getaddrinfo(void); andre@0: #endif andre@0: andre@0: static PRBool andre@0: _pr_probe_ipv6_presence(void) andre@0: { andre@0: #if !defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYNAME) andre@0: if (_pr_find_getipnodebyname() != PR_SUCCESS) andre@0: return PR_FALSE; andre@0: #endif andre@0: andre@0: #if !defined(_PR_INET6) && defined(_PR_HAVE_GETADDRINFO) andre@0: if (_pr_find_getaddrinfo() != PR_SUCCESS) andre@0: return PR_FALSE; andre@0: #endif andre@0: andre@0: return _pr_test_ipv6_socket(); andre@0: } andre@0: #endif /* _PR_INET6_PROBE */ andre@0: andre@0: static PRCallOnceType _pr_init_ipv6_once; andre@0: andre@0: static PRStatus PR_CALLBACK _pr_init_ipv6(void) andre@0: { andre@0: const PRIOMethods *stubMethods; andre@0: andre@0: #if defined(_PR_INET6_PROBE) andre@0: ipv6_is_present = _pr_probe_ipv6_presence(); andre@0: if (ipv6_is_present) andre@0: return PR_SUCCESS; andre@0: #endif andre@0: andre@0: _pr_ipv6_to_ipv4_id = PR_GetUniqueIdentity("Ipv6_to_Ipv4 layer"); andre@0: PR_ASSERT(PR_INVALID_IO_LAYER != _pr_ipv6_to_ipv4_id); andre@0: andre@0: stubMethods = PR_GetDefaultIOMethods(); andre@0: andre@0: ipv6_to_v4_tcpMethods = *stubMethods; /* first get the entire batch */ andre@0: /* then override the ones we care about */ andre@0: ipv6_to_v4_tcpMethods.connect = Ipv6ToIpv4SocketConnect; andre@0: ipv6_to_v4_tcpMethods.bind = Ipv6ToIpv4SocketBind; andre@0: ipv6_to_v4_tcpMethods.accept = Ipv6ToIpv4SocketAccept; andre@0: ipv6_to_v4_tcpMethods.acceptread = Ipv6ToIpv4SocketAcceptRead; andre@0: ipv6_to_v4_tcpMethods.getsockname = Ipv6ToIpv4SocketGetName; andre@0: ipv6_to_v4_tcpMethods.getpeername = Ipv6ToIpv4SocketGetPeerName; andre@0: /* andre@0: ipv6_to_v4_tcpMethods.getsocketoption = Ipv6ToIpv4GetSocketOption; andre@0: ipv6_to_v4_tcpMethods.setsocketoption = Ipv6ToIpv4SetSocketOption; andre@0: */ andre@0: ipv6_to_v4_udpMethods = *stubMethods; /* first get the entire batch */ andre@0: /* then override the ones we care about */ andre@0: ipv6_to_v4_udpMethods.connect = Ipv6ToIpv4SocketConnect; andre@0: ipv6_to_v4_udpMethods.bind = Ipv6ToIpv4SocketBind; andre@0: ipv6_to_v4_udpMethods.sendto = Ipv6ToIpv4SocketSendTo; andre@0: ipv6_to_v4_udpMethods.recvfrom = Ipv6ToIpv4SocketRecvFrom; andre@0: ipv6_to_v4_udpMethods.getsockname = Ipv6ToIpv4SocketGetName; andre@0: ipv6_to_v4_udpMethods.getpeername = Ipv6ToIpv4SocketGetPeerName; andre@0: /* andre@0: ipv6_to_v4_udpMethods.getsocketoption = Ipv6ToIpv4GetSocketOption; andre@0: ipv6_to_v4_udpMethods.setsocketoption = Ipv6ToIpv4SetSocketOption; andre@0: */ andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: #if defined(_PR_INET6_PROBE) andre@0: PRBool _pr_ipv6_is_present(void) andre@0: { andre@0: if (PR_CallOnce(&_pr_init_ipv6_once, _pr_init_ipv6) != PR_SUCCESS) andre@0: return PR_FALSE; andre@0: return ipv6_is_present; andre@0: } andre@0: #endif andre@0: andre@0: PR_IMPLEMENT(PRStatus) _pr_push_ipv6toipv4_layer(PRFileDesc *fd) andre@0: { andre@0: PRFileDesc *ipv6_fd = NULL; andre@0: andre@0: if (PR_CallOnce(&_pr_init_ipv6_once, _pr_init_ipv6) != PR_SUCCESS) andre@0: return PR_FAILURE; andre@0: andre@0: /* andre@0: * For platforms with no support for IPv6 andre@0: * create layered socket for IPv4-mapped IPv6 addresses andre@0: */ andre@0: if (fd->methods->file_type == PR_DESC_SOCKET_TCP) andre@0: ipv6_fd = PR_CreateIOLayerStub(_pr_ipv6_to_ipv4_id, andre@0: &ipv6_to_v4_tcpMethods); andre@0: else andre@0: ipv6_fd = PR_CreateIOLayerStub(_pr_ipv6_to_ipv4_id, andre@0: &ipv6_to_v4_udpMethods); andre@0: if (NULL == ipv6_fd) { andre@0: goto errorExit; andre@0: } andre@0: ipv6_fd->secret = NULL; andre@0: andre@0: if (PR_PushIOLayer(fd, PR_TOP_IO_LAYER, ipv6_fd) == PR_FAILURE) { andre@0: goto errorExit; andre@0: } andre@0: andre@0: return PR_SUCCESS; andre@0: errorExit: andre@0: andre@0: if (ipv6_fd) andre@0: ipv6_fd->dtor(ipv6_fd); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: #endif /* !defined(_PR_INET6) || defined(_PR_INET6_PROBE) */