switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / Protocols / INet / INet4Address.cci
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief INet4Address inline non-template implementation */
30
31 // Custom includes
32
33 #define prefix_ inline
34 //-/////////////////////////////////////////////////////////////////////////////////////////////////
35
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37 // senf::INet4Address
38
39 prefix_ senf::INet4Address::INet4Address()
40 {
41     std::fill(begin(), end(), 0u);
42 }
43
44 prefix_ senf::INet4Address::INet4Address(senf::NoInit_t)
45 {}
46
47 prefix_ senf::INet4Address senf::INet4Address::from_inaddr(inaddr_type v)
48 {
49     return INet4Address(v,IsInAddr);
50 }
51
52 prefix_ senf::INet4Address::inaddr_type & senf::INet4Address::iref()
53 {
54     return *static_cast<inaddr_type *>(static_cast<void *>((&(*this)[0])));
55 }
56
57 prefix_ senf::INet4Address::inaddr_type senf::INet4Address::iref()
58     const
59 {
60     return *static_cast<inaddr_type const *>(static_cast<void const *>(&(*this)[0]));
61 }
62
63 prefix_ senf::INet4Address::inaddr_type senf::INet4Address::inaddr()
64     const
65 {
66     return iref();
67 }
68
69 prefix_ senf::INet4Address::INet4Address(inaddr_type addr, InAddr_t)
70 {
71     iref() = addr;
72 }
73
74 prefix_ bool senf::INet4Address::broadcast()
75     const
76 {
77     return inaddr() == 0xFFFFFFFFu;
78 }
79
80 prefix_ bool senf::INet4Address::boolean_test()
81     const
82 {
83     return inaddr();
84 }
85
86 //-/////////////////////////////////////////////////////////////////////////////////////////////////
87 // senf::INet4Network
88
89 prefix_ unsigned senf::INet4Network::prefix_len()
90     const
91 {
92     return prefix_len_;
93 }
94
95 //-/////////////////////////////////////////////////////////////////////////////////////////////////
96 // private members
97
98 prefix_ boost::uint32_t senf::INet4Network::mask()
99     const
100 {
101     // This is correct as long as the system is using 2-complement arithmetic ...
102     return (~((boost::uint32_t(1u)<<(32u-prefix_len()))-1u)) & 0xFFFFFFFFu;
103 }
104
105 //-/////////////////////////////////////////////////////////////////////////////////////////////////
106 // public members
107
108 prefix_ senf::INet4Network::INet4Network()
109     : prefix_len_(), address_()
110 {}
111
112 prefix_ senf::INet4Network::INet4Network(INet4Address const & address, unsigned prefix_len)
113     : prefix_len_(prefix_len), address_(INet4Address(address.address() & mask()))
114 {}
115
116 prefix_ senf::INet4Address const & senf::INet4Network::address()
117     const
118 {
119     return address_;
120 }
121
122 prefix_ bool senf::INet4Network::boolean_test()
123     const
124 {
125     return address() || prefix_len();
126 }
127
128 prefix_ bool senf::INet4Network::operator==(INet4Network const & other)
129     const
130 {
131     return address() == other.address() && prefix_len() == other.prefix_len();
132 }
133
134 prefix_ bool senf::INet4Network::match(INet4Address const & addr)
135     const
136 {
137     return (addr.address() & mask()) == address_.address();
138 }
139
140 prefix_ bool senf::INet4Network::match(INet4Network const & net)
141     const
142 {
143     return net.prefix_len() >= prefix_len() && match(net.address());
144 }
145
146 prefix_ senf::INet4Address senf::INet4Network::host(boost::uint32_t number)
147 {
148     return INet4Address(address_.address() | (number & ~mask()));
149 }
150
151 prefix_ senf::INet4Network senf::INet4Network::subnet(boost::uint32_t net, unsigned prefix_len)
152 {
153     return INet4Network(host(net << (32-prefix_len)),prefix_len);
154 }
155
156 //-/////////////////////////////////////////////////////////////////////////////////////////////////
157 // namespace members
158
159 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4Network const & addr)
160 {
161     os << addr.address() << '/' << addr.prefix_len();
162     return os;
163 }
164
165 //-/////////////////////////////////////////////////////////////////////////////////////////////////
166 #undef prefix_
167
168 \f
169 // Local Variables:
170 // mode: c++
171 // fill-column: 100
172 // comment-column: 40
173 // c-file-style: "senf"
174 // indent-tabs-mode: nil
175 // ispell-local-dictionary: "american"
176 // compile-command: "scons -u test"
177 // End: