|
DTNMA Reference Tools v2.1.0 - 19.gcd454d0
Delay-Tolerant Networking Management Architecture (DTNMA) Tool Suite
|
This file is only included in the build when ::HAVE_POSTGRESQL is defined. More...
#include "nm_sql.h"#include <cace/ari/cbor.h>#include <cace/amm/typing.h>#include <cace/amm/semtype.h>#include <cace/util/logging.h>#include <cace/ari/text.h>#include <cace/ari/text_util.h>#include <string.h>#include <arpa/inet.h>#include <m-bstring.h>
Include dependency graph for nm_sql.c:Data Structures | |
| struct | refdm_db_pool_t |
Macros | |
| #define | checkConn(idx) (idx < MGR_NUM_SQL_CONNECTIONS && dbpool[idx].conn != NULL) |
| #define | getConn(idx) pthread_mutex_lock(&dbpool[idx].lock) |
| #define | giveConn(idx) pthread_mutex_unlock(&dbpool[idx].lock) |
| #define | dbprep_bind_param_bool(idx, var) |
| #define | dbprep_bind_param_int(idx, var) |
| #define | dbprep_bind_param_short(idx, var) |
| #define | dbprep_bind_param_float(idx, var) |
| #define | dbprep_bind_param_double(idx, var) |
| #define | dbprep_bind_param_bigint(idx, var) |
| #define | dbprep_bind_param_str(idx, var) |
| #define | dbprep_bind_param_null(idx) |
| #define | dbprep_bind_param_byte(idx, var, length) |
| #define | dbprep_declare(dbidx, idx, params, cols) |
| #define | dbexec_prepared |
| #define | dbtest_result(expected) ((PQresultStatus(res) == expected) ? 0 : 1) |
| #define | DB_CHKVOID(status) |
| #define | DB_CHKINT(status) |
| #define | DB_CHKNULL(status) |
| #define | DB_CHKUSR(status, usr) |
| #define | query_log_err(status) CACE_LOG_ERR("ERROR at %s %i: %s (errno: %d)", __FILE__, __LINE__, PQresultErrorMessage(res), status); |
Enumerations | |
| enum | db_con_t { DB_CTRL_CON , DB_RPT_CON , DB_REST_CON , MGR_NUM_SQL_CONNECTIONS } |
| enum | queries { ARI_RPTSET_INSERT , ARI_AGENT_INSERT , ARI_EXECSET_INSERT , REFDM_DB_LOG_MSG , MGR_NUM_QUERIES } |
Functions | |
| static char * | db_mgr_sql_prepare (size_t idx, const char *query, char *stmtName, int nParams, const Oid *paramTypes) |
| cace_ari_ac_t * | db_query_ac (size_t dbidx, int ac_id) |
| void | refdm_db_log_msg (const char *file, int line, const char *fun, int level, size_t dbidx, const char *format,...) |
| Utility function to insert debug or error informational messages into the database. | |
| static void | refdm_db_pool_init (refdm_db_pool_t *conn, const refdm_db_t *parms) |
| Initialize struct state for a single connection in the pool. | |
| static uint32_t | refdm_db_pool_connect (refdm_db_pool_t *conn, size_t idx) |
| Actually attempt the connection from parameters. | |
| uint32_t | refdm_db_mgt_init (const refdm_db_t *parms, uint32_t clear, uint32_t log) |
| void | refdm_db_mgt_close (void) |
| void | refdm__db_mgt_close_conn (size_t idx) |
| int | refdm_db_mgt_connected (size_t idx) |
| int32_t | refdm_db_mgt_query_fetch (int db_idx, PGresult **res, char *format,...) |
| int32_t | refdm_db_mgt_query_insert (int db_idx, uint32_t *idx, char *format,...) |
| static int | transform_cbor_str_to_cace_data (cace_ari_t *ari_item, char *cbor_str, char **errm) |
| Takes a C string and returns the corresponding cace_ari_t. | |
| int | refdm_db_clear_rptset (int32_t agent_idx) |
| Runs a clear request on the rptset database table. | |
| int | refdm_db_fetch_rptset_count (int32_t agent_idx, size_t *count) |
| Runs a query on the database and retrieves the number of RPTSETs. | |
| int | refdm_db_fetch_rptset_list (int32_t agent_idx, cace_ari_list_t *rptsets) |
| Runs a query on the database and retrieves the list of RPTSETs. | |
| refdm_agent_t * | refdm_db_fetch_agent (int32_t id) |
| int32_t | refdm_db_fetch_agent_idx (const char *eid) |
| Get the index of an Agent from its endpoint name. | |
| uint32_t | refdm_db_insert_rptset (const cace_ari_t *val, const refdm_agent_t *agent) |
| uint32_t | refdm_db_insert_agent (const m_string_t eid) |
| uint32_t | refdm_db_insert_execset (const cace_ari_t *val, const refdm_agent_t *agent) |
Variables | |
| const char * | TBL_NAME_RPTSET = "ari_rptset" |
| const char * | COL_NAME_REFERENCE_TIME = "reference_time" |
| const char * | COL_NAME_AGENT_ID = "agent_id" |
| const char * | COL_NAME_ARI_RPTSET_ID = "ari_rptset_id" |
| const char * | COL_NAME_REPORT_LIST = "report_list" |
| const char * | COL_NAME_REPORT_LIST_CBOR = "report_list_cbor" |
| const char * | COL_NAME_NONCE_INT = "nonce_int" |
| const char * | COL_NAME_NONCE_BYTES = "nonce_bytes" |
| refdm_db_pool_t | dbpool [MGR_NUM_SQL_CONNECTIONS] |
| static char * | queries [MGR_NUM_SQL_CONNECTIONS][MGR_NUM_QUERIES] |
This file is only included in the build when ::HAVE_POSTGRESQL is defined.
| #define checkConn | ( | idx | ) | (idx < MGR_NUM_SQL_CONNECTIONS && dbpool[idx].conn != NULL) |
| #define DB_CHKINT | ( | status | ) |
| #define DB_CHKNULL | ( | status | ) |
| #define DB_CHKUSR | ( | status, | |
| usr | |||
| ) |
| #define DB_CHKVOID | ( | status | ) |
| #define dbexec_prepared |
| #define dbprep_bind_param_bigint | ( | idx, | |
| var | |||
| ) |
| #define dbprep_bind_param_bool | ( | idx, | |
| var | |||
| ) |
| #define dbprep_bind_param_byte | ( | idx, | |
| var, | |||
| length | |||
| ) |
| #define dbprep_bind_param_double | ( | idx, | |
| var | |||
| ) |
| #define dbprep_bind_param_float | ( | idx, | |
| var | |||
| ) |
| #define dbprep_bind_param_int | ( | idx, | |
| var | |||
| ) |
| #define dbprep_bind_param_null | ( | idx | ) |
| #define dbprep_bind_param_short | ( | idx, | |
| var | |||
| ) |
| #define dbprep_bind_param_str | ( | idx, | |
| var | |||
| ) |
| #define dbprep_declare | ( | dbidx, | |
| idx, | |||
| params, | |||
| cols | |||
| ) |
| #define dbtest_result | ( | expected | ) | ((PQresultStatus(res) == expected) ? 0 : 1) |
| #define getConn | ( | idx | ) | pthread_mutex_lock(&dbpool[idx].lock) |
| #define giveConn | ( | idx | ) | pthread_mutex_unlock(&dbpool[idx].lock) |
| #define query_log_err | ( | status | ) | CACE_LOG_ERR("ERROR at %s %i: %s (errno: %d)", __FILE__, __LINE__, PQresultErrorMessage(res), status); |
| enum db_con_t |
| enum queries |
|
static |
References CACE_LOG_ERR, and dbpool.
Referenced by refdm_db_pool_connect().
| cace_ari_ac_t * db_query_ac | ( | size_t | dbidx, |
| int | ac_id | ||
| ) |
| void refdm__db_mgt_close_conn | ( | size_t | idx | ) |
References CACE_LOG_WARNING, refdm_db_pool_t::conn, dbpool, and refdm_db_pool_t::lock.
Referenced by refdm_db_mgt_close().
| int refdm_db_clear_rptset | ( | int32_t | agent_idx | ) |
Runs a clear request on the rptset database table.
All rows will be removed.
RET_FAIL_* on failure. References CACE_LOG_ERR, DB_REST_CON, refdm_db_mgt_query_fetch(), RET_FAIL_DATABASE, RET_PASS, and TBL_NAME_RPTSET.
Referenced by refdm_mgr_clear_reports().
| refdm_agent_t * refdm_db_fetch_agent | ( | int32_t | id | ) |
References CACE_LOG_ERR, CACE_LOG_INFO, DB_REST_CON, refdm_agent_t::eid, refdm_agent_init(), refdm_db_mgt_query_fetch(), and RET_PASS.
| int32_t refdm_db_fetch_agent_idx | ( | const char * | eid | ) |
Get the index of an Agent from its endpoint name.
| [in] | sender | The agent EID. |
References CACE_FREE, CACE_LOG_ERR, CACE_LOG_INFO, CACE_MALLOC, DB_REST_CON, DB_RPT_CON, dbpool, refdm_db_mgt_query_fetch(), and RET_PASS.
Referenced by agentsGetHandler(), agentShowHexReports(), agentShowTextReports(), and refdm_mgr_clear_reports().
| int refdm_db_fetch_rptset_count | ( | int32_t | agent_idx, |
| size_t * | count | ||
| ) |
Runs a query on the database and retrieves the number of RPTSETs.
| agent_idx | The row index of the source Agent. | |
| [out] | count | Argument used to return the number of RPTSETs. |
RET_FAIL_* on failure. References CACE_LOG_ERR, DB_REST_CON, refdm_db_mgt_query_fetch(), RET_FAIL_DATABASE, RET_PASS, and TBL_NAME_RPTSET.
Referenced by agentsGetHandler().
| int refdm_db_fetch_rptset_list | ( | int32_t | agent_idx, |
| cace_ari_list_t * | rptsets | ||
| ) |
Runs a query on the database and retrieves the list of RPTSETs.
| agent_idx | The row index of the source Agent. | |
| [out] | rptsets | The list used to hold the retrieved RPTSETs. |
RET_FAIL_* on failure. References cace_ari_init(), CACE_FREE, CACE_LOG_ERR, CACE_LOG_INFO, COL_NAME_REPORT_LIST_CBOR, DB_REST_CON, errm, refdm_db_mgt_query_fetch(), RET_FAIL_DATABASE, RET_PASS, TBL_NAME_RPTSET, and transform_cbor_str_to_cace_data().
Referenced by agentShowHexReports(), and agentShowTextReports().
| uint32_t refdm_db_insert_agent | ( | const m_string_t | eid | ) |
| eid | agent eid being added |
| status | Set to 0 if parsing fails, but not modified on success |
References ARI_AGENT_INSERT, CACE_LOG_INFO, DB_RPT_CON, dbexec_prepared, dbprep_bind_param_str, dbprep_declare, getConn, and giveConn.
Referenced by refdm_mgr_agent_add().
| uint32_t refdm_db_insert_execset | ( | const cace_ari_t * | val, |
| const refdm_agent_t * | agent | ||
| ) |
References agent, ARI_EXECSET_INSERT, cace_ari_cbor_encode(), cace_ari_cget_execset(), CACE_ARI_TEXT_ENC_OPTS_DEFAULT, cace_ari_text_encode(), cace_data_deinit(), cace_data_init(), CACE_DATA_INIT_NULL, CACE_LOG_DEBUG, cace_log_is_enabled_for(), DB_RPT_CON, dbexec_prepared, dbprep_bind_param_byte, dbprep_bind_param_int, dbprep_bind_param_str, dbprep_declare, getConn, giveConn, cace_data_t::len, cace_ari_execset_t::nonce, cace_data_t::ptr, and cace_ari_execset_t::targets.
Referenced by agentSendItems().
| uint32_t refdm_db_insert_rptset | ( | const cace_ari_t * | val, |
| const refdm_agent_t * | agent | ||
| ) |
| val | Report |
| agent | agent table set being inserted in |
| status | Set to 0 if parsing fails, but not modified on success |
References agent, ARI_RPTSET_INSERT, cace_ari_cbor_encode(), cace_ari_cget_rptset(), cace_ari_get_tp(), CACE_ARI_TEXT_ENC_OPTS_DEFAULT, cace_ari_text_encode(), cace_data_deinit(), cace_data_init(), CACE_DATA_INIT_NULL, CACE_LOG_DEBUG, CACE_LOG_ERR, CACE_LOG_INFO, cace_log_is_enabled_for(), cace_utctime_encode(), DB_RPT_CON, dbexec_prepared, dbprep_bind_param_byte, dbprep_bind_param_str, dbprep_declare, getConn, giveConn, cace_data_t::len, cace_ari_rptset_t::nonce, cace_data_t::ptr, and cace_ari_rptset_t::reftime.
Referenced by handle_recv().
| void refdm_db_log_msg | ( | const char * | filename, |
| int | lineno, | ||
| const char * | funcname, | ||
| int | level, | ||
| size_t | dbidx, | ||
| const char * | format, | ||
| ... | |||
| ) |
Utility function to insert debug or error informational messages into the database.
NOTE: If operating within a transaction, caller is responsible for committing transaction.
References checkConn, DB_CHKVOID, dbexec_prepared, dbprep_bind_param_int, dbprep_bind_param_str, dbprep_declare, dbtest_result, getConn, giveConn, and REFDM_DB_LOG_MSG.
| void refdm_db_mgt_close | ( | void | ) |
References CACE_LOG_INFO, MGR_NUM_SQL_CONNECTIONS, and refdm__db_mgt_close_conn().
Referenced by refdm_mgr_deinit().
| int refdm_db_mgt_connected | ( | size_t | idx | ) |
References checkConn, refdm_db_pool_t::conn, DB_LOG_INFO, dbpool, refdm_db_pool_connect(), and SQL_CONN_TRIES.
Referenced by refdm_db_mgt_query_fetch(), and refdm_db_mgt_query_insert().
| uint32_t refdm_db_mgt_init | ( | const refdm_db_t * | parms, |
| uint32_t | clear, | ||
| uint32_t | log | ||
| ) |
References CACE_LOG_INFO, dbpool, MGR_NUM_SQL_CONNECTIONS, refdm_db_pool_connect(), and refdm_db_pool_init().
Referenced by refdm_mgr_init().
| int32_t refdm_db_mgt_query_fetch | ( | int | db_idx, |
| PGresult ** | res, | ||
| char * | format, | ||
| ... | |||
| ) |
References CACE_LOG_ERR, CACE_LOG_INFO, dbpool, errm, getConn, giveConn, refdm_db_mgt_connected(), RET_FAIL_BAD_ARGS, RET_FAIL_DATABASE, RET_FAIL_DATABASE_CONNECTION, and RET_PASS.
Referenced by refdm_db_clear_rptset(), refdm_db_fetch_agent(), refdm_db_fetch_agent_idx(), refdm_db_fetch_rptset_count(), and refdm_db_fetch_rptset_list().
| int32_t refdm_db_mgt_query_insert | ( | int | db_idx, |
| uint32_t * | idx, | ||
| char * | format, | ||
| ... | |||
| ) |
References CACE_LOG_ERR, CACE_LOG_INFO, dbpool, dbtest_result, errm, getConn, giveConn, and refdm_db_mgt_connected().
|
static |
Actually attempt the connection from parameters.
This can occur multiple times for the same connection to re-connect. Prepared queries are connection specific. While we may not use all prepared statements for all connections, initializing the same sets everywhere simplifies management.
References ARI_AGENT_INSERT, ARI_EXECSET_INSERT, ARI_RPTSET_INSERT, CACE_LOG_INFO, CACE_LOG_WARNING, refdm_db_pool_t::conn, db_mgr_sql_prepare(), refdm_db_pool_t::parms, and REFDM_DB_LOG_MSG.
Referenced by refdm_db_mgt_connected(), and refdm_db_mgt_init().
|
static |
Initialize struct state for a single connection in the pool.
References refdm_db_pool_t::conn, refdm_db_pool_t::lock, and refdm_db_pool_t::parms.
Referenced by refdm_db_mgt_init().
|
static |
Takes a C string and returns the corresponding cace_ari_t.
RET_PASS on success otherwise RET_FAIL_* on failure.| [out] | ari_item | The ARI to decode into. |
| [in] | cbor_str | The string to decode from. |
| [out] | errm | If non-null, this will be set to a specific error message associated with any failure. When the return code is non-zero, if the pointed-to pointer is non-null it must be freed using CACE_FREE(). |
References cace_ari_cbor_decode(), cace_data_deinit(), cace_data_init_view(), errm, RET_FAIL_UNEXPECTED, and RET_PASS.
Referenced by refdm_db_fetch_rptset_list().
| const char* COL_NAME_AGENT_ID = "agent_id" |
| const char* COL_NAME_ARI_RPTSET_ID = "ari_rptset_id" |
| const char* COL_NAME_NONCE_BYTES = "nonce_bytes" |
| const char* COL_NAME_NONCE_INT = "nonce_int" |
| const char* COL_NAME_REFERENCE_TIME = "reference_time" |
| const char* COL_NAME_REPORT_LIST = "report_list" |
| const char* COL_NAME_REPORT_LIST_CBOR = "report_list_cbor" |
Referenced by refdm_db_fetch_rptset_list().
|
static |
| const char* TBL_NAME_RPTSET = "ari_rptset" |
Referenced by refdm_db_clear_rptset(), refdm_db_fetch_rptset_count(), and refdm_db_fetch_rptset_list().