Module: sip-router
Branch: master
Commit: 3b70140725f180026e30d327a0fb81b3e8902905
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=3b70140…
Author: Henning Westerholt <henning.westerholt(a)1und1.de>
Committer: Henning Westerholt <henning.westerholt(a)1und1.de>
Date: Mon Sep 7 10:19:38 2009 +0200
cr: add random distribution function
- add a random distribution function to cr
- patch from Marius Zbihlei, marius dot zbihlei at 1and1 dot ro
---
modules/carrierroute/README | 6 +++---
modules/carrierroute/cr_fixup.c | 2 ++
modules/carrierroute/doc/carrierroute_admin.xml | 6 +++---
modules/carrierroute/prime_hash.c | 19 +++++++++++++++++++
modules/carrierroute/prime_hash.h | 2 ++
5 files changed, 29 insertions(+), 6 deletions(-)
diff --git a/modules/carrierroute/README b/modules/carrierroute/README
index 7192926..0ab8268 100644
--- a/modules/carrierroute/README
+++ b/modules/carrierroute/README
@@ -493,7 +493,7 @@ hash_source, descavp)
be a contiguous range starting at 1, limited by the
configuration parameter max_targets. Possible values for
hash_source are: call_id, from_uri, from_user, to_uri and
- to_user.
+ to_user and rand.
* decsavp - Name of the AVP where to store the description.
This parameter is optional.
@@ -537,8 +537,8 @@ rewrite_user, hash_source, descavp)
* hash_source - The hash values of the destination set must
be a contiguous range starting at 1, limited by the
configuration parameter max_targets. Possible values for
- hash_source are: call_id, from_uri, from_user, to_uri and
- to_user.
+ hash_source are: call_id, from_uri, from_user, to_uri ,
+ to_user and rand
* descavp - Name of the AVP where to store the description.
This parameter is optional.
diff --git a/modules/carrierroute/cr_fixup.c b/modules/carrierroute/cr_fixup.c
index f832af4..94632f6 100644
--- a/modules/carrierroute/cr_fixup.c
+++ b/modules/carrierroute/cr_fixup.c
@@ -103,6 +103,8 @@ static enum hash_source hash_fixup(const char * my_hash_source) {
return shs_to_uri;
} else if (strcasecmp("to_user", my_hash_source) == 0) {
return shs_to_user;
+ } else if (strcasecmp("rand", my_hash_source) == 0) {
+ return shs_rand;
} else {
return shs_error;
}
diff --git a/modules/carrierroute/doc/carrierroute_admin.xml
b/modules/carrierroute/doc/carrierroute_admin.xml
index 7814d83..656ff04 100644
--- a/modules/carrierroute/doc/carrierroute_admin.xml
+++ b/modules/carrierroute/doc/carrierroute_admin.xml
@@ -480,8 +480,8 @@ cr_tree_rewrite_uri(tree, domain)
<para><emphasis>hash_source</emphasis> - The hash values of the
destination set must
be a contiguous range starting at 1, limited by the
configuration parameter max_targets. Possible values for
- hash_source are: call_id, from_uri, from_user, to_uri
- and to_user.
+ hash_source are: call_id, from_uri, from_user, to_uri,
+ to_user and rand
</para>
</listitem>
<listitem>
@@ -551,7 +551,7 @@ cr_tree_rewrite_uri(tree, domain)
be a contiguous range starting at 1, limited by the
configuration parameter max_targets. Possible values for
hash_source are: call_id, from_uri, from_user, to_uri
- and to_user.
+ to_user and rand
</para>
</listitem>
<listitem>
diff --git a/modules/carrierroute/prime_hash.c b/modules/carrierroute/prime_hash.c
index 8bb9579..25819c5 100644
--- a/modules/carrierroute/prime_hash.c
+++ b/modules/carrierroute/prime_hash.c
@@ -34,9 +34,13 @@
#include "../../lib/kcore/km_crc.h"
#include <ctype.h>
+#include <stdio.h> /* for snprintf */
+#include <stdlib.h> /* for rand */
#include "prime_hash.h"
+#define CR_RANDBUF_S 20
+static char cr_randbuf[CR_RANDBUF_S];
static int determine_source(struct sip_msg *msg, enum hash_source source,
str *source_string);
@@ -44,6 +48,7 @@ static int validate_msg(struct sip_msg * msg);
static int determine_call_id (struct sip_msg *msg, str *source_string);
static int determine_fromto_uri (struct to_body *fromto, str *source_string);
static int determine_fromto_user (struct to_body *fromto, str *source_string);
+static int determine_fromrand(str* source_string);
static int first_token (str *source_string);
@@ -56,6 +61,7 @@ int hash_func (struct sip_msg * msg,
if(determine_source (msg, source, &source_string) == -1) {
return -1;
}
+
crc32_uint(&source_string, &hash);
ret = hash % denominator;
@@ -128,6 +134,8 @@ static int determine_source (struct sip_msg *msg, enum hash_source
source,
return determine_fromto_uri (get_to(msg), source_string);
case shs_to_user:
return determine_fromto_user (get_to(msg), source_string);
+ case shs_rand:
+ return determine_fromrand(source_string); /* msg is not needed */
default:
LM_ERR("unknown hash source %i.\n",
(int) source);
@@ -190,6 +198,17 @@ static int determine_fromto_user (struct to_body *fromto, str
*source_string) {
return 0;
}
+static int determine_fromrand(str* source_string){
+
+ snprintf(&cr_randbuf[0], CR_RANDBUF_S , "%d", rand());
+
+ LM_NOTICE("randbuf is %s\n", cr_randbuf);
+ source_string->s = cr_randbuf;
+ source_string->len = strlen(source_string->s);
+
+ return 0;
+}
+
static int first_token (str *source_string) {
size_t len;
diff --git a/modules/carrierroute/prime_hash.h b/modules/carrierroute/prime_hash.h
index b059742..b383046 100644
--- a/modules/carrierroute/prime_hash.h
+++ b/modules/carrierroute/prime_hash.h
@@ -45,6 +45,7 @@
* - \b shs_from_user the username part of the URI in the From header field
* - \b shs_to_uri the entire URI in the To header field
* - \b shs_to_user the username part of the URI in the To header field
+ * - \b shs_rand some random data which is not related to any header field
* - \b shs_error no hash specified
*/
enum hash_source {
@@ -53,6 +54,7 @@ enum hash_source {
shs_from_user,
shs_to_uri,
shs_to_user,
+ shs_rand,
shs_error
};