sdlookup.c

Go to the documentation of this file.
00001 /*
00002  * $Id: sdlookup.c 4585 2008-08-06 08:20:30Z klaus_darilion $
00003  *
00004  * Copyright (C) 2004-2006 Voice Sistem SRL
00005  *
00006  * This file is part of Kamailio.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021  *
00022  * History:
00023  * ---------
00024  * 
00025  */
00026 
00027 
00028 #include <string.h>
00029 
00030 #include "../../dprint.h"
00031 #include "../../action.h"
00032 #include "../../config.h"
00033 #include "../../ut.h"
00034 #include "../../mod_fix.h"
00035 #include "../../parser/parse_uri.h"
00036 #include "../../parser/parse_from.h"
00037 #include "../../db/db.h"
00038 
00039 #include "speeddial.h"
00040 #include "sdlookup.h"
00041 
00042 #define MAX_USERURI_SIZE   256
00043 static char useruri_buf[MAX_USERURI_SIZE];
00044 
00045 /**
00046  * Rewrite Request-URI
00047  */
00048 static inline int rewrite_ruri(struct sip_msg* _m, char* _s)
00049 {
00050    struct action act;
00051 
00052    act.type = SET_URI_T;
00053    act.elem[0].type = STRING_ST;
00054    act.elem[0].u.string = _s;
00055    act.next = 0;
00056    
00057    if (do_action(&act, _m) < 0)
00058    {
00059       LM_ERR("do_action failed\n");
00060       return -1;
00061    }
00062    return 0;
00063 }
00064 
00065 /**
00066  *
00067  */
00068 int sd_lookup(struct sip_msg* _msg, char* _table, char* _owner)
00069 {
00070    str user_s, table_s, uri_s;
00071    int nr_keys;
00072    struct sip_uri *puri;
00073    struct sip_uri turi;
00074    db_key_t db_keys[4];
00075    db_val_t db_vals[4];
00076    db_key_t db_cols[1];
00077    db_res_t* db_res = NULL;
00078 
00079    if(_table==NULL || fixup_get_svalue(_msg, (gparam_p)_table, &table_s)!=0)
00080    {
00081       LM_ERR("invalid table parameter");
00082       return -1;
00083    }
00084 
00085    /* init */
00086    nr_keys = 0;
00087    db_cols[0]=&new_uri_column;
00088    
00089    if(_owner)
00090    {
00091       memset(&turi, 0, sizeof(struct sip_uri));
00092       if(fixup_get_svalue(_msg, (gparam_p)_owner, &uri_s)!=0)
00093       {
00094          LM_ERR("invalid owner uri parameter");
00095          return -1;
00096       }
00097       if(parse_uri(uri_s.s, uri_s.len, &turi)!=0)
00098       {
00099          LM_ERR("bad owner SIP address!\n");
00100          goto err_server;
00101       }
00102       LM_DBG("using user id [%.*s]\n", uri_s.len, uri_s.s);
00103       puri = &turi;
00104    } else {
00105       /* take username@domain from From header */
00106       if ( (puri = parse_from_uri(_msg ))==NULL )
00107       {
00108          LM_ERR("failed to parse FROM header\n");
00109          goto err_server;
00110       }
00111    }
00112       
00113    db_keys[nr_keys]=&user_column;
00114    db_vals[nr_keys].type = DB_STR;
00115    db_vals[nr_keys].nul = 0;
00116    db_vals[nr_keys].val.str_val.s = puri->user.s;
00117    db_vals[nr_keys].val.str_val.len = puri->user.len;
00118    nr_keys++;
00119 
00120    if(use_domain>=1)
00121    {
00122       db_keys[nr_keys]=&domain_column;
00123       db_vals[nr_keys].type = DB_STR;
00124       db_vals[nr_keys].nul = 0;
00125       db_vals[nr_keys].val.str_val.s = puri->host.s;
00126       db_vals[nr_keys].val.str_val.len = puri->host.len;
00127       nr_keys++;
00128       
00129       if (dstrip_s.s!=NULL && dstrip_s.len>0
00130          && dstrip_s.len<puri->host.len
00131          && strncasecmp(puri->host.s,dstrip_s.s,dstrip_s.len)==0)
00132       {
00133          db_vals[nr_keys].val.str_val.s   += dstrip_s.len;
00134          db_vals[nr_keys].val.str_val.len -= dstrip_s.len;
00135       }
00136    }
00137    /* take sd from r-uri */
00138    if (parse_sip_msg_uri(_msg) < 0)
00139    {
00140       LM_ERR("failed to parsing Request-URI\n");
00141       goto err_server;
00142    }
00143    
00144    db_keys[nr_keys]=&sd_user_column;
00145    db_vals[nr_keys].type = DB_STR;
00146    db_vals[nr_keys].nul = 0;
00147    db_vals[nr_keys].val.str_val.s = _msg->parsed_uri.user.s;
00148    db_vals[nr_keys].val.str_val.len = _msg->parsed_uri.user.len;
00149    nr_keys++;
00150    
00151    if(use_domain>=2)
00152    {
00153       db_keys[nr_keys]=&sd_domain_column;
00154       db_vals[nr_keys].type = DB_STR;
00155       db_vals[nr_keys].nul = 0;
00156       db_vals[nr_keys].val.str_val.s = _msg->parsed_uri.host.s;
00157       db_vals[nr_keys].val.str_val.len = _msg->parsed_uri.host.len;
00158       nr_keys++;
00159       
00160       if (dstrip_s.s!=NULL && dstrip_s.len>0
00161          && dstrip_s.len<_msg->parsed_uri.host.len
00162          && strncasecmp(_msg->parsed_uri.host.s,dstrip_s.s,dstrip_s.len)==0)
00163       {
00164          db_vals[nr_keys].val.str_val.s   += dstrip_s.len;
00165          db_vals[nr_keys].val.str_val.len -= dstrip_s.len;
00166       }
00167    }
00168    
00169    db_funcs.use_table(db_handle, &table_s);
00170    if(db_funcs.query(db_handle, db_keys, NULL, db_vals, db_cols,
00171       nr_keys /*no keys*/, 1 /*no cols*/, NULL, &db_res)!=0)
00172    {
00173       LM_ERR("failed to query database\n");
00174       goto err_server;
00175    }
00176 
00177    if (RES_ROW_N(db_res)<=0 || RES_ROWS(db_res)[0].values[0].nul != 0)
00178    {
00179       LM_DBG("no sip addres found for R-URI\n");
00180       if (db_res!=NULL && db_funcs.free_result(db_handle, db_res) < 0)
00181          LM_DBG("failed to free result of query\n");
00182       return -1;
00183    }
00184 
00185    user_s.s = useruri_buf+4;
00186    switch(RES_ROWS(db_res)[0].values[0].type)
00187    { 
00188       case DB_STRING:
00189          strcpy(user_s.s, 
00190             (char*)RES_ROWS(db_res)[0].values[0].val.string_val);
00191          user_s.len = strlen(user_s.s);
00192       break;
00193       case DB_STR:
00194          strncpy(user_s.s, 
00195             (char*)RES_ROWS(db_res)[0].values[0].val.str_val.s,
00196             RES_ROWS(db_res)[0].values[0].val.str_val.len);
00197          user_s.len = RES_ROWS(db_res)[0].values[0].val.str_val.len;
00198          user_s.s[user_s.len] = '\0';
00199       break;
00200       case DB_BLOB:
00201          strncpy(user_s.s, 
00202             (char*)RES_ROWS(db_res)[0].values[0].val.blob_val.s,
00203             RES_ROWS(db_res)[0].values[0].val.blob_val.len);
00204          user_s.len = RES_ROWS(db_res)[0].values[0].val.blob_val.len;
00205          user_s.s[user_s.len] = '\0';
00206       default:
00207          LM_ERR("unknown type of DB new_uri column\n");
00208          if (db_res != NULL && db_funcs.free_result(db_handle, db_res) < 0)
00209          {
00210             LM_DBG("failed to free result of query\n");
00211          }
00212          goto err_server;
00213    }
00214    
00215    /* check 'sip:' */
00216    if(user_s.len<4 || strncmp(user_s.s, "sip:", 4))
00217    {
00218       memcpy(useruri_buf, "sip:", 4);
00219       user_s.s -= 4;
00220       user_s.len += 4;
00221    }
00222 
00223    /**
00224     * Free the result because we don't need it anymore
00225     */
00226    if (db_res!=NULL && db_funcs.free_result(db_handle, db_res) < 0)
00227       LM_DBG("failed to free result of query\n");
00228 
00229    /* set the URI */
00230    LM_DBG("URI of sd from R-URI [%s]\n", user_s.s);
00231    if(rewrite_ruri(_msg, user_s.s)<0)
00232    {
00233       LM_ERR("failed to replace the R-URI\n");
00234       goto err_server;
00235    }
00236 
00237    return 1;
00238 
00239 err_server:
00240    return -1;
00241 }
00242 

Generated on Thu May 24 12:00:30 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6