pua_bla/notify.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
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
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
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
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