-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.cpp
168 lines (142 loc) · 5.76 KB
/
socket.cpp
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
#include "socket.h"
#include "log.h"
#include "exception.h"
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>
#include <sstream>
#include <netdb.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/socket.h>
namespace {
class Address {
public:
Address(int type, const std::string& host, const std::string& port) {
struct addrinfo hint;
memset(&hint, 0, sizeof(hint));
hint.ai_family = AF_INET; // TODO: think about IPv6 AF_UNSPEC;
hint.ai_socktype = type;
hint.ai_flags = (AI_NUMERICHOST | AI_PASSIVE |
AI_NUMERICSERV | AI_ADDRCONFIG);
CHECK_CALL_ERROR(getaddrinfo(host == "*" ? NULL : host.c_str(),
port.c_str(), &hint, &info_),
"getaddrinfo(" << host << ":" << port << ")", gai_strerror);
}
~Address() {
freeaddrinfo(info_);
}
operator const struct addrinfo*() const {
return info_;
}
private:
struct addrinfo* info_;
};
std::string socket_name(const struct sockaddr* sa, socklen_t sz) {
char host[NI_MAXHOST];
char port[NI_MAXSERV];
CHECK_CALL_ERROR(getnameinfo(sa, sz,
host, sizeof(host), port, sizeof(port),
NI_NUMERICHOST | NI_NUMERICSERV),
"getnameinfo", gai_strerror);
return std::string(host) + ":" + std::string(port);
}
std::string peer_name(int fd) {
Socket::Target::value_type addr;
socklen_t len = sizeof(addr);
CHECK_CALL(getpeername(fd, (struct sockaddr*)&addr, &len), "getpeername");
return socket_name((const sockaddr*)&addr, len);
}
inline const std::string& socket_type(int type) {
static const std::string tcp = "TCP";
static const std::string udp = "UDP";
static const std::string unknown = "UNKNOWN";
if(type == SOCK_DGRAM) return udp;
if(type == SOCK_STREAM) return tcp;
return unknown;
}
void enable(const FD& socket, int level, int option) {
int enable = 1;
CHECK_CALL(setsockopt(socket.get(), level, option, &enable, sizeof(enable)),
"setsockopt(" << socket << ")");
}
}
Socket::Socket(int type, const std::string& host, unsigned short port)
{
DEBUG("Creating " << socket_type(type) << " socket: " << host << ":" << port);
Address address(type, host, boost::lexical_cast<std::string>(port));
const struct addrinfo* info = address;
socket_.reset(::socket(info->ai_family, info->ai_socktype, info->ai_protocol));
DEBUG("Created " << socket_type(type) << " socket(" << socket_ << "): "
<< socket_name(info->ai_addr, info->ai_addrlen));
enable(socket_, SOL_SOCKET, SO_REUSEADDR);
// enable(socket_, SOL_SOCKET, SO_KEEPALIVE); // server should never close connection
CHECK_CALL(bind(socket_.get(), info->ai_addr, info->ai_addrlen),
"bind(" << socket_ << ")");
if(info->ai_next) {
WARN(host << ":" << port << " has other address info (one of them): "
<< socket_name(info->ai_next->ai_addr, info->ai_next->ai_addrlen));
}
}
Socket::~Socket() {
if(socket_.get() != -1) DEBUG("Close socket(" << socket_ << ")");
}
std::string Socket::error() const {
int err;
socklen_t len = sizeof(err);
CHECK_CALL(getsockopt(socket(), SOL_SOCKET, SO_ERROR, &err, &len),
"getsockopt(" << socket_ << ")");
return err != 0 ? string_error(err) : "";
}
int Socket::read(char* buf, size_t size, Target::value_type* client) {
socklen_t len = client ? sizeof(Target::value_type) : 0;
int read = 0;
CHECK_CALL((read = io(boost::bind(&recvfrom, _1, _2, _3, 0,
(struct sockaddr*)client, &len), buf, size)),
"recvfrom(" << socket_ << ")");
REQUIRE((client == 0 && len == 0) ||
(client != 0 && len == sizeof(Target::value_type)), "strange address");
DEBUG("read(" << socket_ << ") from " <<
(client ? socket_name((const struct sockaddr*)client, len) :
peer_name(socket_.get())) << " " << read << " byte(s)");
return read;
}
int Socket::write(const char* buf, size_t size, const Target::value_type* client) {
socklen_t len = client ? sizeof(Target::value_type) : 0;
int written = 0;
CHECK_CALL((written = io(boost::bind(&sendto, _1, _2, _3, 0,
(const struct sockaddr*)client, len), buf, size)),
"sendto(" << socket_ << ")");
DEBUG("write(" << socket_ << ") to "
<< (client ? socket_name((const struct sockaddr*)client, len) :
peer_name(socket_.get())) << " " << written << " byte(s)");
return written;
}
Socket::Request UDPSocket::read(char* buf, size_t size) {
Socket::Target::value_type client;
int read = Socket::read(buf, size, &client);
return Socket::Request(read, client);
}
int UDPSocket::write(const char* buf, size_t size, const Target& client) {
REQUIRE(client, "write(" << socket() << "): target not specified");
return Socket::write(buf, size, &(*client));
}
TCPSocket::TCPSocket(const std::string& host, unsigned short port) :
Socket(SOCK_STREAM, host, port) {
CHECK_CALL(listen(socket(), 1024), "listen(" << socket() << ")");
}
TCPSocket TCPSocket::accept() const{
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
TCPSocket peer(::accept(socket(), (struct sockaddr*)&addr, &addrlen));
REQUIRE(addrlen == sizeof(addr), "strange size");
DEBUG("Accepted(" << socket() << "): "
<< socket_name((const struct sockaddr*)&addr, addrlen)
<< " as fd(" << peer.socket() << ")");
return peer;
}
Socket::Request TCPSocket::read(char* buf, size_t size) {
return Socket::Request(Socket::read(buf, size, 0), boost::none);
}
int TCPSocket::write(const char* buf, size_t size, const Target&) {
return Socket::write(buf, size, 0);
}