-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSCTPSocket.cpp
More file actions
240 lines (198 loc) · 7.52 KB
/
Copy pathSCTPSocket.cpp
File metadata and controls
240 lines (198 loc) · 7.52 KB
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
#include "SCTPSocket.h"
#include "TempFailure.h"
#include <vector>
#include <netinet/in.h>
#include <poll.h>
using namespace NET;
SCTPSocket::SCTPSocket( uint16_t numOutStreams /* = 10 */,
uint16_t maxInStreams /* = 65535 */,
uint16_t maxAttempts /* = 4 */,
uint16_t maxInitTimeout /* = 0 */)
: InternetSocket( STREAM, IPPROTO_SCTP)
{
setInitValues( numOutStreams, maxInStreams, maxAttempts, maxInitTimeout);
}
SCTPSocket::SCTPSocket( Handle handle)
: InternetSocket( handle.release() )
{}
int SCTPSocket::bind( const std::vector<std::string>& localAddresses, unsigned short localPort /* = 0 */)
{
size_t size = localAddresses.size();
std::vector<sockaddr_in> dest(size);
for( size_t i = 0; i < size; ++i)
fillAddress( localAddresses[i], localPort, dest[i]);
int ret = sctp_bindx( m_socket, reinterpret_cast<sockaddr*>(dest.data()), static_cast<int>(size), SCTP_BINDX_ADD_ADDR);
if(ret < 0)
throw SocketException("Set of local address and port failed (sctp_bindx)");
return ret;
}
int SCTPSocket::connect( const std::vector<std::string>& foreignAddresses, unsigned short foreignPort /* = 0 */)
{
size_t size = foreignAddresses.size();
std::vector<sockaddr_in> dest(size);
for( size_t i = 0; i < size; ++i)
fillAddress( foreignAddresses[i], foreignPort, dest[i]);
// TODO maybe save connection id for later use
int ret = sctp_connectx( m_socket, reinterpret_cast<sockaddr*>(dest.data()), static_cast<int>(size), 0);
if(ret < 0)
throw SocketException("Connect failed (sctp_connectx)");
m_peerDisconnected = false;
return ret;
}
int SCTPSocket::state() const
{
struct sctp_status status;
socklen_t size = sizeof(status);
if( getsockopt( m_socket, IPPROTO_SCTP, SCTP_STATUS, &status, &size) < 0)
throw SocketException("SCTPSocket::state failed (getsockopt)");
return status.sstat_state;
}
int SCTPSocket::notAckedData() const
{
struct sctp_status status;
socklen_t size = sizeof(status);
if( getsockopt( m_socket, IPPROTO_SCTP, SCTP_STATUS, &status, &size) < 0)
throw SocketException("SCTPSocket::notAckedData failed (getsockopt)");
return status.sstat_unackdata;
}
int SCTPSocket::pendingData() const
{
struct sctp_status status;
socklen_t size = sizeof(status);
if( getsockopt( m_socket, IPPROTO_SCTP, SCTP_STATUS, &status, &size) < 0)
throw SocketException("SCTPSocket::pendingData failed (getsockopt)");
return status.sstat_penddata;
}
unsigned SCTPSocket::inStreams() const
{
struct sctp_status status;
socklen_t size = sizeof(status);
if( getsockopt( m_socket, IPPROTO_SCTP, SCTP_STATUS, &status, &size) < 0)
throw SocketException("SCTPSocket::inStreams failed (getsockopt)");
return status.sstat_instrms;
}
unsigned SCTPSocket::outStreams() const
{
struct sctp_status status;
socklen_t size = sizeof(status);
if( getsockopt( m_socket, IPPROTO_SCTP, SCTP_STATUS, &status, &size) < 0)
throw SocketException("SCTPSocket::outStreams failed (getsockopt)");
return status.sstat_outstrms;
}
unsigned SCTPSocket::fragmentationPoint() const
{
struct sctp_status status;
socklen_t size = sizeof(status);
if( getsockopt( m_socket, IPPROTO_SCTP, SCTP_STATUS, &status, &size) < 0)
throw SocketException("SCTPSocket::fragmentationPoint failed (getsockopt)");
return status.sstat_fragmentation_point;
}
std::string SCTPSocket::primaryAddress() const
{
return std::string(); // TODO
}
int SCTPSocket::send( const void* data, size_t length, uint16_t stream, unsigned ttl /* = 0 */, unsigned context /* = 0 */,
unsigned ppid /* = 0 */, abortFlag abort /* = KEEPALIVE */, switchAddressFlag switchAddr /* = KEEP_PRIMARY */)
{
int ret = sctp_sendmsg( m_socket, data, length, nullptr, 0, ppid, abort + switchAddr, stream, ttl, context);
if( ret < 0)
throw SocketException("SCTPSocket::send failed (sctp_sendmsg)");
else if (static_cast<size_t>(ret) < length)
throw SocketException("SCTP sent a fragemented packet!");
return ret;
}
int SCTPSocket::sendUnordered( const void* data, size_t length, uint16_t stream, unsigned ttl /* = 0 */, unsigned context /* = 0 */,
unsigned ppid /* = 0 */, abortFlag abort /* = KEEPALIVE */,
switchAddressFlag switchAddr /* = KEEP_PRIMARY */)
{
int ret = sctp_sendmsg( m_socket, data, length, nullptr, 0, ppid, abort + switchAddr + SCTP_UNORDERED, stream, ttl, context);
if( ret < 0)
throw SocketException("SCTPSocket::send failed (sctp_sendmsg)");
else if (static_cast<size_t>(ret) < length)
throw SocketException("SCTP sent a fragemented packet!");
return ret;
}
int SCTPSocket::receive( void* data, size_t maxLen, uint16_t& stream)
{
struct sctp_sndrcvinfo info;
int ret;
if( (ret = sctp_recvmsg( m_socket, data, maxLen, 0, 0, &info, 0)) < 0)
throw SocketException("SCTPSocket::receive failed (sctp_recvmsg)");
stream = info.sinfo_stream;
return ret;
}
int SCTPSocket::receive( void* data, size_t maxLen, uint16_t& stream, receiveFlag& flag)
{
struct sctp_sndrcvinfo info;
int ret;
if( (ret = sctp_recvmsg( m_socket, data, maxLen, 0, 0, &info, 0)) < 0)
throw SocketException("SCTPSocket::receive failed (sctp_recvmsg)");
stream = info.sinfo_stream;
flag = static_cast<receiveFlag>(info.sinfo_flags);
return ret;
}
int SCTPSocket::timedReceive( void* data, size_t maxLen, uint16_t& stream, int timeout)
{
struct pollfd poll;
poll.fd = m_socket;
poll.events = POLLIN | POLLPRI | POLLRDHUP;
int ret = TEMP_FAILURE_RETRY (::poll( &poll, 1, timeout));
if( ret == 0) return 0;
if( ret < 0) throw SocketException("SCTPSocket::timedReceive failed (poll)");
if( poll.revents & POLLRDHUP)
m_peerDisconnected = true;
if( poll.revents & POLLIN || poll.revents & POLLPRI)
return receive( data, maxLen, stream);
return 0;
}
int SCTPSocket::timedReceive( void* data, size_t maxLen, uint16_t& stream, receiveFlag& flag, int timeout)
{
struct pollfd poll;
poll.fd = m_socket;
poll.events = POLLIN | POLLPRI | POLLRDHUP;
int ret = TEMP_FAILURE_RETRY (::poll( &poll, 1, timeout));
if( ret == 0) return 0;
if( ret < 0) throw SocketException("SCTPSocket::timedReceive failed (poll)");
if( poll.revents & POLLRDHUP)
m_peerDisconnected = true;
if( poll.revents & POLLIN || poll.revents & POLLPRI)
return receive( data, maxLen, stream, flag);
return 0;
}
void SCTPSocket::listen( int backlog /* = 5 */)
{
int ret = ::listen( m_socket, backlog);
if( ret < 0)
throw SocketException("listen failed, most likely another socket is already listening on the same port");
}
SCTPSocket::Handle SCTPSocket::accept() const
{
int ret = ::accept( m_socket, 0, 0);
if( ret < 0)
throw SocketException("SCTPSocket::accept failed (accept)");
return Handle(ret);
}
SCTPSocket::Handle SCTPSocket::timedAccept( int timeout) const
{
struct pollfd poll;
poll.fd = m_socket;
poll.events = POLLIN;
int ret = TEMP_FAILURE_RETRY (::poll( &poll, 1, timeout));
if( ret == 0) return Handle();
if( ret < 0) throw SocketException("SCTPSocket::timedAccept failed (poll)");
ret = ::accept( m_socket, 0, 0);
if( ret < 0)
throw SocketException("SCTPSocket::timedAccept failed (accept)");
return Handle(ret);
}
void SCTPSocket::setInitValues( uint16_t numOutStreams, uint16_t maxInStreams, uint16_t maxAttempts, uint16_t maxInitTimeout)
{
struct sctp_initmsg init;
init.sinit_num_ostreams = numOutStreams;
init.sinit_max_instreams = maxInStreams;
init.sinit_max_attempts = maxAttempts;
init.sinit_max_init_timeo = maxInitTimeout;
int ret = setsockopt( m_socket, IPPROTO_SCTP, SCTP_INITMSG, &init, sizeof(init));
if( ret < 0)
throw SocketException("SCTPSocket construction failed (setsockopt)");
}