Esempio n. 1
0
void set_default_realm_name(char *realm) {
	lock_realms();
	ur_string_map_value_type value = (ur_string_map_value_type)default_realm_params_ptr;
	STRCPY(default_realm_params_ptr->options.name,realm);
	ur_string_map_put(realms, (ur_string_map_key_type)default_realm_params_ptr->options.name, value);
	add_to_secrets_list(&realms_list, realm);
	unlock_realms();
}
Esempio n. 2
0
realm_params_t* get_realm(char* name)
{
	if(name && name[0]) {
		lock_realms();
		ur_string_map_value_type value = 0;
		ur_string_map_key_type key = (ur_string_map_key_type)name;
		if (ur_string_map_get(realms, key, &value)) {
			unlock_realms();
			return (realm_params_t*)value;
		} else {
			realm_params_t *ret = (realm_params_t*)turn_malloc(sizeof(realm_params_t));
			ns_bcopy(default_realm_params_ptr,ret,sizeof(realm_params_t));
			STRCPY(ret->options.name,name);
			value = (ur_string_map_value_type)ret;
			ur_string_map_put(realms, key, value);
			ret->status.alloc_counters =  ur_string_map_create(NULL);
			add_to_secrets_list(&realms_list, name);
			unlock_realms();
			return ret;
		}
	}

	return default_realm_params_ptr;
}
Esempio n. 3
0
static void pgsql_reread_realms(secrets_list_t * realms_list) {
	PGconn * pqc = get_pqdb_connection();
	if(pqc) {
		char statement[TURN_LONG_STRING_SIZE];

		{
			snprintf(statement,sizeof(statement),"select origin,realm from turn_origin_to_realm");
			PGresult *res = PQexec(pqc, statement);

			if(res && (PQresultStatus(res) == PGRES_TUPLES_OK)) {

				ur_string_map *o_to_realm_new = ur_string_map_create(turn_free_simple);

				int i = 0;
				for(i=0;i<PQntuples(res);i++) {
					char *oval = PQgetvalue(res,i,0);
					if(oval) {
						char *rval = PQgetvalue(res,i,1);
						if(rval) {
							get_realm(rval);
							ur_string_map_value_type value = turn_strdup(rval);
							ur_string_map_put(o_to_realm_new, (const ur_string_map_key_type) oval, value);
						}
					}
				}

        update_o_to_realm(o_to_realm_new);
			}

			if(res) {
				PQclear(res);
			}
		}

		{
			{
				size_t i = 0;
				size_t rlsz = 0;

				lock_realms();
				rlsz = realms_list->sz;
				unlock_realms();

				for (i = 0; i<rlsz; ++i) {

					char *realm = realms_list->secrets[i];

					realm_params_t* rp = get_realm(realm);

					lock_realms();
					rp->options.perf_options.max_bps = turn_params.max_bps;
					unlock_realms();

					lock_realms();
					rp->options.perf_options.total_quota = turn_params.total_quota;
					unlock_realms();

					lock_realms();
					rp->options.perf_options.user_quota = turn_params.user_quota;
					unlock_realms();

				}
			}

			snprintf(statement,sizeof(statement),"select realm,opt,value from turn_realm_option");
			PGresult *res = PQexec(pqc, statement);

			if(res && (PQresultStatus(res) == PGRES_TUPLES_OK)) {

				int i = 0;
				for(i=0;i<PQntuples(res);i++) {
					char *rval = PQgetvalue(res,i,0);
					char *oval = PQgetvalue(res,i,1);
					char *vval = PQgetvalue(res,i,2);
					if(rval && oval && vval) {
						realm_params_t* rp = get_realm(rval);
						if(!strcmp(oval,"max-bps"))
							rp->options.perf_options.max_bps = (band_limit_t)strtoul(vval,NULL,10);
						else if(!strcmp(oval,"total-quota"))
							rp->options.perf_options.total_quota = (vint)atoi(vval);
						else if(!strcmp(oval,"user-quota"))
							rp->options.perf_options.user_quota = (vint)atoi(vval);
						else {
							TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unknown realm option: %s\n", oval);
						}
					}
				}
			}

			if(res) {
				PQclear(res);
			}
		}
	}
}
Esempio n. 4
0
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);
}