void update_a_sub(subs_t *subs_copy) { xmlDocPtr doc = NULL; xmlNodePtr service_node = NULL; int now = (int)time(NULL); if (subs_copy->expires < now) { subs_copy->expires = 0; LM_WARN("found expired subscription for: %.*s\n", subs_copy->pres_uri.len, subs_copy->pres_uri.s); goto done; } subs_copy->expires -= now; if(rls_get_service_list(&subs_copy->pres_uri, &subs_copy->watcher_user, &subs_copy->watcher_domain, &service_node, &doc)<0) { LM_ERR("failed getting resource list for: %.*s\n", subs_copy->pres_uri.len, subs_copy->pres_uri.s); goto done; } if(doc==NULL) { LM_WARN("no document returned for: %.*s\n", subs_copy->pres_uri.len, subs_copy->pres_uri.s); goto done; } subs_copy->internal_update_flag = INTERNAL_UPDATE_TRUE; if(resource_subscriptions(subs_copy, service_node)< 0) { LM_ERR("failed sending subscribe requests to resources in list\n"); goto done; } done: if (doc != NULL) xmlFreeDoc(doc); pkg_free(subs_copy); }
static void timer_send_full_state_notifies(int round) { db_key_t query_cols[1], result_cols[22], update_cols[1]; db_val_t query_vals[1], update_vals[1], *values; db_row_t *rows; db1_res_t *result = NULL; int n_result_cols = 0, i; int pres_uri_col, tuser_col, tdomain_col, fuser_col, fdomain_col; int wuser_col, wdomain_col, callid_col, to_tag_col, from_tag_col; int sockinfo_col, lcontact_col, contact_col, rroute_col, event_id_col; int reason_col, event_col, lcseq_col, rcseq_col, status_col; int version_col, expires_col; subs_t sub; str ev_sname; event_t parsed_event; xmlDocPtr doc = NULL; xmlNodePtr service_node = NULL; int now = (int)time(NULL); query_cols[0] = &str_updated_col; query_vals[0].type = DB1_INT; query_vals[0].nul = 0; query_vals[0].val.int_val = round; result_cols[pres_uri_col = n_result_cols++] = &str_presentity_uri_col; result_cols[tuser_col = n_result_cols++] = &str_to_user_col; result_cols[tdomain_col = n_result_cols++] = &str_to_domain_col; result_cols[fuser_col = n_result_cols++] = &str_from_user_col; result_cols[fdomain_col = n_result_cols++] = &str_from_domain_col; result_cols[wuser_col = n_result_cols++] = &str_watcher_username_col; result_cols[wdomain_col = n_result_cols++] = &str_watcher_domain_col; result_cols[callid_col = n_result_cols++] = &str_callid_col; result_cols[to_tag_col = n_result_cols++] = &str_to_tag_col; result_cols[from_tag_col = n_result_cols++] = &str_from_tag_col; result_cols[sockinfo_col = n_result_cols++] = &str_socket_info_col; result_cols[lcontact_col = n_result_cols++] = &str_local_contact_col; result_cols[contact_col = n_result_cols++] = &str_contact_col; result_cols[rroute_col = n_result_cols++] = &str_record_route_col; result_cols[event_id_col = n_result_cols++] = &str_event_id_col; result_cols[reason_col = n_result_cols++] = &str_reason_col; result_cols[event_col = n_result_cols++] = &str_event_col; result_cols[lcseq_col = n_result_cols++] = &str_local_cseq_col; result_cols[rcseq_col = n_result_cols++] = &str_remote_cseq_col; result_cols[status_col = n_result_cols++] = &str_status_col; result_cols[version_col = n_result_cols++] = &str_version_col; result_cols[expires_col = n_result_cols++] = &str_expires_col; update_cols[0] = &str_updated_col; update_vals[0].type = DB1_INT; update_vals[0].nul = 0; update_vals[0].val.int_val = NO_UPDATE_TYPE; if (rls_dbf.use_table(rls_db, &rlsubs_table) < 0) { LM_ERR("use table failed\n"); goto done; } if (dbmode == RLS_DB_ONLY && rls_dbf.start_transaction) { if (rls_dbf.start_transaction(rls_db) < 0) { LM_ERR("in start_transaction\n"); goto done; } } /* Step 1: Find rls_watchers that require full-state notification */ if (rls_dbf.query(rls_db, query_cols, 0, query_vals, result_cols, 1, n_result_cols, 0, &result) < 0) { LM_ERR("in sql query\n"); goto done; } if(result== NULL || result->n<= 0) goto done; /* Step 2: Reset the update flag so we do not full-state notify these watchers again */ if(rls_dbf.update(rls_db, query_cols, 0, query_vals, update_cols, update_vals, 1, 1)< 0) { LM_ERR("in sql update\n"); goto done; } if (dbmode == RLS_DB_ONLY && rls_dbf.end_transaction) { if (rls_dbf.end_transaction(rls_db) < 0) { LM_ERR("in end_transaction\n"); goto done; } } /* Step 3: Full-state notify each watcher we found */ rows = RES_ROWS(result); for (i = 0; i < RES_ROW_N(result); i++) { memset(&sub, 0, sizeof(subs_t)); values = ROW_VALUES(&rows[i]); EXTRACT_STRING(sub.pres_uri, VAL_STRING(&values[pres_uri_col])); EXTRACT_STRING(sub.to_user, VAL_STRING(&values[tuser_col])); EXTRACT_STRING(sub.to_domain, VAL_STRING(&values[tdomain_col])); EXTRACT_STRING(sub.from_user, VAL_STRING(&values[fuser_col])); EXTRACT_STRING(sub.from_domain, VAL_STRING(&values[fdomain_col])); EXTRACT_STRING(sub.watcher_user, VAL_STRING(&values[wuser_col])); EXTRACT_STRING(sub.watcher_domain, VAL_STRING(&values[wdomain_col])); EXTRACT_STRING(sub.callid, VAL_STRING(&values[callid_col])); EXTRACT_STRING(sub.to_tag, VAL_STRING(&values[to_tag_col])); EXTRACT_STRING(sub.from_tag, VAL_STRING(&values[from_tag_col])); EXTRACT_STRING(sub.sockinfo_str, VAL_STRING(&values[sockinfo_col])); EXTRACT_STRING(sub.local_contact, VAL_STRING(&values[lcontact_col])); EXTRACT_STRING(sub.contact, VAL_STRING(&values[contact_col])); EXTRACT_STRING(sub.record_route, VAL_STRING(&values[rroute_col])); EXTRACT_STRING(sub.event_id, VAL_STRING(&values[event_id_col])); EXTRACT_STRING(sub.reason, VAL_STRING(&values[reason_col])); EXTRACT_STRING(ev_sname, VAL_STRING(&values[event_col])); sub.event = pres_contains_event(&ev_sname, &parsed_event); if (sub.event == NULL) { LM_ERR("event not found and set to NULL\n"); goto done; } sub.local_cseq = VAL_INT(&values[lcseq_col]); sub.remote_cseq = VAL_INT(&values[rcseq_col]); sub.status = VAL_INT(&values[status_col]); sub.version = VAL_INT(&values[version_col]); if (VAL_INT(&values[expires_col]) > now + rls_expires_offset) { sub.expires = VAL_INT(&values[expires_col]) - now; if (rls_get_service_list(&sub.pres_uri, &sub.watcher_user, &sub.watcher_domain, &service_node, &doc) < 0) { LM_ERR("failed getting resource list\n"); goto done; } if (doc == NULL) { LM_WARN("no document returned for uri <%.*s>\n", sub.pres_uri.len, sub.pres_uri.s); goto done; } if (send_full_notify(&sub, service_node, &sub.pres_uri, 0) < 0) { LM_ERR("failed sending full state notify\n"); goto done; } xmlFreeDoc(doc); doc = NULL; } else { sub.expires = 0; rls_send_notify(&sub, NULL, NULL, NULL); delete_rlsdb(&sub.callid, &sub.to_tag, &sub.from_tag); } } done: if (result != NULL) rls_dbf.free_result(rls_db, result); if (doc != NULL) xmlFreeDoc(doc); if (dbmode == RLS_DB_ONLY && rls_dbf.abort_transaction) { if (rls_dbf.abort_transaction(rls_db) < 0) LM_ERR("in abort_transaction\n"); } }
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; }