<commit_before>/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "NPL"); you may not use this file except in
 * compliance with the NPL.  You may obtain a copy of the NPL at
 * http://www.mozilla.org/NPL/
 * 
 * Software distributed under the NPL is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
 * for the specific language governing rights and limitations under the
 * NPL.
 * 
 * The Initial Developer of this code under the NPL is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
 * Reserved.
 */

/*
** File:        pripv6.c
** Description: Support for various functions unique to IPv6
*/
#include "primpl.h"
#include <string.h>

#if !defined(_PR_INET6) || defined(_PR_INET6_PROBE)

static PRIOMethods ipv6_to_v4_tcpMethods;
static PRIOMethods ipv6_to_v4_udpMethods;
static PRDescIdentity _pr_ipv6_to_ipv4_id;
extern PRBool IsValidNetAddr(const PRNetAddr *addr);
extern PRIPv6Addr _pr_in6addr_any;
extern PRIPv6Addr _pr_in6addr_loopback;

/*
 * convert an IPv4-mapped IPv6 addr to an IPv4 addr
 */
static void _PR_ConvertToIpv4NetAddr(const PRNetAddr *src_v6addr,
											PRNetAddr *dst_v4addr)
{
const PRUint8 *srcp;

	PR_ASSERT(PR_AF_INET6 == src_v6addr->ipv6.family);

	if (PR_IsNetAddrType(src_v6addr, PR_IpAddrV4Mapped)) {
		srcp = src_v6addr->ipv6.ip.pr_s6_addr;
		memcpy((char *) &dst_v4addr->inet.ip, srcp + 12, 4);
    } else if (PR_IsNetAddrType(src_v6addr, PR_IpAddrAny)) {
        dst_v4addr->inet.ip = htonl(INADDR_ANY);
    } else if (PR_IsNetAddrType(src_v6addr, PR_IpAddrLoopback)) {
        dst_v4addr->inet.ip = htonl(INADDR_LOOPBACK);
    }
	dst_v4addr->inet.family = PR_AF_INET;
	dst_v4addr->inet.port = src_v6addr->ipv6.port;
}

/*
 * convert an IPv4 addr to an IPv4-mapped IPv6 addr
 */
static void _PR_ConvertToIpv6NetAddr(const PRNetAddr *src_v4addr,
                                            PRNetAddr *dst_v6addr)
{
PRUint8 *dstp;

	PR_ASSERT(PR_AF_INET == src_v4addr->inet.family);
	dst_v6addr->ipv6.family = PR_AF_INET6;
	dst_v6addr->ipv6.port = src_v4addr->inet.port;

 	if (htonl(INADDR_ANY) == src_v4addr->inet.ip) {
		dst_v6addr->ipv6.ip = _pr_in6addr_any;
	} else {
		dstp = dst_v6addr->ipv6.ip.pr_s6_addr;
		memset(dstp, 0, 10);
		memset(dstp + 10, 0xff, 2);
		memcpy(dstp + 12,(char *) &src_v4addr->inet.ip, 4);
	}
}

static PRStatus PR_CALLBACK Ipv6ToIpv4SocketBind(PRFileDesc *fd,
								const PRNetAddr *addr)
{
	PRNetAddr tmp_ipv4addr;
	const PRNetAddr *tmp_addrp;
	PRFileDesc *lo = fd->lower;

	if (PR_AF_INET6 != addr->raw.family) {
        PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0);
		return PR_FAILURE;
	}
	if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) ||
    			PR_IsNetAddrType(addr, PR_IpAddrAny)) {
		_PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr);
		tmp_addrp = &tmp_ipv4addr;
	} else {
        PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0);
		return PR_FAILURE;
	}
	return((lo->methods->bind)(lo,tmp_addrp));
}

static PRStatus PR_CALLBACK Ipv6ToIpv4SocketConnect(
    PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout)
{
	PRNetAddr tmp_ipv4addr;
	const PRNetAddr *tmp_addrp;

	if (PR_AF_INET6 != addr->raw.family) {
        PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0);
		return PR_FAILURE;
	}
	if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) ||
			PR_IsNetAddrType(addr, PR_IpAddrLoopback)) {
		_PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr);
		tmp_addrp = &tmp_ipv4addr;
	} else {
        PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0);
		return PR_FAILURE;
	}
	return (fd->lower->methods->connect)(fd->lower, tmp_addrp, timeout);
}

