| version 1.2.2.11, 2012/06/26 08:05:58 | version 1.4.4.1, 2017/10/08 22:48:43 | 
| Line 1 | Line 1 | 
 |  | /************************************************************************* | 
 |  | * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com> | 
 |  | *  by Michael Pounov <misho@openbsd-bg.org> | 
 |  | * | 
 |  | * $Author$ | 
 |  | * $Id$ | 
 |  | * | 
 |  | ************************************************************************** | 
 |  | The ELWIX and AITNET software is distributed under the following | 
 |  | terms: | 
 |  |  | 
 |  | All of the documentation and software included in the ELWIX and AITNET | 
 |  | Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org> | 
 |  |  | 
 |  | Copyright 2004 - 2017 | 
 |  | by Michael Pounov <misho@elwix.org>.  All rights reserved. | 
 |  |  | 
 |  | Redistribution and use in source and binary forms, with or without | 
 |  | modification, are permitted provided that the following conditions | 
 |  | are met: | 
 |  | 1. Redistributions of source code must retain the above copyright | 
 |  | notice, this list of conditions and the following disclaimer. | 
 |  | 2. Redistributions in binary form must reproduce the above copyright | 
 |  | notice, this list of conditions and the following disclaimer in the | 
 |  | documentation and/or other materials provided with the distribution. | 
 |  | 3. All advertising materials mentioning features or use of this software | 
 |  | must display the following acknowledgement: | 
 |  | This product includes software developed by Michael Pounov <misho@elwix.org> | 
 |  | ELWIX - Embedded LightWeight unIX and its contributors. | 
 |  | 4. Neither the name of AITNET nor the names of its contributors | 
 |  | may be used to endorse or promote products derived from this software | 
 |  | without specific prior written permission. | 
 |  |  | 
 |  | THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND | 
 |  | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
 |  | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
 |  | ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 
 |  | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
 |  | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 
 |  | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 
 |  | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
 |  | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 
 |  | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
 |  | SUCH DAMAGE. | 
 |  | */ | 
 | #include "global.h" | #include "global.h" | 
 |  |  | 
 |  |  | 
