[svn-commits] file: branch file/pimp_sip_location r381399 - in /team/file/pimp_sip_location...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Feb 14 09:06:30 CST 2013


Author: file
Date: Thu Feb 14 09:06:27 2013
New Revision: 381399

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=381399
Log:
Bring back AORs and contacts, and add APIs that use them. Next up is some sorcery work to make it easy to retrieve contacts.

Modified:
    team/file/pimp_sip_location/include/asterisk/res_sip.h
    team/file/pimp_sip_location/res/res_sip.c
    team/file/pimp_sip_location/res/res_sip/location.c

Modified: team/file/pimp_sip_location/include/asterisk/res_sip.h
URL: http://svnview.digium.com/svn/asterisk/team/file/pimp_sip_location/include/asterisk/res_sip.h?view=diff&rev=381399&r1=381398&r2=381399
==============================================================================
--- team/file/pimp_sip_location/include/asterisk/res_sip.h (original)
+++ team/file/pimp_sip_location/include/asterisk/res_sip.h Thu Feb 14 09:06:27 2013
@@ -110,6 +110,32 @@
 	pj_ssl_cipher ciphers[SIP_TLS_MAX_CIPHERS];
 	/*! Transport state information */
 	struct ast_sip_transport_state *state;
+};
+
+/*!
+ * \brief Contact associated with an address of record
+ */
+struct ast_sip_contact {
+	/*! Sorcery object details, the id is the aor name plus a random string */
+	SORCERY_OBJECT(details);
+	AST_DECLARE_STRING_FIELDS(
+		/*! Full URI of the contact */
+		AST_STRING_FIELD(uri);
+	);
+	/*! Absolute time that this contact is no longer valid after */
+	struct timeval expiration_time;
+};
+
+/*!
+ * \brief A SIP address of record
+ */
+struct ast_sip_aor {
+	/*! Sorcery object details, the id is the AOR name */
+	SORCERY_OBJECT(details);
+	/*! Default contact expiration if one is not provided in the contact */
+	unsigned int default_expiration;
+	/*! Maximum number of external contacts, 0 to disable */
+	unsigned int max_contacts;
 };
 
 /*!
@@ -427,26 +453,66 @@
 int ast_sip_initialize_sorcery_location(struct ast_sorcery *sorcery);
 
 /*!
- * \brief Retrieve a named location
- *
- * \param name Name of the location
+ * \brief Retrieve a named AOR
+ *
+ * \param aor_name Name of the AOR
  *
  * \retval NULL if not found
  * \retval non-NULL if found
  */
-struct ast_sip_location *ast_sip_location_retrieve(const char *name);
-
-/*!
- * \brief Update the URI and expiration time for a named location
- *
- * \param name Name of the location
- * \param uri Optional fully formed URI
- * \param expiration Optional expiration time
+struct ast_sip_aor *ast_sip_location_retrieve_aor(const char *aor_name);
+
+/*!
+ * \brief Retrieve all contacts currently available for an AOR
+ *
+ * \param aor Pointer to the AOR
+ *
+ * \param NULL if no contacts available
+ * \param non-NULL if contacts available
+ */
+struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_sip_aor *aor);
+
+/*!
+ * \brief Retrieve a named contact
+ *
+ * \param contact_name Name of the contact
+ *
+ * \retval NULL if not found
+ * \retval non-NULL if found
+ */
+struct ast_sip_contact *ast_sip_location_retrieve_contact(const char *contact_name);
+
+/*!
+ * \brief Add a new contact to an AOR
+ *
+ * \param aor Pointer to the AOR
+ * \param uri Full contact URI
+ * \param expiration_time Optional expiration time of the contact
  *
  * \retval -1 failure
  * \retval 0 success
  */
