#if defined(_REENTRANT) && !defined(__GLIBC__)
#include <boost/thread/mutex.hpp>
#endif
-#include <senf/Socket/Protocols/AddressExceptions.hh>
//#include "INet4Address.mpp"
#define prefix_
if (i == std::string::npos)
throw AddressSyntaxException(s);
try {
- prefix_len_ = boost::lexical_cast<unsigned>(std::string(s,i+1));
+ prefix_len_ = prefix_len_checked(boost::lexical_cast<unsigned>(std::string(s,i+1)));
} catch (boost::bad_lexical_cast const &) {
throw AddressSyntaxException(s);
}
\brief INet4Address inline non-template implementation */
// Custom includes
+#include <senf/Socket/Protocols/AddressExceptions.hh>
#define prefix_ inline
//-/////////////////////////////////////////////////////////////////////////////////////////////////
//-/////////////////////////////////////////////////////////////////////////////////////////////////
// senf::INet4Network
-prefix_ unsigned senf::INet4Network::prefix_len()
- const
-{
- return prefix_len_;
-}
-
-//-/////////////////////////////////////////////////////////////////////////////////////////////////
-// private members
-
-prefix_ boost::uint32_t senf::INet4Network::mask()
- const
-{
- // This is correct as long as the system is using 2-complement arithmetic ...
- return (~((boost::uint32_t(1u)<<(32u-prefix_len()))-1u)) & 0xFFFFFFFFu;
-}
-
//-/////////////////////////////////////////////////////////////////////////////////////////////////
// public members
{}
prefix_ senf::INet4Network::INet4Network(INet4Address const & address, unsigned prefix_len)
- : prefix_len_(prefix_len), address_(INet4Address(address.address() & mask()))
+ : prefix_len_( prefix_len_checked(prefix_len)),
+ address_(INet4Address(address.address() & mask()))
{}
prefix_ senf::INet4Address const & senf::INet4Network::address()
return address_;
}
+prefix_ unsigned senf::INet4Network::prefix_len()
+ const
+{
+ return prefix_len_;
+}
+
prefix_ bool senf::INet4Network::boolean_test()
const
{
}
//-/////////////////////////////////////////////////////////////////////////////////////////////////
+// private members
+
+prefix_ unsigned senf::INet4Network::prefix_len_checked(unsigned prefix_len)
+ const
+{
+ if (prefix_len > 32)
+ throw AddressSyntaxException("invalid INet4 prefix length: " +
+ boost::lexical_cast<std::string>(prefix_len));
+ return prefix_len;
+}
+
+prefix_ boost::uint32_t senf::INet4Network::mask()
+ const
+{
+ // This is correct as long as the system is using 2-complement arithmetic ...
+ return (~((boost::uint32_t(1u)<<(32u-prefix_len()))-1u)) & 0xFFFFFFFFu;
+}
+
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
// namespace members
prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4Network const & addr)
private:
boost::uint32_t mask() const;
+ unsigned prefix_len_checked(unsigned prefix_len) const;
unsigned prefix_len_;
INet4Address address_;
BOOST_CHECK_THROW( senf::INet4Network(""), senf::AddressSyntaxException );
BOOST_CHECK_THROW( senf::INet4Network("192.0.2.0/24/beef"), senf::AddressSyntaxException );
+ BOOST_CHECK_THROW( senf::INet4Network("128.129.130.131/128"), senf::AddressSyntaxException );
{
std::stringstream str;
}
{
std::stringstream str;
- senf::INet4Network net ("128.129.130.131/128");
+ senf::INet4Network net ("128.129.130.131/32");
str << net;
- BOOST_CHECK_EQUAL( str.str(), "128.129.130.131/128");
+ BOOST_CHECK_EQUAL( str.str(), "128.129.130.131/32");
str >> net;
BOOST_CHECK( ! str.fail());
- BOOST_CHECK_EQUAL(net, senf::INet4Network("128.129.130.131/128"));
+ BOOST_CHECK_EQUAL(net, senf::INet4Network("128.129.130.131/32"));
}
}
if (i == std::string::npos)
throw AddressSyntaxException(s);
try {
- prefix_len_ = boost::lexical_cast<unsigned>(std::string(s,i+1));
+ prefix_len_ = prefix_len_checked( boost::lexical_cast<unsigned>(std::string(s,i+1)));
} catch (boost::bad_lexical_cast const &) {
throw AddressSyntaxException(s);
}
{}
prefix_ senf::INet6Network::INet6Network(INet6Address const & address, unsigned prefix_len)
- : prefix_len_(prefix_len), address_(address)
+ : prefix_len_( prefix_len_checked(prefix_len)),
+ address_(address)
{
using boost::lambda::_1;
using boost::lambda::_2;
return INet6Network(addr, prefix_len);
}
+prefix_ unsigned senf::INet6Network::prefix_len_checked(unsigned prefix_len)
+ const
+{
+ if (prefix_len > 128)
+ throw AddressSyntaxException("invalid INet6 prefix length: " +
+ boost::lexical_cast<std::string>(prefix_len));
+ return prefix_len;
+}
+
prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6Network const & addr)
{
os << addr.address() << '/' << addr.prefix_len();
private:
unsigned prefix_len_;
INet6Address address_;
+
+ unsigned prefix_len_checked(unsigned prefix_len) const;
};
/** \brief Output INet6Network instance as it's string representation
BOOST_CHECK_THROW( INet6Network(""), AddressSyntaxException );
BOOST_CHECK_THROW( INet6Network("2001:db8:1234::/beef"), AddressSyntaxException );
+ BOOST_CHECK_THROW( INet6Network("2001:db8:1234::/129"), AddressSyntaxException );
{
std::stringstream str;
//-/////////////////////////////////////////////////////////////////////////////////////////////////
// senf::term::Terminfo
+prefix_ senf::term::Terminfo::InvalidTerminfoException::InvalidTerminfoException(std::string const & term)
+ : senf::Exception("Unreadable terminfo file")
+{
+ if (!term.empty())
+ append( ": " + term);
+}
+
prefix_ senf::term::Terminfo::Terminfo()
{}
prefix_ void senf::term::Terminfo::load(std::string const & term)
{
std::string filename (findTerminfo(term));
- if (filename.empty()) throw InvalidTerminfoException();
+ if (filename.empty()) throw InvalidTerminfoException(term);
std::ifstream is (filename.c_str());
- if (!is) throw InvalidTerminfoException();
- load(is);
+ if (!is) throw InvalidTerminfoException(filename);
+ try {
+ load(is);
+ } catch (InvalidTerminfoException & ex) {
+ ex << ": " << filename;
+ throw ex;
+ }
}
prefix_ bool senf::term::Terminfo::getFlag(properties::Boolean p)
boost::uint16_t nNumbers;
boost::uint16_t nStrings;
boost::uint16_t stringPoolSz;
+
+ void letoh() {
+ magic = le16toh(magic);
+ namesSz = le16toh(namesSz);
+ nBooleans = le16toh(nBooleans);
+ nNumbers = le16toh(nNumbers);
+ nStrings = le16toh(nStrings);
+ stringPoolSz = le16toh(stringPoolSz);
+ }
};
}
{
TerminfoHeader h;
is.read(static_cast<char*>(static_cast<void*>(&h)), sizeof(h));
- if (!is || h.magic != TerminfoMagic) throw InvalidTerminfoException();
+ h.letoh();
+ if (!is || h.magic != TerminfoMagic) throw InvalidTerminfoException(
+ "invalid magic number (") << h.magic << "!=" << TerminfoMagic << ")";
name_.resize(h.namesSz);
is.read(&(name_[0]), name_.size());
number_t v;
is.read(static_cast<char*>(static_cast<void*>(&v)), sizeof(v));
if (!is) throw InvalidTerminfoException();
- *i = v;
+ *i = le16toh(v);
}
stringPool_.resize(h.stringPoolSz);
void dump(std::ostream & os) const; ///< Dump a description of the terminfo entry
/** \brief Invalid, incomplete or non-existent terminfo entry exception */
- struct InvalidTerminfoException : public senf::Exception
- { InvalidTerminfoException() : senf::Exception("Unreadable terminfo file") {} };
+ struct InvalidTerminfoException : public senf::Exception {
+ InvalidTerminfoException(std::string const & term = "");
+ };
private:
typedef std::vector<bool> BoolVec;
typedef std::vector<string_t> StringVec;
typedef std::vector<char> StringPool;
- std::string findTerminfo(std::string const & name);
+ static std::string findTerminfo(std::string const & name);
void load(std::istream & is);
std::string name_;