• R/O
  • SSH
  • HTTPS

ogup: Commit


Commit MetaInfo

Revision50 (tree)
Zeit2021-12-10 07:01:24
Autormateuszviste

Log Message

a serverlist submission file may contain multiple entries now so it is easier to peform batch insertion of lists of gopher servers

Ändern Zusammenfassung

Diff

--- trunk/gopherjoker/gopherjoker.c (revision 49)
+++ trunk/gopherjoker/gopherjoker.c (revision 50)
@@ -2,6 +2,7 @@
22 * gopherjoker, part of the Observable Gopherspace Universe Project
33 * Copyright (C) 2019-2021 Mateusz Viste
44 *
5+ * 2021-12-09: a /tmp/ogup/... submission file may contain many entries
56 * 2021-12-07: write last good IP address to database for every host
67 * 2021-12-06: scheduler for probing servers + internal cache of recent checks
78 * 2021-12-04: added --saveperiod and --waitperiod cmdline parameters
@@ -163,59 +164,66 @@
163164
164165
165166 /* open fname and read a server name from it, write it to *s. returns
166- * port number on success, neg value on error */
167-static int get_server_from_file(char *s, size_t ssz, const char *fname) {
167+ * port number on success, neg value on error
168+ * TODO support for IPv6 addresses like [2a60:1234::1]:71
169+ */
170+static void process_serverlist_file(struct gopherlist **glist, const char *fname) {
168171 FILE *f;
169- int port = 70;
172+ int port;
170173 int i;
174+ char s[128];
171175
172176 /* open file */
173177 f = fopen(fname, "rb");
174178 if (f == NULL) {
175179 printf("ERROR: failed to open file %s (%s)\r\n", fname, strerror(errno));
176- return(-1);
180+ return;
177181 }
178182
179- /* read server string */
180- if (fgets(s, (int)ssz, f) == NULL) {
181- printf("read failure from %s: %s\n", fname, strerror(errno));
182- fclose(f);
183- return(-1);
184- }
185- fclose(f);
183+ for (;;) {
184+ /* read server string */
185+ if (fgets(s, sizeof(s), f) == NULL) break;
186186
187- /* parse (look for a ':') */
188- for (i = 0; s[i] != 0; s++) {
189- if ((s[i] == '\r') || (s[i] == '\n')) {
190- s[i] = 0;
191- break;
187+ /* parse (look for a port after ':' and trim \r and \n) */
188+ port = 70;
189+ for (i = 0; s[i] != 0; i++) {
190+ if ((s[i] == '\r') || (s[i] == '\n')) {
191+ s[i] = 0;
192+ break;
193+ }
194+ if (s[i] == ':') {
195+ s[i++] = 0;
196+ port = atoi(s + i);
197+ break;
198+ }
192199 }
193- if (s[i] == ':') {
194- s[i++] = 0;
195- port = atoi(s + i);
196- break;
200+
201+ if ((port < 1) || (port > 65535)) {
202+ printf("invalid port: host=%s port=%d\n", s, port);
203+ continue;
197204 }
198- }
199205
200- if ((port < 1) || (port > 65535)) {
201- printf("invalid port: host=%s port=%d\n", s, port);
202- return(-1);
206+ /* add server to database */
207+ if (glist_addnewhostport(glist, s, (unsigned short)port, 1) != NULL) {
208+ printf("added host: %s:%d\r\n", s, port);
209+ } else {
210+ puts("glist_addnewhostport() call failed");
211+ }
212+
203213 }
204214
205- return(port);
215+ fclose(f);
206216 }
207217
208218
209219 /* insert any newly-subnmitted servers to memory database (and remove the new host file) */
210-static struct gopherlist *loadextrahosts(struct gopherlist *glist, const char *dir) {
220+static void loadextrahosts(struct gopherlist **glist, const char *dir) {
211221 char fname[128];
212- char buff[128];
213222 DIR *d;
214223 struct dirent *dptr;
215- int port;
216224
217225 d = opendir(dir);
218- if (d == NULL) return(glist);
226+ if (d == NULL) return;
219227
220228 while ((dptr = readdir(d)) != NULL) {
221229 if (dptr->d_type != DT_REG) continue; /* skip anything that's not a regular file (includes symlinks) */
@@ -222,21 +230,12 @@
222230 strcpy(fname, dir);
223231 strcat(fname, "/");
224232 strcat(fname, dptr->d_name);
225- printf("loading new host file: %s\n", buff);
226- port = get_server_from_file(buff, sizeof(buff), fname);
227- if (port > 0) {
228- if (glist_addnewhostport(&glist, buff, (unsigned short)port, 1) != NULL) {
229- printf("added host: %s:%d\r\n", buff, port);
230- } else {
231- puts("glist_addnewhostport() call failed");
232- }
233- }
233+ printf("loading new host file: %s\n", fname);
234+ process_serverlist_file(glist, fname);
234235 /* remove file and go to next one */
235236 unlink(fname);
236237 }
237238 closedir(d);
238-
239- return(glist);
240239 }
241240
242241
@@ -474,14 +473,17 @@
474473
475474 for (;;) {
476475
477- printf("\n\n\n--- [%s] ---\n", epoch2human(time(NULL)));
476+ /* make sure that my pending logs are output to stdout (avoids lags when redirecting to file) */
477+ fflush(stdout);
478478
479479 /* do not browse too fast */
480480 while (time(NULL) < nextaction) sleep(1);
481481 nextaction = time(NULL) + WAITPERIOD;
482482
483+ printf("\n\n\n--- [%s] ---\n", epoch2human(time(NULL)));
484+
483485 /* if extra manual hosts required to be added, do it now */
484- glist = loadextrahosts(glist, NEWHOSTSDIR);
486+ loadextrahosts(&glist, NEWHOSTSDIR);
485487
486488 /* save the db once every hour */
487489 if (time(NULL) > nextdbsave) {
Show on old repository browser