Skip to content

Commit

Permalink
Merge pull request #285 from andrei-datcu/master
Browse files Browse the repository at this point in the history
[dispatcher] ds_select_dst domain working corectly now
  • Loading branch information
razvancrainea committed Aug 1, 2014
2 parents 6f4ed2f + 80ef8f4 commit 0a119ed
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 26 deletions.
76 changes: 50 additions & 26 deletions modules/dispatcher/dispatch.c
Expand Up @@ -195,35 +195,52 @@ int add_dest2list(int id, str uri, struct socket_info *sock, int state,
memset(dp, 0, sizeof(ds_dest_t));

/* store uri and attrs strings */
dp->uri.s = shm_malloc( (puri.host.len)
+ (puri.port.len ? puri.port.len + 1 : 0)
+ 1 + attrs.len + 1 );
if(dp->uri.s==NULL)
{
LM_ERR("no more shm memory!\n");
goto err;
}

dp->uri.len = 0;
char *p = dp->uri.s;
dp->uri.len = uri.len;
if (puri.user.len == 0 && puri.passwd.len == 0 && puri.params.len == 0
&& puri.headers.len == 0) {

memcpy(p, puri.host.s, puri.host.len);
dp->uri.len += puri.host.len;
p += puri.host.len;
/* The uri from db is good for ds_select_dst */
dp->uri.s = shm_malloc(uri.len + 1 + attrs.len + 1);
if(dp->uri.s==NULL){
LM_ERR("no more shm memory!\n");
goto err;
}
dp->dst_uri = dp->uri;
dp->attrs.s = dp->uri.s + dp->uri.len + 1;
}
else {
dp->dst_uri.len = uri_typestrlen(puri.type) + 1 + puri.host.len
+ (puri.port.len ? puri.port.len + 1 : 0);
dp->uri.s = shm_malloc(uri.len + 1 + dp->dst_uri.len + 1 + attrs.len + 1);
if(dp->uri.s==NULL){
LM_ERR("no more shm memory!\n");
goto err;
}

if (puri.port.len) {
dp->attrs.s = dp->uri.s + dp->uri.len + 1 + dp->dst_uri.len + 1;
dp->dst_uri.s = dp->uri.s + dp->uri.len + 1;
char *p = uri_type2str(puri.type, dp->dst_uri.s);
*(p++) = ':';
memcpy(p, puri.port.s, puri.port.len);
dp->uri.len += puri.port.len + 1;

memcpy(p, puri.host.s, puri.host.len);
p += puri.host.len;

if (puri.port.len) {
*(p++) = ':';
memcpy(p, puri.port.s, puri.port.len);
}
dp->dst_uri.s[dp->dst_uri.len]='\0';
}
dp->uri.s[dp->uri.len]='\0';

memcpy(dp->uri.s, uri.s, dp->uri.len);

if (attrs.len) {
dp->attrs.s = dp->uri.s + dp->uri.len + 1;
memcpy(dp->attrs.s, attrs.s, attrs.len);
dp->attrs.s[attrs.len]='\0';
dp->attrs.len = attrs.len;
}
else dp->attrs.s = NULL;

/* copy state, weight & socket */
dp->sock = sock;
Expand Down Expand Up @@ -294,7 +311,8 @@ int add_dest2list(int id, str uri, struct socket_info *sock, int state,
d_data->sets_no++;
}

LM_DBG("dest [%d/%d] <%.*s> successfully loaded\n", sp->id, sp->nr, dp->uri.len, dp->uri.s);
LM_DBG("dest [%d/%d] <%.*s> <%.*s> successfully loaded\n", sp->id, sp->nr,
dp->uri.len, dp->uri.s, dp->dst_uri.len, dp->dst_uri.s);

return 0;
err:
Expand Down Expand Up @@ -1220,17 +1238,23 @@ static inline int ds_update_dst(struct sip_msg *msg, str *uri,
struct socket_info *sock, int mode)
{
struct action act;
uri_type utype;
int typelen;

switch(mode)
{
case 1:
act.type = SET_HOSTPORT_T;
act.elem[0].type = STR_ST;
act.elem[0].u.s = *uri;
if (uri->len>4 && strncasecmp(uri->s,"sip:",4)==0) {
act.elem[0].u.s.s += 4;
act.elem[0].u.s.len -= 4;

utype = str2uri_type(uri->s);
if (utype == ERROR_URI_T) {
LM_ERR("Uknown uri type\n");
return -1;
}
typelen = uri_typestrlen(utype);
act.elem[0].u.s.s = uri->s + typelen + 1;
act.elem[0].u.s.len = uri->len - typelen - 1;
act.next = 0;

if (do_action(&act, msg) < 0) {
Expand Down Expand Up @@ -1280,7 +1304,7 @@ static inline int push_ds_2_avps( ds_dest_t *ds, ds_partition_t *partition )
return -1;
}

avp_val.s = ds->uri;
avp_val.s = ds->dst_uri;
if(add_avp(AVP_VAL_STR| partition->dst_avp_type,
partition->dst_avp_name, avp_val)!=0) {
LM_ERR("failed to add DST avp\n");
Expand Down Expand Up @@ -1480,7 +1504,7 @@ int ds_select_dst(struct sip_msg *msg, ds_select_ctl_p ds_select_ctl)
}

if(ds_select_ctl->set_destination
&& ds_update_dst(msg, &selected->uri, selected->sock, ds_select_ctl->mode)!=0)
&& ds_update_dst(msg, &selected->dst_uri, selected->sock, ds_select_ctl->mode)!=0)
{
LM_ERR("cannot set dst addr\n");
goto error;
Expand All @@ -1490,7 +1514,7 @@ int ds_select_dst(struct sip_msg *msg, ds_select_ctl_p ds_select_ctl)
idx->last = (ds_id+1) % idx->nr;

LM_DBG("selected [%d-%d/%d] <%.*s>\n", ds_select_ctl->alg, ds_select_ctl->set, ds_id,
selected->uri.len, selected->uri.s);
selected->dst_uri.len, selected->dst_uri.s);

if(!(ds_flags&DS_FAILOVER_ON))
goto done;
Expand Down
1 change: 1 addition & 0 deletions modules/dispatcher/dispatch.h
Expand Up @@ -70,6 +70,7 @@
typedef struct _ds_dest
{
str uri;
str dst_uri; /* Actual uri used in ds_select_dst ds_select_domain */
str attrs;
int flags;
int weight;
Expand Down
45 changes: 45 additions & 0 deletions parser/parse_uri.c
Expand Up @@ -1496,3 +1496,48 @@ int compare_uris(str *raw_uri_a,struct sip_uri* parsed_uri_a,
compare_uri_val(headers,strncasecmp);
return 0;
}

static const str uri_type_names[6] = {
{NULL, 0}, /*This is the error type*/
str_init("sip"),
str_init("sips"),
str_init("tel"),
str_init("tels"),
str_init("urn:service")
};

char* uri_type2str(const uri_type type, char *result)
{
if (type == ERROR_URI_T)
return NULL;

memcpy(result, uri_type_names[type].s, uri_type_names[type].len);
return result + uri_type_names[type].len;
}

int uri_typestrlen(const uri_type type)
{
return uri_type_names[type].len;
}

uri_type str2uri_type(char * buf)
{
int scheme = 0;
uri_type type = ERROR_URI_T;
scheme=buf[0]+(buf[1]<<8)+(buf[2]<<16)+(buf[3]<<24);
scheme|=0x20202020;
if (scheme==SIP_SCH){
type=SIP_URI_T;
}else if(scheme==SIPS_SCH){
if(buf[4]==':')
type=SIPS_URI_T;
else type = ERROR_URI_T;
}else if (scheme==TEL_SCH){
type=TEL_URI_T;
}else if (scheme==URN_SERVICE_SCH){
if (memcmp(buf+3,URN_SERVICE_STR,URN_SERVICE_STR_LEN) == 0) {
type=URN_SERVICE_URI_T;
}
}
return type;
}
3 changes: 3 additions & 0 deletions parser/parse_uri.h
Expand Up @@ -47,5 +47,8 @@ int parse_sip_msg_uri(struct sip_msg* msg);
int parse_orig_ruri(struct sip_msg* msg);
int compare_uris(str *raw_uri_a,struct sip_uri* parsed_uri_a,
str *raw_uri_b,struct sip_uri *parsed_uri_b);
char * uri_type2str(const uri_type type, char *result);
int uri_typestrlen(const uri_type type);
uri_type str2uri_type(char * buf);

#endif /* PARSE_URI_H */

0 comments on commit 0a119ed

Please sign in to comment.