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