~ubuntu-branches/ubuntu/oneiric/libtorrent/oneiric

« back to all changes in this revision

Viewing changes to .pc/libtorrent-0.12.6-ipv6-07.patch/rak/socket_address.h

  • Committer: Bazaar Package Importer
  • Author(s): Rogério Brito
  • Date: 2011-03-20 01:06:18 UTC
  • mfrom: (1.1.13 upstream) (4.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110320010618-g3wyylccqzqko73c
Tags: 0.12.7-5
* Use Steinar's "real" patch for IPv6. Addresses #490277, #618275,
  and Closes: #617791.
* Adapt libtorrent-0.12.6-ipv6-07.patch. It FTBFS otherwise.
* Add proper attibution to the IPv6 patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// rak - Rakshasa's toolbox
 
2
// Copyright (C) 2005-2007, Jari Sundell
 
3
//
 
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.
 
8
// 
 
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.
 
13
// 
 
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
 
17
//
 
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
 
22
// including the two.
 
23
//
 
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.
 
31
//
 
32
// Contact:  Jari Sundell <jaris@ifi.uio.no>
 
33
//
 
34
//           Skomakerveien 33
 
35
//           3185 Skoppum, NORWAY
 
36
 
 
37
// Wrappers for the various sockaddr types with focus on zero-copy
 
38
// casting between the original type and the wrapper class.
 
39
//
 
40
// The default ctor does not initialize any data.
 
41
//
 
42
// _n suffixes indicate that the argument or return value is in
 
43
// network byte order, _h that they are in hardware byte order.
 
44
 
 
45
// Add define for inet6 scope id?
 
46
 
 
47
#ifndef RAK_SOCKET_ADDRESS_H
 
48
#define RAK_SOCKET_ADDRESS_H
 
49
 
 
50
#include <cstring>
 
51
#include <string>
 
52
#include <stdexcept>
 
53
#include <arpa/inet.h>
 
54
#include <netinet/in.h>
 
55
#include <sys/types.h>
 
56
#include <sys/socket.h>
 
57
 
 
58
namespace rak {
 
59
 
 
60
class socket_address_inet;
 
61
class socket_address_inet6;
 
62
 
 
63
class socket_address {
 
64
public:
 
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;
 
71
 
 
72
#ifdef AF_LOCAL
 
73
  static const sa_family_t af_local  = AF_LOCAL;
 
74
  static const int         pf_local  = PF_LOCAL;
 
75
#else
 
76
  static const sa_family_t af_local  = AF_UNIX;
 
77
  static const int         pf_local  = PF_UNIX;
 
78
#endif
 
79
 
 
80
  bool                is_valid() const;
 
81
  bool                is_bindable() const;
 
82
  bool                is_address_any() const;
 
83
 
 
84
  // Should we need to set AF_UNSPEC?
 
85
  void                clear()                                 { std::memset(this, 0, sizeof(socket_address)); set_family(); }
 
86
 
 
87
  sa_family_t         family() const                          { return m_sockaddr.sa_family; }
 
88
  void                set_family()                            { m_sockaddr.sa_family = af_unspec; }
 
89
 
 
90
  uint16_t            port() const;
 
91
  void                set_port(uint16_t p);
 
92
 
 
93
  std::string         address_str() const;
 
94
  bool                address_c_str(char* buf, socklen_t size) const;
 
95
 
 
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);
 
100
 
 
101
  uint32_t            length() const;
 
102
 
 
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); }
 
105
 
 
106
  sockaddr*           c_sockaddr()                            { return &m_sockaddr; }
 
107
  sockaddr_in*        c_sockaddr_inet()                       { return &m_sockaddrInet; }
 
108
 
 
109
  const sockaddr*     c_sockaddr() const                      { return &m_sockaddr; }
 
110
  const sockaddr_in*  c_sockaddr_inet() const                 { return &m_sockaddrInet; }
 
111
 
 
112
#ifdef RAK_USE_INET6
 
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); }
 
115
 
 
116
  sockaddr_in6*       c_sockaddr_inet6()                      { return &m_sockaddrInet6; }
 
117
  const sockaddr_in6* c_sockaddr_inet6() const                { return &m_sockaddrInet6; }
 
118
#endif
 
119
 
 
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
 
122
  // struct.
 
123
  void                copy(const socket_address& src, size_t length);
 
124
 
 
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); }
 
127
 
 
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;
 
132
 
 
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); }
 
