Compare commits

..

2 commits

Author SHA1 Message Date
lda
610b0807ce
[MOD/WIP] Clean up code a bit 2023-12-03 23:06:17 +01:00
lda
39ef050109
[FIX] Fix memory issue when creating a configuration 2023-12-03 16:41:45 +01:00
3 changed files with 51 additions and 28 deletions

View file

@ -136,11 +136,15 @@ ConfigCreateDefault(Db * db)
config.registration = 0;
config.federation = 1;
/* Create serverName and baseUrl. */
config.serverName = Malloc(HOST_NAME_MAX + 1);
memset(config.serverName, 0, HOST_NAME_MAX + 1);
gethostname(config.serverName, HOST_NAME_MAX);
len = strlen(config.serverName) + 10;
config.baseUrl = Malloc(len);
snprintf(config.baseUrl, len, "https://%s/", config.serverName);
/* Add simple listener without TLS. */
config.listen = ArrayCreate();
listener = Malloc(sizeof(ConfigListener));
listener->maxConnections = Int64Create(0, 0);
@ -151,13 +155,7 @@ ConfigCreateDefault(Db * db)
listener->tls.cert = NULL;
ArrayAdd(config.listen, listener);
config.serverName = Malloc(HOST_NAME_MAX + 1);
/* TODO: Don't set that field(it has to because otherwise j2s will add
* a NULL byte it seems.) */
config.identityServer = StrDuplicate("");
/* Write it all out to the configuration file. */
json = ConfigToJson(&config);
ref = DbCreate(db, 1, "config");
@ -224,3 +222,21 @@ ConfigUnlock(Config * config)
ConfigFullyFree(config);
return DbUnlock(db, dbRef);
}
int
ConfigLogLevelToSyslog(ConfigLogLevel level)
{
switch (level)
{
case CONFIG_LOG_LEVEL_NOTICE:
return LOG_NOTICE;
case CONFIG_LOG_LEVEL_ERROR:
return LOG_ERR;
case CONFIG_LOG_LEVEL_MESSAGE:
return LOG_INFO;
case CONFIG_LOG_LEVEL_DEBUG:
return LOG_DEBUG;
case CONFIG_LOG_LEVEL_WARNING:
return LOG_WARNING;
}
return LOG_INFO;
}

View file

@ -281,12 +281,6 @@ start:
{
LogConfigTimeStampFormatSet(LogConfigGlobal(), tConfig->log.timestampFormat);
}
else
{
/* TODO */
/*Free(tConfig->logTimestamp);
tConfig->logTimestamp = NULL;*/
}
if (tConfig->log.color)
{
@ -297,8 +291,11 @@ start:
LogConfigFlagClear(LogConfigGlobal(), LOG_FLAG_COLOR);
}
/* TODO: Set level properly here. */
/*LogConfigLevelSet(LogConfigGlobal(), flags & ARG_VERBOSE ? LOG_DEBUG : tConfig->logLevel);*/
LogConfigLevelSet(
LogConfigGlobal(),
flags & ARG_VERBOSE ?
LOG_DEBUG :
ConfigLogLevelToSyslog(tConfig->log.level));
if (tConfig->log.output == CONFIG_LOG_OUTPUT_FILE)
{
@ -308,13 +305,17 @@ start:
{
Log(LOG_ERR, "Unable to open log file for appending.");
exit = EXIT_FAILURE;
/*tConfig->flags &= CONFIG_LOG_STDOUT;*/
tConfig->log.output = CONFIG_LOG_OUTPUT_STDOUT;
goto finish;
}
Log(LOG_INFO, "Logging to the log file. Check there for all future messages.");
LogConfigOutputSet(LogConfigGlobal(), logFile);
}
else if (tConfig->log.output == CONFIG_LOG_OUTPUT_STDOUT)
{
Log(LOG_DEBUG, "Already logging to standard output.");
}
else if (tConfig->log.output == CONFIG_LOG_OUTPUT_SYSLOG)
{
Log(LOG_INFO, "Logging to the syslog. Check there for all future messages.");
@ -357,29 +358,30 @@ start:
{
ConfigListener *serverCfg = ArrayGet(tConfig->listen, i);
/* TODO: Think of a nicer solution. */
HttpServerConfig args;
Log(LOG_DEBUG, "HTTP listener: %lu", i);
LogConfigIndent(LogConfigGlobal());
Log(LOG_DEBUG, "Port: %hu", serverCfg->port);
Log(LOG_DEBUG, "Threads: %u", serverCfg->threads);
Log(LOG_DEBUG, "Max Connections: %u", serverCfg->maxConnections);
/*Log(LOG_DEBUG, "Flags: %d", serverCfg->flags);*/
Log(LOG_DEBUG, "TLS Cert: %s", serverCfg->tls.cert);
Log(LOG_DEBUG, "TLS Key: %s", serverCfg->tls.key);
LogConfigUnindent(LogConfigGlobal());
args.port = serverCfg->port;
args.threads = serverCfg->maxConnections;
args.maxConnections = serverCfg->maxConnections;
args.tlsCert = serverCfg->tls.cert;
args.tlsKey = serverCfg->tls.key;
args.flags = args.tlsCert && args.tlsKey ? HTTP_FLAG_TLS : HTTP_FLAG_NONE;
Log(LOG_DEBUG, "HTTP listener: %lu", i);
LogConfigIndent(LogConfigGlobal());
Log(LOG_DEBUG, "Port: %hu", serverCfg->port);
Log(LOG_DEBUG, "Threads: %u", serverCfg->threads);
Log(LOG_DEBUG, "Max Connections: %u", serverCfg->maxConnections);
Log(LOG_DEBUG, "Flags: %d", args.flags);
Log(LOG_DEBUG, "TLS Cert: %s", serverCfg->tls.cert);
Log(LOG_DEBUG, "TLS Key: %s", serverCfg->tls.key);
LogConfigUnindent(LogConfigGlobal());
args.handler = MatrixHttpHandler;
args.handlerArgs = &matrixArgs;
if (serverCfg->tls.cert && serverCfg->tls.key)
if (args.flags & HTTP_FLAG_TLS)
{
if (UInt64Eq(UtilLastModified(serverCfg->tls.cert), UInt64Create(0, 0)))
{

View file

@ -104,4 +104,9 @@ extern Config * ConfigLock(Db *);
*/
extern int ConfigUnlock(Config *);
/**
* Converts a ConfigLogLevel into a valid syslog level.
*/
extern int ConfigLogLevelToSyslog(ConfigLogLevel);
#endif /* TELODENDRIA_CONFIG_H */