Browse code

- allow one alias to many destinations (first replaces the r-uri, next are added as branches) - new param: append_branches to control addition of branches (default is 0, don't add branches -- preserves old behavior)

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@4221 689a6050-402a-0410-94f2-e92a70836424

Elena-Ramona Modroiu authored on 19/05/2008 07:55:24
Showing 6 changed files
... ...
@@ -4,6 +4,10 @@ Daniel-Constantin Mierla
4 4
 
5 5
    <miconda@gmail.com>
6 6
 
7
+Elena-Ramona Modroiu
8
+
9
+   <ramona@asipto.com>
10
+
7 11
 Edited by
8 12
 
9 13
 Daniel-Constantin Mierla
... ...
@@ -11,9 +15,8 @@ Daniel-Constantin Mierla
11 15
    <miconda@gmail.com>
12 16
 
13 17
    Copyright � 2005 voice-system.ro
14
-   Revision History
15
-   Revision $Revision$ $Date: 2008-03-10 17:13:24 +0200
16
-                              (Mon, 10 Mar 2008) $
18
+
19
+   Copyright � 2008 asipto.com
17 20
      __________________________________________________________
18 21
 
19 22
    Table of Contents
... ...
@@ -35,6 +38,7 @@ Daniel-Constantin Mierla
35 38
               1.3.5. alias_domain_column (str)
36 39
               1.3.6. use_domain (int)
37 40
               1.3.7. domain_prefix (str)
41
+              1.3.8. append_branches (int)
38 42
 
39 43
         1.4. Exported Functions
40 44
 
... ...
@@ -49,7 +53,8 @@ Daniel-Constantin Mierla
49 53
    1.5. Set alias_domain_column parameter
50 54
    1.6. Set use_domain parameter
51 55
    1.7. Set domain_prefix parameter
52
-   1.8. alias_db_lookup() usage
56
+   1.8. Set append_branches parameter
57
+   1.9. alias_db_lookup() usage
53 58
 
54 59
 Chapter 1. Admin Guide
55 60
 
... ...
@@ -141,7 +146,7 @@ modparam("alias_db", "alias_domain_column", "adomain")
141 146
    searching for alias. If set to 0, the domain from R-URI is not
142 147
    used, if set to 1 the domain from R-URI is used.
143 148
 
144
-   Default value is "o".
149
+   Default value is "0".
145 150
 
146 151
    Example 1.6. Set use_domain parameter
147 152
 ...
... ...
@@ -160,6 +165,19 @@ modparam("alias_db", "use_domain", 1)
160 165
 modparam("alias_db", "domain_prefix", "sip.")
161 166
 ...
162 167
 
168
+1.3.8. append_branches (int)
169
+
170
+   If the alias resolves to many SIP IDs, the first is replacing
171
+   the R-URI, the rest are added as branches.
172
+
173
+   Default value is "0" (0 - don't add branches; 1 - add
174
+   branches).
175
+
176
+   Example 1.8. Set append_branches parameter
177
+...
178
+modparam("alias_db", "append_branches", 1)
179
+...
180
+
163 181
 1.4. Exported Functions
164 182
 
165 183
 1.4.1.  alias_db_lookup(table_name)
... ...
@@ -177,7 +195,7 @@ modparam("alias_db", "domain_prefix", "sip.")
177 195
 
178 196
    This function can be used from REQUEST_ROUTE, FAILURE_ROUTE.
179 197
 
180
-   Example 1.8. alias_db_lookup() usage
198
+   Example 1.9. alias_db_lookup() usage
181 199
 ...
182 200
 alias_db_lookup("dbaliases");
183 201
 alias_db_lookup("dba_$(rU{s.substr,0,1})");
... ...
@@ -62,6 +62,7 @@ str alias_user_column   = str_init("alias_username");
62 62
 str alias_domain_column = str_init("alias_domain");
63 63
 str domain_prefix       = {NULL, 0};
64 64
 int use_domain          = 0;
65
+int ald_append_branches = 0;
65 66
 
66 67
 db_con_t* db_handle;   /* Database connection handle */
67 68
 db_func_t adbf;  /* DB functions */
... ...
@@ -83,6 +84,7 @@ static param_export_t params[] = {
83 84
 	{"alias_domain_column", STR_PARAM, &alias_domain_column.s },
84 85
 	{"use_domain",          INT_PARAM, &use_domain      },
85 86
 	{"domain_prefix",       STR_PARAM, &domain_prefix.s },
87
+	{"append_branches",     INT_PARAM, &ald_append_branches   },
86 88
 	{0, 0, 0}
87 89
 };
88 90
 
... ...
@@ -42,6 +42,7 @@ extern str alias_user_column;     /* 'alias_username' column name */
42 42
 extern str alias_domain_column;   /* 'alias_domain' column name */
43 43
 extern str domain_prefix;
44 44
 extern int use_domain;      /* use or not the domain for alias lookup */
45
+extern int ald_append_branches;  /* append branches after an alias lookup */
45 46
 
46 47
 extern db_con_t* db_handle;   /* Database connection handle */
47 48
 
... ...
@@ -35,6 +35,7 @@
35 35
 #include "../../parser/parse_uri.h"
36 36
 #include "../../db/db.h"
37 37
 #include "../../mod_fix.h"
38
+#include "../../dset.h"
38 39
 
39 40
 #include "alias_db.h"
40 41
 #include "alookup.h"
... ...
@@ -75,6 +76,7 @@ int alias_db_lookup(struct sip_msg* _msg, char* _table, char* _str2)
75 76
 	db_val_t db_vals[2];
76 77
 	db_key_t db_cols[] = {&user_column, &domain_column};
77 78
 	db_res_t* db_res = NULL;
79
+	int i;
78 80
 	
79 81
 	if(_table==NULL || fixup_get_svalue(_msg, (gparam_p)_table, &table_s)!=0)
80 82
 	{
... ...
@@ -124,87 +126,104 @@ int alias_db_lookup(struct sip_msg* _msg, char* _table, char* _str2)
124 126
 	}
125 127
 
126 128
 	memcpy(useruri_buf, "sip:", 4);
127
-	user_s.len = 4;
128
-	user_s.s = useruri_buf+4;
129
-	switch(RES_ROWS(db_res)[0].values[0].type)
130
-	{ 
131
-		case DB_STRING:
132
-			strcpy(user_s.s, 
133
-				(char*)RES_ROWS(db_res)[0].values[0].val.string_val);
134
-			user_s.len += strlen(user_s.s);
135
-		break;
136
-		case DB_STR:
137
-			strncpy(user_s.s, 
138
-				(char*)RES_ROWS(db_res)[0].values[0].val.str_val.s,
139
-				RES_ROWS(db_res)[0].values[0].val.str_val.len);
140
-			user_s.len += RES_ROWS(db_res)[0].values[0].val.str_val.len;
141
-		break;
142
-		case DB_BLOB:
143
-			strncpy(user_s.s, 
144
-				(char*)RES_ROWS(db_res)[0].values[0].val.blob_val.s,
145
-				RES_ROWS(db_res)[0].values[0].val.blob_val.len);
146
-			user_s.len += RES_ROWS(db_res)[0].values[0].val.blob_val.len;
147
-		default:
148
-			LM_ERR("unknown type of DB user column\n");
149
-			if (db_res != NULL && adbf.free_result(db_handle, db_res) < 0)
150
-			{
151
-				LM_DBG("failed to freeing result of query\n");
152
-			}
153
-			goto err_server;
154
-	}
129
+	for(i=0; i<RES_ROW_N(db_res); i++)
130
+	{
131
+		user_s.len = 4;
132
+		user_s.s = useruri_buf+4;
133
+		switch(RES_ROWS(db_res)[i].values[0].type)
134
+		{ 
135
+			case DB_STRING:
136
+				strcpy(user_s.s, 
137
+					(char*)RES_ROWS(db_res)[i].values[0].val.string_val);
138
+				user_s.len += strlen(user_s.s);
139
+			break;
140
+			case DB_STR:
141
+				strncpy(user_s.s, 
142
+					(char*)RES_ROWS(db_res)[i].values[0].val.str_val.s,
143
+					RES_ROWS(db_res)[i].values[0].val.str_val.len);
144
+				user_s.len += RES_ROWS(db_res)[i].values[0].val.str_val.len;
145
+			break;
146
+			case DB_BLOB:
147
+				strncpy(user_s.s, 
148
+					(char*)RES_ROWS(db_res)[i].values[0].val.blob_val.s,
149
+					RES_ROWS(db_res)[i].values[0].val.blob_val.len);
150
+				user_s.len += RES_ROWS(db_res)[i].values[0].val.blob_val.len;
151
+			break;
152
+			default:
153
+				LM_ERR("unknown type of DB user column\n");
154
+				if (db_res != NULL && adbf.free_result(db_handle, db_res) < 0)
155
+				{
156
+					LM_DBG("failed to freeing result of query\n");
157
+				}
158
+				goto err_server;
159
+		}
155 160
 	
156
-	/* add the @*/
157
-	useruri_buf[user_s.len] = '@';
158
-	user_s.len++;
161
+		/* add the @*/
162
+		useruri_buf[user_s.len] = '@';
163
+		user_s.len++;
159 164
 	
160
-	/* add the domain */
161
-	user_s.s = useruri_buf+user_s.len;
162
-	switch(RES_ROWS(db_res)[0].values[1].type)
163
-	{ 
164
-		case DB_STRING:
165
-			strcpy(user_s.s, 
166
-				(char*)RES_ROWS(db_res)[0].values[1].val.string_val);
167
-			user_s.len += strlen(user_s.s);
168
-		break;
169
-		case DB_STR:
170
-			strncpy(user_s.s, 
171
-				(char*)RES_ROWS(db_res)[0].values[1].val.str_val.s,
172
-				RES_ROWS(db_res)[0].values[1].val.str_val.len);
173
-			user_s.len += RES_ROWS(db_res)[0].values[1].val.str_val.len;
174
-			useruri_buf[user_s.len] = '\0';
175
-		break;
176
-		case DB_BLOB:
177
-			strncpy(user_s.s, 
178
-				(char*)RES_ROWS(db_res)[0].values[1].val.blob_val.s,
179
-				RES_ROWS(db_res)[0].values[1].val.blob_val.len);
180
-			user_s.len += RES_ROWS(db_res)[0].values[1].val.blob_val.len;
181
-			useruri_buf[user_s.len] = '\0';
182
-		default:
183
-			LM_ERR("unknown type of DB user column\n");
184
-			if (db_res != NULL && adbf.free_result(db_handle, db_res) < 0)
165
+		/* add the domain */
166
+		user_s.s = useruri_buf+user_s.len;
167
+		switch(RES_ROWS(db_res)[i].values[1].type)
168
+		{ 
169
+			case DB_STRING:
170
+				strcpy(user_s.s, 
171
+					(char*)RES_ROWS(db_res)[i].values[1].val.string_val);
172
+				user_s.len += strlen(user_s.s);
173
+			break;
174
+			case DB_STR:
175
+				strncpy(user_s.s, 
176
+					(char*)RES_ROWS(db_res)[i].values[1].val.str_val.s,
177
+					RES_ROWS(db_res)[i].values[1].val.str_val.len);
178
+				user_s.len += RES_ROWS(db_res)[i].values[1].val.str_val.len;
179
+				useruri_buf[user_s.len] = '\0';
180
+			break;
181
+			case DB_BLOB:
182
+				strncpy(user_s.s, 
183
+					(char*)RES_ROWS(db_res)[i].values[1].val.blob_val.s,
184
+					RES_ROWS(db_res)[i].values[1].val.blob_val.len);
185
+				user_s.len += RES_ROWS(db_res)[i].values[1].val.blob_val.len;
186
+				useruri_buf[user_s.len] = '\0';
187
+			break;
188
+			default:
189
+				LM_ERR("unknown type of DB user column\n");
190
+				if (db_res != NULL && adbf.free_result(db_handle, db_res) < 0)
191
+				{
192
+					LM_DBG("failed to freeing result of query\n");
193
+				}
194
+				goto err_server;
195
+		}
196
+		/* set the URI */
197
+		LM_DBG("new URI [%d] is [%s]\n", i, useruri_buf);
198
+		if(i==0)
199
+		{
200
+			if(rewrite_ruri(_msg, useruri_buf)<0)
201
+			{
202
+				LM_ERR("cannot replace the R-URI\n");
203
+				goto err_server;
204
+			}
205
+			if(ald_append_branches==0)
206
+				break;
207
+		} else {
208
+			user_s.s = useruri_buf;
209
+			if (append_branch(_msg, &user_s, 0, 0, MIN_Q, 0, 0) == -1)
185 210
 			{
186
-				LM_DBG("failed to freeing result of query\n");
211
+				LM_ERR("error while appending branches\n");
212
+				goto err_server;
187 213
 			}
188
-			goto err_server;
214
+		}
189 215
 	}
190 216
 
191 217
 	/**
192
-	 * Free the result because we don't need it
193
-	 * anymore
218
+	 * Free the DB result
194 219
 	 */
195 220
 	if (db_res!=NULL && adbf.free_result(db_handle, db_res) < 0)
196 221
 		LM_DBG("failed to freeing result of query\n");
197 222
 
198
-	/* set the URI */
199
-	LM_DBG("the URI of alias from R-URI [%s]\n", useruri_buf);
200
-	if(rewrite_ruri(_msg, useruri_buf)<0)
201
-	{
202
-		LM_ERR("cannot replace the R-URI\n");
203
-		goto err_server;
204
-	}
205
-
206 223
 	return 1;
207 224
 
208 225
 err_server:
226
+	if (db_res!=NULL && adbf.free_result(db_handle, db_res) < 0)
227
+		LM_DBG("failed to freeing result of query\n");
209 228
 	return -1;
210 229
 }
... ...
@@ -23,6 +23,11 @@
23 23
 		<surname>Mierla</surname>
24 24
 		<email>miconda@gmail.com</email>
25 25
 	    </author>
26
+	    <author>
27
+		<firstname>Elena-Ramona</firstname>
28
+		<surname>Modroiu</surname>
29
+		<email>ramona@asipto.com</email>
30
+	    </author>
26 31
 	    <editor>
27 32
 		<firstname>Daniel-Constantin</firstname>
28 33
 		<surname>Mierla</surname>
... ...
@@ -33,12 +38,10 @@
33 38
 	    <year>2005</year>
34 39
 	    <holder>&voicesystem;</holder>
35 40
 	</copyright>
36
-	<revhistory>
37
-	    <revision>
38
-		<revnumber>$Revision$</revnumber>
39
-		<date>$Date$</date>
40
-	    </revision>
41
-	</revhistory>
41
+	<copyright>
42
+	    <year>2008</year>
43
+	    <holder>asipto.com</holder>
44
+	</copyright>
42 45
     </bookinfo>
43 46
     <toc></toc>
44 47
     
... ...
@@ -160,7 +160,7 @@ modparam("alias_db", "alias_domain_column", "adomain")
160 160
 		</para>
161 161
 		<para>
162 162
 		<emphasis>
163
-			Default value is <quote>o</quote>.
163
+			Default value is <quote>0</quote>.
164 164
 		</emphasis>
165 165
 		</para>
166 166
 		<example>
... ...
@@ -190,6 +190,28 @@ modparam("alias_db", "use_domain", 1)
190 190
 ...
191 191
 modparam("alias_db", "domain_prefix", "sip.")
192 192
 ...
193
+</programlisting>
194
+		</example>
195
+	</section>
196
+	
197
+	<section>
198
+		<title><varname>append_branches</varname> (int)</title>
199
+		<para>
200
+			If the alias resolves to many SIP IDs, the first is replacing
201
+			the R-URI, the rest are added as branches.
202
+		</para>
203
+		<para>
204
+		<emphasis>
205
+			Default value is <quote>0</quote> (0 - don't add branches;
206
+			1 - add branches).
207
+		</emphasis>
208
+		</para>
209
+		<example>
210
+		<title>Set <varname>append_branches</varname> parameter</title>
211
+		<programlisting format="linespecific">
212
+...
213
+modparam("alias_db", "append_branches", 1)
214
+...
193 215
 </programlisting>
194 216
 		</example>
195 217
 	</section>