-int ast_sip_location_update(const char *name, const char *uri, struct timeval expiration);
+int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri, struct timeval expiration_time);
+
+/*!
+ * \brief Update a contact
+ *
+ * \param contact New contact object with details
+ *
+ * \retval -1 failure
+ * \retval 0 success
+ */
+int ast_sip_location_update_contact(struct ast_sip_contact *contact);
+
+/*!
+* \brief Delete a contact
+*
+* \param contact Contact object to delete
+*
+* \retval -1 failure
+* \retval 0 success
+*/
+int ast_sip_location_delete_contact(struct ast_sip_contact *contact);
 
 /*!
  * \brief Create a new SIP work structure

Modified: team/file/pimp_sip_location/res/res_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/file/pimp_sip_location/res/res_sip.c?view=diff&rev=381399&r1=381398&r2=381399
==============================================================================
--- team/file/pimp_sip_location/res/res_sip.c (original)
+++ team/file/pimp_sip_location/res/res_sip.c Thu Feb 14 09:06:27 2013
@@ -300,7 +300,7 @@
 	pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
 	const pj_str_t HCONTACT = { "Contact", 7 };
 
-	if (!ast_strlen_zero(location_name) && (location = ast_sip_location_retrieve(location_name))) {
+	if (!ast_strlen_zero(location_name) && (location = NULL)) {
 		pj_cstr(&remote_uri, location->uri);
 		if (!ast_strlen_zero(location->transport)) {
 			transport_name = location->transport;

Modified: team/file/pimp_sip_location/res/res_sip/location.c
URL: http://svnview.digium.com/svn/asterisk/team/file/pimp_sip_location/res/res_sip/location.c?view=diff&rev=381399&r1=381398&r2=381399
==============================================================================
--- team/file/pimp_sip_location/res/res_sip/location.c (original)
+++ team/file/pimp_sip_location/res/res_sip/location.c Thu Feb 14 09:06:27 2013
@@ -27,135 +27,105 @@
 #include "asterisk/astobj2.h"
 #include "asterisk/sorcery.h"
 
+/*! \brief Allocator for AOR */
+static void *aor_alloc(const char *name)
+{
+	struct ast_sip_aor *aor = ao2_alloc(sizeof(*aor), NULL);
 
-/*! \brief Destructor for location */
- static void location_destroy(void *obj)
- {
- 	struct ast_sip_location *location = obj;
-
- 	ast_string_field_free_memory(location);
- }
-
-/*! \brief Allocator for location */
-static void *location_alloc(const char *name)
-{
-	struct ast_sip_location *location = ao2_alloc(sizeof(*location), location_destroy);
-
-	if (!location) {
+	if (!aor) {
 		return NULL;
 	}
 
-	if (ast_string_field_init(location, 256)) {
-		ao2_cleanup(location);
+	return aor;
+}
+
+/*! \brief Destructor for contact */
+static void contact_destroy(void *obj)
+{
+	struct ast_sip_contact *contact = obj;
+
+	ast_string_field_free_memory(contact);
+}
+
+/*! \brief Allocator for contact */
+static void *contact_alloc(const char *name)
+{
+	struct ast_sip_contact *contact = ao2_alloc(sizeof(*contact), contact_destroy);
+
+	if (!contact) {
 		return NULL;
 	}
 
-	return location;
+	if (ast_string_field_init(contact, 256)) {
+		ao2_cleanup(contact);
+		return NULL;
+	}
+
+	return contact;
 }
 
-int ast_sip_location_update(const char *name, const char *uri, struct timeval expiration)
+struct ast_sip_aor *ast_sip_location_retrieve_aor(const char *aor_name)
 {
-	RAII_VAR(struct ast_sip_location *, existing, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "location", name), ao2_cleanup);
-	RAII_VAR(struct ast_sip_location *, updated, NULL, ao2_cleanup);
-
-	/* If the location can not be updated with a new URI... HALT! CEASE! DESIST! */
-	if (!existing->update || !(updated = ast_sorcery_copy(ast_sip_get_sorcery(), existing))) {
-		return -1;
-	}
-
-	ast_string_field_set(updated, uri, uri);
-	updated->expiration = expiration;
-
-	return ast_sorcery_update(ast_sip_get_sorcery(), updated);
+	return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "aor", aor_name);
 }
 
-struct ast_sip_location *ast_sip_location_retrieve(const char *name)
+struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_sip_aor *aor)
 {
-	struct ast_sip_location *location = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "location_dynamic", name);
-
-	/* If this location has expired update it and return nothing */
-	if (location && !ast_tvzero(location->expiration) && (ast_tvdiff_ms(location->expiration, ast_tvnow()) <= 0)) {
-		ast_sip_location_update(name, NULL, ast_tv(0, 0));
-		ao2_ref(location, -1);
-		return NULL;
-	} else if (!location) {
-		/* If no dynamic objects could be found fall back to static */
-		location = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "location_static", name);
-	}
-
-	return location;
+	return NULL;
 }
 
-/*! \brief Callback for when a static location is applied */
-static void location_apply(const struct ast_sorcery *sorcery, void *obj)
+struct ast_sip_contact *ast_sip_location_retrieve_contact(const char *contact_name)
 {
-	struct ast_sip_location *location_static = obj;
-	RAII_VAR(struct ast_sip_location *, existing, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "location_dynamic", ast_sorcery_object_get_id(obj)), ao2_cleanup);
-	RAII_VAR(struct ast_sip_location *, copy, NULL, ao2_cleanup);
-	RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
+	return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "contact", contact_name);
+}
 
-	if (!existing) {
-		if (location_static->update) {
-			/* If this location can be updated we need to create a dynamic version, or try to at least */
-			if (!(existing = ast_sorcery_alloc(sorcery, "location_dynamic", ast_sorcery_object_get_id(obj))) ||
-				ast_sorcery_create(sorcery, existing)) {
-				return;
-			}
-		} else {
-			/* Static and dynamic sides are both in-sync */
-			return;
-		}
-	} else if (!location_static->update) {
-		/* Okay, we have an existing dynamic location but it should only be static so therefore delete it */
-		ast_sorcery_delete(sorcery, existing);
-		return;
-	}
+int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri, struct timeval expiration_time)
+{
+	return -1;
+}
 
