File size: 5,450 Bytes
258fb37 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
/*
* Copyright (C) 2019-2023 by Sukchan Lee <acetcom@gmail.com>
*
* This file is part of Open5GS.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <mongoc.h>
#include "ogs-dbi.h"
int __ogs_dbi_domain;
static ogs_mongoc_t self;
/*
* We've added it
* Because the following function is deprecated in the mongo-c-driver
*/
static bool
ogs_mongoc_mongoc_client_get_server_status (mongoc_client_t *client, /* IN */
mongoc_read_prefs_t *read_prefs, /* IN */
bson_t *reply, /* OUT */
bson_error_t *error) /* OUT */
{
bson_t cmd = BSON_INITIALIZER;
bool ret = false;
BSON_ASSERT (client);
BSON_APPEND_INT32 (&cmd, "ping", 1);
ret = mongoc_client_command_simple (
client, "admin", &cmd, read_prefs, reply, error);
bson_destroy (&cmd);
return ret;
}
static char *masked_db_uri(const char *db_uri)
{
char *tmp;
char *array[2], *saveptr = NULL;
char *masked = NULL;
ogs_assert(db_uri);
tmp = ogs_strdup(db_uri);
ogs_assert(tmp);
memset(array, 0, sizeof(array));
array[0] = ogs_strtok_r(tmp, "@", &saveptr);
if (array[0])
array[1] = ogs_strtok_r(NULL, "@", &saveptr);
if (array[1]) {
masked = ogs_msprintf("mongodb://*****:*****@%s", array[1]);
ogs_assert(masked);
} else {
masked = ogs_strdup(array[0]);
ogs_assert(masked);
}
ogs_free(tmp);
return masked;
}
int ogs_mongoc_init(const char *db_uri)
{
bson_t reply;
bson_error_t error;
bson_iter_t iter;
const mongoc_uri_t *uri;
if (!db_uri) {
ogs_error("No DB_URI");
return OGS_ERROR;
}
memset(&self, 0, sizeof(ogs_mongoc_t));
self.masked_db_uri = masked_db_uri(db_uri);
mongoc_init();
self.initialized = true;
self.client = mongoc_client_new(db_uri);
if (!self.client) {
ogs_error("Failed to parse DB URI [%s]", self.masked_db_uri);
return OGS_ERROR;
}
#if MONGOC_CHECK_VERSION(1, 4, 0)
mongoc_client_set_error_api(self.client, 2);
#endif
uri = mongoc_client_get_uri(self.client);
ogs_assert(uri);
self.name = mongoc_uri_get_database(uri);
ogs_assert(self.name);
self.database = mongoc_client_get_database(self.client, self.name);
ogs_assert(self.database);
if (!ogs_mongoc_mongoc_client_get_server_status(
self.client, NULL, &reply, &error)) {
ogs_warn("Failed to connect to server [%s]", self.masked_db_uri);
return OGS_RETRY;
}
ogs_assert(bson_iter_init_find(&iter, &reply, "ok"));
bson_destroy(&reply);
ogs_info("MongoDB URI: '%s'", self.masked_db_uri);
return OGS_OK;
}
void ogs_mongoc_final(void)
{
if (self.database) {
mongoc_database_destroy(self.database);
self.database = NULL;
}
if (self.client) {
mongoc_client_destroy(self.client);
self.client = NULL;
}
if (self.masked_db_uri) {
ogs_free(self.masked_db_uri);
self.masked_db_uri = NULL;
}
if (self.initialized) {
mongoc_cleanup();
self.initialized = false;
}
}
ogs_mongoc_t *ogs_mongoc(void)
{
return &self;
}
int ogs_dbi_init(const char *db_uri)
{
int rv;
ogs_assert(db_uri);
rv = ogs_mongoc_init(db_uri);
if (rv != OGS_OK) return rv;
if (ogs_mongoc()->client && ogs_mongoc()->name) {
self.collection.subscriber = mongoc_client_get_collection(
ogs_mongoc()->client, ogs_mongoc()->name, "subscribers");
ogs_assert(self.collection.subscriber);
}
return OGS_OK;
}
void ogs_dbi_final(void)
{
if (self.collection.subscriber) {
mongoc_collection_destroy(self.collection.subscriber);
}
#if MONGOC_CHECK_VERSION(1, 9, 0)
if (self.stream) {
mongoc_change_stream_destroy(self.stream);
}
#endif
ogs_mongoc_final();
}
int ogs_dbi_collection_watch_init(void)
{
#if MONGOC_CHECK_VERSION(1, 9, 0)
bson_t empty = BSON_INITIALIZER;
const bson_t *err_doc;
bson_error_t error;
bson_t *options = BCON_NEW("fullDocument", "updateLookup");
ogs_mongoc()->stream = mongoc_collection_watch(self.collection.subscriber,
&empty, options);
if (mongoc_change_stream_error_document(ogs_mongoc()->stream, &error,
&err_doc)) {
if (!bson_empty (err_doc)) {
ogs_error("Change Stream Error. Enable replica sets to "
"enable database updates to be sent to MME.");
} else {
ogs_error("Client Error: %s\n", error.message);
}
return OGS_ERROR;
} else {
ogs_info("Change Streams are Enabled.");
}
return OGS_OK;
# else
return OGS_ERROR;
#endif
}
|