Back to index

lightning-sunbird  0.9+nobinonly
pripv6.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** File:        pripv6.c
00040 ** Description: Support for various functions unique to IPv6
00041 */
00042 #include "primpl.h"
00043 #include <string.h>
00044 
00045 #if !defined(_PR_INET6) || defined(_PR_INET6_PROBE)
00046 
00047 static PRIOMethods ipv6_to_v4_tcpMethods;
00048 static PRIOMethods ipv6_to_v4_udpMethods;
00049 static PRDescIdentity _pr_ipv6_to_ipv4_id;
00050 extern PRBool IsValidNetAddr(const PRNetAddr *addr);
00051 extern PRIPv6Addr _pr_in6addr_any;
00052 extern PRIPv6Addr _pr_in6addr_loopback;
00053 
00054 /*
00055  * convert an IPv4-mapped IPv6 addr to an IPv4 addr
00056  */
00057 static void _PR_ConvertToIpv4NetAddr(const PRNetAddr *src_v6addr,
00058                                                                              PRNetAddr *dst_v4addr)
00059 {
00060 const PRUint8 *srcp;
00061 
00062        PR_ASSERT(PR_AF_INET6 == src_v6addr->ipv6.family);
00063 
00064        if (PR_IsNetAddrType(src_v6addr, PR_IpAddrV4Mapped)) {
00065               srcp = src_v6addr->ipv6.ip.pr_s6_addr;
00066               memcpy((char *) &dst_v4addr->inet.ip, srcp + 12, 4);
00067     } else if (PR_IsNetAddrType(src_v6addr, PR_IpAddrAny)) {
00068         dst_v4addr->inet.ip = htonl(INADDR_ANY);
00069     } else if (PR_IsNetAddrType(src_v6addr, PR_IpAddrLoopback)) {
00070         dst_v4addr->inet.ip = htonl(INADDR_LOOPBACK);
00071     }
00072        dst_v4addr->inet.family = PR_AF_INET;
00073        dst_v4addr->inet.port = src_v6addr->ipv6.port;
00074 }
00075 
00076 /*
00077  * convert an IPv4 addr to an IPv4-mapped IPv6 addr
00078  */
00079 static void _PR_ConvertToIpv6NetAddr(const PRNetAddr *src_v4addr,
00080                                             PRNetAddr *dst_v6addr)
00081 {
00082 PRUint8 *dstp;
00083 
00084        PR_ASSERT(PR_AF_INET == src_v4addr->inet.family);
00085        dst_v6addr->ipv6.family = PR_AF_INET6;
00086        dst_v6addr->ipv6.port = src_v4addr->inet.port;
00087 
00088        if (htonl(INADDR_ANY) == src_v4addr->inet.ip) {
00089               dst_v6addr->ipv6.ip = _pr_in6addr_any;
00090        } else {
00091               dstp = dst_v6addr->ipv6.ip.pr_s6_addr;
00092               memset(dstp, 0, 10);
00093               memset(dstp + 10, 0xff, 2);
00094               memcpy(dstp + 12,(char *) &src_v4addr->inet.ip, 4);
00095        }
00096 }
00097 
00098 static PRStatus PR_CALLBACK Ipv6ToIpv4SocketBind(PRFileDesc *fd,
00099                                                         const PRNetAddr *addr)
00100 {
00101        PRNetAddr tmp_ipv4addr;
00102        const PRNetAddr *tmp_addrp;
00103        PRFileDesc *lo = fd->lower;
00104 
00105        if (PR_AF_INET6 != addr->raw.family) {
00106         PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0);
00107               return PR_FAILURE;
00108        }
00109        if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) ||
00110                      PR_IsNetAddrType(addr, PR_IpAddrAny)) {
00111               _PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr);
00112               tmp_addrp = &tmp_ipv4addr;
00113        } else {
00114         PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0);
00115               return PR_FAILURE;
00116        }
00117        return((lo->methods->bind)(lo,tmp_addrp));
00118 }
00119 
00120 static PRStatus PR_CALLBACK Ipv6ToIpv4SocketConnect(
00121     PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout)
00122 {
00123        PRNetAddr tmp_ipv4addr;
00124        const PRNetAddr *tmp_addrp;
00125 
00126        if (PR_AF_INET6 != addr->raw.family) {
00127         PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0);
00128               return PR_FAILURE;
00129        }
00130        if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) ||
00131                      PR_IsNetAddrType(addr, PR_IpAddrLoopback)) {
00132               _PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr);
00133               tmp_addrp = &tmp_ipv4addr;
00134        } else {
00135         PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0);
00136               return PR_FAILURE;
00137        }
00138        return (fd->lower->methods->connect)(fd->lower, tmp_addrp, timeout);
00139 }
00140 
00141 static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketSendTo(
00142     PRFileDesc *fd, const void *buf, PRInt32 amount,
00143     PRIntn flags, const PRNetAddr *addr, PRIntervalTime timeout)
00144 {
00145        PRNetAddr tmp_ipv4addr;
00146        const PRNetAddr *tmp_addrp;
00147 
00148        if (PR_AF_INET6 != addr->raw.family) {
00149         PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0);
00150               return PR_FAILURE;
00151        }
00152        if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) ||
00153                      PR_IsNetAddrType(addr, PR_IpAddrLoopback)) {
00154               _PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr);
00155               tmp_addrp = &tmp_ipv4addr;
00156        } else {
00157         PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0);
00158               return PR_FAILURE;
00159        }
00160     return (fd->lower->methods->sendto)(
00161         fd->lower, buf, amount, flags, tmp_addrp, timeout);
00162 }
00163 
00164 static PRFileDesc* PR_CALLBACK Ipv6ToIpv4SocketAccept (
00165     PRFileDesc *fd, PRNetAddr *addr, PRIntervalTime timeout)
00166 {
00167     PRStatus rv;
00168     PRFileDesc *newfd;
00169     PRFileDesc *newstack;
00170        PRNetAddr tmp_ipv4addr;
00171     PRNetAddr *addrlower = NULL;
00172 
00173     PR_ASSERT(fd != NULL);
00174     PR_ASSERT(fd->lower != NULL);
00175 
00176     newstack = PR_NEW(PRFileDesc);
00177     if (NULL == newstack)
00178     {
00179         PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
00180         return NULL;
00181     }
00182     *newstack = *fd;  /* make a copy of the accepting layer */
00183 
00184     if (addr)
00185         addrlower = &tmp_ipv4addr;
00186     newfd = (fd->lower->methods->accept)(fd->lower, addrlower, timeout);
00187     if (NULL == newfd)
00188     {
00189         PR_DELETE(newstack);
00190         return NULL;
00191     }
00192     if (addr)
00193         _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, addr);
00194 
00195     rv = PR_PushIOLayer(newfd, PR_TOP_IO_LAYER, newstack);
00196     PR_ASSERT(PR_SUCCESS == rv);
00197     return newfd;  /* that's it */
00198 }
00199 
00200 static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketAcceptRead(PRFileDesc *sd,
00201                      PRFileDesc **nd, PRNetAddr **ipv6_raddr, void *buf, PRInt32 amount,
00202                                                  PRIntervalTime timeout)
00203 {
00204     PRInt32 nbytes;
00205     PRStatus rv;
00206        PRNetAddr tmp_ipv4addr;
00207     PRFileDesc *newstack;
00208 
00209     PR_ASSERT(sd != NULL);
00210     PR_ASSERT(sd->lower != NULL);
00211 
00212     newstack = PR_NEW(PRFileDesc);
00213     if (NULL == newstack)
00214     {
00215         PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
00216         return -1;
00217     }
00218     *newstack = *sd;  /* make a copy of the accepting layer */
00219 
00220     nbytes = sd->lower->methods->acceptread(
00221         sd->lower, nd, ipv6_raddr, buf, amount, timeout);
00222     if (-1 == nbytes)
00223     {
00224         PR_DELETE(newstack);
00225         return nbytes;
00226     }
00227        tmp_ipv4addr = **ipv6_raddr;       /* copy */
00228        _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, *ipv6_raddr);
00229 
00230     /* this PR_PushIOLayer call cannot fail */
00231     rv = PR_PushIOLayer(*nd, PR_TOP_IO_LAYER, newstack);
00232     PR_ASSERT(PR_SUCCESS == rv);
00233     return nbytes;
00234 }
00235 
00236 static PRStatus PR_CALLBACK Ipv6ToIpv4SocketGetName(PRFileDesc *fd,
00237                                                                       PRNetAddr *ipv6addr)
00238 {
00239        PRStatus result;
00240        PRNetAddr tmp_ipv4addr;
00241 
00242        result = (fd->lower->methods->getsockname)(fd->lower, &tmp_ipv4addr);
00243        if (PR_SUCCESS == result) {
00244               _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, ipv6addr);
00245               PR_ASSERT(IsValidNetAddr(ipv6addr) == PR_TRUE);
00246        }
00247        return result;
00248 }
00249 
00250 static PRStatus PR_CALLBACK Ipv6ToIpv4SocketGetPeerName(PRFileDesc *fd,
00251                                                                       PRNetAddr *ipv6addr)
00252 {
00253        PRStatus result;
00254        PRNetAddr tmp_ipv4addr;
00255 
00256        result = (fd->lower->methods->getpeername)(fd->lower, &tmp_ipv4addr);
00257        if (PR_SUCCESS == result) {
00258               _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, ipv6addr);
00259               PR_ASSERT(IsValidNetAddr(ipv6addr) == PR_TRUE);
00260        }
00261        return result;
00262 }
00263 
00264 static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketRecvFrom(PRFileDesc *fd, void *buf,
00265                      PRInt32 amount, PRIntn flags, PRNetAddr *ipv6addr,
00266                             PRIntervalTime timeout)
00267 {
00268        PRNetAddr tmp_ipv4addr;
00269        PRInt32 result;
00270 
00271     result = (fd->lower->methods->recvfrom)(
00272         fd->lower, buf, amount, flags, &tmp_ipv4addr, timeout);
00273        if (-1 != result) {
00274               _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, ipv6addr);
00275               PR_ASSERT(IsValidNetAddr(ipv6addr) == PR_TRUE);
00276        }
00277        return result;
00278 }
00279 
00280 #if defined(_PR_INET6_PROBE)
00281 PRBool _pr_ipv6_is_present;
00282 extern PRBool _pr_test_ipv6_socket(void);
00283 
00284 #if !defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYNAME)
00285 extern PRStatus _pr_find_getipnodebyname(void);
00286 #endif
00287 
00288 #if !defined(_PR_INET6) && defined(_PR_HAVE_GETADDRINFO)
00289 extern PRStatus _pr_find_getaddrinfo(void);
00290 #endif
00291 
00292 static PRBool
00293 _pr_probe_ipv6_presence(void)
00294 {
00295 #if !defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYNAME)
00296     if (_pr_find_getipnodebyname() != PR_SUCCESS)
00297         return PR_FALSE;
00298 #endif
00299 
00300 #if !defined(_PR_INET6) && defined(_PR_HAVE_GETADDRINFO)
00301     if (_pr_find_getaddrinfo() != PR_SUCCESS)
00302         return PR_FALSE;
00303 #endif
00304 
00305     return _pr_test_ipv6_socket();
00306 }
00307 #endif  /* _PR_INET6_PROBE */
00308 
00309 PRStatus _pr_init_ipv6()
00310 {
00311     const PRIOMethods *stubMethods;
00312 
00313 #if defined(_PR_INET6_PROBE)
00314     _pr_ipv6_is_present = _pr_probe_ipv6_presence();
00315     if (PR_TRUE == _pr_ipv6_is_present)
00316         return PR_SUCCESS;
00317 #endif
00318 
00319     _pr_ipv6_to_ipv4_id = PR_GetUniqueIdentity("Ipv6_to_Ipv4 layer");
00320     PR_ASSERT(PR_INVALID_IO_LAYER != _pr_ipv6_to_ipv4_id);
00321 
00322        stubMethods = PR_GetDefaultIOMethods();
00323 
00324        ipv6_to_v4_tcpMethods = *stubMethods;  /* first get the entire batch */
00325        /* then override the ones we care about */
00326        ipv6_to_v4_tcpMethods.connect = Ipv6ToIpv4SocketConnect;
00327        ipv6_to_v4_tcpMethods.bind = Ipv6ToIpv4SocketBind;
00328        ipv6_to_v4_tcpMethods.accept = Ipv6ToIpv4SocketAccept;
00329        ipv6_to_v4_tcpMethods.acceptread = Ipv6ToIpv4SocketAcceptRead;
00330        ipv6_to_v4_tcpMethods.getsockname = Ipv6ToIpv4SocketGetName;
00331        ipv6_to_v4_tcpMethods.getpeername = Ipv6ToIpv4SocketGetPeerName;
00332 /*
00333        ipv6_to_v4_tcpMethods.getsocketoption = Ipv6ToIpv4GetSocketOption;
00334        ipv6_to_v4_tcpMethods.setsocketoption = Ipv6ToIpv4SetSocketOption;
00335 */
00336        ipv6_to_v4_udpMethods = *stubMethods;  /* first get the entire batch */
00337        /* then override the ones we care about */
00338        ipv6_to_v4_udpMethods.connect = Ipv6ToIpv4SocketConnect;
00339        ipv6_to_v4_udpMethods.bind = Ipv6ToIpv4SocketBind;
00340        ipv6_to_v4_udpMethods.sendto = Ipv6ToIpv4SocketSendTo;
00341        ipv6_to_v4_udpMethods.recvfrom = Ipv6ToIpv4SocketRecvFrom;
00342        ipv6_to_v4_udpMethods.getsockname = Ipv6ToIpv4SocketGetName;
00343        ipv6_to_v4_udpMethods.getpeername = Ipv6ToIpv4SocketGetPeerName;
00344 /*
00345        ipv6_to_v4_udpMethods.getsocketoption = Ipv6ToIpv4GetSocketOption;
00346        ipv6_to_v4_udpMethods.setsocketoption = Ipv6ToIpv4SetSocketOption;
00347 */
00348        return PR_SUCCESS;
00349 }
00350 
00351 PR_IMPLEMENT(PRStatus) _pr_push_ipv6toipv4_layer(PRFileDesc *fd)
00352 {
00353        PRFileDesc *ipv6_fd = NULL;
00354 
00355        /*
00356         * For platforms with no support for IPv6 
00357         * create layered socket for IPv4-mapped IPv6 addresses
00358         */
00359        if (fd->methods->file_type == PR_DESC_SOCKET_TCP)
00360               ipv6_fd = PR_CreateIOLayerStub(_pr_ipv6_to_ipv4_id,
00361                                                                &ipv6_to_v4_tcpMethods);
00362        else
00363               ipv6_fd = PR_CreateIOLayerStub(_pr_ipv6_to_ipv4_id,
00364                                                                &ipv6_to_v4_udpMethods);
00365        if (NULL == ipv6_fd) {
00366               goto errorExit;
00367        } 
00368        ipv6_fd->secret = NULL;
00369 
00370        if (PR_PushIOLayer(fd, PR_TOP_IO_LAYER, ipv6_fd) == PR_FAILURE) {
00371               goto errorExit;
00372        }
00373 
00374        return PR_SUCCESS;
00375 errorExit:
00376 
00377        if (ipv6_fd)
00378               ipv6_fd->dtor(ipv6_fd);
00379        return PR_FAILURE;
00380 }
00381 
00382 #endif /* !defined(_PR_INET6) || defined(_PR_INET6_PROBE) */