1
// rak - Rakshasa's toolbox
2
// Copyright (C) 2005-2007, Jari Sundell
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
// In addition, as a special exception, the copyright holders give
19
// permission to link the code of portions of this program with the
20
// OpenSSL library under certain conditions as described in each
21
// individual source file, and distribute linked combinations
24
// You must obey the GNU General Public License in all respects for
25
// all of the code used other than OpenSSL. If you modify file(s)
26
// with this exception, you may extend this exception to your version
27
// of the file(s), but you are not obligated to do so. If you do not
28
// wish to do so, delete this exception statement from your version.
29
// If you delete this exception statement from all source files in the
30
// program, then also delete it here.
32
// Contact: Jari Sundell <jaris@ifi.uio.no>
35
// 3185 Skoppum, NORWAY
37
// Wrappers for the various sockaddr types with focus on zero-copy
38
// casting between the original type and the wrapper class.
40
// The default ctor does not initialize any data.
42
// _n suffixes indicate that the argument or return value is in
43
// network byte order, _h that they are in hardware byte order.
45
// Add define for inet6 scope id?
47
#ifndef RAK_SOCKET_ADDRESS_H
48
#define RAK_SOCKET_ADDRESS_H
53
#include <arpa/inet.h>
54
#include <netinet/in.h>
55
#include <sys/types.h>
56
#include <sys/socket.h>
60
class socket_address_inet;
61
class socket_address_inet6;
63
class socket_address {
65
static const sa_family_t af_inet = AF_INET;
66
static const int pf_inet = PF_INET;
67
static const sa_family_t af_inet6 = AF_INET6;
68
static const int pf_inet6 = PF_INET6;
69
static const sa_family_t af_unspec = AF_UNSPEC;
70
static const int pf_unspec = PF_UNSPEC;
73
static const sa_family_t af_local = AF_LOCAL;
74
static const int pf_local = PF_LOCAL;
76
static const sa_family_t af_local = AF_UNIX;
77
static const int pf_local = PF_UNIX;
80
bool is_valid() const;
81
bool is_bindable() const;
82
bool is_address_any() const;
84
// Should we need to set AF_UNSPEC?
85
void clear() { std::memset(this, 0, sizeof(socket_address)); set_family(); }
87
sa_family_t family() const { return m_sockaddr.sa_family; }
88
void set_family() { m_sockaddr.sa_family = af_unspec; }
90
uint16_t port() const;
91
void set_port(uint16_t p);
93
std::string address_str() const;
94
bool address_c_str(char* buf, socklen_t size) const;
96
// Attemts to set it as an inet, then an inet6 address. It will
97
// never set anything but net addresses, no local/unix.
98
bool set_address_str(const std::string& a) { return set_address_c_str(a.c_str()); }
99
bool set_address_c_str(const char* a);
101
uint32_t length() const;
103
socket_address_inet* sa_inet() { return reinterpret_cast<socket_address_inet*>(this); }
104
const socket_address_inet* sa_inet() const { return reinterpret_cast<const socket_address_inet*>(this); }
106
sockaddr* c_sockaddr() { return &m_sockaddr; }
107
sockaddr_in* c_sockaddr_inet() { return &m_sockaddrInet; }
109
const sockaddr* c_sockaddr() const { return &m_sockaddr; }
110
const sockaddr_in* c_sockaddr_inet() const { return &m_sockaddrInet; }
113
socket_address_inet6* sa_inet6() { return reinterpret_cast<socket_address_inet6*>(this); }
114
const socket_address_inet6* sa_inet6() const { return reinterpret_cast<const socket_address_inet6*>(this); }
116
sockaddr_in6* c_sockaddr_inet6() { return &m_sockaddrInet6; }
117
const sockaddr_in6* c_sockaddr_inet6() const { return &m_sockaddrInet6; }
120
// Copy a socket address which has the length 'length. Zero out any
121
// extranous bytes and ensure it does not go beyond the size of this
123
void copy(const socket_address& src, size_t length);
125
static socket_address* cast_from(sockaddr* sa) { return reinterpret_cast<socket_address*>(sa); }
126
static const socket_address* cast_from(const sockaddr* sa) { return reinterpret_cast<const socket_address*>(sa); }
128
// The different families will be sorted according to the
129
// sa_family_t's numeric value.
130
bool operator == (const socket_address& rhs) const;
131
bool operator < (const socket_address& rhs) const;
133
bool operator == (const sockaddr& rhs) const { return *this == *cast_from(&rhs); }
134
bool operator == (const sockaddr* rhs) const { return *this == *cast_from(rhs); }
135
bool operator < (const sockaddr& rhs) const { return *this == *cast_from(&rhs); }
136
bool operator < (const sockaddr* rhs) const { return *this == *cast_from(rhs); }
141
sockaddr_in m_sockaddrInet;
143
sockaddr_in6 m_sockaddrInet6;
148
// Remeber to set the AF_INET.
150
class socket_address_inet {
152
bool is_any() const { return is_port_any() && is_address_any(); }
153
bool is_valid() const { return !is_port_any() && !is_address_any(); }
154
bool is_port_any() const { return port() == 0; }
155
bool is_address_any() const { return m_sockaddr.sin_addr.s_addr == htonl(INADDR_ANY); }
157
void clear() { std::memset(this, 0, sizeof(socket_address_inet)); set_family(); }
159
uint16_t port() const { return ntohs(m_sockaddr.sin_port); }
160
uint16_t port_n() const { return m_sockaddr.sin_port; }
161
void set_port(uint16_t p) { m_sockaddr.sin_port = htons(p); }
162
void set_port_n(uint16_t p) { m_sockaddr.sin_port = p; }
164
// Should address() return the uint32_t?
165
in_addr address() const { return m_sockaddr.sin_addr; }
166
uint32_t address_h() const { return ntohl(m_sockaddr.sin_addr.s_addr); }
167
uint32_t address_n() const { return m_sockaddr.sin_addr.s_addr; }
168
std::string address_str() const;
169
bool address_c_str(char* buf, socklen_t size) const;
171
void set_address(in_addr a) { m_sockaddr.sin_addr = a; }
172
void set_address_h(uint32_t a) { m_sockaddr.sin_addr.s_addr = htonl(a); }
173
void set_address_n(uint32_t a) { m_sockaddr.sin_addr.s_addr = a; }
174
bool set_address_str(const std::string& a) { return set_address_c_str(a.c_str()); }
175
bool set_address_c_str(const char* a);
177
void set_address_any() { set_port(0); set_address_h(INADDR_ANY); }
179
sa_family_t family() const { return m_sockaddr.sin_family; }
180
void set_family() { m_sockaddr.sin_family = AF_INET; }
182
sockaddr* c_sockaddr() { return reinterpret_cast<sockaddr*>(&m_sockaddr); }
183
sockaddr_in* c_sockaddr_inet() { return &m_sockaddr; }
185
const sockaddr* c_sockaddr() const { return reinterpret_cast<const sockaddr*>(&m_sockaddr); }
186
const sockaddr_in* c_sockaddr_inet() const { return &m_sockaddr; }
188
bool operator == (const socket_address_inet& rhs) const;
189
bool operator < (const socket_address_inet& rhs) const;
192
struct sockaddr_in m_sockaddr;
195
// Unique key for the address, excluding port numbers etc.
196
class socket_address_key {
198
// socket_address_host_key() {}
200
socket_address_key(const socket_address& sa) {
204
socket_address_key& operator = (const socket_address& sa) {
205
if (sa.family() == 0) {
206
std::memset(this, 0, sizeof(socket_address_key));
208
} else if (sa.family() == socket_address::af_inet) {
209
// Using hardware order as we use operator < to compare when
211
m_addr.s_addr = sa.sa_inet()->address_h();
214
// When we implement INET6 handling, embed the ipv4 address in
216
throw std::logic_error("socket_address_key(...) received an unsupported protocol family.");
222
// socket_address_key& operator = (const socket_address_key& sa) {
225
bool operator < (const socket_address_key& sa) const {
226
// Compare the memory area instead.
227
return m_addr.s_addr < sa.m_addr.s_addr;
233
// #ifdef RAK_USE_INET6
240
socket_address::is_valid() const {
243
return sa_inet()->is_valid();
245
// return sa_inet6().is_valid();
252
socket_address::is_bindable() const {
255
return !sa_inet()->is_address_any();
262
socket_address::is_address_any() const {
265
return sa_inet()->is_address_any();
272
socket_address::port() const {
275
return sa_inet()->port();
282
socket_address::set_port(uint16_t p) {
285
return sa_inet()->set_port(p);
292
socket_address::address_str() const {
295
return sa_inet()->address_str();
297
return std::string();
302
socket_address::address_c_str(char* buf, socklen_t size) const {
305
return sa_inet()->address_c_str(buf, size);
312
socket_address::set_address_c_str(const char* a) {
313
if (sa_inet()->set_address_c_str(a)) {
314
sa_inet()->set_family();
322
// Is the zero length really needed, should we require some length?
324
socket_address::length() const {
327
return sizeof(sockaddr_in);
334
socket_address::copy(const socket_address& src, size_t length) {
335
length = std::min(length, sizeof(socket_address));
337
// Does this get properly optimized?
338
std::memset(this, 0, sizeof(socket_address));
339
std::memcpy(this, &src, length);
342
// Should we be able to compare af_unspec?
345
socket_address::operator == (const socket_address& rhs) const {
346
if (family() != rhs.family())
351
return *sa_inet() == *rhs.sa_inet();
353
// return *sa_inet6() == *rhs.sa_inet6();
355
throw std::logic_error("socket_address::operator == (rhs) invalid type comparison.");
360
socket_address::operator < (const socket_address& rhs) const {
361
if (family() != rhs.family())
362
return family() < rhs.family();
366
return *sa_inet() < *rhs.sa_inet();
368
// return *sa_inet6() < *rhs.sa_inet6();
370
throw std::logic_error("socket_address::operator < (rhs) invalid type comparison.");
375
socket_address_inet::address_str() const {
376
char buf[INET_ADDRSTRLEN];
378
if (!address_c_str(buf, INET_ADDRSTRLEN))
379
return std::string();
381
return std::string(buf);
385
socket_address_inet::address_c_str(char* buf, socklen_t size) const {
386
return inet_ntop(family(), &m_sockaddr.sin_addr, buf, size);
390
socket_address_inet::set_address_c_str(const char* a) {
391
return inet_pton(AF_INET, a, &m_sockaddr.sin_addr);
395
socket_address_inet::operator == (const socket_address_inet& rhs) const {
397
m_sockaddr.sin_addr.s_addr == rhs.m_sockaddr.sin_addr.s_addr &&
398
m_sockaddr.sin_port == rhs.m_sockaddr.sin_port;
402
socket_address_inet::operator < (const socket_address_inet& rhs) const {
404
m_sockaddr.sin_addr.s_addr < rhs.m_sockaddr.sin_addr.s_addr ||
405
(m_sockaddr.sin_addr.s_addr == rhs.m_sockaddr.sin_addr.s_addr &&
406
m_sockaddr.sin_port < rhs.m_sockaddr.sin_port);