1
// libTorrent - BitTorrent library
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
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>
50
#include "torrent/exceptions.h"
51
#include "socket_fd.h"
56
SocketFd::check_valid() const {
58
throw internal_error("SocketFd function called on an invalid fd.");
62
SocketFd::set_nonblock() {
65
return fcntl(m_fd, F_SETFL, O_NONBLOCK) == 0;
69
SocketFd::set_priority(priority_type p) {
73
return setsockopt(m_fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)) == 0;
77
SocketFd::set_reuse_address(bool state) {
81
return setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == 0;
85
SocketFd::set_send_buffer_size(uint32_t s) {
89
return setsockopt(m_fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) == 0;
93
SocketFd::set_receive_buffer_size(uint32_t s) {
97
return setsockopt(m_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) == 0;
101
SocketFd::get_error() const {
105
socklen_t length = sizeof(err);
107
if (getsockopt(m_fd, SOL_SOCKET, SO_ERROR, &err, &length) == -1)
108
throw internal_error("SocketFd::get_error() could not get error");
114
SocketFd::open_stream() {
115
return (m_fd = socket(rak::socket_address::pf_inet, SOCK_STREAM, IPPROTO_TCP)) != -1;
119
SocketFd::open_datagram() {
120
return (m_fd = socket(rak::socket_address::pf_inet, SOCK_DGRAM, 0)) != -1;
124
SocketFd::open_local() {
125
return (m_fd = socket(rak::socket_address::pf_local, SOCK_STREAM, 0)) != -1;
130
if (::close(m_fd) && errno == EBADF)
131
throw internal_error("SocketFd::close() called on an invalid file descriptor");
135
SocketFd::bind(const rak::socket_address& sa) {
138
return !::bind(m_fd, sa.c_sockaddr(), sa.length());
142
SocketFd::bind(const rak::socket_address& sa, unsigned int length) {
145
return !::bind(m_fd, sa.c_sockaddr(), length);
149
SocketFd::connect(const rak::socket_address& sa) {
152
return !::connect(m_fd, sa.c_sockaddr(), sa.length()) || errno == EINPROGRESS;
156
SocketFd::listen(int size) {
159
return !::listen(m_fd, size);
163
SocketFd::accept(rak::socket_address* sa) {
165
socklen_t len = sizeof(rak::socket_address);
167
return SocketFd(::accept(m_fd, sa != NULL ? sa->c_sockaddr() : NULL, &len));
171
// SocketFd::get_read_queue_size() const {
174
// if (!is_valid() || ioctl(m_fd, SIOCINQ, &v) < 0)
175
// throw internal_error("SocketFd::get_read_queue_size() could not be performed");
181
// SocketFd::get_write_queue_size() const {
184
// if (!is_valid() || ioctl(m_fd, SIOCOUTQ, &v) < 0)
185
// throw internal_error("SocketFd::get_write_queue_size() could not be performed");