~ubuntu-branches/ubuntu/wily/libtorrent/wily-proposed

« back to all changes in this revision

Viewing changes to .pc/libtorrent-0.12.6-ipv6-07.patch/src/net/socket_fd.cc

  • 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
// libTorrent - BitTorrent library
 
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
#include "config.h"
 
38
 
 
39
#include <errno.h>
 
40
#include <fcntl.h>
 
41
#include <sys/ioctl.h>
 
42
#include <sys/types.h>
 
43
#include <sys/socket.h>
 
44
#include <arpa/inet.h>
 
45
#include <netinet/in.h>
 
46
#include <netinet/in_systm.h>
 
47
#include <netinet/ip.h>
 
48
#include <rak/socket_address.h>
 
49
 
 
50
#include "torrent/exceptions.h"
 
51
#include "socket_fd.h"
 
52
 
 
53
namespace torrent {
 
54
 
 
55
inline void
 
56
SocketFd::check_valid() const {
 
57
  if (!is_valid())
 
58
    throw internal_error("SocketFd function called on an invalid fd.");
 
59
}
 
60
 
 
61
bool
 
62
SocketFd::set_nonblock() {
 
63
  check_valid();
 
64
 
 
65
  return fcntl(m_fd, F_SETFL, O_NONBLOCK) == 0;
 
66
}
 
67
 
 
68
bool
 
69
SocketFd::set_priority(priority_type p) {
 
70
  check_valid();
 
71
  int opt = p;
 
72
 
 
73
  return setsockopt(m_fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)) == 0;
 
74
}
 
75
 
 
76
bool
 
77
SocketFd::set_reuse_address(bool state) {
 
78
  check_valid();
 
79
  int opt = state;
 
80
 
 
81
  return setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == 0;
 
82
}
 
83
 
 
84
bool
 
85
SocketFd::set_send_buffer_size(uint32_t s) {
 
86
  check_valid();
 
87
  int opt = s;
 
88
 
 
89
  return setsockopt(m_fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) == 0;
 
90
}
 
91
 
 
92
bool
 
93
SocketFd::set_receive_buffer_size(uint32_t s) {
 
94
  check_valid();
 
95
  int opt = s;
 
96
 
 
97
  return setsockopt(m_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) == 0;
 
98
}
 
99
 
 
100
int
 
101
SocketFd::get_error() const {
 
102
  check_valid();
 
103
 
 
104
  int err;
 
105
  socklen_t length = sizeof(err);
 
106
 
 
107
  if (getsockopt(m_fd, SOL_SOCKET, SO_ERROR, &err, &length) == -1)
 
108
    throw internal_error("SocketFd::get_error() could not get error");
 
109
 
 
110
  return err;
 
111
}
 
112
 
 
113
bool
 
114
SocketFd::open_stream() {
 
115
  return (m_fd = socket(rak::socket_address::pf_inet, SOCK_STREAM, IPPROTO_TCP)) != -1;
 
116
}
 
117
 
 
118
bool
 
119
SocketFd::open_datagram() {
 
120
  return (m_fd = socket(rak::socket_address::pf_inet, SOCK_DGRAM, 0)) != -1;
 
121
}
 
122
 
 
123
bool
 
124
SocketFd::open_local() {
 
125
  return (m_fd = socket(rak::socket_address::pf_local, SOCK_STREAM, 0)) != -1;
 
126
}
 
127
 
 
128
void
 
129
SocketFd::close() {
 
130
  if (::close(m_fd) && errno == EBADF)
 
131
    throw internal_error("SocketFd::close() called on an invalid file descriptor");
 
132
}
 
133
 
 
134
bool
 
135
SocketFd::bind(const rak::socket_address& sa) {
 
136
  check_valid();
 
137
 
 
138
  return !::bind(m_fd, sa.c_sockaddr(), sa.length());
 
139
}
 
140
 
 
141
bool
 
142
SocketFd::bind(const rak::socket_address& sa, unsigned int length) {
 
143
  check_valid();
 
144
 
 
145
  return !::bind(m_fd, sa.c_sockaddr(), length);
 
146
}
 
147
 
 
148
bool
 
149
SocketFd::connect(const rak::socket_address& sa) {
 
150
  check_valid();
 
151
 
 
152
  return !::connect(m_fd, sa.c_sockaddr(), sa.length()) || errno == EINPROGRESS;
 
153
}
 
154
 
 
155
bool
 
156
SocketFd::listen(int size) {
 
157
  check_valid();
 
158
 
 
159
  return !::listen(m_fd, size);
 
160
}
 
161
 
 
162
SocketFd
 
163
SocketFd::accept(rak::socket_address* sa) {
 
164
  check_valid();
 
165
  socklen_t len = sizeof(rak::socket_address);
 
166
 
 
167
  return SocketFd(::accept(m_fd, sa != NULL ? sa->c_sockaddr() : NULL, &len));
 
168
}
 
169
 
 
170
// unsigned int
 
171
// SocketFd::get_read_queue_size() const {
 
172
//   unsigned int v;
 
173
 
 
174
//   if (!is_valid() || ioctl(m_fd, SIOCINQ, &v) < 0)
 
175
//     throw internal_error("SocketFd::get_read_queue_size() could not be performed");
 
176
 
 
177
//   return v;
 
178
// }
 
179
 
 
180
// unsigned int
 
181
// SocketFd::get_write_queue_size() const {
 
182
//   unsigned int v;
 
183
 
 
184
//   if (!is_valid() || ioctl(m_fd, SIOCOUTQ, &v) < 0)
 
185
//     throw internal_error("SocketFd::get_write_queue_size() could not be performed");
 
186
 
 
187
//   return v;
 
188
// }
 
189
 
 
190
}