Révision | 20 (tree) |
---|---|
l'heure | 2021-12-04 01:12:22 |
Auteur | mateuszviste |
new hosts can be submitted through /tmp/ogup/
@@ -2,6 +2,7 @@ | ||
2 | 2 | * gopherjoker, part of the Observable Gopherspace Universe Project |
3 | 3 | * Copyright (C) 2019-2021 Mateusz Viste |
4 | 4 | * |
5 | + * 2021-12-03: new hosts can be fed to gopherjoker through /tmp/ogup/ | |
5 | 6 | * 2020-01-13: added recent history so joker won't revisit places too often |
6 | 7 | * 2020-01-13: variety of minor fixes and casts to shut clang warnings |
7 | 8 | * 2019-03-09: hosts with no menu entries are kept in db - but as IP only |
@@ -33,6 +34,7 @@ | ||
33 | 34 | |
34 | 35 | #include <arpa/inet.h> /* inet_pton() */ |
35 | 36 | #include <ctype.h> /* tolower() */ |
37 | +#include <dirent.h> /* opendir() */ | |
36 | 38 | #include <errno.h> |
37 | 39 | #include <fcntl.h> |
38 | 40 | #include <netdb.h> /* gethostbyname() */ |
@@ -57,7 +59,7 @@ | ||
57 | 59 | |
58 | 60 | #define WAITPERIOD 20 |
59 | 61 | #define SAVEPERIOD 3600 |
60 | -#define NEWHOSTSLIST "/tmp/gjoker_newhosts.csv" | |
62 | +#define NEWHOSTSDIR "/tmp/ogup/" | |
61 | 63 | #define MAXFAILTIME (3600 * 24 * 60) |
62 | 64 | #define TTLINIT 64 |
63 | 65 |
@@ -148,6 +150,7 @@ | ||
148 | 150 | return(-1); |
149 | 151 | } |
150 | 152 | } |
153 | + if ((*dbfile == NULL) || (*dbcount == NULL)) return(-2); | |
151 | 154 | return(0); |
152 | 155 | } |
153 | 156 |
@@ -253,20 +256,80 @@ | ||
253 | 256 | } |
254 | 257 | |
255 | 258 | |
256 | -static struct gopherlist *loadextrahosts(struct gopherlist *glist, const char *fname, unsigned long *glistlen) { | |
259 | +/* open fname and read a server name from it, write it to *s. returns | |
260 | + * port number on success, neg value on error */ | |
261 | +static int get_server_from_file(char *s, size_t ssz, const char *fname) { | |
257 | 262 | FILE *f; |
258 | - char *ptrs[2]; | |
259 | - char buff[64]; | |
263 | + int port = 70; | |
264 | + int i; | |
265 | + | |
266 | + /* open file */ | |
260 | 267 | f = fopen(fname, "rb"); |
261 | - if (f == NULL) return(glist); | |
268 | + if (f == NULL) { | |
269 | + printf("ERROR: failed to open file %s (%s)\r\n", fname, strerror(errno)); | |
270 | + return(-1); | |
271 | + } | |
262 | 272 | |
263 | - while (csvreadline(buff, sizeof(buff), ptrs, 2, f) == 0) { | |
264 | - int port = atoi(ptrs[1]); | |
265 | - if ((port < 1) || (port > 0xffff)) port = 70; | |
266 | - if (glist_addnewhost(&glist, glistlen, ptrs[0], (unsigned short)port, 0) == NULL) break; | |
273 | + /* read server string */ | |
274 | + if (fgets(s, (int)ssz, f) == NULL) { | |
275 | + printf("read failure from %s: %s\n", fname, strerror(errno)); | |
276 | + fclose(f); | |
277 | + return(-1); | |
267 | 278 | } |
268 | 279 | fclose(f); |
269 | 280 | |
281 | + /* parse (look for a ':') */ | |
282 | + for (i = 0; s[i] != 0; s++) { | |
283 | + if ((s[i] == '\r') || (s[i] == '\n')) { | |
284 | + s[i] = 0; | |
285 | + break; | |
286 | + } | |
287 | + if (s[i] == ':') { | |
288 | + s[i++] = 0; | |
289 | + port = atoi(s + i); | |
290 | + break; | |
291 | + } | |
292 | + } | |
293 | + | |
294 | + if ((port < 1) || (port > 65535)) { | |
295 | + printf("invalid port: host=%s port=%d\n", s, port); | |
296 | + return(-1); | |
297 | + } | |
298 | + | |
299 | + return(port); | |
300 | +} | |
301 | + | |
302 | + | |
303 | +/* insert any newly-subnmitted servers to memory database (and remove the new host file) */ | |
304 | +static struct gopherlist *loadextrahosts(struct gopherlist *glist, const char *dir, unsigned long *glistlen) { | |
305 | + char fname[128]; | |
306 | + char buff[128]; | |
307 | + DIR *d; | |
308 | + struct dirent *dptr; | |
309 | + int port; | |
310 | + | |
311 | + d = opendir(dir); | |
312 | + if (d == NULL) return(glist); | |
313 | + | |
314 | + while ((dptr = readdir(d)) != NULL) { | |
315 | + if (dptr->d_type != DT_REG) continue; /* skip anything that's not a regular file (includes symlinks) */ | |
316 | + strcpy(fname, dir); | |
317 | + strcat(fname, "/"); | |
318 | + strcat(fname, dptr->d_name); | |
319 | + printf("loading new host file: %s\n", buff); | |
320 | + port = get_server_from_file(buff, sizeof(buff), fname); | |
321 | + if (port > 0) { | |
322 | + if (glist_addnewhost(&glist, glistlen, buff, (unsigned short)port, 0) != NULL) { | |
323 | + printf("added host: %s:%d\r\n", buff, port); | |
324 | + } else { | |
325 | + puts("glist_addnewhost() call failed"); | |
326 | + } | |
327 | + } | |
328 | + /* remove file and go to next one */ | |
329 | + unlink(fname); | |
330 | + } | |
331 | + closedir(d); | |
332 | + | |
270 | 333 | return(glist); |
271 | 334 | } |
272 | 335 |
@@ -535,7 +598,7 @@ | ||
535 | 598 | unsigned long i; |
536 | 599 | |
537 | 600 | if (parseargs(argc, argv, &dbfile, &dbfilecnt) != 0) { |
538 | - printf("usage: gopherjoker dbfile.csv dbcount.txt\n"); | |
601 | + printf("usage: gopherjoker dbfile.csv dbcount.csv\n"); | |
539 | 602 | return(1); |
540 | 603 | } |
541 | 604 |
@@ -554,7 +617,7 @@ | ||
554 | 617 | nextaction = time(NULL) + WAITPERIOD; |
555 | 618 | |
556 | 619 | /* if extra manual hosts required to be added, do it now */ |
557 | - glist = loadextrahosts(glist, NEWHOSTSLIST, &glistlen); | |
620 | + glist = loadextrahosts(glist, NEWHOSTSDIR, &glistlen); | |
558 | 621 | |
559 | 622 | /* save the db once every hour */ |
560 | 623 | if (time(NULL) > nextdbsave) { |
@@ -579,7 +642,7 @@ | ||
579 | 642 | |
580 | 643 | /* if no host, continue */ |
581 | 644 | if (curhost == NULL) { |
582 | - printf("hosts list empty. add some through " NEWHOSTSLIST ".\n"); | |
645 | + printf("hosts list empty. add some through " NEWHOSTSDIR ".\n"); | |
583 | 646 | continue; |
584 | 647 | } else { |
585 | 648 | printf("fetching %s:%u/1%s ...\n", curhost->fqdn, curhost->port, curhost->selector); |
@@ -613,9 +676,9 @@ | ||
613 | 676 | mlist = menu2gopherlist(buff, bufflen); |
614 | 677 | if (mlist == NULL) { |
615 | 678 | printf("ERR: no entries found in menu\n"); |
616 | - /* if no '1' menu entries at all AND this was its main menu AND I got | |
617 | - * at least 64 bytes of data, then add server's IP address */ | |
618 | - if ((curhost->selector == NULL) && (bufflen >= 64)) { | |
679 | + /* if no '1' menu entries at all AND this was main menu AND got at least | |
680 | + * 64 bytes of data AND it port=70, then add server's IP address */ | |
681 | + if ((curhost->selector == NULL) && (bufflen >= 64) && (curhost->port == 70)) { | |
619 | 682 | printf(" (keeping server in list as %s)\n", curhost_ipaddr); |
620 | 683 | glist_addnewhost(&glist, &glistlen, curhost_ipaddr, curhost->port, 0); |
621 | 684 | } |