diff --git a/src/plugin_prefix.c b/src/plugin_prefix.c index 0a34de4..1c36a44 100644 --- a/src/plugin_prefix.c +++ b/src/plugin_prefix.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -52,6 +53,11 @@ static char const ident[]="$Id$"; static char name[]="plugin_prefix"; static char desc[]="Adds a dial-prefix as defined in config file"; +/* constants */ +#define REDIRECTED_TAG "redirected" +#define REDIRECTED_VAL "prefix" +#define CACHE_TIMEOUT 20 + /* global configuration storage - required for config file location */ extern struct siproxd_config configuration; @@ -68,9 +74,24 @@ static cfgopts_t plugin_cfg_opts[] = { }; +/* local storage needed by plugin */ +/* Call-ID cache, single linked list, dynamically alocated elements */ +typedef struct { + void *next; + osip_call_id_t *call_id; + time_t ts; +} redirected_cache_element_t; + +/* The Queue Head is static */ +static redirected_cache_element_t redirected_cache; + + /* local prototypes */ static int plugin_prefix_redirect(sip_ticket_t *ticket); static int plugin_prefix(sip_ticket_t *ticket); +static int add_to_redirected_cache(sip_ticket_t *ticket); +static int is_in_redirected_cache(sip_ticket_t *ticket); +static int expire_redirected_cache(void); /* @@ -136,28 +157,31 @@ static int plugin_prefix(sip_ticket_t *ticket) { if (!MSG_IS_INVITE(ticket->sipmsg) && !MSG_IS_ACK(ticket->sipmsg)) return STS_SUCCESS; + /* expire old cache entries */ + expire_redirected_cache(); + /* REQ URI with username must exist, prefix string must exist */ if (!req_url || !req_url->username || !plugin_cfg.prefix_akey) return STS_SUCCESS; /* ignore */ /* Loop avoidance: * If this INVITE has already been redirected by a prior 302 - * moved response a "redirected" parameter should be present in the + * moved response a "REDIRECTED_TAG" parameter should be present in the * URI. * Hopefully all UAs (Clients) do honor RFC3261 and copy the * *full* URI form the contact header into the new request header * upon a 3xx response. */ if (req_url) { - osip_uri_param_get_byname(&(req_url->url_params), "redirected", &r); - if (r && r->gvalue && strcmp(r->gvalue,"true")== 0) { + osip_uri_param_get_byname(&(req_url->url_params), REDIRECTED_TAG, &r); + if (r && r->gvalue && strcmp(r->gvalue,REDIRECTED_VAL)== 0) { DEBUGC(DBCLASS_PLUGIN,"Packet has already been redirected (ReqURI)"); return STS_SUCCESS; } } if (to_url) { - osip_uri_param_get_byname(&(to_url->url_params), "redirected", &r); - if (r && r->gvalue && strcmp(r->gvalue,"true")== 0) { + osip_uri_param_get_byname(&(to_url->url_params), REDIRECTED_TAG, &r); + if (r && r->gvalue && strcmp(r->gvalue,REDIRECTED_VAL)== 0) { DEBUGC(DBCLASS_PLUGIN,"Packet has already been redirected (ToURI)"); return STS_SUCCESS; } @@ -172,11 +196,19 @@ static int plugin_prefix(sip_ticket_t *ticket) { DEBUGC(DBCLASS_PLUGIN,"processing INVITE"); sts=plugin_prefix_redirect(ticket); } - /* outgoing ACK request: is result of a local 3xx answer (moved...) */ + /* outgoing ACK request: is result of a local 3xx answer (moved...) + * + * Only consume that particular ACK that belongs to a sent 302 answer, + * nothing else. Otherwise the ACK from the redirected call will get + * consumed as well and causes the call to be aborted (timeout). + * We keep a cache with Call-Ids of such "302 moved" dialogs. + * Only consume such ACKs that are part of such a dialog. + */ else if (MSG_IS_ACK(ticket->sipmsg)) { - /* make sure we only catch ACKs caused by myself (**02 -> *02 legitime) */ - DEBUGC(DBCLASS_PLUGIN,"processing ACK"); - sts=STS_SIP_SENT; /* eat up the ACK that was directed to myself */ + if (is_in_redirected_cache(ticket) == STS_TRUE) { + DEBUGC(DBCLASS_PLUGIN,"processing ACK (consume it)"); + sts=STS_SIP_SENT; /* eat up the ACK that was directed to myself */ + } } return sts; @@ -192,6 +224,7 @@ static int plugin_prefix_redirect(sip_ticket_t *ticket) { size_t username_len; osip_contact_t *contact = NULL; + add_to_redirected_cache(ticket); /* including \0 + leading character(s) */ username_len=strlen(to_user) + strlen(plugin_cfg.prefix_akey) + 1; @@ -216,13 +249,13 @@ static int plugin_prefix_redirect(sip_ticket_t *ticket) { osip_uri_clone(to_url, &contact->url); /* - * Add the 'redirected=true' parameter to URI. Required to figure out + * Add the 'REDIRECTED_TAG=REDIRECTED_VAL' parameter to URI. Required to figure out * if this INVITE has already been processed (redirected) and * does not need further attention by this plugin. * THIS IS REQUIRED TO AVOID A LOOP */ - osip_uri_param_add(&(contact->url->url_params), osip_strdup("redirected"), - osip_strdup("true")); + osip_uri_param_add(&(contact->url->url_params), osip_strdup(REDIRECTED_TAG), + osip_strdup(REDIRECTED_VAL)); /* only copy the part that really belongs to the username */ snprintf(new_to_user, username_len, "%s%s", @@ -243,3 +276,88 @@ static int plugin_prefix_redirect(sip_ticket_t *ticket) { return STS_SIP_SENT; } + +/* + * cache handling + */ +static int add_to_redirected_cache(sip_ticket_t *ticket) { + redirected_cache_element_t *e; + DEBUGC(DBCLASS_PLUGIN, "entered add_to_redirected_cache()"); + + /* allocate */ + e=malloc(sizeof(redirected_cache_element_t)); + if (e == NULL) { + ERROR("out of memory"); + return STS_FAILURE; + } + + /* populate element */ + e->next = NULL; + e->ts = time(NULL); + osip_call_id_clone(ticket->sipmsg->call_id, &(e->call_id)); + + /* add to head of queue */ + e->next = redirected_cache.next; + redirected_cache.next = e; + + DEBUGC(DBCLASS_PLUGIN, "left add_to_redirected_cache()"); + return STS_SUCCESS; +} + +static int is_in_redirected_cache(sip_ticket_t *ticket) { + redirected_cache_element_t *p, *p_prev; + + DEBUGC(DBCLASS_BABBLE, "entered is_in_redirected_cache"); + /* iterate through queue */ + p_prev=NULL; + for (p=&redirected_cache; p; p=p->next) { + DEBUGC(DBCLASS_BABBLE, "l: p=%p, p->next=%p", p, p->next); + if ( (p != &redirected_cache) && (p_prev != NULL) ) { + if (compare_callid(ticket->sipmsg->call_id, p->call_id) == STS_SUCCESS) { + DEBUGC(DBCLASS_BABBLE, "remove p=%p", p); + /* remove from queue */ + p_prev->next = p->next; + free(p); + DEBUGC(DBCLASS_BABBLE, "left is_in_redirected_cache - FOUND"); + return STS_TRUE; + } /* if compare_callid */ + } + p_prev = p; + } /* for */ + DEBUGC(DBCLASS_BABBLE, "left is_in_redirected_cache - NOT FOUND"); + return STS_FALSE; +} + +/* + * Run through the whole Call-Id cache and remove + * expired elements. + */ +static int expire_redirected_cache(void) { + redirected_cache_element_t *p, *p_prev; + time_t now; + + DEBUGC(DBCLASS_BABBLE, "entered expire_redirected_cache"); + now = time(NULL); + + /* iterate through queue */ + p_prev=NULL; + for (p=&redirected_cache; p; p=p->next) { + DEBUGC(DBCLASS_BABBLE, "1: p=%p, p->next=%p", p, p->next); + if ( (p != &redirected_cache) && (p_prev != NULL) ) { + DEBUGC(DBCLASS_BABBLE,"ts:%i, now:%i", (int)p->ts, (int)now); + if ((p->ts + CACHE_TIMEOUT) < now) { + DEBUGC(DBCLASS_BABBLE, "remove p=%p", p); + /* remove from queue */ + p_prev->next = p->next; + free(p); + /* the current element is being removed and invalidated, + * set the iteration pointer to a valid element. */ + p = p_prev; + } /* if timeout */ + DEBUGC(DBCLASS_BABBLE, "2: p=%p, p->next=%p", p, p->next); + } + p_prev = p; + } /* for */ + DEBUGC(DBCLASS_BABBLE, "left expire_redirected_cache"); + return STS_FALSE; +}