-	/* So! Create a copy of the existing object and apply all relevant parts from the static object to it */
-	if (!(copy = ast_sorcery_copy(sorcery, existing)) ||
-		!(objset = ast_sorcery_objectset_create(sorcery, location_static)) ||
-		ast_sorcery_objectset_apply(sorcery, copy, objset)) {
-		return;
-	}
+int ast_sip_location_update_contact(struct ast_sip_contact *contact)
+{
+	return ast_sorcery_update(ast_sip_get_sorcery(), contact);
+}
 
-	/* Once the state of the copy is that of the static version set the few bits from the dynamic part we care about */
-	ast_string_field_set(copy, uri, existing->uri);
-	copy->expiration = existing->expiration;
+int ast_sip_location_delete_contact(struct ast_sip_contact *contact)
+{
+	return ast_sorcery_delete(ast_sip_get_sorcery(), contact);
+}
 
-	/* Because we know that both location_dynamic and location_static are really the same object we can change the object type without harm */
-	ast_copy_string(copy->details.type, "location_dynamic", sizeof(copy->details.type));
+/*! \brief Custom handler for translating from a string timeval to actual structure */
+static int expiration_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
+{
+	struct ast_sip_contact *contact = obj;
+	return ast_get_timeval(var->value, &contact->expiration_time, ast_tv(0, 0), NULL);
+}
 
-	/* Now once we update the information in location_dynamic it should mirror the static aspect */
-	ast_sorcery_update(sorcery, copy);
+/*! \brief Custom handler for translating from an actual structure timeval to string */
+static int expiration_struct2str(const void *obj, const intptr_t *args, char **buf)
+{
+	const struct ast_sip_contact *contact = obj;
+	return (ast_asprintf(buf, "%lu", contact->expiration_time.tv_sec) < 0) ? -1 : 0;
 }
 
 /*! \brief Initialize sorcery with location support */
 int ast_sip_initialize_sorcery_location(struct ast_sorcery *sorcery)
 {
-	/* Location is actually a hybrid object potentially existing across multiple wizards so we address
-	 * the static and dynamic versions individually so we can ensure they remain in sync
-	 */
-	ast_sorcery_apply_default(sorcery, "location_dynamic", "memory", NULL);
-	ast_sorcery_apply_default(sorcery, "location_static", "config", "res_sip.conf,criteria=type=location");
+	ast_sorcery_apply_default(sorcery, "contact", "memory", NULL);
+	ast_sorcery_apply_default(sorcery, "aor", "config", "res_sip.conf,criteria=type=aor");
 
-	if (ast_sorcery_object_register(sorcery, "location_static", location_alloc, NULL, location_apply) ||
-		ast_sorcery_object_register(sorcery, "location_dynamic", location_alloc, NULL, NULL)) {
+	if (ast_sorcery_object_register(sorcery, "contact", contact_alloc, NULL, NULL) ||
+		ast_sorcery_object_register(sorcery, "aor", aor_alloc, NULL, NULL)) {
 		return -1;
 	}
 
-	ast_sorcery_object_field_register(sorcery, "location_static", "type", "", OPT_NOOP_T, 0, 0);
-	ast_sorcery_object_field_register(sorcery, "location_static", "uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, uri));
-	ast_sorcery_object_field_register(sorcery, "location_static", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, transport));
-	ast_sorcery_object_field_register(sorcery, "location_static", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, outbound_proxy));
-	ast_sorcery_object_field_register(sorcery, "location_static", "change_user", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_location, change_user));
-	ast_sorcery_object_field_register(sorcery, "location_static", "update", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_location, update));
+	ast_sorcery_object_field_register(sorcery, "contact", "type", "", OPT_NOOP_T, 0, 0);
+	ast_sorcery_object_field_register(sorcery, "contact", "uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, uri));
+	ast_sorcery_object_field_register_custom(sorcery, "contact", "expiration_time", "", expiration_str2struct, expiration_struct2str, 0, 0);
 
-	ast_sorcery_object_field_register(sorcery, "location_dynamic", "type", "", OPT_NOOP_T, 0, 0);
-	ast_sorcery_object_field_register(sorcery, "location_dynamic", "uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, uri));
-	ast_sorcery_object_field_register(sorcery, "location_dynamic", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, transport));
-	ast_sorcery_object_field_register(sorcery, "location_dynamic", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, outbound_proxy));
-	ast_sorcery_object_field_register(sorcery, "location_dynamic", "change_user", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_location, change_user));
-	ast_sorcery_object_field_register(sorcery, "location_dynamic", "update", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_location, update));
+	ast_sorcery_object_field_register(sorcery, "aor", "type", "", OPT_NOOP_T, 0, 0);
+	ast_sorcery_object_field_register(sorcery, "aor", "default_expiration", "3600", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, default_expiration));
+	ast_sorcery_object_field_register(sorcery, "aor", "max_contacts", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, max_contacts));
 
 	return 0;
 }




More information about the svn-commits mailing list