Table of Contents
db_url
(str)
presentity_table
(str)
active_watchers_table
(str)
watchers_table
(str)
clean_period
(int)
db_update_period
(int)
waitn_time
(int)
notifier_poll_rate
(int)
notifier_processes
(int)
to_tag_pref
(str)
expires_offset
(int)
max_expires
(int)
server_address
(str)
subs_db_mode
(int)
publ_cache
(int)
subs_htable_size
(int)
pres_htable_size
(int)
send_fast_notify
(int)
enable_sphere_check
(int)
timeout_rm_subs
(int)
fetch_rows
(integer)
bind_presence(presence_api_t* api)
add_event
get_rules_doc
get_auth_status
apply_auth_nbody
agg_nbody
free_body
aux_body_processing
aux_free_body
evs_publ_handl
evs_subs_handl
contains_event
get_event_list
update_watchers_status
get_sphere
get_presentity
free_presentity
List of Examples
db_url
parameterpresentity_table
parameteractive_watchers_table
parameterwatchers_table
parameterclean_period
parameterdb_update_period
parameterwaitn_time
parameternotifier_poll_rate
parameternotifier_processes
parameterto_tag_pref
parameterexpires_offset
parametermax_expires
parameterserver_address
parametersubs_db_mode
parameterpubl_cache
parametersubs_htable_size
parameterpres_htable_size
parametersend_fast_notify
parameterenable_sphere_check
parametertimeout_rm_subs
parameterfetch_rows
parameterhandle_publish
usagehandle_subscribe
usagepres_auth_status
usagepres_refresh_watchers
usagepres_update_watchers
usagepresence_api_t
structureTable of Contents
db_url
(str)
presentity_table
(str)
active_watchers_table
(str)
watchers_table
(str)
clean_period
(int)
db_update_period
(int)
waitn_time
(int)
notifier_poll_rate
(int)
notifier_processes
(int)
to_tag_pref
(str)
expires_offset
(int)
max_expires
(int)
server_address
(str)
subs_db_mode
(int)
publ_cache
(int)
subs_htable_size
(int)
pres_htable_size
(int)
send_fast_notify
(int)
enable_sphere_check
(int)
timeout_rm_subs
(int)
fetch_rows
(integer)
The Presence module implements the core functionality of SIP event notification. It handles PUBLISH and SUBSCRIBE messages and generates NOTIFY messages in a general, event independent way. It is extensible and allows registering events to it from other Kamailio modules. Supported SIP event packages are presence, presence.winfo, dialog;sla from the presence_xml module and message-summary from the presence_mwi module.
The module can use database and memory storage (to improve performance). For subscriptions it supports the 4 storage modes: Memory Only, Write Back, Write Through and DB Only. For publishes, it stores the state documents in database only(because of the large size) and it can store a publish cache in memory to avoid unnecessairy database queries. Read the subs_db_mode and publ_cache parameter sections to decide which is the best storage configuration for you.
The module implements several API functions, that can be used by other modules. In fact, it can be used only as a resource module, or "library". This mode of operation is enabled if the db_url parameter is not set to any value.
The Kamailio Presence module implements the specifications in: RFC3265, RFC3856, RFC3857, RFC3858.
The following modules must be loaded before this module:
a database module.
sl.
tm.
The database url.
If set, the module is a fully operational presence server. Otherwise, it is used as a 'library', for its exported functions.
Default value is “NULL”.
Example 1.1. Set db_url
parameter
... modparam("presence", "db_url", "mysql://kamailio:kamailiorw@localhost/kamailio") ...
The name of the db table where PUBLISH presence information is stored.
Default value is “presentity”.
Example 1.2. Set presentity_table
parameter
... modparam("presence", "presentity_table", "presentity") ...
The name of the db table where active subscription information is stored.
Default value is “active_watchers”.
Example 1.3. Set active_watchers_table
parameter
... modparam("presence", "active_watchers_table", "active_watchers") ...
The name of the db table where subscription states are stored.
Default value is “watchers”.
Example 1.4. Set watchers_table
parameter
... modparam("presence", "watchers_table", "watchers") ...
The period in seconds between checks if there are expired messages stored in database.
Default value is “100”. A zero or negative value disables this activity.
The period at which to synchronize cached subscriber info with the database.
Default value is “100”. A zero or negative value disables synchronization.
The maximum time period that NOTIFY requests will be buffered for. The server will attempt to send NOTIFY requests within many seconds of a change occurring.
Note: this parameter is only used when notifier_processes is greater than 0. When notifier_processes is less than or equal to 0 NOTIFY requests are sent immediately.
Default value is “5”.
The number of times per second that the notifier processes should check for work. Approximately 1/(waitn_time * notifier_poll_rate * notifier_processes) of the pending updates will be sent each time a notifier process runs.
Separate notifier processes are only run when subs_db_mode is 3 (DB only mode).
Default value is “10”.
Example 1.8. Set notifier_poll_rate
parameter
... modparam("presence", "notifier_poll_rate", 20) ...
The number of notifier processes that should be started.
Separate notifier processes are only run when subs_db_mode is 3 (DB only mode).
Note: setting this parameter to 0 when subs_db_mode is 3 keeps the old behaviour (sending NOTIFY requests immediately). This (old) behaviour is disabled by default in DB only mode because under load, when lots of NOTIFY requests can be sent on a dialog at the same time, there are race conditions which result in CSeq re-use.
Default value is “1”.
The prefix used when generating to_tag when sending replies for SUBSCRIBE requests.
Default value is “10”.
The value in seconds that should be subtracted from the expires value when sending a 200OK for a publish. It is used for forcing the client to send an update before the old publish expires.
Default value is “0”.
The the maximum admissible expires value for PUBLISH/SUBSCRIBE message (in seconds).
Default value is “3600”.
The presence server address which will become the value of Contact header filed for 200 OK replies to SUBSCRIBE and PUBLISH and in NOTIFY messages.
Example 1.13. Set server_address
parameter
... modparam("presence", "server_address", "sip:10.10.10.10:5060") ...
The presence module can utilize database for persistent subscription storage. If you use database, your subscriptions will survive machine restarts or SW crashes. The disadvantage is that accessing database can be time consuming. Therefore, presence module implements four database accessing modes:
0 - This disables database completely. Only memory will be used. Subscriptions will not survive restart. Use this value if you need a really fast presence module and subscription persistence is not necessary or is provided by other means.
1 - Write-Through scheme. Subscriptions are updated synchronously in database and in memory(used for read operations). Use this scheme if speed is not top priority, but it's important that no subscriptions will be lost during crash or reboot or if you have an external application that reads the state of the subscriptions from database and they need to be updated synchronously.
2 - Write-Back scheme. This is a combination of previous two schemes. All changes are made to memory and database synchronization is done in the timer. The timer deletes all expired contacts and flushes all modified or new subscriptions to database. Use this scheme if you encounter high-load peaks and want them to process as fast as possible. Latency of this mode is much lower than latency of mode 1, but slightly higher than latency of mode 0. To control the interval at which data is flushed to database, set the db_update_period parameter.
3 - DB-Only scheme. No memory cache is kept, all operations being directly performed with the database. The timer deletes all expired subscriptions from database. The mode is useful if you configure more servers sharing the same DB without any replication at SIP level. The mode may be slower due the high number of DB operation.
Default value is 2 (Write-Back scheme).
To improve performance, the presence module holds by default a publish cache that says if a certain publication exists in database. This is only a list of URI + event, so it does not use much memory. The cache is used when a Subscription is received to check if there is any published state in database. This way unnecessary queries in presentity table are avoided.
Setting this parameter to 0 will disable the usage of the publish cache. This is desirable when you have more servers sharing the same database or there are other external entities inserting data into the presentity table.
Default value is “1”.
The size of the in-memory hash table to store subscription dialogs. This parameter will be used as the power of 2 when computing table size.
Default value is “9 (512)”.
The size of the in-memory hash table to store publish records. This parameter will be used as the power of 2 when computing table size.
Default value is “9 (512)”.
This parameter enables or disables the sending of an initial empty NOTIFY after a SUBSCRIBE/reSUBSCRIBE. This caused problems for MWI application, because some CPEs (like Samsung) fail to understand an empty NOTIFY to an message-summary event. This parameter is enabled by default, thus addering to the standard.
Default value is “1 ”.
This parameter is a flag that should be set if permission rules include sphere checking. The sphere information is expected to be present in the RPID body published by the presentity. The flag is introduced as this check requires extra processing that should be avoided if this feature is not supported by the clients.
Default value is “0 ”.
Example 1.19. Set enable_sphere_check
parameter
... modparam("presence", "enable_sphere_check", 1) ...
This parameter is a flag that should be set if subscriptions should be removed from the active_watchers when a NOTIFY times out. RFC3265 section 3.2.2 defines this behaviour as a SHOULD, so by default it is on. Disabling this will keep subscriptions active on unreliable networks.
Default value is “1”.
Handles PUBLISH requests by storing and updating published information in memory cache and database, then calls functions to send NOTIFY messages when changes in the published information occur. It takes one argument -> sender_uri. The parameter was added for enabling BLA implementation. If present, notification of a change in published state is not sent to the respective uri even though a subscription exists. It should be taken from the Sender header. It was left at the decision of the administrator whether or not to transmit the content of this header as parameter for handle_publish, to prevent security problems.
This function can be used from REQUEST_ROUTE.
Return code:
1 - if success.
-1 - if error.
The module sends an appropriate stateless reply in all cases.
Example 1.22. handle_publish
usage
... if(is_method("PUBLISH")) { if($hdr(Sender)!= NULL) handle_publish("$hdr(Sender)"); else handle_publish(); t_release(); } ...
The function which handles SUBSCRIBE requests. It stores or updates information in memory and database and calls functions to send NOTIFY messages when a SUBSCRIBE which initiate a dialog is received.
By default this function uses the From: URI from the SUBSCRIBE request as the Watcher URI. The optional watcher_uri parameter can be used to specify a different Watcher URI, possibly taken from a SIP header like P-Asserted-Identity:.
This function can be used from REQUEST_ROUTE.
Return code:
1 - if success.
-1 - if error.
The module sends an appropriate stateless reply in all cases.
The function checks if watcher is authorized to subscribe event 'presence' of presentity. Both watcher_uri and presentity_uri are pseudo variables. Function returns ACTIVE_STATUS, if subscription is allowed and PENDING_STATUS, TERMINATED_STATUS, or WAITING_STATUS otherwise. See presence/subscribe.h for the corresponding integer codes. In case of error, function returns -1.
This function can be used from REQUEST_ROUTE.
Example 1.24. pres_auth_status
usage
... if (method=="MESSAGE") { pres_auth_status("$fu", $ru"); if ($retcode == 1) { t_relay(); } else { send_reply("403", "Forbidden"); } } ...
The function can be used in configuration to triger notifies to watchers if a change in watchers authorization or in published state occurred (i.e., updates of xcap documents).
Parameters:
uri - the uri of the user who made the change and whose watchers should be informed.
event - the event package.
type - it distinguishes between the three different types of events that can trigger the refresh, depending on its value:
0 - a change in watchers authentication.
1 - a statical update in published state through direct update in db table.
2 - a statical update in published state by modifying the pidf manipulation document.
file_uri - the uri of the pidf-manipulation file on the XCAP server (only used for type 2).
filename - the name of the pidf-manipulation file on the XCAP server (only used for type 2).
This function can be used from ANY_ROUTE.
Example 1.25. pres_refresh_watchers
usage
... pres_refresh_watchers("sip:test@kamailio.org", "presence", 1); ...
The function can be used in configuration to triger updates to watchers status if a change in watchers authorization state occurred (i.e., updates of xcap documents change state from pending to active).
Parameters:
uri - the uri of the user who made the change and whose watchers should be informed. Can be PV.
event - the event package (e.g., presence).
This function can be used from ANY_ROUTE.
Example 1.26. pres_update_watchers
usage
... pres_update_watchers("sip:test@kamailio.org", "presence"); ...
Triggers sending Notify messages to watchers if a change in watchers authorization or in published state occurred.
Name: refreshWatchers
Parameters:
uri - the uri of the user who made the change and whose watchers should be informed
event - the event package.
type - it distinguishes between the three different types of events that can trigger the refresh, depending on its value:
0 - a change in watchers authentication.
1 - a statical update in published state through direct update in db table.
2 - a statical update in published state by modifying the pidf manipulation document.
file_uri - the uri of the pidf-manipulation file on the XCAP server (only used for type 2).
filename - the name of the pidf-manipulation file on the XCAP server (only used for type 2).
MI FIFO Command Format:
:refreshWatchers:fifo_reply sip:test@kamailio.org presence 1 _empty_line_
The module requires 3 tables in the Kamailio database: "presentity", "active_watchers" and "watchers". The SQL syntax to create them can be found in presence-create.sql script in the database directories in the kamailio/scripts folder. You can also find the complete database documentation on the project webpage, http://www.kamailio.org/docs/db-tables/kamailio-db-devel.html.
Table of Contents
bind_presence(presence_api_t* api)
add_event
get_rules_doc
get_auth_status
apply_auth_nbody
agg_nbody
free_body
aux_body_processing
aux_free_body
evs_publ_handl
evs_subs_handl
contains_event
get_event_list
update_watchers_status
get_sphere
get_presentity
free_presentity
The module provides the following functions that can be used in other Kamailio modules.
This function binds the presence modules and fills the structure with one exported function -> add_event, which when called adds a new event to be handled by presence.
Example 2.1. presence_api_t
structure
... typedef struct presence_api { add_event_t add_event; contains_event_t contains_event; search_event_t search_event; get_event_list_t get_event_list; update_watchers_t update_watchers_status; /* subs hash table handling functions */ new_shtable_t new_shtable; destroy_shtable_t destroy_shtable; insert_shtable_t insert_shtable; search_shtable_t search_shtable; delete_shtable_t delete_shtable; update_shtable_t update_shtable; /* function to duplicate a subs structure*/ mem_copy_subs_t mem_copy_subs; /* function used for update in database*/ update_db_subs_t update_db_subs_timer; /* function to extract dialog information from a SUBSCRIBE message */ extract_sdialog_info_t extract_sdialog_info; /* function to request sphere defition for a presentity */ pres_get_sphere_t get_sphere; }presence_api_t; ...
Field type:
... typedef int (*add_event_t)(pres_ev_t* event); ...
This function receives as a parameter a structure with event specific information and adds it to presence event list.
The structure received as a parameter:
... typedef struct pres_ev { str name; event_t* evp; str content_type; int default_expires; int type; int etag_not_new; /* * 0 - the standard mechanism (allocating new etag for each Publish) * 1 - allocating an etag only for an initial Publish */ int req_auth; get_rules_doc_t* get_rules_doc; apply_auth_t* apply_auth_nbody; is_allowed_t* get_auth_status; /* an agg_body_t function should be registered * if the event permits having multiple published * states and requires an aggregation of the information * otherwise, this field should be NULL and the last * published state is taken when constructing Notify msg */ agg_nbody_t* agg_nbody; publ_handling_t * evs_publ_handl; subs_handling_t * evs_subs_handl; free_body_t* free_body; /* sometimes it is necessary that a module make changes for a body for each * active watcher (e.g. setting the "version" parameter in an XML document. * If a module registers the aux_body_processing callback, it gets called for * each watcher. It either gets the body received by the PUBLISH, or the body * generated by the agg_nbody function. * The module can deceide if it makes a copy of the original body, which is then * manipulated, or if it works directly in the original body. If the module makes a * copy of the original body, it also has to register the aux_free_body() to * free this "per watcher" body. */ aux_body_processing_t* aux_body_processing; free_body_t* aux_free_body; struct pres_ev* wipeer; struct pres_ev* next; }pres_ev_t; ...
Filed type:
... typedef int (get_rules_doc_t)(str* user, str* domain, str** rules_doc); ...
This function returns the authorization rules document that will be used in obtaining the status of the subscription and processing the notified body. A reference to the document should be put in the auth_rules_doc of the subs_t structure given as a parameter to the functions described bellow.
This filed is a function to be called for a subscription request to return the state for that subscription according to authorization rules. In the auth_rules_doc field of the subs_t structure received as a parameter should contain the rules document of the presentity in case, if it exists.
It is called only if the req_auth field is not 0.
Filed type:
... typedef int (is_allowed_t)(struct subscription* subs); ...
This parameter should be a function to be called for an event that requires authorization, when constructing final body. The authorization document is taken from the auth_rules_doc field of the subs_t structure given as a parameter. It is called only if the req_auth field is not 0.
Filed type:
... typedef int (apply_auth_t)(str* , struct subscription*, str** ); ...
If present, this field marks that the events requires aggregation of states. This function receives a body array and should return the final body. If not present, it is considered that the event does not require aggregation and the most recent published information is used when constructing Notifies.
Filed type:
... typedef str* (agg_nbody_t)(str* pres_user, str* pres_domain, str** body_array, int n, int off_index); ..
This field must be field in if subsequent processing is performed on the info from database before being inserted in Notify message body(if agg_nbody or apply_auth_nbody fields are filled in). It should match the allocation function used when processing the body.
Filed type:
... typedef void(free_body_t)(char* body); ..
This field must be set if the module needs to manipulate the NOTIFY body for each watcher. E.g. if the XML body includes a 'version' parameter which will be increased for each NOTIFY, on a "per watcher" basis. The module can either allocate a new buffer for the new body an return it (aux_free_body function must be set too) or it manipualtes the original body directly and returns NULL.
Filed type:
... typedef str* (aux_body_processing_t)(struct subscription *subs, str* body); ..
This field must be set if the module registers the aux_body_processing function and allocates memory for the new modified body. Then, this function will be used to free the pointer returned by the aux_body_processing function. If the module does use the aux_body_processing, but does not allocate new memory, but manipulates directly the original body buffer, then the aux_body_processing must return NULL and this field should not be set.
Filed type:
... typedef void(free_body_t)(char* body); ..
This function is called when handling Publish requests. Most contain body correctness check.
... typedef int (publ_handling_t)(struct sip_msg*); ..
It is not compulsory. Should contain event specific handling for Subscription requests.
Filed type:
... typedef int (subs_handling_t)(struct sip_msg*); ..
Field type:
.. typedef pres_ev_t* (*contains_event_t)(str* name, event_t* parsed_event); ...
The function parses the event name received as a parameter and searches the result in the list. It returns the found event or NULL, if not found. If the second argument is an allocated event_t* structure it fills it with the result of the parsing.
Field type:
... typedef int (*get_event_list_t) (str** ev_list); ...
This function returns a string representation of the events registered in presence module.( used for Allowed-Events header).
Field type:
... typedef int (*update_watchers_t)(str pres_uri, pres_ev_t* ev, str* rules_doc); ...
This function is an external command that can be used to announce a change in authorization rules for a presentity. It updates the stored status and sends a Notify to the watchers whose status has changes. (used by presence_xml module when notified through an MI command of a change in an xcap document).
Field type:
... typedef char* (*pres_get_sphere_t)(str* pres_uri); ...
This function searches for a sphere definition in the published information if this has type RPID. If not found returns NULL. (the return value is allocated in private memory and should be freed)
Field type:
... typedef str* (*pres_get_presentity_t)(str pres_uri, pres_ev_t *ev, str *etag, str *contact); ...
This function returns a pointer to a str containing an XML document with all of the matching presentities. If no matching presentities are found the function returns NULL.
The etag and contact parameters are optional and may be set to NULL. Once you are finished with the presentity document you must call free_presentity to free the allocated memory.