• R/O
  • SSH
  • HTTPS

ogup: 提交


Commit MetaInfo

修訂51 (tree)
時間2021-12-11 19:37:06
作者mateuszviste

Log Message

lazy ipv4 scanning when nothing else to do

Change Summary

差異

--- trunk/gopherjoker/gopherjoker.c (revision 50)
+++ trunk/gopherjoker/gopherjoker.c (revision 51)
@@ -1,7 +1,8 @@
11 /*
22 * gopherjoker, part of the Observable Gopherspace Universe Project
3- * Copyright (C) 2019-2021 Mateusz Viste
3+ * Copyright (C) 2019-2022 Mateusz Viste
44 *
5+ * 2021-12-11: lazy scanning of the IPv4 space when time allows (--scanipv4)
56 * 2021-12-09: a /tmp/ogup/... submission file may contain many entries
67 * 2021-12-07: write last good IP address to database for every host
78 * 2021-12-06: scheduler for probing servers + internal cache of recent checks
@@ -39,7 +40,7 @@
3940 #include <dirent.h> /* opendir() */
4041 #include <errno.h>
4142 #include <stdio.h> /* printf(), fopen(), ... */
42-#include <stdlib.h> /* calloc(), rand() */
43+#include <stdlib.h> /* calloc(), rand48() */
4344 #include <string.h> /* strcpy() */
4445 #include <time.h> /* ctime(), time_t */
4546 #include <unistd.h> /* sleep() */
@@ -60,6 +61,7 @@
6061
6162 static int WAITPERIOD = WAITPERIOD_DEFAULT;
6263 static int SAVEPERIOD = SAVEPERIOD_DEFAULT;
64+static int SCANIPV4SPACE = 0;
6365
6466
6567 /**************** FUNCTIONS ****************/
@@ -94,6 +96,8 @@
9496 fprintf(stderr, "ERROR: --waitperiod must be a positive integer\n");
9597 return(-1);
9698 }
99+ } else if (strcasecmp(argv[i], "--scanipv4") == 0) {
100+ SCANIPV4SPACE = 1;
97101 } else if ((argv[i][0] != '-') && ((*dbfile == NULL) || (*dbcount == NULL))) {
98102 if (*dbfile == NULL) {
99103 *dbfile = argv[i];
@@ -279,7 +283,7 @@
279283 for (gptr = glist; gptr != NULL; gptr = gptr->next) cnt++;
280284
281285 /* random entry id */
282- i = (unsigned long)rand() % cnt;
286+ i = (unsigned long)mrand48() % cnt;
283287
284288 /* fast-forward to the given entry and return it */
285289 while (i--) glist = glist->next;
@@ -437,6 +441,40 @@
437441 }
438442
439443
444+/* provides IPv4 address to scan when --scanipv4 enabled */
445+static void pick_an_ipv4_address_to_scan(struct gopherlist **glist) {
446+ static uint32_t lastip;
447+ static int firstrun = 1;
448+ char addr[16]; /* 255.255.255.255 is 15 bytes long (+1 for NULL terminator) */
449+ unsigned short runs = 10; /* add that many IP addresses */
450+
451+ /* if first time, start from a random position */
452+ if (firstrun) {
453+ firstrun = 0;
454+ lastip = (uint32_t)mrand48();
455+ }
456+
457+ while (runs-- != 0) {
458+ lastip++;
459+
460+ /* some ranges must be skipped */
461+ if ((lastip & 0xff000000lu) == 0) lastip = 0x01000000lu; /* 0.x.x.x -> 1.0.0.0 */
462+ if ((lastip & 0xff000000lu) == 0x0A000000lu) lastip = 0x0B000000lu; /* 10.x.x.x -> 11.0.0.0 */
463+ if ((lastip & 0xff000000lu) == 0x7F000000lu) lastip = 0x80000000lu; /* 127.x.x.x -> 128.0.0.0 */
464+ if ((lastip & 0xfff00000lu) == 0xAC100000lu) lastip = 0xAC200000lu; /* 172.[16-31].x.x -> 172.32.0.0 */
465+ if ((lastip & 0xffff0000lu) == 0xC0A80000lu) lastip = 0xC0A90000lu; /* 192.168.x.x -> 192.169.0.0 */
466+ if ((lastip >> 24) >= 224) lastip = 0x01000000lu; /* 224.0.0.0 - 255.255.255.255 -> 1.0.0.0 */
467+
468+ /* generate IP addr string */
469+ sprintf(addr, "%u.%u.%u.%u", (lastip >> 24) & 0xff, (lastip >> 16) & 0xff, (lastip >> 8) & 0xff, lastip & 0xff);
470+ printf("ipv4scan picked ip %s\n", addr);
471+
472+ /* insert glist entry */
473+ glist_addnewhostport(glist, addr, 70, 1);
474+ }
475+}
476+
477+
440478 /**************** MAIN ****************/
441479
442480 int main(int argc, char **argv) {
@@ -456,6 +494,7 @@
456494 "options:\n"
457495 "--waitperiod p sets delay period between spidering actions to p seconds (default=%d)\n"
458496 "--saveperiod p sets save frequency of the database file to p seconds (default=%d)\n"
497+ "--scanipv4 performs IPv4 address space scanning when there is nothing else to do\n"
459498 "\n",
460499 WAITPERIOD_DEFAULT,
461500 SAVEPERIOD_DEFAULT
@@ -469,7 +508,7 @@
469508 glist = loaddb(dbfile);
470509
471510 /* init random engine */
472- srand((unsigned int)time(NULL));
511+ srand48((long)time(NULL));
473512
474513 for (;;) {
475514
@@ -500,7 +539,11 @@
500539 curhost = pickhostfromlist(glist);
501540 if (curhost == NULL) {
502541 printf("no hosts to process. you can add some through " NEWHOSTSDIR ".\n");
503- sleep(NOJOB_SLEEP);
542+ if (SCANIPV4SPACE != 0) {
543+ pick_an_ipv4_address_to_scan(&glist);
544+ } else {
545+ sleep(NOJOB_SLEEP);
546+ }
504547 continue;
505548 }
506549 curhost->nextcheck = time(NULL) + CHECKPERIOD;
Show on old repository browser