-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIP.ino
1744 lines (1677 loc) · 73.3 KB
/
IP.ino
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
#include <TimeLib.h>
time_t t_tijd;
time_t dynDNSResponseTime;
char IPtijd[16];
char myIP[32];
char priorIP[32];
bool GetWANIPAddress() {
String strmyIP;
char GWAlookfor[16];
int rslt;
char ocbuff[16];
bool succes = false;
t_tijd = now();
sprintf(IPtijd, "%02d:%02d:%02d", hour(t_tijd), minute(t_tijd), second(t_tijd)); // IPtijd is max 16
sprintf(temptxtbuff, "\r\n%s Start retrieving WAN IP via api.ipify.org ", IPtijd);
textlog(temptxtbuff, false);
// Get my ip address
EthernetClient whatsmyipClient;
rslt = whatsmyipClient.connect("api.ipify.org", 80);
if (iplog && (rslt != 1)) {
sprintf(temptxtbuff, " request result: %d ", rslt);
textlog(temptxtbuff, false);
}
if (rslt == 1) {
textStringLog("-> connected ", false);
whatsmyipClient << F("GET / HTTP/1.1") << endl;
whatsmyipClient << F("Host: api.ipify.org") << endl << endl;
const static char whtsmpClnt[] PROGMEM = "vegur\r\n\r\n";
strcpy_P(GWAlookfor, (char*) whtsmpClnt); // GWAlookfor is max 16
/*received from api.ipify.org: HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
Content-Type: text/plain
Vary: Origin
Date: Tue, 10 Nov 2020 16:00:11 GMT
Content-Length: 14
Via: 1.1 vegur
77.173.126.238
80.60.45.94
*/
if (whatsmyipClient.find(GWAlookfor)) //response from websit
{
todayIPCnt++;
strmyIP = whatsmyipClient.readStringUntil('<');
if (iplog) {
textStringLog("-> response: ", false);
textStringLog(strmyIP, false);
}
if ((strmyIP.length() > 7) && (strmyIP.length() < 16)) {
strcpy(myIP, strmyIP.c_str()); // myIP is max 32
textStringLog(" -> My WAN IP: ", false);
textlog(myIP, false);
succes = true;
if (strcmp(myIP, priorIP) != 0 ) {
int retr = 0;
bool resp = false;
do {
resp = updateDNS();
retr++;
} while ((resp == false) && (retr < 3));
sendEmail(1);
strncpy(priorIP, myIP, 17); // priorIP (destination) and myIP is max 32
if ( upTime != 0) {
textStringLog(" -> WARNING WAN IP adres changed!", false);
}
}
}
whatsmyipClient.stop();
textStringLog(" -> GetWANIPAddress -> ok", false);
}
else {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d ERROR: in response from api.ipify.org, \"vegur\" not found in response.", hour(), minute(), second()); // temptxtbuff is max 1024 char
textlog(temptxtbuff, false);
}
}
else {
String conerror;
switch (rslt) {
case -1:
conerror = "TIMED_OUT";
break;
case -2:
conerror = "INVALID_SERVER";
break;
case -3:
conerror = "TRUNCATED";
break;
case -4:
conerror = "INVALID_RESPONSE";
break;
}
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d ERROR: %s connection failed to retrieve WAN IP address", hour(), minute(), second(), conerror.c_str()); // temptxtbuff is max 1024 char
textlog(temptxtbuff, false);
}
if (whatsmyipClient.connected()) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d ERROR: GetWANIPAddress stop unexpected connection", hour(), minute(), second()); // temptxtbuff is max 1024 char
textlog(temptxtbuff, false);
whatsmyipClient.stop();
}
return succes;
}
bool updateDNS() {
// updateDNSafraid(DYNDNS_API_KEY);
//modify to spi bus memory
char uapikey[41];
int i;
for (i = 0; i < 40; i++) {
uapikey[i] = DYNDNS_API_KEY[i];
}
uapikey[i] = '\0';
textStringLog("\r\nStored DYNDNS_API_KEY from EEPROM reads: ", false);
textlog(uapikey, true);
return updateDNSafraid(uapikey);
}
bool updateDNSafraid(char* afraidstr)
{
// meldingen zijn
// Updated 1 host(s) baaisolar.remote.mx to 65.234.2.34 in 0.176 seconds
// ERROR: Address 77.172.249.196 has not changed.
bool succes = false;
textStringLog("Update Dynamic DNS at freedns.afraid.org: @", false);
t_tijd = now();
sprintf(IPtijd, "%02d:%02d:%02d", hour(t_tijd), minute(t_tijd), second(t_tijd)); // IPtijd is max 16
textlog(IPtijd, false);
textStringLog(" use API key: ", false);
textlog(afraidstr, false);
// Get my ip address
EthernetClient dynDNSClient; //
if (dynDNSClient.connect("freedns.afraid.org", 80)) {
if (!min_serial || iplog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d DynDNS send cmd: GET /dynamic/update.php?", hour(), minute(), second());
textlog(temptxtbuff, false);
textlog(afraidstr, false);
}
dynDNSClient << F("GET /dynamic/update.php");
dynDNSClient << F("?") << afraidstr << endl;
dynDNSClient << F("Host: freedns.afraid.org") << endl << endl;
// if(dynDNSClient.find("\":\"")) //response from websit
// {
dynDNSCnt++;
String strdynDNSresponse = dynDNSClient.readString();
char uDacobuff[512];
int cpybuflen;
if (strdynDNSresponse != "") {
if (iplog) {
textStringLog("\r\nDynDNS response: ", false);
if (strdynDNSresponse.length() > 511) {
cpybuflen = 511;
}
else {
cpybuflen = strdynDNSresponse.length();
}
strdynDNSresponse.toCharArray(uDacobuff, cpybuflen); // uDacobuff is max 512
uDacobuff[cpybuflen] = '\0';
textlog(uDacobuff, false);
textStringLog("\r\nprocessing response to: ", false);
}
int ncStr = strdynDNSresponse.indexOf("not changed");
if (iplog) {
sprintf(temptxtbuff, "not changed found at: %d", ncStr);
textlog(temptxtbuff, false);
}
if ((ncStr > -1) && (strdynDNSresponse.length() >= ncStr + 11))
{
strdynDNSresponse = strdynDNSresponse.substring(ncStr, ncStr + 11); // response for webpage
succes = true;
}
ncStr = strdynDNSresponse.indexOf("Updated");
if ((ncStr > -1) && (strdynDNSresponse.length() >= ncStr + 7))
{
strdynDNSresponse = strdynDNSresponse.substring(ncStr, ncStr + 7); // response for webpage
succes = true;
}
int i = strdynDNSresponse.length();
if (iplog) {
sprintf(temptxtbuff, "\r\nlength from strdynDNSresponse (i): %d", i);
textlog(temptxtbuff, false);
}
if (i > 63) {
i = 63;
// strdynDNSresponse[63] = '\0';
}
// Length (with one extra character for the null terminator)
strdynDNSresponse.toCharArray(dynDNSresponse, i + 1);
// dynDNSresponse[i] = '\0'; //toCHarArray sets terminator
textStringLog("\r\ndynDNSresponse: ", false);
textlog(dynDNSresponse, false);
}
dynDNSClient.stop();
dynDNSResponseTime = now();
if (iplog)
textStringLog("\r\nEnd updating address in DynDNS", false);
}
if (dynDNSClient.connected()) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d ERROR: dynDNSClient stop unexpected connection", hour(), minute(), second()); // temptxtbuff is max 1024 char
textlog(temptxtbuff, false);
dynDNSClient.stop();
}
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Exit code from updateDNSafraid: ", hour(), minute(), second()); // temptxtbuff is max 1024 char
textlog(temptxtbuff, false);
succes ? textStringLog("success", false) : textStringLog("fail", false);
return succes;
}
/*
============================================================
TFTP for up and downloading known IP scanners list, TFTP start by command setabuse
*/
/* Examples of network traffic.
Note two cases when ACKs with block# of 0 are sent.
Download without options:
tftp -> "\0\1FILENAME\0octet\0"
"\0\3\0\1FILEDATA..." <- tftpd
tftp -> "\0\4\0\1"
...
Download with option of blksize 16384:
tftp -> "\0\1FILENAME\0octet\0blksize\00016384\0"
"\0\6blksize\00016384\0" <- tftpd
tftp -> "\0\4\0\0"
"\0\3\0\1FILEDATA..." <- tftpd
tftp -> "\0\4\0\1"
...
Upload without options:
tftp -> "\0\2FILENAME\0octet\0"
"\0\4\0\0" <- tftpd
tftp -> "\0\3\0\1FILEDATA..."
"\0\4\0\1" <- tftpd
...
Upload with option of blksize 16384:
tftp -> "\0\2FILENAME\0octet\0blksize\00016384\0"
"\0\6blksize\00016384\0" <- tftpd
tftp -> "\0\3\0\1FILEDATA..."
"\0\4\0\1" <- tftpd
...
*/
// TFTP Opcode values from RFC 1350
//
#define TFTP_OPCODE_RRQ 1
#define TFTP_OPCODE_WRQ 2
#define TFTP_OPCODE_DATA 3
#define TFTP_OPCODE_ACK 4
#define TFTP_OPCODE_ERROR 5
// TFTP Error codes from RFC 1350
//
#define TFTP_ERROR_UNDEF 0
#define TFTP_ERROR_NOT_FOUND 1
#define TFTP_ERROR_ACCESS 2
#define TFTP_ERROR_FULL 3
#define TFTP_ERROR_ILLEGAL_OP 4
#define TFTP_ERROR_UNKNOWN_XFER 5
#define TFTP_ERROR_EXISTS 6
#define TFTP_ERROR_NO_SUCH_USER 7
#define TFTP_PORT 69
// TFTP OP codes
#define ERROR_UNKNOWN 0
#define ERROR_INVALID 1
#define ACK 2
#define ERROR_FULL 3
#define FINAL_ACK 4 // Like an ACK, but for the final data packet.?????
#define INVALID_IMAGE 5
#define ERROR_LARGE 6 // packet size > 512 not support (excluding header)
#define INVALID_HOST 7
#define SEND_DATA 8
#define ACK_EOT 9
#define FLASH_ERROR 10
#define TFTP_DATA_SIZE 512
#define TFTP_OPCODE_SIZE 2
#define TFTP_BLOCKNO_SIZE 2
#define TFTP_MAX_PAYLOAD 512
#define TFTP_PACKET_MAX_SIZE ( TFTP_OPCODE_SIZE + TFTP_BLOCKNO_SIZE + TFTP_MAX_PAYLOAD )
#define TIMEOUT 60
bool convertHTTPlistReq = false;
bool convertIPtableReq = false;
bool outputHTTPlistReq = false;
bool outputIPtableReq = false;
bool EndOfTransmission = false;
uint16_t lastPacket = 0;
uint32_t bufferPointer = 0;
uint8_t DownloadStarted = 0;
uint8_t rdbuffer[EEPROM_PAGE_SIZE + 1]; // size of an EEPROM page
uint8_t* bufPtr = rdbuffer;
uint16_t pageOffset = 0; // EEPROM page offset
unsigned int localTFTPport;
unsigned int remoteTFTPport;
IPAddress UDPSendRxTFTPTo;
/* Opcode?: tftp operation is unsupported. The bootloader only supports 'put' */
#define TFTP_OPCODE_ERROR_LEN 12
char tftp_opcode_error_packet[] PROGMEM = "\0\5" "\0\4" "Opcode?";
/* Full: Binary image file is larger than the available space. */
#define TFTP_FULL_ERROR_LEN 15
char tftp_full_error_packet[] PROGMEM = "\0\5" "\0\3" "Flash Full";
/* General catch-all error for unknown errors */
#define TFTP_UNKNOWN_ERROR_LEN 10
char tftp_unknown_error_packet[] PROGMEM = "\0\5" "\0\0" "Error";
/* General catch-all error for unknown errors */
#define TFTP_FLASH_ERROR_LEN 22
char tftp_flash_error_packet[] PROGMEM = "\0\5" "\0\0" "Flash write error";
/* packet too large only 4 + 512 datagram packets are accepted */
#define TFTP_LARGE_ERROR_LEN 25
char tftp_too_large_packet[] PROGMEM = "\0\5" "\0\4" "Packetsize too large";
/* Invalid image file: Doesn't look like a binary image file */
#define TFTP_INVALID_IMAGE_LEN 18
char tftp_invalid_image_packet[] PROGMEM = "\0\5" "\0\1" "Invalid image";
/* Invalid image file: Doesn't look like a binary image file */
#define TFTP_INVALID_HOST_LEN 17
char tftp_invalid_host_packet[] PROGMEM = "\0\5" "\0\1" "Invalid host";
uint8_t timedOut() {
if (DownloadStarted) {
if (tick > (TIMEOUT / 12))
return 1;
}
else if (tick > TIMEOUT) {
return 1; // close UDP port
}
return 0;
}
/*
Tempory routine to print the readback of the flash
*/
void show_buffer_Row2Serial(uint8_t* bufferaddr, uint16_t nrofbytes) {
char asciichar[17];
int acpos = 0;
char karakter;
uint8_t* sfbufPtr = bufferaddr;
Serial.println(); // start always on new line
for (int axc = 0 ; axc < (nrofbytes / 16); axc++) {
for (int i = 0; i < 16; i++) {
// if this is the first byte of the row,
// start row by printing the byte address
if (i == 0) {
sprintf(msgchararray, "%8d = 0x%08X ", sfbufPtr, sfbufPtr); // msgchararray is max 64 char
Serial.print(msgchararray);
}
// read current byte from flash
// write byte in hex form
karakter = *sfbufPtr++;
sprintf(msgchararray, "%02X ", karakter); // msgchararray is max 64 char
if (((byte)karakter > 0x1F) && ((byte)karakter < 0x7F)) {
asciichar[acpos] = karakter;
}
else {
asciichar[acpos] = 0x2E;
}
acpos++;
asciichar[acpos] = 0x00;
// if this is the last byte of the row,
// reset row counter and use println()
// to start a new line
if (i == 15) {
// client << msgchararray << br << F("\r\n");
Serial.print(msgchararray);
Serial.print(F(" "));
Serial.println(asciichar);
acpos = 0;
}
// else just print the hex value with print()
else {
// client << msgchararray;
Serial.print(msgchararray);
}
}
}
}
/*
Tempory routine to print the readback of the flash
*/
void show_Flash_Row2Serial(long int flashaddr) {
#ifdef OPTION_FLASH
char asciichar[17];
int acpos = 0;
char karakter;
uint8_t* sfbufPtr = rdbuffer;
Serial.println(); // start always on new line
flash.readByteArray(flashaddr, rdbuffer, EEPROM_PAGE_SIZE);
for (int axc = 0 ; axc < (EEPROM_PAGE_SIZE / 16); axc++) {
for (int i = 0; i < 16; i++) {
// if this is the first byte of the row,
// start row by printing the byte address
if (i == 0) {
sprintf(msgchararray, "%8d = 0x%08X ", sfbufPtr, sfbufPtr); // msgchararray is max 64 char
Serial.print(msgchararray);
}
// read current byte from flash
// write byte in hex form
karakter = *sfbufPtr++;
sprintf(msgchararray, "%02X ", karakter); // msgchararray is max 64 char
if (((byte)karakter > 0x1F) && ((byte)karakter < 0x7F)) {
asciichar[acpos] = karakter;
}
else {
asciichar[acpos] = 0x2E;
}
acpos++;
asciichar[acpos] = 0x00;
// if this is the last byte of the row,
// reset row counter and use println()
// to start a new line
if (i == 15) {
// client << msgchararray << br << F("\r\n");
Serial.print(msgchararray);
Serial.print(F(" "));
Serial.println(asciichar);
acpos = 0;
}
// else just print the hex value with print()
else {
// client << msgchararray;
Serial.print(msgchararray);
}
}
}
#endif
}
uint32_t countIPtableEntries() {
uint32_t flashaddr = abusefilter1_addr;
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Start counting IPtable whitelist entries at startup", hour(), minute(), second()); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
flash.readByteArray(flashaddr, rdbuffer, EEPROM_PAGE_SIZE);
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: Flash read success address: 0x%08x", hour(), minute(), second(), flashaddr); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
bufPtr = rdbuffer;
// while (*bufPtr != 0xFF) {
while (bufPtr < (rdbuffer + EEPROM_PAGE_SIZE) && (*bufPtr != 0xFF)) {
totalnets++;
bufPtr += 4;
if (bufPtr == (rdbuffer + EEPROM_PAGE_SIZE)) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d INFO Flash read pagebreak detected, reading last Flash page for converted blacklisted HTTP fragments.", hour(), minute(), second()); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
flashaddr += EEPROM_PAGE_SIZE;
if (flashaddr == (abusefilter12_addr + EEPROM_PAGE_SIZE)) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %04d INFO end of flash pages for IPtable reached.", hour(), minute(), second(), __LINE__); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
return totalnets;
}
flash.readByteArray(flashaddr, rdbuffer, EEPROM_PAGE_SIZE);
bufPtr = rdbuffer;
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: Flash read success address: 0x%08x", hour(), minute(), second(), flashaddr); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
}
}
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d IPtable whitelist counts %d entries.", hour(), minute(), second(), totalnets); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
return totalnets;
}
uint32_t countHTTPtableEntries() {
uint32_t flashaddr = HTTPblacklist1_addr;
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Start counting HTTP blacklist", hour(), minute(), second()); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
flash.readByteArray(flashaddr, rdbuffer, EEPROM_PAGE_SIZE);
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: Flash read success address: 0x%08X", hour(), minute(), second(), flashaddr); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
bufPtr = rdbuffer;
while (bufPtr < (rdbuffer + EEPROM_PAGE_SIZE) && (*bufPtr != 0xFF)) {
totalHTTPcmd++;
bufPtr += 16;
if (bufPtr == (rdbuffer + EEPROM_PAGE_SIZE)) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d INFO Flash read pagebreak detected, reading last Flash page for converted blacklisted HTTP fragments.", hour(), minute(), second()); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
flashaddr += EEPROM_PAGE_SIZE;
if (flashaddr == (HTTPblacklist2_addr + EEPROM_PAGE_SIZE)) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d ERROR: Flash end of blacklist area reached.", hour(), minute(), second()); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
return totalHTTPcmd;
}
flash.readByteArray(flashaddr, rdbuffer, EEPROM_PAGE_SIZE);
bufPtr = rdbuffer;
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: Flash read success address: 0x%08X", hour(), minute(), second(), flashaddr); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
}
}
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d HTTP blacklist counts %d entries.", hour(), minute(), second(), totalHTTPcmd); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
return totalHTTPcmd;
}
void writeTFTPbuffer(uint8_t wrbuffer[EEPROM_PAGE_SIZE], uint8_t wrpageOffset, uint16_t nrofbytes) {
uint32_t writeFileAddr = eepromEnd_addr + (wrpageOffset * EEPROM_PAGE_SIZE);
flash.eraseSector(writeFileAddr);
delay(25);
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %04d Writing blacklisted HTTP fragment of %d bytes to TFTPbuffer in flash 0x%08X, reading from wrbuffer at address 0x%08X", hour(), minute(), second(), __LINE__, nrofbytes, writeFileAddr, wrbuffer);
textlog(temptxtbuff, false);
}
if (!flash.writeByteArray(writeFileAddr, wrbuffer, nrofbytes)) {
flash_error = F("writeByteArray writeFileAddr FAIL");
StoreFlashError(flash_error, writeFileAddr);
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d ERROR: Converted TFTPbuffer fragments writing... writeByteArray writeFileAddr FAIL address: 0x%08x", hour(), minute(), second(), writeFileAddr); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
delay(3);
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: Flash write success address: 0x%08x, TFTPbuffer page: %d", hour(), minute(), second(), writeFileAddr, wrpageOffset); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
}
bool prepareIPtable2TFTPbuffer() {
/*
rdbuffer is global buffer
bufPtr = global buffer pointer from rdbuffer
rxBufPtr = recordpointer in global buffer
*/
char cnvipbuff[4]; // the integer in array as ASCII
char* cnvipbuffPtr;
uint8_t ipnet;
// uint8_t* rxBufPtr;
uint8_t wrbuffer[EEPROM_PAGE_SIZE]; // size of an EEPROM page
uint8_t wrpageOffset = 0;
uint8_t* wrBufPtr = wrbuffer;
uint16_t recordcnt = 0;
uint32_t flashaddr = abusefilter1_addr;
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Start converting IPtable whitelist to ASCII text, preparation for TFTP download", hour(), minute(), second()); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
// delay(25);
flash.readByteArray(flashaddr, rdbuffer, EEPROM_PAGE_SIZE);
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: Flash read success address: 0x%08x", hour(), minute(), second(), flashaddr); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
bufPtr = rdbuffer;
// while (*bufPtr != 0xFF) {
while (bufPtr < (rdbuffer + EEPROM_PAGE_SIZE) && (*bufPtr != 0xFF)) {
// rxBufPtr = bufPtr; //temp pointer for reading this entry
// sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Start processing record %d", hour(), minute(), second(), recordcnt); // temptxtbuff is max 1024
// textlog(temptxtbuff, false);
// delay(5);
for (uint8_t idxip = 0; idxip < 4; idxip++) {
cnvipbuffPtr = cnvipbuff;
ipnet = *bufPtr++;
sprintf(cnvipbuff, "%d", ipnet);
/*
if ((recordcnt > 677) && (recordcnt < 681)) {
Serial.print("\r\nConverting: ");
Serial.print(recordcnt);
Serial.print(" value: ");
Serial.print(ipnet);
}
*/
while (*cnvipbuffPtr != 0) {
/*
if ((recordcnt > 677) && (recordcnt < 681)) {
Serial.print("->");
Serial.print(*cnvipbuffPtr);
}
*/
*wrBufPtr++ = *cnvipbuffPtr++;
if (wrBufPtr == (wrbuffer + EEPROM_PAGE_SIZE)) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %04d writing wrbuffer 0x%08X to flash", hour(), minute(), second(), __LINE__, wrbuffer); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
// delay(25);
}
writeTFTPbuffer(wrbuffer, wrpageOffset, EEPROM_PAGE_SIZE);
wrpageOffset++;
wrBufPtr = wrbuffer;
}
}
if (idxip < 3) {
*wrBufPtr++ = 0x2E;
// Serial.print('.');
if (wrBufPtr == (wrbuffer + EEPROM_PAGE_SIZE)) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %04d writing wrbuffer 0x%08X to flash", hour(), minute(), second(), __LINE__, wrbuffer); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
// delay(25);
}
writeTFTPbuffer(wrbuffer, wrpageOffset, EEPROM_PAGE_SIZE);
wrpageOffset++;
wrBufPtr = wrbuffer;
}
}
}
recordcnt++;
*wrBufPtr++ = 0x0D;
if (wrBufPtr == (wrbuffer + EEPROM_PAGE_SIZE)) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d%04d writing wrbuffer 0x%08X to flash", hour(), minute(), second(), __LINE__, wrbuffer); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
// delay(25);
}
writeTFTPbuffer(wrbuffer, wrpageOffset, EEPROM_PAGE_SIZE);
wrpageOffset++;
wrBufPtr = wrbuffer;
}
*wrBufPtr++ = 0x0A;
if (wrBufPtr == (wrbuffer + EEPROM_PAGE_SIZE)) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %04d writing wrbuffer 0x%08X to flash", hour(), minute(), second(), __LINE__, wrbuffer); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
// delay(25);
}
writeTFTPbuffer(wrbuffer, wrpageOffset, EEPROM_PAGE_SIZE);
wrpageOffset++;
wrBufPtr = wrbuffer;
}
bufferPointer += 4;
// bufPtr += 16;
if (bufPtr == (rdbuffer + EEPROM_PAGE_SIZE)) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d INFO Flash read pagebreak detected, reading last Flash page for converted blacklisted HTTP fragments.", hour(), minute(), second()); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
flashaddr += EEPROM_PAGE_SIZE;
if (flashaddr == (abusefilter12_addr + EEPROM_PAGE_SIZE)) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %04d INFO end of flash pages for IPtable reached.", hour(), minute(), second(), __LINE__); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
return false;
}
flash.readByteArray(flashaddr, rdbuffer, EEPROM_PAGE_SIZE);
bufPtr = rdbuffer;
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: Flash read success address: 0x%08x", hour(), minute(), second(), flashaddr); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
}
}
// }
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %04d writing wrbuffer 0x%08X to flash", hour(), minute(), second(), __LINE__, wrbuffer); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
// delay(25);
}
writeTFTPbuffer(wrbuffer, wrpageOffset, (wrBufPtr - 1 - wrbuffer)); // write the remaining
return true;
}
bool prepareHTTP2TFTPbuffer() {
/*
rdbuffer is global buffer
bufPtr = global buffer pointer from rdbuffer
rxBufPtr = recordpointer in global buffer
wr
*/
uint8_t* rxBufPtr;
uint8_t wrbuffer[EEPROM_PAGE_SIZE]; // size of an EEPROM page
uint8_t wrpageOffset = 0;
uint8_t* wrBufPtr = wrbuffer;
uint16_t recordcnt = 0;
uint32_t flashaddr = HTTPblacklist1_addr;
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Start converting HTTP blacklist to ASCII text, preparation for TFTP download", hour(), minute(), second()); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
// delay(25);
flash.readByteArray(flashaddr, rdbuffer, EEPROM_PAGE_SIZE);
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: Flash read success address: 0x%08X", hour(), minute(), second(), flashaddr); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
bufPtr = rdbuffer;
while (*bufPtr != 0xFF) {
while (bufPtr < (rdbuffer + EEPROM_PAGE_SIZE) && (*bufPtr != 0xFF)) {
rxBufPtr = bufPtr; //temp pointer for reading this entry
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Start processing record %d", hour(), minute(), second(), recordcnt); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
// delay(5);
}
while (*rxBufPtr != 0) { //read record
*wrBufPtr++ = *rxBufPtr++;
if (wrBufPtr == (wrbuffer + EEPROM_PAGE_SIZE)) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %04d writing wrbuffer 0x%08X to flash", hour(), minute(), second(), __LINE__, wrbuffer); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
// delay(25);
}
writeTFTPbuffer(wrbuffer, wrpageOffset, EEPROM_PAGE_SIZE);
wrpageOffset++;
}
}
recordcnt++;
*wrBufPtr++ = 0x0D;
if (wrBufPtr == (wrbuffer + EEPROM_PAGE_SIZE)) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %04d writing wrbuffer 0x%08X to flash", hour(), minute(), second(), __LINE__, wrbuffer); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
// delay(25);
}
writeTFTPbuffer(wrbuffer, wrpageOffset, EEPROM_PAGE_SIZE);
wrpageOffset++;
}
*wrBufPtr++ = 0x0A;
if (wrBufPtr == (wrbuffer + EEPROM_PAGE_SIZE)) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %04d writing wrbuffer 0x%08X to flash", hour(), minute(), second(), __LINE__, wrbuffer); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
// delay(25);
}
writeTFTPbuffer(wrbuffer, wrpageOffset, EEPROM_PAGE_SIZE);
wrpageOffset++;
}
bufferPointer += 16;
bufPtr += 16;
if (bufPtr == (rdbuffer + EEPROM_PAGE_SIZE)) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d INFO Flash read pagebreak detected, reading last Flash page for converted blacklisted HTTP fragments.", hour(), minute(), second()); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
flashaddr += EEPROM_PAGE_SIZE;
if (flashaddr == (HTTPblacklist2_addr + EEPROM_PAGE_SIZE)) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d ERROR: Flash end of blacklist area reached.", hour(), minute(), second()); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
return false;
}
flash.readByteArray(flashaddr, rdbuffer, EEPROM_PAGE_SIZE);
bufPtr = rdbuffer;
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: Flash read success address: 0x%08X", hour(), minute(), second(), flashaddr); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
}
}
}
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %04d writing wrbuffer 0x%08X to flash", hour(), minute(), second(), __LINE__, wrbuffer); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
// delay(25);
}
writeTFTPbuffer(wrbuffer, wrpageOffset, (wrBufPtr - 1 - wrbuffer)); // write the remaining
return true;
}
/*
Read flash to prepare transfer the ASCII IPtable or HTTP blacklist back to a host
*/
void prepareTFTPPacket() {
// Serial.print("\r\nsingle byte read from 0x0x000B3000: ");
// Serial.print(flash.readByte(0xB3000));
uint32_t flashaddr = eepromEnd_addr + (pageOffset * EEPROM_PAGE_SIZE);
uint8_t bogusbyte = flash.readByte(flashaddr);
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %s %s:%04d INFO for bug fixing, read a single byte, single byte read from 0x%08X: 0x%02X", hour(), minute(), second(), FILENAME, __func__, __LINE__, eepromEnd_addr, bogusbyte);
textlog(temptxtbuff, false);
}
// sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Prepare and check readbuffer for sendTFTPPacket", hour(), minute(), second(), flashaddr, pageOffset); // temptxtbuff is max 1024
// textlog(temptxtbuff, false);
flash.readByteArray(flashaddr, rdbuffer, EEPROM_PAGE_SIZE);
bufPtr = rdbuffer;
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %s %s:%04d SUCCESS: Flash read success address: 0x%08X, pageOffset: %d, rdbuffer @bufPtr: 0x%08X: 0x%02X", hour(), minute(), second(), FILENAME, __func__, __LINE__, flashaddr, pageOffset, bufPtr, *bufPtr); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
pageOffset++;
}
/*
Load HTTP blacklist to memory
*/
void convertHTTPlist() {
bool rdpagebreak = false;
uint8_t bogusbyte;
uint8_t HTTPposcnt = 0; // counter for first free position in HTTP blacklisttextfragment
uint8_t outbuffer[EEPROM_PAGE_SIZE];
uint8_t* writePtr = outbuffer; //position in text outputarray
uint8_t* readPtr = rdbuffer; //position off scan point to detect a \r\n position in the textlist
uint8_t pageoffset = 0; // current memory page write in flash
uint8_t readoffset = 0;
uint32_t flashaddr = eepromEnd_addr;
uint32_t writeFileAddr;
char HTTPtext[17]; // HTTP blacklisttextfragment+0x00+0x0D, string is max 15 positions
totalHTTPcmd = 0;
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Start converting the HTTP text table to memory structure", hour(), minute(), second()); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
bogusbyte = flash.readByte(flashaddr); // to overcom a library bug
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d outbuffer base is: 0x%08X, actual writePtr in outbuffer: 0x%08X, offset in outbuffer is: 0x%08X", hour(), minute(), second(), outbuffer, writePtr, (writePtr - outbuffer)); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %s %s:%04d INFO: for bug fixing, read a single byte, single byte read from 0x%08X: 0x%02X", hour(), minute(), second(), FILENAME, __func__, __LINE__, eepromEnd_addr, bogusbyte);
textlog(temptxtbuff, false);
}
flash.readByteArray(flashaddr, rdbuffer, EEPROM_PAGE_SIZE);
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: Flash read success address: 0x%08X, sector: %d", hour(), minute(), second(), flashaddr, readoffset); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
while (*readPtr != 0xFF) {
while ((readPtr < (rdbuffer + EEPROM_PAGE_SIZE)) && (*readPtr != 0x0D)) {
if ((*readPtr != 0x0A) && (HTTPposcnt < 15)) {
HTTPtext[HTTPposcnt++] = (char) * readPtr++;
}
else {
readPtr++;
}
}
if (*readPtr == 0x0D) {
while (HTTPposcnt < 17) {
HTTPtext[HTTPposcnt++] = 0x00;
}
// sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: Content of HTTPtext array: %s", hour(), minute(), second(), HTTPtext); // temptxtbuff is max 1024
// textlog(temptxtbuff, false);
readPtr++; // pointer to next entry in case \r recordformat
if (*readPtr == 0x0A) {
readPtr++; // pointer to next entry in case \r\n recordformat
}
// check if we have a valid string
if ((HTTPtext[0] != '#') && (HTTPtext[0] != 0x00)) {
totalHTTPcmd++;
HTTPposcnt = 0;
while ( HTTPposcnt < 16) {
*writePtr++ = HTTPtext[HTTPposcnt++];
}
HTTPposcnt = 0; // reset for new string
if (writePtr == (outbuffer + EEPROM_PAGE_SIZE)) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Converted blacklisted HTTP fragments: %d", hour(), minute(), second(), totalHTTPcmd); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Adres of HTTPblacklist1_addr: 0x%08X, pageoffset: %d, EEPROM_PAGE_SIZE: 0x%08X", hour(), minute(), second(), HTTPblacklist1_addr, pageoffset, EEPROM_PAGE_SIZE); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
writeFileAddr = HTTPblacklist1_addr + (pageoffset * EEPROM_PAGE_SIZE);
flash.eraseSector(writeFileAddr);
delay(25);
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d first entry of blacklisted HTTP fragments in this converted buffer %s at address 0x%08X", hour(), minute(), second(), (char*)outbuffer, outbuffer);
textlog(temptxtbuff, false);
}
if (!flash.writeByteArray(writeFileAddr, outbuffer, EEPROM_PAGE_SIZE)) {
flash_error = F("writeByteArray writeFileAddr FAIL");
StoreFlashError(flash_error, writeFileAddr);
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d ERROR: Converted blacklisted HTTP fragments writing... writeByteArray writeFileAddr FAIL addres: 0x%08X", hour(), minute(), second(), writeFileAddr); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
return;
}
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: blacklisted HTTP fragments writeByteArray writeFileAddr success address: 0x%08X, sector: %d", hour(), minute(), second(), writeFileAddr, pageoffset); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
pageoffset++;
writePtr = outbuffer;
// sprintf(temptxtbuff, "\r\n%02d:%02d:%02d outbuffer base is: 0x%08X, writePtr: 0x%08X, offset address is: 0x%08X", hour(), minute(), second(), outbuffer, writePtr, (writePtr - outbuffer)); // temptxtbuff is max 1024
// textlog(temptxtbuff, false);
delay(3);
}
}
}
if (readPtr == (rdbuffer + EEPROM_PAGE_SIZE)) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d INFO Flash read pagebreak detected, already converted blacklisted HTTP fragments: %d (partial) readed content: %s", hour(), minute(), second(), totalHTTPcmd, HTTPtext); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
flashaddr += EEPROM_PAGE_SIZE;
readoffset++;
flash.readByteArray(flashaddr, rdbuffer, EEPROM_PAGE_SIZE);
readPtr = rdbuffer;
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: Flash read success address: 0x%08X, sector: %d", hour(), minute(), second(), flashaddr, readoffset); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
rdpagebreak = true;
}
}
if (writePtr != outbuffer) {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Total Converted blacklisted HTTP fragments: %d, remaining converted blacklisted HTTP fragments in outputbuffer: %d.", hour(), minute(), second(), totalHTTPcmd, ((writePtr - outbuffer) / 16)); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
writeFileAddr = HTTPblacklist1_addr + (pageoffset * EEPROM_PAGE_SIZE);
flash.eraseSector(writeFileAddr);
delay(25);
if (!flash.writeByteArray(writeFileAddr, outbuffer, (writePtr - outbuffer))) {
flash_error = F("writeByteArray writeFileAddr FAIL");
StoreFlashError(flash_error, writeFileAddr);
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d ERROR: Converted blacklisted HTTP fragments writing... writeByteArray writeFileAddr FAIL addres: 0x%08X", hour(), minute(), second(), writeFileAddr); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
return;
}
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: Converted blacklisted HTTP fragments writeByteArray writeFileAddr success address: 0x%08X, sector: %d", hour(), minute(), second(), writeFileAddr, pageoffset); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
delay(3);
}
writePtr -= 16;
HTTPposcnt = 0;
while (HTTPposcnt < 16) {
HTTPtext[HTTPposcnt++] = (char) * writePtr++;
}
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d HTTP fragments converted: %d, last content of buffer to convert: %s.", hour(), minute(), second(), totalHTTPcmd, HTTPtext); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
/*
read ipaddress and transform to binary table
*/
bool convertIPtable() {
uint8_t bogusbyte;
uint8_t outbuffer[EEPROM_PAGE_SIZE];
uint8_t* writePtr = outbuffer; //position in binary outputarray
uint8_t* readPtr = rdbuffer; //position off scan point to detect a \r\n position char of ipaddres in text
uint8_t pageoffset = 0; // current memory page write in flash
uint8_t readoffset = 0;
char ipaddrbuffer[17]; // 255.255.255.225+0x0D+0x00
uint8_t targetIP[4];
uint8_t ipposcnt = 0; // counter for first free position in ipaddrbuffer
uint32_t flashaddr = eepromEnd_addr;
uint32_t writeFileAddr;
bool rdpagebreak = false;
totalnets = 0;
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Start converting the text IPtable to binary table", hour(), minute(), second()); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
// for unknown reasons, probably timing a read of a byte from flash is necessary to work a reliable flash page read
bogusbyte = flash.readByte(flashaddr);
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d outbuffer base is: 0x%08X, writePtr: 0x%08X", hour(), minute(), second(), outbuffer, writePtr); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %s %s:%04d INFO: for bug fixing, read a single byte, single byte read from 0x%08X: 0x%02X, every value is allowed and ok", hour(), minute(), second(), FILENAME, __func__, __LINE__, flashaddr, bogusbyte);
textlog(temptxtbuff, false);
}
if (!flash.readByteArray(flashaddr, rdbuffer, EEPROM_PAGE_SIZE) ) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d ERROR: Flash read error address: 0x%08X, sector: %d to readbuffer 0x%08X", hour(), minute(), second(), flashaddr, readoffset, rdbuffer); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
else {
if (InExlog) {
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d SUCCESS: Flash read success address: 0x%08X, sector: %d to readbuffer 0x%08X", hour(), minute(), second(), flashaddr, readoffset, rdbuffer); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d readbuffer base is: 0x%08X, readPtr: 0x%08X", hour(), minute(), second(), rdbuffer, readPtr); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
}
while (*readPtr != 0xFF) {
while ((readPtr < (rdbuffer + EEPROM_PAGE_SIZE)) && (*readPtr != 0x0A)) {
if (ipposcnt > 16) { // 123.567.890.234d0
ipaddrbuffer[ipposcnt] = '\0';
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d ERROR Trying a write beyond array bound after net: %d failed content: %s, ipposcnt: %d", hour(), minute(), second(), totalnets, ipaddrbuffer, ipposcnt); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
return false;
}
if (((*readPtr >= 0x30) && (*readPtr <= 0x39)) || (*readPtr == '.') || (*readPtr == 0x0D)) {
ipaddrbuffer[ipposcnt] = *readPtr++;
ipposcnt++;
}
else {
ipaddrbuffer[ipposcnt] = '\0';
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %s %04d %s: WARNING: Input file contains illegal at about line %d readPtr: 0x%08X, character value: 0x%02X failed content: %s, ipposcnt: %d", hour(), minute(), second(),
FILENAME, __LINE__, __func__, totalnets + 1, readPtr, *readPtr, ipaddrbuffer, ipposcnt + 1); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
int verschil = (uint32_t)readPtr % 16;
show_buffer_Row2Serial(readPtr - verschil - 16, 48);
readPtr++;
// sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %04d readPtr after dump: 0x%08X", hour(), minute(), second(), __LINE__, readPtr); // temptxtbuff is max 1024
// textlog(temptxtbuff, false);
}
}
ipaddrbuffer[ipposcnt] = '\0';
// if ((totalnets > 677) && (totalnets < 679)) {
// sprintf(temptxtbuff, "\r\n%02d:%02d:%02d %04d net: %d ipaddrbuff: %s", hour(), minute(), second(), __LINE__, totalnets, ipaddrbuffer); // temptxtbuff is max 1024
// textlog(temptxtbuff, false);
// }
if (rdpagebreak) {
bool nltrue = false;
if (ipaddrbuffer[ipposcnt - 1] == 0x0D) {
// this is normal condition
//mark we have modified for output