-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsctpterminal.cc
More file actions
565 lines (497 loc) · 18 KB
/
sctpterminal.cc
File metadata and controls
565 lines (497 loc) · 18 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
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
/*
* $Id$
*
* SocketAPI implementation for the sctplib.
* Copyright (C) 1999-2026 by Thomas Dreibholz
*
* 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
*
* Purpose: SCTP Terminal
*
*/
/*
Important: The FreeBSD macros cause Socket methods to be translated
to __pthread_* names. This must be prevented!
*/
#if (SYSTEM == OS_FreeBSD)
#define _POSIX_THREAD_SYSCALL_SOFT 0
#endif
#include "tdsystem.h"
#include "thread.h"
#include "tdsocket.h"
#include "tdmessage.h"
#include "randomizer.h"
#include "breakdetector.h"
#include "ext_socket.h"
#include "ansicolor.h"
#include "sctpinfoprinter.h"
#include <signal.h>
#define NOTIFICATION_COLOR 10
#define CONTROL_COLOR 12
#define ERROR_COLOR 9
#define INFO_COLOR 11
#define SCTP_MAXADDRESSES 20
class EchoThread : public Thread
{
public:
EchoThread(Socket* socket,
const cardinal incoming);
~EchoThread();
private:
void run();
Socket* EchoSocket;
cardinal Incoming;
};
// ###### Constructor #######################################################
EchoThread::EchoThread(Socket* socket,
const cardinal incoming)
: Thread("EchoThread")
{
EchoSocket = socket;
Incoming = incoming;
start();
}
// ###### Destructor ########################################################
EchoThread::~EchoThread()
{
}
// ###### Thread main loop ##################################################
void EchoThread::run()
{
char buffer[16384];
for(;;) {
SocketMessage<sizeof(sctp_sndrcvinfo)> message;
message.setBuffer((char*)&buffer,sizeof(buffer));
// ====== Receive string from association =============================
const ssize_t dataLength = EchoSocket->receiveMsg(&message.Header,0);
if(dataLength <= 0) {
sendBreak(true);
return;
}
// ====== Get control data ============================================
printControl(&message.Header);
unsigned short streamID = 0;
unsigned int protoID = 0;
cmsghdr* cmsg = message.getFirstHeader();
if(cmsg != NULL) {
while(cmsg != NULL) {
if((cmsg->cmsg_level == IPPROTO_SCTP) &&
(cmsg->cmsg_type == SCTP_SNDRCV)) {
sctp_sndrcvinfo* info = (sctp_sndrcvinfo*)CData(cmsg);
streamID = info->sinfo_stream;
protoID = ntohl(info->sinfo_ppid);
}
cmsg = message.getNextHeader(cmsg);
}
}
// ====== Print data ==================================================
if(message.Header.msg_flags & MSG_NOTIFICATION) {
sctp_notification* notification = (sctp_notification*)&buffer;
printNotification(notification);
}
else {
if(dataLength > 0) {
if(ColorMode) {
std::cout << "\x1b[" << getANSIColor(streamID + 1) << "m";
}
if(Incoming > 1) {
char str[128];
if(protoID == 0) {
snprintf((char*)&str,sizeof(str),"#%d: ",streamID);
}
else {
snprintf((char*)&str,sizeof(str),"#%d/$%08x: ",
streamID,protoID);
}
std::cout << str;
}
int i;
buffer[dataLength] = 0x00;
for(i = dataLength - 1;i >= 0;i--) {
if(buffer[i] < ' ') {
buffer[i] = 0x00;
}
else {
break;
}
}
for( ;i >= 0;i--) {
if(buffer[i] < ' ') {
buffer[i] = '.';
}
}
std::cout << buffer << std::endl;
if(ColorMode) {
std::cout << "\x1b[" << getANSIColor(0) << "m";
}
std::cout.flush();
}
}
}
}
class CopyThread : public Thread
{
public:
CopyThread(Socket* socket,
const cardinal outgoing,
const cardinal unreliable);
~CopyThread();
private:
void run();
Socket* CopySocket;
cardinal Outgoing;
cardinal Unreliable;
};
// ###### Constructor #######################################################
CopyThread::CopyThread(Socket* socket,
const cardinal outgoing,
const cardinal unreliable)
: Thread("CopyThread", Thread::TF_CancelAsynchronous)
{
CopySocket = socket;
Outgoing = outgoing;
Unreliable = unreliable;
start();
}
// ###### Destructor ########################################################
CopyThread::~CopyThread()
{
}
// ###### Thread main loop ##################################################
void CopyThread::run()
{
unsigned int stream = 0;
unsigned int ppid = 0;
for(;;) {
// ====== Read data from cin ==========================================
Thread::testCancel();
char str[8192];
// For some reason, cin.getline() causes problems with Ctrl-C
// handling. Therefore, we use fgets() and feof() instead.
if(fgets((char*)&str, sizeof(str), stdin) == NULL) {
CopySocket->shutdown(2);
sendBreak(true);
return;
}
if(feof(stdin)) {
CopySocket->shutdown(2);
sendBreak(true);
return;
}
// ====== Check for new stream ID and protocol ID =====================
unsigned int position = 0;
bool update = true;
if(sscanf(str,"#%d/$%x:%n",&stream,&ppid,&position) < 2) {
if(sscanf(str,"#%d/%x:%n",&stream,&ppid,&position) < 2) {
if(sscanf(str,"#%d:%n",&stream,&position) < 1) {
position = 0;
update = false;
}
}
}
// ====== Check parameters ============================================
char* toSend = (char*)&str[position];
if(stream >= Outgoing) {
stream = 0;
if(ColorMode) {
std::cerr << "\x1b[" << getANSIColor(ERROR_COLOR) << "m\x07";
}
std::cerr << "*** Bad stream setting! Using #0. ***" << std::endl;
if(ColorMode) {
std::cerr << "\x1b[" << getANSIColor(0) << "m";
}
std::cerr.flush();
}
if(update == true) {
if(ColorMode) {
std::cout << "\x1b[" << getANSIColor(INFO_COLOR) << "m";
}
char str2[128];
snprintf((char*)&str2,sizeof(str2),"Via stream #%d, protocol $%08x...",
stream,ppid);
std::cout << str2;
if(ColorMode) {
std::cout << "\x1b[" << getANSIColor(0) << "m";
}
std::cout.flush();
}
// ====== Send message ================================================
cardinal length = strlen(toSend);
if(length > 0) {
toSend[length] = '\n';
toSend[length++] = 0x00;
SocketMessage<sizeof(sctp_sndrcvinfo)> message;
message.setBuffer(toSend,length);
sctp_sndrcvinfo* info = (sctp_sndrcvinfo*)message.addHeader(sizeof(sctp_sndrcvinfo),IPPROTO_SCTP,SCTP_SNDRCV);
info->sinfo_assoc_id = 0;
info->sinfo_stream = (unsigned short)stream;
info->sinfo_flags = MSG_PR_SCTP_TTL;
info->sinfo_ppid = htonl(ppid);
if((Unreliable > 0) && (info->sinfo_stream <= Unreliable - 1)) {
info->sinfo_flags = MSG_PR_SCTP_TTL;
info->sinfo_timetolive = 0;
}
else {
info->sinfo_flags = MSG_PR_SCTP_TTL;
info->sinfo_timetolive = 0;
}
const ssize_t result = CopySocket->sendMsg(&message.Header,0);
if(result < 0) {
if(ColorMode) {
std::cerr << "\x1b[" << getANSIColor(ERROR_COLOR) << "m\x07";
}
std::cerr << "*** sendMsg() error #" << result << "! ***" << std::endl;
if(ColorMode) {
std::cerr << "\x1b[" << getANSIColor(0) << "m";
}
}
}
}
}
// ###### Main program ######################################################
int main(int argc, char** argv)
{
bool optForceIPv4 = false;
cardinal instreams = 128;
cardinal outstreams = 128;
cardinal localAddresses = 0;
cardinal remoteAddresses = 0;
cardinal unreliable = 0;
SocketAddress** localAddressArray = SocketAddress::newAddressList(SCTP_MAXADDRESSES);
SocketAddress** remoteAddressArray = SocketAddress::newAddressList(SCTP_MAXADDRESSES);
if((localAddressArray == NULL) || (remoteAddressArray == NULL)) {
std::cerr << "ERROR: Out of memory!" << std::endl;
exit(1);
}
PrintControl = false;
PrintNotifications = false;
if(!sctp_isavailable()) {
#ifdef HAVE_KERNEL_SCTP
std::cerr << "ERROR: Kernel-based SCTP is not available!" << std::endl;
#else
if(getuid() != 0) {
std::cerr << "ERROR: You need root permissions to use the SCTP Library!" << std::endl;
}
else {
std::cerr << "ERROR: SCTP is not available!" << std::endl;
}
#endif
exit(1);
}
// ====== Get arguments ==================================================
if(argc < 2) {
std::cerr << "Usage: " << argv[0] << " "
<< "[Remote address 1] {{Remote address 2} ...} {-force-ipv4|-use-ipv6} {-local=address1} ... {-local=addressN} {-in=instreams} {-out=outstreams} {-nocolor} {-color} {-control|-nocontrol} {-notif|-nonotif}"
<< std::endl;
exit(1);
}
for(unsigned int i = 1;i < (cardinal)argc;i++) {
if(!(strcasecmp(argv[i],"-force-ipv4"))) {
optForceIPv4 = true;
}
else if(!(strcasecmp(argv[i],"-use-ipv6"))) {
optForceIPv4 = false;
}
else if(!(strncasecmp(argv[i],"-in=",4))) {
instreams = atol(&argv[i][4]);
if(instreams < 1) {
instreams = 1;
}
else if(instreams > 65535) {
instreams = 65535;
}
}
else if(!(strncasecmp(argv[i],"-out=",5))) {
outstreams = atol(&argv[i][5]);
if(outstreams < 1) {
outstreams = 1;
}
else if(outstreams > 65535) {
outstreams = 65535;
}
}
else if(!(strncasecmp(argv[i],"-unreliable=",12))) {
unreliable = atol(&argv[i][12]);
}
else if(!(strcasecmp(argv[i],"-color"))) {
ColorMode = true;
}
else if(!(strcasecmp(argv[i],"-nocolor"))) {
ColorMode = false;
}
else if(!(strcasecmp(argv[i],"-control"))) {
PrintControl = true;
}
else if(!(strcasecmp(argv[i],"-notif"))) {
PrintNotifications = true;
}
else if(!(strcasecmp(argv[i],"-nocontrol"))) {
PrintControl = false;
}
else if(!(strcasecmp(argv[i],"-nonotif"))) {
PrintNotifications = false;
}
else if(!(strncasecmp(argv[i],"-local=",7))) {
if(localAddresses < SCTP_MAXADDRESSES) {
localAddressArray[localAddresses] =
SocketAddress::createSocketAddress(0,
&argv[i][7]);
if(localAddressArray[localAddresses] == NULL) {
std::cerr << "ERROR: Argument \"" << argv[i] << "\" specifies an invalid local address!" << std::endl;
exit(1);
}
localAddresses++;
}
else {
std::cerr << "ERROR: Too many local addresses!" << std::endl;
exit(1);
}
}
else {
if(remoteAddresses < SCTP_MAXADDRESSES) {
remoteAddressArray[remoteAddresses] =
SocketAddress::createSocketAddress(0, argv[i]);
if(remoteAddressArray[remoteAddresses] == NULL) {
std::cerr << "ERROR: Argument \"" << argv[i] << "\" is an invalid remote address!" << std::endl;
exit(1);
}
remoteAddresses++;
}
else {
std::cerr << "ERROR: Too many remote addresses!" << std::endl;
exit(1);
}
}
}
if(optForceIPv4) {
InternetAddress::UseIPv6 = false;
}
if(remoteAddresses < 1) {
std::cerr << "ERROR: No remote addresses given!" << std::endl;
exit(1);
}
if(remoteAddressArray[0]->getPort() == 0) {
std::cerr << "ERROR: No remote port number is given with first remote address!" << std::endl;
exit(1);
}
if(localAddresses < 1) {
/*
localAddressArray[0] = new InternetAddress(0);
if(localAddressArray[0] == NULL) {
std::cerr << "ERROR: Out of memory!" << std::endl;
exit(1);
}
localAddresses = 1;
*/
SocketAddress::deleteAddressList(localAddressArray);
if(!Socket::getLocalAddressList(
localAddressArray,
localAddresses,
Socket::GLAF_HideBroadcast|Socket::GLAF_HideMulticast|Socket::GLAF_HideAnycast)) {
std::cerr << "ERROR: Cannot obtain local addresses!" << std::endl;
exit(1);
}
if(localAddresses < 1) {
std::cerr << "ERROR: No valid local addresses have been found?!" << std::endl
<< " Check your network interface configuration!" << std::endl;
exit(1);
}
}
for(cardinal i = 1;i < localAddresses;i++) {
localAddressArray[i]->setPort(localAddressArray[0]->getPort());
}
for(cardinal i = 1;i < remoteAddresses;i++) {
remoteAddressArray[i]->setPort(remoteAddressArray[0]->getPort());
}
unreliable = std::min(outstreams,unreliable);
// ====== Print information ==============================================
std::cout << "SCTP Terminal - Copyright (C) 2001-2026 Thomas Dreibholz" << std::endl;
std::cout << "--------------------------------------------------------" << std::endl;
localAddressArray[0]->setPrintFormat(SocketAddress::PF_Address|SocketAddress::PF_Legacy|SocketAddress::PF_HidePort);
std::cout << "Local Addresses: " << *(localAddressArray[0]) << std::endl;
for(cardinal i = 1;i < localAddresses;i++) {
localAddressArray[i]->setPrintFormat(SocketAddress::PF_Address|SocketAddress::PF_Legacy|SocketAddress::PF_HidePort);
std::cout << " " << *(localAddressArray[i]) << std::endl;
}
std::cout << "Remote Addresses: " << *(remoteAddressArray[0]) << std::endl;
remoteAddressArray[0]->setPrintFormat(SocketAddress::PF_Address|SocketAddress::PF_Legacy|SocketAddress::PF_HidePort);
for(cardinal i = 1;i < remoteAddresses;i++) {
remoteAddressArray[i]->setPrintFormat(SocketAddress::PF_Address|SocketAddress::PF_Legacy|SocketAddress::PF_HidePort);
std::cout << " " << *(remoteAddressArray[i]) << std::endl;
}
std::cout << "Outgoing Streams: " << outstreams << std::endl;
std::cout << "Max. Incoming Streams: " << instreams << std::endl;
std::cout << "Unreliable: " << unreliable << std::endl;
std::cout << std::endl << std::endl;
// ====== Create socket and connect ======================================
Socket clientSocket(Socket::IP,Socket::Stream,Socket::SCTP);
if(clientSocket.bindx((const SocketAddress**)localAddressArray,
localAddresses,
SCTP_BINDX_ADD_ADDR) == false) {
std::cerr << "ERROR: Unable to bind socket!" << std::endl;
exit(1);
}
sctp_initmsg init;
init.sinit_num_ostreams = outstreams;
init.sinit_max_instreams = instreams;
init.sinit_max_attempts = 0;
init.sinit_max_init_timeo = 60;
if(clientSocket.setSocketOption(IPPROTO_SCTP,SCTP_INITMSG,(char*)&init,sizeof(init)) < 0) {
std::cerr << "ERROR: Unable to set SCTP_INITMSG parameters!" << std::endl;
exit(1);
}
sctp_event_subscribe events;
memset((char*)&events,1,sizeof(events));
if(clientSocket.setSocketOption(IPPROTO_SCTP,SCTP_EVENTS,&events,sizeof(events)) < 0) {
std::cerr << "ERROR: SCTP_EVENTS failed!" << std::endl;
exit(1);
}
std::cout << "Connecting... ";
std::cout.flush();
if(clientSocket.connectx((const SocketAddress**)remoteAddressArray,
remoteAddresses) == false) {
std::cout << "failed!" << std::endl;
std::cerr << "ERROR: Unable to connect to remote address(es)!" << std::endl;
exit(1);
}
std::cout << "done. Use Ctrl-D to end transmission." << std::endl << std::endl;
// ====== Start threads ==================================================
installBreakDetector();
EchoThread echo(&clientSocket,instreams);
CopyThread copy(&clientSocket,outstreams,unreliable);
// ====== Main loop ======================================================
while(!breakDetected()) {
Thread::delay(100000);
}
// ====== Stop threads ===================================================
copy.stop();
echo.stop();
SocketAddress::deleteAddressList(localAddressArray);
SocketAddress::deleteAddressList(remoteAddressArray);
std::cout << "\x1b[" << getANSIColor(0) << "mTerminated!" << std::endl;
return 0;
}