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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
#include "common.hh"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <netdb.h>
#include <unistd.h>
#include "config.hh"
#include "sender_client.hh"
#include "sockutils.hh"
namespace stuff {
namespace {
long WRITE_TIMEOUT = 5;
long CONNECT_TIMEOUT = 5;
class SenderClientImpl : public SenderClient {
public:
SenderClientImpl(std::shared_ptr<Error> error)
: error_(error) {
}
bool open(const Config* config) {
if (!config) return false;
sender_ = config->get("sender", "");
if (sender_.empty()) {
if (error_) error_->error("Config missing sender");
return false;
}
sender_bin_ = config->get("sender_bin", "");
return true;
}
void send(const std::string& channel, const std::string& message) override {
struct timeval target;
gettimeofday(&target, NULL);
target.tv_sec += WRITE_TIMEOUT;
send(channel, message, &target);
}
private:
void send(const std::string& channel, const std::string& message,
const struct timeval* target) {
if (!sock_) {
if (!setup()) return;
}
uint32_t size1 = channel.size();
uint32_t size2 = message.size();
size_t pos = 0, len = 8 + size1 + size2;
size1 = htonl(size1);
size2 = htonl(size2);
while (pos < len) {
ssize_t ret;
if (pos < 4) {
size_t const avail = 4 - pos;
ret = write(sock_.get(),
reinterpret_cast<char*>(&size1) + pos, avail);
if (ret > 0) {
pos += ret;
if (static_cast<size_t>(ret) == avail) continue;
}
} else if (pos < 4 + channel.size()) {
size_t const avail = 4 + channel.size() - pos;
ret = write(sock_.get(), channel.data() + pos - 4, avail);
if (ret > 0) {
pos += ret;
if (static_cast<size_t>(ret) == avail) continue;
}
} else if (pos < 8 + channel.size()) {
size_t const avail = 8 + channel.size() - pos;
ret = write(sock_.get(), reinterpret_cast<char*>(&size2)
+ pos - 4 - channel.size(), avail);
if (ret > 0) {
pos += ret;
if (static_cast<size_t>(ret) == avail) continue;
}
} else {
size_t const avail = len - pos;
ret = write(sock_.get(),
message.data() + pos - 8 - channel.size(),
avail);
if (ret > 0) {
pos += ret;
if (static_cast<size_t>(ret) == avail) continue;
}
}
if (ret < 0) {
if (errno == EINTR) continue;
if (errno != EAGAIN && errno != EWOULDBLOCK) {
sock_.reset();
return send(channel, message);
}
}
fd_set write_set;
FD_ZERO(&write_set);
FD_SET(sock_.get(), &write_set);
while (true) {
struct timeval timeout;
if (!calc_timeout(target, &timeout)) {
timeout.tv_sec = 0;
timeout.tv_usec = 0;
}
auto ret = select(sock_.get() + 1, nullptr, &write_set, nullptr,
&timeout);
if (ret < 0 && errno == EINTR) continue;
if (ret <= 0) {
// Timeout or error
sock_.reset();
return send(channel, message);
}
break;
}
}
}
bool connect_timeout(int sock, struct sockaddr* addr, socklen_t addrlen,
const struct timeval* target) {
if (!make_nonblocking(sock)) {
error_->error("Unable to make non-blocking socket", errno);
return false;
}
while (true) {
if (connect(sock, addr, addrlen) == 0) {
return true;
}
if (errno == EINTR) continue;
if (errno != EINPROGRESS) return false;
fd_set write_set;
FD_ZERO(&write_set);
FD_SET(sock, &write_set);
while (true) {
struct timeval timeout;
if (!calc_timeout(target, &timeout)) {
timeout.tv_sec = 0;
timeout.tv_usec = 0;
}
auto ret = select(sock + 1, nullptr, &write_set, nullptr,
&timeout);
if (ret < 0) {
if (errno == EINTR) continue;
return false;
}
if (ret == 0) {
errno = ETIMEDOUT;
return false;
}
int err;
socklen_t len = sizeof(int);
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &len)) {
return false;
}
if (err != 0) {
errno = err;
return false;
}
return true;
}
}
}
bool setup_start() {
if (sender_bin_.empty()) return false;
auto pid = fork();
if (pid < 0) {
if (error_) error_->error("Error forking", errno);
return false;
}
if (pid == 0) {
char* argv[2];
argv[0] = const_cast<char*>(sender_bin_.c_str());
argv[1] = nullptr;
_exit(execv(argv[0], argv));
} else {
int status;
auto ret = waitpid(pid, &status, 0);
if (ret == -1) {
if (error_) {
error_->error("Error waiting for sender bin", errno);
}
}
return true;
}
}
bool setup() {
struct timeval target;
gettimeofday(&target, NULL);
target.tv_sec += CONNECT_TIMEOUT;
auto pos = sender_.find(':');
if (pos != std::string::npos) {
// host:port
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
if (getaddrinfo(sender_.substr(0, pos).c_str(),
sender_.substr(pos + 1).c_str(), &hints, &res)) {
if (error_) error_->error("Error resolving: " + sender_);
return false;
}
for (auto ptr = res; ptr; ptr = ptr->ai_next) {
sock_.reset(socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol));
if (!sock_) continue;
if (!connect_timeout(sock_.get(), res->ai_addr, res->ai_addrlen,
&target)) {
sock_.reset();
continue;
}
break;
}
freeaddrinfo(res);
if (!sock_) {
if (errno == ECONNREFUSED && setup_start()) {
return setup();
}
if (error_) error_->error("Socket/Connect failed", errno);
return false;
}
} else {
// socket
sock_.reset(socket(PF_LOCAL, SOCK_STREAM, 0));
if (!sock_) {
if (error_) {
error_->error("Unable to create unix socket", errno);
}
return false;
}
struct sockaddr_un name;
name.sun_family = AF_LOCAL;
strncpy(name.sun_path, sender_.c_str(), sizeof(name.sun_path));
name.sun_path[sizeof(name.sun_path) - 1] = '\0';
if (!connect_timeout(sock_.get(),
reinterpret_cast<struct sockaddr*>(&name),
SUN_LEN(&name), &target)) {
if ((errno == ECONNREFUSED ||
errno == ENOENT) && setup_start()) {
return setup();
}
if (error_) error_->error("Connect failed", errno);
sock_.reset();
return false;
}
}
return true;
}
std::string sender_;
std::string sender_bin_;
std::shared_ptr<Error> error_;
sockguard sock_;
};
} // namespace
// static
std::unique_ptr<SenderClient> SenderClient::create(
const Config* config, std::shared_ptr<Error> error) {
std::unique_ptr<SenderClientImpl> ret(new SenderClientImpl(error));
if (!ret->open(config)) return nullptr;
return std::move(ret);
}
} // namespace stuff
|