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

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