summaryrefslogtreecommitdiffstats
path: root/src/pgm/inet_lnaof.c
blob: e29e3cf564df9092534cfe56de4313d9cb88859b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
 *
 * Portable implementation of inet_lnaof.
 *
 * Copyright (c) 2006-2012 Miru Limited.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#ifndef _WIN32
#	include <sys/types.h>
#	include <sys/socket.h>
#endif
#include <impl/framework.h>
#include <impl/inet_lnaof.h>


//#define INET_LNAOF_DEBUG

/* Equivalent to the description of inet_lnaof(), extract the host
 * address out of an IP address.
 * 	hostaddr = inet_lnaof(addr)
 *
 * nb: lnaof = local network address of
 *
 * returns TRUE if host address is defined, FALSE if only network address.
 */
bool
pgm_inet_lnaof (
	struct in_addr* restrict	dst,	/* host byte order */
	const struct in_addr* restrict	src,
	const struct in_addr* restrict	netmask
	)
{
	bool has_lna = FALSE;

	pgm_assert (NULL != dst);
	pgm_assert (NULL != src);
	pgm_assert (NULL != netmask);

	dst->s_addr = src->s_addr & netmask->s_addr;
	has_lna = (0 != (src->s_addr & ~netmask->s_addr));

	return has_lna;
}

bool
pgm_inet6_lnaof (
	struct in6_addr* restrict	dst,
	const struct in6_addr* restrict	src,
	const struct in6_addr* restrict	netmask
	)
{
	bool has_lna = FALSE;

	pgm_assert (NULL != dst);
	pgm_assert (NULL != src);
	pgm_assert (NULL != netmask);

	for (unsigned i = 0; i < 16; i++) {
		dst->s6_addr[i] = src->s6_addr[i] & netmask->s6_addr[i];
		has_lna |= (0 != (src->s6_addr[i] & !netmask->s6_addr[i]));
	}

	return has_lna;
}

/* eof */