*/
/** @file
*
- * (c) Copyright 2008 by Intra2net AG
+ * @copyright Copyright 2008-2009 by Intra2net AG
+ * @contact Intra2net Opensource Team \<opensource@intra2net.com\>
*
* @todo unlink unix server socket on close.
*/
* @brief handled incoming connections on the server port.
*
* accepts the new connection, stores the peer address in an internal buffer
- * and calls a (derived) acceptNewConnection method to create an apropriate
+ * and calls a (derived) acceptNewConnection method to create an appropriate
* IO class instance.
* If no io instance is created the connection is closed.
*/
/**
* @brief sets a function which is called when a new connection was established.
*
- * The function gets a (shared) pointer to the new connetion as parameter and is
+ * The function gets a (shared) pointer to the new connection as parameter and is
* expected to store it when it accepts the connection.
- * (Else the conenction gets deleted after the function was called.)
+ * (Else the connection gets deleted after the function was called.)
*
- * @param func the function which hsould be called on new conenctions.
+ * @param func the function which should be called on new connections.
*/
void ServerSocketBaseImplementation::setNewConnectionBaseCallback(
const NewConnectionBaseCallbackFunc& func)
UnixIOSocket::UnixIOSocket()
-: m_peer_pid(0)
-, m_peer_uid(0)
-, m_peer_gid(0)
+: m_peer_pid((unsigned int)-1)
+, m_peer_uid((unsigned int)-1)
+, m_peer_gid((unsigned int)-1)
{
} // eo UnixIOSocket::UnixIOSocket()
UnixIOSocket::UnixIOSocket(const std::string& path)
-: m_peer_pid(0)
-, m_peer_uid(0)
-, m_peer_gid(0)
+: m_peer_pid((unsigned int)-1)
+, m_peer_uid((unsigned int)-1)
+, m_peer_gid((unsigned int)-1)
{
- open(path);
+ open(path);
} // eo UnixIOSocket::UnixIOSocket(const std::string&)
-UnixIOSocket::UnixIOSocket(
- int fd, const std::string& path,
- unsigned int peer_pid, unsigned int peer_uid, unsigned int peer_gid)
+UnixIOSocket::UnixIOSocket(int fd,const std::string& path)
: IOImplementation(fd,fd)
, m_path(path)
-, m_peer_pid(peer_pid)
-, m_peer_uid(peer_uid)
-, m_peer_gid(peer_gid)
+, m_peer_pid((unsigned int)-1)
+, m_peer_uid((unsigned int)-1)
+, m_peer_gid((unsigned int)-1)
{
-} // eo UnixIOSocket::UnixIOSocket(int,const std::string&,unsigned,unsigned,unsigned)
+ update_peer_information(fd);
+} // eo UnixIOSocket::UnixIOSocket(int,const std::string&)
/**
} // eo UnixIOSocket::open(const std::string&,int)
+/**
+ * @brief update the peer info (pid,gid,uid).
+ * @param fd the socket to update the information for.
+ * @return @a true if succesfully determined the peer info.
+ *
+ * @note if the peer info could not be detected the values are set to
+ * (unsigned int)(-1).
+ */
+bool UnixIOSocket::update_peer_information(int fd)
+{
+ m_peer_pid=(unsigned int)-1;
+ m_peer_gid=(unsigned int)-1;
+ m_peer_uid=(unsigned int)-1;
+
+ //TODO add more versions of getting the peer information here...
+ // BSD systems seems to have SO_PEERCRED,too (does someone know how it is used there?)
+
+ #ifdef __linux__
+ { // the linux way to get peer info (pid,gid,uid):
+ struct ucred cred;
+ socklen_t cred_len = sizeof(cred);
+ if (getsockopt(fd,SOL_SOCKET,SO_PEERCRED,&cred,&cred_len) == 0)
+ {
+ m_peer_pid= cred.pid;
+ m_peer_uid= cred.uid;
+ m_peer_gid= cred.gid;
+ return true;
+ }
+ }
+ #else
+ #warning dont know how to determine peer info.
+ #endif
+ return false;
+} // end of UnixIOSocketupdate_peer_information(int)
+
+
/*
** implementation of UnixServerSocketBase
*/
addr.m_addr_un.sun_family= AF_UNIX;
strncpy(addr.m_addr_un.sun_path, path.c_str(), PATH_MAX);
Utils::unlink(path); // just in case...
+ // NOTE this is a place which might require some updates for multithreaded
+ // usage! (setting the umask affects all threads...)
mode_t old_mask= ::umask( (mode & 0777) ^ 0777);
if (::bind(fd,(sockaddr*)&addr.m_addr_un, SUN_LEN(&addr.m_addr_un)) < 0)
{
* It also tries to determine the peer pid, uid and gid.
*
* @param fd the file descriptor of a freshly accepted connection.
- * @param addr conatins "pointer to struct sockaddr"
- * @return @a a (shared) pointer to the new connection isntance; empty if none was
+ * @param addr contains "pointer to struct sockaddr"
+ * @return @a a (shared) pointer to the new connection instance; empty if none was
* created.
*/
IOImplementationPtr UnixServerSocketBase::acceptNewConnection(int fd, boost::any addr)
}
struct sockaddr_un *un_ptr = reinterpret_cast<struct sockaddr_un*>(addr_ptr);
std::string peer_path( un_ptr->sun_path );
- unsigned peer_pid=0;
- unsigned peer_gid=0;
- unsigned peer_uid=0;
-#ifdef __linux__
- { // the linux way to get peer info (pid,gid,uid):
- struct ucred cred;
- socklen_t cred_len = sizeof(cred);
- if (getsockopt(fd,SOL_SOCKET,SO_PEERCRED,&cred,&cred_len) == 0)
- {
- peer_pid= cred.pid;
- peer_uid= cred.uid;
- peer_gid= cred.gid;
- }
- }
-#else
-#error dont know how to determine peer info.
-#endif
- UnixIOSocketPtr ptr( createIOSocket(fd, peer_path, peer_pid, peer_uid, peer_gid) );
+ UnixIOSocketPtr ptr( createIOSocket(fd, peer_path) );
+
return ptr;
} // eo UnixServerSocketBase::acceptNewConnection(int,boost::any);
*
* @param fd file descriptor for the socket
* @param path path as delivered by peer.
- * @param peer_pid peer pid.
- * @param peer_uid peer uid.
- * @param peer_gid peer gid.
* @return (shared) pointer to the new io instance.
*/
UnixIOSocketPtr UnixServerSocketBase::createIOSocket(
- int fd, const std::string& path,
- unsigned int peer_pid,
- unsigned int peer_uid, unsigned int peer_gid)
+ int fd, const std::string& path
+)
{
- return UnixIOSocketPtr(
- new UnixIOSocket(fd, path, peer_pid, peer_uid, peer_gid)
+ return UnixIOSocketPtr
+ (
+ new UnixIOSocket(fd, path)
);
-} // eo UnixServerSocketBase::createIOSocket(int,const std::string&,unsigned,unsigned,unsigned)
+} // eo UnixServerSocketBase::createIOSocket(int,const std::string&)
/** @file
* @brief socket classes for the AsyncIo framework.
*
- * (c) Copyright 2008 by Intra2net AG
+ * @copyright Copyright 2008-2009 by Intra2net AG
+ * @contact Intra2net Opensource Team \<opensource@intra2net.com\>
*/
-#ifndef __SIMPLEIO__SIMPLESOCKET_HPP__
-#define __SIMPLEIO__SIMPLESOCKET_HPP__
+#ifndef __ASYNC_SOCKET_HPP__
+#define __ASYNC_SOCKET_HPP__
#include "async_io.hpp"
+#include "asyncio_system_tools.hpp"
#include <string>
#include <boost/any.hpp>
/**
- * @brief spezialized IO class for unix domain sockets.
+ * @brief specialized IO class for unix domain sockets.
*
*/
class UnixIOSocket
friend class UnixServerSocketBase;
friend class UnixServerSocket<UnixIOSocket>;
- UnixIOSocket(
- int fd, const std::string& path,
- unsigned int peer_pid, unsigned int peer_uid, unsigned int peer_gid);
+ UnixIOSocket(int fd, const std::string& path);
+
+ bool update_peer_information(int fd);
protected:
/**
- * @brief spezialized server socket class for unix domain sockets.
+ * @brief specialized server socket class for unix domain sockets.
*
*/
class UnixServerSocketBase
virtual IOImplementationPtr acceptNewConnection(int fd, boost::any addr);
- virtual UnixIOSocketPtr createIOSocket(
- int fd, const std::string& path,
- unsigned int peer_pid,
- unsigned int peer_uid, unsigned int peer_gid);
+ virtual UnixIOSocketPtr createIOSocket(int fd, const std::string& path);
protected:
/**
* @brief unix server socket class which "produces" connections of a determined type.
*
- + @param IOClass the type of the connections.
+ * @param IOClass the type of the connections.
*/
template<
class IOClass
protected:
virtual UnixIOSocketPtr createIOSocket(
- int fd, const std::string& path,
- unsigned int peer_pid,
- unsigned int peer_uid, unsigned int peer_gid)
+ int fd, const std::string& path)
{
return UnixIOSocketPtr(
- new IOClass(fd, path, peer_pid, peer_uid, peer_gid)
+ new IOClass(fd, path)
);
}
}; // eo class UnixServerSocket
-}// eo namespace AsyncIo
+
+}// end of namespace AsyncIo
#endif