forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocketImpl.cpp
1074 lines (889 loc) · 23.1 KB
/
SocketImpl.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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// SocketImpl.cpp
//
// $Id: //poco/1.4/Net/src/SocketImpl.cpp#6 $
//
// Library: Net
// Package: Sockets
// Module: SocketImpl
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Net/SocketImpl.h"
#include "Poco/Net/NetException.h"
#include "Poco/Net/StreamSocketImpl.h"
#include "Poco/NumberFormatter.h"
#include "Poco/Timestamp.h"
#include <string.h> // FD_SET needs memset on some platforms, so we can't use <cstring>
#if defined(POCO_HAVE_FD_EPOLL)
#include <sys/epoll.h>
#elif defined(POCO_HAVE_FD_POLL)
#include <poll.h>
#endif
#if defined(sun) || defined(__sun) || defined(__sun__)
#include <unistd.h>
#include <stropts.h>
#endif
using Poco::IOException;
using Poco::TimeoutException;
using Poco::InvalidArgumentException;
using Poco::NumberFormatter;
using Poco::Timespan;
namespace Poco {
namespace Net {
SocketImpl::SocketImpl():
_sockfd(POCO_INVALID_SOCKET),
_blocking(true)
{
}
SocketImpl::SocketImpl(poco_socket_t sockfd):
_sockfd(sockfd),
_blocking(true)
{
}
SocketImpl::~SocketImpl()
{
close();
}
SocketImpl* SocketImpl::acceptConnection(SocketAddress& clientAddr)
{
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
char buffer[SocketAddress::MAX_ADDRESS_LENGTH];
struct sockaddr* pSA = reinterpret_cast<struct sockaddr*>(buffer);
poco_socklen_t saLen = sizeof(buffer);
poco_socket_t sd;
do
{
sd = ::accept(_sockfd, pSA, &saLen);
}
while (sd == POCO_INVALID_SOCKET && lastError() == POCO_EINTR);
if (sd != POCO_INVALID_SOCKET)
{
clientAddr = SocketAddress(pSA, saLen);
return new StreamSocketImpl(sd);
}
error(); // will throw
return 0;
}
void SocketImpl::connect(const SocketAddress& address)
{
if (_sockfd == POCO_INVALID_SOCKET)
{
init(address.af());
}
int rc;
do
{
#if defined(POCO_VXWORKS)
rc = ::connect(_sockfd, (sockaddr*) address.addr(), address.length());
#else
rc = ::connect(_sockfd, address.addr(), address.length());
#endif
}
while (rc != 0 && lastError() == POCO_EINTR);
if (rc != 0)
{
int err = lastError();
error(err, address.toString());
}
}
void SocketImpl::connect(const SocketAddress& address, const Poco::Timespan& timeout)
{
if (_sockfd == POCO_INVALID_SOCKET)
{
init(address.af());
}
setBlocking(false);
try
{
#if defined(POCO_VXWORKS)
int rc = ::connect(_sockfd, (sockaddr*) address.addr(), address.length());
#else
int rc = ::connect(_sockfd, address.addr(), address.length());
#endif
if (rc != 0)
{
int err = lastError();
if (err != POCO_EINPROGRESS && err != POCO_EWOULDBLOCK)
error(err, address.toString());
if (!poll(timeout, SELECT_READ | SELECT_WRITE | SELECT_ERROR))
throw Poco::TimeoutException("connect timed out", address.toString());
err = socketError();
if (err != 0) error(err);
}
}
catch (Poco::Exception&)
{
setBlocking(true);
throw;
}
setBlocking(true);
}
void SocketImpl::connectNB(const SocketAddress& address)
{
if (_sockfd == POCO_INVALID_SOCKET)
{
init(address.af());
}
setBlocking(false);
#if defined(POCO_VXWORKS)
int rc = ::connect(_sockfd, (sockaddr*) address.addr(), address.length());
#else
int rc = ::connect(_sockfd, address.addr(), address.length());
#endif
if (rc != 0)
{
int err = lastError();
if (err != POCO_EINPROGRESS && err != POCO_EWOULDBLOCK)
error(err, address.toString());
}
}
void SocketImpl::bind(const SocketAddress& address, bool reuseAddress)
{
if (_sockfd == POCO_INVALID_SOCKET)
{
init(address.af());
}
if (reuseAddress)
{
setReuseAddress(true);
setReusePort(true);
}
#if defined(POCO_VXWORKS)
int rc = ::bind(_sockfd, (sockaddr*) address.addr(), address.length());
#else
int rc = ::bind(_sockfd, address.addr(), address.length());
#endif
if (rc != 0) error(address.toString());
}
void SocketImpl::bind6(const SocketAddress& address, bool reuseAddress, bool ipV6Only)
{
#if defined(POCO_HAVE_IPv6)
if (address.family() != IPAddress::IPv6)
throw Poco::InvalidArgumentException("SocketAddress must be an IPv6 address");
if (_sockfd == POCO_INVALID_SOCKET)
{
init(address.af());
}
#ifdef IPV6_V6ONLY
setOption(IPPROTO_IPV6, IPV6_V6ONLY, ipV6Only ? 1 : 0);
#else
if (ipV6Only) throw Poco::NotImplementedException("IPV6_V6ONLY not defined.");
#endif
if (reuseAddress)
{
setReuseAddress(true);
setReusePort(true);
}
int rc = ::bind(_sockfd, address.addr(), address.length());
if (rc != 0) error(address.toString());
#else
throw Poco::NotImplementedException("No IPv6 support available");
#endif
}
void SocketImpl::listen(int backlog)
{
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
int rc = ::listen(_sockfd, backlog);
if (rc != 0) error();
}
void SocketImpl::close()
{
if (_sockfd != POCO_INVALID_SOCKET)
{
poco_closesocket(_sockfd);
_sockfd = POCO_INVALID_SOCKET;
}
}
void SocketImpl::shutdownReceive()
{
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
int rc = ::shutdown(_sockfd, 0);
if (rc != 0) error();
}
void SocketImpl::shutdownSend()
{
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
int rc = ::shutdown(_sockfd, 1);
if (rc != 0) error();
}
void SocketImpl::shutdown()
{
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
int rc = ::shutdown(_sockfd, 2);
if (rc != 0) error();
}
int SocketImpl::sendBytes(const void* buffer, int length, int flags)
{
#if defined(POCO_BROKEN_TIMEOUTS)
if (_sndTimeout.totalMicroseconds() != 0)
{
if (!poll(_sndTimeout, SELECT_WRITE))
throw TimeoutException();
}
#endif
int rc;
do
{
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
rc = ::send(_sockfd, reinterpret_cast<const char*>(buffer), length, flags);
}
while (_blocking && rc < 0 && lastError() == POCO_EINTR);
if (rc < 0) error();
return rc;
}
int SocketImpl::receiveBytes(void* buffer, int length, int flags)
{
#if defined(POCO_BROKEN_TIMEOUTS)
if (_recvTimeout.totalMicroseconds() != 0)
{
if (!poll(_recvTimeout, SELECT_READ))
throw TimeoutException();
}
#endif
int rc;
do
{
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
rc = ::recv(_sockfd, reinterpret_cast<char*>(buffer), length, flags);
}
while (_blocking && rc < 0 && lastError() == POCO_EINTR);
if (rc < 0)
{
int err = lastError();
if (err == POCO_EAGAIN && !_blocking)
;
else if (err == POCO_EAGAIN || err == POCO_ETIMEDOUT)
throw TimeoutException(err);
else
error(err);
}
return rc;
}
int SocketImpl::sendTo(const void* buffer, int length, const SocketAddress& address, int flags)
{
int rc;
do
{
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
#if defined(POCO_VXWORKS)
rc = ::sendto(_sockfd, (char*) buffer, length, flags, (sockaddr*) address.addr(), address.length());
#else
rc = ::sendto(_sockfd, reinterpret_cast<const char*>(buffer), length, flags, address.addr(), address.length());
#endif
}
while (_blocking && rc < 0 && lastError() == POCO_EINTR);
if (rc < 0) error();
return rc;
}
int SocketImpl::receiveFrom(void* buffer, int length, SocketAddress& address, int flags)
{
#if defined(POCO_BROKEN_TIMEOUTS)
if (_recvTimeout.totalMicroseconds() != 0)
{
if (!poll(_recvTimeout, SELECT_READ))
throw TimeoutException();
}
#endif
char abuffer[SocketAddress::MAX_ADDRESS_LENGTH];
struct sockaddr* pSA = reinterpret_cast<struct sockaddr*>(abuffer);
poco_socklen_t saLen = sizeof(abuffer);
int rc;
do
{
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
rc = ::recvfrom(_sockfd, reinterpret_cast<char*>(buffer), length, flags, pSA, &saLen);
}
while (_blocking && rc < 0 && lastError() == POCO_EINTR);
if (rc >= 0)
{
address = SocketAddress(pSA, saLen);
}
else
{
int err = lastError();
if (err == POCO_EAGAIN && !_blocking)
;
else if (err == POCO_EAGAIN || err == POCO_ETIMEDOUT)
throw TimeoutException(err);
else
error(err);
}
return rc;
}
void SocketImpl::sendUrgent(unsigned char data)
{
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
int rc = ::send(_sockfd, reinterpret_cast<const char*>(&data), sizeof(data), MSG_OOB);
if (rc < 0) error();
}
int SocketImpl::available()
{
int result;
ioctl(FIONREAD, result);
return result;
}
bool SocketImpl::secure() const
{
return false;
}
bool SocketImpl::poll(const Poco::Timespan& timeout, int mode)
{
poco_socket_t sockfd = _sockfd;
if (sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
#if defined(POCO_HAVE_FD_EPOLL)
int epollfd = epoll_create(1);
if (epollfd < 0)
{
char buf[1024];
strerror_r(errno, buf, sizeof(buf));
error(std::string("Can't create epoll queue: ") + buf);
}
struct epoll_event evin;
memset(&evin, 0, sizeof(evin));
if (mode & SELECT_READ)
evin.events |= EPOLLIN;
if (mode & SELECT_WRITE)
evin.events |= EPOLLOUT;
if (mode & SELECT_ERROR)
evin.events |= EPOLLERR;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &evin) < 0)
{
char buf[1024];
strerror_r(errno, buf, sizeof(buf));
::close(epollfd);
error(std::string("Can't insert socket to epoll queue: ") + buf);
}
Poco::Timespan remainingTime(timeout);
int rc;
do
{
struct epoll_event evout;
memset(&evout, 0, sizeof(evout));
Poco::Timestamp start;
rc = epoll_wait(epollfd, &evout, 1, remainingTime.totalMilliseconds());
if (rc < 0 && lastError() == POCO_EINTR)
{
Poco::Timestamp end;
Poco::Timespan waited = end - start;
if (waited < remainingTime)
remainingTime -= waited;
else
remainingTime = 0;
}
}
while (rc < 0 && lastError() == POCO_EINTR);
::close(epollfd);
if (rc < 0) error();
return rc > 0;
#elif defined(POCO_HAVE_FD_POLL)
pollfd pollBuf;
memset(&pollBuf, 0, sizeof(pollfd));
pollBuf.fd = _sockfd;
if (mode & SELECT_READ) pollBuf.events |= POLLIN;
if (mode & SELECT_WRITE) pollBuf.events |= POLLOUT;
Poco::Timespan remainingTime(timeout);
int rc;
do
{
Poco::Timestamp start;
rc = ::poll(&pollBuf, 1, remainingTime.totalMilliseconds());
if (rc < 0 && lastError() == POCO_EINTR)
{
Poco::Timestamp end;
Poco::Timespan waited = end - start;
if (waited < remainingTime)
remainingTime -= waited;
else
remainingTime = 0;
}
}
while (rc < 0 && lastError() == POCO_EINTR);
#else
fd_set fdRead;
fd_set fdWrite;
fd_set fdExcept;
FD_ZERO(&fdRead);
FD_ZERO(&fdWrite);
FD_ZERO(&fdExcept);
if (mode & SELECT_READ)
{
FD_SET(sockfd, &fdRead);
}
if (mode & SELECT_WRITE)
{
FD_SET(sockfd, &fdWrite);
}
if (mode & SELECT_ERROR)
{
FD_SET(sockfd, &fdExcept);
}
Poco::Timespan remainingTime(timeout);
int errorCode = POCO_ENOERR;
int rc;
do
{
struct timeval tv;
tv.tv_sec = (long) remainingTime.totalSeconds();
tv.tv_usec = (long) remainingTime.useconds();
Poco::Timestamp start;
rc = ::select(int(sockfd) + 1, &fdRead, &fdWrite, &fdExcept, &tv);
if (rc < 0 && (errorCode = lastError()) == POCO_EINTR)
{
Poco::Timestamp end;
Poco::Timespan waited = end - start;
if (waited < remainingTime)
remainingTime -= waited;
else
remainingTime = 0;
}
}
while (rc < 0 && errorCode == POCO_EINTR);
if (rc < 0) error(errorCode);
return rc > 0;
#endif // POCO_HAVE_FD_EPOLL
}
void SocketImpl::setSendBufferSize(int size)
{
setOption(SOL_SOCKET, SO_SNDBUF, size);
}
int SocketImpl::getSendBufferSize()
{
int result;
getOption(SOL_SOCKET, SO_SNDBUF, result);
return result;
}
void SocketImpl::setReceiveBufferSize(int size)
{
setOption(SOL_SOCKET, SO_RCVBUF, size);
}
int SocketImpl::getReceiveBufferSize()
{
int result;
getOption(SOL_SOCKET, SO_RCVBUF, result);
return result;
}
void SocketImpl::setSendTimeout(const Poco::Timespan& timeout)
{
#if defined(_WIN32) && !defined(POCO_BROKEN_TIMEOUTS)
int value = (int) timeout.totalMilliseconds();
setOption(SOL_SOCKET, SO_SNDTIMEO, value);
#elif defined(POCO_BROKEN_TIMEOUTS)
_sndTimeout = timeout;
#else
setOption(SOL_SOCKET, SO_SNDTIMEO, timeout);
#endif
}
Poco::Timespan SocketImpl::getSendTimeout()
{
Timespan result;
#if defined(_WIN32) && !defined(POCO_BROKEN_TIMEOUTS)
int value;
getOption(SOL_SOCKET, SO_SNDTIMEO, value);
result = Timespan::TimeDiff(value)*1000;
#elif defined(POCO_BROKEN_TIMEOUTS)
result = _sndTimeout;
#else
getOption(SOL_SOCKET, SO_SNDTIMEO, result);
#endif
return result;
}
void SocketImpl::setReceiveTimeout(const Poco::Timespan& timeout)
{
#ifndef POCO_BROKEN_TIMEOUTS
#if defined(_WIN32)
int value = (int) timeout.totalMilliseconds();
setOption(SOL_SOCKET, SO_RCVTIMEO, value);
#else
setOption(SOL_SOCKET, SO_RCVTIMEO, timeout);
#endif
#else
_recvTimeout = timeout;
#endif
}
Poco::Timespan SocketImpl::getReceiveTimeout()
{
Timespan result;
#if defined(_WIN32) && !defined(POCO_BROKEN_TIMEOUTS)
int value;
getOption(SOL_SOCKET, SO_RCVTIMEO, value);
result = Timespan::TimeDiff(value)*1000;
#elif defined(POCO_BROKEN_TIMEOUTS)
result = _recvTimeout;
#else
getOption(SOL_SOCKET, SO_RCVTIMEO, result);
#endif
return result;
}
SocketAddress SocketImpl::address()
{
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
char buffer[SocketAddress::MAX_ADDRESS_LENGTH];
struct sockaddr* pSA = reinterpret_cast<struct sockaddr*>(buffer);
poco_socklen_t saLen = sizeof(buffer);
int rc = ::getsockname(_sockfd, pSA, &saLen);
if (rc == 0)
return SocketAddress(pSA, saLen);
else
error();
return SocketAddress();
}
SocketAddress SocketImpl::peerAddress()
{
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
char buffer[SocketAddress::MAX_ADDRESS_LENGTH];
struct sockaddr* pSA = reinterpret_cast<struct sockaddr*>(buffer);
poco_socklen_t saLen = sizeof(buffer);
int rc = ::getpeername(_sockfd, pSA, &saLen);
if (rc == 0)
return SocketAddress(pSA, saLen);
else
error();
return SocketAddress();
}
void SocketImpl::setOption(int level, int option, int value)
{
setRawOption(level, option, &value, sizeof(value));
}
void SocketImpl::setOption(int level, int option, unsigned value)
{
setRawOption(level, option, &value, sizeof(value));
}
void SocketImpl::setOption(int level, int option, unsigned char value)
{
setRawOption(level, option, &value, sizeof(value));
}
void SocketImpl::setOption(int level, int option, const IPAddress& value)
{
setRawOption(level, option, value.addr(), value.length());
}
void SocketImpl::setOption(int level, int option, const Poco::Timespan& value)
{
struct timeval tv;
tv.tv_sec = (long) value.totalSeconds();
tv.tv_usec = (long) value.useconds();
setRawOption(level, option, &tv, sizeof(tv));
}
void SocketImpl::setRawOption(int level, int option, const void* value, poco_socklen_t length)
{
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
#if defined(POCO_VXWORKS)
int rc = ::setsockopt(_sockfd, level, option, (char*) value, length);
#else
int rc = ::setsockopt(_sockfd, level, option, reinterpret_cast<const char*>(value), length);
#endif
if (rc == -1) error();
}
void SocketImpl::getOption(int level, int option, int& value)
{
poco_socklen_t len = sizeof(value);
getRawOption(level, option, &value, len);
}
void SocketImpl::getOption(int level, int option, unsigned& value)
{
poco_socklen_t len = sizeof(value);
getRawOption(level, option, &value, len);
}
void SocketImpl::getOption(int level, int option, unsigned char& value)
{
poco_socklen_t len = sizeof(value);
getRawOption(level, option, &value, len);
}
void SocketImpl::getOption(int level, int option, Poco::Timespan& value)
{
struct timeval tv;
poco_socklen_t len = sizeof(tv);
getRawOption(level, option, &tv, len);
value.assign(tv.tv_sec, tv.tv_usec);
}
void SocketImpl::getOption(int level, int option, IPAddress& value)
{
char buffer[IPAddress::MAX_ADDRESS_LENGTH];
poco_socklen_t len = sizeof(buffer);
getRawOption(level, option, buffer, len);
value = IPAddress(buffer, len);
}
void SocketImpl::getRawOption(int level, int option, void* value, poco_socklen_t& length)
{
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
int rc = ::getsockopt(_sockfd, level, option, reinterpret_cast<char*>(value), &length);
if (rc == -1) error();
}
void SocketImpl::setLinger(bool on, int seconds)
{
struct linger l;
l.l_onoff = on ? 1 : 0;
l.l_linger = seconds;
setRawOption(SOL_SOCKET, SO_LINGER, &l, sizeof(l));
}
void SocketImpl::getLinger(bool& on, int& seconds)
{
struct linger l;
poco_socklen_t len = sizeof(l);
getRawOption(SOL_SOCKET, SO_LINGER, &l, len);
on = l.l_onoff != 0;
seconds = l.l_linger;
}
void SocketImpl::setNoDelay(bool flag)
{
int value = flag ? 1 : 0;
setOption(IPPROTO_TCP, TCP_NODELAY, value);
}
bool SocketImpl::getNoDelay()
{
int value(0);
getOption(IPPROTO_TCP, TCP_NODELAY, value);
return value != 0;
}
void SocketImpl::setKeepAlive(bool flag)
{
int value = flag ? 1 : 0;
setOption(SOL_SOCKET, SO_KEEPALIVE, value);
}
bool SocketImpl::getKeepAlive()
{
int value(0);
getOption(SOL_SOCKET, SO_KEEPALIVE, value);
return value != 0;
}
void SocketImpl::setReuseAddress(bool flag)
{
int value = flag ? 1 : 0;
setOption(SOL_SOCKET, SO_REUSEADDR, value);
}
bool SocketImpl::getReuseAddress()
{
int value(0);
getOption(SOL_SOCKET, SO_REUSEADDR, value);
return value != 0;
}
void SocketImpl::setReusePort(bool flag)
{
#ifdef SO_REUSEPORT
try
{
int value = flag ? 1 : 0;
setOption(SOL_SOCKET, SO_REUSEPORT, value);
}
catch (IOException&)
{
// ignore error, since not all implementations
// support SO_REUSEPORT, even if the macro
// is defined.
}
#endif
}
bool SocketImpl::getReusePort()
{
#ifdef SO_REUSEPORT
int value(0);
getOption(SOL_SOCKET, SO_REUSEPORT, value);
return value != 0;
#else
return false;
#endif
}
void SocketImpl::setOOBInline(bool flag)
{
int value = flag ? 1 : 0;
setOption(SOL_SOCKET, SO_OOBINLINE, value);
}
bool SocketImpl::getOOBInline()
{
int value(0);
getOption(SOL_SOCKET, SO_OOBINLINE, value);
return value != 0;
}
void SocketImpl::setBroadcast(bool flag)
{
int value = flag ? 1 : 0;
setOption(SOL_SOCKET, SO_BROADCAST, value);
}
bool SocketImpl::getBroadcast()
{
int value(0);
getOption(SOL_SOCKET, SO_BROADCAST, value);
return value != 0;
}
void SocketImpl::setBlocking(bool flag)
{
#if !defined(POCO_OS_FAMILY_UNIX)
int arg = flag ? 0 : 1;
ioctl(FIONBIO, arg);
#else
int arg = fcntl(F_GETFL);
long flags = arg & ~O_NONBLOCK;
if (!flag) flags |= O_NONBLOCK;
(void) fcntl(F_SETFL, flags);
#endif
_blocking = flag;
}
int SocketImpl::socketError()
{
int result(0);
getOption(SOL_SOCKET, SO_ERROR, result);
return result;
}
void SocketImpl::init(int af)
{
initSocket(af, SOCK_STREAM);
}
void SocketImpl::initSocket(int af, int type, int proto)
{
poco_assert (_sockfd == POCO_INVALID_SOCKET);
_sockfd = ::socket(af, type, proto);
if (_sockfd == POCO_INVALID_SOCKET)
error();
#if defined(__MACH__) && defined(__APPLE__) || defined(__FreeBSD__)
// SIGPIPE sends a signal that if unhandled (which is the default)
// will crash the process. This only happens on UNIX, and not Linux.
//
// In order to have POCO sockets behave the same across platforms, it is
// best to just ignore SIGPIPE all together.
setOption(SOL_SOCKET, SO_NOSIGPIPE, 1);
#endif
}
void SocketImpl::ioctl(poco_ioctl_request_t request, int& arg)
{
#if defined(_WIN32)
int rc = ioctlsocket(_sockfd, request, reinterpret_cast<u_long*>(&arg));
#elif defined(POCO_VXWORKS)
int rc = ::ioctl(_sockfd, request, (int) &arg);
#else
int rc = ::ioctl(_sockfd, request, &arg);
#endif
if (rc != 0) error();
}
void SocketImpl::ioctl(poco_ioctl_request_t request, void* arg)
{
#if defined(_WIN32)
int rc = ioctlsocket(_sockfd, request, reinterpret_cast<u_long*>(arg));
#elif defined(POCO_VXWORKS)
int rc = ::ioctl(_sockfd, request, (int) arg);
#else
int rc = ::ioctl(_sockfd, request, arg);
#endif
if (rc != 0) error();
}
#if defined(POCO_OS_FAMILY_UNIX)
int SocketImpl::fcntl(poco_fcntl_request_t request)
{
int rc = ::fcntl(_sockfd, request);
if (rc == -1) error();
return rc;
}
int SocketImpl::fcntl(poco_fcntl_request_t request, long arg)
{
int rc = ::fcntl(_sockfd, request, arg);
if (rc == -1) error();
return rc;
}
#endif
void SocketImpl::reset(poco_socket_t aSocket)
{
_sockfd = aSocket;
}
void SocketImpl::error()
{
int err = lastError();
std::string empty;
error(err, empty);
}
void SocketImpl::error(const std::string& arg)
{
error(lastError(), arg);
}
void SocketImpl::error(int code)
{
std::string arg;
error(code, arg);
}
void SocketImpl::error(int code, const std::string& arg)
{
switch (code)
{
case POCO_ENOERR: return;
case POCO_ESYSNOTREADY:
throw NetException("Net subsystem not ready", code);
case POCO_ENOTINIT:
throw NetException("Net subsystem not initialized", code);
case POCO_EINTR:
throw IOException("Interrupted", code);
case POCO_EACCES:
throw IOException("Permission denied", code);
case POCO_EFAULT: