Blame lib/x509/ip-in-cidr.h

Packit 549fdc
/*
Packit 549fdc
 * Copyright (C) 2014-2016 Free Software Foundation, Inc.
Packit 549fdc
 * Copyright (C) 2016 Red Hat, Inc.
Packit 549fdc
 *
Packit 549fdc
 * Authors: Nikos Mavrogiannopoulos, Daiki Ueno, Martin Ukrop
Packit 549fdc
 *
Packit 549fdc
 * This file is part of GnuTLS.
Packit 549fdc
 *
Packit 549fdc
 * The GnuTLS is free software; you can redistribute it and/or
Packit 549fdc
 * modify it under the terms of the GNU Lesser General Public License
Packit 549fdc
 * as published by the Free Software Foundation; either version 2.1 of
Packit 549fdc
 * the License, or (at your option) any later version.
Packit 549fdc
 *
Packit 549fdc
 * This library is distributed in the hope that it will be useful, but
Packit 549fdc
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 549fdc
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 549fdc
 * Lesser General Public License for more details.
Packit 549fdc
 *
Packit 549fdc
 * You should have received a copy of the GNU Lesser General Public License
Packit 549fdc
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
Packit 549fdc
 *
Packit 549fdc
 */
Packit 549fdc
Packit 549fdc
/*-
Packit 549fdc
 * ip_in_cidr:
Packit 549fdc
 * @ip: IP datum (IPv4 or IPv6)
Packit 549fdc
 * @cidr: CIDR datum (IPv4 or IPv6)
Packit 549fdc
 *
Packit 549fdc
 * Check if @ip lies in the given @cidr range.
Packit 549fdc
 * The @ip version must match the @cidr version (v4/v6),
Packit 549fdc
 * (this is not checked).
Packit 549fdc
 *
Packit 549fdc
 * Returns: 1 if @ip lies withing @cidr, 0 otherwise
Packit 549fdc
 -*/
Packit 549fdc
static unsigned ip_in_cidr(const gnutls_datum_t *ip, const gnutls_datum_t *cidr)
Packit 549fdc
{
Packit 549fdc
	unsigned byte;
Packit 549fdc
#ifndef BUILD_IN_TESTS
Packit 549fdc
	char str_ip[48];
Packit 549fdc
	char str_cidr[97];
Packit 549fdc
Packit 549fdc
	_gnutls_hard_log("matching %.*s with CIDR constraint %.*s\n",
Packit 549fdc
					 (int) sizeof(str_ip),
Packit 549fdc
					 _gnutls_ip_to_string(ip->data, ip->size, str_ip, sizeof(str_ip)),
Packit 549fdc
					 (int) sizeof(str_cidr),
Packit 549fdc
					 _gnutls_cidr_to_string(cidr->data, cidr->size, str_cidr, sizeof(str_cidr)));
Packit 549fdc
#endif
Packit 549fdc
	for (byte = 0; byte < ip->size; byte++)
Packit 549fdc
		if (((ip->data[byte] ^ cidr->data[byte]) & cidr->data[ip->size+byte]) != 0)
Packit 549fdc
			return 0;
Packit 549fdc
Packit 549fdc
	return 1; /* match */
Packit 549fdc
}