137
 
 
138
private:
 
139
  union {
 
140
    sockaddr            m_sockaddr;
 
141
    sockaddr_in         m_sockaddrInet;
 
142
#ifdef RAK_USE_INET6
 
143
    sockaddr_in6        m_sockaddrInet6;
 
144
#endif
 
145
  };
 
146
};
 
147
 
 
148
// Remeber to set the AF_INET.
 
149
 
 
150
class socket_address_inet {
 
151
public:
 
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); }
 
156
 
 
157
  void                clear()                                 { std::memset(this, 0, sizeof(socket_address_inet)); set_family(); }
 
158
 
 
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; }
 
163
 
 
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;
 
170
 
 
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);
 
176
 
 
177
  void                set_address_any()                       { set_port(0); set_address_h(INADDR_ANY); }
 
178
 
 
179
  sa_family_t         family() const                          { return m_sockaddr.sin_family; }
 
180
  void                set_family()                            { m_sockaddr.sin_family = AF_INET; }
 
181
 
 
182
  sockaddr*           c_sockaddr()                            { return reinterpret_cast<sockaddr*>(&m_sockaddr); }
 
183
  sockaddr_in*        c_sockaddr_inet()                       { return &m_sockaddr; }
 
184
 
 
185
  const sockaddr*     c_sockaddr() const                      { return reinterpret_cast<const sockaddr*>(&m_sockaddr); }
 
186
  const sockaddr_in*  c_sockaddr_inet() const                 { return &m_sockaddr; }
 
187
 
 
188
  bool                operator == (const socket_address_inet& rhs) const;
 
189
  bool                operator < (const socket_address_inet& rhs) const;
 
190
 
 
191
private:
 
192
  struct sockaddr_in  m_sockaddr;
 
193
};
 
194
 
 
195
// Unique key for the address, excluding port numbers etc.
 
196
class socket_address_key {
 
197
public:
 
198
//   socket_address_host_key() {}
 
199
 
 
200
  socket_address_key(const socket_address& sa) {
 
201
    *this = sa;
 
202
  }
 
203
 
 
204
  socket_address_key& operator = (const socket_address& sa) {
 
205
    if (sa.family() == 0) {
 
206
      std::memset(this, 0, sizeof(socket_address_key));
 
207
 
 
208
    } else if (sa.family() == socket_address::af_inet) {
 
209
      // Using hardware order as we use operator < to compare when
 
210
      // using inet only.
 
211
      m_addr.s_addr = sa.sa_inet()->address_h();
 
212
 
 
213
    } else {
 
214
      // When we implement INET6 handling, embed the ipv4 address in
 
215
      // the ipv6 address.
 
216
      throw std::logic_error("socket_address_key(...) received an unsupported protocol family.");
 
217
    }
 
218
 
 
219
    return *this;
 
220
  }
 
221
 
 
222
//   socket_address_key& operator = (const socket_address_key& sa) {
 
223
//   }
 
224
 
 
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;
 
228
  }    
 
229
 
 
230
private:
 
231
  union {
 
232
    in_addr m_addr;
 
233
// #ifdef RAK_USE_INET6
 
234
//     in_addr6 m_addr6;
 
235
// #endif
 
236
  };
 
237
};
 
238
 
 
239
inline bool
 
240
socket_address::is_valid() const {
 
241
  switch (family()) {
 
242
  case af_inet:
 
243
    return sa_inet()->is_valid();
 
244
//   case af_inet6:
 
245
//     return sa_inet6().is_valid();
 
246
  default:
 
247
    return false;
 
248
  }
 
249
}
 
250
 
 
251
inline bool
 
252
socket_address::is_bindable() const {
 
253
  switch (family()) {
 
254
  case af_inet:
 
255
    return !sa_inet()->is_address_any();
 
256
  default:
 
257
    return false;
 
258
  }
 
259
}
 
260
 
 
261
inline bool
 
262
socket_address::is_address_any() const {
 
263
  switch (family()) {
 
264
  case af_inet:
 
265
    return sa_inet()->is_address_any();
 
266
  default:
 
267
    return true;
 
268
  }
 
269
}
 
270
 
 
271
inline uint16_t
 
272
socket_address::port() const {
 
273
  switch (family()) {
 
274
  case af_inet:
 
275
    return sa_inet()->port();
 
276
  default:
 
277
    return 0;
 
278
  }
 
279
}
 
280
 
 
281
inline void
 
