-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
943 lines (849 loc) · 40.2 KB
/
main.c
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
/***************************************************************************
* Copyright (C) 2018-2020 D9ping
*
* IpAddressExpress 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.
*
* IpAddressExpress 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 IpAddressExpress. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <curl/curl.h>
#include <sqlite3.h>
#include "db.h"
#define PROGRAMNAME "IpAddressExpress"
#define PROGRAMVERSION "1.0.1"
#define PROGRAMWEBSITE " (+https://github.com/D9ping/IpAddressExpress)"
#define DATABASEFILENAME "ipaddressexpress.db"
#define CONFIGNAMEPREVIP "lastrunip"
#define CONFIGNAMELASTRUNDT "lastrundatetime"
#define SEED_LENGTH 32
#define IPV6_TEXT_LENGTH 34
#define MAXNUMSECDELAY 60
#define MAXSIZEIPADDRDOWNLOAD 20
#define MAXLENPATHPOSTHOOK 1023
#define MAXLENURL 1023
#define MAXSIZEAVOIDURLNRS 4096
#define MAXPRIORITY 9
#define MAXCHOOSERETRIES 8
#define PROTOCOLDNS 0
#define PROTOCOLHTTP 1
#define PROTOCOLHTTPS 2
typedef unsigned int uint;
struct Settings {
int secondsdelay;
int argnposthook;
int errorwait;
bool verbosemode;
bool silentmode;
bool retryposthook;
bool showip;
bool showlastrun;
bool savelastrun;
bool unsafehttp;
bool unsafedns;
bool tripleconfirm;
};
/**
* Get the current date and time in ISO8601 format without timezone.
* @return An character array of 19 bytes(without null terminator) with the current date and time.
*/
char * get_current_time_str(char *iso8601timebuf)
{
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(iso8601timebuf, 20, "%Y-%m-%d %H:%M:%S", timeinfo);
return iso8601timebuf;
}
/**
* Print time and date with an error message to stderr output.
* @param char[] errormsg
*/
void print_dt_error(char * errormsg)
{
char * iso8601timebuf = malloc(20 * sizeof(char));
iso8601timebuf = get_current_time_str(iso8601timebuf);
fprintf(stderr, "%s %s", iso8601timebuf, errormsg);
}
/**
* Print date and time and error message with an url to stderr output.
* @param char[] errormsgformat The format of the error message.
* @param char[] url The url added in the error message.
*/
void print_error_with_url(char * errormsgformat, const char * url)
{
char errormsg[2304];
int cw;
cw = snprintf(errormsg,
2304,
errormsgformat,
url);
if (cw >= 0 && cw <= 2304) {
print_dt_error(errormsg);
}
}
/**
* Print the different results from public ip address services.
*/
void print_detected_difference(char * ipaddrnow, char * ipaddrconfirm, const char * urlipservice, const char * confirmurl)
{
print_dt_error("Alert: one of the ip address services could have lied.\n");
fprintf(stderr, "IPv4: %s from %s.\n", ipaddrnow, urlipservice);
fprintf(stderr, "IPv4: %s from %s.\n", ipaddrconfirm, confirmurl);
}
/**
* Verify that a IPv4 address is valid by using inet_pton.
* @return 1 if character array is valid IPv4 address.
*/
int is_valid_ipv4_addr(char *ipv4addr)
{
struct sockaddr_in sa;
int result = inet_pton(AF_INET, ipv4addr, &(sa.sin_addr));
return result;
}
/**
* Get a new urlnr that is available to choice.
* @return A random number between 0 and maxurls.
*/
int get_new_random_urlnr(sqlite3 *db, bool unsafehttp, bool unsafedns, bool verbosemode, bool silentmode)
{
if (verbosemode) {
printf("Choose random urlnr.\n");
}
bool needaddlasturl = false;
int lasturlnr = get_config_value_int(db, "lasturlnr");
if (lasturlnr == -1) {
needaddlasturl = true;
}
// Initializes a random number generator using a seed from /dev/urandom */
FILE * rand_fd;
rand_fd = fopen("/dev/urandom", "r");
if (rand_fd == NULL) {
if (!silentmode) {
print_dt_error("Error: could not open /dev/urandom\n");
}
exit(EXIT_FAILURE);
}
int allowedprotocoltypes = 2;
if (unsafehttp) {
allowedprotocoltypes = 1;
} else if (unsafedns) {
allowedprotocoltypes = 0;
}
const int numavailableipservices = get_count_available_ipservices(db, allowedprotocoltypes);
int availableurlnrs[numavailableipservices];
get_urlnrs_ipservices(db, availableurlnrs, 0, allowedprotocoltypes);
uint *seed = (uint *) malloc(SEED_LENGTH * sizeof(uint));
int resreadrnd = fread(seed, sizeof(uint), SEED_LENGTH, rand_fd);
if (resreadrnd <= 0) {
if (!silentmode) {
print_dt_error("Warning: Could not read from /dev/urandom.\n");
}
}
srand(*seed);
fclose(rand_fd);
if (numavailableipservices <= 0) {
if (!silentmode) {
print_dt_error("Error: no more ipservices available.\n");
}
exit(EXIT_FAILURE);
}
// Choose a new random position in availableurlnrs. Between 0 and numavailableipservices.
int p = (rand() % numavailableipservices);
int urlnr = availableurlnrs[p];
if (numavailableipservices > 0 && !needaddlasturl)
{
int tries = 0;
while (urlnr == lasturlnr && tries < MAXCHOOSERETRIES)
{
p = (rand() % numavailableipservices);
urlnr = availableurlnrs[p];
++tries;
int priority = get_priority_ipservice(db, urlnr);
if (priority > 1 && priority <= MAXPRIORITY) {
if (rand() % priority != 0) {
if (verbosemode) {
printf("Choose different number again.\n");
}
p = (rand() % numavailableipservices);
urlnr = availableurlnrs[p];
}
}
}
if (urlnr == lasturlnr && tries == MAXCHOOSERETRIES && !silentmode) {
print_dt_error("Maximum number of retries to choice different urlnr has been reached.\n\
Could not avoid to use same urlnr as in last run.\n");
}
}
free(seed);
if (needaddlasturl) {
add_config_value_int(db, "lasturlnr", urlnr, verbosemode);
} else {
update_config_value_int(db, "lasturlnr", urlnr, verbosemode);
}
return urlnr;
}
/**
* Strip everything in the character array from the new line character till the end
* of the array. So str does not contain any new line character anymore.
*/
void strip_on_newlinechar(char *str, int strlength)
{
for (int i = 0; i < strlength; ++i) {
if (str[i] == '\n') {
// Added null character here so it's shorter just before first line feed.
str[i] = '\0';
return;
}
}
}
/**
* Read a text file with ip address.
* @return A character array with the ip address without a newline character.
*/
char * read_file_ipaddr(char *filepathip, bool silentmode)
{
size_t len = 0;
ssize_t bytesread;
FILE *fpfileip;
fpfileip = fopen(filepathip, "r");
if (fpfileip == NULL) {
if (!silentmode) {
print_dt_error("Error: Could not get public ip address from file.\n");
}
exit(EXIT_FAILURE);
}
char *ipaddrstr;
bytesread = getline(&ipaddrstr, &len, fpfileip);
// A hex text full out written IPv6 address is 34 characters +1 for null character.
if (bytesread > IPV6_TEXT_LENGTH) {
fclose(fpfileip);
if (!silentmode) {
print_dt_error("Error: response server too long.\n");
}
exit(EXIT_FAILURE);
}
fclose(fpfileip);
strip_on_newlinechar(ipaddrstr, bytesread);
return ipaddrstr;
}
/**
* Parse http status code.
*/
void parse_httpcode_status(int httpcode, sqlite3 *db, int urlnr)
{
char errmsg[128];
int cw;
switch (httpcode) {
case 420L:
case 429L:
case 503L:
case 509L:
print_dt_error("Warning: rate limiting active.\
Avoid the current public ip address service for some time.");
// Temporary disable
update_disabled_ipsevice(db, urlnr, true);
exit(EXIT_FAILURE);
case 408L:
case 500L:
case 502L:
case 504L:
cw = snprintf(errmsg,
128,
"Warning: used public ip address service has an error or other issue (http error: %d).\n",
httpcode);
if (cw >= 0 && cw <= 128) {
print_dt_error(errmsg);
}
// Temporary disable
update_disabled_ipsevice(db, urlnr, true);
break;
case 401L:
case 403L:
case 404L:
case 410L:
print_dt_error("Warning: the used public ip address service has quit or does not\
want automatic use.\nNever use this public ip address service again.\n");
// Disable forever
update_disabled_ipsevice(db, urlnr, false);
break;
case 301L:
case 302L:
case 308L:
cw = snprintf(errmsg,
128,
"Warning: public IP address service has changed url and is redirecting\
(http status code: %d).\n",
httpcode);
if (cw >= 0 && cw <= 128) {
print_dt_error(errmsg);
}
// Disable forever
update_disabled_ipsevice(db, urlnr, false);
break;
}
}
/**
* Download the ip address from a ipservice.
* @return ip address
*/
char * download_ipaddr_ipservice(char *ipaddr, const char *urlipservice, sqlite3 *db, int urlnr,
bool unsafehttp, bool silentmode, int * httpcodestatus)
{
//char * downloadtempfilepath;
char * downloadtempfilepath = malloc(16 * sizeof(char));
downloadtempfilepath = "/tmp/tempip.txt";
FILE *fpdownload;
// mode w+: truncates the file to zero length if it exists,
// otherwise creates a file if it does not exist.
fpdownload = fopen(downloadtempfilepath, "w+");
curl_global_init(CURL_GLOBAL_DEFAULT);
CURL * curlsession = curl_easy_init();
if (!curlsession || curlsession == NULL) {
if (!silentmode) {
print_dt_error("Error: should not setup cUrl session.\n");
}
curl_easy_cleanup(curlsession);
fclose(fpdownload);
exit(EXIT_FAILURE);
}
curl_easy_setopt(curlsession, CURLOPT_URL, urlipservice);
curl_easy_setopt(curlsession, CURLOPT_HTTPGET, 1L);
// Write to fpdownload
curl_easy_setopt(curlsession, CURLOPT_WRITEDATA, fpdownload);
// Default 300s, changed to max. 90 seconds to connect
curl_easy_setopt(curlsession, CURLOPT_CONNECTTIMEOUT, 90L);
// Default timeout is 0/never. changed to 90 seconds
curl_easy_setopt(curlsession, CURLOPT_TIMEOUT, 90L);
// Enable TLS false start. default disabled in curl, more testing needed.
//curl_easy_setopt(curlsession, CURLOPT_SSL_FALSESTART, 1L);
// Never follow redirects.
curl_easy_setopt(curlsession, CURLOPT_FOLLOWLOCATION, 0L);
curl_easy_setopt(curlsession, CURLOPT_MAXREDIRS, 0L);
// Make libcurl explicitly close the connection when done with the transfer.
curl_easy_setopt(curlsession, CURLOPT_FORBID_REUSE, 1L);
// limit the connection cache for this handle to no more than 3.
// Default 5
curl_easy_setopt(curlsession, CURLOPT_MAXCONNECTS, 3L);
// enable TCP keep-alive for this transfer
// Default 0
curl_easy_setopt(curlsession, CURLOPT_TCP_KEEPALIVE, 0L);
// TCP Fast Open: Default 0
// Beware: the TLS session cache does not work when TCP Fast Open is enabled.
// TCP Fast Open is also known to be problematic on or across certain networks.
//curl_easy_setopt(curlsession, CURLOPT_TCP_FASTOPEN, 0L);
// Resolve host name using IPv4-names only
curl_easy_setopt(curlsession, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
// Only support TLS 1.2 and later only.
curl_easy_setopt(curlsession, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
if (!unsafehttp) {
// Only allow https to be used.
curl_easy_setopt(curlsession, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
} else {
// Allow https and unsafe http.
curl_easy_setopt(curlsession, CURLOPT_PROTOCOLS,
CURLPROTO_HTTPS | CURLPROTO_HTTP);
}
char useragent[128];
strcpy(useragent, PROGRAMNAME);
strcat(useragent, "/");
strcat(useragent, PROGRAMVERSION);
strcat(useragent, PROGRAMWEBSITE);
curl_easy_setopt(curlsession, CURLOPT_USERAGENT, useragent);
// Perform the request, res will get the return code
CURLcode res;
res = curl_easy_perform(curlsession);
// Check for errors
if (res != CURLE_OK) {
if (!silentmode) {
char curlErr[1024];
int cw;
snprintf(curlErr,
1024,
"Error: %s (urlnr = %d)\n",
curl_easy_strerror(res), urlnr);
print_dt_error(curlErr);
}
curl_easy_cleanup(curlsession);
fclose(fpdownload);
switch (res) {
case CURLE_TOO_MANY_REDIRECTS:
case CURLE_REMOTE_ACCESS_DENIED:
case CURLE_URL_MALFORMAT:
case CURLE_REMOTE_FILE_NOT_FOUND:
case CURLE_SSL_CACERT:
update_disabled_ipsevice(db, urlnr, true);
break;
default:
// CURLE_OPERATION_TIMEDOUT, CURLE_GOT_NOTHING, CURLE_READ_ERROR, CURLE_RECV_ERROR,
// CURLE_COULDNT_CONNECT, CURLE_COULDNT_RESOLVE_HOST, CURLE_COULDNT_RESOLVE_PROXY,
// CURLE_WRITE_ERROR
update_disabled_ipsevice(db, urlnr, true);
break;
}
exit(EXIT_FAILURE);
}
int httpcode = 0;
curl_easy_getinfo(curlsession, CURLINFO_RESPONSE_CODE, &httpcode);
// Check the filelength of the downloaded file.
fclose(fpdownload);
fpdownload = fopen(downloadtempfilepath, "r");
fseek(fpdownload, 0L, SEEK_END);
unsigned long long int downloadedfilesize = ftell(fpdownload);
fclose(fpdownload);
// Cleanup curl.
curl_easy_cleanup(curlsession);
// Set HTTP status code.
*httpcodestatus = httpcode;
// Check filesize
if (downloadedfilesize == 0) {
if (!silentmode) {
char emptyFileErr[128];
snprintf(emptyFileErr,
128,
"Error: downloaded file is empty(urlnr = %d).\n",
urlnr);
print_dt_error(emptyFileErr);
}
// Temporary disable
update_disabled_ipsevice(db, urlnr, true);
exit(EXIT_FAILURE);
} else if (downloadedfilesize > MAXSIZEIPADDRDOWNLOAD) {
// Did not return only an ip address.
if (!silentmode) {
char responseTooBigErr[128];
snprintf(responseTooBigErr,
128,
"Error: response ip service(urlnr = %d) too big.\n",
urlnr);
print_dt_error(responseTooBigErr);
}
// Temporary disable
update_disabled_ipsevice(db, urlnr, true);
exit(EXIT_FAILURE);
}
ipaddr = read_file_ipaddr(downloadtempfilepath, silentmode);
return ipaddr;
}
/**
* Check if argumentval is a integer and return it, if not output error and exit the program.
*/
int read_commandline_argument_int_value(char * argumentval, bool silentmode)
{
// Check if argv[n] valid is integer
int lenargumentval = strlen(argumentval);
for (int i = 0; i < lenargumentval; ++i) {
if (!isdigit(argumentval[i])) {
if (!silentmode) {
print_dt_error("Error: invalid argument number value.\n");
}
exit(EXIT_FAILURE);
}
}
return atoi(argumentval);
}
/**
* Parse the command line arguments to the settings struct.
*/
struct Settings parse_commandline_args(int argc, char **argv, struct Settings settings)
{
bool argnumdelaysec = false;
bool argnumerrorwait = false;
bool argposthook = false;
// Parse command-line arguments and set settings struct.
for (int n = 1; n < argc; ++n) {
if (argnumdelaysec) {
argnumdelaysec = false;
settings.secondsdelay = read_commandline_argument_int_value(argv[n], settings.silentmode);
continue;
} else if (argnumerrorwait) {
argnumerrorwait = false;
settings.errorwait = read_commandline_argument_int_value(argv[n], settings.silentmode);
continue;
} else if (argposthook) {
argposthook = false;
// Check length posthook
if (strlen(argv[n]) > MAXLENPATHPOSTHOOK) {
if (!settings.silentmode) {
print_dt_error("Error: posthook command too long.\n");
}
exit(EXIT_FAILURE);
}
// For now, if multiple posthook's are provided
// override the command with last posthook command.
settings.argnposthook = n;
continue;
}
if (strcmp(argv[n], "--posthook") == 0) {
argposthook = true;
} else if (strcmp(argv[n], "--delay") == 0) {
argnumdelaysec = true;
} else if (strcmp(argv[n], "--errorwait") == 0) {
argnumerrorwait = true;
} else if (strcmp(argv[n], "--retryposthook") == 0) {
settings.retryposthook = true;
} else if (strcmp(argv[n], "--showip") == 0) {
settings.showip = true;
} else if (strcmp(argv[n], "--showlastrun") == 0) {
settings.showlastrun = true;
} else if (strcmp(argv[n], "--nosavelastrun") == 0) {
settings.savelastrun = false;
} else if (strcmp(argv[n], "--unsafedns") == 0) {
settings.unsafedns = true;
} else if (strcmp(argv[n], "--unsafehttp") == 0) {
settings.unsafehttp = true;
} else if (strcmp(argv[n], "--tripleconfirm") == 0) {
settings.tripleconfirm = true;
} else if (strcmp(argv[n], "--failsilent") == 0) {
settings.silentmode = true;
} else if (strcmp(argv[n], "--version") == 0) {
printf("%s %s\n", PROGRAMNAME, PROGRAMVERSION);
exit(EXIT_SUCCESS);
} else if (strcmp(argv[n], "-v") == 0 || strcmp(argv[n], "--verbose") == 0) {
settings.verbosemode = true;
} else if (strcmp(argv[n], "-h") == 0 || strcmp(argv[n], "--help") == 0) {
printf("--posthook The script or program to run on public IPv4 address change.\n\
Put the command between quotes. Spaces in path are currently not supported.\n");
printf("--retryposthook Rerun posthook on next run if posthook command \n\
did not return 0 as exit code.\n");
printf("--showip Always print the currently confirmed public IPv4 address.\n");
printf("--showlastrun Show the last date and time %s has been runnend\
and directly exit.\n", PROGRAMNAME);
printf("--nosavelastrun Don't save the date and time of current run.\n");
printf("--unsafehttp Allow the use of http public ip services, no TLS/SSL.\n");
printf("--delay 1-59 Delay the execution of this program with X number of seconds.\n");
printf("--errorwait n The number of seconds that an ipservice that causes a temporary error\n");
printf(" has to wait before it being able to be used again.\n");
int errorwaithours = settings.errorwait / 3600;
printf(" By default %d seconds (%d hours).\n", settings.errorwait, errorwaithours);
printf("--failsilent Fail silently do not print issues to stderr.\n");
printf("--tripleconfirm Confirm ip address change with a additional third ip service.\n");
printf("--version Print the version of this program and exit.\n");
printf("-v --verbose Run in verbose mode, output what this program does.\n");
printf("-h --help Print this help message.\n");
exit(EXIT_SUCCESS);
} else if (!settings.silentmode) {
fprintf(stderr, "Ignore unknown argument \"%s\".\n", argv[n]);
}
}
return settings;
}
int main(int argc, char **argv)
{
struct Settings settings;
// Set default values:
settings.secondsdelay = 0;
settings.argnposthook = 0;
settings.errorwait = 14400; // 4 hours
settings.retryposthook = false;
settings.unsafehttp = false;
settings.unsafedns = false;
settings.tripleconfirm = false;
settings.showip = false;
settings.silentmode = false;
settings.verbosemode = false;
settings.showlastrun = false;
settings.savelastrun = true;
settings = parse_commandline_args(argc, argv, settings);
if (settings.secondsdelay > 0 && settings.secondsdelay < 60) {
if (settings.verbosemode) {
printf("Delay %d seconds.\n", settings.secondsdelay);
}
sleep(settings.secondsdelay);
} else if (settings.secondsdelay >= MAXNUMSECDELAY && !settings.silentmode) {
char errormsg[128];
int cw;
cw = snprintf(errormsg,
128,
"Ignoring secondsdelay. secondsdelay has to be less than %d seconds.\n",
MAXNUMSECDELAY);
if (cw >= 0 && cw <= 128) {
print_dt_error(errormsg);
}
}
bool dbsetup = false;
if (access(DATABASEFILENAME, F_OK) == -1 ) {
dbsetup = true;
if (settings.verbosemode) {
printf("%s does not exists. Creating %s.\n", DATABASEFILENAME, DATABASEFILENAME);
}
}
/* Setup database connection */
sqlite3 *db;
int rc;
rc = sqlite3_open(DATABASEFILENAME, &db);
if (rc) {
print_dt_error("Can't open database file.\n");
exit(EXIT_FAILURE);
} else if (settings.verbosemode) {
printf("Opened database successfully.\n");
}
if (dbsetup) {
create_table_ipservice(db, settings.verbosemode);
create_table_config(db, settings.verbosemode);
add_config_value_int(db, "lasturlnr", -2, settings.verbosemode);
/* added default ipservice records */
add_ipservice(db, 0, "https://ipinfo.io/ip", false, PROTOCOLHTTPS, 1, settings.verbosemode);
add_ipservice(db, 1, "https://api.ipify.org/?format=text", false, PROTOCOLHTTPS, 1, settings.verbosemode);
add_ipservice(db, 2, "https://wtfismyip.com/text", false, PROTOCOLHTTPS, 1, settings.verbosemode);
add_ipservice(db, 3, "https://v4.ident.me/", false, PROTOCOLHTTPS, 1, settings.verbosemode);
add_ipservice(db, 4, "https://ipv4.icanhazip.com/", false, PROTOCOLHTTPS, 1, settings.verbosemode);
add_ipservice(db, 5, "https://checkip.amazonaws.com/", false, PROTOCOLHTTPS, 1, settings.verbosemode);
// Dns does not response anymore, api seems gone
add_ipservice(db, 6, "https://bot.whatismyipaddress.com/", true, PROTOCOLHTTPS, 2, settings.verbosemode);
add_ipservice(db, 7, "https://secure.informaction.com/ipecho/", false, PROTOCOLHTTPS, 1, settings.verbosemode);
add_ipservice(db, 8, "https://l2.io/ip", false, PROTOCOLHTTPS, 1, settings.verbosemode);
add_ipservice(db, 9, "https://www.trackip.net/ip", false, PROTOCOLHTTPS, 1, settings.verbosemode);
// Has https connect error.
add_ipservice(db, 10, "https://ip4.seeip.org/", true, PROTOCOLHTTPS, 2, settings.verbosemode);
// Page not found:
add_ipservice(db, 11, "https://locate.now.sh/ip", true, PROTOCOLHTTPS, 2, settings.verbosemode);
// Response returns more than only IPv4 address now.
add_ipservice(db, 12, "https://tnx.nl/ip", true, PROTOCOLHTTPS, 2, settings.verbosemode);
// Page not found:
add_ipservice(db, 13, "https://diagnostic.opendns.com/myip", true, PROTOCOLHTTPS, 2, settings.verbosemode);
// Can give: 429 error
add_ipservice(db, 14, "http://myip.dnsomatic.com/", false, PROTOCOLHTTP, 2, settings.verbosemode);
add_ipservice(db, 15, "http://whatismyip.akamai.com/", false, PROTOCOLHTTP, 1, settings.verbosemode);
add_ipservice(db, 16, "http://myexternalip.com/raw", false, PROTOCOLHTTP, 1, settings.verbosemode);
add_ipservice(db, 17, "https://ipecho.net/plain", false, PROTOCOLHTTPS, 1, settings.verbosemode);
// Error page:
add_ipservice(db, 18, "http://plain-text-ip.com/", true, PROTOCOLHTTP, 2, settings.verbosemode);
}
if (settings.showlastrun) {
char *lastrundt;
lastrundt = get_config_value_str(db, CONFIGNAMELASTRUNDT);
if (settings.verbosemode) {
printf("Last run on: ");
}
printf("%s\n", lastrundt);
exit(EXIT_SUCCESS);
}
int num_all_urls = get_count_all_ipservices(db);
if (num_all_urls < 2) {
if (!settings.silentmode) {
print_dt_error("No enough ipservices added to the database. Need at least 2 ipservices.\n");
exit(EXIT_FAILURE);
}
}
int urlnr = get_new_random_urlnr(db, settings.unsafehttp, settings.unsafedns,
settings.verbosemode, settings.silentmode);
const char *urlipservice;
urlipservice = get_url_ipservice(db, urlnr);
if (settings.verbosemode) {
printf("Using %s for getting public IPv4 address.\n", urlipservice);
}
int httpcodestatus = 0;
char * ipaddrnow; // FIXME: uninitialized variable ipaddrnow
ipaddrnow = download_ipaddr_ipservice(ipaddrnow,
urlipservice,
db,
urlnr,
settings.unsafehttp,
settings.silentmode,
&httpcodestatus);
parse_httpcode_status(httpcodestatus, db, urlnr);
if (is_valid_ipv4_addr(ipaddrnow) != 1) {
free(ipaddrnow);
if (!settings.silentmode) {
print_error_with_url("Error: invalid IPv4 address from '%s'.\n",
urlipservice);
}
exit(EXIT_FAILURE);
}
char *ipaddrconfirm;
if (is_config_exists(db, CONFIGNAMEPREVIP) == true) {
ipaddrconfirm = get_config_value_str(db, CONFIGNAMEPREVIP);
} else {
if (settings.verbosemode) {
printf("First run of %s.\n", PROGRAMNAME);
}
const char *confirmurl;
urlnr = get_new_random_urlnr(db, settings.unsafehttp, settings.unsafedns,
settings.verbosemode, settings.silentmode);
confirmurl = get_url_ipservice(db, urlnr);
if (settings.verbosemode) {
printf("Using %s to confirm current public IPv4 address with.\n", confirmurl);
}
int httpcodeconfirm = -1;
// FIXME: uninitialized variable ipaddrconfirm
ipaddrconfirm = download_ipaddr_ipservice(ipaddrconfirm,
confirmurl,
db,
urlnr,
settings.unsafehttp,
settings.silentmode,
&httpcodeconfirm);
parse_httpcode_status(httpcodeconfirm, db, urlnr);
if (is_valid_ipv4_addr(ipaddrconfirm) != 1) {
free(ipaddrconfirm);
if (!settings.silentmode) {
print_error_with_url("Error: invalid IPv4 address for first run confirmation from %s.\n",
confirmurl);
}
exit(EXIT_FAILURE);
}
// Check for ip address difference between the two requested services on first run.
if (strcmp(ipaddrnow, ipaddrconfirm) != 0) {
if (!settings.silentmode) {
print_detected_difference(ipaddrnow, ipaddrconfirm, urlipservice, confirmurl);
printf("Try getting current public IPv4 address again on next run.\n");
}
exit(EXIT_FAILURE);
}
}
if (settings.savelastrun) {
char * lastrundt = malloc(20 * sizeof(char));
lastrundt = get_current_time_str(lastrundt);
if (is_config_exists(db, CONFIGNAMELASTRUNDT)) {
update_config_value_str(db, CONFIGNAMELASTRUNDT, lastrundt, settings.verbosemode);
} else {
add_config_value_str(db, CONFIGNAMELASTRUNDT, lastrundt, settings.verbosemode);
}
}
if (strcmp(ipaddrnow, ipaddrconfirm) != 0) {
// Ip address has changed.
if (settings.verbosemode) {
printf("Public ip change detected, IPv4 address different from last\
run.\n");
}
int additionalconfirmruns = 1;
if (settings.tripleconfirm) {
additionalconfirmruns = 2;
}
for (int i = 0; i < additionalconfirmruns; ++i) {
urlnr = get_new_random_urlnr(db,
settings.unsafehttp,
settings.unsafedns,
settings.verbosemode,
settings.silentmode);
const char *confirmchangeurl;
confirmchangeurl = get_url_ipservice(db, urlnr);
if (settings.verbosemode) {
printf("Ipservice %s is used to confirm public IPv4 address.\n", confirmchangeurl);
}
char *ipaddrconfirmchange;
int httpcodeconfirmchange = -2;
// FIXME: uninitialized variable ipaddrconfirmchange
ipaddrconfirmchange = download_ipaddr_ipservice(ipaddrconfirmchange,
confirmchangeurl,
db,
urlnr,
settings.unsafehttp,
settings.silentmode,
&httpcodeconfirmchange);
parse_httpcode_status(httpcodeconfirmchange, db, urlnr);
if (is_valid_ipv4_addr(ipaddrconfirmchange) != 1) {
if (!settings.silentmode) {
print_error_with_url("Error: invalid IP(IPv4) address returned from confirm ipservice: %s\n",
confirmchangeurl);
}
if (settings.showip) {
// Show old valid ip address.
printf("%s", ipaddrconfirm);
}
exit(EXIT_FAILURE);
}
// Check if new ip address with different service is the same new ip address.
if (strcmp(ipaddrnow, ipaddrconfirmchange) != 0) {
if (!settings.silentmode) {
print_detected_difference(ipaddrnow,
ipaddrconfirmchange,
urlipservice,
confirmchangeurl);
print_dt_error("It's now unknown if public ip address has actually changed.\n");
}
if (settings.showip) {
// Show old ip address.
printf("%s", ipaddrconfirm);
}
exit(EXIT_FAILURE);
}
}
if (settings.verbosemode) {
printf("Execute posthook command with \"%s\" as argument.\n", ipaddrnow);
}
if (settings.argnposthook <= 1) {
if (!settings.silentmode) {
print_dt_error("Error: no posthook provided.\n");
if (settings.showip) {
// Show current ip address.
printf("%s", ipaddrnow);
}
}
exit(EXIT_FAILURE);
}
char cmdposthook[MAXLENPATHPOSTHOOK + 1];
if (strchr(ipaddrnow, '"') != NULL) {
if (!settings.silentmode) {
print_dt_error("Error: quote(s) in ip address.\n");
}
exit(EXIT_FAILURE);
} else if (strchr(argv[settings.argnposthook], '"') != NULL) {
if (!settings.silentmode) {
print_dt_error("Error: quote in posthook command.\n");
}
exit(EXIT_FAILURE);
}
snprintf(cmdposthook, MAXLENPATHPOSTHOOK, "\"%s\" \"%s\"",
argv[settings.argnposthook], ipaddrnow);
/* printf("cmdposthook = [ %s ]\n", cmdposthook); // DEBUG */
unsigned short exitcode = system(cmdposthook);
if (exitcode != 0) {
if (!settings.silentmode) {
char errormsg[64];
int cw;
cw = snprintf(errormsg,
64,
"Error: script returned error (exitcode %hu).\n",
exitcode);
if (cw >= 0 && cw <= 64) {
print_dt_error(errormsg);
}
}
if (settings.retryposthook) {
if (settings.showip) {
// Do show new ip address.
printf("%s", ipaddrnow);
}
// Run posthook next time again because CONFIGNAMEPREVIP is
// not updated to new ip in database.
exit(EXIT_FAILURE);
}
}
if (is_config_exists(db, CONFIGNAMEPREVIP) == true) {
update_config_value_str(db, CONFIGNAMEPREVIP, ipaddrnow, settings.verbosemode);
} else {
add_config_value_str(db, CONFIGNAMEPREVIP, ipaddrnow, settings.verbosemode);
}
} else if (settings.verbosemode) {
printf("The current public ip is the same as the public ip from last ipservice.\n");
if (is_config_exists(db, CONFIGNAMEPREVIP) == false) {
// Readded missing CONFIGNAMEPREVIP config value.
add_config_value_str(db, CONFIGNAMEPREVIP, ipaddrnow, settings.verbosemode);
}
}
if (settings.showip) {
// Show current ip address.
printf("%s", ipaddrnow);
}
reenable_expired_disabled_ipservices(db, settings.errorwait);
return EXIT_SUCCESS;
}