forked from yasheena/telnetspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelnetSpy.cpp
1172 lines (1090 loc) · 25.2 KB
/
TelnetSpy.cpp
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* TELNET SERVER FOR ESP8266 / ESP32
* Cloning the serial port via Telnet.
*
* Written by Wolfgang Mattis ([email protected]).
* Version 1.41 / June 26, 2022.
* MIT license, all text above must be included in any redistribution.
*
* Proposed Modifications 10 Sep 2022 by Ray Jones ([email protected])
* Always perform full buffer recall upon a fresh telnet connection.
* Buffer left untainted by telnet pings.
* Much tider timeout handling, leveraging the inherent
* behaviour of rollover by using signed compares after subtracting
* two unsigned values.
*
* Proposals Marked using RLJ_SPY_MODS
*/
#ifdef ESP8266
extern "C" {
#include "user_interface.h"
}
#endif
#include "TelnetSpy.h"
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif
static TelnetSpy* actualObject = NULL;
static void TelnetSpy_putc(char c) {
if (NULL != actualObject) {
actualObject->debugWrite(c);
}
}
static void TelnetSpy_ignore_putc(char c) {;
}
#ifdef RLJ_SPY_MODS
#define TELNETSPY_SERIALPORT Serial
#endif
TelnetSpy::TelnetSpy() {
port = TELNETSPY_PORT;
telnetServer = NULL;
started = false;
listening = false;
firstMainLoop = true;
#ifdef RLJ_SPY_MODS
usedSer = &TELNETSPY_SERIALPORT; // OTHER WORK: nifty SerialIntercept by hooking HardwareSerial - then you can collect other Arduino library messages!
#else
usedSer = &Serial;
#endif
storeOffline = true;
connected = false;
callbackConnect = NULL;
callbackDisconnect = NULL;
callbackNvtBRK = NULL;
callbackNvtIP = (void(*)()) 1; // 1 => use ESP.restart;
callbackNvtAO = (void(*)()) 1; // 1 => use TelnetSpy.disconnectClient()
callbackNvtAYT = NULL;
callbackNvtEC = NULL;
callbackNvtEL = NULL;
callbackNvtGA = NULL;
callbackNvtWWDD = NULL;
welcomeMsg = strdup(TELNETSPY_WELCOME_MSG);
rejectMsg = strdup(TELNETSPY_REJECT_MSG);
filterChar = 0;
filterMsg = NULL;
filterCallback = NULL;
minBlockSize = TELNETSPY_MIN_BLOCK_SIZE;
collectingTime = TELNETSPY_COLLECTING_TIME;
maxBlockSize = TELNETSPY_MAX_BLOCK_SIZE;
pingTime = TELNETSPY_PING_TIME;
#ifdef RLJ_SPY_MODS
pingHoldoff = 0;
waitHoldoff = 0;
NVTidx = 0;
#else
pingRef = 0xFFFFFFFF;
waitRef = 0xFFFFFFFF;
#endif
nvtDetected = false;
telnetBuf = NULL;
bufLen = 0;
uint16_t size = TELNETSPY_BUFFER_LEN;
while (!setBufferSize(size)) {
size = size >> 1;
if (size < minBlockSize) {
setBufferSize(minBlockSize);
break;
}
}
recBuf = NULL;
recLen = 0;
setRecBufferSize(TELNETSPY_REC_BUFFER_LEN);
debugOutput = TELNETSPY_CAPTURE_OS_PRINT;
if (debugOutput) {
setDebugOutput(true);
}
}
TelnetSpy::~TelnetSpy() {
end();
if (welcomeMsg) free(welcomeMsg);
if (rejectMsg) free(rejectMsg);
if (filterMsg) free(filterMsg);
if (telnetBuf) free(telnetBuf);
if (recBuf) free(recBuf);
}
void TelnetSpy::setPort(uint16_t portToUse) {
port = portToUse;
if (listening) {
if (client.connected()) {
sendBlock();
client.flush();
client.stop();
}
if (connected && (callbackDisconnect != NULL)) {
callbackDisconnect();
}
connected = false;
telnetServer->close();
delete telnetServer;
telnetServer = new WiFiServer(port);
if (started) {
telnetServer->begin();
telnetServer->setNoDelay(bufLen > 0);
}
}
}
void TelnetSpy::setWelcomeMsg(const char* msg) {
if (welcomeMsg) {
free(welcomeMsg);
}
welcomeMsg = strdup(msg);
}
void TelnetSpy::setWelcomeMsg(const String& msg) {
if (welcomeMsg) {
free(welcomeMsg);
}
welcomeMsg = strdup(msg.c_str());
}
void TelnetSpy::setRejectMsg(const char* msg) {
if (rejectMsg) {
free(rejectMsg);
}
rejectMsg = strdup(msg);
}
void TelnetSpy::setRejectMsg(const String& msg) {
if (rejectMsg) {
free(rejectMsg);
}
rejectMsg = strdup(msg.c_str());
}
void TelnetSpy::setMinBlockSize(uint16_t minSize) {
minBlockSize = min(max((uint16_t) 1, minSize), maxBlockSize);
}
void TelnetSpy::setCollectingTime(uint16_t colTime) {
collectingTime = colTime;
}
void TelnetSpy::setMaxBlockSize(uint16_t maxSize) {
maxBlockSize = max(maxSize, minBlockSize);
}
bool TelnetSpy::setBufferSize(uint16_t newSize) {
if (telnetBuf && (bufLen == newSize)) {
return true;
}
if (newSize == 0) {
bufLen = 0;
if (telnetBuf) {
free(telnetBuf);
telnetBuf = NULL;
}
if (telnetServer) {
telnetServer->setNoDelay(false);
}
return true;
}
newSize = max(newSize, minBlockSize);
uint16_t oldBufLen = bufLen;
bufLen = newSize;
uint16_t tmp;
if (!telnetBuf || (bufUsed == 0)) {
bufRdIdx = 0;
bufWrIdx = 0;
bufUsed = 0;
} else {
if (bufLen < oldBufLen) {
if (bufRdIdx < bufWrIdx) {
if (bufWrIdx > bufLen) {
tmp = min(bufLen, (uint16_t) (bufWrIdx - max(bufLen, bufRdIdx)));
memcpy(telnetBuf, &telnetBuf[bufWrIdx - tmp], tmp);
bufWrIdx = tmp;
if (bufWrIdx > bufRdIdx) {
bufRdIdx = bufWrIdx;
} else {
if (bufRdIdx > bufLen) {
bufRdIdx = 0;
}
}
if (bufRdIdx == bufWrIdx) {
bufUsed = bufLen;
} else {
bufUsed = bufWrIdx - bufRdIdx;
}
}
} else {
if (bufWrIdx > bufLen) {
memcpy(telnetBuf, &telnetBuf[bufWrIdx - bufLen], bufLen);
bufRdIdx = 0;
bufWrIdx = 0;
bufUsed = bufLen;
} else {
tmp = min(bufLen - bufWrIdx, oldBufLen - bufRdIdx);
memcpy(&telnetBuf[bufLen - tmp], &telnetBuf[oldBufLen - tmp], tmp);
bufRdIdx = bufLen - tmp;
bufUsed = bufWrIdx + tmp;
}
}
}
}
#ifdef RLJ_SPY_MODS
bufLeftToSend = bufUsed;
#endif
char* temp = (char*) realloc(telnetBuf, bufLen);
if (!temp) {
return false;
}
telnetBuf = temp;
if (telnetBuf && (bufLen > oldBufLen) && (bufRdIdx > bufWrIdx)) {
tmp = bufLen - (oldBufLen - bufRdIdx);
memcpy(&telnetBuf[tmp], &telnetBuf[bufRdIdx], oldBufLen - bufRdIdx);
bufRdIdx = tmp;
}
if (telnetServer) {
telnetServer->setNoDelay(true);
}
return true;
}
uint16_t TelnetSpy::getBufferSize() {
if (!telnetBuf) {
return 0;
}
return bufLen;
}
void TelnetSpy::setStoreOffline(bool store) {
storeOffline = store;
}
bool TelnetSpy::getStoreOffline() {
return storeOffline;
}
void TelnetSpy::setPingTime(uint16_t pngTime) {
pingTime = pngTime;
#ifdef RLJ_SPY_MODS
if (pingTime != 0) {
setHoldoff(pingHoldoff, pingTime);
}
#else
if (pingTime == 0) {
pingRef = 0xFFFFFFFF;
} else {
pingRef = (millis() & 0x7FFFFFF) + pingTime;
}
#endif
}
bool TelnetSpy::setRecBufferSize(uint16_t newSize) {
if (recBuf && (recLen == newSize)) {
return true;
}
if (recBuf) {
free(recBuf);
recBuf = NULL;
recLen = 0;
}
if (newSize == 0) {
return true;
}
recBuf = (char*) malloc(newSize);
if (!recBuf) {
return false;
}
recLen = newSize;
recRdIdx = 0;
recWrIdx = 0;
recUsed = 0;
return true;
}
uint16_t TelnetSpy::getRecBufferSize() {
if (!recBuf) {
return 0;
}
return recLen;
}
void TelnetSpy::setSerial(HardwareSerial* usedSerial) {
usedSer = usedSerial;
}
size_t TelnetSpy::write (uint8_t data) {
if (telnetBuf) {
if (storeOffline || client.connected()) {
if (bufUsed == bufLen) { // BUFFER IS FULL!
if (client.connected()) {
sendBlock(); // try and send - but why? - does not change avaialble write space!
}
if (bufUsed == bufLen) { // buffer is 100% full, shed oldest line
#ifdef RLJ_SPY_MODS
removeOldestLine();
#else
char c;
while (bufUsed > 0) {
c = pullTelnetBuf();
if (c == '\n') {
break;
}
}
if (peekTelnetBuf() == '\r') {
pullTelnetBuf();
}
#endif
}
}
addTelnetBuf(data);
}
} else {
if (client.connected()) {
client.write(data);
}
}
if ((NULL != usedSer) && *usedSer) {
return usedSer->write(data);
}
return 1;
}
void TelnetSpy::debugWrite (uint8_t data) {
if (telnetBuf) {
if (storeOffline || client.connected()) {
if (bufUsed == bufLen) {
#ifdef RLJ_SPY_MODS
removeOldestLine();
#else
char c;
while (bufUsed > 0) {
c = pullTelnetBuf();
if (c == '\n') {
break;
}
}
if (peekTelnetBuf() == '\r') {
pullTelnetBuf();
}
#endif
}
addTelnetBuf(data);
}
}
#ifdef ESP8266
ets_putc(data);
#else
ets_write_char_uart(data);
#endif
}
int TelnetSpy::available (void) {
if (usedSer) {
int avail = usedSer->available();
if (avail > 0) {
return avail;
}
}
if (client.connected()) {
return telnetAvailable();
}
return 0;
}
int TelnetSpy::read (void) {
int val = -1;
if (usedSer) {
val = usedSer->read();
if (val != -1) {
return val;
}
}
if (client.connected()) {
if (telnetAvailable()) {
if (recBuf) {
if (recUsed == 0) {
val = -1;
} else {
CRITCAL_SECTION_START
val = recBuf[recRdIdx++];
if (recRdIdx >= recLen) {
recRdIdx = 0;
}
recUsed--;
CRITCAL_SECTION_END
}
} else {
val = client.read();
}
}
}
return val;
}
int TelnetSpy::peek (void) {
int val = -1;
if (usedSer) {
val = usedSer->peek();
if (val != -1) {
return val;
}
}
if (client.connected()) {
if (telnetAvailable()) {
if (recBuf) {
val = recBuf[recRdIdx];
} else {
val = client.peek();
}
}
}
return val;
}
void TelnetSpy::flush (void) {
if (usedSer) {
usedSer->flush();
}
if (client.connected()) {
sendBlock();
client.flush();
}
}
#ifdef ESP8266
void TelnetSpy::begin(unsigned long baud, SerialConfig config, SerialMode mode, uint8_t tx_pin) {
if (usedSer) {
usedSer->begin(baud, config, mode, tx_pin);
}
setDebugOutput(debugOutput);
started = true;
}
#else // ESP32
void TelnetSpy::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert) {
if (usedSer) {
usedSer->begin(baud, config, rxPin, txPin, invert);
}
setDebugOutput(debugOutput);
started = true;
}
#endif
void TelnetSpy::end() {
if (debugOutput) {
setDebugOutput(false);
}
if (usedSer) {
usedSer->end();
}
if (client.connected()) {
sendBlock();
client.flush();
client.stop();
}
if (connected && (callbackDisconnect != NULL)) {
callbackDisconnect();
}
connected = false;
telnetServer->close();
delete telnetServer;
telnetServer = NULL;
listening = false;
started = false;
}
#ifdef ESP8266
void TelnetSpy::swap(uint8_t tx_pin) {
if (usedSer) {
usedSer->swap(tx_pin);
}
}
void TelnetSpy::set_tx(uint8_t tx_pin) {
if (usedSer) {
usedSer->set_tx(tx_pin);
}
}
void TelnetSpy::pins(uint8_t tx, uint8_t rx) {
if (usedSer) {
usedSer->pins(tx, rx);
}
}
bool TelnetSpy::isTxEnabled(void) {
if (usedSer) {
return usedSer->isTxEnabled();
}
return true;
}
bool TelnetSpy::isRxEnabled(void) {
if (usedSer) {
return usedSer->isRxEnabled();
}
return true;
}
#endif
int TelnetSpy::availableForWrite(void) {
#ifdef RLJ_SPY_MODS
if(NVTidx) {
return NVTidx;
}
#endif
if (usedSer) {
return min(usedSer->availableForWrite(), bufLen - bufUsed);
}
return bufLen - bufUsed;
}
TelnetSpy::operator bool() const {
if (usedSer) {
return (bool) *usedSer;
}
return true;
}
void TelnetSpy::setDebugOutput(bool en) {
debugOutput = en;
if (debugOutput) {
actualObject = this;
ets_install_putc1(TelnetSpy_putc); // Set system printing (os_printf) to TelnetSpy
#ifdef ESP8266
system_set_os_print(true);
#endif
} else {
if (actualObject == this) {
#ifdef ESP8266
system_set_os_print(false);
#endif
ets_install_putc1(TelnetSpy_ignore_putc); // Ignore system printing
actualObject = NULL;
}
}
}
uint32_t TelnetSpy::baudRate(void) {
if (usedSer) {
return usedSer->baudRate();
}
return 115200;
}
#ifdef RLJ_SPY_MODS
void TelnetSpy::sendBlock() {
bool action = false;
CRITCAL_SECTION_START
uint16_t idx = NVTidx; // avoid tainting telnet buffer with pings
uint8_t NVTcpy[2] = {NVT[0], NVT[1] };
NVTidx = 0;
CRITCAL_SECTION_END
if(idx) { // typ. telnet NOP being sent out of bounds
client.write(&NVTcpy[--idx], 1);
if(idx) {
client.write(&NVTcpy[--idx], 1);
}
action = true;
}
CRITCAL_SECTION_START
uint16_t len = bufLeftToSend;
if (len > maxBlockSize) {
len = maxBlockSize;
}
len = min(len, (uint16_t) (bufLen - bufRdIdx)); // in case we approaching the end of buffer memory (wraparound)
idx = bufRdIdx;
CRITCAL_SECTION_END
if (len) {
#ifdef DEBUG_TENETSPY
TELNETSPY_SERIALPORT.printf("TelnetSpy:%d %d %d %d %d %d\r\n", bufRdIdxStart, len, bufLeftToSend, bufRdIdx, bufUsed, bufLen); // DEBUG directly to serial port, always
#endif
action = true;
client.write(&telnetBuf[idx], len);
CRITCAL_SECTION_START
bufRdIdx += len;
if (bufRdIdx >= bufLen) {
bufRdIdx -= bufLen; // BUG FIX? was =0, nah len should be constrained, but still .....
}
bufLeftToSend -= len;
CRITCAL_SECTION_END
}
if(action) {
setHoldoff(waitHoldoff, collectingTime);
if(pingTime!=0 && !isHoldoff(pingHoldoff))
setHoldoff(pingHoldoff, pingTime);
}
}
#else
void TelnetSpy::sendBlock() {
CRITCAL_SECTION_START
uint16_t len = bufUsed;
if (len > maxBlockSize) {
len = maxBlockSize;
}
len = min(len, (uint16_t) (bufLen - bufRdIdx));
uint16_t idx = bufRdIdx;
CRITCAL_SECTION_END
if (len == 0) {
return;
}
client.write(&telnetBuf[idx], len);
CRITCAL_SECTION_START
bufRdIdx += len;
if (bufRdIdx >= bufLen) {
bufRdIdx = 0;
}
bufUsed -= len;
if (bufUsed == 0) {
bufRdIdx = 0;
bufWrIdx = 0;
}
CRITCAL_SECTION_END
waitRef = 0xFFFFFFFF;
if (pingRef != 0xFFFFFFFF) {
pingRef = (millis() & 0x7FFFFFF) + pingTime;
if (pingRef > 0x7FFFFFFF) {
pingRef -= 0x80000000;
}
}
}
#endif
void TelnetSpy::addTelnetBuf(char c) {
#ifdef RLJ_SPY_MODS
CRITCAL_SECTION_START
if (bufUsed == bufLen) {
removeOldestLine(); // get rid of oldest line in the buffer, reducing bufUsed in the process
}
telnetBuf[bufWrIdx++] = c;
if (bufWrIdx >= bufLen) {
bufWrIdx = 0;
}
bufUsed++;
bufLeftToSend++;
CRITCAL_SECTION_END
#else
CRITCAL_SECTION_START
telnetBuf[bufWrIdx] = c;
if (bufUsed == bufLen) {
bufRdIdx++; // TRYING TO UNDERSTAND PURPOSE, should we not make space?
if (bufRdIdx >= bufLen) {
bufRdIdx = 0;
}
} else {
bufUsed++;
}
bufWrIdx++;
if (bufWrIdx >= bufLen) {
bufWrIdx = 0;
}
CRITCAL_SECTION_END
#endif
}
void TelnetSpy::removeOldestLine()
{
char c;
while (bufUsed > 0) { // only if buffer has data
c = pullTelnetBuf(); // get oldest character in buffer
if (c == '\n') { // stop once we have removed a line feed
break;
}
}
if (peekTelnetBuf() == '\r') {
pullTelnetBuf(); // remove a possible CR (not normal, should be CR/LF usually!)
}
}
char TelnetSpy::pullTelnetBuf() {
if (bufUsed == 0) {
return 0;
}
CRITCAL_SECTION_START
#ifdef RLJ_SPY_MODS
char c = telnetBuf[bufRdIdxStart++]; // obtain OLDEST character in the buffer and increment tail position (oldest data)
if (bufRdIdxStart >= bufLen) {
bufRdIdxStart = 0;
}
#else
char c = telnetBuf[bufRdIdx++];
if (bufRdIdx >= bufLen) {
bufRdIdx = 0;
}
#endif
bufUsed--; // decrement number of active bytes
CRITCAL_SECTION_END
return c;
}
char TelnetSpy::peekTelnetBuf() {
if (bufUsed == 0) {
return 0;
}
CRITCAL_SECTION_START
#ifdef RLJ_SPY_MODS
char c = telnetBuf[bufRdIdxStart];
#else
char c = telnetBuf[bufRdIdx];
#endif
CRITCAL_SECTION_END
return c;
}
int TelnetSpy::telnetAvailable() {
checkReceive();
if (recBuf) {
return recUsed;
}
return client.available();
}
bool TelnetSpy::isClientConnected() {
return connected;
}
void TelnetSpy::setCallbackOnConnect(void (*callback)()) {
callbackConnect = callback;
}
void TelnetSpy::setCallbackOnDisconnect(void (*callback)()) {
callbackDisconnect = callback;
}
void TelnetSpy::disconnectClient() {
if (client.connected()) {
sendBlock();
client.flush();
client.stop();
}
if (connected && (callbackDisconnect != NULL)) {
callbackDisconnect();
}
connected = false;
}
void TelnetSpy::clearBuffer() {
bufUsed = 0;
bufRdIdx = 0;
bufWrIdx = 0;
#ifdef RLJ_SPY_MODS
bufRdIdxStart = 0;
#endif
}
void TelnetSpy::setFilter(char ch, const char* msg, void (*callback)()) {
filterChar = ch;
if (filterMsg) {
free(filterMsg);
}
filterMsg = strdup(msg);
filterCallback = callback;
}
void TelnetSpy::setFilter(char ch, const String& msg, void (*callback)()) {
filterChar = ch;
if (filterMsg) {
free(filterMsg);
}
filterMsg = strdup(msg.c_str());
filterCallback = callback;
}
char TelnetSpy::getFilter() {
return filterChar;
}
void TelnetSpy::setCallbackOnNvtBRK(void (*callback)()) {
callbackNvtBRK = callback;
}
void TelnetSpy::setCallbackOnNvtIP(void (*callback)()) {
callbackNvtIP = callback;
}
void TelnetSpy::setCallbackOnNvtAO(void (*callback)()) {
callbackNvtAO = callback;
}
void TelnetSpy::setCallbackOnNvtAYT(void (*callback)()) {
callbackNvtAYT = callback;
}
void TelnetSpy::setCallbackOnNvtEC(void (*callback)()) {
callbackNvtEC = callback;
}
void TelnetSpy::setCallbackOnNvtEL(void (*callback)()) {
callbackNvtEL = callback;
}
void TelnetSpy::setCallbackOnNvtGA(void (*callback)()) {
callbackNvtGA = callback;
}
void TelnetSpy::setCallbackOnNvtWWDD(void (*callback)(char command, char option)) {
callbackNvtWWDD = callback;
}
void TelnetSpy::handle() {
if (firstMainLoop) {
firstMainLoop = false;
// Between setup() and loop() the configuration for os_print may be changed so it must be renewed
if (debugOutput && (actualObject == this)) {
setDebugOutput(true);
}
}
if (!started) {
return;
}
if (!listening) {
switch (WiFi.getMode()) {
case WIFI_MODE_STA:
if (WiFi.status() != WL_CONNECTED) {
return;
}
break;
case WIFI_MODE_AP:
case WIFI_MODE_APSTA:
break;
default:
return;
}
telnetServer = new WiFiServer(port);
telnetServer->begin();
telnetServer->setNoDelay(bufLen > 0);
listening = true;
}
if (telnetServer->hasClient()) {
if (client.connected()) {
WiFiClient rejectClient = telnetServer->available();
if (strlen(rejectMsg) > 0) {
rejectClient.write((const uint8_t*) rejectMsg, strlen(rejectMsg));
}
rejectClient.flush();
rejectClient.stop();
} else {
client = telnetServer->available();
if (strlen(welcomeMsg) > 0) {
client.write((const uint8_t*) welcomeMsg, strlen(welcomeMsg));
}
#ifdef RLJ_SPY_MODS
// reset bufRdIdx to replay as much as we hold
CRITCAL_SECTION_START
bufRdIdx = bufRdIdxStart;
bufLeftToSend = bufUsed;
CRITCAL_SECTION_END
#endif
}
}
#ifdef RLJ_SPY_MODS
if (client.connected()) {
if (!connected) {
connected = true;
if(pingTime != 0) {
setHoldoff(pingHoldoff, pingTime);
}
if (callbackConnect != NULL) {
callbackConnect();
}
}
} else {
if (connected) {
connected = false;
sendBlock();
client.flush();
client.stop();
pingHoldoff = 0;
setHoldoff(waitHoldoff, collectingTime);
if (callbackDisconnect != NULL) {
callbackDisconnect();
}
}
}
if (client.connected() && (bufLeftToSend > 0)) {
if (bufLeftToSend >= minBlockSize) {
sendBlock();
} else {
if(!isHoldoff(waitHoldoff)) {
sendBlock();
setHoldoff(waitHoldoff, collectingTime);
}
}
}
if (client.connected() && pingTime !=0 && !isHoldoff(pingHoldoff)) {
CRITCAL_SECTION_START
// avoid tainting telnet buffer with pings, use extra OOB buffer
if (nvtDetected) {
// Send a NOP via telnet NVT protocol (out of bounds)
NVT[0] = 241;
NVT[1] = 255;
NVTidx = 2;
} else {
// Send a NULL
NVT[0] = 0;
NVTidx = 1;
}
CRITCAL_SECTION_END
#ifdef DEBUG_TENETSPY
TELNETSPY_SERIALPORT.println("telnet NOP"); // DEBUG directly to serial port, always
#endif
sendBlock();
}
#else
if (client.connected()) {
if (!connected) {
connected = true;
if (pingTime != 0) {
pingRef = (millis() & 0x7FFFFFF) + pingTime;
}
if (callbackConnect != NULL) {
callbackConnect();
}
}
} else {
if (connected) {
connected = false;
sendBlock();
client.flush();
client.stop();
pingRef = 0xFFFFFFFF;
waitRef = 0xFFFFFFFF;
if (callbackDisconnect != NULL) {
callbackDisconnect();
}
}
}
if (client.connected() && (bufUsed > 0)) {
if (bufUsed >= minBlockSize) {
sendBlock();
} else {
unsigned long m = millis() & 0x7FFFFFF;
if (waitRef == 0xFFFFFFFF) {
waitRef = m + collectingTime;
if (waitRef > 0x7FFFFFFF) {
waitRef -= 0x80000000;
}
} else {
if (!((waitRef < 0x20000000) && (m > 0x60000000)) && (m >= waitRef)) {
sendBlock();
}
}
}
}
if (client.connected() && (pingRef != 0xFFFFFFFF)) {
unsigned long m = millis() & 0x7FFFFFF;
if (!((pingRef < 0x20000000) && (m > 0x60000000)) && (m >= pingRef)) {
if (nvtDetected) {
// Send a NOP via telnet NVT protocol
addTelnetBuf(255);
addTelnetBuf(241);
} else {
// Send a NULL