alookup.c

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

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