/usr/include/claw/socket_traits_unix.hpp is in libclaw-net-dev 1.7.4-2.
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 | /*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
/**
* \file socket_traits_unix.hpp
* \brief Unix interface for using sockets.
* \author Julien Jorge
*/
#ifndef __CLAW_SOCKET_TRAITS_UNIX_HPP__
#define __CLAW_SOCKET_TRAITS_UNIX_HPP__
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <unistd.h>
#include <cstring>
#include <claw/assert.hpp>
namespace claw
{
/**
* \brief Unix interface for using sockets.
* \author Julien Jorge
*/
class socket_traits_unix
{
public:
/** \brief Type of the system description of the socket. */
typedef int descriptor;
public:
/** \brief Invalid socket descriptor. */
static const descriptor invalid_socket = -1;
public:
/*------------------------------------------------------------------------*/
/**
* \brief Initialize the use of the socket library.
* \return true if the initialization is successful.
*/
static bool init()
{
return true;
} // socket_traits_unix::init()
/*------------------------------------------------------------------------*/
/**
* \brief Close the socket library.
* \return true if the operation is successful.
*/
static bool release()
{
return true;
} // socket_traits_unix::release()
/*------------------------------------------------------------------------*/
/**
* \brief Open a socket.
* \return The descriptor on the loaded socket.
*/
static descriptor open()
{
descriptor fd = invalid_socket;
fd = socket(AF_INET, SOCK_STREAM, 0);
return fd;
} // socket_traits_unix::open()
/*------------------------------------------------------------------------*/
/**
* \brief Close a socket.
* \param d The descriptor of the socket to close.
* \return true if the socket has been closed.
*/
static bool close( descriptor d )
{
return ::close(d) == 0;
} // socket_traits_unix::close()
/*------------------------------------------------------------------------*/
/**
* \brief Connect a socket to a port.
* \param d The descriptor of the socket to connect.
* \param address The adress to connect to.
* \param port The port to connect to.
* \return true if the connection is available.
*/
static bool connect( descriptor d, const std::string& address, int port )
{
CLAW_PRECOND( d != invalid_socket );
bool result = false;
struct hostent* hp = gethostbyname(address.c_str());
if (hp)
{
struct sockaddr_in sa;
memset (&sa, '\0', sizeof(sa));
sa.sin_family = hp->h_addrtype;
sa.sin_port = htons(port);
memcpy( &sa.sin_addr, hp->h_addr, hp->h_length );
if (::connect(d, (struct sockaddr*)&sa, (socklen_t)sizeof(sa)) != -1)
result = true;
}
return result;
} // socket_traits_unix::connect()
/*------------------------------------------------------------------------*/
/**
* \brief Open a socket for incoming connexions.
* \param d The descriptor of the socket to open.
* \param port The port to connect to.
* \param queue_size The size of the queue for incoming connexions.
* \return true if the socket has been opened.
*/
static bool listen( descriptor d, int port, unsigned int queue_size )
{
CLAW_PRECOND( d != invalid_socket );
struct sockaddr_in addr;
memset (&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if ( bind(d, (struct sockaddr*)&addr, sizeof(addr)) != -1 )
return ::listen(d, queue_size) != -1;
else
return false;
} // socket_traits_unix::listen()
/*------------------------------------------------------------------------*/
/**
* \brief Select a socket for reading.
* \param d The descriptor of the socket to read.
* \param time_limit Maximum of seconds to wait before considering there's
* nothing to read. If \a time_limit is negative, the method wait
* until there is something to read.
* \return true if the socket is ready to be read.
*/
static bool select_read( descriptor d, int time_limit = -1 )
{
CLAW_PRECOND( d != invalid_socket );
struct timeval tv, *ptv;
fd_set fds;
if ( time_limit < 0 )
ptv = NULL;
else
{
tv.tv_sec = time_limit;
tv.tv_usec = 0;
ptv = &tv;
}
FD_ZERO(&fds);
FD_SET(d, &fds);
select( d+1, &fds, NULL, NULL, ptv );
return FD_ISSET( d, &fds );
} // socket_traits_unix::select_read()
/*------------------------------------------------------------------------*/
/**
* \brief Accept an incoming connexion.
* \param d The descriptor of the socket to listen.
* \return The descriptor of the incoming connexion.
*/
static descriptor accept( descriptor d )
{
return ::accept( d, NULL, NULL );
} // socket_traits_unix::accept()
/*------------------------------------------------------------------------*/
/**
* \brief Tell if a descriptor is a valid socket descriptor.
* \param d The descriptor to test.
*/
static bool valid_descriptor( descriptor d )
{
return d != invalid_socket;
} // socket_traits_unix::valid_descriptor()
/*------------------------------------------------------------------------*/
/**
* \brief Tell if a descriptor is a opened socket.
* \param d The descriptor to test.
*/
static bool is_open( descriptor d )
{
struct stat buf;
return fstat(d, &buf) == 0;
} // socket_traits_unix::is_open()
}; // class socket_traits_unix
typedef socket_traits_unix socket_traits;
} // namespace claw
#endif // __CLAW_SOCKET_TRAITS_UNIX_HPP__
|