std::ostream& stream;
public:
- MCSniffer(senf::INet4Address addr, std::ostream& s)
+ MCSniffer(senf::INet4SocketAddress addr, std::ostream& s)
: stream(s)
{
sock.protocol().bind(addr);
std::ofstream f2 ("233.132.152.2.txt");
MCSniffer sniffer1 (
- senf::INet4Address::INet4Address("233.132.152.1:22344"), f1);
+ senf::INet4SocketAddress("233.132.152.1:22344"), f1);
MCSniffer sniffer2 (
- senf::INet4Address::INet4Address("233.132.152.2:22344"), f2);
+ senf::INet4SocketAddress("233.132.152.2:22344"), f2);
senf::Scheduler::instance().process();
}
#include "Socket/Protocols/INet/TCPSocketHandle.hh"
#include "Socket/Protocols/INet/INetAddressing.hh"
-
int main(int argc, char const * argv[])
{
try {
for (int i=0; i<=1000; i++) {
senf::TCPv4ClientSocketHandle sock;
- sock.connect(senf::INet4Address("127.0.0.1", 4243));
+ sock.connect(senf::INet4SocketAddress("127.0.0.1", 4243));
sock.protocol().linger(true);
std::stringstream s;
return 0;
}
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u"
+// comment-column: 40
+// End:
public:
Server(std::string const & host, unsigned int port)
- : serverSock(senf::INet4Address(host, port)) {}
+ : serverSock(senf::INet4SocketAddress(host, port)) {}
void run()
{
}
return 0;
}
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u"
+// comment-column: 40
+// End:
namespace senf {
- /** \brief Internal: Packet type of DataPacket
-
- \internal
- */
- struct DataPacketType : public PacketTypeBase
- {};
+ /** \brief Generic payload-only packet
+ \par Packet type (typedef):
+ \ref DataPacket
- /** \brief Generic payload-only packet
-
DataPacket is a simple generic packet with just a payload of uninterpreted data. This is the
packet used whenever no more specific packet type can be found for a sub-packet (e.g. as the
payload data of a UDP packet)
\ingroup packet_module
*/
+ struct DataPacketType : public PacketTypeBase
+ {};
+
+ /** \brief Generic payload-only packet typedef */
typedef ConcretePacket<DataPacketType> DataPacket;
}
###########################################################################
-# Load subversion information
-svninfo = dict(
- [ map(lambda y:y.strip(),x.split(":",1))
- for x in os.popen("svn info").read().split("\n")
- if ':' in x ] )
-svninfo['commited'] = not(os.popen("svn status -q").read())
-
# Load utilities and setup libraries
SENFSCons.UseBoost()
SENFSCons.UseSTLPort()
DOXY_XREF_TYPES = [ 'bug', 'fixme', 'todo', 'idea' ],
DOXY_HTML_XSL = '#/doclib/html-munge.xsl',
ENV = { 'TODAY' : str(datetime.date.today()),
- 'REVISION' : svninfo['Revision'] + (not(svninfo['commited']) and " + local changes" or ""),
+ 'REVISION' : os.popen("svnversion").read().strip()
},
)
///////////////////////////////ct.p////////////////////////////////////////
template <class Handle>
-prefix_ senf::WriteHelper<Handle>::WriteHelper(Handle handle, std::string data,
+prefix_ senf::WriteHelper<Handle>::WriteHelper(Handle handle, std::string const & data,
Callback callback)
: handle_(handle), data_(data), callback_(callback),
offset_(data_.begin()), errno_(0)
template <class Handle>
prefix_ typename senf::WriteHelper<Handle>::ptr
-senf::WriteHelper<Handle>::dispatch(Handle handle, std::string data, Callback callback)
+senf::WriteHelper<Handle>::dispatch(Handle handle, std::string const & data, Callback callback)
{
return ptr(new WriteHelper(handle, data, callback));
}
///\name Structors and default members
///@{
- static ptr dispatch(Handle handle, std::string data, Callback callback);
+ static ptr dispatch(Handle handle, std::string const & data, Callback callback);
///< Register new WriteHelper instance
/**< The registered callback will be called after all \a
data has been sent or when some error condition is
protected:
private:
- WriteHelper(Handle handle, std::string data, Callback callback);
+ WriteHelper(Handle handle, std::string const & data, Callback callback);
static void dispatchProcess(ptr helper, Handle handle, senf::Scheduler::EventId event);
void process(Handle handle, senf::Scheduler::EventId event);
TAGFILES = "$(TOPDIR)/Utils/doc/Utils.tag"
GENERATE_TAGFILE = doc/Socket.tag
RECURSIVE = Yes
-
+SHOW_DIRECTORIES = Yes
FHandle(int fd)
: senf::FileHandle(std::auto_ptr<senf::FileBody>(
new senf::FileBody(fd))) {}
- FHandle(std::string name)
+ FHandle(std::string const & name)
: senf::FileHandle(std::auto_ptr<senf::FileBody>(
new senf::FileBody()))
{
// senf::INet4Address::INet4Address
prefix_ senf::INet4Address::INet4Address(address_type value)
- : addr_(htonl(value))
-{}
+{
+ iref() = htonl(value);
+}
prefix_ senf::INet4Address senf::INet4Address::from_string(std::string const & s)
{
- ::in_addr ina;
+ struct in_addr ina;
if (::inet_pton(AF_INET,s.c_str(),&ina) > 0)
return senf::INet4Address::from_inaddr(ina.s_addr);
+ int herr (0);
+
+ // If available, we use the reentrant GNU variant. This has the additional advantage, that we
+ // can explicitly ask for IpV4 addresses
+
+# ifdef __GLIBC__
+
+ struct hostent entbuf;
+ char buffer[4096];
+ struct hostent * ent (0);
+ ::gethostbyname2_r(s.c_str(), AF_INET, &entbuf, buffer, sizeof(buffer), &ent, &herr);
+
+# else // ! __GLIBC__
+
# ifdef _REENTRANT
static boost::mutex mutex;
boost::mutex::scoped_lock lock(mutex);
# endif
- ::hostent * ent (::gethostbyname(s.c_str()));
+ struct hostent * ent (::gethostbyname(s.c_str()));
+ herr = h_errno;
+
+# endif // __GLIBC__
+
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<in_addr*>(*(ent->h_addr_list))->s_addr);
prefix_ bool senf::INet4Address::local()
const
{
- address_type l (ntohl(addr_));
+ address_type l (address());
return
(l & 0xFF000000u) == 0x0A000000u ||
(l & 0xFFF00000u) == 0xAC100000u ||
prefix_ bool senf::INet4Address::loopback()
const
{
- return (ntohl(addr_) & 0xFF000000u) == 0x7F000000u;
+ return (address() & 0xFF000000u) == 0x7F000000u;
}
prefix_ bool senf::INet4Address::multicast()
const
{
- return (ntohl(addr_) & 0xF0000000u) == 0xE0000000u;
+ return (address() & 0xF0000000u) == 0xE0000000u;
+}
+
+prefix_ senf::INet4Address::address_type senf::INet4Address::address()
+ const
+{
+ return ntohl(iref());
}
senf::INet4Address const senf::INet4Address::None;
{
::in_addr ina;
char buffer[16];
- ina.s_addr = addr.raw();
+ ina.s_addr = addr.inaddr();
::inet_ntop(AF_INET,&ina,buffer,16);
buffer[15] = 0;
os << buffer;
// senf::INet4Address
prefix_ senf::INet4Address::INet4Address()
- : addr_()
+{
+ std::fill(begin(), end(), 0u);
+}
+
+prefix_ senf::INet4Address::INet4Address(NoInit_t)
{}
-prefix_ senf::INet4Address senf::INet4Address::from_inaddr(unsigned long v)
+prefix_ senf::INet4Address senf::INet4Address::from_inaddr(inaddr_type v)
{
- INet4Address addr;
- addr.addr_ = v;
- return addr;
+ return INet4Address(v,IsInAddr);
+}
+
+prefix_ senf::INet4Address::INet4Address(inaddr_type addr, InAddr_t)
+{
+ iref() = addr;
}
prefix_ bool senf::INet4Address::broadcast()
const
{
- return addr_ == 0xFFFFFFFFu;
+ return inaddr() == 0xFFFFFFFFu;
}
prefix_ bool senf::INet4Address::boolean_test()
const
{
- return addr_;
+ return inaddr();
+}
+
+prefix_ senf::INet4Address::inaddr_type senf::INet4Address::inaddr()
+ const
+{
+ return iref();
+}
+
+prefix_ senf::INet4Address::inaddr_type & senf::INet4Address::iref()
+{
+ return *reinterpret_cast<inaddr_type *>(&(*this)[0]);
}
-prefix_ unsigned long senf::INet4Address::raw()
+prefix_ senf::INet4Address::inaddr_type senf::INet4Address::iref()
const
{
- return addr_;
+ return *reinterpret_cast<inaddr_type const *>(&(*this)[0]);
}
///////////////////////////////cci.e///////////////////////////////////////
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
/** \file
- \brief INet4Address inline template implementation */
+ \brief INet4Address non-inline template implementation */
//#include "INet4Address.ih"
// Custom includes
-#define prefix_ inline
-///////////////////////////////cti.p///////////////////////////////////////
+#define prefix_
+///////////////////////////////ct.p////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// senf::INet4Address
template <class InputIterator>
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);
+ INet4Address addr (INet4Address::noinit);
+ iterator j (addr.begin());
+ iterator const j_end (addr.end());
+ for (;j!=j_end;++j,++i)
+ *j = *i;
+ return addr;
}
-///////////////////////////////cti.e///////////////////////////////////////
+///////////////////////////////ct.e////////////////////////////////////////
#undef prefix_
\f
#include <iostream>
#include <string>
#include <boost/cstdint.hpp>
+#include <boost/function.hpp>
+#include <boost/array.hpp>
#include "Utils/SafeBool.hh"
//#include "INet4Address.mpp"
namespace senf {
- /** \brief
+ /** \brief IpV4 Internet address
+
+ INet4Address represents a simple IP address. It is modelled as a fixed-size
+ container/sequence of 4 bytes.
+
+ \todo Add additional classes for CIDR addresses and networks and network math.
*/
class INet4Address
- : public ComparableSafeBool<INet4Address>
+ : public boost::array<boost::uint8_t,4>,
+ public ComparableSafeBool<INet4Address>
+
{
public:
- typedef uint32_t address_type;
+ typedef uint32_t address_type; ///< Address representation as number in host byte order
+ typedef uint32_t inaddr_type; ///< Legacy address representation in network byte order
+ typedef boost::function<void (INet4Address const &)> Callback;
+ ///< Callback for asynchronous from_string call
+
+ static INet4Address const None; ///< The empty (0) address
+ static INet4Address const Loopback; ///< The loopback (127.0.0.1) address
+ static INet4Address const Broadcast; ////< The global broadcast (255.255.255.255) address
- static INet4Address const None;
- static INet4Address const Loopback;
- static INet4Address const Broadcast;
+ enum NoInit_t { noinit };
- INet4Address();
+ INet4Address(); ///< Construct an empty address
+ INet4Address(NoInit_t); ///< Construct uninitialized (!) address
explicit INet4Address(address_type value);
+ ///< Construct an address constant
static INet4Address from_string(std::string const & s);
+ ///< Convert string to address
+ /**< This member will try to convert the given string into
+ an IP address. from_string() supports all standard IP
+ literal representations as well es hostnames.
+ \attention This call may block if \a s represents a
+ hostname which must be looked up via some network
+ protocol like DNS or NIS
+ \throws SyntaxException if the address cannot be
+ converted for some reason
+ \param[in] s Address literal or hostname */
+
+ static void from_string(std::string const & s, Callback const & cb);
+ ///< Convert string to address (async/non-blocking)
+ /**< This member works like
+ from_string(std::string const &). However unlike
+ from_string(std::string const &), this call will not
+ block. Instead it will call \a cb passing the
+ INet4Address instance as soon as the address has been
+ resolved (which may be immediate if the address
+ represents an IP literal). \par
+ On error, the address passed to \a cb will be empty.
+ \param[in] s Address literal or hostname
+ \param[in] cb Callback to pass the address to
+ \fixme Implement */
+
template <class InputIterator>
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;
+ ///< Construct address from 4 bytes of raw data
+ /**< from_data will build an address from 4 bytes of raw
+ data as accessed by the iterator. The data must be in
+ network byte order. */
+ static INet4Address from_inaddr(inaddr_type v);
+ ///< Construct address from integer in network byte order
+ /**< This call is used when interfacing with other legacy
+ code to convert a network byte order address in an
+ integer number into an INet4Address. */
+
+ bool local() const; ///< \c true, if address is locally administered
+ /**< This call checks, if the address is within one of the
+ IANA private ranges. */
+ bool loopback() const; ///< \c true, if address is within the loopback network
+ /**< Checks, whether the address is in the IANA loopback
+ network 10.0.0.0/8 */
+ bool multicast() const; ///< \c true, if address is a multicast address
+ /**< Checks, whether the address is in the 224.0.0.0/4
+ network reserved for multicast addresses by the
+ IANA. */
+ bool broadcast() const; ///< \c true, if address is 255.255.255.255
+ bool boolean_test() const; ///< \c true, if address is non-empty (!= 0.0.0.0)
+
+ inaddr_type inaddr() const; ///< Return the raw network byte order address
+ /**< This member is used to interact with legacy code.
+ \return */
+ address_type address() const; ///< Return address represented as integer number
+ /**< This member returns the address as an integer number in
+ host byte order. This representation allows simple
+ network math operations. */
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_;
+ enum InAddr_t { IsInAddr };
+ INet4Address(inaddr_type addr, InAddr_t);
+ inaddr_type & iref();
+ inaddr_type iref() const;
};
std::ostream & operator<<(std::ostream & os, INet4Address const & addr);
///////////////////////////////hh.e////////////////////////////////////////
#include "INet4Address.cci"
-//#include "INet4Address.ct"
-#include "INet4Address.cti"
+#include "INet4Address.ct"
+//#include "INet4Address.cti"
#endif
\f
BOOST_AUTO_UNIT_TEST(inet4Address)
{
- senf::INet4Address addr (senf::INet4Address::from_string("127.0.0.1"));
+ senf::INet4Address addr (senf::INet4Address::from_string("127.0.0.1"));
BOOST_CHECK_EQUAL( addr, senf::INet4Address::Loopback );
addr = senf::INet4Address::from_string("localhost");
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_EQUAL( addr.inaddr(), htonl(0x80818283u) );
+ BOOST_CHECK_EQUAL( addr.address(), 0x80818283u );
BOOST_CHECK( ! addr.loopback() );
BOOST_CHECK( ! addr.local() );
///////////////////////////////////////////////////////////////////////////
// senf::INet4Address
-prefix_ senf::INet4SocketAddress::INet4SocketAddress(std::string host, unsigned port)
+prefix_ senf::INet4SocketAddress::INet4SocketAddress(std::string const & host, unsigned port)
{
clear();
/** \todo gethostbyname support */
addr_.sin_family = AF_INET;
}
-prefix_ void senf::INet4SocketAddress::assignString(std::string address)
+prefix_ void senf::INet4SocketAddress::assignString(std::string const & address)
{
clear();
unsigned i = address.find(':');
assignString(address);
}
-prefix_ senf::INet4SocketAddress::INet4SocketAddress(std::string address)
+prefix_ senf::INet4SocketAddress::INet4SocketAddress(std::string const & address)
{
assignString(address);
}
INet4SocketAddress(char const * address); ///< Set address and port
/**< See INet4SocketAddress(std::string)
\throws InvalidINetAddressException */
- INet4SocketAddress(std::string address); ///< Set address and port
+ INet4SocketAddress(std::string const & 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
not hostnames
\param[in] address Address and port
\throws InvalidINetAddressException */
- INet4SocketAddress(std::string host, unsigned port); ///< Set address and port explicitly
+ INet4SocketAddress(std::string const & host, unsigned port);
+ ///< Set address and port explicitly
/**< \param[in] host ip address in dotted-quad notation
\param[in] port port number
\throws InvalidINetAddressException */
/// @}
private:
- void assignString(std::string addr);
+ void assignString(std::string const & addr);
struct ::sockaddr_in addr_;
};
throw SystemException(errno);
}
-prefix_ void senf::UDPProtocol::mcIface(std::string iface)
+prefix_ void senf::UDPProtocol::mcIface(std::string const & iface)
const
{
struct ip_mreqn mreqn;
\todo fix this as soon as we have a real address class
(different from the sockaddress class */
- void mcIface(std::string iface = std::string()) const;
+ void mcIface(std::string const & iface = std::string()) const;
///< set default multicast interface of the socket
/**< \param[in] iface name of interface */
return std::string(name);
}
-prefix_ void senf::LLSocketAddress::interface(std::string iface)
+prefix_ void senf::LLSocketAddress::interface(std::string const & iface)
{
if (iface.empty())
addr_.sll_ifindex = 0;
// changing those members, which are sensible to be changed.
void address(MACAddress const & addr); ///< Change address
- void interface(std::string iface); ///< Change interface
+ void interface(std::string const & iface); ///< Change interface
void protocol(unsigned prot); ///< Change protocol
///\name Generic SocketAddress interface
{
MACAddress mac (MACAddress::noinit);
iterator j (mac.begin());
- iterator j_end (mac.end());
+ iterator const j_end (mac.end());
for (;j!=j_end;++j,++i)
*j = *i;
return mac;
namespace {
- void do_mc(int fd, std::string interface, senf::MACAddress address, bool add)
+ void do_mc(int fd, std::string const & interface, senf::MACAddress address, bool add)
{
struct packet_mreq mreq;
mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
addtogroup
aListCollection
alloc
+async
aVectorCollection
BaseParser
berlios
bitfield
bund
+Callback
callbacks
+cb
cerr
cfi
+CIDR
ClientSocketHandle
CommunicationPolicy
CommunicationPolicyBase
defaultInit
defgroup
dil
+DNS
dontinclude
ElementParser
endcode
hh
hideinitializer
Hmm
+hostname
+hostnames
href
htm
html
http
+IANA
ih
impl
INet
ListB
ListN
ListPolicy
+loopback
MACAddress
mainpage
mixin
nextPacketKey
nextPacketRange
nextPacketType
+NIS
NoAddressingPolicy
noinit
nothrow