/usr/include/ui-utilcpp/Socket.hpp is in libui-utilcpp-dev 1.8.3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | /**
* @file
* @brief Socket, descriptors and sockets.
*/
#ifndef UI_UTIL_SOCKET_HPP
#define UI_UTIL_SOCKET_HPP
// STDC++
#include <string>
#include <iostream>
#include <vector>
// STDC
#include <ctime>
#include <cstring>
// C++ libraries
#include <ui-utilcpp/Exception.hpp>
#include <ui-utilcpp/Sys.hpp>
#include <ui-utilcpp/File.hpp>
namespace UI {
namespace Util {
/**
* @example EchoServer.cpp
* Example program for socket / socket streams implementation.
* Should be installed as ui-utilcpp-echoserver along with the library.
*/
/** @brief Socket abstraction. */
class Socket
#ifndef WIN32
: public FileDescriptor
#endif
{
public:
/** @brief Construct socket. */
Socket(int fd=-1, bool closeFd=false);
#ifdef WIN32
~Socket();
/** @brief Exceptions for this class. */
typedef CodeException<File::ErrorCode> Exception;
#endif
public:
/** @brief Get human-readable id string. */
std::string getId(bool const & peer=false) const;
/** @brief Get peer id. */
std::string getPeerId() const;
/** @name Configure socket behaviour.
* @{ */
Socket & setRcvTimeout(long int seconds, long int microseconds=0);
Socket & setSndTimeout(long int seconds, long int microseconds=0);
Socket & setUnblock(bool unblock=true);
/** @} */
/** @brief Connect this socket. */
virtual Socket & connect();
/** @brief Bind this socket. */
virtual Socket & bind();
/** @brief Start listening. */
Socket & listen(int backlog=16);
/** @brief Accept an incoming socket connection. */
int accept(long int toSeconds=0, long int toMicroSeconds=0);
/** @brief Shutdown socket. */
int shutdown(int how, bool doThrow=true);
/** @name Wrappers for recv(2) and send(2).
* @{ */
ssize_t recv(void * const buf, size_t len, int flags=0);
ssize_t send(void const * const msg, size_t len, int flags=0);
/** @} */
/** @brief C++-like virtual read method
*
* This implementation uses recv(2).
*/
virtual std::streamsize read(void * const buf, std::streamsize count);
/** @brief C++-like virtual write method
*
* This implementation uses send(2).
*/
virtual std::streamsize write(void const * const buf, std::streamsize count);
#ifdef WIN32
int getFd() const
{
return (int)fd_;
}
void sendStoredBuffer();
protected:
SOCKET fd_;
bool closeFd_;
std::vector<char> sendBuffer; // This buffer stores all data that is send using the stdSend() method;
// all stored data is actually send before destroying the object.
#endif
};
/** @brief INet Socket. */
class INetSocket: public Socket
{
private:
INetSocket(INetSocket const &);
public:
INetSocket(std::string const & host, unsigned int port, bool closeFd=true, bool reuseAddr=false);
INetSocket(int fd, bool closeFd=false);
~INetSocket();
virtual INetSocket & bind();
virtual INetSocket & connect();
private:
struct addrinfo * addrInfo_;
struct addrinfo * addr_;
};
/** @brief Unix Socket. */
#ifndef WIN32
class UnixSocket: public Socket
{
public:
UnixSocket(std::string const & path, bool closeFd=true);
UnixSocket(int fd, bool closeFd=false);
~UnixSocket();
/** @brief Bind this unix socket.
*
* @note Default permissions: "srw-------" (Only owner has rw-Access).
*/
UnixSocket & unixBind(uid_t uid=::geteuid(), gid_t gid=::getegid(), mode_t mode=S_IRUSR | S_IWUSR, bool silentUnlink=true);
virtual UnixSocket & bind();
virtual UnixSocket & connect();
private:
struct sockaddr_un unSa_;
bool silentUnlink_;
};
#endif
/** @brief Template IO stream buffer for all file descriptors types. */
template <typename FDType>
class FDTypeBuf: public std::streambuf, private FDType
{
public:
/** @brief Constructor for file descriptor stream buffer. */
FDTypeBuf(int fd, bool closeFd=false, int bufPutbackSize=4, int bufDataSize=1024)
:FDType(fd, closeFd)
,bufPutbackSize_(bufPutbackSize)
,bufDataSize_(bufDataSize)
,buf_(new char[bufPutbackSize_+bufDataSize_])
{
setg(buf_+bufPutbackSize, buf_+bufPutbackSize, buf_+bufPutbackSize);
}
~FDTypeBuf()
{
delete[] buf_;
}
/** @brief Shortcut. */
typedef std::streambuf::traits_type traits;
protected:
/** @brief Buffer control variables. */
int bufPutbackSize_, bufDataSize_;
/** @brief Internal buffer. */
char * buf_;
/** @brief streambuf overflow overload. */
virtual int overflow(int c)
{
if (c != traits::eof())
{
char ch((char)c);
if (FDType::write(&ch, 1) != 1)
{
return traits::eof();
}
}
return c;
}
/** @brief streambuf xsputn overload. */
virtual std::streamsize xsputn(const char * s, std::streamsize n)
{
return FDType::write(s, n);
}
/** @brief streambuf underflow overload. */
virtual int underflow()
{
if (gptr() >= egptr())
{
int numPutback(gptr() - eback());
if (numPutback > bufPutbackSize_)
{
numPutback = bufPutbackSize_;
}
std::memmove(buf_+(bufPutbackSize_-numPutback), gptr()-numPutback, numPutback);
std::streamsize num(FDType::read(buf_+bufPutbackSize_, bufDataSize_));
if (num <= 0)
{
return traits::eof();
}
setg(buf_+(bufPutbackSize_-numPutback), buf_+bufPutbackSize_, buf_+bufPutbackSize_+num);
}
return traits::to_int_type(*gptr());
}
};
/** @brief IO stream for file descriptors. */
template <typename FDType>
class FDTypeStream: public std::iostream
{
public:
/** Constructor for file descriptor stream. */
FDTypeStream(int fd, bool closeFd=false)
:std::iostream(0)
,buf_(fd, closeFd)
{
rdbuf(&buf_);
}
private:
FDTypeBuf<FDType> buf_;
};
/** @brief socketpair(2) abstraction. */
class SocketPair
{
private:
bool const closeFd_;
int sv_[2];
public:
SocketPair(bool const & closeFd=true);
~SocketPair();
int const & first() const;
int const & second() const;
};
}}
#endif
|