| Line 99  mqtt_rtlm_close(sqlite3 *sql) | Line 144  mqtt_rtlm_close(sqlite3 *sql) | 
 | */ | */ | 
 | int | int | 
 | mqtt_rtlm_init_session(cfg_root_t *cfg, sqlite3 *sql, const char *connid, const char *user, | mqtt_rtlm_init_session(cfg_root_t *cfg, sqlite3 *sql, const char *connid, const char *user, | 
| const char *host, char will, ...) | const char *host, int will, ...) | 
 | { | { | 
 | va_list lst; | va_list lst; | 
 | int ret = 0; | int ret = 0; | 
| Line 247  mqtt_rtlm_chk_session(cfg_root_t *cfg, sqlite3 *sql, c | Line 292  mqtt_rtlm_chk_session(cfg_root_t *cfg, sqlite3 *sql, c | 
 | * @txtlen = text length | * @txtlen = text length | 
 | * @user = username | * @user = username | 
 | * @host = hostname | * @host = hostname | 
 |  | * @qos = QoS | 
 | * @retain = !=0 retain message to database | * @retain = !=0 retain message to database | 
 | * return: -1 error, 0 no publish or >0 published ok | * return: -1 error, 0 no publish or >0 published ok | 
 | */ | */ | 
 | int | int | 
 | mqtt_rtlm_write_topic(cfg_root_t *cfg, sqlite3 *sql, const char *connid, u_short msgid, | mqtt_rtlm_write_topic(cfg_root_t *cfg, sqlite3 *sql, const char *connid, u_short msgid, | 
| const char *topic, void *txt, int txtlen, const char *user, const char *host, char retain) | const char *topic, void *txt, int txtlen, const char *user, | 
|  | const char *host, char qos, char retain) | 
 | { | { | 
 | int ret = 0; | int ret = 0; | 
 | char *str, *psStmt; | char *str, *psStmt; | 
| Line 266  mqtt_rtlm_write_topic(cfg_root_t *cfg, sqlite3 *sql, c | Line 313  mqtt_rtlm_write_topic(cfg_root_t *cfg, sqlite3 *sql, c | 
 | mqtt_rtlm_log("Error:: not found topics table name"); | mqtt_rtlm_log("Error:: not found topics table name"); | 
 | return -1; | return -1; | 
 | } | } | 
| psStmt = sqlite3_mprintf("INSERT INTO %s (Retain, ConnID, MsgID, Topic, Value, PubUser, " | psStmt = sqlite3_mprintf("INSERT INTO %s (QoS, Retain, ConnID, MsgID, Topic, Value, PubUser, " | 
| "PubDate, PubHost) VALUES (%d, '%q', %u, '%q', ?1, '%q', " | "PubDate, PubHost) VALUES (%d, %d, '%q', %u, '%q', ?1, '%q', " | 
 | "datetime('now', 'localtime'), '%q');", | "datetime('now', 'localtime'), '%q');", | 
| str, retain, connid, msgid, topic, txt, user, host); | str, qos, retain, connid, msgid, topic, user, host); | 
 |  |  | 
| if (sqlite3_prepare_v2(sql, psStmt, strlen(psStmt), &stmt, NULL)) { | if (sqlite3_prepare_v2(sql, psStmt, strlen(psStmt), &stmt, NULL) || !stmt) { | 
 | MQTT_RTLM_LOG(sql); | MQTT_RTLM_LOG(sql); | 
 | sqlite3_free(psStmt); | sqlite3_free(psStmt); | 
 | return -1; | return -1; | 
| Line 419  mqtt_rtlm_delete_topic(cfg_root_t *cfg, sqlite3 *sql, | Line 466  mqtt_rtlm_delete_topic(cfg_root_t *cfg, sqlite3 *sql, | 
 | * @cfg = loaded config | * @cfg = loaded config | 
 | * @sql = SQL handle | * @sql = SQL handle | 
 | * @connid = connection id | * @connid = connection id | 
 | * @msgid = MessageID |  | 
 | * @topic = topic | * @topic = topic | 
 | * @retain = retain 0 get only dynamic, >0 get only retained and -1 no matter | * @retain = retain 0 get only dynamic, >0 get only retained and -1 no matter | 
 | * return: NULL error or not found and !=NULL allocated subscribe topics | * return: NULL error or not found and !=NULL allocated subscribe topics | 
 | */ | */ | 
 | mqtt_subscr_t * | mqtt_subscr_t * | 
| mqtt_rtlm_read_topic(cfg_root_t *cfg, sqlite3 *sql, const char *connid, u_short msgid, | mqtt_rtlm_read_topic(cfg_root_t *cfg, sqlite3 *sql, const char *connid, | 
 | const char *topic, char retain) | const char *topic, char retain) | 
 | { | { | 
 | int rowz = 0; | int rowz = 0; | 
| Line 455  mqtt_rtlm_read_topic(cfg_root_t *cfg, sqlite3 *sql, co | Line 501  mqtt_rtlm_read_topic(cfg_root_t *cfg, sqlite3 *sql, co | 
 | mqtt_rtlm_log("Error:: not found topics table name"); | mqtt_rtlm_log("Error:: not found topics table name"); | 
 | return NULL; | return NULL; | 
 | } | } | 
| psStmt = sqlite3_mprintf("SELECT Retain, Topic, Value FROM %s WHERE " | psStmt = sqlite3_mprintf("SELECT QoS, Topic, Value  FROM %s WHERE " | 
| "ConnID = '%q' AND MsgID = %d AND Topic LIKE '%q' %s;", | "ConnID LIKE '%q' AND Topic LIKE '%q' %s;", | 
| str, connid, msgid, topic, szStr); | str, connid, topic, szStr); | 
 |  |  | 
 | if (sqlite3_prepare_v2(sql, psStmt, strlen(psStmt), &stmt, NULL)) { | if (sqlite3_prepare_v2(sql, psStmt, strlen(psStmt), &stmt, NULL)) { | 
 | MQTT_RTLM_LOG(sql); | MQTT_RTLM_LOG(sql); | 
| Line 469  mqtt_rtlm_read_topic(cfg_root_t *cfg, sqlite3 *sql, co | Line 515  mqtt_rtlm_read_topic(cfg_root_t *cfg, sqlite3 *sql, co | 
 | /* calculate count of rows and allocate subscribe items */ | /* calculate count of rows and allocate subscribe items */ | 
 | while (sqlite3_step(stmt) == SQLITE_ROW) | while (sqlite3_step(stmt) == SQLITE_ROW) | 
 | rowz++; | rowz++; | 
| if (!(s = io_malloc((rowz + 1) * sizeof(mqtt_subscr_t)))) { | if (!(s = malloc((rowz + 1) * sizeof(mqtt_subscr_t)))) { | 
 | mqtt_rtlm_log("Error:: System #%d - %s", errno, strerror(errno)); | mqtt_rtlm_log("Error:: System #%d - %s", errno, strerror(errno)); | 
 | goto end; | goto end; | 
 | } else | } else | 
| Line 479  mqtt_rtlm_read_topic(cfg_root_t *cfg, sqlite3 *sql, co | Line 525  mqtt_rtlm_read_topic(cfg_root_t *cfg, sqlite3 *sql, co | 
 | /* fill with data */ | /* fill with data */ | 
 | for (j = 0; j < rowz && sqlite3_step(stmt) == SQLITE_ROW; j++) { | for (j = 0; j < rowz && sqlite3_step(stmt) == SQLITE_ROW; j++) { | 
 | s[j].sub_ret = (char) sqlite3_column_int(stmt, 0); | s[j].sub_ret = (char) sqlite3_column_int(stmt, 0); | 
| s[j].sub_topic.msg_base = (u_char*) io_strdup((char*) sqlite3_column_text(stmt, 1)); | s[j].sub_topic.msg_base = (u_char*) strdup((char*) sqlite3_column_text(stmt, 1)); | 
 | s[j].sub_topic.msg_len = strlen((char*) s[j].sub_topic.msg_base); | s[j].sub_topic.msg_len = strlen((char*) s[j].sub_topic.msg_base); | 
 | AIT_SET_PTR(&v, (void*) sqlite3_column_blob(stmt, 2), sqlite3_column_bytes(stmt, 2)); | AIT_SET_PTR(&v, (void*) sqlite3_column_blob(stmt, 2), sqlite3_column_bytes(stmt, 2)); | 
 | s[j].sub_value.msg_len = AIT_LEN(&v); | s[j].sub_value.msg_len = AIT_LEN(&v); | 
| s[j].sub_value.msg_base = (u_char*) io_malloc(s[j].sub_value.msg_len); | s[j].sub_value.msg_base = (u_char*) malloc(s[j].sub_value.msg_len); | 
 | if (s[j].sub_value.msg_base) | if (s[j].sub_value.msg_base) | 
 | memcpy(s[j].sub_value.msg_base, AIT_GET_PTR(&v), s[j].sub_value.msg_len); | memcpy(s[j].sub_value.msg_base, AIT_GET_PTR(&v), s[j].sub_value.msg_len); | 
 |  | AIT_FREE_VAL(&v); | 
 | } | } | 
 | end: | end: | 
 | sqlite3_finalize(stmt); | sqlite3_finalize(stmt); | 
| Line 633  mqtt_rtlm_read_subscribe(cfg_root_t *cfg, sqlite3 *sql | Line 680  mqtt_rtlm_read_subscribe(cfg_root_t *cfg, sqlite3 *sql | 
 | /* calculate count of rows and allocate subscribe items */ | /* calculate count of rows and allocate subscribe items */ | 
 | while (sqlite3_step(stmt) == SQLITE_ROW) | while (sqlite3_step(stmt) == SQLITE_ROW) | 
 | rowz++; | rowz++; | 
| if (!(s = io_malloc((rowz + 1) * sizeof(mqtt_subscr_t)))) { | if (!(s = malloc((rowz + 1) * sizeof(mqtt_subscr_t)))) { | 
 | mqtt_rtlm_log("Error:: System #%d - %s", errno, strerror(errno)); | mqtt_rtlm_log("Error:: System #%d - %s", errno, strerror(errno)); | 
 | goto end; | goto end; | 
 | } else | } else | 
| Line 643  mqtt_rtlm_read_subscribe(cfg_root_t *cfg, sqlite3 *sql | Line 690  mqtt_rtlm_read_subscribe(cfg_root_t *cfg, sqlite3 *sql | 
 | /* fill with data */ | /* fill with data */ | 
 | for (j = 0; j < rowz && sqlite3_step(stmt) == SQLITE_ROW; j++) { | for (j = 0; j < rowz && sqlite3_step(stmt) == SQLITE_ROW; j++) { | 
 | s[j].sub_ret = (char) sqlite3_column_int(stmt, 0); | s[j].sub_ret = (char) sqlite3_column_int(stmt, 0); | 
| s[j].sub_topic.msg_base = (u_char*) io_strdup((char*) sqlite3_column_text(stmt, 1)); | s[j].sub_topic.msg_base = (u_char*) strdup((char*) sqlite3_column_text(stmt, 1)); | 
 | s[j].sub_topic.msg_len = strlen((char*) s[j].sub_topic.msg_base); | s[j].sub_topic.msg_len = strlen((char*) s[j].sub_topic.msg_base); | 
 | s[j].sub_value.msg_base = NULL; | s[j].sub_value.msg_base = NULL; | 
 | s[j].sub_value.msg_len = 0; | s[j].sub_value.msg_len = 0; |