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
|
///File: comm-server.c
//Written by: Mark Rader
//Version: 1.0
//Date: Sun Aug 10 2008
//Copyright (c) 2008 by Mark Rader.
// 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 2
// of the License, or (at your option) any later version.
// $Log$
//
#ifndef lint
static char *rcsid = "$Header$";
#endif
#include "comm-server.h"
#include "view.h"
#include "comm-structs.h"
#include <string.h>
char *
pathStateName(unsigned int state)
{
switch (state) {
case SCTP_PATH_OK:
return "REACHABLE";
break;
case SCTP_PATH_UNREACHABLE:
return "UNREACHABLE";
break;
case SCTP_PATH_ADDED:
return "ADDED";
break;
case SCTP_PATH_REMOVED:
return "REMOVED";
break;
case SCTP_PATH_CONFIRMED:
return "CONFIRMED";
break;
case SCTP_PATH_UNCONFIRMED:
return "UNCONFIRMED";
break;
default:
return "UNKNOWN";
break;
}
}
void printUsage(void)
{
printf("Usage: echo_server [options]\n");
printf("options:\n");
printf("-i ignore OOTB packets\n");
printf("-s source address\n");
printf("-t time to live in ms\n");
printf("-v verbose mode\n");
printf("-V very verbose mode\n");
}
void getArgs(int argc, char **argv)
{
int i;
char *opt;
verbose = 1;
vverbose = 1;
for(i=1; i < argc ;i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'h':
printUsage();
exit(0);
case 's':
if (i+1 >= argc) {
printUsage();
exit(0);
}
opt = argv[++i];
if ((noOfLocalAddresses < MAXIMUM_NUMBER_OF_LOCAL_ADDRESSES) &&
(strlen(opt) < SCTP_MAX_IP_LEN )) {
strcpy((char *)localAddressList[noOfLocalAddresses], opt);
noOfLocalAddresses++;
};
break;
case 'i':
sendOOTBAborts = 0;
break;
case 't':
if (i+1 >= argc) {
printUsage();
exit(0);
}
opt = argv[++i];
timeToLive = atoi(opt);
break;
case 'v':
verbose = 1;
break;
case 'V':
verbose = 1;
vverbose = 1;
break;
default:
unknownCommand = 1;
break;
}
} else
unknownCommand = 1;
}
verbose = 1;
vverbose = 1;
}
void checkArgs(void)
{
int abortProgram;
int printUsageInfo;
abortProgram = 0;
printUsageInfo = 0;
if (noOfLocalAddresses == 0) {
#ifdef HAVE_IPV6
strcpy((char *)localAddressList[noOfLocalAddresses], "::0");
#else
strcpy((char *)localAddressList[noOfLocalAddresses], "0.0.0.0");
#endif
noOfLocalAddresses++;
}
if (unknownCommand ==1) {
printf("Error: Unkown options in command.\n");
printUsageInfo = 1;
}
if (printUsageInfo == 1)
printUsage();
if (abortProgram == 1)
exit(-1);
}
//int count = 0;
void dataArriveNotif(unsigned int assocID, unsigned short streamID, unsigned int len,
unsigned short streamSN,unsigned int TSN, unsigned int protoID,
unsigned int unordered, void* ulpDataPtr)
{
unsigned char chunk[MAXIMUM_PAYLOAD_LENGTH];
unsigned int length;
unsigned short ssn;
unsigned int tsn;
lin_message *buffer;
unsigned int junk_length;
int i;
unsigned char clean_chunk[MAXIMUM_PAYLOAD_LENGTH];
if (vverbose) {
fprintf(stdout, "%-8x: Data arrived (%u bytes on stream %u, %s)\n",
assocID, len, streamID, (unordered==SCTP_ORDERED_DELIVERY)?"ordered":"unordered");
fflush(stdout);
}
/* read it */
length = MAXIMUM_PAYLOAD_LENGTH;
lin_message *message;
sctp_receive(assocID, streamID, chunk, &length,&ssn, &tsn, SCTP_MSG_DEFAULT);
//sctp_receive(assocID, streamID, chunk, &length);
//count++;
//printf("%d, In Receive Mode\n", count);
//printf("%s\n", chunk);
//lin_message *test1;
//printf("%s\n",chunk);
if (streamID == 0){
message = (lin_message *)chunk;
printf("%d command message\n",message->commandID);
// printf("%d command bufferID\n",message->bufferID);
// printf("%d command view\n",message->viewID);
//printf("%d\n",strcspn(chunk,","));
// junk_length = strcspn(chunk,",")+1;
// memcpy(clean_chunk,chunk+junk_length,strlen(chunk)-junk_length);
//printf("%s\n", clean_chunk);
parse_data(assocID, streamID, protoID,message,length,ssn,tsn);
}
// printf("below parse data\n");
if (streamID > 0){
printf("%d stream messagen\n", streamID);
sctp_receive(assocID, streamID, chunk, &length,&ssn, &tsn, SCTP_MSG_DEFAULT);
key_message *kmessage;
kmessage = (key_message *)chunk;
printf("%d command message\n",kmessage->commandID);
}
//(unsigned char )message = chunk;
/* TODO: Call the dispatch routine, passing it
* assocID, chunk, length. assocID is trusted. */
/* Old: send it back (because it's an echo server) */
/* SCTP_send(assocID,
(unsigned short)min(streamID, ((struct ulp_data *) ulpDataPtr)->maximumStreamID),
chunk, length,
protoID,
SCTP_USE_PRIMARY, SCTP_NO_CONTEXT, timeToLive, unordered, SCTP_BUNDLING_DISABLED);
*/
}
void sendFailureNotif(unsigned int assocID,
unsigned char *unsent_data, unsigned int dataLength, unsigned int *context, void* ulpDataPtr)
{
if (verbose) {
fprintf(stdout, "%-8u: Send failure\n", assocID);
fflush(stdout);
}
}
void networkStatusChangeNotif(unsigned int assocID, short destAddrIndex, unsigned short newState, void* ulpDataPtr)
{
SCTP_AssociationStatus assocStatus;
SCTP_PathStatus pathStatus;
unsigned short pathID;
if (verbose) {
fprintf(stdout, "%-8x: Network status change: path %u is now %s\n",
assocID, destAddrIndex, pathStateName(newState));
fflush(stdout);
}
/* if the primary path has become inactive */
if ((newState == SCTP_PATH_UNREACHABLE) &&
(destAddrIndex == SCTP_getPrimary(assocID))) {
/* select a new one */ /* should we have a sctp_get_primary()? */
SCTP_getAssocStatus(assocID, &assocStatus);
for (pathID=0; pathID < assocStatus.numberOfAddresses; pathID++){
SCTP_getPathStatus(assocID, pathID, &pathStatus);
if (pathStatus.state == SCTP_PATH_OK)
break;
}
/* and use it */
if (pathID < assocStatus.numberOfAddresses) {
SCTP_setPrimary(assocID, pathID);
}
}
}
void* communicationUpNotif(unsigned int assocID, int status,
unsigned int noOfDestinations,
unsigned short noOfInStreams, unsigned short noOfOutStreams,
int associationSupportsPRSCTP, void* dummy)
{
struct ulp_data *ulpDataPtr;
if (verbose) {
fprintf(stdout, "%-8x: Communication up (%u In-Streams, %u Out-Streams)\n", assocID, noOfInStreams, noOfOutStreams);
fflush(stdout);
}
ulpDataPtr = malloc(sizeof(struct ulp_data));
ulpDataPtr->maximumStreamID = noOfOutStreams - 1;
systemID = assocID;
printf("We have a connection\nassociation = %d\n", assocID);
return((void *) ulpDataPtr);
}
void communicationLostNotif(unsigned int assocID, unsigned short status, void* ulpDataPtr)
{
unsigned char buffer[MAXIMUM_PAYLOAD_LENGTH];
unsigned int bufferLength;
unsigned short streamID, streamSN;
unsigned int protoID;
unsigned int tsn;
if (verbose) {
fprintf(stdout, "%-8x: Communication lost (status %u)\n", assocID, status);
fflush(stdout);
}
/* retrieve data */
bufferLength = sizeof(buffer);
while (SCTP_receiveUnsent(assocID, buffer, &bufferLength, &tsn,
&streamID, &streamSN, &protoID) >= 0){
/* do something with the retrieved data */
/* after that, reset bufferLength */
bufferLength = sizeof(buffer);
}
bufferLength = sizeof(buffer);
while (SCTP_receiveUnacked(assocID, buffer, &bufferLength, &tsn,
&streamID, &streamSN, &protoID) >= 0){
/* do something with the retrieved data */
/* after that, reset bufferLength */
bufferLength = sizeof(buffer);
}
/* free ULP data */
free((struct ulp_data *) ulpDataPtr);
/* delete the association */
SCTP_deleteAssociation(assocID);
}
void communicationErrorNotif(unsigned int assocID, unsigned short status, void* dummy)
{
if (verbose) {
fprintf(stdout, "%-8x: Communication error (status %u)\n", assocID, status);
fflush(stdout);
}
}
void restartNotif(unsigned int assocID, void* ulpDataPtr)
{
SCTP_AssociationStatus assocStatus;
if (verbose) {
fprintf(stdout, "%-8x: Restart\n", assocID);
fflush(stdout);
}
SCTP_getAssocStatus(assocID, &assocStatus);
/* update ULP data */
((struct ulp_data *) ulpDataPtr)->maximumStreamID = assocStatus.outStreams - 1;
}
void shutdownCompleteNotif(unsigned int assocID, void* ulpDataPtr)
{
if (verbose) {
fprintf(stdout, "%-8x: Shutdown complete\n", assocID);
fflush(stdout);
}
/* free ULP data */
free((struct ulp_data *) ulpDataPtr);
SCTP_deleteAssociation(assocID);
}
void shutdownReceivedNotif(unsigned int assocID, void* ulpDataPtr)
{
if (verbose) {
fprintf(stdout, "%-8x: Shutdown received\n", assocID);
fflush(stdout);
}
}
int Server_Start()
{
printf("in server loop\n");
SCTP_ulpCallbacks echoUlp;
/* SCTP_InstanceParameters instanceParameters; */
SCTP_LibraryParameters params;
/* initialize the echo_ulp variable */
echoUlp.dataArriveNotif = &dataArriveNotif;
echoUlp.sendFailureNotif = &sendFailureNotif;
echoUlp.networkStatusChangeNotif = &networkStatusChangeNotif;
echoUlp.communicationUpNotif = &communicationUpNotif;
echoUlp.communicationLostNotif = &communicationLostNotif;
echoUlp.communicationErrorNotif = &communicationErrorNotif;
echoUlp.restartNotif = &restartNotif;
echoUlp.shutdownCompleteNotif = &shutdownCompleteNotif;
echoUlp.peerShutdownReceivedNotif = &shutdownReceivedNotif;
/* handle all command line options */
int argc = 1;
char *argv[1];
getArgs(argc, argv);
checkArgs();
SCTP_initLibrary();
SCTP_getLibraryParameters(¶ms);
params.sendOotbAborts = sendOOTBAborts;
/* params.checksumAlgorithm = SCTP_CHECKSUM_ALGORITHM_ADLER32; */
params.checksumAlgorithm = SCTP_CHECKSUM_ALGORITHM_CRC32C;
SCTP_setLibraryParameters(¶ms);
/* set up the "server" */
/* set up the "server" */
int sctpInstance;
sctpInstance= SCTP_registerInstance(ECHO_PORT,
MAXIMUM_NUMBER_OF_IN_STREAMS, MAXIMUM_NUMBER_OF_OUT_STREAMS,
noOfLocalAddresses, localAddressList,
echoUlp);
/* run the event handler forever */
int select;
while (1) {
// printf("in loop");
select = SCTP_eventLoop();
switch (select){
case -1:
printf("error occured");
return 0;
break;
case 0:
break;
default:
printf("Received Result %d\n",select);
printf("DataArrived %d\n",echoUlp.dataArriveNotif);
printf("Instance %d\n",sctpInstance);
printf("association = %d\n",systemID);
// return 0;
break;
}
}
/* this will never be reached */
return 0;
}
|