int rls_handle_subscribe(struct sip_msg* msg, str watcher_user, str watcher_domain) { subs_t subs; pres_ev_t* event = NULL; int err_ret = -1; int ret = to_presence_code; str* contact = NULL; xmlDocPtr doc = NULL; xmlNodePtr service_node = NULL; unsigned int hash_code=0; int to_tag_gen = 0; event_t* parsed_event; param_t* ev_param = NULL; str reason; int rt; str rlsubs_did = {0, 0}; memset(&subs, 0, sizeof(subs_t)); /** sanity checks - parse all headers */ if (parse_headers(msg, HDR_EOH_F, 0)<-1) { LM_ERR("failed parsing all headers\n"); if (slb.freply(msg, 400, &pu_400_rpl) < 0) { LM_ERR("while sending 400 reply\n"); return -1; } return 0; } /* check for To and From headesr */ if(parse_to_uri(msg)<0 || parse_from_uri(msg)<0) { LM_ERR("failed to find To or From headers\n"); if (slb.freply(msg, 400, &pu_400_rpl) < 0) { LM_ERR("while sending 400 reply\n"); return -1; } return 0; } if(get_from(msg)->tag_value.s ==NULL || get_from(msg)->tag_value.len==0) { LM_ERR("no from tag value present\n"); return -1; } if(msg->callid==NULL || msg->callid->body.s==NULL) { LM_ERR("cannot find callid header\n"); return -1; } if(parse_sip_msg_uri(msg)<0) { LM_ERR("failed parsing Request URI\n"); return -1; } /* check for header 'Support: eventlist' */ if(msg->supported==NULL) { LM_DBG("supported header not found - not for rls\n"); goto forpresence; } if(parse_supported(msg)<0) { LM_ERR("failed to parse supported headers\n"); return -1; } if(!(get_supported(msg) & F_SUPPORTED_EVENTLIST)) { LM_DBG("No support for 'eventlist' - not for rls\n"); goto forpresence; } /* inspecting the Event header field */ if(msg->event && msg->event->body.len > 0) { if (!msg->event->parsed && (parse_event(msg->event) < 0)) { LM_ERR("cannot parse Event header\n"); goto error; } if(! ( ((event_t*)msg->event->parsed)->type & rls_events) ) { goto forpresence; } } else { goto bad_event; } /* search event in the list */ parsed_event = (event_t*)msg->event->parsed; event = pres_search_event(parsed_event); if(event==NULL) { goto bad_event; } subs.event= event; /* extract the id if any*/ ev_param= parsed_event->params.list; while(ev_param) { if(ev_param->name.len==2 && strncmp(ev_param->name.s, "id", 2)==0) { subs.event_id = ev_param->body; break; } ev_param= ev_param->next; } /* extract dialog information from message headers */ if(pres_extract_sdialog_info(&subs, msg, rls_max_expires, &to_tag_gen, rls_server_address, watcher_user, watcher_domain)<0) { LM_ERR("bad subscribe request\n"); goto error; } hash_code = core_hash(&subs.callid, &subs.to_tag, hash_size); if (CONSTR_RLSUBS_DID(&subs, &rlsubs_did) < 0) { LM_ERR("cannot build rls subs did\n"); goto error; } subs.updated = core_hash(&rlsubs_did, NULL, 0) % (waitn_time * rls_notifier_poll_rate * rls_notifier_processes); if(get_to(msg)->tag_value.s==NULL || get_to(msg)->tag_value.len==0) { /* initial Subscribe */ /*verify if Request URI represents a list by asking xcap server*/ if(uandd_to_uri(msg->parsed_uri.user, msg->parsed_uri.host, &subs.pres_uri)<0) { LM_ERR("while constructing uri from user and domain\n"); goto error; } if(rls_get_service_list(&subs.pres_uri, &subs.watcher_user, &subs.watcher_domain, &service_node, &doc)<0) { LM_ERR("while attepmting to get a resource list\n"); goto error; } if(doc==NULL) { /* if not for RLS, pass it to presence serivce */ LM_DBG("list not found - searched for uri <%.*s>\n", subs.pres_uri.len, subs.pres_uri.s); goto forpresence; } /* if correct reply with 200 OK */ if(reply_200(msg, &subs.local_contact, subs.expires)<0) goto error; subs.local_cseq = 0; if(subs.expires != 0) { subs.version = 1; if (dbmode==RLS_DB_ONLY) { rt=insert_rlsdb( &subs ); } else { rt=pres_insert_shtable(rls_table, hash_code, &subs); } if (rt<0) { LM_ERR("while adding new subscription\n"); goto error; } } } else { /* search if a stored dialog */ if ( dbmode == RLS_DB_ONLY ) { if (rls_dbf.start_transaction) { if (rls_dbf.start_transaction(rls_db, DB_LOCKING_WRITE) < 0) { LM_ERR("in start_transaction\n"); goto error; } } rt = get_dialog_subscribe_rlsdb(&subs); if (rt <= 0) { LM_DBG("subscription dialog not found for <%.*s@%.*s>\n", subs.watcher_user.len, subs.watcher_user.s, subs.watcher_domain.len, subs.watcher_domain.s); if (rls_dbf.end_transaction) { if (rls_dbf.end_transaction(rls_db) < 0) { LM_ERR("in end_transaction\n"); goto error; } } goto forpresence; } else if(rt>=400) { reason = (rt==400)?pu_400_rpl:stale_cseq_rpl; if (slb.freply(msg, 400, &reason) < 0) { LM_ERR("while sending reply\n"); goto error; } if (rls_dbf.end_transaction) { if (rls_dbf.end_transaction(rls_db) < 0) { LM_ERR("in end_transaction\n"); goto error; } } ret = 0; goto stop; } /* if correct reply with 200 OK */ if(reply_200(msg, &subs.local_contact, subs.expires)<0) goto error; if (update_dialog_subscribe_rlsdb(&subs) < 0) { LM_ERR("while updating resource list subscription\n"); goto error; } if (rls_dbf.end_transaction) { if (rls_dbf.end_transaction(rls_db) < 0) { LM_ERR("in end_transaction\n"); goto error; } } } else { lock_get(&rls_table[hash_code].lock); if(pres_search_shtable(rls_table, subs.callid, subs.to_tag, subs.from_tag, hash_code)==NULL) { lock_release(&rls_table[hash_code].lock); LM_DBG("subscription dialog not found for <%.*s@%.*s>\n", subs.watcher_user.len, subs.watcher_user.s, subs.watcher_domain.len, subs.watcher_domain.s); goto forpresence; } lock_release(&rls_table[hash_code].lock); /* if correct reply with 200 OK */ if(reply_200(msg, &subs.local_contact, subs.expires)<0) goto error; rt = update_rlsubs(&subs, hash_code); if(rt<0) { LM_ERR("while updating resource list subscription\n"); goto error; } if(rt>=400) { reason = (rt==400)?pu_400_rpl:stale_cseq_rpl; if (slb.freply(msg, 400, &reason) < 0) { LM_ERR("while sending reply\n"); goto error; } ret = 0; goto stop; } } if(rls_get_service_list(&subs.pres_uri, &subs.watcher_user, &subs.watcher_domain, &service_node, &doc)<0) { LM_ERR("failed getting resource list\n"); goto error; } if(doc==NULL) { /* warning: no document returned?!?! */ LM_WARN("no document returned for uri <%.*s>\n", subs.pres_uri.len, subs.pres_uri.s); goto done; } } if (dbmode != RLS_DB_ONLY) { /* sending notify with full state */ if(send_full_notify(&subs, service_node, &subs.pres_uri, hash_code)<0) { LM_ERR("failed sending full state notify\n"); goto error; } } /* send subscribe requests for all in the list */ if(resource_subscriptions(&subs, service_node)< 0) { LM_ERR("failed sending subscribe requests to resources in list\n"); goto error; } if (dbmode !=RLS_DB_ONLY) remove_expired_rlsubs(&subs, hash_code); done: ret = 1; stop: forpresence: if(contact!=NULL) { if(contact->s!=NULL) pkg_free(contact->s); pkg_free(contact); } if(subs.pres_uri.s!=NULL) pkg_free(subs.pres_uri.s); if(subs.record_route.s!=NULL) pkg_free(subs.record_route.s); if(doc!=NULL) xmlFreeDoc(doc); if (rlsubs_did.s != NULL) pkg_free(rlsubs_did.s); return ret; bad_event: err_ret = 0; if(reply_489(msg)<0) { LM_ERR("failed sending 489 reply\n"); err_ret = -1; } error: LM_ERR("occured in rls_handle_subscribe\n"); if(contact!=NULL) { if(contact->s!=NULL) pkg_free(contact->s); pkg_free(contact); } if(subs.pres_uri.s!=NULL) pkg_free(subs.pres_uri.s); if(subs.record_route.s!=NULL) pkg_free(subs.record_route.s); if(doc!=NULL) xmlFreeDoc(doc); if (rlsubs_did.s != NULL) pkg_free(rlsubs_did.s); if (rls_dbf.abort_transaction) { if (rls_dbf.abort_transaction(rls_db) < 0) LM_ERR("in abort_transaction\n"); } return err_ret; }
int resource_subscriptions(subs_t* subs, xmlNodePtr rl_node) { char* uri= NULL; subs_info_t s; str wuri= {0, 0}; str did_str= {0, 0}; str *tmp_str; int cont_no= 0; static str ehdr= {SUBS_EXTRA_HDRS, SUBS_EXTRA_HDRS_LEN}; list_entry_t *rls_contact_list = NULL; list_entry_t *rls_subs_list = NULL; void* params[2] = {&s, &rls_contact_list}; /* if is initial send an initial Subscribe * else search in hash table for a previous subscription */ if(CONSTR_RLSUBS_DID(subs, &did_str)< 0) { LM_ERR("Failed to create did\n"); return -1; } memset(&s, 0, sizeof(subs_info_t)); if( uandd_to_uri(subs->from_user, subs->from_domain, &wuri)< 0) { LM_ERR("while constructing uri from user and domain\n"); goto error; } s.id= did_str; s.watcher_uri= &wuri; s.to_uri.s=0; s.contact= &server_address; s.event= get_event_flag(&subs->event->name); if(presence_server.s) s.outbound_proxy= &presence_server; if(s.event< 0) { LM_ERR("not recognized event\n"); goto error; } s.expires= subs->expires; s.source_flag= RLS_SUBSCRIBE; s.extra_headers= &ehdr; s.internal_update_flag = subs->internal_update_flag; if(process_list_and_exec(rl_node, subs->from_user, subs->from_domain, send_resource_subs, params, &cont_no) < 0) { LM_ERR("while processing list\n"); goto error; } LM_INFO("Subscription from %.*s for resource list uri %.*s expanded to" " %d contacts\n", wuri.len, wuri.s, subs->pres_uri.len, subs->pres_uri.s, cont_no); if (s.internal_update_flag) { s.internal_update_flag = 0; rls_subs_list = pua_get_subs_list(&did_str); while ((tmp_str = list_pop(&rls_contact_list)) != NULL) { rls_subs_list = list_remove(*tmp_str, rls_subs_list); pkg_free(tmp_str->s); pkg_free(tmp_str); } while ((tmp_str = list_pop(&rls_subs_list)) != NULL) { LM_DBG("Removing subscription for %.*s\n", tmp_str->len, tmp_str->s); s.expires = 0; send_resource_subs(tmp_str->s, params); pkg_free(tmp_str->s); pkg_free(tmp_str); } } if (rls_contact_list != NULL) { list_free(&rls_contact_list); } pkg_free(wuri.s); pkg_free(did_str.s); return 0; error: if(wuri.s) pkg_free(wuri.s); if(uri) xmlFree(uri); if(did_str.s) pkg_free(did_str.s); return -1; }
/** * send subscriptions to the list from XML node */ int resource_subscriptions(subs_t* subs, xmlNodePtr xmlnode) { subs_info_t s; str wuri= {0, 0}; str extra_headers; str did_str= {0, 0}; str *tmp_str; list_entry_t *rls_contact_list = NULL; list_entry_t *rls_subs_list = NULL; void* params[2] = {&s, &rls_contact_list}; /* if is initial send an initial Subscribe * else search in hash table for a previous subscription */ if(CONSTR_RLSUBS_DID(subs, &did_str)<0) { LM_ERR("cannot build rls subs did\n"); goto error; } memset(&s, 0, sizeof(subs_info_t)); if(uandd_to_uri(subs->watcher_user, subs->watcher_domain, &wuri)<0) { LM_ERR("while constructing uri from user and domain\n"); goto error; } s.id = did_str; s.watcher_uri = &wuri; s.contact = &rls_server_address; s.event = get_event_flag(&subs->event->name); if(s.event<0) { LM_ERR("not recognized event\n"); goto error; } s.expires = subs->expires; s.source_flag = RLS_SUBSCRIBE; if(rls_outbound_proxy.s) s.outbound_proxy = &rls_outbound_proxy; extra_headers.s = "Supported: eventlist\r\n" "Accept: application/pidf+xml, application/rlmi+xml," " application/watcherinfo+xml," " multipart/related\r\n"; extra_headers.len = strlen(extra_headers.s); s.extra_headers = &extra_headers; s.internal_update_flag = subs->internal_update_flag; counter = 0; if(process_list_and_exec(xmlnode, subs->watcher_user, subs->watcher_domain, send_resource_subs, params)<0) { LM_ERR("while processing list\n"); goto error; } if (rls_max_backend_subs > 0 && counter > rls_max_backend_subs) LM_WARN("%.*s has too many contacts. Max: %d, has: %d\n", wuri.len, wuri.s, rls_max_backend_subs, counter); if (s.internal_update_flag == INTERNAL_UPDATE_TRUE) { counter = 0; s.internal_update_flag = 0; rls_subs_list = pua_get_subs_list(&did_str); while ((tmp_str = list_pop(&rls_contact_list)) != NULL) { LM_DBG("Finding and removing %.*s from subscription list\n", tmp_str->len, tmp_str->s); rls_subs_list = list_remove(*tmp_str, rls_subs_list); pkg_free(tmp_str->s); pkg_free(tmp_str); } while ((tmp_str = list_pop(&rls_subs_list)) != NULL) { LM_DBG("Removing subscription for %.*s\n", tmp_str->len, tmp_str->s); s.expires = 0; send_resource_subs(tmp_str->s, params); pkg_free(tmp_str->s); pkg_free(tmp_str); } } if (rls_contact_list != NULL) list_free(&rls_contact_list); pkg_free(wuri.s); pkg_free(did_str.s); return 0; error: if(wuri.s) pkg_free(wuri.s); if(did_str.s) pkg_free(did_str.s); if(rls_contact_list) list_free(&rls_contact_list); return -1; }