Patch: EMI UUCP DLR

"Nikos Balkanas" <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <0BC909C8C90449F4B822C6B1BACBA75B@drwho>
Hi,

Thanks all for your input. This patch improves on the previous one, by 
accounting for inexact destinations send by the SMSc and deploying LIKE %... 
constructs in the query for last 7 digits of destination. It also will query 
on destination only when asked to (configuration or otherwise). Since this 
is a lot of work, this time I have only changed dlr_mysql.c to see if we all 
agree. If we are in agreement, I will expand to rest of databases and rest 
of code.

Notes:

1) If no destination is specified in the DLR, it assumes that relevant 
argument is NULL. Verified from code in the case of EMI & SMPP.
2) Choise to implicate dst in query depends on variable use_dst. This could 
be configured/smsc, or to avoid unnecessary configurations, it could be set 
from the smsc driver. I.e. always use dst on queries for EMI smscs, never 
for others. Is this safe, or best to let user configure this?

BR,
Nikos
mysql.diff (application/octet-stream, 6.7 KB)
Index: gw/dlr_mysql.c
===================================================================
--- gw/dlr_mysql.c	(revision 4833)
+++ gw/dlr_mysql.c	(working copy)
@@ -136,27 +136,53 @@
     dlr_entry_destroy(entry);
 }
 
