/usr/include/pj++/sock.hpp is in libpjproject-dev 2.1.0.0.ast20130823-1.
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | /* $Id: sock.hpp 2394 2008-12-23 17:27:53Z bennylp $ */
/*
* Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
*
* 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
*/
#ifndef __PJPP_SOCK_HPP__
#define __PJPP_SOCK_HPP__
#include <pj/sock.h>
#include <pj/string.h>
class Pj_Event_Handler;
//
// Base class for address.
//
class Pj_Addr
{
};
//
// Internet address.
//
class Pj_Inet_Addr : public pj_sockaddr_in, public Pj_Addr
{
public:
//
// Get port number.
//
pj_uint16_t get_port_number() const
{
return pj_sockaddr_in_get_port(this);
}
//
// Set port number.
//
void set_port_number(pj_uint16_t port)
{
sin_family = PJ_AF_INET;
pj_sockaddr_in_set_port(this, port);
}
//
// Get IP address.
//
pj_uint32_t get_ip_address() const
{
return pj_sockaddr_in_get_addr(this).s_addr;
}
//
// Get address string.
//
const char *get_address() const
{
return pj_inet_ntoa(sin_addr);
}
//
// Set IP address.
//
void set_ip_address(pj_uint32_t addr)
{
sin_family = PJ_AF_INET;
pj_sockaddr_in_set_addr(this, addr);
}
//
// Set address.
//
pj_status_t set_address(const pj_str_t *addr)
{
return pj_sockaddr_in_set_str_addr(this, addr);
}
//
// Set address.
//
pj_status_t set_address(const char *addr)
{
pj_str_t s;
return pj_sockaddr_in_set_str_addr(this, pj_cstr(&s, addr));
}
//
// Compare for equality.
//
bool operator==(const Pj_Inet_Addr &rhs) const
{
return sin_family == rhs.sin_family &&
sin_addr.s_addr == rhs.sin_addr.s_addr &&
sin_port == rhs.sin_port;
}
private:
//
// Dummy length used in pj_ioqueue_recvfrom() etc
//
friend class Pj_Event_Handler;
friend class Pj_Socket;
friend class Pj_Sock_Stream;
friend class Pj_Sock_Dgram;
int addrlen_;
};
//
// Socket base class.
//
// Note:
// socket will not automatically be closed on destructor.
//
class Pj_Socket
{
public:
//
// Default constructor.
//
Pj_Socket()
: sock_(PJ_INVALID_SOCKET)
{
}
//
// Initialize from a socket handle.
//
explicit Pj_Socket(pj_sock_t sock)
: sock_(sock)
{
}
//
// Copy constructor.
//
Pj_Socket(const Pj_Socket &rhs)
: sock_(rhs.sock_)
{
}
//
// Destructor will not close the socket.
// You must call close() explicitly.
//
~Pj_Socket()
{
}
//
// Set socket handle.
//
void set_handle(pj_sock_t sock)
{
sock_ = sock;
}
//
// Get socket handle.
//
pj_sock_t get_handle() const
{
return sock_;
}
//
// Get socket handle.
//
pj_sock_t& get_handle()
{
return sock_;
}
//
// See if the socket is valid.
//
bool is_valid() const
{
return sock_ != PJ_INVALID_SOCKET;
}
//
// Create the socket.
//
pj_status_t create(int af, int type, int proto)
{
return pj_sock_socket(af, type, proto, &sock_);
}
//
// Bind socket.
//
pj_status_t bind(const Pj_Inet_Addr &addr)
{
return pj_sock_bind(sock_, &addr, sizeof(Pj_Inet_Addr));
}
//
// Close socket.
//
pj_status_t close()
{
pj_sock_close(sock_);
}
//
// Get peer socket name.
//
pj_status_t getpeername(Pj_Inet_Addr *addr)
{
return pj_sock_getpeername(sock_, addr, &addr->addrlen_);
}
//
// getsockname
//
pj_status_t getsockname(Pj_Inet_Addr *addr)
{
return pj_sock_getsockname(sock_, addr, &addr->addrlen_);
}
//
// getsockopt.
//
pj_status_t getsockopt(pj_uint16_t level, pj_uint16_t optname,
void *optval, int *optlen)
{
return pj_sock_getsockopt(sock_, level, optname, optval, optlen);
}
//
// setsockopt
//
pj_status_t setsockopt(pj_uint16_t level, pj_uint16_t optname,
const void *optval, int optlen)
{
return pj_sock_setsockopt(sock_, level, optname, optval, optlen);
}
//
// receive data.
//
pj_ssize_t recv(void *buf, pj_size_t len, int flag = 0)
{
pj_ssize_t bytes = len;
if (pj_sock_recv(sock_, buf, &bytes, flag) != PJ_SUCCESS)
return -1;
return bytes;
}
//
// send data.
//
pj_ssize_t send(const void *buf, pj_ssize_t len, int flag = 0)
{
pj_ssize_t bytes = len;
if (pj_sock_send(sock_, buf, &bytes, flag) != PJ_SUCCESS)
return -1;
return bytes;
}
//
// connect.
//
pj_status_t connect(const Pj_Inet_Addr &addr)
{
return pj_sock_connect(sock_, &addr, sizeof(Pj_Inet_Addr));
}
//
// assignment.
//
Pj_Socket &operator=(const Pj_Socket &rhs)
{
sock_ = rhs.sock_;
return *this;
}
protected:
friend class Pj_Event_Handler;
pj_sock_t sock_;
};
#if PJ_HAS_TCP
//
// Stream socket.
//
class Pj_Sock_Stream : public Pj_Socket
{
public:
//
// Default constructor.
//
Pj_Sock_Stream()
{
}
//
// Initialize from a socket handle.
//
explicit Pj_Sock_Stream(pj_sock_t sock)
: Pj_Socket(sock)
{
}
//
// Copy constructor.
//
Pj_Sock_Stream(const Pj_Sock_Stream &rhs) : Pj_Socket(rhs)
{
}
//
// Assignment.
//
Pj_Sock_Stream &operator=(const Pj_Sock_Stream &rhs)
{
sock_ = rhs.sock_;
return *this;
}
//
// listen()
//
pj_status_t listen(int backlog = 5)
{
return pj_sock_listen(sock_, backlog);
}
//
// blocking accept()
//
Pj_Sock_Stream accept(Pj_Inet_Addr *remote_addr = NULL)
{
pj_sock_t newsock;
int *addrlen = remote_addr ? &remote_addr->addrlen_ : NULL;
pj_status_t status;
status = pj_sock_accept(sock_, &newsock, remote_addr, addrlen);
if (status != PJ_SUCCESS)
return Pj_Sock_Stream(-1);
return Pj_Sock_Stream(newsock);
}
//
// shutdown()
//
pj_status_t shutdown(int how = PJ_SHUT_RDWR)
{
return pj_sock_shutdown(sock_, how);
}
};
#endif
//
// Datagram socket.
//
class Pj_Sock_Dgram : public Pj_Socket
{
public:
//
// Default constructor.
//
Pj_Sock_Dgram()
{
}
//
// Initialize from a socket handle.
//
explicit Pj_Sock_Dgram(pj_sock_t sock)
: Pj_Socket(sock)
{
}
//
// Copy constructor.
//
Pj_Sock_Dgram(const Pj_Sock_Dgram &rhs)
: Pj_Socket(rhs)
{
}
//
// Assignment.
//
Pj_Sock_Dgram &operator=(const Pj_Sock_Dgram &rhs)
{
Pj_Socket::operator =(rhs);
return *this;
}
//
// recvfrom()
//
pj_ssize_t recvfrom( void *buf, pj_size_t len, int flag = 0,
Pj_Inet_Addr *fromaddr = NULL)
{
pj_ssize_t bytes = len;
int *addrlen = fromaddr ? &fromaddr->addrlen_ : NULL;
if (pj_sock_recvfrom( sock_, buf, &bytes, flag,
fromaddr, addrlen) != PJ_SUCCESS)
{
return -1;
}
return bytes;
}
//
// sendto()
//
pj_ssize_t sendto( const void *buf, pj_size_t len, int flag,
const Pj_Inet_Addr &addr)
{
pj_ssize_t bytes = len;
if (pj_sock_sendto( sock_, buf, &bytes, flag,
&addr, sizeof(pj_sockaddr_in)) != PJ_SUCCESS)
{
return -1;
}
return bytes;
}
};
#endif /* __PJPP_SOCK_HPP__ */
|