bool _mongoc_convert_array (mongoc_client_t *client, const bson_iter_t *iter, bson_t *doc, bson_error_t *error) { uint32_t len; const uint8_t *data; bson_t value; if (!BSON_ITER_HOLDS_ARRAY (iter)) { CONVERSION_ERR ("Invalid field \"%s\" in opts, should contain array," " not %s", bson_iter_key (iter), _mongoc_bson_type_to_str (bson_iter_type (iter))); } bson_iter_array (iter, &len, &data); if (!bson_init_static (&value, data, len)) { BSON_ERR ("Corrupt BSON in field \"%s\" in opts", bson_iter_key (iter)); } bson_destroy (doc); bson_copy_to (&value, doc); return true; }
int monary_load_size_value(const bson_iter_t* bsonit, monary_column_item* citem, int idx) { bson_type_t type; const uint8_t* discard; uint32_t size; uint32_t* dest; type = bson_iter_type(bsonit); switch (type) { case BSON_TYPE_UTF8: case BSON_TYPE_CODE: bson_iter_utf8(bsonit, &size); break; case BSON_TYPE_BINARY: bson_iter_binary(bsonit, NULL, &size, &discard); break; case BSON_TYPE_DOCUMENT: bson_iter_document(bsonit, &size, &discard); break; case BSON_TYPE_ARRAY: bson_iter_array(bsonit, &size, &discard); break; default: return 0; } dest = ((uint32_t*) citem->storage) + idx; memcpy(dest, &size, sizeof(uint32_t)); return 1; }
types::b_array element::get_array() const { CITER; const std::uint8_t* buf; std::uint32_t len; bson_iter_array(&iter, &len, &buf); return types::b_array{document::view{buf, len}}; }
types::b_array element::get_array() const { BSONCXX_TYPE_CHECK(k_array); BSONCXX_CITER; const std::uint8_t* buf; std::uint32_t len; bson_iter_array(&iter, &len, &buf); return types::b_array{array::view{buf, len}}; }
/** * _mongoc_gridfs_file_new_from_bson: * * creates a gridfs file from a bson object * * This is only really useful for instantiating a gridfs file from a server * side object */ mongoc_gridfs_file_t * _mongoc_gridfs_file_new_from_bson (mongoc_gridfs_t *gridfs, const bson_t *data) { mongoc_gridfs_file_t *file; const char *key; bson_iter_t iter; const uint8_t *buf; uint32_t buf_len; ENTRY; BSON_ASSERT (gridfs); BSON_ASSERT (data); file = bson_malloc0 (sizeof *file); file->gridfs = gridfs; bson_copy_to (data, &file->bson); bson_iter_init (&iter, &file->bson); while (bson_iter_next (&iter)) { key = bson_iter_key (&iter); if (0 == strcmp (key, "_id")) { bson_oid_copy (bson_iter_oid (&iter), &file->files_id); } else if (0 == strcmp (key, "length")) { file->length = bson_iter_int64 (&iter); } else if (0 == strcmp (key, "chunkSize")) { file->chunk_size = bson_iter_int32 (&iter); } else if (0 == strcmp (key, "uploadDate")) { file->upload_date = bson_iter_date_time (&iter); } else if (0 == strcmp (key, "md5")) { file->bson_md5 = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "filename")) { file->bson_filename = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "contentType")) { file->bson_content_type = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "aliases")) { bson_iter_array (&iter, &buf_len, &buf); bson_init_static (&file->bson_aliases, buf, buf_len); } else if (0 == strcmp (key, "metadata")) { bson_iter_document (&iter, &buf_len, &buf); bson_init_static (&file->bson_metadata, buf, buf_len); } } /* TODO: is there are a minimal object we should be verifying that we * actually have here? */ RETURN (file); }
void bsonToArray(bson_iter_t* iter, Array* output, bool isDocument) { bson_t bson; const uint8_t *document = NULL; uint32_t document_len = 0; if (isDocument) { bson_iter_document(iter, &document_len, &document); } else { bson_iter_array(iter, &document_len, &document); } bson_init_static(&bson, document, document_len); Array child = Array::Create(); bsonToVariant(&bson, &child); output->add( String(bson_iter_key(iter)), child ); }
/** * _mongoc_gridfs_file_new_from_bson: * * creates a gridfs file from a bson object * * This is only really useful for instantiating a gridfs file from a server * side object */ mongoc_gridfs_file_t * _mongoc_gridfs_file_new_from_bson (mongoc_gridfs_t *gridfs, const bson_t *data) { mongoc_gridfs_file_t *file; const bson_value_t *value; const char *key; bson_iter_t iter; const uint8_t *buf; uint32_t buf_len; ENTRY; BSON_ASSERT (gridfs); BSON_ASSERT (data); file = (mongoc_gridfs_file_t *)bson_malloc0 (sizeof *file); file->gridfs = gridfs; bson_copy_to (data, &file->bson); bson_iter_init (&iter, &file->bson); while (bson_iter_next (&iter)) { key = bson_iter_key (&iter); if (0 == strcmp (key, "_id")) { value = bson_iter_value (&iter); bson_value_copy (value, &file->files_id); } else if (0 == strcmp (key, "length")) { if (!BSON_ITER_HOLDS_INT32 (&iter) && !BSON_ITER_HOLDS_INT64 (&iter) && !BSON_ITER_HOLDS_DOUBLE (&iter)) { GOTO (failure); } file->length = bson_iter_as_int64 (&iter); } else if (0 == strcmp (key, "chunkSize")) { if (!BSON_ITER_HOLDS_INT32 (&iter) && !BSON_ITER_HOLDS_INT64 (&iter) && !BSON_ITER_HOLDS_DOUBLE (&iter)) { GOTO (failure); } if (bson_iter_as_int64 (&iter) > INT32_MAX) { GOTO (failure); } file->chunk_size = (int32_t)bson_iter_as_int64 (&iter); } else if (0 == strcmp (key, "uploadDate")) { if (!BSON_ITER_HOLDS_DATE_TIME (&iter)){ GOTO (failure); } file->upload_date = bson_iter_date_time (&iter); } else if (0 == strcmp (key, "md5")) { if (!BSON_ITER_HOLDS_UTF8 (&iter)) { GOTO (failure); } file->bson_md5 = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "filename")) { if (!BSON_ITER_HOLDS_UTF8 (&iter)) { GOTO (failure); } file->bson_filename = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "contentType")) { if (!BSON_ITER_HOLDS_UTF8 (&iter)) { GOTO (failure); } file->bson_content_type = bson_iter_utf8 (&iter, NULL); } else if (0 == strcmp (key, "aliases")) { if (!BSON_ITER_HOLDS_ARRAY (&iter)) { GOTO (failure); } bson_iter_array (&iter, &buf_len, &buf); bson_init_static (&file->bson_aliases, buf, buf_len); } else if (0 == strcmp (key, "metadata")) { if (!BSON_ITER_HOLDS_DOCUMENT (&iter)) { GOTO (failure); } bson_iter_document (&iter, &buf_len, &buf); bson_init_static (&file->bson_metadata, buf, buf_len); } } /* TODO: is there are a minimal object we should be verifying that we * actually have here? */ RETURN (file); failure: bson_destroy (&file->bson); RETURN (NULL); }
int be_mongo_aclcheck(void *conf, const char *clientid, const char *username, const char *topic, int acc) { struct mongo_backend *handle = (struct mongo_backend *) conf; mongoc_collection_t *collection; mongoc_cursor_t *cursor; bson_error_t error; const bson_t *doc; bson_iter_t iter; bool check = false; int match = 0, foundFlag = 0; bson_t query; bson_init(&query); bson_append_utf8(&query, "username", -1, username, -1); collection = mongoc_client_get_collection(handle->client, dbName, colName); cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 0, 0, &query, NULL, NULL); while (!mongoc_cursor_error (cursor, &error) && mongoc_cursor_more (cursor)) { if (foundFlag == 0 && mongoc_cursor_next (cursor, &doc)) { bson_iter_init(&iter, doc); bson_iter_find(&iter, topicLoc); int64_t topId = (int64_t) bson_iter_as_int64(&iter);//, NULL); bson_destroy(&query); mongoc_cursor_destroy(cursor); mongoc_collection_destroy(collection); bson_init(&query); bson_append_int64(&query, topicID, -1, topId); collection = mongoc_client_get_collection(handle->client, dbName, topicLoc); cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 0, 0, &query, NULL, NULL); foundFlag = 1; } if (foundFlag == 1 && mongoc_cursor_next(cursor, &doc)) { bson_iter_init(&iter, doc); bson_iter_find(&iter, topicLoc); uint32_t len; const uint8_t *arr; bson_iter_array(&iter, &len, &arr); bson_t b; if (bson_init_static(&b, arr, len)) { bson_iter_init(&iter, &b); while (bson_iter_next(&iter)) { char *str = bson_iter_dup_utf8(&iter, &len); mosquitto_topic_matches_sub(str, topic, &check); if (check) { match = 1; bson_free(str); break; } bson_free(str); } } } } if (mongoc_cursor_error (cursor, &error)) { fprintf (stderr, "Cursor Failure: %s\n", error.message); return 0; } bson_destroy(&query); mongoc_cursor_destroy (cursor); mongoc_collection_destroy(collection); return match; }
struct transaction* transaction_from_bson(bson_t const* doc) { char key[9]; bson_iter_t iter; bson_iter_t subiter; struct transaction* tx = transaction_new(); if(!bson_iter_init_find(&iter, doc, "version") || !BSON_ITER_HOLDS_INT32(&iter)) goto error; transaction_set_version(tx, bson_iter_int32(&iter)); // Read Inputs if(!bson_iter_init_find(&iter, doc, "inputs") || !BSON_ITER_HOLDS_ARRAY(&iter)) goto error; uint32_t inputs_doc_length; uint8_t const* inputs_doc_data; bson_iter_array(&iter, &inputs_doc_length, &inputs_doc_data); bson_t inputs_doc; bson_init_static(&inputs_doc, inputs_doc_data, inputs_doc_length); size_t index = 0; for(;;) { bson_snprintf(key, sizeof(key), "%u", (unsigned int)index); key[sizeof(key) - 1] = '\0'; // If the array key isn't found, then we reached the end of the array if(!bson_iter_init_find(&subiter, &inputs_doc, key)) break; // If it's not a document, then there's an error if(!BSON_ITER_HOLDS_DOCUMENT(&subiter)) goto error; struct transaction_input* input = transaction_input_new(); struct transaction_output_reference* output_reference = transaction_input_output_reference(input); // Load the input document bson_t element_doc; uint32_t element_doc_length; uint8_t const* element_doc_data; bson_iter_document(&subiter, &element_doc_length, &element_doc_data); bson_init_static(&element_doc, element_doc_data, element_doc_length); bson_iter_t elementiter; // Output reference if(!bson_iter_init_find(&elementiter, &element_doc, "output_reference") || !BSON_ITER_HOLDS_DOCUMENT(&elementiter)) goto error; bson_t output_reference_doc; uint32_t output_reference_doc_length; uint8_t const* output_reference_doc_data; bson_iter_document(&elementiter, &output_reference_doc_length, &output_reference_doc_data); bson_init_static(&output_reference_doc, output_reference_doc_data, output_reference_doc_length); bson_iter_t output_reference_iter; uint8_t const* hash; uint32_t hash_size; if(!bson_iter_init_find(&output_reference_iter, &output_reference_doc, "hash") || !BSON_ITER_HOLDS_BINARY(&output_reference_iter)) goto error; bson_iter_binary(&output_reference_iter, BSON_SUBTYPE_BINARY, &hash_size, &hash); assert(hash_size == 32); transaction_output_reference_set_hash(output_reference, (unsigned char const*)hash); if(!bson_iter_init_find(&output_reference_iter, &output_reference_doc, "index") || !BSON_ITER_HOLDS_INT32(&output_reference_iter)) goto error; transaction_output_reference_set_index(output_reference, bson_iter_int32(&output_reference_iter)); // Script if(!bson_iter_init_find(&elementiter, &element_doc, "script") || !BSON_ITER_HOLDS_BINARY(&elementiter)) goto error; uint32_t script_size; uint8_t const* script_data; bson_iter_binary(&elementiter, BSON_SUBTYPE_BINARY, &script_size, &script_data); struct script* script; size_t script_size_result; script_size_result = unserialize_script((unsigned char const*)script_data, script_size, &script, script_size); assert(script_size_result == script_size); transaction_input_set_script(input, script); // Sequence if(!bson_iter_init_find(&elementiter, &element_doc, "sequence") || !BSON_ITER_HOLDS_INT32(&elementiter)) goto error; transaction_input_set_sequence(input, bson_iter_int32(&elementiter)); transaction_add_input(tx, input); index += 1; } // Read Outputs if(!bson_iter_init_find(&iter, doc, "outputs") || !BSON_ITER_HOLDS_ARRAY(&iter)) goto error; uint32_t outputs_doc_length; uint8_t const* outputs_doc_data; bson_iter_array(&iter, &outputs_doc_length, &outputs_doc_data); bson_t outputs_doc; bson_init_static(&outputs_doc, outputs_doc_data, outputs_doc_length); index = 0; for(;;) { bson_snprintf(key, sizeof(key), "%u", (unsigned int)index); key[sizeof(key) - 1] = '\0'; // If the array key isn't found, then we reached the end of the array if(!bson_iter_init_find(&subiter, &outputs_doc, key)) break; // If it's not a document, then there's an error if(!BSON_ITER_HOLDS_DOCUMENT(&subiter)) goto error; struct transaction_output* output = transaction_output_new(); // Load the output document bson_t element_doc; uint32_t element_doc_length; uint8_t const* element_doc_data; bson_iter_document(&subiter, &element_doc_length, &element_doc_data); bson_init_static(&element_doc, element_doc_data, element_doc_length); bson_iter_t elementiter; // Value if(!bson_iter_init_find(&elementiter, &element_doc, "value") || !BSON_ITER_HOLDS_INT64(&elementiter)) goto error; transaction_output_set_value(output, bson_iter_int64(&elementiter)); // Script if(!bson_iter_init_find(&elementiter, &element_doc, "script") || !BSON_ITER_HOLDS_BINARY(&elementiter)) goto error; uint32_t script_size; uint8_t const* script_data; bson_iter_binary(&elementiter, BSON_SUBTYPE_BINARY, &script_size, &script_data); struct script* script; size_t script_size_result; script_size_result = unserialize_script((unsigned char const*)script_data, script_size, &script, script_size); assert(script_size_result == script_size); transaction_output_set_script(output, script); transaction_add_output(tx, output); index += 1; } if(!bson_iter_init_find(&iter, doc, "lock_time") || !BSON_ITER_HOLDS_INT32(&iter)) goto error; transaction_set_lock_time(tx, bson_iter_int32(&iter)); return tx; error: return NULL; }
void mongoc_server_description_handle_ismaster ( mongoc_server_description_t *sd, const bson_t *ismaster_response, int64_t rtt_msec, bson_error_t *error) { bson_iter_t iter; bool is_master = false; bool is_shard = false; bool is_secondary = false; bool is_arbiter = false; bool is_replicaset = false; bool is_hidden = false; const uint8_t *bytes; uint32_t len; int num_keys = 0; ENTRY; BSON_ASSERT (sd); mongoc_server_description_reset (sd); if (!ismaster_response) { EXIT; } bson_destroy (&sd->last_is_master); bson_copy_to (ismaster_response, &sd->last_is_master); sd->has_is_master = true; bson_iter_init (&iter, &sd->last_is_master); while (bson_iter_next (&iter)) { num_keys++; if (strcmp ("ok", bson_iter_key (&iter)) == 0) { /* ismaster responses never have ok: 0, but spec requires we check */ if (! bson_iter_as_bool (&iter)) goto failure; } else if (strcmp ("ismaster", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_BOOL (&iter)) goto failure; is_master = bson_iter_bool (&iter); } else if (strcmp ("maxMessageSizeBytes", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_INT32 (&iter)) goto failure; sd->max_msg_size = bson_iter_int32 (&iter); } else if (strcmp ("maxBsonObjectSize", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_INT32 (&iter)) goto failure; sd->max_bson_obj_size = bson_iter_int32 (&iter); } else if (strcmp ("maxWriteBatchSize", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_INT32 (&iter)) goto failure; sd->max_write_batch_size = bson_iter_int32 (&iter); } else if (strcmp ("minWireVersion", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_INT32 (&iter)) goto failure; sd->min_wire_version = bson_iter_int32 (&iter); } else if (strcmp ("maxWireVersion", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_INT32 (&iter)) goto failure; sd->max_wire_version = bson_iter_int32 (&iter); } else if (strcmp ("msg", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_UTF8 (&iter)) goto failure; is_shard = !!bson_iter_utf8 (&iter, NULL); } else if (strcmp ("setName", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_UTF8 (&iter)) goto failure; sd->set_name = bson_iter_utf8 (&iter, NULL); } else if (strcmp ("secondary", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_BOOL (&iter)) goto failure; is_secondary = bson_iter_bool (&iter); } else if (strcmp ("hosts", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_ARRAY (&iter)) goto failure; bson_iter_array (&iter, &len, &bytes); bson_init_static (&sd->hosts, bytes, len); } else if (strcmp ("passives", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_ARRAY (&iter)) goto failure; bson_iter_array (&iter, &len, &bytes); bson_init_static (&sd->passives, bytes, len); } else if (strcmp ("arbiters", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_ARRAY (&iter)) goto failure; bson_iter_array (&iter, &len, &bytes); bson_init_static (&sd->arbiters, bytes, len); } else if (strcmp ("primary", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_UTF8 (&iter)) goto failure; sd->current_primary = bson_iter_utf8 (&iter, NULL); } else if (strcmp ("arbiterOnly", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_BOOL (&iter)) goto failure; is_arbiter = bson_iter_bool (&iter); } else if (strcmp ("isreplicaset", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_BOOL (&iter)) goto failure; is_replicaset = bson_iter_bool (&iter); } else if (strcmp ("tags", bson_iter_key (&iter)) == 0) { if (! BSON_ITER_HOLDS_DOCUMENT (&iter)) goto failure; bson_iter_document (&iter, &len, &bytes); bson_init_static (&sd->tags, bytes, len); } else if (strcmp ("hidden", bson_iter_key (&iter)) == 0) { is_hidden = bson_iter_bool (&iter); } } if (is_shard) { sd->type = MONGOC_SERVER_MONGOS; } else if (sd->set_name) { if (is_hidden) { sd->type = MONGOC_SERVER_RS_OTHER; } else if (is_master) { sd->type = MONGOC_SERVER_RS_PRIMARY; } else if (is_secondary) { sd->type = MONGOC_SERVER_RS_SECONDARY; } else if (is_arbiter) { sd->type = MONGOC_SERVER_RS_ARBITER; } else { sd->type = MONGOC_SERVER_RS_OTHER; } } else if (is_replicaset) { sd->type = MONGOC_SERVER_RS_GHOST; } else if (num_keys > 0) { sd->type = MONGOC_SERVER_STANDALONE; } else { sd->type = MONGOC_SERVER_UNKNOWN; } mongoc_server_description_update_rtt(sd, rtt_msec); EXIT; failure: sd->type = MONGOC_SERVER_UNKNOWN; sd->round_trip_time = -1; EXIT; }
QVariantMap TBson::fromBson(const TBsonObject *obj) { QVariantMap ret; bson_iter_t it; const bson_t *bson = (const bson_t *)obj; bson_iter_init(&it, bson); while (bson_iter_next(&it)) { bson_type_t t = bson_iter_type(&it); QString key(bson_iter_key(&it)); switch (t) { case BSON_TYPE_EOD: return ret; break; case BSON_TYPE_DOUBLE: ret[key] = bson_iter_double(&it); break; case BSON_TYPE_UTF8: ret[key] = QString::fromUtf8(bson_iter_utf8(&it, nullptr)); break; case BSON_TYPE_ARRAY: { const uint8_t *docbuf = nullptr; uint32_t doclen = 0; bson_t sub[1]; bson_iter_array(&it, &doclen, &docbuf); if (bson_init_static(sub, docbuf, doclen)) { ret[key] = fromBson(sub).values(); } break; } case BSON_TYPE_DOCUMENT: { const uint8_t *docbuf = nullptr; uint32_t doclen = 0; bson_t sub[1]; bson_iter_document(&it, &doclen, &docbuf); if (bson_init_static(sub, docbuf, doclen)) { ret[key] = fromBson(sub); } break; } case BSON_TYPE_BINARY: { const uint8_t *binary = nullptr; bson_subtype_t subtype = BSON_SUBTYPE_BINARY; uint32_t len = 0; bson_iter_binary(&it, &subtype, &len, &binary); if (binary) { ret[key] = QByteArray((char *)binary, len); } break; } case BSON_TYPE_UNDEFINED: ret[key] = QVariant(); break; case BSON_TYPE_OID: { char oidhex[25]; bson_oid_to_string(bson_iter_oid(&it), oidhex); ret[key] = QString(oidhex); break; } case BSON_TYPE_BOOL: ret[key] = (bool)bson_iter_bool(&it); break; case BSON_TYPE_DATE_TIME: { #if QT_VERSION >= 0x040700 QDateTime date; date.setMSecsSinceEpoch(bson_iter_date_time(&it)); #else qint64 val = bson_iter_date_time(&it); qint64 days = val / 86400000; // 24*60*60*1000 int msecs = val % 86400000; QDate dt = QDate(1970, 1, 1).addDays(days); QTime tm = QTime(0, 0, 0).addMSecs(msecs); QDateTime date(dt, tm, Qt::UTC); #endif ret[key] = date; break; } case BSON_TYPE_NULL: ret[key] = QVariant(); break; case BSON_TYPE_REGEX: ret[key] = QRegExp(QLatin1String(bson_iter_regex(&it, nullptr))); break; case BSON_TYPE_CODE: ret[key] = QString(bson_iter_code(&it, nullptr)); break; case BSON_TYPE_SYMBOL: ret[key] = QString(bson_iter_symbol(&it, nullptr)); break; case BSON_TYPE_INT32: ret[key] = bson_iter_int32(&it); break; case BSON_TYPE_INT64: ret[key] = (qint64)bson_iter_int64(&it); break; case BSON_TYPE_CODEWSCOPE: // FALL THROUGH case BSON_TYPE_TIMESTAMP: // FALL THROUGH (internal use) // do nothing break; default: tError("fromBson() unknown type: %d", t); break; } //tSystemDebug("fromBson : t:%d key:%s = %s", t, qPrintable(key), qPrintable(ret[key].toString())); } return ret; }
static int mongo_read_realms_ip_lists(const char *kind, ip_range_list_t * list) { int ret = 0; char field_name[129]; sprintf(field_name, "%s_peer_ip", kind); mongoc_collection_t * collection = mongo_get_collection("realm"); if (!collection) return ret; bson_t query; bson_init(&query); bson_t fields; bson_init(&fields); BSON_APPEND_INT32(&fields, "realm", 1); BSON_APPEND_INT32(&fields, field_name, 1); mongoc_cursor_t * cursor; cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 0, 0, &query, &fields, NULL); if (!cursor) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error querying MongoDB collection 'realm'\n"); ret = -1; } else { const bson_t * item; uint32_t length; bson_iter_t iter; char realm[513]; while (mongoc_cursor_next(cursor, &item)) { if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "realm") && BSON_ITER_HOLDS_UTF8(&iter)) { STRCPY(realm,bson_iter_utf8(&iter, &length)); if (bson_iter_init(&iter, item) && bson_iter_find(&iter, field_name) && BSON_ITER_HOLDS_ARRAY(&iter)) { const uint8_t *docbuf = NULL; uint32_t doclen = 0; bson_t ip_range_array; bson_iter_t ip_range_iter; bson_iter_array(&iter, &doclen, &docbuf); bson_init_static(&ip_range_array, docbuf, doclen); if (bson_iter_init(&ip_range_iter, &ip_range_array)) { while (bson_iter_next(&ip_range_iter)) { if (BSON_ITER_HOLDS_UTF8(&ip_range_iter)) { const char* ip_range = bson_iter_utf8(&ip_range_iter, &length); add_ip_list_range(ip_range, realm, list); } } } } } } mongoc_cursor_destroy(cursor); } mongoc_collection_destroy(collection); bson_destroy(&query); bson_destroy(&fields); return ret; }
static int mongo_list_origins(u08bits *realm, secrets_list_t *origins, secrets_list_t *realms) { mongoc_collection_t * collection = mongo_get_collection("realm"); if(!collection) return -1; u08bits realm0[STUN_MAX_REALM_SIZE+1] = "\0"; if(!realm) realm=realm0; bson_t query, child; bson_init(&query); bson_append_document_begin(&query, "$orderby", -1, &child); BSON_APPEND_INT32(&child, "realm", 1); bson_append_document_end(&query, &child); bson_append_document_begin(&query, "$query", -1, &child); if (realm && realm[0]) { BSON_APPEND_UTF8(&child, "realm", (const char *)realm); } bson_append_document_end(&query, &child); bson_t fields; bson_init(&fields); BSON_APPEND_INT32(&fields, "origin", 1); BSON_APPEND_INT32(&fields, "realm", 1); mongoc_cursor_t * cursor; cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 0, 0, &query, &fields, NULL); int ret = -1; if (!cursor) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error querying MongoDB collection 'realm'\n"); } else { const bson_t * item; uint32_t length; bson_iter_t iter; while (mongoc_cursor_next(cursor, &item)) { if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "realm") && BSON_ITER_HOLDS_UTF8(&iter)) { const char * _realm = bson_iter_utf8(&iter, &length); if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "origin") && BSON_ITER_HOLDS_ARRAY(&iter)) { const uint8_t *docbuf = NULL; uint32_t doclen = 0; bson_t origin_array; bson_iter_t origin_iter; bson_iter_array(&iter, &doclen, &docbuf); bson_init_static(&origin_array, docbuf, doclen); if (bson_iter_init(&origin_iter, &origin_array)) { while(bson_iter_next(&origin_iter)) { if (BSON_ITER_HOLDS_UTF8(&origin_iter)) { const char * _origin = bson_iter_utf8(&origin_iter, &length); if(origins) { add_to_secrets_list(origins,_origin); if(realms) { add_to_secrets_list(realms,_realm); } } else { printf("%s ==>> %s\n", _realm, _origin); } } } } } } } mongoc_cursor_destroy(cursor); ret = 0; } mongoc_collection_destroy(collection); bson_destroy(&query); bson_destroy(&fields); return ret; }
static void mongo_reread_realms(secrets_list_t * realms_list) { UNUSED_ARG(realms_list); mongoc_collection_t * collection = mongo_get_collection("realm"); if (!collection) return; bson_t query; bson_init(&query); bson_t fields; bson_init(&fields); BSON_APPEND_INT32(&fields, "realm", 1); BSON_APPEND_INT32(&fields, "origin", 1); BSON_APPEND_INT32(&fields, "options", 1); mongoc_cursor_t * cursor; cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 0, 0, &query, &fields, NULL); if (!cursor) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error querying MongoDB collection 'realm'\n"); } else { ur_string_map *o_to_realm_new = ur_string_map_create(turn_free_simple); const bson_t * item; uint32_t length; bson_iter_t iter; while (mongoc_cursor_next(cursor, &item)) { if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "realm") && BSON_ITER_HOLDS_UTF8(&iter)) { char * _realm = turn_strdup(bson_iter_utf8(&iter, &length)); get_realm(_realm); if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "origin") && BSON_ITER_HOLDS_ARRAY(&iter)) { const uint8_t *docbuf = NULL; uint32_t doclen = 0; bson_t origin_array; bson_iter_t origin_iter; bson_iter_array(&iter, &doclen, &docbuf); bson_init_static(&origin_array, docbuf, doclen); if (bson_iter_init(&origin_iter, &origin_array)) { while (bson_iter_next(&origin_iter)) { if (BSON_ITER_HOLDS_UTF8(&origin_iter)) { char* _origin = turn_strdup(bson_iter_utf8(&origin_iter, &length)); char *rval = turn_strdup(_realm); ur_string_map_value_type value = (ur_string_map_value_type) (rval); ur_string_map_put(o_to_realm_new, (const ur_string_map_key_type) _origin, value); turn_free(_origin,strlen(_origin)+1); } } } } realm_params_t* rp = get_realm(_realm); lock_realms(); rp->options.perf_options.max_bps = turn_params.max_bps; rp->options.perf_options.total_quota = turn_params.total_quota; rp->options.perf_options.user_quota = turn_params.user_quota; unlock_realms(); if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "options") && BSON_ITER_HOLDS_DOCUMENT(&iter)) { const uint8_t *docbuf = NULL; uint32_t doclen = 0; bson_t options; bson_iter_t options_iter; bson_iter_document(&iter, &doclen, &docbuf); bson_init_static(&options, docbuf, doclen); if (bson_iter_init(&options_iter, &options)) { while (bson_iter_next(&options_iter)) { const char * _k = bson_iter_key(&options_iter); uint64_t _v = 0; if (BSON_ITER_HOLDS_DOUBLE(&options_iter)) { _v = (uint64_t) bson_iter_double(&options_iter); } else if (BSON_ITER_HOLDS_INT32(&options_iter)) { _v = (uint64_t)bson_iter_int32(&options_iter); } else if (BSON_ITER_HOLDS_INT64(&options_iter)) { _v = (uint64_t) bson_iter_int64(&options_iter); } if (_v) { if (!strcmp(_k, "max-bps")) rp->options.perf_options.max_bps = (band_limit_t) _v; else if (!strcmp(_k, "total-quota")) rp->options.perf_options.total_quota = (vint) _v; else if (!strcmp(_k, "user-quota")) rp->options.perf_options.user_quota = (vint) _v; else { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unknown realm option: %s\n", _k); } } } } } turn_free(_realm,strlen(_realm)+1); } } update_o_to_realm(o_to_realm_new); mongoc_cursor_destroy(cursor); } mongoc_collection_destroy(collection); bson_destroy(&query); bson_destroy(&fields); }