open5gs/src/app/context.c

302 lines
9.2 KiB
C
Raw Normal View History

2017-07-13 01:31:07 +00:00
#define TRACE_MODULE _context
2017-07-13 04:36:16 +00:00
#include "core_file.h"
2017-07-22 04:05:43 +00:00
#include "core_debug.h"
2017-10-28 15:44:09 +00:00
#include "core_lib.h"
2017-07-22 04:05:43 +00:00
#include <mongoc.h>
2017-07-13 04:36:16 +00:00
2017-07-13 01:31:07 +00:00
#include "context.h"
static context_t self;
static int context_initialized = 0;
status_t context_init()
{
d_assert(context_initialized == 0, return CORE_ERROR,
"Context already has been context_initialized");
memset(&self, 0, sizeof(context_t));
context_initialized = 1;
return CORE_OK;
}
2017-07-20 14:07:37 +00:00
status_t context_final()
{
d_assert(context_initialized == 1, return CORE_ERROR,
"Context already has been finalized");
2017-10-28 15:44:09 +00:00
if (self.config.bson)
bson_destroy(self.config.bson);
2017-07-20 14:07:37 +00:00
context_initialized = 0;
return CORE_OK;
}
context_t* context_self()
{
return &self;
}
2017-10-27 03:15:14 +00:00
status_t context_read_file()
2017-07-20 14:07:37 +00:00
{
char buf[MAX_ERROR_STRING_LEN];
config_t *config = &self.config;
status_t rv;
file_t *file;
2017-10-28 15:44:09 +00:00
bson_error_t error;
2017-07-20 14:07:37 +00:00
size_t json_len;
2017-10-28 15:44:09 +00:00
jsmn_parser parser;
2017-07-20 14:07:37 +00:00
int result;
2017-10-27 03:15:14 +00:00
d_assert(config->path, return CORE_ERROR,);
2017-07-20 14:07:37 +00:00
rv = file_open(&file, config->path, FILE_READ, FILE_OS_DEFAULT);
if (rv != CORE_OK)
{
d_fatal("Can't open configuration file '%s' (errno = %d, %s)",
config->path, rv, core_strerror(rv, buf, MAX_ERROR_STRING_LEN));
return rv;
}
json_len = MAX_CONFIG_FILE_SIZE;
rv = file_read(file, config->json, &json_len);
if (rv != CORE_OK)
{
d_fatal("Can't read configuration file '%s' (errno = %d, %s)",
config->path, rv, core_strerror(rv, buf, MAX_ERROR_STRING_LEN));
return rv;
}
file_close(file);
jsmn_init(&parser);
result = jsmn_parse(&parser, config->json, strlen(config->json),
config->token, sizeof(config->token)/sizeof(config->token[0]));
if (result < 0)
{
d_fatal("Failed to parse configuration file '%s' (jsmnerr = %d)",
config->path, result);
return CORE_ERROR;
}
if (result < 1 || config->token[0].type != JSMN_OBJECT)
{
d_fatal("Failed to parse configuration file '%s' (OBJECT expected)",
config->path);
return CORE_ERROR;
}
2017-10-28 15:44:09 +00:00
config->bson = bson_new_from_json((const uint8_t *)config->json, -1, &error);;
if (config->bson == NULL)
{
d_fatal("Failed to parse configuration file '%s'", config->path);
return CORE_ERROR;
}
2017-07-20 14:07:37 +00:00
d_print(" Config '%s'\n", config->path);
return CORE_OK;
}
2017-07-14 05:09:31 +00:00
status_t context_parse_config()
{
config_t *config = &self.config;
2017-10-28 15:44:09 +00:00
bson_iter_t iter, child1_iter, child2_iter;
c_uint32_t length = 0;
2017-07-14 05:09:31 +00:00
2017-10-28 15:44:09 +00:00
d_assert(config, return CORE_ERROR, );
2017-07-14 05:09:31 +00:00
self.log.console = -1;
2017-10-28 15:44:09 +00:00
if (!bson_iter_init(&iter, config->bson))
2017-07-14 05:09:31 +00:00
{
2017-10-28 15:44:09 +00:00
d_error("bson_iter_init failed in this document");
return CORE_ERROR;
}
2017-07-14 05:09:31 +00:00
2017-10-28 15:44:09 +00:00
while(bson_iter_next(&iter))
{
const char *key = bson_iter_key(&iter);
if (!strcmp(key, "DB_URI") && BSON_ITER_HOLDS_UTF8(&iter))
2017-07-14 05:09:31 +00:00
{
2017-10-28 15:44:09 +00:00
self.db_uri = bson_iter_utf8(&iter, &length);
}
else if (!strcmp(key, "LOG") && BSON_ITER_HOLDS_DOCUMENT(&iter))
{
bson_iter_recurse(&iter, &child1_iter);
while(bson_iter_next(&child1_iter))
2017-07-14 05:09:31 +00:00
{
2017-10-28 15:44:09 +00:00
const char *child1_key = bson_iter_key(&child1_iter);
if (!strcmp(child1_key, "CONSOLE") &&
BSON_ITER_HOLDS_INT32(&child1_iter))
{
self.log.console = bson_iter_int32(&child1_iter);
}
else if (!strcmp(child1_key, "SYSLOG") &&
2017-10-28 15:44:09 +00:00
BSON_ITER_HOLDS_UTF8(&child1_iter))
2017-07-31 13:35:25 +00:00
{
self.log.syslog = bson_iter_utf8(&child1_iter, &length);
2017-07-31 13:35:25 +00:00
}
2017-10-28 15:44:09 +00:00
else if (!strcmp(child1_key, "SOCKET") &&
BSON_ITER_HOLDS_DOCUMENT(&iter))
2017-07-31 13:35:25 +00:00
{
2017-10-28 15:44:09 +00:00
bson_iter_recurse(&child1_iter, &child2_iter);
while(bson_iter_next(&child2_iter))
2017-07-31 13:35:25 +00:00
{
2017-10-28 15:44:09 +00:00
const char *child2_key = bson_iter_key(&child2_iter);
if (!strcmp(child2_key, "FILE") &&
BSON_ITER_HOLDS_UTF8(&child2_iter))
2017-07-31 13:35:25 +00:00
{
2017-10-28 15:44:09 +00:00
self.log.socket.file =
bson_iter_utf8(&child2_iter, &length);
2017-07-31 13:35:25 +00:00
}
2017-10-28 15:44:09 +00:00
else if (!strcmp(child2_key, "UNIX_DOMAIN") &&
BSON_ITER_HOLDS_UTF8(&child2_iter))
2017-07-31 13:35:25 +00:00
{
2017-10-28 15:44:09 +00:00
self.log.socket.unix_domain =
bson_iter_utf8(&child2_iter, &length);
}
2017-07-31 13:35:25 +00:00
}
}
else if (!strcmp(child1_key, "FILE") &&
BSON_ITER_HOLDS_UTF8(&child1_iter))
{
self.log.file = bson_iter_utf8(&child1_iter, &length);
}
2017-07-14 05:09:31 +00:00
}
2017-10-28 15:44:09 +00:00
}
else if (!strcmp(key, "TRACE") && BSON_ITER_HOLDS_DOCUMENT(&iter))
{
bson_iter_recurse(&iter, &child1_iter);
while(bson_iter_next(&child1_iter))
2017-07-14 05:09:31 +00:00
{
2017-10-28 15:44:09 +00:00
const char *child1_key = bson_iter_key(&child1_iter);
if (!strcmp(child1_key, "S1AP") &&
BSON_ITER_HOLDS_INT32(&child1_iter))
{
self.trace_level.s1ap = bson_iter_int32(&child1_iter);
}
else if (!strcmp(child1_key, "NAS") &&
BSON_ITER_HOLDS_INT32(&child1_iter))
{
self.trace_level.nas = bson_iter_int32(&child1_iter);
}
else if (!strcmp(child1_key, "FD") &&
BSON_ITER_HOLDS_INT32(&child1_iter))
{
self.trace_level.fd = bson_iter_int32(&child1_iter);
}
else if (!strcmp(child1_key, "GTP") &&
BSON_ITER_HOLDS_INT32(&child1_iter))
{
self.trace_level.gtp = bson_iter_int32(&child1_iter);
}
else if (!strcmp(child1_key, "OTHERS") &&
BSON_ITER_HOLDS_INT32(&child1_iter))
{
self.trace_level.others = bson_iter_int32(&child1_iter);
}
2017-07-14 05:09:31 +00:00
}
2017-10-28 15:44:09 +00:00
}
else if (!strcmp(key, "NODE") && BSON_ITER_HOLDS_DOCUMENT(&iter))
{
bson_iter_recurse(&iter, &child1_iter);
while(bson_iter_next(&child1_iter))
2017-07-14 05:09:31 +00:00
{
2017-10-28 15:44:09 +00:00
const char *child1_key = bson_iter_key(&child1_iter);
if (!strcmp(child1_key, "DISABLE_HSS") &&
BSON_ITER_HOLDS_INT32(&child1_iter))
{
self.node.disable_hss = bson_iter_int32(&child1_iter);
}
else if (!strcmp(child1_key, "DISABLE_SGW") &&
BSON_ITER_HOLDS_INT32(&child1_iter))
{
self.node.disable_sgw = bson_iter_int32(&child1_iter);
}
else if (!strcmp(child1_key, "DISABLE_PGW") &&
BSON_ITER_HOLDS_INT32(&child1_iter))
{
self.node.disable_pgw = bson_iter_int32(&child1_iter);
}
else if (!strcmp(child1_key, "DISABLE_PCRF") &&
BSON_ITER_HOLDS_INT32(&child1_iter))
{
self.node.disable_pcrf = bson_iter_int32(&child1_iter);
}
2017-07-14 05:09:31 +00:00
}
}
}
return CORE_OK;
}
2017-10-28 15:44:09 +00:00
status_t context_db_init(const char *db_uri)
2017-07-13 01:31:07 +00:00
{
2017-07-20 14:07:37 +00:00
bson_t reply;
bson_error_t error;
bson_iter_t iter;
2017-07-13 01:31:07 +00:00
2017-07-20 14:07:37 +00:00
const mongoc_uri_t *uri;
2017-07-13 01:31:07 +00:00
2017-07-20 14:07:37 +00:00
mongoc_init();
2017-07-13 01:31:07 +00:00
2017-07-20 14:07:37 +00:00
self.db_client = mongoc_client_new(db_uri);
if (!self.db_client)
{
d_error("Failed to parse DB URI [%s]", db_uri);
return CORE_ERROR;
}
2017-07-13 04:36:16 +00:00
#if MONGOC_MAJOR_VERSION >= 1 && MONGOC_MINOR_VERSION >= 4
2017-07-20 14:07:37 +00:00
mongoc_client_set_error_api(self.db_client, 2);
#endif
2017-07-14 05:09:31 +00:00
2017-07-20 14:07:37 +00:00
uri = mongoc_client_get_uri(self.db_client);
d_assert(uri, context_db_final(); return CORE_ERROR,
"Database is not defined in DB_URI [%s]", db_uri);
2017-07-13 04:36:16 +00:00
2017-07-20 14:07:37 +00:00
self.db_name = (char *)mongoc_uri_get_database(uri);
d_assert(self.db_name, context_db_final(); return CORE_ERROR,
"Database is not defined in DB_URI [%s]", db_uri);
2017-07-13 04:36:16 +00:00
2017-07-20 14:07:37 +00:00
self.database = mongoc_client_get_database(self.db_client, self.db_name);
d_assert(self.database, context_db_final(); return CORE_ERROR,
"Database is not defined in DB_URI [%s]", db_uri);
2017-07-13 04:36:16 +00:00
2017-07-20 14:07:37 +00:00
if (!mongoc_client_get_server_status(self.db_client, NULL, &reply, &error))
2017-07-13 04:36:16 +00:00
{
2017-07-20 14:07:37 +00:00
d_error("Failed to conect to server [%s]", db_uri);
2017-10-31 12:29:39 +00:00
return CORE_EAGAIN;
2017-07-13 05:12:30 +00:00
}
2017-07-20 14:07:37 +00:00
d_assert(bson_iter_init_find(&iter, &reply, "ok"),
bson_destroy(&reply); context_db_final(); return CORE_ERROR,
"Invalid reply for server status [%s]", db_uri);
2017-07-13 04:36:16 +00:00
2017-07-20 14:07:37 +00:00
bson_destroy(&reply);
2017-07-13 04:36:16 +00:00
return CORE_OK;
}
2017-07-14 05:09:31 +00:00
2017-07-20 14:07:37 +00:00
status_t context_db_final()
{
if (self.database)
{
2017-07-20 14:07:37 +00:00
mongoc_database_destroy(self.database);
self.database = NULL;
}
2017-07-20 14:07:37 +00:00
if (self.db_client)
{
2017-07-20 14:07:37 +00:00
mongoc_client_destroy(self.db_client);
self.db_client = NULL;
}
2017-07-20 14:07:37 +00:00
mongoc_cleanup();
return CORE_OK;
}