static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketSendTo(
    PRFileDesc *fd, const void *buf, PRInt32 amount,
    PRIntn flags, const PRNetAddr *addr, PRIntervalTime timeout)
{
	PRNetAddr tmp_ipv4addr;
	const PRNetAddr *tmp_addrp;

	if (PR_AF_INET6 != addr->raw.family) {
        PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0);
		return PR_FAILURE;
	}
	if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) ||
			PR_IsNetAddrType(addr, PR_IpAddrLoopback)) {
		_PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr);
		tmp_addrp = &tmp_ipv4addr;
	} else {
        PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0);
		return PR_FAILURE;
	}
    return (fd->lower->methods->sendto)(
        fd->lower, buf, amount, flags, tmp_addrp, timeout);
}

static PRFileDesc* PR_CALLBACK Ipv6ToIpv4SocketAccept (
    PRFileDesc *fd, PRNetAddr *addr, PRIntervalTime timeout)
{
    PRStatus rv;
    PRFileDesc *newfd;
    PRFileDesc *newstack;
	PRNetAddr tmp_ipv4addr;

    PR_ASSERT(fd != NULL);
    PR_ASSERT(fd->lower != NULL);

    newstack = PR_NEW(PRFileDesc);
    if (NULL == newstack)
    {
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
        return NULL;
    }
    *newstack = *fd;  /* make a copy of the accepting layer */

    newfd = (fd->lower->methods->accept)(fd->lower, &tmp_ipv4addr, timeout);
    if (NULL == newfd)
    {
        PR_DELETE(newstack);
        return NULL;
    }
	_PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, addr);

    rv = PR_PushIOLayer(newfd, PR_TOP_IO_LAYER, newstack);
    PR_ASSERT(PR_SUCCESS == rv);
    return newfd;  /* that's it */
}

static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketAcceptRead(PRFileDesc *sd,
			PRFileDesc **nd, PRNetAddr **ipv6_raddr, void *buf, PRInt32 amount,
							PRIntervalTime timeout)
{
    PRInt32 nbytes;
    PRStatus rv;
	PRNetAddr tmp_ipv4addr;
    PRFileDesc *newstack;

    PR_ASSERT(sd != NULL);
    PR_ASSERT(sd->lower != NULL);

    newstack = PR_NEW(PRFileDesc);
    if (NULL == newstack)
    {
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
        return -1;
    }
    *newstack = *sd;  /* make a copy of the accepting layer */
<commit_msg>Bugzilla bug #31049: Ipv6ToIpv4SocketAccept should handle a null 'addr' argument.<commit_after>/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "NPL"); you may not use this file except in
 * compliance with the NPL.  You may obtain a copy of the NPL at
 * http://www.mozilla.org/NPL/
 * 
 * Software distributed under the NPL is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
 * for the specific language governing rights and limitations under the
 * NPL.
 * 
 * The Initial Developer of this code under the NPL is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
 * Reserved.
 */

/*
** File:        pripv6.c
** Description: Support for various functions unique to IPv6
*/
#include "primpl.h"
#include <string.h>

#if !defined(_PR_INET6) || defined(_PR_INET6_PROBE)

static PRIOMethods ipv6_to_v4_tcpMethods;
static PRIOMethods ipv6_to_v4_udpMethods;
static PRDescIdentity _pr_ipv6_to_ipv4_id;
extern PRBool IsValidNetAddr(const PRNetAddr *addr);
extern PRIPv6Addr _pr_in6addr_any;
extern PRIPv6Addr _pr_in6addr_loopback;

/*
 * convert an IPv4-mapped IPv6 addr to an IPv4 addr
 */
static void _PR_ConvertToIpv4NetAddr(const PRNetAddr *src_v6addr,
											PRNetAddr *dst_v4addr)
{
const PRUint8 *srcp;

	PR_ASSERT(PR_AF_INET6 == src_v6addr->ipv6.family);

	if (PR_IsNetAddrType(src_v6addr, PR_IpAddrV4Mapped)) {
		srcp = src_v6addr->ipv6.ip.pr_s6_addr;
		memcpy((char *) &dst_v4addr->inet.ip, srcp + 12, 4);
    } else if (PR_IsNetAddrType(src_v6addr, PR_IpAddrAny)) {
        dst_v4addr->inet.ip = htonl(INADDR_ANY);
    } else if (PR_IsNetAddrType(src_v6addr, PR_IpAddrLoopback)) {
        dst_v4addr->inet.ip = htonl(INADDR_LOOPBACK);
    }
	dst_v4addr->inet.family = PR_AF_INET;
	dst_v4addr->inet.port = src_v6addr->ipv6.port;
}

/*
 * convert an IPv4 addr to an IPv4-mapped IPv6 addr
 */
static void _PR_ConvertToIpv6NetAddr(const PRNetAddr *src_v4addr,
                                            PRNetAddr *dst_v6addr)
{
PRUint8 *dstp;

	PR_ASSERT(PR_AF_INET == src_v4addr->inet.family);
	dst_v6addr->ipv6.family = PR_AF_INET6;
	dst_v6addr->ipv6.port = src_v4addr->inet.port;

 	if (htonl(INADDR_ANY) == src_v4addr->inet.ip) {
		dst_v6addr->ipv6.ip = _pr_in6addr_any;
	} else {
		dstp = dst_v6addr->ipv6.ip.pr_s6_addr;
		memset(dstp, 0, 10);
		memset(dstp + 10, 0xff, 2);
		memcpy(dstp + 12,(char *) &src_v4addr->inet.ip, 4);
	}
}

static PRStatus PR_CALLBACK Ipv6ToIpv4SocketBind(PRFileDesc *fd,
								const PRNetAddr *addr)
{
	PRNetAddr tmp_ipv4addr;
	const PRNetAddr *tmp_addrp;
	PRFileDesc *lo = fd->lower;

	if (PR_AF_INET6 != addr->raw.family) {
        PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0);
		return PR_FAILURE;
	}
	if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) ||
    			PR_IsNetAddrType(addr, PR_IpAddrAny)) {
		_PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr);
		tmp_addrp = &tmp_ipv4addr;
	} else {
        PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0);
		return PR_FAILURE;
	}
	return((lo->methods->bind)(lo,tmp_addrp));
}

