X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Socket%2FProtocols%2FBSDSocketAddress.hh;h=ec3edfdf090513e43a38ad27739eb55d120b2536;hb=58a75247c7915bf6fa5e8205d06cdc4a9adfc537;hp=61cb2204139bdccc839d61ad7f0ca4140007df72;hpb=8475937b6551f73a1cddd8fa830b7128d4c8ebf7;p=senf.git diff --git a/Socket/Protocols/BSDSocketAddress.hh b/Socket/Protocols/BSDSocketAddress.hh index 61cb220..ec3edfd 100644 --- a/Socket/Protocols/BSDSocketAddress.hh +++ b/Socket/Protocols/BSDSocketAddress.hh @@ -23,10 +23,12 @@ /** \file \brief BSDSocketAddress public header */ -#ifndef HH_BSDSocketAddress_ -#define HH_BSDSocketAddress_ 1 +#ifndef HH_SENF_Socket_Protocols_BSDSocketAddress_ +#define HH_SENF_Socket_Protocols_BSDSocketAddress_ 1 // Custom includes +#include +#include #include "../../Utils/safe_bool.hh" #include #include @@ -50,6 +52,20 @@ namespace senf { It is \e not possible to create or store BSDSocketAddress instances: You must either store an address in one of the specifically typed subclasses or using GenericBSDSocketAddress. + A BSDSocketAddress or GenericBSDSocketAddress can be cast (like a downcast) to (the correct) + type specific cast using sockaddr_cast: + + \code + void foo(senf::BSDSOcketAddress const & addr) + { + if (addr.family() == senf::INet4SocketAddress::addressFamily) { + senf::INet4SocketAddress i4addr ( + senf::sockaddr_cast(addr) ); + ... + } + } + \endcode + All these classes provide a generic \c sockaddr API to interface with legacy \c sockaddr based code (e.g. the BSD socket API). In this base-class, this interface is read-only, the derived classes however provide a read-write interface. @@ -98,8 +114,16 @@ namespace senf { void socklen(socklen_t len); private: - - socklen_t len_; + + // The following incantation is needed to fix the alignment of the sockaddr data members + // which will be added by the derived classes later: The alignment must be forced + // to coincide with the struct sockaddr_storage alignment (which must have the largest + // alignment of all sockaddr types). + union { + socklen_t len_; + boost::type_with_alignment::value> a_; + char _b[boost::alignment_of::value]; + }; }; /** \brief Safe socket address down-cast