/** Reads the property configuration files. * * Clears all current values from the property table, and then reads and * sets properties from the configuration files. All properties read from * configuration files are considered mutable by the application (except for * the values of the specified configuration files. * * @return Zero on success, or a non-zero value on error. Properties will * be set to default on error, or if not set by one or more of the * configuration files. * * @internal */ static int ReadPropertyFiles(void) { /* load all default values first - override later with file entries */ if (SetDefaultValues() != 0) return -1; /* read global, and then app configuration files */ if (*s_GlobalPropertyFile) if (ReadFileProperties(s_GlobalPropertyFile)) SLPPropertySet("net.slp.OpenSLPConfigFile", s_GlobalPropertyFile, SLP_PA_READONLY); /* read environment specified configuration file */ if (*s_EnvPropertyFile) if (ReadFileProperties(s_EnvPropertyFile)) SLPPropertySet("net.slp.EnvConfigFile", s_EnvPropertyFile, SLP_PA_READONLY); /* if set, read application-specified configuration file */ if (*s_AppPropertyFile) if (ReadFileProperties(s_AppPropertyFile)) SLPPropertySet("net.slp.AppConfigFile", s_AppPropertyFile, SLP_PA_READONLY); return 0; }
/*=========================================================================*/ int SLPDPropertyInit(const char* conffile) /*=========================================================================*/ { char* myname = 0; char* myinterfaces = 0; char* myurl = 0; SLPPropertyReadFile(conffile); memset(&G_SlpdProperty,0,sizeof(G_SlpdProperty)); /*-------------------------------------------------------------*/ /* Set the properties without hard defaults */ /*-------------------------------------------------------------*/ G_SlpdProperty.isDA = SLPPropertyAsBoolean(SLPPropertyGet("net.slp.isDA")); G_SlpdProperty.activeDADetection = SLPPropertyAsBoolean(SLPPropertyGet("net.slp.activeDADetection")); if(G_SlpdProperty.activeDADetection) { G_SlpdProperty.DAActiveDiscoveryInterval = atoi(SLPPropertyGet("net.slp.DAActiveDiscoveryInterval")); if(G_SlpdProperty.DAActiveDiscoveryInterval > 1 && G_SlpdProperty.DAActiveDiscoveryInterval < SLPD_CONFIG_DA_FIND ) { G_SlpdProperty.DAActiveDiscoveryInterval = SLPD_CONFIG_DA_FIND; } } else { G_SlpdProperty.DAActiveDiscoveryInterval = 0; } G_SlpdProperty.passiveDADetection = SLPPropertyAsBoolean(SLPPropertyGet("net.slp.passiveDADetection")); G_SlpdProperty.isBroadcastOnly = SLPPropertyAsBoolean(SLPPropertyGet("net.slp.isBroadcastOnly")); G_SlpdProperty.multicastTTL = atoi(SLPPropertyGet("net.slp.multicastTTL")); G_SlpdProperty.multicastMaximumWait = atoi(SLPPropertyGet("net.slp.multicastMaximumWait")); G_SlpdProperty.unicastMaximumWait = atoi(SLPPropertyGet("net.slp.unicastMaximumWait")); G_SlpdProperty.randomWaitBound = atoi(SLPPropertyGet("net.slp.randomWaitBound")); G_SlpdProperty.maxResults = atoi(SLPPropertyGet("net.slp.maxResults")); G_SlpdProperty.traceMsg = SLPPropertyAsBoolean(SLPPropertyGet("net.slp.traceMsg")); G_SlpdProperty.traceReg = SLPPropertyAsBoolean(SLPPropertyGet("net.slp.traceReg")); G_SlpdProperty.traceDrop = SLPPropertyAsBoolean(SLPPropertyGet("net.slp.traceDrop")); G_SlpdProperty.traceDATraffic = SLPPropertyAsBoolean(SLPPropertyGet("net.slp.traceDATraffic")); G_SlpdProperty.DAAddresses = SLPPropertyGet("net.slp.DAAddresses"); G_SlpdProperty.DAAddressesLen = strlen(G_SlpdProperty.DAAddresses); /* TODO make sure that we are using scopes correctly. What about DHCP, etc*/ G_SlpdProperty.useScopes = SLPPropertyGet("net.slp.useScopes"); G_SlpdProperty.useScopesLen = strlen(G_SlpdProperty.useScopes); G_SlpdProperty.locale = SLPPropertyGet("net.slp.locale"); G_SlpdProperty.localeLen = strlen(G_SlpdProperty.locale); G_SlpdProperty.securityEnabled = SLPPropertyAsBoolean(SLPPropertyGet("net.slp.securityEnabled")); G_SlpdProperty.checkSourceAddr = SLPPropertyAsBoolean(SLPPropertyGet("net.slp.checkSourceAddr")); G_SlpdProperty.DAHeartBeat = SLPPropertyAsInteger(SLPPropertyGet("net.slp.DAHeartBeat")); /*-------------------------------------*/ /* Set the net.slp.interfaces property */ /*-------------------------------------*/ if(SLPIfaceGetInfo(SLPPropertyGet("net.slp.interfaces"),&G_SlpdProperty.ifaceInfo) == 0) { if(SLPPropertyGet("net.slp.interfaces")) { if(SLPIfaceSockaddrsToString(G_SlpdProperty.ifaceInfo.iface_addr, G_SlpdProperty.ifaceInfo.iface_count, &myinterfaces) == 0) { if(myinterfaces) { SLPPropertySet("net.slp.interfaces", myinterfaces); xfree(myinterfaces); } } } G_SlpdProperty.interfaces = SLPPropertyGet("net.slp.interfaces"); } G_SlpdProperty.interfacesLen = strlen(G_SlpdProperty.interfaces); /*---------------------------------------------------------*/ /* Set the value used internally as the url for this agent */ /*---------------------------------------------------------*/ /* 27 is the size of "service:directory-agent://(NULL)" */ if(SLPNetGetThisHostname(&myname,1) == 0) { myurl = (char*)xmalloc(27 + strlen(myname)); if(G_SlpdProperty.isDA) { strcpy(myurl,SLP_DA_SERVICE_TYPE); } else { strcpy(myurl,SLP_SA_SERVICE_TYPE); } strcat(myurl,"://"); strcat(myurl,myname); SLPPropertySet("net.slp.agentUrl",myurl); G_SlpdProperty.myUrl = SLPPropertyGet("net.slp.agentUrl"); G_SlpdProperty.myUrlLen = strlen(G_SlpdProperty.myUrl); xfree(myurl); xfree(myname); } /*----------------------------------*/ /* Set other values used internally */ /*----------------------------------*/ G_SlpdProperty.DATimestamp = 1; /* DATimestamp must start at 1 */ G_SlpdProperty.activeDiscoveryXmits = 3; /* ensures xmit on first 3 calls to SLPDKnownDAActiveDiscovery() */ G_SlpdProperty.nextActiveDiscovery = 0; /* ensures xmit on first call to SLPDKnownDAActiveDiscovery() */ G_SlpdProperty.nextPassiveDAAdvert = 0; /* ensures xmit on first call to SLPDKnownDAPassiveDiscovery()*/ return 0; }
/*=========================================================================*/ int SLPPropertyReadFile(const char* conffile) /* Reads and sets properties from the specified configuration file */ /* */ /* conffile (IN) the path of the config file to read. */ /* */ /* Returns - zero on success. non-zero on error. Properties will be set*/ /* to default on error. */ /*=========================================================================*/ { char* line; char* alloced; FILE* fp; char* namestart; char* nameend; char* valuestart; char* valueend; if(SetDefaultValues()) { return -1; } alloced = xmalloc(4096); if(alloced == 0) { /* out of memory */ errno = ENOMEM; return -1; } fp = fopen(conffile,"r"); if(!fp) { goto CLEANUP; } /* Set the property that keeps track of conffile */ SLPPropertySet("net.slp.OpenSLPConfigFile",conffile); while(fgets(alloced,4096,fp)) { line = alloced; /* trim whitespace */ while(*line && *line <= 0x20) { line++; } if(*line == 0) { continue; } /* skip commented lines */ if(*line == '#' || *line == ';') { continue; } /* parse out the property name*/ namestart = line; nameend = line; nameend = strchr(nameend,'='); if(nameend == 0) { continue; } valuestart = nameend + 1; /* start of value for later*/ while(*nameend <= 0x20 || *nameend == '=') { *nameend = 0; nameend --; } /* parse out the property value */ while(*valuestart && *valuestart <= 0x20) { valuestart++; } valueend = valuestart; /* Seek to the end of the value */ while(*valueend) { valueend++; } /* Remove any whitespace that might be present */ while(valueend != valuestart && *valueend <= 0x20) { *valueend = 0; valueend --; } /* set the property */ if(valuestart && *valuestart) { SLPPropertySet(namestart, valuestart); } } CLEANUP: if(fp) { fclose(fp); } if(alloced) { xfree(alloced); } return 0; }
/*-------------------------------------------------------------------------*/ int SetDefaultValues() /*-------------------------------------------------------------------------*/ { int result = 0; result |= SLPPropertySet("net.slp.isBroadcastOnly","false"); result |= SLPPropertySet("net.slp.multicastTimeouts","500,750,1000,1500,2000,3000"); result |= SLPPropertySet("net.slp.multicastMaximumWait","5000"); result |= SLPPropertySet("net.slp.unicastTimeouts","500,750,1000,1500,2000,3000"); result |= SLPPropertySet("net.slp.unicastMaximumWait","5000"); result |= SLPPropertySet("net.slp.datagramTimeouts",""); result |= SLPPropertySet("net.slp.maxResults","256"); result |= SLPPropertySet("net.slp.DADiscoveryTimeouts","500,750,1000,1500,2000,3000"); result |= SLPPropertySet("net.slp.DADiscoveryMaximumWait","2000"); result |= SLPPropertySet("net.slp.DAActiveDiscoveryInterval","1"); result |= SLPPropertySet("net.slp.DAAddresses",""); result |= SLPPropertySet("net.slp.watchRegistrationPID","true"); result |= SLPPropertySet("net.slp.activeDADetection","true"); result |= SLPPropertySet("net.slp.passiveDADetection","true"); result |= SLPPropertySet("net.slp.useScopes","default"); result |= SLPPropertySet("net.slp.locale","en"); result |= SLPPropertySet("net.slp.randomWaitBound","5000"); result |= SLPPropertySet("net.slp.interfaces",""); result |= SLPPropertySet("net.slp.securityEnabled","false"); result |= SLPPropertySet("net.slp.multicastTTL","8"); result |= SLPPropertySet("net.slp.MTU","1400"); result |= SLPPropertySet("net.slp.traceMsg","false"); result |= SLPPropertySet("net.slp.traceReg","false"); result |= SLPPropertySet("net.slp.traceDrop","false"); result |= SLPPropertySet("net.slp.traceDATraffic","false"); result |= SLPPropertySet("net.slp.isDA","false"); result |= SLPPropertySet("net.slp.DAHeartBeat","10800"); result |= SLPPropertySet("net.slp.securityEnabled","false"); result |= SLPPropertySet("net.slp.checkSourceAddr","true"); #ifdef _WIN32 result |= SLPPropertySet("net.slp.OpenSLPVersion", SLP_VERSION); #else /* UNIX */ result |= SLPPropertySet("net.slp.OpenSLPVersion", VERSION); #endif return result; }
/*-------------------------------------------------------------------------*/ int SetDefaultValues() /*-------------------------------------------------------------------------*/ { int result = 0; result |= SLPPropertySet("net.slp.multicastTTL","8"); result |= SLPPropertySet("net.slp.isBroadcastOnly","false"); result |= SLPPropertySet("net.slp.passiveDADetection","false"); result |= SLPPropertySet("net.slp.activeDADetection","false"); result |= SLPPropertySet("net.slp.locale","en"); result |= SLPPropertySet("net.slp.multicastTimeouts",""); result |= SLPPropertySet("net.slp.DADiscoveryTimeouts",""); result |= SLPPropertySet("net.slp.datagramTimeouts",""); result |= SLPPropertySet("net.slp.randomWaitBound","1000"); result |= SLPPropertySet("net.slp.interfaces",""); result |= SLPPropertySet("net.slp.DAAddresses",""); result |= SLPPropertySet("net.slp.securityEnabled","false"); result |= SLPPropertySet("net.slp.unicastMaximumWait","15000"); result |= SLPPropertySet("net.slp.multicastMaximumWait","15000"); result |= SLPPropertySet("net.slp.multicastTTL","255"); result |= SLPPropertySet("net.slp.MTU","1400"); result |= SLPPropertySet("net.slp.useScopes","DEFAULT"); result |= SLPPropertySet("net.slp.traceMsg","false"); result |= SLPPropertySet("net.slp.traceReg","false"); result |= SLPPropertySet("net.slp.traceDrop","false"); result |= SLPPropertySet("net.slp.traceDATraffic","false"); result |= SLPPropertySet("net.slp.interfaces",""); result |= SLPPropertySet("notfound",""); return result; }
/** Reinitialize the slpd property management subsystem. * * Clears and rereads configuration parameters from files into the system. * Resets slpd-specific overrides. */ void SLPDPropertyReinit(void) { int sts; char * myinterfaces = 0; int family = AF_UNSPEC; /* free previous settings (if any) */ xfree(G_SlpdProperty.useScopes); xfree(G_SlpdProperty.DAAddresses); xfree(G_SlpdProperty.interfaces); xfree(G_SlpdProperty.locale); /* reinitialize property sub-system */ (void)SLPPropertyReinit(); /* set the properties without hard defaults */ G_SlpdProperty.isDA = SLPPropertyAsBoolean("net.slp.isDA"); G_SlpdProperty.activeDADetection = SLPPropertyAsBoolean("net.slp.activeDADetection"); if (G_SlpdProperty.activeDADetection) { G_SlpdProperty.DAActiveDiscoveryInterval = SLPPropertyAsInteger("net.slp.DAActiveDiscoveryInterval"); if (G_SlpdProperty.DAActiveDiscoveryInterval > 1 && G_SlpdProperty.DAActiveDiscoveryInterval < SLPD_CONFIG_DA_FIND) G_SlpdProperty.DAActiveDiscoveryInterval = SLPD_CONFIG_DA_FIND; } else G_SlpdProperty.DAActiveDiscoveryInterval = 0; G_SlpdProperty.passiveDADetection = SLPPropertyAsBoolean("net.slp.passiveDADetection"); G_SlpdProperty.staleDACheckPeriod = SLPPropertyAsInteger("net.slp.staleDACheckPeriod"); if (G_SlpdProperty.staleDACheckPeriod > 0) { if (G_SlpdProperty.staleDACheckPeriod < (SLPD_HEARTBEATS_PER_CHECK_PERIOD + 1) * SLPD_AGE_INTERVAL) G_SlpdProperty.staleDACheckPeriod = (SLPD_HEARTBEATS_PER_CHECK_PERIOD + 1) * SLPD_AGE_INTERVAL; SLPDLog("staleDACheckPeriod = %ds\n", G_SlpdProperty.staleDACheckPeriod); } G_SlpdProperty.isBroadcastOnly = SLPPropertyAsBoolean("net.slp.isBroadcastOnly"); G_SlpdProperty.multicastTTL = SLPPropertyAsInteger("net.slp.multicastTTL"); G_SlpdProperty.multicastMaximumWait = SLPPropertyAsInteger("net.slp.multicastMaximumWait"); G_SlpdProperty.unicastMaximumWait = SLPPropertyAsInteger("net.slp.unicastMaximumWait"); SLPPropertyAsIntegerVector("net.slp.unicastTimeouts", G_SlpdProperty.unicastTimeouts, MAX_RETRANSMITS); G_SlpdProperty.randomWaitBound = SLPPropertyAsInteger("net.slp.randomWaitBound"); G_SlpdProperty.maxResults = SLPPropertyAsInteger("net.slp.maxResults"); G_SlpdProperty.traceMsg = SLPPropertyAsBoolean("net.slp.traceMsg"); G_SlpdProperty.traceReg = SLPPropertyAsBoolean("net.slp.traceReg"); G_SlpdProperty.traceDrop = SLPPropertyAsBoolean("net.slp.traceDrop"); G_SlpdProperty.traceDATraffic = SLPPropertyAsBoolean("net.slp.traceDATraffic"); G_SlpdProperty.appendLog = SLPPropertyAsBoolean("net.slp.appendLog"); if ((G_SlpdProperty.DAAddresses = SLPPropertyXDup("net.slp.DAAddresses")) != 0) G_SlpdProperty.DAAddressesLen = strlen(G_SlpdProperty.DAAddresses); /** @todo Make sure that we are using scopes correctly. What about DHCP, etc? */ if ((G_SlpdProperty.useScopes = SLPPropertyXDup("net.slp.useScopes")) != 0) G_SlpdProperty.useScopesLen = strlen(G_SlpdProperty.useScopes); if ((G_SlpdProperty.locale = SLPPropertyXDup("net.slp.locale")) != 0) G_SlpdProperty.localeLen = strlen(G_SlpdProperty.locale); G_SlpdProperty.securityEnabled = SLPPropertyAsBoolean("net.slp.securityEnabled"); G_SlpdProperty.checkSourceAddr = SLPPropertyAsBoolean("net.slp.checkSourceAddr"); G_SlpdProperty.DAHeartBeat = SLPPropertyAsInteger("net.slp.DAHeartBeat"); if (G_SlpdProperty.staleDACheckPeriod > 0) { /* Adjust the heartbeat interval if we need to send it faster for * stale DA detection */ int maxHeartbeat = G_SlpdProperty.staleDACheckPeriod / SLPD_HEARTBEATS_PER_CHECK_PERIOD; if ((G_SlpdProperty.DAHeartBeat == 0) || (G_SlpdProperty.DAHeartBeat > maxHeartbeat)) { SLPDLog("Overriding heartbeat to %ds for stale DA check\n", maxHeartbeat); G_SlpdProperty.DAHeartBeat = maxHeartbeat; } } /* Can't send it out more frequently than every interval */ if (G_SlpdProperty.DAHeartBeat < SLPD_AGE_INTERVAL) G_SlpdProperty.DAHeartBeat = SLPD_AGE_INTERVAL; G_SlpdProperty.port = (uint16_t)SLPPropertyAsInteger("net.slp.port"); /* set the net.slp.interfaces property */ if (SLPNetIsIPV4() && SLPNetIsIPV6()) family = AF_UNSPEC; else if (SLPNetIsIPV4()) family = AF_INET; else if (SLPNetIsIPV6()) family = AF_INET6; myinterfaces = SLPPropertyXDup("net.slp.interfaces"); sts = SLPIfaceGetInfo(myinterfaces, &G_SlpdProperty.ifaceInfo, family); xfree(myinterfaces); if (!G_SlpdProperty.indexingPropertiesSet) { #ifdef ENABLE_PREDICATES G_SlpdProperty.indexedAttributesLen = 0; if ((G_SlpdProperty.indexedAttributes = SLPPropertyXDup("net.slp.indexedAttributes")) != 0) G_SlpdProperty.indexedAttributesLen = strlen(G_SlpdProperty.indexedAttributes); #endif G_SlpdProperty.srvtypeIsIndexed = SLPPropertyAsBoolean("net.slp.indexSrvtype"); G_SlpdProperty.indexingPropertiesSet = 1; } else { #ifdef ENABLE_PREDICATES int attrchg = 0; char *indexedAttributes = SLPPropertyXDup("net.slp.indexedAttributes"); if (!indexedAttributes && G_SlpdProperty.indexedAttributes) attrchg = 1; else if (indexedAttributes && !G_SlpdProperty.indexedAttributes) attrchg = 1; else if (indexedAttributes) { if (strcmp(indexedAttributes, G_SlpdProperty.indexedAttributes) != 0) attrchg = 1; } if (attrchg) SLPDLog("Cannot change value of net.slp.indexedAttributes without restarting the daemon\n"); xfree(indexedAttributes); #endif if (G_SlpdProperty.srvtypeIsIndexed != SLPPropertyAsBoolean("net.slp.indexSrvtype")) SLPDLog("Cannot change value of net.slp.indexSrvtype without restarting the daemon\n"); } if (sts == 0) { myinterfaces = 0; if (SLPIfaceSockaddrsToString(G_SlpdProperty.ifaceInfo.iface_addr, G_SlpdProperty.ifaceInfo.iface_count, &myinterfaces) == 0) { SLPPropertySet("net.slp.interfaces", myinterfaces, SLP_PA_USERSET); G_SlpdProperty.interfaces = myinterfaces; G_SlpdProperty.interfacesLen = strlen(G_SlpdProperty.interfaces); } } /* set the value used internally as the url for this agent */ strcpy(G_SlpdProperty.urlPrefix, G_SlpdProperty.isDA? SLP_DA_SERVICE_TYPE: SLP_SA_SERVICE_TYPE); strcat(G_SlpdProperty.urlPrefix, "://"); G_SlpdProperty.urlPrefixLen = strlen(G_SlpdProperty.urlPrefix); /* set other values used internally */ G_SlpdProperty.DATimestamp = (uint32_t)time(0); /* DATimestamp must be the boot time of the process */ G_SlpdProperty.activeDiscoveryXmits = 3; /* ensures xmit on first 3 calls to SLPDKnownDAActiveDiscovery() */ G_SlpdProperty.nextActiveDiscovery = 0; /* ensures xmit on first call to SLPDKnownDAActiveDiscovery() */ G_SlpdProperty.nextPassiveDAAdvert = 0; /* ensures xmit on first call to SLPDKnownDAPassiveDiscovery()*/ }
int main(int argc, char * argv[]) { int ec, nval, ival; bool bval; int ivec[10]; FILE * fp; char const * pval; /* create a global configuration file */ fp = fopen(TEST_G_CFG_FILENAME, "w+"); if (!fp) return FAIL; fputs("\n", fp); fputs(" \n", fp); fputs("# This is a comment.\n", fp); fputs(" # This is another comment.\n", fp); fputs(" \t\f# This is the last comment.\n", fp); fputs("\t\t \f\tStrange Text with no equals sign\n", fp); fputs("net.slp.isDA=true\n", fp); /* default value is false */ fputs("net.slp.DAHeartBeat = 10801\n", fp); fputs("net.slp.DAAttributes=\n", fp); fputs("net.slp.useScopes =DEFAULT\n", fp); fputs("net.slp.DAAddresses\t\t\t= \n", fp); fputs("net.slp.traceDATraffic = true\n", fp); fputs("net.slp.multicastTimeouts=1001,1251,1501,2001,4001\n", fp); fclose(fp); /* create an application configuration file */ fp = fopen(TEST_A_CFG_FILENAME, "w+"); if (!fp) return FAIL; fputs("net.slp.DAHeartBeat = 10802\n", fp); fclose(fp); /* specify app configuration file */ ec = SLPPropertySetAppConfFile(TEST_A_CFG_FILENAME); if (ec != 0) return FAIL; /* specify global configuration file - initialize */ ec = SLPPropertyInit(TEST_G_CFG_FILENAME); if (ec != 0) return FAIL; /* set a mutable value */ ec = SLPPropertySet("net.slp.traceDATraffic", "false", 0); if (ec != 0) return FAIL; /* set a user-only settable value */ ec = SLPPropertySet("net.slp.isDA", "false", SLP_PA_USERSET); if (ec != 0) return FAIL; pval = SLPPropertyGet("net.slp.traceDATraffic", 0, 0); if (pval == 0 || strcmp(pval, "false") != 0) return FAIL; ival = SLPPropertyAsInteger("net.slp.DAHeartBeat"); if (ival != 10802) return FAIL; bval = SLPPropertyAsBoolean("net.slp.isDA"); if (bval != false) return FAIL; nval = SLPPropertyAsIntegerVector("net.slp.multicastTimeouts", ivec, 10); if (nval != 5 || ivec[0] != 1001 || ivec[1] != 1251 || ivec[2] != 1501 || ivec[3] != 2001 || ivec[4] != 4001) return FAIL; ival = SLPPropertyAsInteger("net.slp.fake"); if (ival != 0) return FAIL; bval = SLPPropertyAsBoolean("net.slp.fake"); if (bval != false) return FAIL; nval = SLPPropertyAsIntegerVector("net.slp.fake", ivec, 10); if (nval != 0) return FAIL; pval = SLPPropertyGet("net.slp.OpenSLPConfigFile", 0, 0); if (pval == 0 || strcmp(pval, TEST_G_CFG_FILENAME) != 0) return FAIL; /* reset a user-only settable value - indicate non-user is setting */ ec = SLPPropertySet("net.slp.isDA", "true", 0); if (ec == 0) return FAIL; /* reset a user-only settable value - indicate user is setting */ ec = SLPPropertySet("net.slp.isDA", "true", SLP_PA_USERSET); if (ec != 0) return FAIL; SLPPropertyExit(); unlink(TEST_A_CFG_FILENAME); unlink(TEST_G_CFG_FILENAME); return PASS; }
/** Reads a specified configuration file into non-userset properties. * * Reads all values from a specified configuration file into the in-memory * database * * @param[in] conffile - The name of the file to be read. * * @return A Boolean value of true if the file could be opened and read, * or false if the file did not exist, or could not be opened. * * @internal */ static bool ReadFileProperties(char const * conffile) { FILE * fp; char * alloced; bool retval = false; #define CONFFILE_RDBUFSZ 4096 /* allocate a buffer and read the configuration file */ if ((alloced = xmalloc(CONFFILE_RDBUFSZ)) == 0) return false; /* open configuration file for read - missing file returns false */ if ((fp = fopen(conffile, "r")) != 0) { /* read a line at a time - max 4k characters per line */ while (fgets(alloced, CONFFILE_RDBUFSZ, fp)) { char * line = alloced; char * namestart; char * nameend; char * valuestart; char * valueend; /* trim leading white space */ while (*line && *line <= 0x20) line++; if (*line == 0) continue; /* skip all comments: # or ; to EOL */ if (*line == '#' || *line == ';') continue; /* parse out the property name */ namestart = line; /* ignore lines not containing '=' */ nameend = strchr(namestart, '='); if (nameend == 0) continue; /* capture value start for later */ valuestart = nameend + 1; /* trim trailing white space from name */ *nameend-- = 0; while (*nameend <= 0x20) *nameend-- = 0; /* parse out the property value - trim leading white space */ while (*valuestart && *valuestart <= 0x20) valuestart++; /* find end of value, trim trailing white space */ valueend = valuestart + strlen(valuestart); while (valueend != valuestart && *valueend <= 0x20) *valueend-- = 0; /* set the property (as mutable) */ if (valuestart && *valuestart) SLPPropertySet(namestart, valuestart, 0); } fclose(fp); retval = true; } xfree(alloced); return retval; }
/** * Initializes the MTU configuration property value. If the user specified * value is more than the value that is allowed by the kernel, MTU value is * adjusted to the actual value set by the kernel. If the default values of * SO_SNDBUF and SO_RCVBUF are greater than the global MTU value, SO_SNDBUF * and SO_RCVBUF are not set explicitly. * * @internal */ static void InitializeMTUPropertyValue() { #ifndef _WIN32 int mtuChanged = 0; int family; sockfd_t sock; int value = 0; socklen_t valSize = sizeof(int); #endif s_GlobalPropertyInternalRcvBufSize = s_GlobalPropertyInternalSndBufSize = 0; s_GlobalPropertyMTU = SLPPropertyAsInteger("net.slp.MTU"); #ifndef _WIN32 family = SLPPropertyAsBoolean("net.slp.useIPv4") ? AF_INET : AF_INET6; if ((sock = socket(family, SOCK_DGRAM, 0)) != SLP_INVALID_SOCKET) { if (getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &value, &valSize) != -1) { if (value < s_GlobalPropertyMTU) { setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &s_GlobalPropertyMTU, sizeof(int)); s_GlobalPropertyInternalRcvBufSize = s_GlobalPropertyMTU; } } if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &value, &valSize) != -1) { if (value < s_GlobalPropertyMTU) { setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &s_GlobalPropertyMTU, sizeof(int)); s_GlobalPropertyInternalSndBufSize = s_GlobalPropertyMTU; } } // If the actual value set by the kernel is less than the MTU value, // adjust here. if (s_GlobalPropertyInternalRcvBufSize && getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &value, &valSize) != -1) { if (value < s_GlobalPropertyMTU) { s_GlobalPropertyInternalRcvBufSize = value; } } if (s_GlobalPropertyInternalSndBufSize && getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &value, &valSize) != -1) { if (value < s_GlobalPropertyMTU) { s_GlobalPropertyInternalSndBufSize = value; } } close(sock); // If both values are set adjust the s_GlobalPropertyMTU value. if (s_GlobalPropertyInternalRcvBufSize && s_GlobalPropertyInternalSndBufSize) { s_GlobalPropertyMTU = s_GlobalPropertyInternalRcvBufSize; if (s_GlobalPropertyMTU < s_GlobalPropertyInternalSndBufSize) { s_GlobalPropertyMTU = s_GlobalPropertyInternalSndBufSize; } mtuChanged = 1; } } if (mtuChanged) { char tmp[13]; snprintf(tmp, 13, "%d", s_GlobalPropertyMTU); SLPPropertySet("net.slp.MTU", tmp, 0); } #endif }
/** Sets all SLP default property values. * * @return Zero on success, or non-zero with errno set on error. * * @internal */ static int SetDefaultValues(void) { /* The table of default property values - comments following some values * indicate deviations from the default values specified in RFC 2614. */ static struct { char * name, * value; unsigned attrs; } defaults[] = { /* Section 2.1.1 DA Configuration */ {"net.slp.isDA", "false", 0}, {"net.slp.DAHeartBeat", "10800", 0}, {"net.slp.DAAttributes", "", 0}, /* Section 2.1.2 Static Scope Configuration */ {"net.slp.useScopes", "DEFAULT", 0}, {"net.slp.DAAddresses", "", 0}, /* Section 2.1.3 Tracing and Logging */ {"net.slp.traceDATraffic", "false", 0}, {"net.slp.traceMsg", "false", 0}, {"net.slp.traceDrop", "false", 0}, {"net.slp.traceReg", "false", 0}, {"net.slp.appendLog", "true", 0}, /* Section 2.1.4 Serialized Proxy Registrations */ {"net.slp.serializedRegURL", "", 0}, /* Section 2.1.5 Network Configuration Properties */ {"net.slp.isBroadcastOnly", "false", 0}, {"net.slp.passiveDADetection", "true", 0}, /* false */ {"net.slp.multicastTTL", "255", 0}, /* 8 */ {"net.slp.DAActiveDiscoveryInterval", "900", 0}, /* 1 */ {"net.slp.multicastMaximumWait", "15000"}, /* 5000 */ {"net.slp.multicastTimeouts", "500,750,1000,1500,2000,3000", 0}, /* 500,750,1000,1500,2000,3000 */ {"net.slp.DADiscoveryTimeouts", "500,750,1000,1500,2000,3000", 0}, /* 500,750,1000,1500,2000,3000 */ {"net.slp.datagramTimeouts", "500,750,1000,1500,2000,3000", 0}, /* I made up these numbers */ {"net.slp.randomWaitBound", "1000", 0}, {"net.slp.MTU", "1400", 0}, {"net.slp.interfaces", "", 0}, /* Section 2.1.6 SA Configuration */ {"net.slp.SAAttributes", "", 0}, /* Section 2.1.7 UA Configuration */ {"net.slp.locale", "en", 0}, {"net.slp.maxResults", "-1", 0}, /* 256 */ {"net.slp.typeHint", "", 0}, /* Section 2.1.8 Security */ {"net.slp.securityEnabled", "false", 0}, /* Additional properties that transcend RFC 2614 */ {"net.slp.watchRegistrationPID", "true", 0}, {"net.slp.OpenSLPVersion", SLP_VERSION, 0}, {"net.slp.unicastMaximumWait", "5000", 0}, {"net.slp.unicastTimeouts", "500,750,1000,1500,2000,3000", 0}, {"net.slp.DADiscoveryMaximumWait", "5000", 0}, {"net.slp.activeDADetection", "true", 0}, {"net.slp.checkSourceAddr", "true", 0}, {"net.slp.broadcastAddr", "255.255.255.255", 0}, {"net.slp.port", "427", 0}, {"net.slp.useDHCP", "true", 0}, /* Additional properties that are specific to IPv6 */ {"net.slp.useIPv6", "false", 0}, {"net.slp.useIPv4", "true", 0}, }; int i; for (i = 0; i < sizeof(defaults)/sizeof(*defaults); i++) if (SLPPropertySet(defaults[i].name, defaults[i].value, defaults[i].attrs) != 0) return -1; return 0; }
/*=========================================================================*/ int SLPPropertyReadFile(const char* conffile) /* Reads and sets properties from the specified configuration file */ /* */ /* conffile (IN) the path of the config file to read. */ /* */ /* Returns - zero on success. non-zero on error. Properties will be set*/ /* to default on error. */ /*=========================================================================*/ { char* line; char* alloced; FILE* fd; char* namestart; char* nameend; char* valuestart; char* valueend; if(SetDefaultValues()) { return -1; } alloced = malloc(4096); if(alloced == 0) { /* out of memory */ errno = ENOMEM; return -1; } fd = fopen(conffile,"r"); if(fd == 0) { goto CLEANUP; } while(fgets(alloced,4096,fd)) { line = alloced; /* trim whitespace */ while(*line && *line <= 0x20) { line++; } if(*line == 0) { continue; } /* skip commented lines */ if(*line == '#' || *line == ';') { continue; } /* parse out the property name*/ namestart = line; nameend = line; while(*nameend && *nameend != '=') nameend++; if(*nameend == 0) { continue; } valuestart = nameend + 1; /* start of value for later*/ while(*nameend <= 0x20 || *nameend == '=') { *nameend = 0; nameend --; } /* parse out the property value */ while(*valuestart <= 0x20) { valuestart++; } valueend = valuestart; while(*valueend) { valueend++; } while(*valueend <= 0x20) { *valueend = 0; valueend --; } /* set the property */ SLPPropertySet(namestart, valuestart); } CLEANUP: if(alloced) { free(alloced); } return 0; }