-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathidexample1to1.c
More file actions
257 lines (225 loc) · 6.6 KB
/
idexample1to1.c
File metadata and controls
257 lines (225 loc) · 6.6 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* SocketAPI implementation for the sctplib.
* Copyright (C) 1999-2003 by Michael Tuexen
* Copyright (C) 2003-2026 by Thomas Dreibholz
*
* $Id$
*
* Realized in co-operation between
* - Siemens AG
* - University of Duisburg-Essen, Institute for Experimental Mathematics
* - Münster University of Applied Sciences, Burgsteinfurt
*
* Acknowledgement
* This work was partially funded by the Bundesministerium fuer Bildung und
* Forschung (BMBF) of the Federal Republic of Germany (Foerderkennzeichen 01AK045).
* The authors alone are responsible for the contents.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Contact: discussion@sctp.de
* thomas.dreibholz@gmail.com
* tuexen@fh-muenster.de
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/uio.h>
#include <ext_socket.h>
#include <string.h>
#define BUFLEN 100
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 = (union sctp_notification*)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=%u 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;
}
}
static void *
mysctp_recvmsg(int fd, struct msghdr *msg, void *buf, size_t *buflen, ssize_t *nrp, size_t cmsglen)
{
ssize_t nr = 0, nnr = 0;
struct iovec iov[1];
*nrp = 0;
iov->iov_base = buf;
iov->iov_len = *buflen;
msg->msg_iov = iov;
msg->msg_iovlen = 1;
for (;;) {
#ifndef MSG_XPG4_2
#define MSG_XPG4_2 0
#endif
msg->msg_flags = MSG_XPG4_2;
msg->msg_controllen = cmsglen;
nnr = ext_recvmsg(fd, msg, 0);
if (nnr <= 0) {
/* EOF or error */
*nrp = nr;
return (NULL);
}
nr += nnr;
if ((msg->msg_flags & MSG_EOR) != 0) {
*nrp = nr;
return (buf);
}
/* Realloc the buffer? */
if (*buflen == (size_t)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, int socketModeone_to_many)
{
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 = BUFLEN;
if (!(buf = (char*)malloc(BUFLEN))) {
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 = (char*)mysctp_recvmsg(fd, msg, buf, &buflen, &nr, cmsglen))) {
/* Intercept notifications here */
if (msg->msg_flags & MSG_NOTIFICATION) {
handle_event(buf);
continue;
}
iov->iov_base = buf;
iov->iov_len = nr;
msg->msg_iov = iov;
msg->msg_iovlen = 1;
printf("got %u bytes on stream %hu:\n", (unsigned int)nr, sri->sinfo_stream);
write(0, buf, nr);
/* Echo it back */
msg->msg_flags = MSG_XPG4_2;
if (ext_sendmsg(fd, msg, 0) < 0) {
perror("sendmsg");
exit(1);
}
}
if (nr < 0) {
perror("recvmsg");
}
if(socketModeone_to_many == 0)
ext_close(fd);
}
int main()
{
int lfd, cfd;
struct sctp_event_subscribe event;
struct sockaddr_in sin[1];
if ((lfd = ext_socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP)) == -1) {
perror("socket");
exit(1);
}
sin->sin_family = AF_INET;
sin->sin_port = htons(7);
sin->sin_addr.s_addr = INADDR_ANY;
if (ext_bind(lfd, (struct sockaddr *)sin, sizeof (*sin)) == -1) {
perror("bind");
exit(1);
}
if (ext_listen(lfd, 1) == -1) {
perror("listen");
exit(1);
}
/* Wait for new associations */
for (;;) {
if ((cfd = ext_accept(lfd, NULL, 0)) == -1) {
perror("accept");
exit(1);
}
/* Enable all events */
event.sctp_data_io_event = 1;
event.sctp_association_event = 1;
event.sctp_address_event = 1;
event.sctp_send_failure_event = 1;
event.sctp_peer_error_event = 1;
event.sctp_shutdown_event = 1;
event.sctp_partial_delivery_event = 1;
event.sctp_adaptation_layer_event = 1;
if (ext_setsockopt(cfd, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(event)) < 0) {
perror("setsockopt SCTP_EVENTS");
exit(1);
}
/* Echo back any and all data */
echo(cfd,0);
}
}