Skip to content
Snippets Groups Projects
Commit e621b08f authored by dw6's avatar dw6
Browse files

basic implementation of Unix Domain Sockets, for now Datagram only. docs mostly missing.

parent 191b45f9
No related branches found
No related tags found
No related merge requests found
......@@ -103,7 +103,7 @@ namespace senf {
address.
\param[in] local address to bind to */
/**< \note This member is implicitly called from the
ProtocolClientSocketHandle::ProtocolClientSocketHandle()
ProtocolClientSocketHandle::ProtocolClientSocketHandle()
constructor */
///@}
......
# -*- python -*-
Import('env')
import SENFSCons, glob
###########################################################################
SENFSCons.AllIncludesHH(env, [ f for f in glob.glob("*.hh")
if f not in ('all_includes.hh','UN.hh') and not f.endswith('.test.hh') ])
sources = SENFSCons.GlobSources()
allob = []
allob.extend(
SENFSCons.Objects( env, sources = sources, LIBS = [ 'Socket', 'Utils' ] ) )
for sc in glob.glob("*/SConscript"):
ob = SConscript(sc)
if ob : allob.extend(ob)
SENFSCons.InstallIncludeFiles(env, [ 'UN.hh', 'all_includes.hh' ])
Return('allob')
// $Id$
//
// Copyright (C) 2007
// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
// Stefan Bund <g0dil@berlios.de>
//
// 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 INet public header */
#ifndef HH_UN_
#define HH_UN_ 1
#include "all_includes.hh"
#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:
// $Id$
//
// Copyright (C) 2007
//
// 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 UNAddress non-inline non-template implementation */
#include "UNAddress.hh"
//#include "UNAddress.ih"
// Custom includes
//#include "UNAddress.mpp"
#define prefix_
///////////////////////////////cc.p////////////////////////////////////////
prefix_ senf::UNAddress::UNAddress()
//:path("")
{
path = boost::filesystem::path("");
}
prefix_ senf::UNAddress::UNAddress(boost::filesystem::path p)
//:path(p)
{
}
prefix_ senf::UNAddress::UNAddress senf::UNAddress::fromString(std::string & s)
{
return senf::UNAddress::UNAddress(boost::filesystem::path(s));
}
prefix_ senf::UNAddress::UNAddress senf::UNAddress::fromPath(boost::filesystem::path & p){
return senf::UNAddress::UNAddress(p);
}
prefix_ std::string senf::UNAddress::pathString()
{
return path.string();
}
prefix_ const std::ostream & senf::operator<<(std::ostream & os, UNAddress const & addr)
{
os << addr.pathString();
return os;
}
///////////////////////////////cc.e////////////////////////////////////////
#undef prefix_
//#include "UNAddress.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:
// $Id$
//
// Copyright (C) 2007
//
// 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 UNAddress public header */
#ifndef HH_UNAddress_
#define HH_UNAddress_ 1
// Custom includes
#include <string>
#include <boost/operators.hpp>
#include <boost/filesystem/path.hpp>
#include "../../../Utils/SafeBool.hh"
//#include "UNAddress.mpp"
///////////////////////////////hh.p////////////////////////////////////////
namespace senf {
class UNAddress
: public boost::filesystem::path,
public ComparableSafeBool<UNAddress>
{
public:
UNAddress();
explicit UNAddress(boost::filesystem::path);
static UNAddress fromString(std::string & s);
static UNAddress fromPath(boost::filesystem::path & p);
static std::string pathString();
struct AddressException : public std::exception {};
private:
static boost::filesystem::path path;
};
const std::ostream & operator<<(std::ostream & os, UNAddress const & addr);
}
///////////////////////////////hh.e////////////////////////////////////////
//#include "UNAddress.cci"
//#include "UNAddress.ct"
//#include "UNAddress.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:
// $Id$
//
// Copyright (C) 2007
//
// 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 UNAddressing non-inline non-template implementation */
#include "UNAddressing.hh"
//#include "UNAddressing.ih"
// Custom includes
#include <stdio.h>
#include <boost/operators.hpp>
#include <boost/filesystem/path.hpp>
#include "../../../Utils/SafeBool.hh"
//#include "UNAddressing.mpp"
#define prefix_
///////////////////////////////cc.p////////////////////////////////////////
prefix_ senf::UNSocketAddress::UNSocketAddress(boost::filesystem::path p)
{
chdir(p.branch_path().string().c_str());
//Check if the unix domain socket already exists...
if(!remove(p.leaf().c_str()))
printf("File already existed and therefore was deleted!\n");
else
printf("File not found, will be created.\n");
fflush(stdout);
sockAddr.sun_family = AF_UNIX;
strcpy(sockAddr.sun_path, p.string().c_str());
}
prefix_ senf::UNSocketAddress fromString(std::string s)
{
return senf::UNSocketAddress::UNSocketAddress(boost::filesystem::path(s));
}
prefix_ senf::UNSocketAddress fromPath(boost::filesystem::path p)
{
return senf::UNSocketAddress::UNSocketAddress(p);
}
prefix_ std::string senf::UNSocketAddress::path()
{
return std::string(sockAddr.sun_path);
}
prefix_ sockaddr_un senf::UNSocketAddress::sockaddr()
{
struct sockaddr_un out;
out.sun_family = sockAddr.sun_family;
strcpy(out.sun_path, sockAddr.sun_path);
return out;
}
prefix_ sockaddr * senf::UNSocketAddress::sockaddr_p()
{
return reinterpret_cast <struct sockaddr *> (&sockAddr);
}
prefix_ sockaddr const * senf::UNSocketAddress::sockaddr_p()
const
{
return reinterpret_cast <struct sockaddr const *> (&sockAddr);
}
prefix_ unsigned senf::UNSocketAddress::sockaddr_len()
const
{
return sizeof(sockAddr);
}
prefix_ std::ostream & operator<<(std::ostream & os, senf::UNSocketAddress::UNSocketAddress const & addr){
os << addr.path();
return os;
}
///////////////////////////////cc.e////////////////////////////////////////
#undef prefix_
//#include "UNAddressing.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:
// $Id$
//
// Copyright (C) 2007
//
// 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 UNAddressing public header */
#ifndef HH_UNAddressing_
#define HH_UNAddressing_ 1
// Custom includes
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <sys/un.h>
#include <boost/cstdint.hpp>
#include <boost/operators.hpp>
#include "boost/filesystem/path.hpp"
#include "../../../Socket/SocketPolicy.hh"
#include "../../../Socket/ClientSocketHandle.hh"
#include "../../../Socket/CommunicationPolicy.hh"
#include "../../../Socket/Protocols/GenericAddressingPolicy.hh"
#include "UNAddress.hh"
//#include "UNAddressing.mpp"
///////////////////////////////hh.p////////////////////////////////////////
namespace senf {
class UNSocketAddress
: public ComparableSafeBool<UNSocketAddress>
{
public:
//UNSocketAddress();
explicit UNSocketAddress(boost::filesystem::path p);
///< Construct an address constant
static UNSocketAddress from_string(std::string const s);
static UNSocketAddress from_path(boost::filesystem::path const p);
static std::string path();
static sockaddr_un sockaddr();
struct sockaddr * sockaddr_p();
struct sockaddr const * sockaddr_p() const;
unsigned sockaddr_len() const;
private:
static struct sockaddr_un sockAddr;
};
std::ostream & operator<<(std::ostream & os, UNSocketAddress const & addr);
struct UNAddressingPolicy
: public AddressingPolicyBase,
private GenericAddressingPolicy<UNSocketAddress>
{
typedef UNSocketAddress Address;
using GenericAddressingPolicy<UNSocketAddress>::peer;
using GenericAddressingPolicy<UNSocketAddress>::local;
using GenericAddressingPolicy<UNSocketAddress>::connect;
using GenericAddressingPolicy<UNSocketAddress>::bind;
};
}
///////////////////////////////hh.e////////////////////////////////////////
//#include "UNAddressing.cci"
//#include "UNAddressing.ct"
//#include "UNAddressing.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:
// $Id$
//
// Copyright (C) 2007
//
// 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 UNDatagramSocketHandle non-inline non-template implementation */
#include "UNDatagramSocketHandle.hh"
//#include "UNDatagramSocketHandle.ih"
// Custom includes
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include "../../../Utils/Exception.hh"
//#include "UNDatagramSocketHandle.mpp"
#define prefix_
///////////////////////////////cc.p////////////////////////////////////////
prefix_ void senf::UNDatagramSocketProtocol::init_client() const
{
int sock = ::socket(PF_UNIX,SOCK_DGRAM,0);
if (sock < 0)
throw SystemException(errno);
body().fd(sock);
}
prefix_ void senf::UNDatagramSocketProtocol::init_client(UNSocketAddress const & address) const
{
init_client();
//bind(address);
}
///////////////////////////////cc.e////////////////////////////////////////
#undef prefix_
//#include "UNDatagramSocketHandle.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:
// $Id$
//
// Copyright (C) 2007
//
// 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 UNDatagramSocketHandle public header */
#ifndef HH_UNDatagramSocketHandle_
#define HH_UNDatagramSocketHandle_ 1
// Custom includes
#include "UNAddressing.hh"
#include "UNProtocol.hh"
#include "../../../Socket/Protocols/BSDSocketProtocol.hh"
#include "../../../Socket/FramingPolicy.hh"
#include "../../../Socket/CommunicationPolicy.hh"
#include "../../../Socket/ReadWritePolicy.hh"
#include "../../../Socket/BufferingPolicy.hh"
#include "../../../Socket/ProtocolClientSocketHandle.hh"
//#include "UNDatagramSocketHandle.mpp"
///////////////////////////////hh.p////////////////////////////////////////
namespace senf {
/// \addtogroup concrete_protocol_group
/// @{
typedef MakeSocketPolicy<
UNAddressingPolicy,
DatagramFramingPolicy,
UnconnectedCommunicationPolicy,
ReadablePolicy,
WriteablePolicy,
SocketBufferingPolicy
>::policy UNDatagramSocket_Policy;
class UNDatagramSocketProtocol
: public ConcreteSocketProtocol<UNDatagramSocket_Policy>,
public UNProtocol,
public BSDSocketProtocol,
public AddressableBSDSocketProtocol
{
public:
///////////////////////////////////////////////////////////////////////////
// internal interface
///\name Constructors
///@{
void init_client() const; ///< Create unconnected client socket
/**< \note This member is implicitly called from the
ProtocolClientSocketHandle::ProtocolClientSocketHandle()
constructor */
void init_client(UNSocketAddress const & address) const;
///< Create client socket and bind
/**< Creates a new client socket and bind to the given
address.
\param[in] local address to bind to */
/**< \note This member is implicitly called from the
ProtocolClientSocketHandle::ProtocolClientSocketHandle()
constructor */
///@}
///\name Abstract Interface Implementation
std::auto_ptr<SocketProtocol> clone() const;
///@}
};
typedef ProtocolClientSocketHandle<UNDatagramSocketProtocol> UNDatagramClientSocketHandle;
typedef MakeSocketPolicy<
UNDatagramSocket_Policy,
UNAddressingPolicy
>::policy UDPv6Socket_Policy;
}
///////////////////////////////hh.e////////////////////////////////////////
//#include "UNDatagramSocketHandle.cci"
//#include "UNDatagramSocketHandle.ct"
//#include "UNDatagramSocketHandle.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:
// $Id$
//
// Copyright (C) 2007
//
// 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 UNProtocol non-inline non-template implementation */
#include "UNProtocol.hh"
//#include "UNProtocol.ih"
// Custom includes
#include <sys/socket.h>
#include "../../../Utils/Exception.hh"
//#include "UNProtocol.mpp"
#define prefix_
///////////////////////////////cc.p////////////////////////////////////////
prefix_ void senf::UNProtocol::connect(UNSocketAddress const & address)
const
{
if(::connect(body().fd(), address.sockaddr_p(), sizeof(address.sockaddr())) < 0)
throw SystemException(errno);
}
prefix_ void senf::UNProtocol::bind(UNSocketAddress const & address)
const
{
if(::bind(body().fd(), address.sockaddr_p(), sizeof(address.sockaddr())) < 0)
throw SystemException(errno);
}
///////////////////////////////cc.e////////////////////////////////////////
#undef prefix_
//#include "UNProtocol.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:
// $Id$
//
// Copyright (C) 2007
//
// 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 UNProtocol public header */
#ifndef HH_UNProtocol_
#define HH_UNProtocol_ 1
// Custom includes
#include "../../../Socket/SocketProtocol.hh"
#include "UNAddressing.hh"
#include "../../../Socket/ClientSocketHandle.hh"
#include "../../../Socket/CommunicationPolicy.hh"
//#include "UNProtocol.mpp"
///////////////////////////////hh.p////////////////////////////////////////
namespace senf {
/// \addtogroup protocol_facets_group
/// @{
/** \brief Protocol facet providing Unix Domain Addressing related API
This protocol facet introduces all the socket api protocol members which are related to Unix
Domain addressing.
\todo connect() is only available on stream sockets. We want to access bind() and connect()
via the ClientSocketHandle -> see SocketProtocol todo point
*/
class UNProtocol
: public virtual SocketProtocol
{
public:
void connect(UNSocketAddress 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(UNSocketAddress 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 */
};
}
///////////////////////////////hh.e////////////////////////////////////////
//#include "UNProtocol.cci"
//#include "UNProtocol.ct"
//#include "UNProtocol.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:
#include "UNAddress.hh"
#include "UNAddressing.hh"
#include "UNDatagramSocketHandle.hh"
#include "UNProtocol.hh"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment