From f90b21ec78c8502239b70c4116255300ebf45a7a Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Tue, 15 May 2012 12:56:10 +0930 Subject: [PATCH] Implement rhizome config options rhizome.datastore_path - if not set, reverts to serval instance path rhizome.enable - if not set, defaults to true --- commandline.c | 27 ++++++++++++++++---------- dna.c | 24 +++++++++++++---------- log.c | 1 - overlay.c | 6 +++--- overlay_interface.c | 2 +- rhizome.h | 3 ++- rhizome_database.c | 46 ++++++++++++++++++++++++++++++++++----------- serval.h | 14 ++++++++------ server.c | 6 +++--- 9 files changed, 83 insertions(+), 46 deletions(-) diff --git a/commandline.c b/commandline.c index ea2185b9..71ee3789 100644 --- a/commandline.c +++ b/commandline.c @@ -504,7 +504,7 @@ int app_dna_lookup(int argc, const char *const *argv, struct command_line_option int confValueRotor=0; char confValue[4][128]; -char *confValueGet(char *var,char *defaultValue) +const char *confValueGet(const char *var, const char *defaultValue) { if (!var) return defaultValue; int varLen=strlen(var); @@ -542,6 +542,18 @@ char *confValueGet(char *var,char *defaultValue) return defaultValue; } +int confValueGetBoolean(const char *var, int defaultValue) +{ + const char *value = confValueGet(var, NULL); + if (!value) + return defaultValue; + int flag = confParseBoolean(value, var); + if (flag >= 0) + return flag; + WARNF("Config option %s: using default value %s", var, defaultValue ? "true" : "false"); + return defaultValue; +} + void confSetDebugFlags() { char filename[1024]; @@ -565,12 +577,12 @@ void confSetDebugFlags() ++p; int flag; if (*p) { + *p = '\0'; char *q = p + 1; while (*q && *q != '\n') ++q; *q = '\0'; - if ((flag = confParseBoolean(p + 1)) != -1) { - *p = '\0'; + if ((flag = confParseBoolean(p + 1, flagname)) != -1) { long long mask = debugFlagMask(flagname); if (mask == -1) if (flag) setall = 1; else clearall = 1; @@ -590,13 +602,13 @@ void confSetDebugFlags() } } -int confParseBoolean(const char *text) +int confParseBoolean(const char *text, const char *option_name) { if (!strcasecmp(text, "on") || !strcasecmp(text, "yes") || !strcasecmp(text, "true") || !strcmp(text, "1")) return 1; if (!strcasecmp(text, "off") || !strcasecmp(text, "no") || !strcasecmp(text, "false") || !strcmp(text, "0")) return 0; - WARNF("Invalid boolean value '%s'", text); + WARNF("Config option %s: invalid boolean value '%s'", option_name, text); return -1; } @@ -633,7 +645,6 @@ int app_server_start(int argc, const char *const *argv, struct command_line_opti instance directory when it starts up. */ if (server_remove_stopfile() == -1) return -1; - rhizome_datastore_path = serval_instancepath(); rhizome_opendb(); overlayMode = 1; if (foregroundP) @@ -1039,7 +1050,6 @@ int app_rhizome_add_file(int argc, const char *const *argv, struct command_line_ /* Ensure the Rhizome database exists and is open */ if (create_serval_instance_dir() == -1) return -1; - rhizome_datastore_path = serval_instancepath(); rhizome_opendb(); /* Create a new manifest that will represent the file. If a manifest file was supplied, then read * it, otherwise create a blank manifest. */ @@ -1116,7 +1126,6 @@ int app_rhizome_extract_manifest(int argc, const char *const *argv, struct comma /* Ensure the Rhizome database exists and is open */ if (create_serval_instance_dir() == -1) return -1; - rhizome_datastore_path = serval_instancepath(); rhizome_opendb(); /* Extract the manifest from the database */ rhizome_manifest *m = NULL; @@ -1153,7 +1162,6 @@ int app_rhizome_extract_file(int argc, const char *const *argv, struct command_l /* Ensure the Rhizome database exists and is open */ if (create_serval_instance_dir() == -1) return -1; - rhizome_datastore_path = serval_instancepath(); rhizome_opendb(); /* Extract the file from the database */ int ret = rhizome_retrieve_file(fileid, filepath); @@ -1182,7 +1190,6 @@ int app_rhizome_list(int argc, const char *const *argv, struct command_line_opti /* Create the instance directory if it does not yet exist */ if (create_serval_instance_dir() == -1) return -1; - rhizome_datastore_path = serval_instancepath(); rhizome_opendb(); return rhizome_list_manifests(atoi(offset), atoi(limit)); } diff --git a/dna.c b/dna.c index ef23cd37..84ea17c6 100644 --- a/dna.c +++ b/dna.c @@ -90,6 +90,7 @@ int parseOldCommandLine(int argc, char **argv) int instance=-1; int foregroundMode=0; int clientMode=0; + const char *rhizome_path = NULL; WARNF("The use of the old command line structure is being deprecated."); WARNF("Type '%s help' to learn about the new command line structure.", argv[0]); while ((c = getopt(argc,argv,"Ab:B:E:G:I:Sf:d:i:l:L:mnp:P:r:s:t:v:R:W:U:D:CO:M:N:")) != -1) { @@ -97,16 +98,8 @@ int parseOldCommandLine(int argc, char **argv) { case 'S': serverMode=1; break; case 'r': /* Enable rhizome */ - if (rhizome_datastore_path) return WHY("-r specified more than once"); - rhizome_datastore_path=optarg; - rhizome_opendb(); - /* Also set keyring file to be in the Rhizome directory, to save the need to specify it - separately. */ - char temp[1024]; - if (snprintf(temp, sizeof(temp), "%s/serval.keyring", optarg) - >= sizeof(temp)) - exit(WHY("Rhizome directory name too long.")); - keyring_file = strdup(temp); + if (rhizome_path) return WHY("-r specified more than once"); + rhizome_path = optarg; break; case 'M': /* Distribute specified manifest and file pair using Rhizome. */ /* This option assumes that the manifest is locally produced, and will @@ -223,7 +216,18 @@ int parseOldCommandLine(int argc, char **argv) if (keyring_file&&clientMode) usage("Only servers use backing files"); if (serverMode&&clientMode) usage("You asked me to be both server and client. That's silly."); + if (rhizome_path) { + rhizome_set_datastore_path(rhizome_path); + rhizome_opendb(); + } if (serverMode) { + if (!keyring_file) { + /* Set keyring file to be in the Rhizome directory, to save the need to specify it separately. */ + char temp[1024]; + if (!FORM_RHIZOME_DATASTORE_PATH(temp, "serval.keyring")) + exit(-1); + keyring_file = strdup(temp); + } if (!foregroundMode) daemon(0,0); return server(keyring_file); diff --git a/log.c b/log.c index e9d295fc..e8bde6ff 100644 --- a/log.c +++ b/log.c @@ -124,7 +124,6 @@ long long debugFlagMask(const char *flagname) { else if (!strcasecmp(flagname,"routing")) return DEBUG_OVERLAYROUTING; else if (!strcasecmp(flagname,"security")) return DEBUG_SECURITY; else if (!strcasecmp(flagname,"rhizome")) return DEBUG_RHIZOME; - else if (!strcasecmp(flagname,"norhizome")) return DEBUG_DISABLERHIZOME; else if (!strcasecmp(flagname,"filesync")) return DEBUG_RHIZOMESYNC; else if (!strcasecmp(flagname,"monitorroutes")) return DEBUG_OVERLAYROUTEMONITOR; else if (!strcasecmp(flagname,"queues")) return DEBUG_QUEUES; diff --git a/overlay.c b/overlay.c index 83bbde73..fbabf74a 100644 --- a/overlay.c +++ b/overlay.c @@ -117,7 +117,7 @@ int overlayServerMode() /* Get rhizome server started BEFORE populating fd list so that the server's listen socket is in the list for poll() */ - if (rhizome_datastore_path) rhizome_server_poll(); + if (rhizome_enabled()) rhizome_server_poll(); while(1) { @@ -205,7 +205,7 @@ int overlayServerMode() } } overlay_rx_messages(); - if (rhizome_datastore_path) { + if (rhizome_enabled()) { rhizome_server_poll(); rhizome_fetch_poll(); overlay_mdp_poll(); @@ -216,7 +216,7 @@ int overlayServerMode() Well, for now let's just check anyway. */ if (debug&DEBUG_IO) fprintf(stderr,"poll() timeout.\n"); overlay_rx_messages(); - if (rhizome_datastore_path) { + if (rhizome_enabled()) { rhizome_server_poll(); rhizome_fetch_poll(); overlay_mdp_poll(); diff --git a/overlay_interface.c b/overlay_interface.c index a43499a8..3d428ae9 100644 --- a/overlay_interface.c +++ b/overlay_interface.c @@ -828,7 +828,7 @@ int overlay_tick_interface(int i, long long now) overlay_stuff_packet_from_queue(i,e,OQ_ORDINARY,now,pax,&frame_pax,MAX_FRAME_PAX); overlay_stuff_packet_from_queue(i,e,OQ_OPPORTUNISTIC,now,pax,&frame_pax,MAX_FRAME_PAX); /* 5. XXX Fill the packet up to a suitable size with anything that seems a good idea */ - if (!(debug&DEBUG_DISABLERHIZOME)) + if (rhizome_enabled()) overlay_rhizome_add_advertisements(i,e); if (debug&DEBUG_PACKETCONSTRUCTION) diff --git a/rhizome.h b/rhizome.h index 84075b79..2af60243 100644 --- a/rhizome.h +++ b/rhizome.h @@ -154,7 +154,8 @@ typedef struct rhizome_manifest { } rhizome_manifest; extern long long rhizome_space; -extern const char *rhizome_datastore_path; +const char *rhizome_datastore_path(); +int rhizome_set_datastore_path(const char *path); int form_rhizome_datastore_path(char * buf, size_t bufsiz, const char *fmt, ...); int create_rhizome_datastore_dir(); diff --git a/rhizome_database.c b/rhizome_database.c index ce633047..b1f74106 100644 --- a/rhizome_database.c +++ b/rhizome_database.c @@ -22,17 +22,45 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include long long rhizome_space=0; -const char *rhizome_datastore_path = NULL; +static const char *rhizome_thisdatastore_path = NULL; +static int rhizome_enabled_flag = -1; // unknown + +int rhizome_enabled() +{ + if (rhizome_enabled_flag < 0) + rhizome_enabled_flag = confValueGetBoolean("rhizome.enable", 1); + return rhizome_enabled_flag; +} + +const char *rhizome_datastore_path() +{ + if (!rhizome_thisdatastore_path) + rhizome_set_datastore_path(NULL); + return rhizome_thisdatastore_path; +} + +int rhizome_set_datastore_path(const char *path) +{ + if (path) + rhizome_thisdatastore_path = path; + else { + rhizome_thisdatastore_path = confValueGet("rhizome.datastore_path", NULL); + if (rhizome_thisdatastore_path && rhizome_thisdatastore_path[0] != '/') { + WHYF("Invalid rhizome.datastore_path config setting: '%s' -- should be absolute path", rhizome_thisdatastore_path); + } + } + if (!rhizome_thisdatastore_path) { + rhizome_thisdatastore_path = serval_instancepath(); + WARNF("Rhizome datastore path not configured -- using instance path '%s'", rhizome_thisdatastore_path); + } + return 0; +} int form_rhizome_datastore_path(char * buf, size_t bufsiz, const char *fmt, ...) { char *bufp = buf; char *bufe = bufp + bufsiz - 1; - if (!rhizome_datastore_path) { - WHY("Cannot open rhizome database -- no path specified"); - return 0; - } - bufp += snprintf(bufp, bufe - buf, "%s/", rhizome_datastore_path); + bufp += snprintf(bufp, bufe - buf, "%s/", rhizome_datastore_path()); if (bufp > bufe) { WHY("Path buffer overrun"); return 0; @@ -52,11 +80,7 @@ int form_rhizome_datastore_path(char * buf, size_t bufsiz, const char *fmt, ...) int create_rhizome_datastore_dir() { - if (!rhizome_datastore_path) { - WHY("Cannot create rhizome database -- no path specified"); - return 0; - } - return mkdirs(rhizome_datastore_path, 0700); + return mkdirs(rhizome_datastore_path(), 0700); } sqlite3 *rhizome_db=NULL; diff --git a/serval.h b/serval.h index e24fd02e..c76fdc5b 100755 --- a/serval.h +++ b/serval.h @@ -151,7 +151,8 @@ extern int returnMultiVars; extern char *gatewayspec; -extern const char *rhizome_datastore_path; +int rhizome_enabled(); +const char *rhizome_datastore_path(); extern struct in_addr client_addr; extern int client_port; @@ -387,9 +388,11 @@ extern int hexdigit[16]; extern int sock; -char *confValueGet(char *var,char *defaultValue); +const char *confValueGet(const char *var, const char *defaultValue); +int confValueGetBoolean(const char *var, int defaultValue); void confSetDebugFlags(); -int confParseBoolean(const const char *text); +int confParseBoolean(const char *text, const char *option_name); + int recvwithttl(int sock,unsigned char *buffer,int bufferlen,int *ttl, struct sockaddr *recvaddr,unsigned int *recvaddrlen); int validateSid(const char *sid); @@ -1054,9 +1057,8 @@ int overlay_saw_mdp_containing_frame(int interface,overlay_frame *f,long long no #define DEBUG_QUEUES (1 << 19) #define DEBUG_BROADCASTS (1 << 20) #define DEBUG_RHIZOMESYNC (1 << 21) -#define DEBUG_DISABLERHIZOME (1 << 22) -#define DEBUG_PACKETTX (1 << 23) -#define DEBUG_PACKETCONSTRUCTION (1 << 24) +#define DEBUG_PACKETTX (1 << 22) +#define DEBUG_PACKETCONSTRUCTION (1 << 23) int serval_packetvisualise(FILE *f,char *message,unsigned char *packet,int plen); diff --git a/server.c b/server.c index 71cec696..54c98294 100644 --- a/server.c +++ b/server.c @@ -827,7 +827,7 @@ int simpleServerMode() /* Get rhizome server started BEFORE populating fd list so that the server's listen socket is in the list for poll() */ - if (rhizome_datastore_path) rhizome_server_poll(); + if (rhizome_enabled()) rhizome_server_poll(); /* Get list of file descripters to watch */ fds[0].fd=sock; fds[0].events=POLLIN; @@ -841,12 +841,12 @@ int simpleServerMode() } /* Wait patiently for packets to arrive. */ - if (rhizome_datastore_path) rhizome_server_poll(); + if (rhizome_enabled()) rhizome_server_poll(); while ((r=poll(fds,fdcount,100000))<1) { if (sigIoFlag) { sigIoFlag=0; break; } sleep(0); } - if (rhizome_datastore_path) rhizome_server_poll(); + if (rhizome_enabled()) rhizome_server_poll(); unsigned char buffer[16384]; int ttl=-1; // unknown