Bladeren bron

modules/usrloc and registrar: added possibility to unregister without aor

- added possibility to unregister without aor if usrloc uses db_mode=3
- added new usrloc api function delete_urecord_by_ruid()
Juha Heinanen 12 jaren geleden
bovenliggende
commit
6c3853981a

+ 3 - 1
modules/registrar/README

@@ -869,7 +869,8 @@ add_sock_hdr("Sock-Info");
 
    The function removes contacts associated with 'uri'. If 'ruid' is
    provided a specific contact is removed, if 'ruid' is not provided all
-   contacts are removed.
+   contacts are removed. If 'ruid' is provided and usrloc is using
+   db_mode=3, 'uri' does not need to be given and can be empty string.
 
    Meaning of the parameters is as follows:
      * domain - Name of table that should be used for the lookup or
@@ -887,6 +888,7 @@ add_sock_hdr("Sock-Info");
 unregister("location", "$ru");
 unregister("location", "sip:[email protected]");
 unregister("location", "$ru", "$ulc(caller=>ruid)");
+unregister("location", "", "$ruid");
 ...
 
 4.7.  reg_fetch_contacts(domain, uri, profile)

+ 4 - 1
modules/registrar/doc/registrar_admin.xml

@@ -1109,7 +1109,9 @@ add_sock_hdr("Sock-Info");
 		<para>
 		The function removes contacts associated with 'uri'. If 'ruid' is
 		provided a specific contact is removed, if 'ruid' is not provided
-		all contacts are removed.
+		all contacts are removed.  If 'ruid' is provided and usrloc is
+		using db_mode=3, 'uri' does not need to be given and can be
+		empty string.
 		</para>
 		<para>Meaning of the parameters is as follows:</para>
 		<itemizedlist>
@@ -1144,6 +1146,7 @@ add_sock_hdr("Sock-Info");
 unregister("location", "$ru");
 unregister("location", "sip:[email protected]");
 unregister("location", "$ru", "$ulc(caller=>ruid)");
+unregister("location", "", "$ruid");
 ...
 </programlisting>
 		</example>

+ 3 - 3
modules/registrar/reg_mod.c

