Replaced the strange definitions of IN6ADDR_ANY_INIT and IN6ADDR_LOOPBACK_INIT on...
[strongswan.git] / src / libfreeswan / anyaddr.c
1 /*
2 * special addresses
3 * Copyright (C) 2000 Henry Spencer.
4 *
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Library General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 * License for more details.
14 */
15 #include <sys/socket.h>
16
17 #include "internal.h"
18 #include "freeswan.h"
19
20 /* OpenSolaris defines strange versions of these macros */
21 #ifdef __sun
22 #undef IN6ADDR_ANY_INIT
23 #define IN6ADDR_ANY_INIT {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }}}
24
25 #undef IN6ADDR_LOOPBACK_INIT
26 #define IN6ADDR_LOOPBACK_INIT {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }}}
27 #endif
28
29 static struct in6_addr v6any = IN6ADDR_ANY_INIT;
30 static struct in6_addr v6loop = IN6ADDR_LOOPBACK_INIT;
31
32 /*
33 - anyaddr - initialize to the any-address value
34 */
35 err_t /* NULL for success, else string literal */
36 anyaddr(af, dst)
37 int af; /* address family */
38 ip_address *dst;
39 {
40 uint32_t v4any = htonl(INADDR_ANY);
41
42 switch (af) {
43 case AF_INET:
44 return initaddr((unsigned char *)&v4any, sizeof(v4any), af, dst);
45 break;
46 case AF_INET6:
47 return initaddr((unsigned char *)&v6any, sizeof(v6any), af, dst);
48 break;
49 default:
50 return "unknown address family in anyaddr/unspecaddr";
51 break;
52 }
53 }
54
55 /*
56 - unspecaddr - initialize to the unspecified-address value
57 */
58 err_t /* NULL for success, else string literal */
59 unspecaddr(af, dst)
60 int af; /* address family */
61 ip_address *dst;
62 {
63 return anyaddr(af, dst);
64 }
65
66 /*
67 - loopbackaddr - initialize to the loopback-address value
68 */
69 err_t /* NULL for success, else string literal */
70 loopbackaddr(af, dst)
71 int af; /* address family */
72 ip_address *dst;
73 {
74 uint32_t v4loop = htonl(INADDR_LOOPBACK);
75
76 switch (af) {
77 case AF_INET:
78 return initaddr((unsigned char *)&v4loop, sizeof(v4loop), af, dst);
79 break;
80 case AF_INET6:
81 return initaddr((unsigned char *)&v6loop, sizeof(v6loop), af, dst);
82 break;
83 default:
84 return "unknown address family in loopbackaddr";
85 break;
86 }
87 }
88
89 /*
90 - isanyaddr - test for the any-address value
91 */
92 int
93 isanyaddr(src)
94 const ip_address *src;
95 {
96 uint32_t v4any = htonl(INADDR_ANY);
97 int cmp;
98
99 switch (src->u.v4.sin_family) {
100 case AF_INET:
101 cmp = memcmp(&src->u.v4.sin_addr.s_addr, &v4any, sizeof(v4any));
102 break;
103 case AF_INET6:
104 cmp = memcmp(&src->u.v6.sin6_addr, &v6any, sizeof(v6any));
105 break;
106 default:
107 return 0;
108 break;
109 }
110
111 return (cmp == 0) ? 1 : 0;
112 }
113
114 /*
115 - isunspecaddr - test for the unspecified-address value
116 */
117 int
118 isunspecaddr(src)
119 const ip_address *src;
120 {
121 return isanyaddr(src);
122 }
123
124 /*
125 - isloopbackaddr - test for the loopback-address value
126 */
127 int
128 isloopbackaddr(src)
129 const ip_address *src;
130 {
131 uint32_t v4loop = htonl(INADDR_LOOPBACK);
132 int cmp;
133
134 switch (src->u.v4.sin_family) {
135 case AF_INET:
136 cmp = memcmp(&src->u.v4.sin_addr.s_addr, &v4loop, sizeof(v4loop));
137 break;
138 case AF_INET6:
139 cmp = memcmp(&src->u.v6.sin6_addr, &v6loop, sizeof(v6loop));
140 break;
141 default:
142 return 0;
143 break;
144 }
145
146 return (cmp == 0) ? 1 : 0;
147 }