OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / lib / 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  * RCSID $Id: anyaddr.c,v 1.5 2000/09/19 07:09:32 rgb Exp $
16  */
17 #include "internal.h"
18 #include "freeswan.h"
19
20 /* these are mostly fallbacks for the no-IPv6-support-in-library case */
21 #ifndef IN6ADDR_ANY_INIT
22 #define IN6ADDR_ANY_INIT        {{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }}
23 #endif
24 #ifndef IN6ADDR_LOOPBACK_INIT
25 #define IN6ADDR_LOOPBACK_INIT   {{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }}
26 #endif
27
28 static struct in6_addr v6any = IN6ADDR_ANY_INIT;
29 static struct in6_addr v6loop = IN6ADDR_LOOPBACK_INIT;
30
31 /*
32  - anyaddr - initialize to the any-address value
33  */
34 err_t                           /* NULL for success, else string literal */
35 anyaddr(af, dst)
36 int af;                         /* address family */
37 ip_address *dst;
38 {
39         uint32_t v4any = htonl(INADDR_ANY);
40
41         switch (af) {
42         case AF_INET:
43                 return initaddr((unsigned char *)&v4any, sizeof(v4any), af, dst);
44                 break;
45         case AF_INET6:
46                 return initaddr((unsigned char *)&v6any, sizeof(v6any), af, dst);
47                 break;
48         default:
49                 return "unknown address family in anyaddr/unspecaddr";
50                 break;
51         }
52 }
53
54 /*
55  - unspecaddr - initialize to the unspecified-address value
56  */
57 err_t                           /* NULL for success, else string literal */
58 unspecaddr(af, dst)
59 int af;                         /* address family */
60 ip_address *dst;
61 {
62         return anyaddr(af, dst);
63 }
64
65 /*
66  - loopbackaddr - initialize to the loopback-address value
67  */
68 err_t                           /* NULL for success, else string literal */
69 loopbackaddr(af, dst)
70 int af;                         /* address family */
71 ip_address *dst;
72 {
73         uint32_t v4loop = htonl(INADDR_LOOPBACK);
74
75         switch (af) {
76         case AF_INET:
77                 return initaddr((unsigned char *)&v4loop, sizeof(v4loop), af, dst);
78                 break;
79         case AF_INET6:
80                 return initaddr((unsigned char *)&v6loop, sizeof(v6loop), af, dst);
81                 break;
82         default:
83                 return "unknown address family in loopbackaddr";
84                 break;
85         }
86 }
87
88 /*
89  - isanyaddr - test for the any-address value
90  */
91 int
92 isanyaddr(src)
93 const ip_address *src;
94 {
95         uint32_t v4any = htonl(INADDR_ANY);
96         int cmp;
97
98         switch (src->u.v4.sin_family) {
99         case AF_INET:
100                 cmp = memcmp(&src->u.v4.sin_addr.s_addr, &v4any, sizeof(v4any));
101                 break;
102         case AF_INET6:
103                 cmp = memcmp(&src->u.v6.sin6_addr, &v6any, sizeof(v6any));
104                 break;
105         default:
106                 return 0;
107                 break;
108         }
109
110         return (cmp == 0) ? 1 : 0;
111 }
112
113 /*
114  - isunspecaddr - test for the unspecified-address value
115  */
116 int
117 isunspecaddr(src)
118 const ip_address *src;
119 {
120         return isanyaddr(src);
121 }
122
123 /*
124  - isloopbackaddr - test for the loopback-address value
125  */
126 int
127 isloopbackaddr(src)
128 const ip_address *src;
129 {
130         uint32_t v4loop = htonl(INADDR_LOOPBACK);
131         int cmp;
132
133         switch (src->u.v4.sin_family) {
134         case AF_INET:
135                 cmp = memcmp(&src->u.v4.sin_addr.s_addr, &v4loop, sizeof(v4loop));
136                 break;
137         case AF_INET6:
138                 cmp = memcmp(&src->u.v6.sin6_addr, &v6loop, sizeof(v6loop));
139                 break;
140         default:
141                 return 0;
142                 break;
143         }
144
145         return (cmp == 0) ? 1 : 0;
146 }