Ejemplo n.º 1
0
int presxml_check_basic(struct sip_msg *msg, str presentity_uri, str status)
{
	str *presentity = NULL;
	struct sip_uri parsed_uri;
	pres_ev_t *ev;
	static str event = str_init("presence");
	int retval = -1;
	xmlDocPtr xmlDoc = NULL;
	xmlNodePtr tuple = NULL, basicNode = NULL;
	char *basicVal = NULL;

	if (parse_uri(presentity_uri.s, presentity_uri.len, &parsed_uri) < 0)
	{
		LM_ERR("bad uri: %.*s\n", presentity_uri.len, presentity_uri.s);
		return -1;
	}

	ev = pres_contains_event(&event, NULL);
	if (ev == NULL)
	{
		LM_ERR("event presence is not registered\n");
		return -1;
	}

	presentity = pres_get_presentity(presentity_uri, ev, NULL, NULL);

	if (presentity == NULL || presentity->len <= 0 || presentity->s == NULL)
	{
		LM_DBG("cannot get presentity for %.*s\n", presentity_uri.len, presentity_uri.s);
		return -1;
	}

	if ((xmlDoc = xmlParseMemory(presentity->s, presentity->len)) == NULL)
	{
		LM_ERR("while parsing XML memory\n");
		goto error;
	}

	if ((tuple = xmlDocGetNodeByName(xmlDoc, "tuple", NULL)) == NULL)
	{
		LM_ERR("unable to extract 'tuple'\n");
		goto error;
	}

	while (tuple != NULL)
	{
		if (xmlStrcasecmp(tuple->name, (unsigned char *) "tuple") == 0)
		{
			if ((basicNode = xmlNodeGetNodeByName(tuple, "basic", NULL)) == NULL)
			{
				LM_ERR("while extracting 'basic' node\n");
				goto error;
			}

			if ((basicVal = (char *) xmlNodeGetContent(basicNode)) == NULL)
			{
				LM_ERR("while getting 'basic' content\n");
				goto error;
			}

			if (strncasecmp(basicVal, status.s, status.len) == 0)
				retval = 1;

			xmlFree(basicVal);
		}
		tuple = tuple->next;
	}
error:
	if (xmlDoc != NULL)
		xmlFreeDoc(xmlDoc);
	pres_free_presentity(presentity, ev);
	return retval;
}
Ejemplo n.º 2
0
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");
	}
}
Ejemplo n.º 3
0
int presxml_check_activities(struct sip_msg *msg, str presentity_uri, str activity)
{
	str *presentity = NULL;
	struct sip_uri parsed_uri;
	pres_ev_t *ev;
	static str event = str_init("presence");
	char *nodeName = NULL;
	int retval = -1;
	xmlDocPtr xmlDoc = NULL;
	xmlNodePtr person = NULL, activitiesNode = NULL, activityNode = NULL;

	if (parse_uri(presentity_uri.s, presentity_uri.len, &parsed_uri) < 0)
	{
		LM_ERR("bad uri: %.*s\n", presentity_uri.len, presentity_uri.s);
		return -1;
	}

	ev = pres_contains_event(&event, NULL);
	if (ev == NULL)
	{
		LM_ERR("event presence is not registered\n");
		return -1;
	}

	if ((nodeName = pkg_malloc(activity.len + 1)) == NULL)
	{
		LM_ERR("cannot pkg_malloc for nodeName\n");
		return -1;		
	}
	memcpy(nodeName, activity.s, activity.len);
	nodeName[activity.len] = '\0';

	presentity = pres_get_presentity(presentity_uri, ev, NULL, NULL);

	if (presentity == NULL || presentity->len <= 0 || presentity->s == NULL)
	{
		LM_DBG("cannot get presentity for %.*s\n", presentity_uri.len, presentity_uri.s);
		goto error;
	}

	if ((xmlDoc = xmlParseMemory(presentity->s, presentity->len)) == NULL)
	{
		LM_ERR("while parsing XML memory\n");
		goto error;
	}

	if ((person = xmlDocGetNodeByName(xmlDoc, "person", NULL)) == NULL)
	{
		LM_DBG("unable to extract 'person'\n");
		retval = -2;
		goto error;
	}

	while (person != NULL)
	{
		if (xmlStrcasecmp(person->name, (unsigned char *) "person") == 0)
		{
			if ((activitiesNode = xmlNodeGetNodeByName(person, "activities", NULL)) == NULL)
			{
				LM_DBG("unable to extract 'activities' node\n");
				if (retval <= 0)
				{
					retval = -2;
				}
				break;
			}

			if (activitiesNode->children == NULL)
			{
				LM_DBG("activities node has no children\n");
				if (retval <= 0)
				{
					retval = -2;
				}
				break;
			}

			if ((activityNode = xmlNodeGetNodeByName(activitiesNode, nodeName, NULL)) != NULL)
			{
				retval = 1;
			}
		}
		person = person->next;
	}
error:
	if (nodeName != NULL)
		pkg_free(nodeName);
	if (xmlDoc != NULL)
		xmlFreeDoc(xmlDoc);
	if(presentity != NULL)
		pres_free_presentity(presentity, ev);
	return retval;
}