INet4Address.hh

Go to the documentation of this file.
00001 // $Id: INet4Address.hh 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2007
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #ifndef HH_SENF_Socket_Protocols_INet_INet4Address_
00027 #define HH_SENF_Socket_Protocols_INet_INet4Address_ 1
00028 
00029 // Custom includes
00030 #include <iostream>
00031 #include <string>
00032 #include <boost/cstdint.hpp>
00033 #include <boost/array.hpp>
00034 #include <boost/operators.hpp>
00035 #include <senf/Utils/safe_bool.hh>
00036 #include <senf/Utils/Tags.hh>
00037 
00038 //#include "INet4Address.mpp"
00039 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00040 
00041 namespace senf {
00042 
00088     class INet4Address
00089         : public boost::array<boost::uint8_t,4>,
00090           public comparable_safe_bool<INet4Address>
00091     {
00092     public:
00093         //-////////////////////////////////////////////////////////////////////////
00094         // Types
00095 
00096         typedef uint32_t address_type;  
00097         typedef uint32_t inaddr_type;   
00098 
00099         static INet4Address const None; 
00100         static INet4Address const Loopback; 
00101         static INet4Address const Broadcast; 
00102 
00103         //-////////////////////////////////////////////////////////////////////////
00105         //\{
00106 
00107         INet4Address();                 
00108         explicit INet4Address(senf::NoInit_t); 
00109         explicit INet4Address(address_type value);
00111 
00112         static INet4Address from_string(std::string const & s);
00114 
00126         template <class InputIterator>
00127         static INet4Address from_data(InputIterator i);
00129 
00132         static INet4Address from_inaddr(inaddr_type v);
00134 
00138         //\}
00139         //-////////////////////////////////////////////////////////////////////////
00141         //\{
00142 
00143         bool local() const;             
00144 
00146         bool loopback() const;          
00147 
00149         bool multicast() const;         
00150 
00153         bool broadcast() const;         
00154         bool boolean_test() const;      
00155 
00156         inaddr_type inaddr() const;     
00157 
00159         address_type address() const;   
00160 
00163         //-/////////////////////////////////////////////////////////////////////////////////////////
00164 
00165     private:
00166         enum InAddr_t { IsInAddr };
00167         INet4Address(inaddr_type addr, InAddr_t);
00168         inaddr_type & iref();
00169         inaddr_type iref() const;
00170     };
00171 
00175     std::ostream & operator<<(std::ostream & os, INet4Address const & addr);
00176 
00182     std::istream & operator>>(std::istream & os, INet4Address & addr);
00183 
00207     template <boost::uint32_t address, unsigned prefix_len>
00208     class CheckINet4Network
00209     {
00210     public:
00211         static bool match(INet4Address const & addr);
00212     };
00213 
00218     class INet4Network
00219         : public boost::equality_comparable<INet4Network>,
00220           public comparable_safe_bool<INet4Network>
00221     {
00222     public:
00223         //-////////////////////////////////////////////////////////////////////////
00225         //\{
00226 
00227         INet4Network();                 
00228         INet4Network(INet4Address const & address, unsigned prefix_len);
00230         explicit INet4Network(std::string const & s); 
00231 
00232         //\}
00233         //-////////////////////////////////////////////////////////////////////////
00234 
00235         INet4Address const & address() const; 
00236         unsigned prefix_len() const;    
00237 
00238         bool boolean_test() const;      
00239         bool operator==(INet4Network const & other) const;
00241 
00242         bool match(INet4Address const & addr) const; 
00243         bool match(INet4Network const & net) const; 
00244 
00247         INet4Address host(boost::uint32_t number); 
00248 
00255         INet4Network subnet(boost::uint32_t net, unsigned prefix_len);
00257 
00267     protected:
00268 
00269     private:
00270         boost::uint32_t mask() const;
00271 
00272         unsigned prefix_len_;
00273         INet4Address address_;
00274     };
00275 
00279     std::ostream & operator<<(std::ostream & os, INet4Network const & addr);
00280 
00287     std::istream & operator>>(std::istream & is, INet4Network & addr);
00288 
00289 }
00290 
00291 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00292 #include "INet4Address.cci"
00293 #include "INet4Address.ct"
00294 #include "INet4Address.cti"
00295 #endif
00296 
00297 
00298 // Local Variables:
00299 // mode: c++
00300 // fill-column: 100
00301 // comment-column: 40
00302 // c-file-style: "senf"
00303 // indent-tabs-mode: nil
00304 // ispell-local-dictionary: "american"
00305 // compile-command: "scons -u test"
00306 // End: