Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:21 2009

## Asterisk developer's documentation

# astdb.h File Reference

Persistant data storage (akin to *doze registry). More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Data Structures

struct | ast_db_entry

## Functions

int | ast_db_del (const char *family, const char *key)
int | ast_db_deltree (const char *family, const char *keytree)
void | ast_db_freetree (struct ast_db_entry *entry)
int | ast_db_get (const char *family, const char *key, char *out, int outlen)
struct ast_db_entry * | ast_db_gettree (const char *family, const char *keytree)
int | ast_db_put (const char *family, const char *key, char *value)

## Detailed Description

Persistant data storage (akin to *doze registry).

Definition in file astdb.h.

## Function Documentation

int ast_db_del | ( | const char * | family,
|  | const char * | key |
| ) |  |  |

Definition at line 217 of file db.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), dbinit(), key(), and LOG_DEBUG.

Referenced by ast_privacy_set(), auth_exec(), cache_lookup_internal(), database_del(), del_exec(), destroy_association(), dump_agents(), dump_queue_members(), expire_registry(), handle_dbdel(), reload_agents(), reload_queue_members(), and update_registry().

    00218 {
    00219    char fullkey[256];
    00220    DBT key;
    00221    int res, fullkeylen;
    00222
    00223    ast_mutex_lock(&dblock);
    00224    if (dbinit()) {
    00225       ast_mutex_unlock(&dblock);
    00226       return -1;
    00227    }
    00228
    00229    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
    00230    memset(&key, 0, sizeof(key));
    00231    key.data = fullkey;
    00232    key.size = fullkeylen + 1;
    00233
    00234    res = astdb->del(astdb, &key, 0);
    00235    astdb->sync(astdb, 0);
    00236
    00237    ast_mutex_unlock(&dblock);
    00238
    00239    if (res)
    00240       ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
    00241    return res;
    00242 }

int ast_db_deltree | ( | const char * | family,
|  | const char * | keytree |
| ) |  |  |

Definition at line 103 of file db.c.

References ast_mutex_lock(), ast_mutex_unlock(), dbinit(), key(), keymatch(), keys, and pass.

Referenced by ast_privacy_reset(), database_deltree(), deltree_exec(), dundi_flush(), handle_dbdeltree(), and iax_provision_reload().

    00104 {
    00105    char prefix[256];
    00106    DBT key, data;
    00107    char *keys;
    00108    int res;
    00109    int pass;
    00110
    00111    if (family) {
    00112       if (keytree) {
    00113          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
    00114       } else {
    00115          snprintf(prefix, sizeof(prefix), "/%s", family);
    00116       }
    00117    } else if (keytree) {
    00118       return -1;
    00119    } else {
    00120       prefix[0] = '\0';
    00121    }
    00122
    00123    ast_mutex_lock(&dblock);
    00124    if (dbinit()) {
    00125       ast_mutex_unlock(&dblock);
    00126       return -1;
    00127    }
    00128
    00129    memset(&key, 0, sizeof(key));
    00130    memset(&data, 0, sizeof(data));
    00131    pass = 0;
    00132    while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
    00133       if (key.size) {
    00134          keys = key.data;
    00135          keys[key.size - 1] = '\0';
    00136       } else {
    00137          keys = "<bad key>";
    00138       }
    00139       if (keymatch(keys, prefix)) {
    00140          astdb->del(astdb, &key, 0);
    00141       }
    00142    }
    00143    astdb->sync(astdb, 0);
    00144    ast_mutex_unlock(&dblock);
    00145    return 0;
    00146 }

void ast_db_freetree | ( | struct ast_db_entry * | entry | ) |

Definition at line 462 of file db.c.

References free, last, and ast_db_entry::next.

Referenced by reload_agents(), and reload_queue_members().

    00463 {
    00464    struct ast_db_entry *last;
    00465    while (dbe) {
    00466       last = dbe;
    00467       dbe = dbe->next;
    00468       free(last);
    00469    }
    00470 }

int ast_db_get | ( | const char * | family,
|  | const char * | key,
|  | char * | out,
|  | int | outlen |
| ) |  |  |

Definition at line 175 of file db.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), dbinit(), key(), LOG_DEBUG, and LOG_NOTICE.