@@ -516,11 +516,11 @@ static int w_unregister(struct sip_msg* _m, char* _d, char* _uri)
 
 static int w_unregister2(struct sip_msg* _m, char* _d, char* _uri, char *_ruid)
 {
-	str uri = {0};
+        str uri = {0, 0};
 	str ruid = {0};
-	if(fixup_get_svalue(_m, (gparam_p)_uri, &uri)!=0 || uri.len<=0)
+	if(fixup_get_svalue(_m, (gparam_p)_uri, &uri)!=0)
 	{
-		LM_ERR("invalid uri parameter\n");
+	        LM_ERR("invalid uri parameter\n");
 		return -1;
 	}
 	if(fixup_get_svalue(_m, (gparam_p)_ruid, &ruid)!=0 || ruid.len<=0)

+ 38 - 14
modules/registrar/save.c

@@ -970,15 +970,16 @@ int unregister(struct sip_msg* _m, udomain_t* _d, str* _uri, str *_ruid)
 	sip_uri_t *u;
 	urecord_t *r;
 	ucontact_t *c;
-
-	if (extract_aor(_uri, &aor, NULL) < 0) {
-		LM_ERR("failed to extract Address Of Record\n");
-		return -1;
-	}
+	int res;
 
 	if (_ruid == NULL) {
 		/* No ruid provided - remove all contacts for aor */
 
+	        if (extract_aor(_uri, &aor, NULL) < 0) {
+		        LM_ERR("failed to extract Address Of Record\n");
+		        return -1;
+		}
+
 		u = parse_to_uri(_m);
 		if(u==NULL)
 			return -2;
@@ -991,16 +992,39 @@ int unregister(struct sip_msg* _m, udomain_t* _d, str* _uri, str *_ruid)
 	} else {
 		/* ruid provided - remove a specific contact */
 
-		if (ul.get_urecord_by_ruid(_d, ul.get_aorhash(&aor),
-				_ruid, &r, &c) != 0) {
-			LM_WARN("AOR/Contact not found\n");
-			return -1;
-		}
-		if (ul.delete_ucontact(r, c) != 0) {
-			LM_WARN("could not delete contact\n");
-			return -1;
+	        if (_uri->len > 0) {
+
+		        if (extract_aor(_uri, &aor, NULL) < 0) {
+		                LM_ERR("failed to extract Address Of Record\n");
+		                return -1;
+		        }
+
+		        if (ul.get_urecord_by_ruid(_d, ul.get_aorhash(&aor),
+						   _ruid, &r, &c) != 0) {
+			        LM_WARN("AOR/Contact not found\n");
+			        return -1;
+			}
+			if (ul.delete_ucontact(r, c) != 0) {
+			        LM_WARN("could not delete contact\n");
+			        return -1;
+			}
+			ul.unlock_udomain(_d, &aor);
+
+		} else {
+
+   		        res = ul.delete_urecord_by_ruid(_d, _ruid);
+			switch (res) {
+			case -1:
+			        LM_ERR("could not delete contact\n");
+			        return -1;
+			case -2:
+			        LM_WARN("contact not found\n");
+			        return -1;
+			default:
+			        return 1;
+			}
+
 		}
-		ul.unlock_udomain(_d, &aor);
 	}
 
 	return 1;

+ 61 - 48
modules/usrloc/README

@@ -102,22 +102,23 @@ Bogdan-Andrei Iancu
               1.1. ul_register_domain(name)
               1.2. ul_insert_urecord(domain, aor, rec)
               1.3. ul_delete_urecord(domain, aor)
-              1.4. ul_get_urecord(domain, aor)
-              1.5. ul_lock_udomain(domain)
-              1.6. ul_unlock_udomain(domain)
-              1.7. ul_release_urecord(record)
-              1.8. ul_insert_ucontact(record, contact, expires, q, callid,
+              1.4. ul_delete_urecord_by_ruid(domain, ruid)
+              1.5. ul_get_urecord(domain, aor)
+              1.6. ul_lock_udomain(domain)
+              1.7. ul_unlock_udomain(domain)
+              1.8. ul_release_urecord(record)
+              1.9. ul_insert_ucontact(record, contact, expires, q, callid,
                       cseq, flags, cont, ua, sock)
 
-              1.9. ul_delete_ucontact (record, contact)
-              1.10. ul_get_ucontact(record, contact)
-              1.11. ul_get_all_ucontacts (buf, len, flags)
-              1.12. ul_update_ucontact(contact, expires, q, callid, cseq,
+              1.10. ul_delete_ucontact (record, contact)
+              1.11. ul_get_ucontact(record, contact)
+              1.12. ul_get_all_ucontacts (buf, len, flags)
+              1.13. ul_update_ucontact(contact, expires, q, callid, cseq,
                       set, res, ua, sock)
 
-              1.13. ul_bind_ursloc( api )
-              1.14. ul_register_ulcb(type ,callback, param)
-              1.15. ul_get_num_users()
+              1.14. ul_bind_ursloc( api )
+              1.15. ul_register_ulcb(type ,callback, param)
+              1.16. ul_get_num_users()
 
    List of Examples
 
@@ -931,44 +932,46 @@ Chapter 2. Developer Guide
         1.1. ul_register_domain(name)
         1.2. ul_insert_urecord(domain, aor, rec)
         1.3. ul_delete_urecord(domain, aor)
-        1.4. ul_get_urecord(domain, aor)
-        1.5. ul_lock_udomain(domain)
-        1.6. ul_unlock_udomain(domain)
-        1.7. ul_release_urecord(record)
-        1.8. ul_insert_ucontact(record, contact, expires, q, callid, cseq,
+        1.4. ul_delete_urecord_by_ruid(domain, ruid)
+        1.5. ul_get_urecord(domain, aor)
+        1.6. ul_lock_udomain(domain)
+        1.7. ul_unlock_udomain(domain)
+        1.8. ul_release_urecord(record)
+        1.9. ul_insert_ucontact(record, contact, expires, q, callid, cseq,
                 flags, cont, ua, sock)
 
-        1.9. ul_delete_ucontact (record, contact)
-        1.10. ul_get_ucontact(record, contact)
-        1.11. ul_get_all_ucontacts (buf, len, flags)
-        1.12. ul_update_ucontact(contact, expires, q, callid, cseq, set,
+        1.10. ul_delete_ucontact (record, contact)
+        1.11. ul_get_ucontact(record, contact)
+        1.12. ul_get_all_ucontacts (buf, len, flags)
+        1.13. ul_update_ucontact(contact, expires, q, callid, cseq, set,
                 res, ua, sock)
 
-        1.13. ul_bind_ursloc( api )
-        1.14. ul_register_ulcb(type ,callback, param)
-        1.15. ul_get_num_users()
+        1.14. ul_bind_ursloc( api )
+        1.15. ul_register_ulcb(type ,callback, param)
+        1.16. ul_get_num_users()
 
 1. Available Functions
 
    1.1. ul_register_domain(name)
    1.2. ul_insert_urecord(domain, aor, rec)
    1.3. ul_delete_urecord(domain, aor)
-   1.4. ul_get_urecord(domain, aor)
-   1.5. ul_lock_udomain(domain)
-   1.6. ul_unlock_udomain(domain)
-   1.7. ul_release_urecord(record)
-   1.8. ul_insert_ucontact(record, contact, expires, q, callid, cseq,
+   1.4. ul_delete_urecord_by_ruid(domain, ruid)
+   1.5. ul_get_urecord(domain, aor)
+   1.6. ul_lock_udomain(domain)
+   1.7. ul_unlock_udomain(domain)
+   1.8. ul_release_urecord(record)
+   1.9. ul_insert_ucontact(record, contact, expires, q, callid, cseq,
           flags, cont, ua, sock)
 
-   1.9. ul_delete_ucontact (record, contact)
-   1.10. ul_get_ucontact(record, contact)
-   1.11. ul_get_all_ucontacts (buf, len, flags)
-   1.12. ul_update_ucontact(contact, expires, q, callid, cseq, set, res,
+   1.10. ul_delete_ucontact (record, contact)
+   1.11. ul_get_ucontact(record, contact)
+   1.12. ul_get_all_ucontacts (buf, len, flags)
+   1.13. ul_update_ucontact(contact, expires, q, callid, cseq, set, res,
           ua, sock)
 
-   1.13. ul_bind_ursloc( api )
-   1.14. ul_register_ulcb(type ,callback, param)
-   1.15. ul_get_num_users()
+   1.14. ul_bind_ursloc( api )
+   1.15. ul_register_ulcb(type ,callback, param)
+   1.16. ul_get_num_users()
 
 1.1.  ul_register_domain(name)
 
@@ -1012,7 +1015,17 @@ Chapter 2. Developer Guide
      * str* aor - Address of record (aka username) of the record, that
        should be deleted.
 
-1.4.  ul_get_urecord(domain, aor)
+1.4.  ul_delete_urecord_by_ruid(domain, ruid)
+
+   The function deletes from given domain a contact with given ruid.
+
+   Meaning of the parameters is as follows:
+     * udomain_t* domain - Pointer to domain returned by
+       ul_register_udomain.
+
+     * str* ruid - ruid of contact that should be deleted.
+
+1.5.  ul_get_urecord(domain, aor)
 
    The function returns pointer to record with given Address of Record.
 
@@ -1022,7 +1035,7 @@ Chapter 2. Developer Guide
 
      * str* aor - Address of Record of request record.
 
-1.5.  ul_lock_udomain(domain)
+1.6.  ul_lock_udomain(domain)
 
    The function lock the specified domain, it means, that no other
    processes will be able to access during the time. This prevents race
@@ -1033,14 +1046,14 @@ Chapter 2. Developer Guide
    Meaning of the parameters is as follows:
      * udomain_t* domain - Domain to be locked.
 
-1.6.  ul_unlock_udomain(domain)
+1.7.  ul_unlock_udomain(domain)
 
    Unlock the specified domain previously locked by ul_lock_udomain.
 
    Meaning of the parameters is as follows:
      * udomain_t* domain - Domain to be unlocked.
 
-1.7.  ul_release_urecord(record)
+1.8.  ul_release_urecord(record)
 
    Do some sanity checks - if all contacts have been removed, delete the
    entire record structure.
@@ -1048,7 +1061,7 @@ Chapter 2. Developer Guide
    Meaning of the parameters is as follows:
      * urecord_t* record - Record to be released.
 
-1.8.  ul_insert_ucontact(record, contact, expires, q, callid, cseq, flags,
+1.9.  ul_insert_ucontact(record, contact, expires, q, callid, cseq, flags,
 cont, ua, sock)
 
    The function inserts a new contact in the given record with specified
@@ -1069,7 +1082,7 @@ cont, ua, sock)
      * struct socket_info *sock - socket on which the REGISTER message was
        received on.
 
-1.9.  ul_delete_ucontact (record, contact)
+1.10.  ul_delete_ucontact (record, contact)
 
    The function deletes given contact from record.
 
@@ -1079,7 +1092,7 @@ cont, ua, sock)
 
      * ucontact_t* contact - Contact to be deleted.
 
-1.10.  ul_get_ucontact(record, contact)
+1.11.  ul_get_ucontact(record, contact)
 
    The function tries to find contact with given Contact URI and returns
    pointer to structure representing the contact.
@@ -1089,7 +1102,7 @@ cont, ua, sock)
 
      * str_t* contact - URI of the request contact.
 
-1.11.  ul_get_all_ucontacts (buf, len, flags)
+1.12.  ul_get_all_ucontacts (buf, len, flags)
 
    The function retrieves all contacts of all registered users and returns
    them in the caller-supplied buffer. If the buffer is too small, the
@@ -1110,7 +1123,7 @@ cont, ua, sock)
 
      * unsigned int flags - Flags that must be set.
 
-1.12.  ul_update_ucontact(contact, expires, q, callid, cseq, set, res, ua,
+1.13.  ul_update_ucontact(contact, expires, q, callid, cseq, set, res, ua,
 sock)
 
    The function updates contact with new values.
@@ -1129,7 +1142,7 @@ sock)
      * struct socket_info *sock - socket on which the REGISTER message was
        received on.
 
-1.13.  ul_bind_ursloc( api )
+1.14.  ul_bind_ursloc( api )
 
    The function imports all functions that are exported by the USRLOC
    module. Overs for other modules which want to user the internal USRLOC
@@ -1138,7 +1151,7 @@ sock)
    Meaning of the parameters is as follows:
      * usrloc_api_t* api - USRLOC API
 
-1.14.  ul_register_ulcb(type ,callback, param)
+1.15.  ul_register_ulcb(type ,callback, param)
 
    The function register with USRLOC a callback function to be called when
    some event occures inside USRLOC.
@@ -1151,6 +1164,6 @@ sock)
      * void *param - some parameter to be passed to the callback each time
        when it is called.
 
-1.15.  ul_get_num_users()
+1.16.  ul_get_num_users()
 
    The function loops through all domains summing up the number of users.

+ 25 - 0
modules/usrloc/doc/usrloc_devel.xml

@@ -98,6 +98,31 @@
 		</itemizedlist>
 	</section>
 
+	<section>
+		<title>
+		<function moreinfo="none">ul_delete_urecord_by_ruid(domain, ruid)</function>
+		</title>
+		<para>
+		The function deletes from given domain a contact with
+		given ruid.
+		</para>
+		<para>Meaning of the parameters is as follows:</para>
+		<itemizedlist>
+		<listitem>
+			<para><emphasis>udomain_t* domain</emphasis> - Pointer
+			to domain returned by ul_register_udomain.
+			</para>
+		</listitem>
+		</itemizedlist>
+		<itemizedlist>
+		<listitem>
+			<para><emphasis>str* ruid</emphasis> - ruid of contact
+			that should be deleted.
+			</para>
+		</listitem>
+		</itemizedlist>
+	</section>
+
 	<section>
 		<title>
 		<function moreinfo="none">ul_get_urecord(domain, aor)</function>

+ 44 - 0
modules/usrloc/urecord.c

@@ -437,6 +437,39 @@ int db_delete_urecord(urecord_t* _r)
 }
 
 
+/*!
+ * \brief Delete a record from the database based on ruid
+ * \return 0 on success, -1 on failure
+ */
+int db_delete_urecord_by_ruid(str *_table, str *_ruid)
+{
+	db_key_t keys[1];
+	db_val_t vals[1];
+
+	keys[0] = &ruid_col;
+	vals[0].type = DB1_STR;
+	vals[0].nul = 0;
+	vals[0].val.str_val.s = _ruid->s;
+	vals[0].val.str_val.len = _ruid->len;
+
+	if (ul_dbf.use_table(ul_dbh, _table) < 0) {
+		LM_ERR("use_table failed\n");
+		return -1;
+	}
+
+	if (ul_dbf.delete(ul_dbh, keys, 0, vals, 1) < 0) {
+		LM_ERR("failed to delete from database\n");
+		return -1;
+	}
+
+	if (ul_dbf.affected_rows(ul_dbh) == 0) {
+	        return -2;
+	}
+
+	return 0;
+}
+
+
 /*!
  * \brief Release urecord previously obtained through get_urecord
  * \warning Failing to calls this function after get_urecord will
@@ -517,6 +550,17 @@ int delete_ucontact(urecord_t* _r, struct ucontact* _c)
 }
 
 
+int delete_urecord_by_ruid(udomain_t* _d, str *_ruid)
+{
+    if (db_mode != DB_ONLY) {
+	LM_ERR("delete_urecord_by_ruid currently available only in db_mode=3\n");
+	return -1;
+    }
+
+    return db_delete_urecord_by_ruid(_d->name, _ruid);
+}
+
+
 /*!
  * \brief Match a contact record to a contact string
  * \param ptr contact record

+ 10 - 0
modules/usrloc/urecord.h

@@ -119,6 +119,16 @@ void timer_urecord(urecord_t* _r);
 int db_delete_urecord(urecord_t* _r);
 
 
+/*!
+ * \brief Delete a record from the database based on ruid
+ * \param _d pointer to domain from which record is deleted
+ * \param _ruid pointer to ruid of the record which is deleted
+ * \return 0 on success, -1 on failure, and -2 if record was
+ * not found
+ */
+int delete_urecord_by_ruid(udomain_t* _d, str *_ruid);
+
+
 /* ===== Module interface ======== */
 
 

+ 1 - 0
modules/usrloc/usrloc.c

@@ -64,6 +64,7 @@ int bind_usrloc(usrloc_api_t* api)
 	api->get_all_ucontacts  = get_all_ucontacts;
 	api->insert_urecord     = insert_urecord;
 	api->delete_urecord     = delete_urecord;
+	api->delete_urecord_by_ruid     = delete_urecord_by_ruid;
 	api->get_urecord        = get_urecord;
 	api->lock_udomain       = lock_udomain;
 	api->unlock_udomain     = unlock_udomain;

+ 3 - 0
modules/usrloc/usrloc.h

@@ -151,6 +151,8 @@ typedef int (*get_urecord_by_ruid_t)(udomain_t* _d, unsigned int _aorhash,
 
 typedef int  (*delete_urecord_t)(struct udomain* _d, str* _aor, struct urecord* _r);
 
+typedef int  (*delete_urecord_by_ruid_t)(struct udomain* _d, str* _ruid);
+
 typedef int (*update_ucontact_t)(struct urecord* _r, struct ucontact* _c,
 		struct ucontact_info* _ci);
 typedef void (*release_urecord_t)(struct urecord* _r);
@@ -199,6 +201,7 @@ typedef struct usrloc_api {
 
 	insert_urecord_t     insert_urecord;
 	delete_urecord_t     delete_urecord;
+	delete_urecord_by_ruid_t     delete_urecord_by_ruid;
 	get_urecord_t        get_urecord;
 	lock_udomain_t       lock_udomain;
 	unlock_udomain_t     unlock_udomain;