static PRStatus PR_CALLBACK Ipv6ToIpv4SocketConnect(
    PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout)
{
	PRNetAddr tmp_ipv4addr;
	const PRNetAddr *tmp_addrp;

	if (PR_AF_INET6 != addr->raw.family) {
        PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0);
		return PR_FAILURE;
	}
	if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) ||
			PR_IsNetAddrType(addr, PR_IpAddrLoopback)) {
		_PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr);
		tmp_addrp = &tmp_ipv4addr;
	} else {
        PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0);
		return PR_FAILURE;
	}
	return (fd->lower->methods->connect)(fd->lower, tmp_addrp, timeout);
}

static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketSendTo(
    PRFileDesc *fd, const void *buf, PRInt32 amount,
    PRIntn flags, const PRNetAddr *addr, PRIntervalTime timeout)
{
	PRNetAddr tmp_ipv4addr;
	const PRNetAddr *tmp_addrp;

	if (PR_AF_INET6 != addr->raw.family) {
        PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0);
		return PR_FAILURE;
	}
	if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) ||
			PR_IsNetAddrType(addr, PR_IpAddrLoopback)) {
		_PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr);
		tmp_addrp = &tmp_ipv4addr;
	} else {
        PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0);
		return PR_FAILURE;
	}
    return (fd->lower->methods->sendto)(
        fd->lower, buf, amount, flags, tmp_addrp, timeout);
}

static PRFileDesc* PR_CALLBACK Ipv6ToIpv4SocketAccept (
    PRFileDesc *fd, PRNetAddr *addr, PRIntervalTime timeout)
{
    PRStatus rv;
    PRFileDesc *newfd;
    PRFileDesc *newstack;
	PRNetAddr tmp_ipv4addr;
    PRNetAddr *addrlower = NULL;

    PR_ASSERT(fd != NULL);
    PR_ASSERT(fd->lower != NULL);

    newstack = PR_NEW(PRFileDesc);
    if (NULL == newstack)
    {
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
        return NULL;
    }
    *newstack = *fd;  /* make a copy of the accepting layer */

    if (addr)
        addrlower = &tmp_ipv4addr;
    newfd = (fd->lower->methods->accept)(fd->lower, addrlower, timeout);
    if (NULL == newfd)
    {
        PR_DELETE(newstack);
        return NULL;
    }
    if (addr)
        _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, addr);

    rv = PR_PushIOLayer(newfd, PR_TOP_IO_LAYER, newstack);
    PR_ASSERT(PR_SUCCESS == rv);
    return newfd;  /* that's it */
}

static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketAcceptRead(PRFileDesc *sd,
			PRFileDesc **nd, PRNetAddr **ipv6_raddr, void *buf, PRInt32 amount,
							PRIntervalTime timeout)
{
    PRInt32 nbytes;
    PRStatus rv;
	PRNetAddr tmp_ipv4addr;
    PRFileDesc *newstack;

    PR_ASSERT(sd != NULL);
    PR_ASSERT(sd->lower != NULL);

    newstack = PR_NEW(PRFileDesc);
    if (NULL == newstack)
    {
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
        return -1;
    }
    *newstack = *sd;  /* make a copy of the accepting layer */

{'fraction_non_alphanumeric': 0.10930799630034474, 'fraction_numerical': 0.02312284537122677, 'mean_word_length': 3.9414208558371415, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3261274', 'n_tokens_mistral': 4763, 'n_tokens_neox': 4602, 'n_words': 966}