282
socket_address::set_port(uint16_t p) {
 
283
  switch (family()) {
 
284
  case af_inet:
 
285
    return sa_inet()->set_port(p);
 
286
  default:
 
287
    break;
 
288
  }
 
289
}
 
290
 
 
291
inline std::string
 
292
socket_address::address_str() const {
 
293
  switch (family()) {
 
294
  case af_inet:
 
295
    return sa_inet()->address_str();
 
296
  default:
 
297
    return std::string();
 
298
  }
 
299
}
 
300
 
 
301
inline bool
 
302
socket_address::address_c_str(char* buf, socklen_t size) const {
 
303
  switch (family()) {
 
304
  case af_inet:
 
305
    return sa_inet()->address_c_str(buf, size);
 
306
  default:
 
307
    return false;
 
308
  }
 
309
}
 
310
 
 
311
inline bool
 
312
socket_address::set_address_c_str(const char* a) {
 
313
  if (sa_inet()->set_address_c_str(a)) {
 
314
    sa_inet()->set_family();
 
315
    return true;
 
316
 
 
317
  } else {
 
318
    return false;
 
319
  }
 
320
}
 
321
 
 
322
// Is the zero length really needed, should we require some length?
 
323
inline uint32_t
 
324
socket_address::length() const {
 
325
  switch(family()) {
 
326
  case af_inet:
 
327
    return sizeof(sockaddr_in);
 
328
  default:
 
329
    return 0;
 
330
  }      
 
331
}
 
332
 
 
333
inline void
 
334
socket_address::copy(const socket_address& src, size_t length) {
 
335
  length = std::min(length, sizeof(socket_address));
 
336
  
 
337
  // Does this get properly optimized?
 
338
  std::memset(this, 0, sizeof(socket_address));
 
339
  std::memcpy(this, &src, length);
 
340
}
 
341
 
 
342
// Should we be able to compare af_unspec?
 
343
 
 
344
inline bool
 
345
socket_address::operator == (const socket_address& rhs) const {
 
346
  if (family() != rhs.family())
 
347
    return false;
 
348
 
 
349
  switch (family()) {
 
350
  case af_inet:
 
351
    return *sa_inet() == *rhs.sa_inet();
 
352
//   case af_inet6:
 
353
//     return *sa_inet6() == *rhs.sa_inet6();
 
354
  default:
 
355
    throw std::logic_error("socket_address::operator == (rhs) invalid type comparison.");
 
356
  }
 
357
}
 
358
 
 
359
inline bool
 
360
socket_address::operator < (const socket_address& rhs) const {
 
361
  if (family() != rhs.family())
 
362
    return family() < rhs.family();
 
363
 
 
364
  switch (family()) {
 
365
  case af_inet:
 
366
    return *sa_inet() < *rhs.sa_inet();
 
367
//   case af_inet6:
 
368
//     return *sa_inet6() < *rhs.sa_inet6();
 
369
  default:
 
370
    throw std::logic_error("socket_address::operator < (rhs) invalid type comparison.");
 
371
  }
 
372
}
 
373
 
 
374
inline std::string
 
375
socket_address_inet::address_str() const {
 
376
  char buf[INET_ADDRSTRLEN];
 
377
 
 
378
  if (!address_c_str(buf, INET_ADDRSTRLEN))
 
379
    return std::string();
 
380
 
 
381
  return std::string(buf);
 
382
}
 
383
 
 
384
inline bool
 
385
socket_address_inet::address_c_str(char* buf, socklen_t size) const {
 
386
  return inet_ntop(family(), &m_sockaddr.sin_addr, buf, size);
 
387
}
 
388
 
 
389
inline bool
 
390
socket_address_inet::set_address_c_str(const char* a) {
 
391
  return inet_pton(AF_INET, a, &m_sockaddr.sin_addr);
 
392
}
 
393
 
 
394
inline bool
 
395
socket_address_inet::operator == (const socket_address_inet& rhs) const {
 
396
  return
 
397
    m_sockaddr.sin_addr.s_addr == rhs.m_sockaddr.sin_addr.s_addr &&
 
398
    m_sockaddr.sin_port == rhs.m_sockaddr.sin_port;
 
399
}
 
400
 
 
401
inline bool
 
402
socket_address_inet::operator < (const socket_address_inet& rhs) const {
 
403
  return
 
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);
 
407
}
 
408
 
 
409
}
 
410
 
 
411
#endif