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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
|
#include <ares.h>
#ifndef _WIN32
#include <arpa/inet.h>
#include <sys/socket.h>
#else
#include <io.h>
#endif
#include <sys/types.h>
#ifdef _MSC_VER
#else
#include <unistd.h>
#endif
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include "Socket.h"
#include "Threads.h"
#include "Printer.h"
#include "Main.h"
#include "Async.h"
#include "Task.h"
#include "TaskMan.h"
#ifdef _WIN32
inline static SOCKET getSocket(int fd) { return _get_osfhandle(fd); }
#else
inline static int getSocket(int fd) { return fd; }
#endif
static Balau::String getErrorMessage() {
Balau::String msg;
#ifdef _WIN32
char * lpMsgBuf;
if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
WSAGetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL)) {
char * eol = strrchr(lpMsgBuf, '\n');
if (eol)
*eol = 0;
msg = lpMsgBuf;
} else {
msg = "FormatMessage failed";
}
LocalFree(lpMsgBuf);
#else
msg = strerror(errno);
#endif
return msg;
}
#if defined(_WIN32) && !defined(AI_V4MAPPED)
// mingw32 is retarded.
#define AI_NUMERICSERV 0x00000008L
#define AI_ALL 0x00000100L
#define AI_ADDRCONFIG 0x00000400L
#define AI_V4MAPPED 0x00000800L
#define AI_NON_AUTHORITATIVE 0x00004000L
#define AI_SECURE 0x00008000L
#define AI_RETURN_PREFERRED_NAMES 0x00010000L
#define AI_FQDN 0x00020000L
#define AI_FILESERVER 0x00040000L
// and winXP is stupid
static const char * inet_ntop4(const unsigned char * src, char * dst, socklen_t size) {
char out[INET_ADDRSTRLEN];
int len = sprintf(out, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
if (len < 0) {
return NULL;
} else if (len > size) {
errno = ENOSPC;
return NULL;
}
return strcpy(dst, out);
}
static const char * inet_ntop6(const unsigned char * src, char * dst, socklen_t size) {
char tmp[INET6_ADDRSTRLEN], *tp;
struct { int base, len; } best, cur;
unsigned int words[8];
int i;
memset(words, 0, sizeof(words));
for (i = 0; i < 16; i += 2)
words[i / 2] = (src[i] << 8) | src[i + 1];
best.base = -1;
cur.base = -1;
for (i = 0; i < 8; i++) {
if (words[i] == 0) {
if (cur.base == -1)
cur.base = i, cur.len = 1;
else
cur.len++;
} else {
if (cur.base != -1) {
if (best.base == -1 || cur.len > best.len)
best = cur;
cur.base = -1;
}
}
}
if (cur.base != -1) {
if (best.base == -1 || cur.len > best.len)
best = cur;
}
if (best.base != -1 && best.len < 2)
best.base = -1;
tp = tmp;
for (i = 0; i < 8; i++) {
if (best.base != -1 && i >= best.base && i < (best.base + best.len)) {
if (i == best.base)
*tp++ = ':';
continue;
}
if (i != 0)
*tp++ = ':';
if (i == 6 && best.base == 0 && (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp)))
return NULL;
tp += strlen (tp);
break;
}
int len = sprintf (tp, "%x", words[i]);
if (len < 0)
return NULL;
tp += len;
}
if (best.base != -1 && (best.base + best.len) == 8)
*tp++ = ':';
*tp++ = '\0';
if ((socklen_t) (tp - tmp) > size) {
errno = ENOSPC;
return NULL;
}
return strcpy(dst, tmp);
}
static const char * inet_ntop(int af, const void * src, char * dst, socklen_t size) {
switch (af) {
case AF_INET:
return inet_ntop4((const unsigned char *) src, dst, size);
case AF_INET6:
return inet_ntop6((const unsigned char *) src, dst, size);
default:
errno = WSAEAFNOSUPPORT;
WSASetLastError(errno);
return NULL;
}
}
#endif
Balau::Socket::Socket() throw (GeneralException) {
#ifdef _WIN32
int fd = _open_osfhandle(WSASocket(AF_INET6, SOCK_STREAM, 0, 0, 0, 0), 0);
#else
int fd = socket(AF_INET6, SOCK_STREAM, 0);
#endif
m_name = "Socket(nonconnected)";
RAssert(fd >= 0, "socket() returned %i", fd);
setFD(fd);
int on = 0;
int r = setsockopt(getSocket(fd), IPPROTO_IPV6, IPV6_V6ONLY, (char *)&on, sizeof(on));
EAssert(r == 0, "setsockopt returned %i", r);
memset(&m_localAddr, 0, sizeof(m_localAddr));
memset(&m_remoteAddr, 0, sizeof(m_remoteAddr));
Printer::elog(E_SOCKET, "Creating a socket at %p", this);
}
Balau::Socket::Socket(int fd) {
socklen_t len;
m_connected = true;
len = sizeof(m_localAddr);
getsockname(fd, (sockaddr *) &m_localAddr, &len);
len = sizeof(m_remoteAddr);
getpeername(fd, (sockaddr *) &m_remoteAddr, &len);
char prtLocal[INET6_ADDRSTRLEN], prtRemote[INET6_ADDRSTRLEN];
const char * rLocal, * rRemote;
len = sizeof(m_localAddr);
rLocal = inet_ntop(AF_INET6, &m_localAddr.sin6_addr, prtLocal, len);
rRemote = inet_ntop(AF_INET6, &m_remoteAddr.sin6_addr, prtRemote, len);
EAssert(rLocal, "inet_ntop returned NULL");
EAssert(rRemote, "inet_ntop returned NULL");
setFD(fd);
m_name.set("Socket(Connected - [%s]:%i <- [%s]:%i)", rLocal, ntohs(m_localAddr.sin6_port), rRemote, ntohs(m_remoteAddr.sin6_port));
Printer::elog(E_SOCKET, "Created a new socket from listener at %p; %s", this, m_name.to_charp());
}
void Balau::Socket::close() throw (GeneralException) {
if (isClosed())
return;
#ifdef _WIN32
_close(getFD());
#else
::close(getFD());
#endif
Printer::elog(E_SOCKET, "Closing socket at %p", this);
m_connected = false;
m_connecting = false;
m_listening = false;
internalClose();
}
bool Balau::Socket::canRead() { return true; }
bool Balau::Socket::canWrite() { return true; }
const char * Balau::Socket::getName() { return m_name.to_charp(); }
void Balau::Socket::resolve(const char * hostname) {
if (!m_resolving && !m_resolved) {
m_resolving = 2;
Task * t = Task::getCurrentTask();
IO<Socket> self(this);
auto callback = [self](int status, int timeouts, struct hostent * hostent, int family, void * destAddr, size_t sizeofDest, bool & failed) mutable {
if (status == ARES_SUCCESS) {
IAssert(hostent->h_addrtype == family, "We asked for socket family %i, but got %i instead", family, hostent->h_addrtype);
uint8_t * srcPtr = (uint8_t *) hostent->h_addr_list[0];
memcpy(destAddr, srcPtr, sizeofDest);
} else {
failed = true;
}
if (--self->m_resolving == 0) {
self->m_resolveEvent.doSignal();
self->m_resolved = true;
}
};
t->getTaskMan()->getHostByName(hostname, AF_INET, [callback, self](int status, int timeouts, struct hostent * hostent) mutable { callback(status, timeouts, hostent, AF_INET, &self->m_resolvedAddr4, sizeof(self->m_resolvedAddr4), self->m_resolve4Failed); });
t->getTaskMan()->getHostByName(hostname, AF_INET6, [callback, self](int status, int timeouts, struct hostent * hostent) mutable { callback(status, timeouts, hostent, AF_INET6, &self->m_resolvedAddr6, sizeof(self->m_resolvedAddr6), self->m_resolve6Failed); });
Task::operationYield(&m_resolveEvent, Task::INTERRUPTIBLE);
}
}
void Balau::Socket::initAddr(sockaddr_in6 & out) {
out.sin6_family = AF_INET6;
out.sin6_port = 0;
out.sin6_flowinfo = 0;
out.sin6_addr = in6addr_any;
}
void Balau::Socket::resolved(sockaddr_in6 & out) {
if (!m_resolve6Failed) {
memcpy(&out.sin6_addr, &m_resolvedAddr6, sizeof(struct in6_addr));
} else {
if (m_resolvedAddr4.s_addr == htonl(INADDR_LOOPBACK)) {
out.sin6_addr = in6addr_loopback;
} else {
memset(&out.sin6_addr, 0, sizeof(struct in6_addr));
// v4 mapped IPv6 address
out.sin6_addr.s6_addr[10] = 0xff;
out.sin6_addr.s6_addr[11] = 0xff;
memcpy(out.sin6_addr.s6_addr + 12, &m_resolvedAddr4, sizeof(struct in_addr));
}
}
m_resolved = false;
m_resolveEvent.reset();
}
bool Balau::Socket::setLocal(const char * hostname, int port) {
AAssert(m_localAddr.sin6_family == 0, "Can't call setLocal twice");
if (hostname && hostname[0])
resolve(hostname);
initAddr(m_localAddr);
if (m_resolving || m_resolved) {
AAssert(m_resolved && !m_resolving, "Please don't call setLocal after a EAgain without checking its resolution status first.");
if (m_resolve4Failed && m_resolve6Failed) {
Printer::elog(E_SOCKET, "Got a resolution error for host %s", hostname);
m_resolved = false;
return false;
}
resolved(m_localAddr);
}
if (port)
m_localAddr.sin6_port = htons(port);
#ifndef _WIN32
int enable = 1;
setsockopt(getFD(), SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable));
#endif
return bind(getSocket(getFD()), (struct sockaddr *) &m_localAddr, sizeof(m_localAddr)) == 0;
}
#if defined(_WIN32) && !defined(EISCONN)
#define EISCONN WSAEISCONN
#endif
bool Balau::Socket::connect(const char * hostname, int port) {
AAssert(!m_listening, "You can't call Socket::connect() on a listening socket");
AAssert(!m_connected, "You can't call Socket::connect() on an already connected socket");
AAssert(hostname, "You can't call Socket::connect() without a hostname");
AAssert(!isClosed(), "You can't call Socket::connect() on a closed socket");
if (!m_connecting && !m_resolving)
resolve(hostname);
if (!m_connecting && (m_resolving || m_resolved)) {
AAssert(m_resolved && !m_resolving, "Please don't call connect after a EAgain without checking its resolution status first.");
if (m_resolve4Failed && m_resolve6Failed) {
Printer::elog(E_SOCKET, "Got a resolution error for host %s", hostname);
m_resolved = false;
return false;
}
Printer::elog(E_SOCKET, "Got a resolution answer");
initAddr(m_remoteAddr);
resolved(m_remoteAddr);
m_remoteAddr.sin6_port = htons(port);
m_connecting = true;
m_resolved = false;
} else {
// if we end up there, it means our yield earlier threw an EAgain exception.
AAssert(gotR(), "Please don't call connect after a EAgain without checking its signal first.");
}
int spins = 0;
do {
Printer::elog(E_SOCKET, "Connecting now...");
int r;
int err;
if (spins == 0) {
r = ::connect(getSocket(getFD()), (sockaddr *)&m_remoteAddr, sizeof(m_remoteAddr));
#ifdef _WIN32
err = WSAGetLastError();
#else
err = errno;
#endif
} else {
socklen_t sLen = sizeof(err);
int g = getsockopt(getSocket(getFD()), SOL_SOCKET, SO_ERROR, (char *) &err, &sLen);
EAssert(g == 0, "getsockopt failed; g = %i", g);
r = err != 0 ? -1 : 0;
}
if ((r == 0) || ((r < 0) && (err == EISCONN))) {
m_connected = true;
m_connecting = false;
socklen_t len;
len = sizeof(m_localAddr);
getsockname(getSocket(getFD()), (sockaddr *)&m_localAddr, &len);
len = sizeof(m_remoteAddr);
getpeername(getSocket(getFD()), (sockaddr *)&m_remoteAddr, &len);
char prtLocal[INET6_ADDRSTRLEN], prtRemote[INET6_ADDRSTRLEN];
const char * rLocal, * rRemote;
len = sizeof(m_localAddr);
rLocal = inet_ntop(AF_INET6, &m_localAddr.sin6_addr, prtLocal, len);
rRemote = inet_ntop(AF_INET6, &m_remoteAddr.sin6_addr, prtRemote, len);
EAssert(rLocal, "inet_ntop returned NULL");
EAssert(rRemote, "inet_ntop returned NULL");
m_name.set("Socket(Connected - [%s]:%i -> [%s]:%i)", rLocal, ntohs(m_localAddr.sin6_port), rRemote, ntohs(m_remoteAddr.sin6_port));
Printer::elog(E_SOCKET, "Connected; %s", m_name.to_charp());
m_evtW->stop();
return true;
}
#ifdef _WIN32
#ifdef _MSC_VER
if (err != WSAEWOULDBLOCK) {
#else
if (err != WSAWOULDBLOCK) {
#endif
#else
if (err != EINPROGRESS) {
#endif
Printer::elog(E_SOCKET, "Connect() failed with the following error code: %i (%s)", err, strerror(err));
return false;
} else {
IAssert(spins == 0, "We shouldn't have spinned...");
}
Task::operationYield(m_evtW, Task::INTERRUPTIBLE);
// if we're still here, it means the parent task doesn't want to be thrown an exception
IAssert(gotW(), "We shouldn't have been awoken without getting our event signalled");
} while (spins++ < 2);
return false;
}
bool Balau::Socket::listen() {
AAssert(!m_listening, "You can't call Socket::listen() on an already listening socket");
AAssert(!m_connecting, "You can't call Socket::listen() on a connecting socket");
AAssert(!m_connected, "You can't call Socket::listen() on a connected socket");
AAssert(!isClosed(), "You can't call Socket::listen() on a closed socket");
if (::listen(getSocket(getFD()), 16) == 0) {
m_listening = true;
socklen_t len;
len = sizeof(m_localAddr);
getsockname(getSocket(getFD()), (sockaddr *)&m_localAddr, &len);
char prtLocal[INET6_ADDRSTRLEN];
const char * rLocal;
len = sizeof(m_localAddr);
rLocal = inet_ntop(AF_INET6, &m_localAddr.sin6_addr, prtLocal, len);
EAssert(rLocal, "inet_ntop() returned NULL");
m_name.set("Socket(Listener - [%s]:%i)", rLocal, ntohs(m_localAddr.sin6_port));
Printer::elog(E_SOCKET, "Socket %i started to listen: %s", getFD(), m_name.to_charp());
} else {
String msg = getErrorMessage();
Printer::elog(E_SOCKET, "listen() failed with error %i (%s)", errno, msg.to_charp());
}
return m_listening;
}
#ifdef _WIN32
#ifndef EWOULDBLOCK
#define EWOULDBLOCK EAGAIN
#endif
#endif
Balau::IO<Balau::Socket> Balau::Socket::accept() throw (GeneralException) {
AAssert(m_listening, "You can't call accept() on a non-listening socket");
AAssert(!isClosed(), "You can't call accept() on a closed socket");
while(true) {
sockaddr_in6 remoteAddr;
socklen_t len = sizeof(sockaddr_in6);
Printer::elog(E_SOCKET, "Socket %i (%s) is going to accept()", getFD(), m_name.to_charp());
#ifdef _WIN32
SOCKET s;
#else
int s;
#endif
s = ::accept(getSocket(getFD()), (sockaddr *)&remoteAddr, &len);
#ifndef _WIN32
if (s < 0) {
int err = errno;
#else
if (s == INVALID_SOCKET) {
int err = WSAGetLastError();
#ifdef _MSC_VER
if (err == WSAEWOULDBLOCK)
#else
if (err == WSAWOULDBLOCK)
#endif
err = EAGAIN;
#endif
if ((err == EAGAIN) || (err == EINTR) || (err == EWOULDBLOCK)) {
Task::operationYield(m_evtR, Task::INTERRUPTIBLE);
} else {
String msg = getErrorMessage();
m_evtR->stop();
throw GeneralException(String("Unexpected error accepting a connection: #") + errno + "(" + msg + ")");
}
} else {
Printer::elog(E_SOCKET, "Listener at %p got a new connection", this);
m_evtR->reset();
#ifdef _MSC_VER
return IO<Socket>(new Socket(_open_osfhandle(s, 0)));
#else
return IO<Socket>(new Socket(s));
#endif
}
}
}
ssize_t Balau::Socket::read(void * buf, size_t count) throw (GeneralException) {
AAssert(m_connected, "You can't call read() on a non-connected socket");
return Selectable::read(buf, count);
}
ssize_t Balau::Socket::write(const void * buf, size_t count) throw (GeneralException) {
AAssert(m_connected, "You can't call write() on a non-connected socket");
return Selectable::write(buf, count);
}
ssize_t Balau::Socket::recv(int sockfd, void *buf, size_t len, int flags) {
ssize_t r = ::recv(sockfd, (char *) buf, len, flags);
if (r < 0) {
#ifdef _WIN32
int err = WSAGetLastError();
if (err == WSAECONNABORTED)
return 0;
#endif
}
return r;
}
ssize_t Balau::Socket::send(int sockfd, const void *buf, size_t len, int flags) {
return ::send(sockfd, (const char *) buf, len, flags);
}
Balau::ListenerBase::ListenerBase(int port, const char * local, void * opaque) : m_listener(new Socket()), m_stop(false), m_local(local), m_port(port), m_opaque(opaque) {
m_name = String("Listener for something - Starting on ") + local + ":" + port;
Printer::elog(E_SOCKET, "Created a listener task at %p (%s)", this, m_name.to_charp());
}
const char * Balau::ListenerBase::getName() const {
return m_name.to_charp();
}
void Balau::ListenerBase::stop() {
Printer::elog(E_SOCKET, "Listener task at %p (%s) is asked to stop.", this, m_name.to_charp());
m_stop = true;
m_evt.trigger();
}
void Balau::ListenerBase::Do() {
try {
while (!m_stop) {
bool r;
IO<Socket> io;
switch (m_state) {
case 0:
waitFor(&m_evt);
m_state++;
case 1:
Printer::elog(E_SOCKET, "Listener task at %p (%s) is going to setLocal(%s, %i)", this, m_name.to_charp(), m_local.to_charp(), m_port);
r = m_listener->setLocal(m_local.to_charp(), m_port);
EAssert(r, "Couldn't set the local IP/port to listen to");
Printer::elog(E_SOCKET, "Listener task at %p (%s) starts listening", this, m_name.to_charp());
r = m_listener->listen();
EAssert(r, "Couldn't listen on the given IP/port");
setName();
m_started = true;
m_state++;
default:
Printer::elog(E_SOCKET, "Listener task at %p (%s) starts accepting", this, m_name.to_charp());
io = m_listener->accept();
Printer::elog(E_SOCKET, "Listener task at %p (%s) accepted a connection: %s", this, m_name.to_charp(), io->getName());
if (!m_stop)
factory(io, m_opaque);
}
}
}
catch (EAgain &) {
taskSwitch();
}
}
|