-
Notifications
You must be signed in to change notification settings - Fork 0
/
sctp_server.c
258 lines (224 loc) · 6.79 KB
/
sctp_server.c
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
/*
* SCTP echo server
*
* reference: https://docs.oracle.com/cd/E19120-01/open.solaris/817-4415/sockets-27/index.html
*/
#include "header.h"
static void handle_event(void *buf);
static void echo(int fd);
static void* getmsg(int fd, struct msghdr *msg,
void *buf, size_t *buflen, ssize_t *nrp, size_t cmsglen);
int main(void) {
int sd, new_sd, buffer_len, frag_interleave, flags = 0;
char buffer[BUFFER_SIZE];
socklen_t sd_len;
struct iovec iov[1];
struct sockaddr_in addr[2];
struct sctp_initmsg initmsg;
struct sctp_sndrcvinfo *sndrcvinfo;
struct sctp_event_subscribe events;
struct msghdr msg[1];
struct cmsghdr *cmsg;
struct sctp_assoc_value assoc_intl, assoc_reconf;
char cbuf[sizeof(*cmsg) + sizeof(*sndrcvinfo)];
size_t msg_len = sizeof(*cmsg) + sizeof(*sndrcvinfo);
iov->iov_base = msg;
sd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
frag_interleave = 2;
handle_error(
setsockopt(sd, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE, &frag_interleave, sizeof(frag_interleave)) != 0,
"set frag interleave"
)
memset(&assoc_intl, 0, sizeof(struct sctp_assoc_value));
assoc_intl.assoc_id = 0;
assoc_intl.assoc_value = 1;
handle_error(
setsockopt(sd, IPPROTO_SCTP, SCTP_INTERLEAVING_SUPPORTED, &assoc_intl, sizeof(assoc_intl)) != 0,
"enable interleave"
)
sd_len = sizeof(struct sockaddr_in);
addr[1].sin_family = AF_INET;
addr[1].sin_port = htons(SERVER_PORT);
addr[1].sin_addr.s_addr = inet_addr(SERVER_ADDR);
addr[2].sin_family = AF_INET;
addr[2].sin_port = htons(SERVER_PORT);
addr[2].sin_addr.s_addr = inet_addr(SERVER_ADDR2);
handle_error(bind(sd, (struct sockaddr *)&addr[1], sizeof(struct sockaddr_in)) < 0, "bind1")
handle_error(sctp_bindx(sd, (struct sockaddr*)&addr[2], 1, SCTP_BINDX_ADD_ADDR) != 0, "sctp_bindx")
memset(&assoc_reconf, 0, sizeof(struct sctp_assoc_value));
assoc_reconf.assoc_id = 0;
assoc_reconf.assoc_value = (SCTP_ENABLE_RESET_STREAM_REQ | SCTP_ENABLE_RESET_ASSOC_REQ | SCTP_ENABLE_CHANGE_ASSOC_REQ);
handle_error(
setsockopt(sd, IPPROTO_SCTP, SCTP_ENABLE_STREAM_RESET, &assoc_reconf, sizeof(assoc_reconf)) != 0,
"enable reset"
)
memset(&initmsg, 0, sizeof(struct sctp_initmsg));
initmsg.sinit_max_attempts = 3;
initmsg.sinit_max_instreams = MAX_STREAM;
initmsg.sinit_num_ostreams = MAX_STREAM;
handle_error(setsockopt(sd, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof(initmsg)), "setsockopt")
memset(&events, 0, sizeof(struct sctp_event_subscribe));
events.sctp_data_io_event = 1;
events.sctp_association_event = 1;
events.sctp_send_failure_event = 1;
events.sctp_address_event = 1;
events.sctp_peer_error_event = 1;
events.sctp_shutdown_event = 1;
memset(msg, 0, sizeof (*msg));
msg->msg_control = cbuf;
msg->msg_controllen = msg_len;
msg->msg_flags = 0;
cmsg = (struct cmsghdr *)cbuf;
sndrcvinfo = (struct sctp_sndrcvinfo *)(cmsg + 1);
handle_error(listen(sd, 1) == -1, "listen")
while(1) {
handle_error((new_sd = accept(sd, (struct sockaddr*)&addr, &sd_len)) < 0, "accept", close(sd);)
handle_error(setsockopt(new_sd, IPPROTO_SCTP, SCTP_EVENTS, &events, sizeof(events)) < 0, "new_sd setsockopt")
echo(new_sd);
}
}
/*
* Receive a message from the network.
*/
static void* getmsg(int fd, struct msghdr *msg, void *buf, size_t *buflen,
ssize_t *nrp, size_t cmsglen)
{
ssize_t nr = 0;
struct iovec iov[1];
*nrp = 0;
iov->iov_base = buf;
msg->msg_iov = iov;
msg->msg_iovlen = 1;
/* Loop until a whole message is received. */
while(1) {
msg->msg_flags = 0;
msg->msg_iov->iov_len = *buflen;
msg->msg_controllen = cmsglen;
nr += recvmsg(fd, msg, 0);
if (nr <= 0) {
/* EOF or error */
*nrp = nr;
return NULL;
}
/* Whole message is received, return it. */
if (msg->msg_flags & MSG_EOR) {
*nrp = nr;
return buf;
}
/* Maybe we need a bigger buffer, do realloc(). */
if (*buflen == nr) {
buf = realloc(buf, *buflen * 2);
if (buf == 0) {
fprintf(stderr, "out of memory\n");
exit(1);
}
*buflen *= 2;
}
/* Set the next read offset */
iov->iov_base = (char *)buf + nr;
iov->iov_len = *buflen - nr;
}
}
static void echo(int fd)
{
ssize_t nr;
struct sctp_sndrcvinfo *sri;
struct msghdr msg[1];
struct cmsghdr *cmsg;
char cbuf[sizeof (*cmsg) + sizeof (*sri)];
char *buf;
size_t buflen;
struct iovec iov[1];
size_t cmsglen = sizeof (*cmsg) + sizeof (*sri);
/* Allocate the initial data buffer */
buflen = BUFFER_SIZE;
if ((buf = malloc(BUFFER_SIZE)) == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
/* Set up the msghdr structure for receiving */
memset(msg, 0, sizeof (*msg));
msg->msg_control = cbuf;
msg->msg_controllen = cmsglen;
msg->msg_flags = 0;
cmsg = (struct cmsghdr *)cbuf;
sri = (struct sctp_sndrcvinfo *)(cmsg + 1);
/* Wait for something to echo */
while ((buf = getmsg(fd, msg, buf, &buflen, &nr, cmsglen)) != NULL) {
/* Intercept notifications here */
if (msg->msg_flags & MSG_NOTIFICATION) {
handle_event(buf);
continue;
}
iov->iov_base = buf;
msg->msg_iov = iov;
msg->msg_iovlen = 1;
iov->iov_len = nr;
msg->msg_control = cbuf;
msg->msg_controllen = sizeof (*cmsg) + sizeof (*sri);
printf("\ngot %u bytes on stream %hu:\n", nr,
sri->sinfo_stream);
write(0, buf, nr);
/* Echo it back */
msg->msg_flags = 0;
if (sendmsg(fd, msg, 0) < 0) {
perror("sendmsg");
exit(1);
}
}
if (nr < 0) {
perror("recvmsg");
}
close(fd);
}
static void handle_event(void *buf)
{
struct sctp_assoc_change *sac;
struct sctp_send_failed *ssf;
struct sctp_paddr_change *spc;
struct sctp_remote_error *sre;
union sctp_notification *snp;
char addrbuf[INET6_ADDRSTRLEN];
const char *ap;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
snp = buf;
switch (snp->sn_header.sn_type) {
case SCTP_ASSOC_CHANGE:
sac = &snp->sn_assoc_change;
printf(">>> assoc_change: state=%hu, error=%hu, instr=%hu "
"outstr=%hu\n", sac->sac_state, sac->sac_error,
sac->sac_inbound_streams, sac->sac_outbound_streams);
break;
case SCTP_SEND_FAILED:
ssf = &snp->sn_send_failed;
printf(">>> sendfailed: len=%hu err=%d\n", ssf->ssf_length,
ssf->ssf_error);
break;
case SCTP_PEER_ADDR_CHANGE:
spc = &snp->sn_paddr_change;
if (spc->spc_aaddr.ss_family == AF_INET) {
sin = (struct sockaddr_in *)&spc->spc_aaddr;
ap = inet_ntop(AF_INET, &sin->sin_addr, addrbuf,
INET6_ADDRSTRLEN);
} else {
sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
ap = inet_ntop(AF_INET6, &sin6->sin6_addr, addrbuf,
INET6_ADDRSTRLEN);
}
printf(">>> intf_change: %s state=%d, error=%d\n", ap,
spc->spc_state, spc->spc_error);
break;
case SCTP_REMOTE_ERROR:
sre = &snp->sn_remote_error;
printf(">>> remote_error: err=%hu len=%hu\n",
ntohs(sre->sre_error), ntohs(sre->sre_length));
break;
case SCTP_SHUTDOWN_EVENT:
printf(">>> shutdown event\n");
break;
default:
printf("unknown type: %hu\n", snp->sn_header.sn_type);
break;
}
}