Referenced by ast_privacy_check(), auth_exec(), cache_lookup_internal(), check_access(), create_addr(), database_get(), database_increment(), function_db_exists(), function_db_read(), get_exec(), handle_dbget(), iax_provision_version(), load_password(), lookupblacklist_exec(), lookupcidname_exec(), manager_dbget(), populate_addr(), reg_source_db(), reload_agents(), and reload_queue_members().

    00176 {
    00177    char fullkey[256] = "";
    00178    DBT key, data;
    00179    int res, fullkeylen;
    00180
    00181    ast_mutex_lock(&dblock);
    00182    if (dbinit()) {
    00183       ast_mutex_unlock(&dblock);
    00184       return -1;
    00185    }
    00186
    00187    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
    00188    memset(&key, 0, sizeof(key));
    00189    memset(&data, 0, sizeof(data));
    00190    memset(value, 0, valuelen);
    00191    key.data = fullkey;
    00192    key.size = fullkeylen + 1;
    00193
    00194    res = astdb->get(astdb, &key, &data, 0);
    00195
    00196    ast_mutex_unlock(&dblock);
    00197
    00198    /* Be sure to NULL terminate our data either way */
    00199    if (res) {
    00200       ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
    00201    } else {
    00202 #if 0
    00203       printf("Got value of size %d\n", data.size);
    00204 #endif
    00205       if (data.size) {
    00206          ((char *)data.data)[data.size - 1] = '\0';
    00207          /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
    00208          strncpy(value, data.data, (valuelen > data.size) ? data.size : valuelen);
    00209          value[valuelen - 1] = '\0';
    00210       } else {
    00211          ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
    00212       }
    00213    }
    00214    return res;
    00215 }

struct ast_db_entry* ast_db_gettree | ( | const char * | family,
|  | const char * | keytree |
| ) |  |  | `[read]`

Definition at line 399 of file db.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_strlen_zero(), ast_db_entry::data, dbinit(), ast_db_entry::key, key(), keymatch(), keys, last, LOG_WARNING, malloc, ast_db_entry::next, and pass.

Referenced by reload_agents(), and reload_queue_members().

    00400 {
    00401    char prefix[256];
    00402    DBT key, data;
    00403    char *keys, *values;
    00404    int res;
    00405    int pass;
    00406    struct ast_db_entry *last = NULL;
    00407    struct ast_db_entry *cur, *ret=NULL;
    00408
    00409    if (!ast_strlen_zero(family)) {
    00410       if (!ast_strlen_zero(keytree)) {
    00411          /* Family and key tree */
    00412          snprintf(prefix, sizeof(prefix), "/%s/%s", family, prefix);
    00413       } else {
    00414          /* Family only */
    00415          snprintf(prefix, sizeof(prefix), "/%s", family);
    00416       }
    00417    } else {
    00418       prefix[0] = '\0';
    00419    }
    00420    ast_mutex_lock(&dblock);
    00421    if (dbinit()) {
    00422       ast_mutex_unlock(&dblock);
    00423       ast_log(LOG_WARNING, "Database unavailable\n");
    00424       return NULL;
    00425    }
    00426    memset(&key, 0, sizeof(key));
    00427    memset(&data, 0, sizeof(data));
    00428    pass = 0;
    00429    while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
    00430       if (key.size) {
    00431          keys = key.data;
    00432          keys[key.size - 1] = '\0';
    00433       } else {
    00434          keys = "<bad key>";
    00435       }
    00436       if (data.size) {
    00437          values = data.data;
    00438          values[data.size - 1] = '\0';
    00439       } else {
    00440          values = "<bad value>";
    00441       }
    00442       if (keymatch(keys, prefix)) {
    00443          cur = malloc(sizeof(struct ast_db_entry) + strlen(keys) + strlen(values) + 2);
    00444          if (cur) {
    00445             cur->next = NULL;
    00446             cur->key = cur->data + strlen(values) + 1;
    00447             strcpy(cur->data, values);
    00448             strcpy(cur->key, keys);
    00449             if (last) {
    00450                last->next = cur;
    00451             } else {
    00452                ret = cur;
    00453             }
    00454             last = cur;
    00455          }
    00456       }
    00457    }
    00458    ast_mutex_unlock(&dblock);
    00459    return ret;
    00460 }

int ast_db_put | ( | const char * | family,
|  | const char * | key,
|  | char * | value |
| ) |  |  |

Definition at line 148 of file db.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), dbinit(), key(), and LOG_WARNING.

Referenced by ast_privacy_set(), cache_save(), cache_save_hint(), database_increment(), database_put(), dump_agents(), dump_queue_members(), function_db_write(), handle_command_response(), handle_dbput(), iax_provision_build(), manager_dbput(), mgcp_ss(), parse_register_contact(), put_exec(), save_secret(), skinny_ss(), ss_thread(), and update_registry().

    00149 {
    00150    char fullkey[256];
    00151    DBT key, data;
    00152    int res, fullkeylen;
    00153
    00154    ast_mutex_lock(&dblock);
    00155    if (dbinit()) {
    00156       ast_mutex_unlock(&dblock);
    00157       return -1;
    00158    }
    00159
    00160    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
    00161    memset(&key, 0, sizeof(key));
    00162    memset(&data, 0, sizeof(data));
    00163    key.data = fullkey;
    00164    key.size = fullkeylen + 1;
    00165    data.data = value;
    00166    data.size = strlen(value) + 1;
    00167    res = astdb->put(astdb, &key, &data, 0);
    00168    astdb->sync(astdb, 0);
    00169    ast_mutex_unlock(&dblock);
    00170    if (res)
    00171       ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
    00172    return res;
    00173 }