forked from dyninc/OpenBFDD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Socket.cpp
executable file
·807 lines (667 loc) · 18.5 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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
/**************************************************************
* Copyright (c) 2011-2017, Dynamic Network Services, Inc.
* Jake Montgomery ([email protected]) & Tom Daly ([email protected])
* Distributed under the FreeBSD License - see LICENSE
***************************************************************/
#include "common.h"
#include "Socket.h"
#include "utils.h"
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
/**
* Checks for 'expected', non-fatal errors that may occur in certain functions.
* For now, the same set is used for all checks.
*/
static inline bool isErrorExpected(int error)
{
return (error == ENOBUFS
|| error == EAGAIN
|| error == EINTR
|| error == ENOMEM
|| error == EWOULDBLOCK
);
}
void Socket::clear()
{
m_socket = -1;
m_address.clear();
m_owned = false;
m_error = 0;
}
Socket::Socket() :
m_logName()
, m_quiet(false)
, m_verboseErrorLogType(Log::Error)
, m_verboseExpectedLogType(Log::Debug)
{
clear();
}
Socket::Socket(int sock, Addr::Type family, bool owned /*false*/) :
m_logName()
, m_quiet(false)
, m_verboseErrorLogType(Log::Error)
, m_verboseExpectedLogType(Log::Debug)
{
clear();
m_socket = sock;
m_address.SetAny(family);
m_owned = owned;
}
Socket::Socket(const Socket &src) :
m_logName()
, m_quiet(false)
, m_verboseErrorLogType(Log::Error)
, m_verboseExpectedLogType(Log::Debug)
{
clear();
copy(src);
}
/**
* Copies the socket. This will NOT own the socket.
* Does not copy quiet, log or log name settings.
*
* @param src
*/
void Socket::copy(const Socket &src)
{
Close();
m_socket = src.m_socket;
m_address = src.m_address;
m_error = src.m_error;
}
Socket::~Socket()
{
Close();
}
Socket& Socket::operator = (const Socket &src)
{
Close();
copy(src);
return *this;
}
bool Socket::Open(Addr::Type family, int type, int protocol)
{
Close();
m_socket = ::socket(Addr::TypeToFamily(family), type, protocol);
if (empty())
return setErrorAndLog(errno, "Failed create socket. family %d, type %d proto %d", family, type, protocol);
m_address.SetAny(family);
m_owned = true;
return true;
}
bool Socket::OpenUDP(Addr::Type family)
{
return Open(family, SOCK_DGRAM, IPPROTO_UDP);
}
bool Socket::OpenTCP(Addr::Type family)
{
return Open(family, SOCK_STREAM, IPPROTO_TCP);
}
void Socket::Attach(int sock, Addr::Type family, bool owned /*false*/)
{
Close();
m_socket = sock;
m_address.SetAny(family);
m_owned = owned;
}
void Socket::Attach(int sock, const sockaddr *addr, socklen_t addrlen, bool owned /*false*/)
{
Close();
m_socket = sock;
m_address = SockAddr(addr, addrlen);
m_owned = owned;
}
void Socket::Transfer(Socket &src)
{
Close();
if (src.m_owned)
{
copy(src.Detach());
TakeOwnership();
}
else
copy(src);
}
void Socket::SetLogName(const char *str)
{
if (!str || !*str)
m_logName.clear();
else
m_logName = str;
}
bool Socket::SetQuiet(bool quiet)
{
bool old = m_quiet;
m_quiet = quiet;
return old;
}
Log::Type Socket::SetVerbosity(Log::Type type)
{
Log::Type old = m_verboseErrorLogType;
m_verboseErrorLogType = type;
return old;
}
Log::Type Socket::SetExpectedVerbosity(Log::Type type)
{
Log::Type old = m_verboseExpectedLogType;
m_verboseExpectedLogType = type;
return old;
}
void Socket::Close()
{
if (!empty() && m_owned)
::close(m_socket);
clear(); // must do full clear, because internally we rely on that behavior.
}
void Socket::AlwaysClose()
{
if (!empty())
::close(m_socket);
clear(); // must do full clear, because internally we rely on that behavior.
}
bool Socket::SetBlocking(bool block)
{
if (!ensureSocket())
return false;
int flags;
flags = ::fcntl(m_socket, F_GETFL);
if (flags == -1)
return setErrorAndLog(errno, "Failed to get socket flags to set to %sblocking", block ? "" : "non-");
if (block)
flags &= ~(int)O_NONBLOCK;
else
flags |= O_NONBLOCK;
if (-1 == ::fcntl(m_socket, F_SETFL, flags))
return setErrorAndLog(errno, "Failed to set socket to %sblocking", block ? "" : "non-");
return true;
}
/**
* Calls setsockopt.
*
* will set m_error
*
* @param level
* @param optname
* @param name
* @param value
*
* @return bool
*/
bool Socket::setIntSockOpt(int level, int optname, const char *name, int value)
{
if (!ensureSocket())
return false;
int on = value;
if (0 > ::setsockopt(m_socket, level, optname, &on, sizeof(on)))
return setErrorAndLog(errno, "Failed to set socket %s to %d", name, value);
return true;
}
/**
* Calls getsockopt.
*
* @param level
* @param optname
* @param name
* @param value [out] - set on success
*
* @return bool
*/
bool Socket::getIntSockOpt(int level, int optname, const char *name, int &out_value)
{
if (!ensureSocket())
return false;
socklen_t out_len = sizeof(int);
out_value = 0;
if (0 > ::getsockopt(m_socket, level, optname, &out_value, &out_len))
return setErrorAndLog(errno, "Failed to get socket %s", name);
return true;
}
bool Socket::SetReusePort(bool reuse)
{
return setIntSockOpt(SOL_SOCKET, SO_REUSEADDR, "SO_REUSEADDR", reuse ? 1 : 0);
}
bool Socket::SetSendBufferSize(int bufsize)
{
return setIntSockOpt(SOL_SOCKET, SO_SNDBUF, "SO_SNDBUF", bufsize);
}
bool Socket::GetSendBufferSize(int &out_bufsize)
{
return getIntSockOpt(SOL_SOCKET, SO_SNDBUF, "SO_SNDBUF", out_bufsize);
}
bool Socket::SetReceiveBufferSize(int bufsize)
{
return setIntSockOpt(SOL_SOCKET, SO_RCVBUF, "SO_RCVBUF", bufsize);
}
bool Socket::GetReceiveBufferSize(int &out_bufsize)
{
return getIntSockOpt(SOL_SOCKET, SO_RCVBUF, "SO_RCVBUF", out_bufsize);
}
bool Socket::SetUseTimestamp(bool timestamp)
{
return setIntSockOpt(SOL_SOCKET, SO_TIMESTAMP, "SO_TIMESTAMP", timestamp ? 1 : 0);
}
bool Socket::SetTTLOrHops(int hops)
{
if (!ensureSocket())
return false;
if (GetAddress().IsIPv4())
{
// Is there a 255 limit?
return setIntSockOpt(IPPROTO_IP, IP_TTL, "IP_TTL", hops);
}
else
return setIntSockOpt(IPPROTO_IPV6, IPV6_UNICAST_HOPS, "IPV6_UNICAST_HOPS", hops);
}
bool Socket::SetReceiveTTLOrHops(bool receive)
{
int val = receive ? 1 : 0;
if (!ensureSocket())
return false;
else if (GetAddress().IsIPv4())
{
// Is there a 255 limit?
return setIntSockOpt(IPPROTO_IP, IP_RECVTTL, "IP_RECVTTL", val);
}
else
return setIntSockOpt(IPPROTO_IPV6, IPV6_RECVHOPLIMIT, "IPV6_RECVHOPLIMIT", val);
}
bool Socket::SetReceiveDestinationAddress(bool receive)
{
int val = receive ? 1 : 0;
if (!ensureSocket())
return false;
else if (GetAddress().IsIPv4())
{
#ifdef IP_RECVDSTADDR
return setIntSockOpt(IPPROTO_IP, IP_RECVDSTADDR, "IP_RECVDSTADDR", val);
#elif defined IP_PKTINFO
return setIntSockOpt(IPPROTO_IP, IP_PKTINFO, "IP_PKTINFO", val);
#endif
return setErrorAndLog(ENOTSUP, "Platform does not support IP_RECVDSTADDR or IP_PKTINFO");
}
else
return setIntSockOpt(IPPROTO_IPV6, IPV6_RECVPKTINFO, "IPV6_RECVPKTINFO", val);
}
bool Socket::GetPendingError(int &ouError)
{
return getIntSockOpt(SOL_SOCKET, SO_ERROR, "SO_ERROR", ouError);
}
bool Socket::SetIPv6Only(bool ipv6Only)
{
if (!ensureSocket())
return false;
if (GetAddress().IsIPv6())
return setIntSockOpt(IPPROTO_IPV6, IPV6_V6ONLY, "IPV6_V6ONLY", ipv6Only ? 1 : 0);
else
return setErrorAndLog(ENOTSUP, "IPV6_V6ONLY not supported on IPv4 socket");
}
bool Socket::Bind(const SockAddr &address)
{
if (!ensureSocket())
return false;
m_address.clear();
if (::bind(m_socket, &address.GetSockAddr(), address.GetSize()) < 0)
return setErrorAndLog(errno, "Failed to bind socket to %s", address.ToString());
m_address = address;
return true;
}
bool Socket::Connect(const SockAddr &address)
{
if (!ensureSocket())
return false;
m_address.clear();
if (::connect(m_socket, &address.GetSockAddr(), address.GetSize()) < 0)
{
bool expected = (errno == EINPROGRESS);
setErrorAndLogAsExpected(expected, errno, "Failed to connect socket to %s", address.ToString());
if (!expected)
return false;
}
m_address = address;
return true;
}
bool Socket::Listen(int backlog)
{
if (!ensureSocket())
return false;
if (::listen(m_socket, backlog) < 0)
return setErrorAndLog(errno, "Failed to listen on socket");
return true;
}
bool Socket::SendTo(const void *buffer, size_t bufferLen, const SockAddr &toAddress, int flags /*0*/)
{
if (!ensureSocket())
return false;
if (::sendto(m_socket, buffer, bufferLen, flags, &toAddress.GetSockAddr(), toAddress.GetSize()) < 0)
return setErrorAndLogAsExpected(isErrorExpected(errno), errno, "Error sending packet using sendto to %s", toAddress.ToString());
return true;
}
bool Socket::SendToStream(const void **buffer, size_t *bufferLen, const SockAddr &toAddress, int flags /*0*/)
{
if (!ensureSocket())
return false;
ssize_t result = ::sendto(m_socket, *buffer, *bufferLen, flags, &toAddress.GetSockAddr(), toAddress.GetSize());
if (result < 0)
return setErrorAndLogAsExpected(isErrorExpected(errno), errno, "Error sending packet using sendto");
if (result == ssize_t(*bufferLen))
{
*bufferLen = 0;
*buffer = NULL;
}
else
{
if (size_t(result) > *bufferLen)
{
*bufferLen = 0;
*buffer = NULL;
return logError("Unexpected sendto() sent more data than was supplied");
}
*buffer = reinterpret_cast<const char *>(*buffer) + result;
*bufferLen -= result;
}
return true;
}
bool Socket::SendToStream(void **buffer, size_t *bufferLen, const SockAddr &toAddress, int flags /*0*/)
{
return SendToStream(const_cast<const void **>(buffer), bufferLen, toAddress, flags);
}
bool Socket::Send(const void *buffer, size_t bufferLen, int flags /*0*/)
{
if (!ensureSocket())
return false;
if (::send(m_socket, buffer, bufferLen, flags) < 0)
return setErrorAndLogAsExpected(isErrorExpected(errno), errno, "Error sending packet using send");
return true;
}
bool Socket::SendStream(const void **buffer, size_t *bufferLen, int flags /*0*/)
{
if (!ensureSocket())
return false;
ssize_t result = ::send(m_socket, *buffer, *bufferLen, flags);
if (result < 0)
return setErrorAndLogAsExpected(isErrorExpected(errno), errno, "Error sending packet using send");
if (result == ssize_t(*bufferLen))
{
*bufferLen = 0;
*buffer = NULL;
}
else
{
if (size_t(result) > *bufferLen)
{
*bufferLen = 0;
*buffer = NULL;
return logError("Unexpected send() sent more data than was supplied");
}
*buffer = reinterpret_cast<const char *>(*buffer) + result;
*bufferLen -= result;
}
return true;
}
bool Socket::SendStream(void **buffer, size_t *bufferLen, int flags /*0*/)
{
return SendStream(const_cast<const void **>(buffer), bufferLen, flags);
}
/**
* Like sendmsg(), for stream based protocols.
*
* This function can have one of three results:
*
* 1 - If the write completes successfully, and completely, then
* message->msg_iov is set to NULL and message->msg_iovlen is set to 0. true is
* returned.
* 2 - If the write completes partially, message->msg_iov and
* message->msg_iovlen will be modified to reflect the unwritten data location
* and length. In this case the function returns true.
* 3 - If there is some other failure, then message->msg_iov and
* message->msg_iovlen will remain unchanged, and false will be returned.
*
* @note Use GetLastError() for error code on failure. LastErrorWasSendFatal()
* may be helpful when deciding whether to try again.
*
* @param message [in/out] - The message containing the data to be written.
* May no be NULL. See description for value on return.
*
* @return bool - False on failure. A partial write is NOT failure.
*/
bool Socket::SendMsgStream(struct msghdr *message, int flags /*0*/)
{
if (!ensureSocket())
return false;
if (message->msg_iovlen == 0)
return true;
ssize_t result = ::sendmsg(m_socket, message, flags);
if (result < 0)
return setErrorAndLogAsExpected(isErrorExpected(errno), errno, "Error sending packet using sendmsg");
// Determine how much has been written (Note - Some non-POSIX compliant systems
// have message->msg_iovlen as size_t)
int whichIOVec;
for (whichIOVec = 0; whichIOVec < ssize_t(message->msg_iovlen); ++whichIOVec)
{
ssize_t thisLen = message->msg_iov[whichIOVec].iov_len;
if (thisLen != 0 && result == thisLen)
{
// Completed this one
++whichIOVec;
result -= thisLen;
break;
}
else if (result < thisLen)
{
// Partial write to here
message->msg_iov[whichIOVec].iov_len = thisLen - result;
message->msg_iov[whichIOVec].iov_base = reinterpret_cast<uint8_t *>(message->msg_iov[whichIOVec].iov_base) + result;
result -= thisLen;
break;
}
result -= thisLen;
}
// whichIOVec is one past the last complete write
if (result != 0)
{
message->msg_iovlen = 0;
message->msg_iov = NULL;
return setErrorAndLog(EINVAL, "Unexpected sendmsg() sent more data than was supplied");
}
if (whichIOVec >= ssize_t(message->msg_iovlen))
{
// Full write
message->msg_iovlen = 0;
message->msg_iov = NULL;
return true;
}
// partial write
message->msg_iovlen -= whichIOVec;
message->msg_iov += whichIOVec;
return true;
}
bool Socket::LastErrorWasSendFatal()
{
return !isErrorExpected(m_error);
}
bool Socket::Receive(void *buffer, size_t *inOutBufferLen, int flags /*0*/)
{
if (!LogVerify(inOutBufferLen))
return false;
size_t bufLen = *inOutBufferLen;
*inOutBufferLen = 0;
if (!LogVerify(buffer))
return false;
if (!ensureSocket())
return false;
ssize_t size = ::recv(m_socket, buffer, bufLen, flags);
if (size < 0)
return setErrorAndLogAsExpected(isErrorExpected(errno), errno, "Error reading packet using recv");
*inOutBufferLen = size_t(size);
return true;
}
bool Socket::ReceiveStream(void **buffer, size_t *bufferRemain, size_t *written /*NULL*/, int flags /*0*/)
{
if (!LogVerify(*bufferRemain))
return false;
if (!LogVerify(*buffer))
return false;
if (!ensureSocket())
return false;
size_t bufLen = *bufferRemain;
ssize_t size = ::recv(m_socket, *buffer, bufLen, flags);
if (size < 0)
return setErrorAndLogAsExpected(isErrorExpected(errno), errno, "Error reading packet using recv");
*buffer = reinterpret_cast<char *>(*buffer) + size;
*bufferRemain -= size;
if (written)
*written += size;
return true;
}
bool Socket::LastErrorWasReceiveFatal()
{
return !isErrorExpected(m_error);
}
bool Socket::Accept(Socket &outResult)
{
if (!ensureSocket())
return false;
outResult.Close();
sockaddr_storage faddr;
socklen_t fromlen = sizeof(faddr);
int sock = ::accept(m_socket, (sockaddr *)&faddr, &fromlen);
if (sock == -1)
return setErrorAndLog(errno, "Failed to accept on socket");
// It is always success, when if we can not read from addr
outResult.m_socket = sock;
outResult.m_address = SockAddr((sockaddr *)&faddr, fromlen);
outResult.m_owned = true;
if (!outResult.m_address.IsValid())
gLog.LogError("Unexpected invalid address from accept. Size %zu", size_t(fromlen));
return true;
}
size_t Socket::GetMaxControlSizeReceiveTTLOrHops()
{
// IP_TTL and IPV6_HOPLIMIT use int, and IP_RECVTTL may use byte.
return (CMSG_SPACE(int));
}
size_t Socket::GetMaxControlSizeReceiveDestinationAddress()
{
// We could assume that in6_pktinfo is going to be the largest, but the
// following actually models what we do.
size_t size = 0;
size = max(size, CMSG_SPACE(sizeof(in6_pktinfo)));
#ifdef IP_RECVDSTADDR
size = max(size, CMSG_SPACE(sizeof(in_addr)));
#endif
#ifdef IP_PKTINFO
size = max(size, CMSG_SPACE(sizeof(in6_pktinfo)));
#endif
return size;
}
/**
* Same as !empty(), but sets m_error and logs a message on failure.
*
* @return bool
*/
bool Socket::ensureSocket()
{
if (!empty())
return true;
return setErrorAndLog(EBADF, "Socket is invalid");
}
inline bool Socket::shouldLog(bool expected)
{
if (expected)
return !m_quiet && m_verboseExpectedLogType != Log::TypeCount && gLog.LogTypeEnabled(m_verboseExpectedLogType);
else
return !m_quiet && m_verboseErrorLogType != Log::TypeCount && gLog.LogTypeEnabled(m_verboseErrorLogType);
}
/**
* Helper function, will log a message based on m_error, and logs the message.
* Will prepend the socket name, if any. Will append the error string.
*
* Will always log, so call shouldLog() first.
*
* @return - false always!
*/
void Socket::doErrorLog(Log::Type type, const char *format, va_list args)
{
if (m_logName.empty() && m_error == 0)
{
// Special case to avoid extra copy
gLog.MessageVa(type, format, args);
}
const char *str = FormatMediumStrVa(format, args);
if (m_error != 0)
{
if (!m_logName.empty())
gLog.Optional(type, "%s : %s : (%d) %s", m_logName.c_str(), str, m_error, SystemErrorToString(m_error));
else
gLog.Optional(type, "%s : (%d) %s", str, m_error, SystemErrorToString(m_error));
}
else
{
LogAssert(!m_logName.empty()); // the empty case handled at the start of this fn.
gLog.Optional(type, "%s : %s", m_logName.c_str(), str);
}
}
/**
* Sets m_error to error, and logs the message, if settings allow.
* Will prepend the socket name, if any. Will append the error string.
* Will log 'expected' messages as prescribed by SetVerbose().
*
* @param isExpected;
* @param error
* @param format
*
* @return - false always!
*/
bool Socket::setErrorAndLogAsExpected(bool isExpected, int error, const char *format, ...)
{
m_error = error;
if (!shouldLog(isExpected))
return false;
va_list args;
va_start(args, format);
doErrorLog(isExpected ? m_verboseExpectedLogType : m_verboseErrorLogType, format, args);
va_end(args);
return false;
}
/**
* Sets m_error to error, and logs the message, if settings allow.
* Will prepend the socket name, if any. Will append the error string.
*
* @param error
* @param format
*
* @return - false always!
*/
bool Socket::setErrorAndLog(int error, const char *format, ...)
{
m_error = error;
if (!shouldLog(false))
return false;
va_list args;
va_start(args, format);
doErrorLog(m_verboseErrorLogType, format, args);
va_end(args);
return false;
}
/**
* Logs the message, if settings allow. Will prepend the socket name, if any.
* m_error will be cleared.
*
* @param error
* @param format
*
* @return - false always!
*/
bool Socket::logError(const char *format, ...)
{
m_error = 0;
if (!shouldLog(false))
return false;
va_list args;
va_start(args, format);
doErrorLog(m_verboseErrorLogType, format, args);
va_end(args);
return false;
}