summaryrefslogtreecommitdiff
path: root/src/libfreeswan/anyaddr.c
blob: 12100f07efef9a1db5a398a6eff072ee0b68b58b (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * special addresses
 * Copyright (C) 2000  Henry Spencer.
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/lgpl.txt>.
 * 
 * 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 Library General Public
 * License for more details.
 *
 * RCSID $Id: anyaddr.c 3265 2007-10-08 19:52:55Z andreas $
 */
#include "internal.h"
#include "freeswan.h"

/* these are mostly fallbacks for the no-IPv6-support-in-library case */
#ifndef IN6ADDR_ANY_INIT
#define	IN6ADDR_ANY_INIT	{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }}
#endif
#ifndef IN6ADDR_LOOPBACK_INIT
#define	IN6ADDR_LOOPBACK_INIT	{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }}
#endif

static struct in6_addr v6any = IN6ADDR_ANY_INIT;
static struct in6_addr v6loop = IN6ADDR_LOOPBACK_INIT;

/*
 - anyaddr - initialize to the any-address value
 */
err_t				/* NULL for success, else string literal */
anyaddr(af, dst)
int af;				/* address family */
ip_address *dst;
{
	uint32_t v4any = htonl(INADDR_ANY);

	switch (af) {
	case AF_INET:
		return initaddr((unsigned char *)&v4any, sizeof(v4any), af, dst);
		break;
	case AF_INET6:
		return initaddr((unsigned char *)&v6any, sizeof(v6any), af, dst);
		break;
	default:
		return "unknown address family in anyaddr/unspecaddr";
		break;
	}
}

/*
 - unspecaddr - initialize to the unspecified-address value
 */
err_t				/* NULL for success, else string literal */
unspecaddr(af, dst)
int af;				/* address family */
ip_address *dst;
{
	return anyaddr(af, dst);
}

/*
 - loopbackaddr - initialize to the loopback-address value
 */
err_t				/* NULL for success, else string literal */
loopbackaddr(af, dst)
int af;				/* address family */
ip_address *dst;
{
	uint32_t v4loop = htonl(INADDR_LOOPBACK);

	switch (af) {
	case AF_INET:
		return initaddr((unsigned char *)&v4loop, sizeof(v4loop), af, dst);
		break;
	case AF_INET6:
		return initaddr((unsigned char *)&v6loop, sizeof(v6loop), af, dst);
		break;
	default:
		return "unknown address family in loopbackaddr";
		break;
	}
}

/*
 - isanyaddr - test for the any-address value
 */
int
isanyaddr(src)
const ip_address *src;
{
	uint32_t v4any = htonl(INADDR_ANY);
	int cmp;

	switch (src->u.v4.sin_family) {
	case AF_INET:
		cmp = memcmp(&src->u.v4.sin_addr.s_addr, &v4any, sizeof(v4any));
		break;
	case AF_INET6:
		cmp = memcmp(&src->u.v6.sin6_addr, &v6any, sizeof(v6any));
		break;
	default:
		return 0;
		break;
	}

	return (cmp == 0) ? 1 : 0;
}

/*
 - isunspecaddr - test for the unspecified-address value
 */
int
isunspecaddr(src)
const ip_address *src;
{
	return isanyaddr(src);
}

/*
 - isloopbackaddr - test for the loopback-address value
 */
int
isloopbackaddr(src)
const ip_address *src;
{
	uint32_t v4loop = htonl(INADDR_LOOPBACK);
	int cmp;

	switch (src->u.v4.sin_family) {
	case AF_INET:
		cmp = memcmp(&src->u.v4.sin_addr.s_addr, &v4loop, sizeof(v4loop));
		break;
	case AF_INET6:
		cmp = memcmp(&src->u.v6.sin6_addr, &v6loop, sizeof(v6loop));
		break;
	default:
		return 0;
		break;
	}

	return (cmp == 0) ? 1 : 0;
}