pua_bla/notify.c

Go to the documentation of this file.
00001 /*
00002  * $Id: notify.c 1666 2007-03-02 13:40:09Z anca_vamanu $
00003  *
00004  * pua_bla module - pua Bridged Line Appearance
00005  *
00006  * Copyright (C) 2007 Voice Sistem S.R.L.
00007  *
00008  * This file is part of 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  *  2007-03-30  initial version (anca)
00027  */
00028 #include<stdio.h>
00029 #include<stdlib.h>
00030 #include<libxml/parser.h>
00031 
00032 #include "../../parser/parse_content.h"
00033 #include "../../parser/contact/parse_contact.h"
00034 #include "../../parser/parse_from.h"
00035 #include "../../cmpapi.h"
00036 #include "../pua/hash.h"
00037 #include"pua_bla.h"
00038 
00039 int bla_handle_notify(struct sip_msg* msg, char* s1, char* s2)
00040 {
00041    publ_info_t publ;
00042    struct to_body *pto= NULL, TO, *pfrom = NULL;
00043    str body;
00044    ua_pres_t dialog;
00045    unsigned int expires= 0;
00046    struct hdr_field* hdr;
00047    str subs_state;
00048    int found= 0;
00049    str extra_headers= {0, 0};
00050    static char buf[255];
00051    xmlDoc* doc= NULL;
00052    str contact;
00053 
00054    memset(&publ, 0, sizeof(publ_info_t));
00055    memset(&dialog, 0, sizeof(ua_pres_t));
00056  
00057    LM_DBG("start\n");
00058   
00059    if ( parse_headers(msg,HDR_EOH_F, 0)==-1 )
00060    {
00061       LM_ERR("parsing headers\n");
00062       return -1;
00063    }
00064   
00065    if( msg->to==NULL || msg->to->body.s==NULL)
00066    {
00067       LM_ERR("cannot parse TO header\n");
00068       goto error;
00069    }
00070    /* examine the to header */
00071    if(msg->to->parsed != NULL)
00072    {
00073       pto = (struct to_body*)msg->to->parsed;
00074       LM_DBG("'To' header ALREADY PARSED: <%.*s>\n",
00075             pto->uri.len, pto->uri.s );
00076    }
00077    else
00078    {
00079       memset( &TO , 0, sizeof(TO) );
00080       parse_to(msg->to->body.s,msg->to->body.s + msg->to->body.len + 1, &TO);
00081       if(TO.uri.len <= 0)
00082       {
00083          LM_DBG("'To' header NOT parsed\n");
00084          goto error;
00085       }
00086       pto = &TO;
00087    }
00088    publ.pres_uri= &pto->uri;
00089    dialog.watcher_uri= publ.pres_uri;
00090   
00091    if (pto->tag_value.s==NULL || pto->tag_value.len==0 )
00092    {
00093       LM_ERR("NULL to_tag value\n");
00094       goto error;
00095    }
00096    dialog.from_tag= pto->tag_value;
00097   
00098    if( msg->callid==NULL || msg->callid->body.s==NULL)
00099    {
00100       LM_ERR("cannot parse callid header\n");
00101       goto error;
00102    }
00103    dialog.call_id = msg->callid->body;
00104   
00105    if (!msg->from || !msg->from->body.s)
00106    {
00107       LM_ERR("cannot find 'from' header!\n");
00108       goto error;
00109    }
00110    if (msg->from->parsed == NULL)
00111    {
00112       LM_DBG(" 'From' header not parsed\n");
00113       /* parsing from header */
00114       if ( parse_from_header( msg )<0 )
00115       {
00116          LM_DBG(" ERROR cannot parse From header\n");
00117          goto error;
00118       }
00119    }
00120    pfrom = (struct to_body*)msg->from->parsed;
00121    dialog.pres_uri= &pfrom->uri;
00122  
00123    if( pfrom->tag_value.s ==NULL || pfrom->tag_value.len == 0)
00124    {
00125       LM_ERR("no from tag value present\n");
00126       goto error;
00127    }
00128  
00129    dialog.to_tag= pfrom->tag_value;
00130    dialog.event= BLA_EVENT;
00131    dialog.flag= BLA_SUBSCRIBE;
00132    if(pua_is_dialog(&dialog)< 0)
00133    {
00134       LM_ERR("Notify in a non existing dialog\n");
00135       goto error;
00136    }
00137    LM_DBG("found a matching dialog\n");
00138  
00139    /* parse Subscription-State and extract expires if existing */
00140    hdr = msg->headers;
00141    while (hdr!= NULL)
00142    {
00143       if(cmp_hdrname_strzn(&hdr->name, "Subscription-State",18)==0)
00144       {
00145          found = 1;
00146          break;
00147       }
00148       hdr = hdr->next;
00149    }
00150    if(found==0 )
00151    {
00152       LM_ERR("No Subscription-State header found\n");
00153       goto error;
00154    }
00155    subs_state= hdr->body;
00156    if(strncmp(subs_state.s, "terminated", 10)== 0)
00157       expires= 0;
00158    else
00159       if(strncmp(subs_state.s, "active", 6)== 0 ||
00160             strncmp(subs_state.s, "pending", 7)==0 )
00161       {
00162             char* sep= NULL;
00163             str exp= {0, 0};
00164          sep= strchr(subs_state.s, ';');
00165             if(sep== NULL)
00166             {
00167                LM_ERR("No expires found in Notify\n");
00168                goto error;
00169             }
00170             if(strncmp(sep+1, "expires=", 8)!= 0)
00171             {
00172                LM_ERR("No expires found in Notify\n");
00173                goto error;
00174             }
00175             exp.s= sep+ 9;
00176             sep= exp.s;
00177             while((*sep)>='0' && (*sep)<='9')
00178             {
00179                sep++;
00180                exp.len++;
00181             }
00182             if( str2int(&exp, &expires)< 0)
00183             {
00184                LM_ERR("while parsing int\n");
00185                goto error;
00186             }
00187          }
00188    
00189       if ( get_content_length(msg) == 0 )
00190       {
00191          LM_ERR("content length= 0\n");
00192          goto error;
00193       }
00194       else
00195       {
00196          body.s=get_body(msg);
00197          if (body.s== NULL)
00198          {
00199             LM_ERR("cannot extract body from msg\n");
00200             goto error;
00201          }
00202          body.len = get_content_length( msg );
00203       }
00204       
00205    if(msg->contact== NULL || msg->contact->body.s== NULL)
00206    {
00207       LM_ERR("no contact header found");
00208       goto error;
00209    }
00210    if( parse_contact(msg->contact) <0 )
00211    {
00212       LM_ERR(" cannot parse contact header\n");
00213       goto error;
00214    }
00215 
00216    if(msg->contact->parsed == NULL)
00217    {
00218       LM_ERR("cannot parse contact header\n");
00219       goto error;
00220    }
00221    contact = ((contact_body_t* )msg->contact->parsed)->contacts->uri;
00222 
00223    /* build extra_headers with Sender*/
00224       extra_headers.s= buf;
00225       memcpy(extra_headers.s, header_name.s, header_name.len);
00226       extra_headers.len= header_name.len;
00227       memcpy(extra_headers.s+extra_headers.len,": ",2);
00228       extra_headers.len+= 2;
00229       memcpy(extra_headers.s+ extra_headers.len, contact.s, contact.len);
00230       extra_headers.len+= contact.len;
00231       memcpy(extra_headers.s+ extra_headers.len, CRLF, CRLF_LEN);
00232       extra_headers.len+= CRLF_LEN;
00233    
00234       publ.body= &body;
00235       publ.source_flag= BLA_PUBLISH;
00236       publ.expires= expires;
00237       publ.event= BLA_EVENT;
00238       publ.extra_headers= &extra_headers;
00239    
00240       if(pua_send_publish(&publ)< 0)
00241       {
00242          LM_ERR("while sending Publish\n");
00243          goto error;
00244       }
00245       
00246       xmlCleanupParser();
00247       xmlMemoryDump();
00248    
00249    return 1;
00250    
00251 error:
00252       if(doc)
00253          xmlFreeDoc(doc);
00254    
00255       return 0;
00256 }
00257 

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