From: g0dil Date: Thu, 26 Jul 2007 22:48:39 +0000 (+0000) Subject: Socket/Protocols/INet: Implement INet4Address add rename old INet4Address to INet4Soc... X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=9c67039f2a8d9c445ce21912ed814743f61b104c;p=senf.git Socket/Protocols/INet: Implement INet4Address add rename old INet4Address to INet4SocketAddress git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@357 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Socket/Protocols/INet/INet4Address.cc b/Socket/Protocols/INet/INet4Address.cc new file mode 100644 index 0000000..2833d57 --- /dev/null +++ b/Socket/Protocols/INet/INet4Address.cc @@ -0,0 +1,121 @@ +// Copyright (C) 2007 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief INet4Address non-inline non-template implementation */ + +#include "INet4Address.hh" +//#include "INet4Address.ih" + +// Custom includes +#include +#include +#include +#ifdef _REENTRANT +#include +#endif + +//#include "INet4Address.mpp" +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// senf::INet4Address::INet4Address + +prefix_ senf::INet4Address::INet4Address(address_type value) + : addr_(htonl(value)) +{} + +prefix_ senf::INet4Address senf::INet4Address::from_string(std::string const & s) +{ + ::in_addr ina; + if (::inet_pton(AF_INET,s.c_str(),&ina) > 0) + return senf::INet4Address::from_inaddr(ina.s_addr); +# ifdef _REENTRANT + static boost::mutex mutex; + boost::mutex::scoped_lock lock(mutex); +# endif + ::hostent * ent (::gethostbyname(s.c_str())); + if (!ent) + ///\fixme Need to give better exception here + throw SyntaxException(); + if (ent->h_addrtype != AF_INET) + throw SyntaxException(); + // We are only interested in the first address ... + return senf::INet4Address::from_inaddr( + reinterpret_cast(*(ent->h_addr_list))->s_addr); +} + +prefix_ bool senf::INet4Address::local() + const +{ + address_type l (ntohl(addr_)); + return + (l & 0xFF000000u) == 0x0A000000u || + (l & 0xFFF00000u) == 0xAC100000u || + (l & 0xFFFF0000u) == 0xA9FE0000u || + (l & 0xFFFF0000u) == 0xC0A80000u; +} + +prefix_ bool senf::INet4Address::loopback() + const +{ + return (ntohl(addr_) & 0xFF000000u) == 0x7F000000u; +} + +prefix_ bool senf::INet4Address::multicast() + const +{ + return (ntohl(addr_) & 0xF0000000u) == 0xE0000000u; +} + +senf::INet4Address const senf::INet4Address::None; +senf::INet4Address const senf::INet4Address::Loopback = senf::INet4Address(0x7F000001u); +senf::INet4Address const senf::INet4Address::Broadcast = senf::INet4Address(0xFFFFFFFFu); + + +/////////////////////////////////////////////////////////////////////////// +// namespace members + +prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4Address const & addr) +{ + ::in_addr ina; + char buffer[16]; + ina.s_addr = addr.raw(); + ::inet_ntop(AF_INET,&ina,buffer,16); + buffer[15] = 0; + os << buffer; + return os; +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ +//#include "INet4Address.mpp" + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/Socket/Protocols/INet/INet4Address.cci b/Socket/Protocols/INet/INet4Address.cci new file mode 100644 index 0000000..bf593e0 --- /dev/null +++ b/Socket/Protocols/INet/INet4Address.cci @@ -0,0 +1,73 @@ +// Copyright (C) 2007 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief INet4Address inline non-template implementation */ + +// Custom includes + +#define prefix_ inline +///////////////////////////////cci.p/////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// senf::INet4Address + +prefix_ senf::INet4Address::INet4Address() + : addr_() +{} + +prefix_ senf::INet4Address senf::INet4Address::from_inaddr(unsigned long v) +{ + INet4Address addr; + addr.addr_ = v; + return addr; +} + +prefix_ bool senf::INet4Address::broadcast() + const +{ + return addr_ == 0xFFFFFFFFu; +} + +prefix_ bool senf::INet4Address::boolean_test() + const +{ + return addr_; +} + +prefix_ unsigned long senf::INet4Address::raw() + const +{ + return addr_; +} + +///////////////////////////////cci.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/Socket/Protocols/INet/INet4Address.cti b/Socket/Protocols/INet/INet4Address.cti new file mode 100644 index 0000000..e13fdbb --- /dev/null +++ b/Socket/Protocols/INet/INet4Address.cti @@ -0,0 +1,56 @@ +// Copyright (C) 2007 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief INet4Address inline template implementation */ + +//#include "INet4Address.ih" + +// Custom includes + +#define prefix_ inline +///////////////////////////////cti.p/////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// senf::INet4Address + +template +prefix_ senf::INet4Address senf::INet4Address::from_data(InputIterator i) +{ + address_type v ((address_type(*i)&0xFF) << 24); + v |= (address_type(*++i)&0xFF) << 16; + v |= (address_type(*++i)&0xFF) << 8; + v |= (address_type(*++i)&0xFF); + return senf::INet4Address(v); +} + +///////////////////////////////cti.e/////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/Socket/Protocols/INet/INet4Address.hh b/Socket/Protocols/INet/INet4Address.hh new file mode 100644 index 0000000..303ea4b --- /dev/null +++ b/Socket/Protocols/INet/INet4Address.hh @@ -0,0 +1,94 @@ +// Copyright (C) 2007 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief INet4Address public header */ + +#ifndef HH_INet4Address_ +#define HH_INet4Address_ 1 + +// Custom includes +#include +#include +#include +#include "Utils/SafeBool.hh" + +//#include "INet4Address.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace senf { + + /** \brief + */ + class INet4Address + : public ComparableSafeBool + { + public: + typedef uint32_t address_type; + + static INet4Address const None; + static INet4Address const Loopback; + static INet4Address const Broadcast; + + INet4Address(); + explicit INet4Address(address_type value); + + static INet4Address from_string(std::string const & s); + template + static INet4Address from_data(InputIterator i); + static INet4Address from_inaddr(unsigned long v); + + bool local() const; + bool loopback() const; + bool multicast() const; + bool broadcast() const; + bool boolean_test() const; + + unsigned long raw() const; + + struct SyntaxException : public std::exception + { virtual char const * what() const throw() { return "invalid INet4 address syntax"; } }; + + private: + // Hmm ... address_type or unsigned long? I'd use address_type but the man pages for in_addr + // have sa_addr as unsigned long + unsigned long addr_; + }; + + std::ostream & operator<<(std::ostream & os, INet4Address const & addr); + +} + +///////////////////////////////hh.e//////////////////////////////////////// +#include "INet4Address.cci" +//#include "INet4Address.ct" +#include "INet4Address.cti" +#endif + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/Socket/Protocols/INet/INet4Address.test.cc b/Socket/Protocols/INet/INet4Address.test.cc new file mode 100644 index 0000000..e4d92de --- /dev/null +++ b/Socket/Protocols/INet/INet4Address.test.cc @@ -0,0 +1,79 @@ +// Copyright (C) 2007 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief INet4Address.test unit tests */ + +//#include "INet4Address.test.hh" +//#include "INet4Address.test.ih" + +// Custom includes +#include +#include +#include "INet4Address.hh" + +#include +#include + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +BOOST_AUTO_UNIT_TEST(inet4Address) +{ + senf::INet4Address addr (senf::INet4Address::from_string("127.0.0.1")); + BOOST_CHECK_EQUAL( addr, senf::INet4Address::Loopback ); + + addr = senf::INet4Address::from_string("localhost"); + BOOST_CHECK_EQUAL( addr, senf::INet4Address::Loopback ); + BOOST_CHECK( addr.loopback() ); + + char data[] = { 128, 129, 130, 131 }; + addr = senf::INet4Address::from_data(data); + BOOST_CHECK_EQUAL( addr, senf::INet4Address::from_string("128.129.130.131") ); + BOOST_CHECK_EQUAL( addr.raw(), htonl(0x80818283u) ); + + BOOST_CHECK( ! addr.loopback() ); + BOOST_CHECK( ! addr.local() ); + BOOST_CHECK( senf::INet4Address::from_string("192.168.1.2").local() ); + BOOST_CHECK( ! addr.multicast() ); + BOOST_CHECK( senf::INet4Address::from_string("224.1.2.3").multicast() ); + BOOST_CHECK( ! addr.broadcast() ); + BOOST_CHECK( senf::INet4Address::from_string("255.255.255.255").broadcast() ); + BOOST_CHECK( addr ); + BOOST_CHECK( ! senf::INet4Address() ); + + std::stringstream str; + str << addr; + BOOST_CHECK_EQUAL( str.str(), "128.129.130.131" ); +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: diff --git a/Socket/Protocols/INet/INetAddressing.cc b/Socket/Protocols/INet/INetAddressing.cc index b977770..519a202 100644 --- a/Socket/Protocols/INet/INetAddressing.cc +++ b/Socket/Protocols/INet/INetAddressing.cc @@ -43,7 +43,7 @@ /////////////////////////////////////////////////////////////////////////// // senf::INet4Address -prefix_ senf::INet4Address::INet4Address(std::string host, unsigned port) +prefix_ senf::INet4SocketAddress::INet4SocketAddress(std::string host, unsigned port) { clear(); /** \todo gethostbyname support */ @@ -52,7 +52,7 @@ prefix_ senf::INet4Address::INet4Address(std::string host, unsigned port) addr_.sin_port = htons(port); } -prefix_ std::string senf::INet4Address::str() +prefix_ std::string senf::INet4SocketAddress::str() const { std::stringstream s; @@ -60,13 +60,13 @@ prefix_ std::string senf::INet4Address::str() return s.str(); } -prefix_ void senf::INet4Address::clear() +prefix_ void senf::INet4SocketAddress::clear() { ::memset(&addr_,0,sizeof(addr_)); addr_.sin_family = AF_INET; } -prefix_ void senf::INet4Address::assignString(std::string address) +prefix_ void senf::INet4SocketAddress::assignString(std::string address) { clear(); unsigned i = address.find(':'); diff --git a/Socket/Protocols/INet/INetAddressing.cci b/Socket/Protocols/INet/INetAddressing.cci index 45802bc..8c399f6 100644 --- a/Socket/Protocols/INet/INetAddressing.cci +++ b/Socket/Protocols/INet/INetAddressing.cci @@ -33,59 +33,59 @@ /////////////////////////////////////////////////////////////////////////// // senf::INet4Address -prefix_ senf::INet4Address::INet4Address() +prefix_ senf::INet4SocketAddress::INet4SocketAddress() { clear(); } -prefix_ senf::INet4Address::INet4Address(char const * address) +prefix_ senf::INet4SocketAddress::INet4SocketAddress(char const * address) { assignString(address); } -prefix_ senf::INet4Address::INet4Address(std::string address) +prefix_ senf::INet4SocketAddress::INet4SocketAddress(std::string address) { assignString(address); } -prefix_ bool senf::INet4Address::operator==(INet4Address const & other) +prefix_ bool senf::INet4SocketAddress::operator==(INet4SocketAddress const & other) const { return addr_.sin_port == other.addr_.sin_port && addr_.sin_addr.s_addr == other.addr_.sin_addr.s_addr; } -prefix_ std::string senf::INet4Address::host() +prefix_ std::string senf::INet4SocketAddress::host() const { char buffer[128]; return std::string(::inet_ntop(AF_INET,&addr_.sin_addr,buffer,128)); } -prefix_ unsigned senf::INet4Address::port() +prefix_ unsigned senf::INet4SocketAddress::port() const { return ntohs(addr_.sin_port); } -prefix_ struct sockaddr * senf::INet4Address::sockaddr_p() +prefix_ struct sockaddr * senf::INet4SocketAddress::sockaddr_p() { return reinterpret_cast(&addr_); } -prefix_ struct sockaddr const * senf::INet4Address::sockaddr_p() +prefix_ struct sockaddr const * senf::INet4SocketAddress::sockaddr_p() const { return reinterpret_cast(&addr_); } -prefix_ unsigned senf::INet4Address::sockaddr_len() +prefix_ unsigned senf::INet4SocketAddress::sockaddr_len() const { return sizeof(addr_); } -prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4Address const & addr) +prefix_ std::ostream & senf::operator<<(std::ostream & os, INet4SocketAddress const & addr) { os << addr.str(); return os; diff --git a/Socket/Protocols/INet/INetAddressing.hh b/Socket/Protocols/INet/INetAddressing.hh index db204ac..72b9088 100644 --- a/Socket/Protocols/INet/INetAddressing.hh +++ b/Socket/Protocols/INet/INetAddressing.hh @@ -53,14 +53,14 @@ namespace senf { \todo Implement more complete interface \todo gethostbyname support ? */ - class INet4Address + class INet4SocketAddress { public: - INet4Address(); - INet4Address(char const * address); ///< Set address and port - /**< See INet4Address(std::string) + INet4SocketAddress(); + INet4SocketAddress(char const * address); ///< Set address and port + /**< See INet4SocketAddress(std::string) \throws InvalidINetAddressException */ - INet4Address(std::string address); ///< Set address and port + INet4SocketAddress(std::string address); ///< Set address and port /**< This constructor expects a string of the form 'xxx.xxx.xxx.xxx:pppp'. The constructor will use this value to initialize the host and port members. This @@ -68,13 +68,14 @@ namespace senf { not hostnames \param[in] address Address and port \throws InvalidINetAddressException */ - INet4Address(std::string host, unsigned port); ///< Set address and port explicitly + INet4SocketAddress(std::string host, unsigned port); ///< Set address and port explicitly /**< \param[in] host ip address in dotted-quad notation \param[in] port port number \throws InvalidINetAddressException */ - bool operator==(INet4Address const & other) const; ///< Check INet4Address for equality + bool operator==(INet4SocketAddress const & other) const; + ///< Check INet4SocketAddress for equality std::string str() const; ///< Return "address:port" string std::string host() const; ///< Return address in doted quad notation @@ -101,7 +102,7 @@ namespace senf { \related INet4Address */ - std::ostream & operator<<(std::ostream & os, INet4Address const & addr); + std::ostream & operator<<(std::ostream & os, INet4SocketAddress const & addr); /** \brief IPv6 network address @@ -294,14 +295,14 @@ namespace senf { */ struct INet4AddressingPolicy : public AddressingPolicyBase, - private GenericAddressingPolicy + private GenericAddressingPolicy { - typedef INet4Address Address; + typedef INet4SocketAddress Address; - using GenericAddressingPolicy::peer; - using GenericAddressingPolicy::local; - using GenericAddressingPolicy::connect; - using GenericAddressingPolicy::bind; + using GenericAddressingPolicy::peer; + using GenericAddressingPolicy::local; + using GenericAddressingPolicy::connect; + using GenericAddressingPolicy::bind; }; /** \brief Addressing policy supporting IPv6 addressing diff --git a/Socket/Protocols/INet/INetAddressing.test.cc b/Socket/Protocols/INet/INetAddressing.test.cc index 48398e6..317c077 100644 --- a/Socket/Protocols/INet/INetAddressing.test.cc +++ b/Socket/Protocols/INet/INetAddressing.test.cc @@ -36,34 +36,34 @@ BOOST_AUTO_UNIT_TEST(inet4Address) { - using senf::INet4Address; + using senf::INet4SocketAddress; using senf::InvalidINetAddressException; { - INet4Address addr; + INet4SocketAddress addr; addr = "127.0.0.1:12345"; } { - INet4Address addr1("127.0.0.1:12345"); - INet4Address addr2(std::string("127.0.0.1:12345")); - INet4Address addr3("127.0.0.1",12345); + INet4SocketAddress addr1("127.0.0.1:12345"); + INet4SocketAddress addr2(std::string("127.0.0.1:12345")); + INet4SocketAddress addr3("127.0.0.1",12345); } - BOOST_CHECK_EQUAL( INet4Address("127.0.0.1:12345"), INet4Address("127.0.0.1",12345) ); + BOOST_CHECK_EQUAL( INet4SocketAddress("127.0.0.1:12345"), INet4SocketAddress("127.0.0.1",12345) ); - BOOST_CHECK_THROW( INet4Address("127.0.0.1"), InvalidINetAddressException ); - BOOST_CHECK_THROW( INet4Address("foo@bar:12345"), InvalidINetAddressException ); - BOOST_CHECK_THROW( INet4Address("127.0.0.1:1234a"), InvalidINetAddressException ); - BOOST_CHECK_THROW( INet4Address("foo@bar",12345), InvalidINetAddressException ); + BOOST_CHECK_THROW( INet4SocketAddress("127.0.0.1"), InvalidINetAddressException ); + BOOST_CHECK_THROW( INet4SocketAddress("foo@bar:12345"), InvalidINetAddressException ); + BOOST_CHECK_THROW( INet4SocketAddress("127.0.0.1:1234a"), InvalidINetAddressException ); + BOOST_CHECK_THROW( INet4SocketAddress("foo@bar",12345), InvalidINetAddressException ); - BOOST_CHECK_EQUAL( INet4Address("127.0.0.1:12345").host(), "127.0.0.1" ); - BOOST_CHECK_EQUAL( INet4Address("127.0.0.1:12345").port(), 12345u ); - BOOST_CHECK_EQUAL( INet4Address("127.0.0.1:12345").str(), "127.0.0.1:12345" ); + BOOST_CHECK_EQUAL( INet4SocketAddress("127.0.0.1:12345").host(), "127.0.0.1" ); + BOOST_CHECK_EQUAL( INet4SocketAddress("127.0.0.1:12345").port(), 12345u ); + BOOST_CHECK_EQUAL( INet4SocketAddress("127.0.0.1:12345").str(), "127.0.0.1:12345" ); { - INet4Address addr("127.0.0.1:12345"); + INet4SocketAddress addr("127.0.0.1:12345"); BOOST_CHECK_EQUAL( reinterpret_cast< ::sockaddr_in * >(addr.sockaddr_p())->sin_port, htons(12345) ); BOOST_CHECK_EQUAL( reinterpret_cast< ::sockaddr_in * >(addr.sockaddr_p())->sin_addr.s_addr, diff --git a/Socket/Protocols/INet/INetProtocol.cc b/Socket/Protocols/INet/INetProtocol.cc index fff10c0..f4cbff2 100644 --- a/Socket/Protocols/INet/INetProtocol.cc +++ b/Socket/Protocols/INet/INetProtocol.cc @@ -38,14 +38,14 @@ /////////////////////////////////////////////////////////////////////////// // senf::IPv4Protocol -prefix_ void senf::IPv4Protocol::connect(INet4Address const & address) +prefix_ void senf::IPv4Protocol::connect(INet4SocketAddress const & address) const { if (::connect(body().fd(),address.sockaddr_p(), address.sockaddr_len()) < 0) throw SystemException(errno); } -prefix_ void senf::IPv4Protocol::bind(INet4Address const & address) +prefix_ void senf::IPv4Protocol::bind(INet4SocketAddress const & address) const { if (::bind(body().fd(),address.sockaddr_p(), address.sockaddr_len()) < 0) diff --git a/Socket/Protocols/INet/INetProtocol.hh b/Socket/Protocols/INet/INetProtocol.hh index 4728855..49687c6 100644 --- a/Socket/Protocols/INet/INetProtocol.hh +++ b/Socket/Protocols/INet/INetProtocol.hh @@ -62,11 +62,11 @@ namespace senf { : public virtual SocketProtocol { public: - void connect(INet4Address const & address) const; ///< Connect to remote address + void connect(INet4SocketAddress const & address) const; ///< Connect to remote address /**< \todo make this obsolete by allowing access to the ClientSocketHandle from ConcreateSocketProtocol \param[in] address Address to connect to */ - void bind(INet4Address const & address) const; ///< Set local socket address + void bind(INet4SocketAddress const & address) const; ///< Set local socket address /**< \todo make this obsolete by allowing access to the ClientSocketHandle from ConcreateSocketProtocol \param[in] address Address to set */ diff --git a/Socket/Protocols/INet/TCPSocketHandle.cc b/Socket/Protocols/INet/TCPSocketHandle.cc index 2f7f0ad..07e1def 100644 --- a/Socket/Protocols/INet/TCPSocketHandle.cc +++ b/Socket/Protocols/INet/TCPSocketHandle.cc @@ -51,7 +51,7 @@ prefix_ void senf::TCPv4SocketProtocol::init_client() } prefix_ void -senf::TCPv4SocketProtocol::init_client(INet4Address const & address) +senf::TCPv4SocketProtocol::init_client(INet4SocketAddress const & address) const { init_client(); @@ -67,7 +67,7 @@ prefix_ void senf::TCPv4SocketProtocol::init_server() body().fd(sock); } -prefix_ void senf::TCPv4SocketProtocol::init_server(INet4Address const & address, +prefix_ void senf::TCPv4SocketProtocol::init_server(INet4SocketAddress const & address, unsigned backlog) const { diff --git a/Socket/Protocols/INet/TCPSocketHandle.hh b/Socket/Protocols/INet/TCPSocketHandle.hh index d0bb8bf..f55f55d 100644 --- a/Socket/Protocols/INet/TCPSocketHandle.hh +++ b/Socket/Protocols/INet/TCPSocketHandle.hh @@ -100,7 +100,7 @@ namespace senf { /**< \note This member is implicitly called from the ProtocolClientSocketHandle::ProtocolClientSocketHandle() constructor */ - void init_client(INet4Address const & address) const; + void init_client(INet4SocketAddress const & address) const; ///< Create client socket and connect /**< Creates a new client socket and connects to the given address. @@ -113,7 +113,7 @@ namespace senf { /**< \note This member is implicitly called from the ProtocolServerSocketHandle::ProtocolServerSocketHandle() constructor */ - void init_server(INet4Address const & address, unsigned backlog=1) const; + void init_server(INet4SocketAddress const & address, unsigned backlog=1) const; ///< Create server socket and listen /**< Creates a new server socket, binds to \a address end starts listening for new connections with a backlog of diff --git a/Socket/Protocols/INet/TCPSocketHandle.test.cc b/Socket/Protocols/INet/TCPSocketHandle.test.cc index 0889d6d..279f4a1 100644 --- a/Socket/Protocols/INet/TCPSocketHandle.test.cc +++ b/Socket/Protocols/INet/TCPSocketHandle.test.cc @@ -156,7 +156,7 @@ BOOST_AUTO_UNIT_TEST(tcpv4ClientSocketHandle) { senf::TCPv4ClientSocketHandle sock; - BOOST_CHECK_THROW( sock.connect(senf::INet4Address("127.0.0.1:12345")), senf::SystemException ); + BOOST_CHECK_THROW( sock.connect(senf::INet4SocketAddress("127.0.0.1:12345")), senf::SystemException ); BOOST_CHECK_THROW( sock.protocol().connect("127.0.0.1:12345"), senf::SystemException ); } diff --git a/Socket/Protocols/INet/UDPProtocol.cc b/Socket/Protocols/INet/UDPProtocol.cc index 593eb1a..a107916 100644 --- a/Socket/Protocols/INet/UDPProtocol.cc +++ b/Socket/Protocols/INet/UDPProtocol.cc @@ -73,7 +73,7 @@ prefix_ void senf::UDPProtocol::mcLoop(bool value) throw SystemException(errno); } -prefix_ void senf::UDPProtocol::mcAddMembership(INet4Address const & mcAddr) +prefix_ void senf::UDPProtocol::mcAddMembership(INet4SocketAddress const & mcAddr) const { struct ip_mreqn mreqn; @@ -84,8 +84,8 @@ prefix_ void senf::UDPProtocol::mcAddMembership(INet4Address const & mcAddr) throw SystemException(errno); } -prefix_ void senf::UDPProtocol::mcAddMembership(INet4Address const & mcAddr, - INet4Address const & localAddr) +prefix_ void senf::UDPProtocol::mcAddMembership(INet4SocketAddress const & mcAddr, + INet4SocketAddress const & localAddr) const { struct ip_mreqn mreqn; @@ -96,7 +96,7 @@ prefix_ void senf::UDPProtocol::mcAddMembership(INet4Address const & mcAddr, throw SystemException(errno); } -prefix_ void senf::UDPProtocol::mcDropMembership(INet4Address const & mcAddr) +prefix_ void senf::UDPProtocol::mcDropMembership(INet4SocketAddress const & mcAddr) const { struct ip_mreqn mreqn; @@ -107,8 +107,8 @@ prefix_ void senf::UDPProtocol::mcDropMembership(INet4Address const & mcAddr) throw SystemException(errno); } -prefix_ void senf::UDPProtocol::mcDropMembership(INet4Address const & mcAddr, - INet4Address const & localAddr) +prefix_ void senf::UDPProtocol::mcDropMembership(INet4SocketAddress const & mcAddr, + INet4SocketAddress const & localAddr) const { struct ip_mreqn mreqn; diff --git a/Socket/Protocols/INet/UDPProtocol.hh b/Socket/Protocols/INet/UDPProtocol.hh index 58412c1..a94668e 100644 --- a/Socket/Protocols/INet/UDPProtocol.hh +++ b/Socket/Protocols/INet/UDPProtocol.hh @@ -56,7 +56,7 @@ namespace senf { bool mcLoop() const; ///< Return current multicast loopback state void mcLoop(bool value) const; ///< Set multicast loopback state - void mcAddMembership(INet4Address const & mcAddr) const; + void mcAddMembership(INet4SocketAddress const & mcAddr) const; ///< Join multicast group /**< This member will add \a mcAddr to the list of multicast groups received. The group is joined on the default @@ -64,7 +64,7 @@ namespace senf { \param[in] mcAddr address of group to join \todo fix this as soon as we have a real address class (different from the sockaddress class */ - void mcAddMembership(INet4Address const & mcAddr, INet4Address const & localAddr) const; + void mcAddMembership(INet4SocketAddress const & mcAddr, INet4SocketAddress const & localAddr) const; ///< join multicast group on a specific address/interface /**< This member will add \a mcAddr to the list of multicast groups received. The group is joined on the interface @@ -74,7 +74,7 @@ namespace senf { \todo fix this as soon as we have a real address class (different from the sockaddress class */ - void mcDropMembership(INet4Address const & mcAddr) const; + void mcDropMembership(INet4SocketAddress const & mcAddr) const; ///< Leave multicast group /**< This member will remove \a mcAddr from the list of multicast groups received. The group is left from the @@ -82,7 +82,7 @@ namespace senf { \param[in] mcAddr address of group to leave \todo fix this as soon as we have a real address class (different from the sockaddress class */ - void mcDropMembership(INet4Address const & mcAddr, INet4Address const & localAddr) const; + void mcDropMembership(INet4SocketAddress const & mcAddr, INet4SocketAddress const & localAddr) const; ///< leave multicast group on a specific address/interface /**< This member will remove \a mcAddr from the list of multicast groups received. The group is left from the diff --git a/Socket/Protocols/INet/UDPSocketHandle.cc b/Socket/Protocols/INet/UDPSocketHandle.cc index 525e531..9626033 100644 --- a/Socket/Protocols/INet/UDPSocketHandle.cc +++ b/Socket/Protocols/INet/UDPSocketHandle.cc @@ -51,7 +51,7 @@ prefix_ void senf::UDPv4SocketProtocol::init_client() } prefix_ void -senf::UDPv4SocketProtocol::init_client(INet4Address const & address) +senf::UDPv4SocketProtocol::init_client(INet4SocketAddress const & address) const { init_client(); diff --git a/Socket/Protocols/INet/UDPSocketHandle.hh b/Socket/Protocols/INet/UDPSocketHandle.hh index 1a1e93d..35003e7 100644 --- a/Socket/Protocols/INet/UDPSocketHandle.hh +++ b/Socket/Protocols/INet/UDPSocketHandle.hh @@ -97,7 +97,7 @@ namespace senf { /**< \note This member is implicitly called from the ProtocolClientSocketHandle::ProtocolClientSocketHandle() constructor */ - void init_client(INet4Address const & address) const; + void init_client(INet4SocketAddress const & address) const; ///< Create client socket and connect /**< Creates a new client socket and connects to the given address. diff --git a/Socket/Protocols/Raw/LLAddressing.test.cc b/Socket/Protocols/Raw/LLAddressing.test.cc index 32e4d31..1d9f570 100644 --- a/Socket/Protocols/Raw/LLAddressing.test.cc +++ b/Socket/Protocols/Raw/LLAddressing.test.cc @@ -71,7 +71,7 @@ BOOST_AUTO_UNIT_TEST(llAddress) BOOST_CHECK_EQUAL( a.protocol(), 123u ); BOOST_CHECK_EQUAL( a.interface(), "lo" ); BOOST_CHECK_EQUAL( a.arptype(), 0u ); - BOOST_CHECK_EQUAL( a.pkttype(), 0u ); + BOOST_CHECK_EQUAL( a.pkttype(), senf::LLSocketAddress::Undefined ); BOOST_CHECK( ! a.address() ); } } diff --git a/Socket/Protocols/Raw/MACAddress.cc b/Socket/Protocols/Raw/MACAddress.cc index 4d00c8a..4a568a8 100644 --- a/Socket/Protocols/Raw/MACAddress.cc +++ b/Socket/Protocols/Raw/MACAddress.cc @@ -100,6 +100,9 @@ prefix_ senf::MACAddress senf::MACAddress::from_eui64(boost::uint64_t v) return mac; } +senf::MACAddress const senf::MACAddress::Broadcast = senf::MACAddress(0xFFFFFFFFFFFFull); +senf::MACAddress const senf::MACAddress::None; + /////////////////////////////////////////////////////////////////////////// // namespace members diff --git a/Socket/Protocols/Raw/MACAddress.cci b/Socket/Protocols/Raw/MACAddress.cci index d80d0e6..3f2c402 100644 --- a/Socket/Protocols/Raw/MACAddress.cci +++ b/Socket/Protocols/Raw/MACAddress.cci @@ -39,13 +39,23 @@ prefix_ senf::MACAddress::MACAddress() prefix_ senf::MACAddress::MACAddress(NoInit_t) {} +prefix_ senf::MACAddress::MACAddress(boost::uint64_t v) +{ + (*this)[0] = boost::uint8_t( v>>40 ); + (*this)[1] = boost::uint8_t( v>>32 ); + (*this)[2] = boost::uint8_t( v>>24 ); + (*this)[3] = boost::uint8_t( v>>16 ); + (*this)[4] = boost::uint8_t( v>> 8 ); + (*this)[5] = boost::uint8_t( v ); +} + prefix_ bool senf::MACAddress::local() const { return (*this)[0] & 0x02; } -prefix_ bool senf::MACAddress::group() +prefix_ bool senf::MACAddress::multicast() const { return (*this)[0] & 0x01; diff --git a/Socket/Protocols/Raw/MACAddress.hh b/Socket/Protocols/Raw/MACAddress.hh index 4f91297..367839d 100644 --- a/Socket/Protocols/Raw/MACAddress.hh +++ b/Socket/Protocols/Raw/MACAddress.hh @@ -51,10 +51,14 @@ namespace senf { : public boost::array, public ComparableSafeBool { + static MACAddress const Broadcast; ///< The broadcast address + static MACAddress const None; ///< The empty (0) address + enum NoInit_t { noinit }; MACAddress(); ///< Construct zero-initialized address MACAddress(NoInit_t); ///< Construct uninitialized (!) address + explicit MACAddress(boost::uint64_t v); ///< Construct MACAddress constants static MACAddress from_string(std::string const & s); ///< Construct address from string representation @@ -80,7 +84,7 @@ namespace senf { EUI-64. */ bool local() const; ///< \c true, if address is locally administered - bool group() const; ///< \c true, if address is a group/multicast address + bool multicast() const; ///< \c true, if address is a group/multicast address bool broadcast() const; ///< \c true, if address is the broadcast address bool boolean_test() const; ///< \c true, if address is the zero address diff --git a/Socket/Protocols/Raw/MACAddress.test.cc b/Socket/Protocols/Raw/MACAddress.test.cc index 39963f5..86dfa3e 100644 --- a/Socket/Protocols/Raw/MACAddress.test.cc +++ b/Socket/Protocols/Raw/MACAddress.test.cc @@ -38,12 +38,20 @@ BOOST_AUTO_UNIT_TEST(macAddress) { senf::MACAddress mac (senf::MACAddress::from_string("A1-b2-C3:d4:E5:f6")); + + BOOST_CHECK_EQUAL( mac[0], 0xA1u ); + BOOST_CHECK_EQUAL( mac[1], 0xB2u ); + BOOST_CHECK_EQUAL( mac[2], 0xC3u ); + BOOST_CHECK_EQUAL( mac[3], 0xD4u ); + BOOST_CHECK_EQUAL( mac[4], 0xE5u ); + BOOST_CHECK_EQUAL( mac[5], 0xF6u ); + std::stringstream str; str << mac; BOOST_CHECK_EQUAL( str.str(), "a1:b2:c3:d4:e5:f6" ); BOOST_CHECK( ! mac.local() ); - BOOST_CHECK( mac.group() ); + BOOST_CHECK( mac.multicast() ); BOOST_CHECK( ! mac.broadcast() ); BOOST_CHECK( mac ); BOOST_CHECK_EQUAL( mac.oui(), 0xa1b2c3u ); @@ -54,6 +62,7 @@ BOOST_AUTO_UNIT_TEST(macAddress) BOOST_CHECK( ! mac2 ); mac2 = senf::MACAddress::from_string("ff:ff:ff:ff:ff:ff"); BOOST_CHECK( mac2.broadcast() ); + BOOST_CHECK_EQUAL( mac2, senf::MACAddress::Broadcast ); char data[] = { 0x01,0x02,0x03,0x04,0x05,0x06 }; mac2 = senf::MACAddress::from_data(data); BOOST_CHECK_EQUAL( boost::lexical_cast(mac2), "01:02:03:04:05:06" );