xorp

ipvxnet.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxorp/ipvxnet.hh,v 1.22 2008/10/02 21:57:31 bms Exp $
00023 
00024 #ifndef __LIBXORP_IPVXNET_HH__
00025 #define __LIBXORP_IPVXNET_HH__
00026 
00027 #include "xorp.h"
00028 #include "ipnet.hh"
00029 #include "ipvx.hh"
00030 #include "ipv4net.hh"
00031 #include "ipv6net.hh"
00032 
00040 typedef IPNet<IPvX> BaseIPvXNet;
00041 
00042 template<>
00043 inline
00044 IPNet<IPvX>::IPNet(const IPvX& ipvx, uint8_t prefix_len)
00045     throw (InvalidNetmaskLength)
00046     : _prefix_len(prefix_len)
00047 {
00048     if (prefix_len > ipvx.addr_bitlen())
00049     xorp_throw(InvalidNetmaskLength, prefix_len);
00050     _masked_addr = ipvx.mask_by_prefix_len(prefix_len);
00051 }
00052 
00053 template <>
00054 inline void
00055 IPNet<IPvX>::initialize_from_string(const char *cp)
00056     throw (InvalidString, InvalidNetmaskLength)
00057 {
00058     char *slash = strrchr(const_cast<char*>(cp), '/');
00059     if (slash == 0) xorp_throw(InvalidString, "Missing slash");
00060 
00061     if (*(slash + 1) == 0)
00062     xorp_throw(InvalidString, "Missing prefix length");
00063     _prefix_len = atoi(slash + 1);
00064 
00065     string addr = string(cp, slash - cp);
00066 
00067     _masked_addr = IPvX(addr.c_str()).mask_by_prefix_len(_prefix_len);
00068 }
00069 
00075 class IPvXNet : public BaseIPvXNet {
00076 public:
00085     explicit IPvXNet(int family) throw (InvalidFamily)
00086     : BaseIPvXNet(IPvX::ZERO(family), 0) {}
00087 #ifdef XORP_USE_USTL
00088     IPvXNet() : BaseIPvXNet(IPvX::ZERO(AF_INET), 0) {}
00089 #endif
00090 
00095     IPvXNet(const BaseIPvXNet& n) : BaseIPvXNet(n) {}
00096 
00102     IPvXNet(const IPvXNet& n) : BaseIPvXNet(n) {}
00103 
00109     IPvXNet(const IPv4Net& v4net)
00110     : BaseIPvXNet(v4net.masked_addr(), v4net.prefix_len()) {}
00111 
00117     IPvXNet(const IPv6Net& v6net)
00118     : BaseIPvXNet(v6net.masked_addr(), v6net.prefix_len()) {}
00119 
00127     IPvXNet(const char *cp) throw (InvalidString, InvalidNetmaskLength)
00128     : BaseIPvXNet(cp) {}
00129 
00136     IPvXNet(const IPvX& a, uint8_t prefix_len) throw (InvalidNetmaskLength)
00137     : BaseIPvXNet(a, prefix_len) {}
00138 
00139     // The following methods are specific to IPvXNet
00140 
00146     bool is_ipv4() const { return masked_addr().is_ipv4(); }
00147 
00153     bool is_ipv6() const { return masked_addr().is_ipv6(); }
00154 
00160     IPv4Net get_ipv4net() const     throw (InvalidCast) {
00161         return IPv4Net(masked_addr().get_ipv4(), prefix_len());
00162     }
00163 
00169     IPv6Net get_ipv6net() const     throw (InvalidCast) {
00170         return IPv6Net(masked_addr().get_ipv6(), prefix_len());
00171     }
00172 
00179     void get(IPv4Net& to_ipv4net) const throw (InvalidCast) {
00180     to_ipv4net = get_ipv4net();
00181     }
00182 
00189     void get(IPv6Net& to_ipv6net) const throw (InvalidCast) {
00190     to_ipv6net = get_ipv6net();
00191     }
00192 
00198     int af() const { return masked_addr().af(); }
00199 
00212     bool is_unicast() const {
00213     if (is_ipv4()) {
00214         return (get_ipv4net().is_unicast());
00215     } else {
00216         return (get_ipv6net().is_unicast());
00217     }
00218     }
00219 
00231     static IPvXNet ip_multicast_base_prefix(int family) throw (InvalidFamily) {
00232     return IPvXNet(IPvX::MULTICAST_BASE(family),
00233                IPvX::ip_multicast_base_address_mask_len(family));
00234     }
00235 
00249     static IPvXNet ip_class_a_base_prefix(int family) throw (InvalidFamily) {
00250     return IPvXNet(IPvX::CLASS_A_BASE(family),
00251                IPvX::ip_class_a_base_address_mask_len(family));
00252     }
00253 
00267     static IPvXNet ip_class_b_base_prefix(int family) throw (InvalidFamily) {
00268     return IPvXNet(IPvX::CLASS_B_BASE(family),
00269                IPvX::ip_class_b_base_address_mask_len(family));
00270     }
00271 
00285     static IPvXNet ip_class_c_base_prefix(int family) throw (InvalidFamily) {
00286     return IPvXNet(IPvX::CLASS_C_BASE(family),
00287                IPvX::ip_class_c_base_address_mask_len(family));
00288     }
00289 
00303     static IPvXNet ip_experimental_base_prefix(int family)
00304     throw (InvalidFamily) {
00305     return IPvXNet(IPvX::EXPERIMENTAL_BASE(family),
00306                IPvX::ip_experimental_base_address_mask_len(family));
00307     }
00308 
00314     bool is_multicast() const {
00315     return (ip_multicast_base_prefix(masked_addr().af()).contains(*this));
00316     }
00317 
00327     bool is_class_a() const {
00328     if (is_ipv4()) {
00329         return (ip_class_a_base_prefix(masked_addr().af()).contains(*this));
00330     }
00331     return (false);
00332     }
00333 
00343     bool is_class_b() const {
00344     if (is_ipv4()) {
00345         return (ip_class_b_base_prefix(masked_addr().af()).contains(*this));
00346     }
00347     return (false);
00348     }
00349 
00359     bool is_class_c() const {
00360     if (is_ipv4()) {
00361         return (ip_class_c_base_prefix(masked_addr().af()).contains(*this));
00362     }
00363     return (false);
00364     }
00365 
00375     bool is_experimental() const {
00376     if (is_ipv4()) {
00377         return (ip_experimental_base_prefix(masked_addr().af()).contains(*this));
00378     }
00379     return (false);
00380     }
00381 };
00382 
00383 #endif // __LIBXORP_IPVXNET_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations