Ejemplo n.º 1
0
static long dlr_redis_messages(void)
{
    List *result, *row;
    DBPoolConn *conn;
    long msgs = -1;

    conn = dbpool_conn_consume(pool);
    if (conn == NULL)
        return -1;

    if (dbpool_conn_select(conn, octstr_imm("DBSIZE"), NULL, &result) != 0) {
        dbpool_conn_produce(conn);
        return 0;
    }

    dbpool_conn_produce(conn);

    if (gwlist_len(result) > 0) {
        row = gwlist_extract_first(result);
        msgs = atol(octstr_get_cstr(gwlist_get(row, 0)));
        gwlist_destroy(row, octstr_destroy_item);

        while ((row = gwlist_extract_first(result)) != NULL)
            gwlist_destroy(row, octstr_destroy_item);
    }
    gwlist_destroy(result, NULL);

    return msgs;
}
Ejemplo n.º 2
0
static long dlr_messages_oracle()
{
    List *result, *row;
    Octstr *sql;
    DBPoolConn *conn;
    long msgs = -1;

    conn = dbpool_conn_consume(pool);
    if (conn == NULL)
        return -1;

    sql = octstr_format("SELECT count(*) FROM %S", fields->table);
#if defined(DLR_TRACE)
    debug("dlr.oracle", 0, "sql: %s", octstr_get_cstr(sql));
#endif

    if (dbpool_conn_select(conn, sql, NULL, &result) != 0) {
        octstr_destroy(sql);
        dbpool_conn_produce(conn);
        return -1;
    }
    dbpool_conn_produce(conn);
    octstr_destroy(sql);

    if (gwlist_len(result) > 0) {
        row = gwlist_extract_first(result);
        msgs = strtol(octstr_get_cstr(gwlist_get(row,0)), NULL, 10);
        gwlist_destroy(row, octstr_destroy_item);
    }
    gwlist_destroy(result, NULL);

    return msgs;
}
Ejemplo n.º 3
0
static void oracle_client_thread(void *arg)
{
    DBPool *pool = arg;
    DBPoolConn *pconn = NULL;
    int i;
    List *result;

    for (i = 1; i <= queries; i++) {
        pconn = dbpool_conn_consume(pool);

        if (pconn == NULL)
            continue;
#if 1 /* selects */
        if (dbpool_conn_select(pconn, sql, NULL, &result) == 0) {
            long i,j;
            for (i=0; i < gwlist_len(result); i++) {
                List *row = gwlist_get(result, i);
                for (j=0; j < gwlist_len(row); j++)
                    debug("", 0, "col = %ld   value = '%s'", j, octstr_get_cstr(gwlist_get(row,j)));
                gwlist_destroy(row, octstr_destroy_item);
            }
        }
        gwlist_destroy(result, NULL);
        dbpool_conn_produce(pconn);
#else /* only updates */
        debug("", 0, "rows processed = %d ", dbpool_conn_update(pconn, sql, NULL));
        dbpool_conn_produce(pconn);
#endif
    }
}
Ejemplo n.º 4
0
static struct dlr_entry* dlr_get_oracle(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
{
    Octstr *sql;
    DBPoolConn *pconn;
    List *result = NULL, *row;
    struct dlr_entry *res = NULL;
    List *binds = gwlist_create();

    pconn = dbpool_conn_consume(pool);
    if (pconn == NULL) /* should not happens, but sure is sure */
        return NULL;

    sql = octstr_format("SELECT %S, %S, %S, %S, %S, %S FROM %S WHERE %S=:1 AND %S=:2 AND %S=:3 AND ROWNUM < 2",
                        fields->field_mask, fields->field_serv,
                        fields->field_url, fields->field_src,
                        fields->field_dst, fields->field_boxc,
                        fields->table, fields->field_smsc,
                        fields->field_ts, fields->field_dst);

    gwlist_append(binds, (Octstr *)smsc);      /* :1 */
    gwlist_append(binds, (Octstr *)ts);        /* :2 */
    gwlist_append(binds, (Octstr *)dst);       /* :3 */

#if defined(DLR_TRACE)
    debug("dlr.oracle", 0, "sql: %s", octstr_get_cstr(sql));
#endif
    if (dbpool_conn_select(pconn, sql, binds, &result) != 0) {
        octstr_destroy(sql);
        dbpool_conn_produce(pconn);
        return NULL;
    }
    octstr_destroy(sql);
    gwlist_destroy(binds, NULL);
    dbpool_conn_produce(pconn);

#define LO2CSTR(r, i) octstr_get_cstr(gwlist_get(r, i))

    if (gwlist_len(result) > 0) {
        row = gwlist_extract_first(result);
        res = dlr_entry_create();
        gw_assert(res != NULL);
        res->mask = atoi(LO2CSTR(row,0));
        res->service = octstr_create(LO2CSTR(row, 1));
        res->url = octstr_create(LO2CSTR(row,2));
        res->source = octstr_create(LO2CSTR(row, 3));
        res->destination = octstr_create(LO2CSTR(row, 4));
        res->boxc_id = octstr_create(LO2CSTR(row, 5));
        gwlist_destroy(row, octstr_destroy_item);
        res->smsc = octstr_duplicate(smsc);
    }
    gwlist_destroy(result, NULL);

#undef LO2CSTR

    return res;
}
Ejemplo n.º 5
0
static struct dlr_entry* dlr_get_mssql(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
{
    Octstr *sql, *like;
    DBPoolConn *pconn;
    List *result = NULL, *row;
    struct dlr_entry *res = NULL;

    pconn = dbpool_conn_consume(pool);
    if (pconn == NULL) /* should not happens, but sure is sure */
        return NULL;

    if (dst)
        like = octstr_format("AND %S LIKE '%%%S'", fields->field_dst, dst);
    else
        like = octstr_imm("");

    sql = octstr_format("SELECT %S, %S, %S, %S, %S, %S FROM %S WHERE %S='%S'"
          " AND %S='%S' %S", fields->field_mask, fields->field_serv,
          fields->field_url, fields->field_src, fields->field_dst,
          fields->field_boxc, fields->table, fields->field_smsc, smsc,
          fields->field_ts, ts, like);

#if defined(DLR_TRACE)
    debug("dlr.mssql", 0, "sql: %s", octstr_get_cstr(sql));
#endif
    if (dbpool_conn_select(pconn, sql, NULL, &result) != 0) {
        octstr_destroy(sql);
        dbpool_conn_produce(pconn);
        return NULL;
    }
    octstr_destroy(sql);
    octstr_destroy(like);
    dbpool_conn_produce(pconn);

#define LO2CSTR(r, i) octstr_get_cstr(gwlist_get(r, i))

    if (gwlist_len(result) > 0) {
        row = gwlist_extract_first(result);
        res = dlr_entry_create();
        gw_assert(res != NULL);
        res->mask = atoi(LO2CSTR(row,0));
        res->service = octstr_create(LO2CSTR(row, 1));
        res->url = octstr_create(LO2CSTR(row,2));
        res->source = octstr_create(LO2CSTR(row, 3));
        res->destination = octstr_create(LO2CSTR(row, 4));
        res->boxc_id = octstr_create(LO2CSTR(row, 5));
        gwlist_destroy(row, octstr_destroy_item);
        res->smsc = octstr_duplicate(smsc);
    }
    gwlist_destroy(result, NULL);

#undef LO2CSTR

    return res;
}
Ejemplo n.º 6
0
static MYSQL_RES* mysql_select(const Octstr *sql)
{
    int state;
    MYSQL_RES *result = NULL;
    DBPoolConn *pc;

#if defined(SQLBOX_TRACE)
    debug("SQLBOX", 0, "sql: %s", octstr_get_cstr(sql));
#endif

    pc = dbpool_conn_consume(pool);
    if (pc == NULL) {
        error(0, "MYSQL: Database pool got no connection! DB update failed!");
        return NULL;
    }

    state = mysql_query(pc->conn, octstr_get_cstr(sql));
    if (state != 0) {
        error(0, "MYSQL: %s", mysql_error(pc->conn));
    } else {
        result = mysql_store_result(pc->conn);
    }

    dbpool_conn_produce(pc);

    return result;
}
Ejemplo n.º 7
0
/* Remove DLR */
static void dlr_mongodb_remove(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
{
    DBPoolConn *pconn;
    bson cond;
    bson_buffer cond_buf;
    mongo_connection *conn = NULL;

    pconn = dbpool_conn_consume(pool);
    if (pconn == NULL) {
        return;
    }
    conn = (mongo_connection*)pconn->conn;

    bson_buffer_init(&cond_buf);
    bson_append_string(&cond_buf, octstr_get_cstr(fields->field_smsc), octstr_get_cstr(smsc));
    bson_append_string(&cond_buf, octstr_get_cstr(fields->field_ts), octstr_get_cstr(ts));

    if (dst) {
        bson_append_string(&cond_buf, octstr_get_cstr(fields->field_dst), octstr_get_cstr(dst));
    }

    bson_from_buffer(&cond, &cond_buf);

    MONGO_TRY {
        mongo_remove(conn, mongodb_namespace, &cond);
    } MONGO_CATCH {
        mongodb_error("dlr_mongodb_remove", conn->exception.type);
    }

    dbpool_conn_produce(pconn);
    bson_destroy(&cond);
}
Ejemplo n.º 8
0
static void sqlite3_client_thread(void *arg)
{
    unsigned long i, succeeded, failed;
    DBPool *pool = arg;
    char *errmsg = 0;

    succeeded = failed = 0;

    info(0,"Client thread started with %ld queries to perform on pool", queries);

    /* perform random queries on the pool */
    for (i = 1; i <= queries; i++) {
        DBPoolConn *pconn;
        int state;

        /* provide us with a connection from the pool */
        pconn = dbpool_conn_consume(pool);
        debug("",0,"Query %ld/%ld: sqlite conn obj at %p",
              i, queries, (void*) pconn->conn);

        state = sqlite3_exec(pconn->conn, octstr_get_cstr(sql), callback3, 0, &errmsg);
        if (state != SQLITE_OK) {
            error(0, "SQLite3: %s", errmsg);
            failed++;
        } else {
            succeeded++;
        }

        /* return the connection to the pool */
        dbpool_conn_produce(pconn);
    }
    info(0, "This thread: %ld succeeded, %ld failed.", succeeded, failed);
}
Ejemplo n.º 9
0
static void mysql_client_thread(void *arg)
{
    unsigned long i, succeeded, failed;
    DBPool *pool = arg;

    succeeded = failed = 0;

    info(0,"Client thread started with %ld queries to perform on pool", queries);

    /* perform random queries on the pool */
    for (i = 1; i <= queries; i++) {
        DBPoolConn *pconn;
        int state;
        MYSQL_RES *result;

        /* provide us with a connection from the pool */
        pconn = dbpool_conn_consume(pool);
        debug("",0,"Query %ld/%ld: mysql thread id %ld obj at %p",
              i, queries, mysql_thread_id(pconn->conn), (void*) pconn->conn);

        state = mysql_query(pconn->conn, octstr_get_cstr(sql));
        if (state != 0) {
            error(0, "MYSQL: %s", mysql_error(pconn->conn));
            failed++;
        } else {
            succeeded++;
        }
        result = mysql_store_result(pconn->conn);
        mysql_free_result(result);

        /* return the connection to the pool */
        dbpool_conn_produce(pconn);
    }
    info(0, "This thread: %ld succeeded, %ld failed.", succeeded, failed);
}
Ejemplo n.º 10
0
void mssql_save_msg(Msg *msg, Octstr *momt /*, Octstr smsbox_id */)
{
    Octstr *sql;
    Octstr *stuffer[30];
    int stuffcount = 0;
    DBPoolConn *pc;
    pc = dbpool_conn_consume(pool);
    if (pc == NULL) {
        error(0, "MSSql: DBPool Error!");
        return;
    }

    sql = octstr_format(SQLBOX_MSSQL_INSERT_QUERY, sqlbox_logtable, st_str(momt), st_str(msg->sms.sender),
        st_str(msg->sms.receiver), st_str(msg->sms.udhdata), st_str(msg->sms.msgdata), st_num(msg->sms.time),
        st_str(msg->sms.smsc_id), st_str(msg->sms.service), st_str(msg->sms.account), st_num(msg->sms.sms_type),
        st_num(msg->sms.mclass), st_num(msg->sms.mwi), st_num(msg->sms.coding), st_num(msg->sms.compress),
        st_num(msg->sms.validity), st_num(msg->sms.deferred), st_num(msg->sms.dlr_mask), st_str(msg->sms.dlr_url),
        st_num(msg->sms.pid), st_num(msg->sms.alt_dcs), st_num(msg->sms.rpi), st_str(msg->sms.charset),
        st_str(msg->sms.boxc_id), st_str(msg->sms.binfo), st_str(msg->sms.meta_data));
#if defined(SQLBOX_TRACE)
     debug("SQLBOX", 0, "sql: %s", octstr_get_cstr(sql));
#endif
    sql_update(pc, sql, NULL);
    while (stuffcount > 0) {
        octstr_destroy(stuffer[--stuffcount]);
    }
    dbpool_conn_produce(pc);
    octstr_destroy(sql);
}
Ejemplo n.º 11
0
static void redis_update(const Octstr *sql, const Octstr *data)
{
    redisReply *reply;
    DBPoolConn *pc;

#if defined(SQLBOX_TRACE)
     debug("SQLBOX", 0, "sql: %s", octstr_get_cstr(sql));
     debug("SQLBOX", 0, "data: %s", octstr_get_cstr(data));
#endif

    pc = dbpool_conn_consume(pool);
    if (pc == NULL) {
        error(0, "REDIS: Database pool got no connection! DB update failed!");
        return;
    }

    if(data == NULL)
        reply = redisCommand(pc->conn, octstr_get_cstr(sql));
    else
        reply = redisCommand(pc->conn, octstr_get_cstr(sql), octstr_get_cstr(data));
    if (reply->type == REDIS_REPLY_ERROR)
        error(0, "REDIS: redisCommand() failed: %s", reply->str);

    freeReplyObject(reply);
    dbpool_conn_produce(pc);
}
Ejemplo n.º 12
0
static void dlr_remove_mssql(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
{
    Octstr *sql, *like;
    DBPoolConn *pconn;
    int res;

    debug("dlr.mssql", 0, "removing DLR from database");

    pconn = dbpool_conn_consume(pool);
    /* just for sure */
    if (pconn == NULL)
        return;

    if (dst)
        like = octstr_format("AND %S LIKE '%%%S'", fields->field_dst, dst);
    else
        like = octstr_imm("");

    sql = octstr_format("SET ROWCOUNT 1\nDELETE FROM %S WHERE %S='%S' AND "
         "%S='%S' %S \nSET ROWCOUNT 0", fields->table, fields->field_smsc,
         smsc, fields->field_ts, ts, like);

#if defined(DLR_TRACE)
    debug("dlr.mssql", 0, "sql: %s", octstr_get_cstr(sql));
#endif

    if ((res = dbpool_conn_update(pconn, sql, NULL)) == -1)
        error(0, "DLR: MSSQL: Error while removing dlr entry for DST<%s>", octstr_get_cstr(dst));
    else if (!res)
        warning(0, "DLR: MSSQL: No dlr deleted for DST<%s>", octstr_get_cstr(dst));

    dbpool_conn_produce(pconn);
    octstr_destroy(sql);
    octstr_destroy(like);
}
Ejemplo n.º 13
0
/* Create index on smsc and ts fields, as these are used for retrieving the DLR */
static void dlr_mongodb_ensure_index(void)
{
    DBPoolConn *pconn;
    mongo_connection *conn = NULL;
    bson_buffer bb;
    bson key;

    pconn = dbpool_conn_consume(pool);
    if (pconn == NULL) {
        return;
    }
    conn = (mongo_connection*)pconn->conn;

    bson_buffer_init(&bb);
    bson_append_int(&bb, octstr_get_cstr(fields->field_smsc), 1);
    bson_append_int(&bb, octstr_get_cstr(fields->field_ts), 1);
    bson_from_buffer(&key, &bb);

    MONGO_TRY {
        mongo_create_index(conn, mongodb_namespace, &key, 0, NULL);
    } MONGO_CATCH {
        mongodb_error("dlr_mongodb_ensure_index", conn->exception.type);
    }

    dbpool_conn_produce(pconn);
    bson_destroy(&key);
}
Ejemplo n.º 14
0
static void dlr_update_mssql(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int status)
{
    Octstr *sql, *like;
    DBPoolConn *pconn;
    int res;

    debug("dlr.mssql", 0, "updating DLR status in database");

    pconn = dbpool_conn_consume(pool);
    /* just for sure */
    if (pconn == NULL)
        return;

    if (dst)
        like = octstr_format("AND %S LIKE '%%%S'", fields->field_dst, dst);
    else
        like = octstr_imm("");

    sql = octstr_format("SET ROWCOUNT 1\nUPDATE %S SET %S=%d WHERE %S='%S' "
        "AND %S='%S' %S\nSET ROWCOUNT 0",
        fields->table, fields->field_status, status, fields->field_smsc, smsc,
        fields->field_ts, ts, like);

#if defined(DLR_TRACE)
    debug("dlr.mssql", 0, "sql: %s", octstr_get_cstr(sql));
#endif
    if ((res = dbpool_conn_update(pconn, sql, NULL)) == -1)
        error(0, "DLR: MSSQL: Error while updating dlr entry for DST<%s>", octstr_get_cstr(dst));
    else if (!res)
        warning(0, "DLR: MSSQL: No dlr found to update for DST<%s> (status: %d)", octstr_get_cstr(dst), status);

    dbpool_conn_produce(pconn);
    octstr_destroy(sql);
    octstr_destroy(like);
}
Ejemplo n.º 15
0
static void dlr_update_oracle(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int status)
{
    Octstr *sql, *os_status;
    DBPoolConn *pconn;
    List *binds = gwlist_create();

    debug("dlr.oracle", 0, "updating DLR status in database");

    pconn = dbpool_conn_consume(pool);
    /* just for sure */
    if (pconn == NULL)
        return;

    sql = octstr_format("UPDATE %S SET %S=:1 WHERE %S=:2 AND %S=:3 AND %S=:4 AND ROWNUM < 2",
                        fields->table, fields->field_status,
                        fields->field_smsc, fields->field_ts, fields->field_dst);

    os_status = octstr_format("%d", status);
    gwlist_append(binds, (Octstr *)os_status); /* :1 */
    gwlist_append(binds, (Octstr *)smsc);      /* :2 */
    gwlist_append(binds, (Octstr *)ts);        /* :3 */
    gwlist_append(binds, (Octstr *)dst);       /* :4 */
#if defined(DLR_TRACE)
    debug("dlr.oracle", 0, "sql: %s", octstr_get_cstr(sql));
#endif
    if (dbpool_conn_update(pconn, sql, binds) == -1)
        error(0, "DLR: ORACLE: Error while updating dlr entry for DST<%s>", octstr_get_cstr(dst));

    dbpool_conn_produce(pconn);
    gwlist_destroy(binds, NULL);
    octstr_destroy(os_status);
    octstr_destroy(sql);
}
Ejemplo n.º 16
0
static void dlr_add_mssql(struct dlr_entry *entry)
{
    Octstr *sql;
    DBPoolConn *pconn;
    debug("dlr.mssql", 0, "adding DLR entry into database");
    int res;

    pconn = dbpool_conn_consume(pool);
    /* just for sure */
    if (pconn == NULL) {
        dlr_entry_destroy(entry);
        return;
    }
    
    sql = octstr_format("INSERT INTO %S (%S, %S, %S, %S, %S, %S, %S, %S, %S) VALUES "
                "('%S', '%S', '%S', '%S', '%S', '%S', '%d', '%S', '%d')",
                fields->table, fields->field_smsc, fields->field_ts, fields->field_src,
                fields->field_dst, fields->field_serv, fields->field_url,
                fields->field_mask, fields->field_boxc, fields->field_status,
                entry->smsc, entry->timestamp, entry->source, entry->destination,
                entry->service, entry->url, entry->mask, entry->boxc_id, 0);

#if defined(DLR_TRACE)
    debug("dlr.mssql", 0, "sql: %s", octstr_get_cstr(sql));
#endif
    if ((res = dbpool_conn_update(pconn, sql, NULL)) == -1)
        error(0, "DLR: MSSQL: Error while adding dlr entry for DST<%s>", octstr_get_cstr(entry->destination));
    else if (!res)
        warning(0, "DLR: MSSQL: No dlr inserted for DST<%s>", octstr_get_cstr(entry->destination));

    dbpool_conn_produce(pconn);
    octstr_destroy(sql);
    dlr_entry_destroy(entry);
}
Ejemplo n.º 17
0
static void dlr_redis_remove(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
{
    Octstr *key, *sql;
    DBPoolConn *pconn;
    List *binds = gwlist_create();
    int res;

    debug("dlr.redis", 0, "Removing DLR from keystore");

    pconn = dbpool_conn_consume(pool);
    /* just for sure */
    if (pconn == NULL) {
        error(0, "DLR: REDIS: No connection available");
        gwlist_destroy(binds, NULL);
        return;
    }

    /*
    octstr_replace(dst, octstr_imm(" "), octstr_imm(""));
    octstr_replace(ts, octstr_imm(" "), octstr_imm(""));
    */

    key = octstr_format((dst ? "%S:?:?:?" : "%S:?:?"), fields->table);

    sql = octstr_format("DEL %S", key, fields->table);
    gwlist_append(binds, (Octstr *)smsc); /* key */
    gwlist_append(binds, (Octstr *)ts); /* key */
    if (dst)
        gwlist_append(binds, (Octstr *)dst); /* key */

    res = dbpool_conn_update(pconn, sql, binds);
 
    /*
     * Redis DEL returns the number of keys deleted
     */ 
    if (res != 1) {
        /* 
         * We may fail to delete a DLR that was successfully retrieved
         * just above due to race conditions when duplicate message IDs
         * are received. This happens frequently when testing via the
         * Logica SMPP emulator due to its duplicate message ID bugs.
         */
        error(0, "DLR: REDIS: Error while removing dlr entry for %s",
              octstr_get_cstr(key));
    }
    /* We don't perform 'DECR <table>:Count', since we have TTL'ed
     * expirations, which can't be handled with manual counters. */

    dbpool_conn_produce(pconn);
    octstr_destroy(sql);
    octstr_destroy(key);
    gwlist_destroy(binds, NULL);
}
Ejemplo n.º 18
0
static void dlr_mysql_add(struct dlr_entry *entry)
{
    Octstr *sql, *os_mask;
    DBPoolConn *pconn;
    List *binds = gwlist_create();
    int res;

    debug("dlr.mysql", 0, "adding DLR entry into database");

    pconn = dbpool_conn_consume(pool);
    /* just for sure */
    if (pconn == NULL) {
        dlr_entry_destroy(entry);
        return;
    }

    sql = octstr_format("INSERT INTO `%S` (`%S`, `%S`, `%S`, `%S`, `%S`, `%S`, `%S`, `%S`, `%S`) VALUES "
                        "(?, ?, ?, ?, ?, ?, ?, ?, 0)",
                        fields->table, fields->field_smsc, fields->field_ts,
                        fields->field_src, fields->field_dst, fields->field_serv,
                        fields->field_url, fields->field_mask, fields->field_boxc,
                        fields->field_status);
    os_mask = octstr_format("%d", entry->mask);
    gwlist_append(binds, entry->smsc);
    gwlist_append(binds, entry->timestamp);
    gwlist_append(binds, entry->source);
    gwlist_append(binds, entry->destination);
    gwlist_append(binds, entry->service);
    gwlist_append(binds, entry->url);
    gwlist_append(binds, os_mask);
    gwlist_append(binds, entry->boxc_id);

#if defined(DLR_TRACE)
    debug("dlr.mysql", 0, "sql: %s", octstr_get_cstr(sql));
#endif
    if ((res = dbpool_conn_update(pconn, sql, binds)) == -1)
        error(0, "DLR: MYSQL: Error while adding dlr entry for DST<%s>", octstr_get_cstr(entry->destination));
    else if (!res)
        warning(0, "DLR: MYSQL: No dlr inserted for DST<%s>", octstr_get_cstr(entry->destination));

    dbpool_conn_produce(pconn);
    octstr_destroy(sql);
    gwlist_destroy(binds, NULL);
    octstr_destroy(os_mask);
    dlr_entry_destroy(entry);
}
Ejemplo n.º 19
0
static void dlr_mysql_update(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int status)
{
    Octstr *sql, *os_status, *like;
    DBPoolConn *pconn;
    List *binds = gwlist_create();
    int res;

    debug("dlr.mysql", 0, "updating DLR status in database");

    pconn = dbpool_conn_consume(pool);
    /* just for sure */
    if (pconn == NULL)
        return;

    if (dst)
        like = octstr_format("AND `%S` LIKE CONCAT('%%', ?)", fields->field_dst);
    else
        like = octstr_imm("");

    sql = octstr_format("UPDATE `%S` SET `%S`=? WHERE `%S`=? AND `%S`=? %S LIMIT 1",
                        fields->table, fields->field_status,
                        fields->field_smsc, fields->field_ts,
                        like);

    os_status = octstr_format("%d", status);
    gwlist_append(binds, (Octstr *)os_status);
    gwlist_append(binds, (Octstr *)smsc);
    gwlist_append(binds, (Octstr *)ts);
    if (dst)
        gwlist_append(binds, (Octstr *)dst);

#if defined(DLR_TRACE)
    debug("dlr.mysql", 0, "sql: %s", octstr_get_cstr(sql));
#endif
    if ((res = dbpool_conn_update(pconn, sql, binds)) == -1)
        error(0, "DLR: MYSQL: Error while updating dlr entry for DST<%s>", octstr_get_cstr(dst));
    else if (!res)
       warning(0, "DLR: MYSQL: No dlr found to update for DST<%s>, (status %d)", octstr_get_cstr(dst), status);

    dbpool_conn_produce(pconn);
    gwlist_destroy(binds, NULL);
    octstr_destroy(os_status);
    octstr_destroy(sql);
    octstr_destroy(like);
}
Ejemplo n.º 20
0
static int gw_sdb_query(char *query,
                        int (*callback)(int, char **, void *), void *closure)
{
    DBPoolConn *pc;
    int rows;

    pc = dbpool_conn_consume(pool);
    if (pc == NULL) {
        error(0, "SDB: Database pool got no connection!");
        return -1;
    }

    rows = sdb_query(pc->conn, query, callback, closure);

    dbpool_conn_produce(pc);

    return rows;
}
Ejemplo n.º 21
0
static void dlr_update_sqlite3(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int status)
{
    Octstr *sql, *os_status, *like;
    DBPoolConn *pconn;
    List *binds = gwlist_create();
    int res;

    debug("dlr.sqlite3", 0, "updating DLR status in database");

    pconn = dbpool_conn_consume(pool);
    /* just for sure */
    if (pconn == NULL)
        return;

    if (dst)
        like = octstr_format("AND %S LIKE '%?4'", fields->field_dst);
    else
        like = octstr_imm("");

    sql = octstr_format("UPDATE %S SET %S=?1 WHERE ROWID IN (SELECT ROWID FROM %S WHERE %S=?2 AND %S=?3 %S LIMIT 1)",
                        fields->table, fields->field_status, fields->table,
                        fields->field_smsc, fields->field_ts, fields->field_dst);

    os_status = octstr_format("%d", status);
    gwlist_append(binds, (Octstr *)os_status); /* ?1 */
    gwlist_append(binds, (Octstr *)smsc);      /* ?2 */
    gwlist_append(binds, (Octstr *)ts);        /* ?3 */
    if (dst)
        gwlist_append(binds, (Octstr *)dst);   /* ?4 */
    
#if defined(DLR_TRACE)
    debug("dlr.sqlite3", 0, "sql: %s", octstr_get_cstr(sql));
#endif
    if ((res = dbpool_conn_update(pconn, sql, binds)) == -1)
        error(0, "DLR: SQLite3: Error while updating dlr entry for DST<%s>", octstr_get_cstr(dst));
    else if (!res)
        warning(0, "DLR: SQLite3: No dlr found to update for DST<%s> (status: %d)", octstr_get_cstr(dst), status);

    dbpool_conn_produce(pconn);
    gwlist_destroy(binds, NULL);
    octstr_destroy(os_status);
    octstr_destroy(sql);
    octstr_destroy(like);
}
Ejemplo n.º 22
0
static void dlr_redis_update(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int status)
{
    Octstr *key, *sql, *os_status;
    DBPoolConn *pconn;
    List *binds = gwlist_create();
    int res;

    debug("dlr.redis", 0, "Updating DLR status in keystore");

    pconn = dbpool_conn_consume(pool);
    /* just for sure */
    if (pconn == NULL) {
        error(0, "DLR: REDIS: No connection available");
        gwlist_destroy(binds, NULL);
        return;
    }

    os_status = octstr_format("%d", status);

    key = octstr_format((dst ? "%S:?:?:?" : "%S:?:?"), fields->table);

    sql = octstr_format("HSET %S %S ?", key, fields->field_status);
    gwlist_append(binds, (Octstr*)smsc);
    gwlist_append(binds, (Octstr*)ts);
    if (dst != NULL)
        gwlist_append(binds, (Octstr*)dst);
    gwlist_append(binds, os_status);

    if ((res = dbpool_conn_update(pconn, sql, binds)) == -1) {
        error(0, "DLR: REDIS: Error while updating dlr entry for %s",
              octstr_get_cstr(key));
    }
    else if (!res) {
        warning(0, "DLR: REDIS: No dlr found to update for %s",
                octstr_get_cstr(key));
    }

    dbpool_conn_produce(pconn);
    octstr_destroy(os_status);
    octstr_destroy(key);
    octstr_destroy(sql);
    gwlist_destroy(binds, NULL);
}
Ejemplo n.º 23
0
/* Remove all DLRs from our namespace */
static void dlr_mongodb_flush(void)
{
    DBPoolConn *pconn;
    bson b;
    mongo_connection *conn = NULL;

    pconn = dbpool_conn_consume(pool);
    if (pconn == NULL) {
        return;
    }
    conn = (mongo_connection*)pconn->conn;

    MONGO_TRY {
        mongo_remove(conn, mongodb_namespace, bson_empty(&b));
    } MONGO_CATCH {
        mongodb_error("dlr_mongodb_flush", conn->exception.type);
    }
    dbpool_conn_produce(pconn);
}
Ejemplo n.º 24
0
/* Add a new DLR entry to MongoDB */
static void dlr_mongodb_add(struct dlr_entry *entry)
{
    DBPoolConn *pconn;
    bson b;
    bson_buffer buf;
    mongo_connection *conn = NULL;

    pconn = dbpool_conn_consume(pool);
    if (pconn == NULL) {
        dlr_entry_destroy(entry);
        return;
    }
    conn = (mongo_connection*)pconn->conn;

    bson_buffer_init(&buf);
    bson_append_new_oid(&buf, "_id");

    bson_append_string(&buf, octstr_get_cstr(fields->field_smsc), octstr_get_cstr(entry->smsc));
    bson_append_string(&buf, octstr_get_cstr(fields->field_ts), octstr_get_cstr(entry->timestamp));
    bson_append_string(&buf, octstr_get_cstr(fields->field_src), octstr_get_cstr(entry->source));
    bson_append_string(&buf, octstr_get_cstr(fields->field_dst), octstr_get_cstr(entry->destination));
    bson_append_string(&buf, octstr_get_cstr(fields->field_serv), octstr_get_cstr(entry->service));
    bson_append_string(&buf, octstr_get_cstr(fields->field_url), octstr_get_cstr(entry->url));
    bson_append_string(&buf, octstr_get_cstr(fields->field_account), octstr_get_cstr(entry->account));
    bson_append_string(&buf, octstr_get_cstr(fields->field_binfo), octstr_get_cstr(entry->binfo));
    bson_append_int(&buf, octstr_get_cstr(fields->field_mask), entry->mask);
    bson_append_string(&buf, octstr_get_cstr(fields->field_boxc), octstr_get_cstr(entry->boxc_id));
    bson_append_int(&buf, octstr_get_cstr(fields->field_status), 0);

    bson_from_buffer(&b, &buf);

    /* TODO: namespace support */
    MONGO_TRY {
        mongo_insert(conn, mongodb_namespace, &b);
    } MONGO_CATCH {
        mongodb_error("dlr_mongodb_insert", conn->exception.type);
    }

    dbpool_conn_produce(pconn);

    bson_destroy(&b);
    dlr_entry_destroy(entry);
}
Ejemplo n.º 25
0
void sqlbox_configure_mssql(Cfg* cfg)
{
    DBPoolConn *pc;
    CfgGroup *grp;
    Octstr *sql;

    if (!(grp = cfg_get_single_group(cfg, octstr_imm("sqlbox"))))
        panic(0, "SQLBOX: MSSql: group 'sqlbox' is not specified!");

    sqlbox_logtable = cfg_get(grp, octstr_imm("sql-log-table"));
    if (sqlbox_logtable == NULL) {
        panic(0, "Parameter 'sql-log-table' not configured.");
    }
    sqlbox_insert_table = cfg_get(grp, octstr_imm("sql-insert-table"));
    if (sqlbox_insert_table == NULL) {
        panic(0, "Parameter 'sql-insert-table' not configured.");
    }

    pc = dbpool_conn_consume(pool);
    if (pc == NULL) {
        error(0, "MSSql: DBPool Error!");
        return;
    }

    /* create send_sms && sent_sms tables if they do not exist */
    sql = octstr_format(SQLBOX_MSSQL_CREATE_LOG_TABLE, sqlbox_logtable, sqlbox_logtable);
#if defined(SQLBOX_TRACE)
     debug("SQLBOX", 0, "sql: %s", octstr_get_cstr(sql));
#endif
    sql_update(pc, sql, NULL);
    octstr_destroy(sql);

    sql = octstr_format(SQLBOX_MSSQL_CREATE_INSERT_TABLE, sqlbox_insert_table, sqlbox_insert_table);
#if defined(SQLBOX_TRACE)
     debug("SQLBOX", 0, "sql: %s", octstr_get_cstr(sql));
#endif
    sql_update(pc, sql, NULL);
    octstr_destroy(sql);

    dbpool_conn_produce(pc);

}
Ejemplo n.º 26
0
static void dlr_add_oracle(struct dlr_entry *entry)
{
    Octstr *sql, *os_mask;
    DBPoolConn *pconn;
    List *binds = gwlist_create();
    debug("dlr.oracle", 0, "adding DLR entry into database");

    pconn = dbpool_conn_consume(pool);
    /* just for sure */
    if (pconn == NULL) {
        dlr_entry_destroy(entry);
        return;
    }

    sql = octstr_format("INSERT INTO %S (%S, %S, %S, %S, %S, %S, %S, %S, %S) VALUES "
                        "(:1, :2, :3, :4, :5, :6, :7, :8, 0)",
                        fields->table, fields->field_smsc, fields->field_ts,
                        fields->field_src, fields->field_dst, fields->field_serv, 
                        fields->field_url, fields->field_mask, fields->field_boxc,
                        fields->field_status);
    os_mask = octstr_format("%d", entry->mask);
    
    gwlist_append(binds, entry->smsc);         /* :1 */
    gwlist_append(binds, entry->timestamp);    /* :2 */
    gwlist_append(binds, entry->source);       /* :3 */
    gwlist_append(binds, entry->destination);  /* :4 */
    gwlist_append(binds, entry->service);      /* :5 */
    gwlist_append(binds, entry->url);          /* :6 */
    gwlist_append(binds, os_mask);             /* :7 */
    gwlist_append(binds, entry->boxc_id);      /* :8 */
#if defined(DLR_TRACE)
    debug("dlr.oracle", 0, "sql: %s", octstr_get_cstr(sql));
#endif
    if (dbpool_conn_update(pconn, sql, binds) == -1)
        error(0, "DLR: ORACLE: Error while adding dlr entry for DST<%s>", octstr_get_cstr(entry->destination));

    dbpool_conn_produce(pconn);
    octstr_destroy(sql);
    gwlist_destroy(binds, NULL);
    octstr_destroy(os_mask);
    dlr_entry_destroy(entry);
}
Ejemplo n.º 27
0
/* Update DLR */
static void dlr_mongodb_update(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int status)
{
    DBPoolConn *pconn;
    bson cond, op;
    bson_buffer cond_buf, op_buf;
    mongo_connection *conn = NULL;

    pconn = dbpool_conn_consume(pool);
    if (pconn == NULL) {
        return;
    }
    conn = (mongo_connection*)pconn->conn;

    bson_buffer_init(&cond_buf);
    bson_append_string(&cond_buf, octstr_get_cstr(fields->field_smsc), octstr_get_cstr(smsc));
    bson_append_string(&cond_buf, octstr_get_cstr(fields->field_ts), octstr_get_cstr(ts));

    if (dst) {
        bson_append_string(&cond_buf, octstr_get_cstr(fields->field_dst), octstr_get_cstr(dst));
    }

    bson_from_buffer(&cond, &cond_buf);

    bson_buffer_init(&op_buf);
    {
        bson_buffer *sub = bson_append_start_object(&op_buf, "$set");
        bson_append_int(sub, octstr_get_cstr(fields->field_status), status);
        bson_append_finish_object(sub);
    }
    bson_from_buffer(&op, &op_buf);

    MONGO_TRY {
        mongo_update(conn, mongodb_namespace, &cond, &op, 0);
    } MONGO_CATCH {
        mongodb_error("dlr_mongodb_update", conn->exception.type);
    }

    dbpool_conn_produce(pconn);
    bson_destroy(&cond);
    bson_destroy(&op);
}
Ejemplo n.º 28
0
static void dlr_remove_sqlite3(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
{
    Octstr *sql, *like;
    DBPoolConn *pconn;
    List *binds = gwlist_create();
    int res;
    debug("dlr.sqlite3", 0, "removing DLR from database");

    pconn = dbpool_conn_consume(pool);
    /* just for sure */
    if (pconn == NULL)
        return;
    
    if (dst)
        like = octstr_format("AND %S LIKE '%?3'", fields->field_dst);
    else
        like = octstr_imm("");

    sql = octstr_format("DELETE FROM %S WHERE ROWID IN (SELECT ROWID FROM %S WHERE %S=?1 AND %S=?2 %S LIMIT 1)",
                        fields->table, fields->table,
                        fields->field_smsc, fields->field_ts, like);

    gwlist_append(binds, (Octstr *)smsc);      /* ?1 */
    gwlist_append(binds, (Octstr *)ts);        /* ?2 */
    if (dst)
        gwlist_append(binds, (Octstr *)dst);   /* ?3 */

#if defined(DLR_TRACE)
    debug("dlr.sqlite3", 0, "sql: %s", octstr_get_cstr(sql));
#endif

    if ((res = dbpool_conn_update(pconn, sql, binds)) == -1)
        error(0, "DLR: SQLite3: Error while removing dlr entry for DST<%s>", octstr_get_cstr(dst));
    else if (!res)
        warning(0, "DLR: SQLite3: No dlr deleted for DST<%s>", octstr_get_cstr(dst));

    dbpool_conn_produce(pconn);
    gwlist_destroy(binds, NULL);
    octstr_destroy(sql);
    octstr_destroy(like);
}
Ejemplo n.º 29
0
static inline int pgsql_update(const Octstr *sql)
{
    DBPoolConn *pc;
    int ret = 0;

#if defined(DLR_TRACE)
    debug("dlr.pgsql", 0, "sql: %s", octstr_get_cstr(sql));
#endif

    pc = dbpool_conn_consume(pool);
    if (pc == NULL) {
        error(0, "PGSQL: Database pool got no connection! DB update failed!");
        return -1;
    }

    if ((ret = dbpool_conn_update(pc, sql, NULL)) == -1)
        error(0, "PGSQL: DB update failed!");
    
    dbpool_conn_produce(pc);
    return ret;
}
Ejemplo n.º 30
0
static inline List *pgsql_select(const Octstr *sql)
{
    DBPoolConn *pc;
    List *ret = NULL;

#if defined(DLR_TRACE)
    debug("dlr.pgsql", 0, "sql: %s", octstr_get_cstr(sql));
#endif

    pc = dbpool_conn_consume(pool);
    if (pc == NULL) {
        error(0, "PGSQL: Database pool got no connection! DB operation failed!");
        return NULL;
    }

    if (dbpool_conn_select(pc, sql, NULL, &ret) == -1)
        error(0, "PGSQL: Select failed!");
    
    dbpool_conn_produce(pc);
    return ret;
}