-static struct dlr_entry* dlr_mysql_get(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
+static struct dlr_entry* dlr_mysql_get(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int use_dst)
 {
-    Octstr *sql;
+    Octstr *sql, *like = NULL;
     DBPoolConn *pconn;
     List *result = NULL, *row;
     struct dlr_entry *res = NULL;
     List *binds = gwlist_create();
+    int len;
 
     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`=? AND `%S`=? LIMIT 1",
+    if (use_dst && dst)
+    {
+       len = octstr_len(dst);
+       if (len < 7) sql = octstr_format("SELECT `%S`, `%S`, `%S`, `%S`, `%S`, `%S` FROM `%S` WHERE `%S`=? AND `%S`=? AND `%S`=? LIMIT 1",
                         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);
+       else sql = octstr_format("SELECT `%S`, `%S`, `%S`, `%S`, `%S`, `%S` FROM `%S` WHERE `%S`=? AND `%S`=? AND `%S` LIKE ? LIMIT 1",
+                        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);
+    }
+    else sql = octstr_format("SELECT `%S`, `%S`, `%S`, `%S`, `%S`, `%S` FROM `%S` WHERE `%S`=? AND `%S`=? LIMIT 1",
+                        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);
 
     gwlist_append(binds, (Octstr *)smsc);
     gwlist_append(binds, (Octstr *)ts);
+    if (use_dst && dst)
+    {
+       if (len < 7) gwlist_append(binds, dst);
+       else
+       {
+          like = octstr_create(strcat("%", octstr_get_cstr(dst) + len - 7));
+          gwlist_append(binds, like);
+       }
+    }
 
 #if defined(DLR_TRACE)
     debug("dlr.mysql", 0, "sql: %s", octstr_get_cstr(sql));
@@ -164,10 +190,12 @@
 
     if (dbpool_conn_select(pconn, sql, binds, &result) != 0) {
         octstr_destroy(sql);
+        if (like) octstr_destroy(like);
         dbpool_conn_produce(pconn);
         return NULL;
     }
     octstr_destroy(sql);
+    if (like) octstr_destroy(like);
     gwlist_destroy(binds, NULL);
     dbpool_conn_produce(pconn);
 
@@ -193,12 +221,12 @@
     return res;
 }
 
-static void dlr_mysql_remove(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
+static void dlr_mysql_remove(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int use_dst)
 {
-    Octstr *sql;
+    Octstr *sql, *like = NULL;
     DBPoolConn *pconn;
     List *binds = gwlist_create();
-    int res;
+    int res, len;
 
     debug("dlr.mysql", 0, "removing DLR from database");
 
@@ -207,12 +235,31 @@
     if (pconn == NULL)
         return;
 
-    sql = octstr_format("DELETE FROM `%S` WHERE `%S`=? AND `%S`=? LIMIT 1",
+    if (use_dst && dst)
+    {
+       len = octstr_len(dst);
+       if (len < 7) sql = octstr_format("DELETE FROM `%S` WHERE `%S`=? AND `%S`=? AND `%S`=? LIMIT 1",
                         fields->table, fields->field_smsc,
+                        fields->field_ts, fields->field_dst);
+       else sql = octstr_format("DELETE FROM `%S` WHERE `%S`=? AND `%S`=? AND `%S` LIKE ? LIMIT 1",
+                        fields->table, fields->field_smsc,
+                        fields->field_ts, fields->field_dst);
+    }
+    else sql = octstr_format("DELETE FROM `%S` WHERE `%S`=? AND `%S`=? LIMIT 1",
+                        fields->table, fields->field_smsc,
                         fields->field_ts);
 
     gwlist_append(binds, (Octstr *)smsc);
     gwlist_append(binds, (Octstr *)ts);
+    if (use_dst && dst)
+    {
+       if (len < 7) gwlist_append(binds, dst);
+       else
+       {
+          like = octstr_create(strcat("%", octstr_get_cstr(dst) + len - 7));
+          gwlist_append(binds, like);
+       }
+    }
 
 #if defined(DLR_TRACE)
     debug("dlr.mysql", 0, "sql: %s", octstr_get_cstr(sql));
@@ -226,14 +273,15 @@
     dbpool_conn_produce(pconn);
     gwlist_destroy(binds, NULL);
     octstr_destroy(sql);
+    if (like) octstr_destroy(like);
 }
 
-static void dlr_mysql_update(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int status)
+static void dlr_mysql_update(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int status, int use_dst)
 {
-    Octstr *sql, *os_status;
+    Octstr *sql, *os_status, *like = NULL;
     DBPoolConn *pconn;
     List *binds = gwlist_create();
-    int res;
+    int res, len;
 
     debug("dlr.mysql", 0, "updating DLR status in database");
 
@@ -242,14 +290,35 @@
     if (pconn == NULL)
         return;
 
-    sql = octstr_format("UPDATE `%S` SET `%S`=? WHERE `%S`=? AND `%S`=? LIMIT 1",
+    if (use_dst && dst)
+    {
+       len = octstr_len(dst);
+       if (len < 7) sql = octstr_format("UPDATE `%S` SET `%S`=? WHERE `%S`=? AND `%S`=? AND `%S`=? LIMIT 1",
                         fields->table, fields->field_status,
+                        fields->field_smsc, fields->field_ts,
+                        fields->field_dst);
+       else sql = octstr_format("UPDATE `%S` SET `%S`=? WHERE `%S`=? AND `%S`=? AND `%S` LIKE ? LIMIT 1",
+                        fields->table, fields->field_status,
+                        fields->field_smsc, fields->field_ts,
+                        fields->field_dst);
+    }
+    else sql = octstr_format("UPDATE `%S` SET `%S`=? WHERE `%S`=? AND `%S`=? LIMIT 1",
+                        fields->table, fields->field_status,
                         fields->field_smsc, fields->field_ts);
 
     os_status = octstr_format("%d", status);
     gwlist_append(binds, (Octstr *)os_status);
     gwlist_append(binds, (Octstr *)smsc);
     gwlist_append(binds, (Octstr *)ts);
+    if (use_dst && dst)
+    {
+       if (len < 7) gwlist_append(binds, dst);
+       else
+       {
+          like = octstr_create(strcat("%", octstr_get_cstr(dst) + len - 7));
+          gwlist_append(binds, like);
+       }
+    }
 
 #if defined(DLR_TRACE)
     debug("dlr.mysql", 0, "sql: %s", octstr_get_cstr(sql));
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.