Merge "AST-2018-005: Fix tdata leaks when calling pjsip_endpt_send_response(2)"
[asterisk/asterisk.git] / apps / app_osplookup.c
index d8f437b..c42e187 100644 (file)
  *
  * \author Mark Spencer <markster@digium.com>
  *
+ * \extref The OSP Toolkit: http://www.transnexus.com
+ * \extref OpenSSL http://www.openssl.org
+ *
  * \ingroup applications
  */
 
 /*** MODULEINFO
        <depend>osptk</depend>
-       <depend>ssl</depend>
+       <depend>openssl</depend>
+       <support_level>extended</support_level>
  ***/
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
-#include <sys/types.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <errno.h>
 #include <osp/osp.h>
 #include <osp/osputils.h>
+#include <osp/ospb64.h>
 
+#include "asterisk/paths.h"
 #include "asterisk/lock.h"
 #include "asterisk/config.h"
 #include "asterisk/utils.h"
@@ -50,60 +49,459 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/app.h"
 #include "asterisk/module.h"
 #include "asterisk/pbx.h"
-#include "asterisk/options.h"
 #include "asterisk/cli.h"
-#include "asterisk/logger.h"
 #include "asterisk/astosp.h"
 
+/*** DOCUMENTATION
+       <application name="OSPAuth" language="en_US">
+               <synopsis>
+                       OSP Authentication.
+               </synopsis>
+               <syntax>
+                       <parameter name="provider">
+                               <para>The name of the provider that authenticates the call.</para>
+                       </parameter>
+                       <parameter name="options">
+                               <para>Reserverd.</para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Authenticate a call by OSP.</para>
+                       <para>Input variables:</para>
+                       <variablelist>
+                               <variable name="OSPINPEERIP">
+                                       <para>The last hop IP address.</para>
+                               </variable>
+                               <variable name="OSPINTOKEN">
+                                       <para>The inbound OSP token.</para>
+                               </variable>
+                       </variablelist>
+                       <para>Output variables:</para>
+                       <variablelist>
+                               <variable name="OSPINHANDLE">
+                                       <para>The inbound call OSP transaction handle.</para>
+                               </variable>
+                               <variable name="OSPINTIMELIMIT">
+                                       <para>The inbound call duration limit in seconds.</para>
+                               </variable>
+                       </variablelist>
+                       <para>This application sets the following channel variable upon completion:</para>
+                       <variablelist>
+                               <variable name="OSPAUTHSTATUS">
+                                       <para>The status of OSPAuth attempt as a text string, one of</para>
+                                       <value name="SUCCESS" />
+                                       <value name="FAILED" />
+                                       <value name="ERROR" />
+                               </variable>
+                       </variablelist>
+               </description>
+               <see-also>
+                       <ref type="application">OSPLookup</ref>
+                       <ref type="application">OSPNext</ref>
+                       <ref type="application">OSPFinish</ref>
+               </see-also>
+       </application>
+       <application name="OSPLookup" language="en_US">
+               <synopsis>
+                       Lookup destination by OSP.
+               </synopsis>
+               <syntax>
+                       <parameter name="exten" required="true">
+                               <para>The exten of the call.</para>
+                       </parameter>
+                       <parameter name="provider">
+                               <para>The name of the provider that is used to route the call.</para>
+                       </parameter>
+                       <parameter name="options">
+                               <enumlist>
+                                       <enum name="h">
+                                               <para>generate H323 call id for the outbound call</para>
+                                       </enum>
+                                       <enum name="s">
+                                               <para>generate SIP call id for the outbound call. Have not been implemented</para>
+                                       </enum>
+                                       <enum name="i">
+                                               <para>generate IAX call id for the outbound call. Have not been implemented</para>
+                                       </enum>
+                               </enumlist>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Looks up destination via OSP.</para>
+                       <para>Input variables:</para>
+                       <variablelist>
+                               <variable name="OSPINACTUALSRC">
+                                       <para>The actual source device IP address in indirect mode.</para>
+                               </variable>
+                               <variable name="OSPINPEERIP">
+                                       <para>The last hop IP address.</para>
+                               </variable>
+                               <variable name="OSPINTECH">
+                                       <para>The inbound channel technology for the call.</para>
+                               </variable>
+                               <variable name="OSPINHANDLE">
+                                       <para>The inbound call OSP transaction handle.</para>
+                               </variable>
+                               <variable name="OSPINTIMELIMIT">
+                                       <para>The inbound call duration limit in seconds.</para>
+                               </variable>
+                               <variable name="OSPINNETWORKID">
+                                       <para>The inbound source network ID.</para>
+                               </variable>
+                               <variable name="OSPINNPRN">
+                                       <para>The inbound routing number.</para>
+                               </variable>
+                               <variable name="OSPINNPCIC">
+                                       <para>The inbound carrier identification code.</para>
+                               </variable>
+                               <variable name="OSPINNPDI">
+                                       <para>The inbound number portability database dip indicator.</para>
+                               </variable>
+                               <variable name="OSPINSPID">
+                                       <para>The inbound service provider identity.</para>
+                               </variable>
+                               <variable name="OSPINOCN">
+                                       <para>The inbound operator company number.</para>
+                               </variable>
+                               <variable name="OSPINSPN">
+                                       <para>The inbound service provider name.</para>
+                               </variable>
+                               <variable name="OSPINALTSPN">
+                                       <para>The inbound alternate service provider name.</para>
+                               </variable>
+                               <variable name="OSPINMCC">
+                                       <para>The inbound mobile country code.</para>
+                               </variable>
+                               <variable name="OSPINMNC">
+                                       <para>The inbound mobile network code.</para>
+                               </variable>
+                               <variable name="OSPINTOHOST">
+                                       <para>The inbound To header host part.</para>
+                               </variable>
+                               <variable name="OSPINRPIDUSER">
+                                       <para>The inbound Remote-Party-ID header user part.</para>
+                               </variable>
+                               <variable name="OSPINPAIUSER">
+                                       <para>The inbound P-Asserted-Identify header user part.</para>
+                               </variable>
+                               <variable name="OSPINDIVUSER">
+                                       <para>The inbound Diversion header user part.</para>
+                               </variable>
+                               <variable name="OSPINDIVHOST">
+                                       <para>The inbound Diversion header host part.</para>
+                               </variable>
+                               <variable name="OSPINPCIUSER">
+                                       <para>The inbound P-Charge-Info header user part.</para>
+                               </variable>
+                               <variable name="OSPINCUSTOMINFOn">
+                                       <para>The inbound custom information, where <literal>n</literal> is the index beginning with <literal>1</literal>
+                                       upto <literal>8</literal>.</para>
+                               </variable>
+                       </variablelist>
+                       <para>Output variables:</para>
+                       <variablelist>
+                               <variable name="OSPOUTHANDLE">
+                                       <para>The outbound call OSP transaction handle.</para>
+                               </variable>
+                               <variable name="OSPOUTTECH">
+                                       <para>The outbound channel technology for the call.</para>
+                               </variable>
+                               <variable name="OSPDESTINATION">
+                                       <para>The outbound destination IP address.</para>
+                               </variable>
+                               <variable name="OSPOUTCALLING">
+                                       <para>The outbound calling number.</para>
+                               </variable>
+                               <variable name="OSPOUTCALLED">
+                                       <para>The outbound called number.</para>
+                               </variable>
+                               <variable name="OSPOUTNETWORKID">
+                                       <para>The outbound destination network ID.</para>
+                               </variable>
+                               <variable name="OSPOUTNPRN">
+                                       <para>The outbound routing number.</para>
+                               </variable>
+                               <variable name="OSPOUTNPCIC">
+                                       <para>The outbound carrier identification code.</para>
+                               </variable>
+                               <variable name="OSPOUTNPDI">
+                                       <para>The outbound number portability database dip indicator.</para>
+                               </variable>
+                               <variable name="OSPOUTSPID">
+                                       <para>The outbound service provider identity.</para>
+                               </variable>
+                               <variable name="OSPOUTOCN">
+                                       <para>The outbound operator company number.</para>
+                               </variable>
+                               <variable name="OSPOUTSPN">
+                                       <para>The outbound service provider name.</para>
+                               </variable>
+                               <variable name="OSPOUTALTSPN">
+                                       <para>The outbound alternate service provider name.</para>
+                               </variable>
+                               <variable name="OSPOUTMCC">
+                                       <para>The outbound mobile country code.</para>
+                               </variable>
+                               <variable name="OSPOUTMNC">
+                                       <para>The outbound mobile network code.</para>
+                               </variable>
+                               <variable name="OSPOUTTOKEN">
+                                       <para>The outbound OSP token.</para>
+                               </variable>
+                               <variable name="OSPDESTREMAILS">
+                                       <para>The number of remained destinations.</para>
+                               </variable>
+                               <variable name="OSPOUTTIMELIMIT">
+                                       <para>The outbound call duration limit in seconds.</para>
+                               </variable>
+                               <variable name="OSPOUTCALLIDTYPES">
+                                       <para>The outbound Call-ID types.</para>
+                               </variable>
+                               <variable name="OSPOUTCALLID">
+                                       <para>The outbound Call-ID. Only for H.323.</para>
+                               </variable>
+                               <variable name="OSPDIALSTR">
+                                       <para>The outbound Dial command string.</para>
+                               </variable>
+                       </variablelist>
+                       <para>This application sets the following channel variable upon completion:</para>
+                       <variablelist>
+                               <variable name="OSPLOOKUPSTATUS">
+                                       <para>The status of OSPLookup attempt as a text string, one of</para>
+                                       <value name="SUCCESS" />
+                                       <value name="FAILED" />
+                                       <value name="ERROR" />
+                               </variable>
+                       </variablelist>
+               </description>
+               <see-also>
+                       <ref type="application">OSPAuth</ref>
+                       <ref type="application">OSPNext</ref>
+                       <ref type="application">OSPFinish</ref>
+               </see-also>
+       </application>
+       <application name="OSPNext" language="en_US">
+               <synopsis>
+                       Lookup next destination by OSP.
+               </synopsis>
+               <description>
+                       <para>Looks up the next destination via OSP.</para>
+                       <para>Input variables:</para>
+                       <variablelist>
+                               <variable name="OSPINHANDLE">
+                                       <para>The inbound call OSP transaction handle.</para>
+                               </variable>
+                               <variable name="OSPOUTHANDLE">
+                                       <para>The outbound call OSP transaction handle.</para>
+                               </variable>
+                               <variable name="OSPINTIMELIMIT">
+                                       <para>The inbound call duration limit in seconds.</para>
+                               </variable>
+                               <variable name="OSPOUTCALLIDTYPES">
+                                       <para>The outbound Call-ID types.</para>
+                               </variable>
+                               <variable name="OSPDESTREMAILS">
+                                       <para>The number of remained destinations.</para>
+                               </variable>
+                       </variablelist>
+                       <para>Output variables:</para>
+                       <variablelist>
+                               <variable name="OSPOUTTECH">
+                                       <para>The outbound channel technology.</para>
+                               </variable>
+                               <variable name="OSPDESTINATION">
+                                       <para>The destination IP address.</para>
+                               </variable>
+                               <variable name="OSPOUTCALLING">
+                                       <para>The outbound calling number.</para>
+                               </variable>
+                               <variable name="OSPOUTCALLED">
+                                       <para>The outbound called number.</para>
+                               </variable>
+                               <variable name="OSPOUTNETWORKID">
+                                       <para>The outbound destination network ID.</para>
+                               </variable>
+                               <variable name="OSPOUTNPRN">
+                                       <para>The outbound routing number.</para>
+                               </variable>
+                               <variable name="OSPOUTNPCIC">
+                                       <para>The outbound carrier identification code.</para>
+                               </variable>
+                               <variable name="OSPOUTNPDI">
+                                       <para>The outbound number portability database dip indicator.</para>
+                               </variable>
+                               <variable name="OSPOUTSPID">
+                                       <para>The outbound service provider identity.</para>
+                               </variable>
+                               <variable name="OSPOUTOCN">
+                                       <para>The outbound operator company number.</para>
+                               </variable>
+                               <variable name="OSPOUTSPN">
+                                       <para>The outbound service provider name.</para>
+                               </variable>
+                               <variable name="OSPOUTALTSPN">
+                                       <para>The outbound alternate service provider name.</para>
+                               </variable>
+                               <variable name="OSPOUTMCC">
+                                       <para>The outbound mobile country code.</para>
+                               </variable>
+                               <variable name="OSPOUTMNC">
+                                       <para>The outbound mobile network code.</para>
+                               </variable>
+                               <variable name="OSPOUTTOKEN">
+                                       <para>The outbound OSP token.</para>
+                               </variable>
+                               <variable name="OSPDESTREMAILS">
+                                       <para>The number of remained destinations.</para>
+                               </variable>
+                               <variable name="OSPOUTTIMELIMIT">
+                                       <para>The outbound call duration limit in seconds.</para>
+                               </variable>
+                               <variable name="OSPOUTCALLID">
+                                       <para>The outbound Call-ID. Only for H.323.</para>
+                               </variable>
+                               <variable name="OSPDIALSTR">
+                                       <para>The outbound Dial command string.</para>
+                               </variable>
+                       </variablelist>
+                       <para>This application sets the following channel variable upon completion:</para>
+                       <variablelist>
+                               <variable name="OSPNEXTSTATUS">
+                                       <para>The status of the OSPNext attempt as a text string, one of</para>
+                                       <value name="SUCCESS" />
+                                       <value name="FAILED" />
+                                       <value name="ERROR" />
+                               </variable>
+                       </variablelist>
+               </description>
+               <see-also>
+                       <ref type="application">OSPAuth</ref>
+                       <ref type="application">OSPLookup</ref>
+                       <ref type="application">OSPFinish</ref>
+               </see-also>
+       </application>
+       <application name="OSPFinish" language="en_US">
+               <synopsis>
+                       Report OSP entry.
+               </synopsis>
+               <syntax>
+                       <parameter name="cause">
+                               <para>Hangup cause.</para>
+                       </parameter>
+                       <parameter name="options">
+                               <para>Reserved.</para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Report call state.</para>
+                       <para>Input variables:</para>
+                       <variablelist>
+                               <variable name="OSPINHANDLE">
+                                       <para>The inbound call OSP transaction handle.</para>
+                               </variable>
+                               <variable name="OSPOUTHANDLE">
+                                       <para>The outbound call OSP transaction handle.</para>
+                               </variable>
+                               <variable name="OSPAUTHSTATUS">
+                                       <para>The OSPAuth status.</para>
+                               </variable>
+                               <variable name="OSPLOOKUPSTATUS">
+                                       <para>The OSPLookup status.</para>
+                               </variable>
+                               <variable name="OSPNEXTSTATUS">
+                                       <para>The OSPNext status.</para>
+                               </variable>
+                               <variable name="OSPINAUDIOQOS">
+                                       <para>The inbound call leg audio QoS string.</para>
+                               </variable>
+                               <variable name="OSPOUTAUDIOQOS">
+                                       <para>The outbound call leg audio QoS string.</para>
+                               </variable>
+                       </variablelist>
+                       <para>This application sets the following channel variable upon completion:</para>
+                       <variablelist>
+                               <variable name="OSPFINISHSTATUS">
+                                       <para>The status of the OSPFinish attempt as a text string, one of</para>
+                                       <value name="SUCCESS" />
+                                       <value name="FAILED" />
+                                       <value name="ERROR" />
+                               </variable>
+                       </variablelist>
+               </description>
+               <see-also>
+                       <ref type="application">OSPAuth</ref>
+                       <ref type="application">OSPLookup</ref>
+                       <ref type="application">OSPNext</ref>
+               </see-also>
+       </application>
+ ***/
+
 /* OSP Buffer Sizes */
-#define OSP_INTSTR_SIZE                ((unsigned int)16)              /* OSP signed/unsigned int string buffer size */
-#define OSP_NORSTR_SIZE                ((unsigned int)256)             /* OSP normal string buffer size */
-#define OSP_TOKSTR_SIZE                ((unsigned int)4096)    /* OSP token string buffer size */
-#define OSP_TECHSTR_SIZE       ((unsigned int)32)              /* OSP signed/unsigned int string buffer size */
-#define OSP_UUID_SIZE          ((unsigned int)16)              /* UUID size */
-#define OSP_UUIDSTR_SIZE       ((unsigned int)36)              /* UUID string size */
+#define OSP_SIZE_INTSTR                ((unsigned int)16)                      /* OSP signed/unsigned int string buffer size */
+#define OSP_SIZE_NORSTR                ((unsigned int)256)                     /* OSP normal string buffer size */
+#define OSP_SIZE_KEYSTR                ((unsigned int)1024)            /* OSP certificate string buffer size */
+#define OSP_SIZE_TOKSTR                ((unsigned int)4096)            /* OSP token string buffer size */
+#define OSP_SIZE_TECHSTR       ((unsigned int)32)                      /* OSP signed/unsigned int string buffer size */
+#define OSP_SIZE_UUID          ((unsigned int)16)                      /* UUID size */
+#define OSP_SIZE_UUIDSTR       ((unsigned int)36)                      /* UUID string size */
+#define OSP_SIZE_QOSSTR                ((unsigned int)1024)            /* QoS string buffer size */
+
+/* Call ID Type*/
+#define OSP_CALLID_UNDEF       ((unsigned int)0)                       /* Undefined */
+#define OSP_CALLID_SIP         ((unsigned int)(1 << 0))        /* SIP */
+#define OSP_CALLID_H323                ((unsigned int)(1 << 1))        /* H.323 */
+#define OSP_CALLID_IAX         ((unsigned int)(1 << 2))        /* IAX2 */
+#define OSP_CALLID_MAXNUM      ((unsigned int)3)                       /* Max number of call ID types */
+
+/* OSP Supported Destination Protocols */
+#define OSP_PROT_SIP           ((const char*)"SIP")            /* SIP protocol name */
+#define OSP_PROT_H323          ((const char*)"H323")           /* H.323 Q.931 protocol name*/
+#define OSP_PROT_IAX           ((const char*)"IAX")            /* IAX2 protocol name */
+#define OSP_PROT_SKYPE         ((const char*)"SKYPE")          /* Skype protocol name */
+
+/* OSP supported Destination Tech */
+#define OSP_TECH_SIP           ((const char*)"SIP")            /* SIP tech name */
+#define OSP_TECH_H323          ((const char*)"H323")           /* OH323 tech name */
+#define OSP_TECH_IAX           ((const char*)"IAX2")           /* IAX2 tech name */
+#define OSP_TECH_SKYPE         ((const char*)"SKYPE")          /* Skype tech name */
+
+/* SIP OSP header field name */
+#define OSP_SIP_HEADER         ((const char*)"P-OSP-Auth-Token")
 
 /* OSP Authentication Policy */
 enum osp_authpolicy {
-       OSP_AUTH_NO,            /* Accept any call */
+       OSP_AUTH_NO = 0,        /* Accept any call */
        OSP_AUTH_YES,           /* Accept call with valid OSP token or without OSP token */
-       OSP_AUTH_EXCLUSIVE      /* Only accept call with valid OSP token */
+       OSP_AUTH_EXC            /* Only accept call with valid OSP token */
 };
 
-/* Call ID type*/
-#define OSP_CALLID_UNDEFINED   ((unsigned int)0)                       /* UNDEFINED */
-#define OSP_CALLID_H323                        ((unsigned int)(1 << 0))        /* H.323 */
-#define OSP_CALLID_SIP                 ((unsigned int)(1 << 1))        /* SIP */
-#define OSP_CALLID_IAX                 ((unsigned int)(1 << 2))        /* IAX2 */
-#define OSP_CALLID_MAXNUM              ((unsigned int)3)                       /* Max number of call ID type */
-
-/* OSP Supported Destination Protocols */
-#define OSP_PROT_H323                  ((char*)"H323")                         /* H323 Q931 protocol name*/
-#define OSP_PROT_SIP                   ((char*)"SIP")                          /* SIP protocol name */
-#define OSP_PROT_IAX                   ((char*)"IAX")                          /* IAX protocol name */
-#define OSP_PROT_OTHER                 ((char*)"OTHER")                        /* Other protocol name */
-
-/* OSP supported Destination Tech */
-#if 0
-#define OSP_TECH_H323                  ((char*)"OOH323")                       /* OOH323 tech name */
-#endif
-#define OSP_TECH_H323                  ((char*)"H323")                         /* OH323 tech name */
-#define OSP_TECH_SIP                   ((char*)"SIP")                          /* SIP tech name */
-#define OSP_TECH_IAX                   ((char*)"IAX2")                         /* IAX2 tech name */
+/* OSP Work Mode */
+enum osp_workmode {
+       OSP_MODE_DIRECT= 0,     /* Direct */
+       OSP_MODE_INDIRECT       /* Indirect */
+};
 
-/* SIP OSP header field name */
-#define OSP_SIP_HEADER                 ((char*)"P-OSP-Auth-Token: ")
+/* OSP Service Type */
+enum osp_srvtype {
+       OSP_SRV_VOICE = 0,      /* Normal voice service */
+       OSP_SRV_NPQUERY         /* Ported number query service */
+};
 
 /* OSP Constants */
+#define OSP_OK                                 ((int)1)                                        /* OSP function call successful */
+#define OSP_FAILED                             ((int)0)                                        /* OSP function call failed */
+#define OSP_ERROR                              ((int)-1)                                       /* OSP function call error */
+#define OSP_AST_OK                             ((int)0)                                        /* Asterisk function call successful */
+#define OSP_AST_ERROR                  ((int)-1)                                       /* Asterisk function call error */
 #define OSP_INVALID_HANDLE             ((int)-1)                                       /* Invalid OSP handle, provider, transaction etc. */
 #define OSP_CONFIG_FILE                        ((const char*)"osp.conf")       /* OSP configuration file name */
 #define OSP_GENERAL_CAT                        ((const char*)"general")        /* OSP global configuration context name */
 #define OSP_DEF_PROVIDER               ((const char*)"default")        /* OSP default provider context name */
 #define OSP_MAX_CERTS                  ((unsigned int)10)                      /* OSP max number of cacerts */
-#define OSP_MAX_SRVS                   ((unsigned int)10)                      /* OSP max number of service points */
-#define OSP_DEF_MAXCONNECTIONS ((unsigned int)20)                      /* OSP default max_connections */
-#define OSP_MIN_MAXCONNECTIONS ((unsigned int)1)                       /* OSP min max_connections */
-#define OSP_MAX_MAXCONNECTIONS ((unsigned int)1000)            /* OSP max max_connections */
+#define OSP_MAX_SPOINTS                        ((unsigned int)10)                      /* OSP max number of service points */
+#define OSP_DEF_MAXCONNECT             ((unsigned int)20)                      /* OSP default max_connections */
+#define OSP_MIN_MAXCONNECT             ((unsigned int)1)                       /* OSP min max_connections */
+#define OSP_MAX_MAXCONNECT             ((unsigned int)1000)            /* OSP max max_connections */
 #define OSP_DEF_RETRYDELAY             ((unsigned int)0)                       /* OSP default retry delay */
 #define OSP_MIN_RETRYDELAY             ((unsigned int)0)                       /* OSP min retry delay */
 #define OSP_MAX_RETRYDELAY             ((unsigned int)10)                      /* OSP max retry delay */
@@ -113,306 +511,390 @@ enum osp_authpolicy {
 #define OSP_DEF_TIMEOUT                        ((unsigned int)500)                     /* OSP default timeout in ms */
 #define OSP_MIN_TIMEOUT                        ((unsigned int)200)                     /* OSP min timeout in ms */
 #define OSP_MAX_TIMEOUT                        ((unsigned int)10000)           /* OSP max timeout in ms */
-#define OSP_DEF_AUTHPOLICY             ((enum osp_authpolicy)OSP_AUTH_YES)
+#define OSP_DEF_AUTHPOLICY             OSP_AUTH_YES                            /* OSP default auth policy, yes */
 #define OSP_AUDIT_URL                  ((const char*)"localhost")      /* OSP default Audit URL */
 #define OSP_LOCAL_VALIDATION   ((int)1)                                        /* Validate OSP token locally */
 #define OSP_SSL_LIFETIME               ((unsigned int)300)                     /* SSL life time, in seconds */
 #define OSP_HTTP_PERSISTENCE   ((int)1)                                        /* In seconds */
 #define OSP_CUSTOMER_ID                        ((const char*)"")                       /* OSP customer ID */
 #define OSP_DEVICE_ID                  ((const char*)"")                       /* OSP device ID */
-#define OSP_DEF_DESTINATIONS   ((unsigned int)5)                       /* OSP default max number of destinations */
+#define OSP_DEF_MAXDESTS               ((unsigned int)12)                      /* OSP default max number of destinations */
 #define OSP_DEF_TIMELIMIT              ((unsigned int)0)                       /* OSP default duration limit, no limit */
-#define OSP_DEF_PROTOCOL               OSP_PROT_SIP                            /* OSP default destination protocol, SIP */
+#define OSP_DEF_PROTOCOL               OSP_PROT_SIP                            /* OSP default signaling protocol, SIP */
+#define OSP_DEF_WORKMODE               OSP_MODE_DIRECT                         /* OSP default work mode, direct */
+#define OSP_DEF_SRVTYPE                        OSP_SRV_VOICE                           /* OSP default service type, voice */
+#define OSP_MAX_CUSTOMINFO             ((unsigned int)8)                       /* OSP max number of custom info */
+#define OSP_DEF_INTSTATS               ((int)-1)                                       /* OSP default int statistic */
+#define OSP_DEF_FLOATSTATS             ((float)-1)                                     /* OSP default float statistic */
 
 /* OSP Provider */
 struct osp_provider {
-       char name[OSP_NORSTR_SIZE];                                             /* OSP provider context name */
-       char privatekey[OSP_NORSTR_SIZE];                               /* OSP private key file name */
-       char localcert[OSP_NORSTR_SIZE];                                /* OSP local cert file name */
-       unsigned int cacount;                                                   /* Number of cacerts */
-       char cacerts[OSP_MAX_CERTS][OSP_NORSTR_SIZE];   /* Cacert file names */
-       unsigned int spcount;                                                   /* Number of service points */
-       char srvpoints[OSP_MAX_SRVS][OSP_NORSTR_SIZE];  /* Service point URLs */
-       int maxconnections;                                                             /* Max number of connections */
-       int retrydelay;                                                                 /* Retry delay */
-       int retrylimit;                                                                 /* Retry limit */
-       int timeout;                                                                    /* Timeout in ms */
-       char source[OSP_NORSTR_SIZE];                                   /* IP of self */
-       enum osp_authpolicy authpolicy;                                 /* OSP authentication policy */
-       char* defaultprotocol;                                                  /* OSP default destination protocol */
        OSPTPROVHANDLE handle;                                                  /* OSP provider handle */
+       char name[OSP_SIZE_NORSTR];                                             /* OSP provider context name */
+       char privatekey[OSP_SIZE_NORSTR];                               /* OSP private key file name */
+       char localcert[OSP_SIZE_NORSTR];                                /* OSP local cert file name */
+       unsigned int canum;                                                             /* Number of cacerts */
+       char cacerts[OSP_MAX_CERTS][OSP_SIZE_NORSTR];   /* Cacert file names */
+       unsigned int spnum;                                                             /* Number of service points */
+       char spoints[OSP_MAX_SPOINTS][OSP_SIZE_NORSTR]; /* Service point URLs */
+       unsigned int maxconnect;                                                /* Max number of connections */
+       unsigned int retrydelay;                                                /* Retry delay */
+       unsigned int retrylimit;                                                /* Retry limit */
+       unsigned int timeout;                                                   /* Timeout in ms */
+       char source[OSP_SIZE_NORSTR];                                   /* IP of self */
+       enum osp_authpolicy authpolicy;                                 /* OSP authentication policy */
+       const char* defprotocol;                                                /* OSP default signaling protocol */
+       enum osp_workmode workmode;                                             /* OSP work mode */
+       enum osp_srvtype srvtype;                                               /* OSP service type */
        struct osp_provider* next;                                              /* Pointer to next OSP provider */
 };
 
 /* Call ID */
 struct osp_callid {
-       char buf[OSPC_CALLID_MAXSIZE];          /* Call ID string */
-       unsigned int len;                                       /* Call ID length */
+       unsigned char buf[OSP_SIZE_NORSTR];             /* Call ID string */
+       unsigned int len;                                               /* Call ID length */
+};
+
+/* Number Portability Data */
+struct osp_npdata {
+       const char* rn;                                                 /* Rounding Number */
+       const char* cic;                                                /* Carrier Identification Code */
+       int npdi;                                                               /* NP Database Dip Indicator */
+       const char* opname[OSPC_OPNAME_NUMBER]; /* Operator Names */
+};
+
+/* SIP Header Parameters */
+struct osp_headers {
+       const char* rpiduser;                                   /* Remote-Party-ID header user info */
+       const char* paiuser;                                    /* P-Asserted-Identity header user info */
+       const char* divuser;                                    /* Diversion header user info */
+       const char* divhost;                                    /* Diversion header host info */
+       const char* pciuser;                                    /* P-Charge-Info header user info */
 };
 
 /* OSP Application In/Output Results */
-struct osp_result {
-       int inhandle;                                           /* Inbound transaction handle */
-       int outhandle;                                          /* Outbound transaction handle */
-       unsigned int intimelimit;                       /* Inbound duration limit */
-       unsigned int outtimelimit;                      /* Outbound duration limit */
-       char tech[OSP_TECHSTR_SIZE];            /* Outbound Asterisk TECH string */
-       char dest[OSP_NORSTR_SIZE];                     /* Outbound destination IP address */
-       char called[OSP_NORSTR_SIZE];           /* Outbound called number, may be translated */
-       char calling[OSP_NORSTR_SIZE];          /* Outbound calling number, may be translated */
-       char token[OSP_TOKSTR_SIZE];            /* Outbound OSP token */
-       char networkid[OSP_NORSTR_SIZE];        /* Outbound network ID */
-       unsigned int numresults;                        /* Number of remain outbound destinations */
-       struct osp_callid outcallid;            /* Outbound call ID */
+struct osp_results {
+       int inhandle;                                                                           /* Inbound transaction handle */
+       int outhandle;                                                                          /* Outbound transaction handle */
+       unsigned int intimelimit;                                                       /* Inbound duration limit */
+       unsigned int outtimelimit;                                                      /* Outbound duration limit */
+       char intech[OSP_SIZE_TECHSTR];                                          /* Inbound Asterisk TECH string */
+       char outtech[OSP_SIZE_TECHSTR];                                         /* Outbound Asterisk TECH string */
+       char dest[OSP_SIZE_NORSTR];                                                     /* Outbound destination IP address */
+       char calling[OSP_SIZE_NORSTR];                                          /* Outbound calling number, may be translated */
+       char called[OSP_SIZE_NORSTR];                                           /* Outbound called number, may be translated */
+       char token[OSP_SIZE_TOKSTR];                                            /* Outbound OSP token */
+       char networkid[OSP_SIZE_NORSTR];                                        /* Outbound network ID */
+       char nprn[OSP_SIZE_NORSTR];                                                     /* Outbound NP routing number */
+       char npcic[OSP_SIZE_NORSTR];                                            /* Outbound NP carrier identification code */
+       int npdi;                                                                                       /* Outbound NP database dip indicator */
+       char opname[OSPC_OPNAME_NUMBER][OSP_SIZE_NORSTR];       /* Outbound Operator names */
+       unsigned int numdests;                                                          /* Number of remain outbound destinations */
+       struct osp_callid outcallid;                                            /* Outbound call ID */
+};
+
+/* OSP Call Leg */
+enum osp_callleg {
+       OSP_CALL_INBOUND,       /* Inbound call leg */
+       OSP_CALL_OUTBOUND       /* Outbound call leg */
+};
+
+/* OSP Media Stream Direction */
+enum osp_direction {
+       OSP_DIR_RX = 0,         /* Receive */
+       OSP_DIR_TX,                     /* Send */
+       OSP_DIR_NUMBER          /* Number of directions */
+};
+
+/* OSP Metrics */
+struct osp_metrics {
+       int value;                      /* Value */
+       float min;                      /* Minimum */
+       float max;                      /* Maximum */
+       float avg;                      /* Average */
+       float sdev;                     /* Standard deviation */
 };
 
 /* OSP Module Global Variables */
-AST_MUTEX_DEFINE_STATIC(osplock);                                                      /* Lock of OSP provider list */
+AST_MUTEX_DEFINE_STATIC(osp_lock);                                                     /* Lock of OSP provider list */
 static int osp_initialized = 0;                                                                /* Init flag */
-static int osp_hardware = 0;                                                           /* Hardware accelleration flag */
-static struct osp_provider* ospproviders = NULL;                       /* OSP provider list */
+static int osp_hardware = 0;                                                           /* Hardware acceleration flag */
+static int osp_security = 0;                                                           /* Using security features flag */
+static struct osp_provider* osp_providers = NULL;                      /* OSP provider list */
 static unsigned int osp_tokenformat = TOKEN_ALGO_SIGNED;       /* Token format supported */
 
+/* OSP default certificates */
+const char* B64PKey = "MIIBOgIBAAJBAK8t5l+PUbTC4lvwlNxV5lpl+2dwSZGW46dowTe6y133XyVEwNiiRma2YNk3xKs/TJ3Wl9Wpns2SYEAJsFfSTukCAwEAAQJAPz13vCm2GmZ8Zyp74usTxLCqSJZNyMRLHQWBM0g44Iuy4wE3vpi7Wq+xYuSOH2mu4OddnxswCP4QhaXVQavTAQIhAOBVCKXtppEw9UaOBL4vW0Ed/6EA/1D8hDW6St0h7EXJAiEAx+iRmZKhJD6VT84dtX5ZYNVk3j3dAcIOovpzUj9a0CECIEduTCapmZQ5xqAEsLXuVlxRtQgLTUD4ZxDElPn8x0MhAiBE2HlcND0+qDbvtwJQQOUzDgqg5xk3w8capboVdzAlQQIhAMC+lDL7+gDYkNAft5Mu+NObJmQs4Cr+DkDFsKqoxqrm";
+const char* B64LCert = "MIIBeTCCASMCEHqkOHVRRWr+1COq3CR/xsowDQYJKoZIhvcNAQEEBQAwOzElMCMGA1UEAxMcb3NwdGVzdHNlcnZlci50cmFuc25leHVzLmNvbTESMBAGA1UEChMJT1NQU2VydmVyMB4XDTA1MDYyMzAwMjkxOFoXDTA2MDYyNDAwMjkxOFowRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQCvLeZfj1G0wuJb8JTcVeZaZftncEmRluOnaME3ustd918lRMDYokZmtmDZN8SrP0yd1pfVqZ7NkmBACbBX0k7pAgMBAAEwDQYJKoZIhvcNAQEEBQADQQDnV8QNFVVJx/+7IselU0wsepqMurivXZzuxOmTEmTVDzCJx1xhA8jd3vGAj7XDIYiPub1PV23eY5a2ARJuw5w9";
+const char* B64CACert = "MIIBYDCCAQoCAQEwDQYJKoZIhvcNAQEEBQAwOzElMCMGA1UEAxMcb3NwdGVzdHNlcnZlci50cmFuc25leHVzLmNvbTESMBAGA1UEChMJT1NQU2VydmVyMB4XDTAyMDIwNDE4MjU1MloXDTEyMDIwMzE4MjU1MlowOzElMCMGA1UEAxMcb3NwdGVzdHNlcnZlci50cmFuc25leHVzLmNvbTESMBAGA1UEChMJT1NQU2VydmVyMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAPGeGwV41EIhX0jEDFLRXQhDEr50OUQPq+f55VwQd0TQNts06BP29+UiNdRW3c3IRHdZcJdC1Cg68ME9cgeq0h8CAwEAATANBgkqhkiG9w0BAQQFAANBAGkzBSj1EnnmUxbaiG1N4xjIuLAWydun7o3bFk2tV8dBIhnuh445obYyk1EnQ27kI7eACCILBZqi2MHDOIMnoN0=";
+
 /* OSP Client Wrapper APIs */
 
 /*!
  * \brief Create OSP provider handle according to configuration
  * \param cfg OSP configuration
- * \param provider OSP provider context name
- * \return 1 Success, 0 Failed, -1 Error
+ * \param name OSP provider context name
+ * \return OSP_OK Success, OSP_FAILED Failed, OSP_ERROR Error
  */
 static int osp_create_provider(
        struct ast_config* cfg,
-       const char* provider)
+       const char* name)
 {
-       int res;
-       unsigned int t, i, j;
-       struct osp_provider* p;
-       struct ast_variable* v;
+       int res = OSP_FAILED;
+       struct ast_variable* var;
+       struct osp_provider* provider;
        OSPTPRIVATEKEY privatekey;
-       OSPTCERT localcert;
-       const char* psrvpoints[OSP_MAX_SRVS];
-       OSPTCERT cacerts[OSP_MAX_CERTS];
-       const OSPTCERT* pcacerts[OSP_MAX_CERTS];
-       int error = OSPC_ERR_NO_ERROR;
-
-       if (!(p = ast_calloc(1, sizeof(*p)))) {
+       OSPT_CERT localcert;
+       OSPT_CERT cacerts[OSP_MAX_CERTS];
+       const OSPT_CERT* pcacerts[OSP_MAX_CERTS];
+       const char* pspoints[OSP_MAX_SPOINTS];
+       unsigned char privatekeydata[OSP_SIZE_KEYSTR];
+       unsigned char localcertdata[OSP_SIZE_KEYSTR];
+       unsigned char cacertdata[OSP_SIZE_KEYSTR];
+       int i, num, error = OSPC_ERR_NO_ERROR;
+
+       if (!(provider = ast_calloc(1, sizeof(*provider)))) {
                ast_log(LOG_ERROR, "Out of memory\n");
-               return -1;
-       }
-
-       ast_copy_string(p->name, provider, sizeof(p->name));
-       snprintf(p->privatekey, sizeof(p->privatekey), "%s/%s-privatekey.pem", ast_config_AST_KEY_DIR, provider);
-       snprintf(p->localcert, sizeof(p->localcert), "%s/%s-localcert.pem", ast_config_AST_KEY_DIR, provider);
-       p->maxconnections = OSP_DEF_MAXCONNECTIONS;
-       p->retrydelay = OSP_DEF_RETRYDELAY;
-       p->retrylimit = OSP_DEF_RETRYLIMIT;
-       p->timeout = OSP_DEF_TIMEOUT;
-       p->authpolicy = OSP_DEF_AUTHPOLICY;
-       p->defaultprotocol = OSP_DEF_PROTOCOL;
-       p->handle = OSP_INVALID_HANDLE;
-
-       v = ast_variable_browse(cfg, provider);
-       while(v) {
-               if (!strcasecmp(v->name, "privatekey")) {
-                       if (v->value[0] == '/') {
-                               ast_copy_string(p->privatekey, v->value, sizeof(p->privatekey));
-                       } else {
-                               snprintf(p->privatekey, sizeof(p->privatekey), "%s/%s", ast_config_AST_KEY_DIR, v->value);
+               return OSP_ERROR;
+       }
+
+       /* ast_calloc has set 0 in provider */
+       provider->handle = OSP_INVALID_HANDLE;
+       ast_copy_string(provider->name, name, sizeof(provider->name));
+       snprintf(provider->privatekey, sizeof(provider->privatekey), "%s/%s-privatekey.pem", ast_config_AST_KEY_DIR, name);
+       snprintf(provider->localcert, sizeof(provider->localcert), "%s/%s-localcert.pem", ast_config_AST_KEY_DIR, name);
+       snprintf(provider->cacerts[0], sizeof(provider->cacerts[0]), "%s/%s-cacert_0.pem", ast_config_AST_KEY_DIR, name);
+       provider->maxconnect = OSP_DEF_MAXCONNECT;
+       provider->retrydelay = OSP_DEF_RETRYDELAY;
+       provider->retrylimit = OSP_DEF_RETRYLIMIT;
+       provider->timeout = OSP_DEF_TIMEOUT;
+       provider->authpolicy = OSP_DEF_AUTHPOLICY;
+       provider->defprotocol = OSP_DEF_PROTOCOL;
+       provider->workmode = OSP_DEF_WORKMODE;
+       provider->srvtype = OSP_DEF_SRVTYPE;
+
+       for (var = ast_variable_browse(cfg, name); var != NULL; var = var->next) {
+               if (!strcasecmp(var->name, "privatekey")) {
+                       if (osp_security) {
+                               if (var->value[0] == '/') {
+                                       ast_copy_string(provider->privatekey, var->value, sizeof(provider->privatekey));
+                               } else {
+                                       snprintf(provider->privatekey, sizeof(provider->privatekey), "%s/%s", ast_config_AST_KEY_DIR, var->value);
+                               }
+                               ast_debug(1, "OSP: privatekey '%s'\n", provider->privatekey);
                        }
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: privatekey '%s'\n", p->privatekey);
-               } else if (!strcasecmp(v->name, "localcert")) {
-                       if (v->value[0] == '/') {
-                               ast_copy_string(p->localcert, v->value, sizeof(p->localcert));
-                       } else {
-                               snprintf(p->localcert, sizeof(p->localcert), "%s/%s", ast_config_AST_KEY_DIR, v->value);
+               } else if (!strcasecmp(var->name, "localcert")) {
+                       if (osp_security) {
+                               if (var->value[0] == '/') {
+                                       ast_copy_string(provider->localcert, var->value, sizeof(provider->localcert));
+                               } else {
+                                       snprintf(provider->localcert, sizeof(provider->localcert), "%s/%s", ast_config_AST_KEY_DIR, var->value);
+                               }
+                               ast_debug(1, "OSP: localcert '%s'\n", provider->localcert);
                        }
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: localcert '%s'\n", p->localcert);
-               } else if (!strcasecmp(v->name, "cacert")) {
-                       if (p->cacount < OSP_MAX_CERTS) {
-                               if (v->value[0] == '/') {
-                                       ast_copy_string(p->cacerts[p->cacount], v->value, sizeof(p->cacerts[0]));
+               } else if (!strcasecmp(var->name, "cacert")) {
+                       if (osp_security) {
+                               if (provider->canum < OSP_MAX_CERTS) {
+                                       if (var->value[0] == '/') {
+                                               ast_copy_string(provider->cacerts[provider->canum], var->value, sizeof(provider->cacerts[provider->canum]));
+                                       } else {
+                                               snprintf(provider->cacerts[provider->canum], sizeof(provider->cacerts[provider->canum]), "%s/%s", ast_config_AST_KEY_DIR, var->value);
+                                       }
+                                       ast_debug(1, "OSP: cacerts[%d]: '%s'\n", provider->canum, provider->cacerts[provider->canum]);
+                                       provider->canum++;
                                } else {
-                                       snprintf(p->cacerts[p->cacount], sizeof(p->cacerts[0]), "%s/%s", ast_config_AST_KEY_DIR, v->value);
+                                       ast_log(LOG_WARNING, "OSP: Too many CA Certificates at line %d\n", var->lineno);
                                }
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: cacert[%d]: '%s'\n", p->cacount, p->cacerts[p->cacount]);
-                               p->cacount++;
-                       } else {
-                               ast_log(LOG_WARNING, "OSP: Too many CA Certificates at line %d\n", v->lineno);
                        }
-               } else if (!strcasecmp(v->name, "servicepoint")) {
-                       if (p->spcount < OSP_MAX_SRVS) {
-                               ast_copy_string(p->srvpoints[p->spcount], v->value, sizeof(p->srvpoints[0]));
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: servicepoint[%d]: '%s'\n", p->spcount, p->srvpoints[p->spcount]);
-                               p->spcount++;
+               } else if (!strcasecmp(var->name, "servicepoint")) {
+                       if (provider->spnum < OSP_MAX_SPOINTS) {
+                               ast_copy_string(provider->spoints[provider->spnum], var->value, sizeof(provider->spoints[provider->spnum]));
+                               ast_debug(1, "OSP: servicepoint[%d]: '%s'\n", provider->spnum, provider->spoints[provider->spnum]);
+                               provider->spnum++;
                        } else {
-                               ast_log(LOG_WARNING, "OSP: Too many Service Points at line %d\n", v->lineno);
+                               ast_log(LOG_WARNING, "OSP: Too many Service Points at line %d\n", var->lineno);
                        }
-               } else if (!strcasecmp(v->name, "maxconnections")) {
-                       if ((sscanf(v->value, "%d", &t) == 1) && (t >= OSP_MIN_MAXCONNECTIONS) && (t <= OSP_MAX_MAXCONNECTIONS)) {
-                               p->maxconnections = t;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: maxconnections '%d'\n", t);
+               } else if (!strcasecmp(var->name, "maxconnect")) {
+                       if ((sscanf(var->value, "%30d", &num) == 1) && (num >= OSP_MIN_MAXCONNECT) && (num <= OSP_MAX_MAXCONNECT)) {
+                               provider->maxconnect = num;
+                               ast_debug(1, "OSP: maxconnect '%d'\n", num);
                        } else {
-                               ast_log(LOG_WARNING, "OSP: maxconnections should be an integer from %d to %d, not '%s' at line %d\n",
-                                       OSP_MIN_MAXCONNECTIONS, OSP_MAX_MAXCONNECTIONS, v->value, v->lineno);
+                               ast_log(LOG_WARNING, "OSP: maxconnect should be an integer from %d to %d, not '%s' at line %d\n",
+                                       OSP_MIN_MAXCONNECT, OSP_MAX_MAXCONNECT, var->value, var->lineno);
                        }
-               } else if (!strcasecmp(v->name, "retrydelay")) {
-                       if ((sscanf(v->value, "%d", &t) == 1) && (t >= OSP_MIN_RETRYDELAY) && (t <= OSP_MAX_RETRYDELAY)) {
-                               p->retrydelay = t;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: retrydelay '%d'\n", t);
+               } else if (!strcasecmp(var->name, "retrydelay")) {
+                       if ((sscanf(var->value, "%30d", &num) == 1) && (num >= OSP_MIN_RETRYDELAY) && (num <= OSP_MAX_RETRYDELAY)) {
+                               provider->retrydelay = num;
+                               ast_debug(1, "OSP: retrydelay '%d'\n", num);
                        } else {
                                ast_log(LOG_WARNING, "OSP: retrydelay should be an integer from %d to %d, not '%s' at line %d\n",
-                                       OSP_MIN_RETRYDELAY, OSP_MAX_RETRYDELAY, v->value, v->lineno);
+                                       OSP_MIN_RETRYDELAY, OSP_MAX_RETRYDELAY, var->value, var->lineno);
                        }
-               } else if (!strcasecmp(v->name, "retrylimit")) {
-                       if ((sscanf(v->value, "%d", &t) == 1) && (t >= OSP_MIN_RETRYLIMIT) && (t <= OSP_MAX_RETRYLIMIT)) {
-                               p->retrylimit = t;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: retrylimit '%d'\n", t);
+               } else if (!strcasecmp(var->name, "retrylimit")) {
+                       if ((sscanf(var->value, "%30d", &num) == 1) && (num >= OSP_MIN_RETRYLIMIT) && (num <= OSP_MAX_RETRYLIMIT)) {
+                               provider->retrylimit = num;
+                               ast_debug(1, "OSP: retrylimit '%d'\n", num);
                        } else {
                                ast_log(LOG_WARNING, "OSP: retrylimit should be an integer from %d to %d, not '%s' at line %d\n",
-                                       OSP_MIN_RETRYLIMIT, OSP_MAX_RETRYLIMIT, v->value, v->lineno);
+                                       OSP_MIN_RETRYLIMIT, OSP_MAX_RETRYLIMIT, var->value, var->lineno);
                        }
-               } else if (!strcasecmp(v->name, "timeout")) {
-                       if ((sscanf(v->value, "%d", &t) == 1) && (t >= OSP_MIN_TIMEOUT) && (t <= OSP_MAX_TIMEOUT)) {
-                               p->timeout = t;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: timeout '%d'\n", t);
+               } else if (!strcasecmp(var->name, "timeout")) {
+                       if ((sscanf(var->value, "%30d", &num) == 1) && (num >= OSP_MIN_TIMEOUT) && (num <= OSP_MAX_TIMEOUT)) {
+                               provider->timeout = num;
+                               ast_debug(1, "OSP: timeout '%d'\n", num);
                        } else {
                                ast_log(LOG_WARNING, "OSP: timeout should be an integer from %d to %d, not '%s' at line %d\n",
-                                       OSP_MIN_TIMEOUT, OSP_MAX_TIMEOUT, v->value, v->lineno);
+                                       OSP_MIN_TIMEOUT, OSP_MAX_TIMEOUT, var->value, var->lineno);
                        }
-               } else if (!strcasecmp(v->name, "source")) {
-                       ast_copy_string(p->source, v->value, sizeof(p->source));
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: source '%s'\n", p->source);
-               } else if (!strcasecmp(v->name, "authpolicy")) {
-                       if ((sscanf(v->value, "%d", &t) == 1) && ((t == OSP_AUTH_NO) || (t == OSP_AUTH_YES) || (t == OSP_AUTH_EXCLUSIVE))) {
-                               p->authpolicy = t;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: authpolicy '%d'\n", t);
+               } else if (!strcasecmp(var->name, "source")) {
+                       ast_copy_string(provider->source, var->value, sizeof(provider->source));
+                       ast_debug(1, "OSP: source '%s'\n", provider->source);
+               } else if (!strcasecmp(var->name, "authpolicy")) {
+                       if ((sscanf(var->value, "%30d", &num) == 1) && ((num == OSP_AUTH_NO) || (num == OSP_AUTH_YES) || (num == OSP_AUTH_EXC))) {
+                               provider->authpolicy = num;
+                               ast_debug(1, "OSP: authpolicy '%d'\n", num);
                        } else {
                                ast_log(LOG_WARNING, "OSP: authpolicy should be %d, %d or %d, not '%s' at line %d\n",
-                                       OSP_AUTH_NO, OSP_AUTH_YES, OSP_AUTH_EXCLUSIVE, v->value, v->lineno);
+                                       OSP_AUTH_NO, OSP_AUTH_YES, OSP_AUTH_EXC, var->value, var->lineno);
+                       }
+               } else if (!strcasecmp(var->name, "defprotocol")) {
+                       if (!strcasecmp(var->value, OSP_PROT_SIP)) {
+                               provider->defprotocol = OSP_PROT_SIP;
+                               ast_debug(1, "OSP: default protocol SIP\n");
+                       } else if (!strcasecmp(var->value, OSP_PROT_H323)) {
+                               provider->defprotocol = OSP_PROT_H323;
+                               ast_debug(1, "OSP: default protocol H.323\n");
+                       } else if (!strcasecmp(var->value, OSP_PROT_IAX)) {
+                               provider->defprotocol = OSP_PROT_IAX;
+                               ast_debug(1, "OSP: default protocol IAX\n");
+                       } else if (!strcasecmp(var->value, OSP_PROT_SKYPE)) {
+                               provider->defprotocol = OSP_PROT_SKYPE;
+                               ast_debug(1, "OSP: default protocol Skype\n");
+                       } else {
+                               ast_log(LOG_WARNING, "OSP: default protocol should be %s, %s, %s or %s not '%s' at line %d\n",
+                                       OSP_PROT_SIP, OSP_PROT_H323, OSP_PROT_IAX, OSP_PROT_SKYPE, var->value, var->lineno);
+                       }
+               } else if (!strcasecmp(var->name, "workmode")) {
+                       if ((sscanf(var->value, "%30d", &num) == 1) && ((num == OSP_MODE_DIRECT) || (num == OSP_MODE_INDIRECT))) {
+                               provider->workmode = num;
+                               ast_debug(1, "OSP: workmode '%d'\n", num);
+                       } else {
+                               ast_log(LOG_WARNING, "OSP: workmode should be %d or %d, not '%s' at line %d\n",
+                                       OSP_MODE_DIRECT, OSP_MODE_INDIRECT, var->value, var->lineno);
                        }
-               } else if (!strcasecmp(v->name, "defaultprotocol")) {
-                       if (!strcasecmp(v->value, OSP_PROT_SIP)) {
-                               p->defaultprotocol = OSP_PROT_SIP;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: default protocol '%s'\n", p->defaultprotocol);
-                       } else if (!strcasecmp(v->value, OSP_PROT_H323)) {
-                               p->defaultprotocol = OSP_PROT_H323;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: default protocol '%s'\n", p->defaultprotocol);
-                       } else if (!strcasecmp(v->value, OSP_PROT_IAX)) {
-                               p->defaultprotocol = OSP_PROT_IAX;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: default protocol '%s'\n", p->defaultprotocol);
+               } else if (!strcasecmp(var->name, "servicetype")) {
+                       if ((sscanf(var->value, "%30d", &num) == 1) && ((num == OSP_SRV_VOICE) || (num == OSP_SRV_NPQUERY))) {
+                               provider->srvtype = num;
+                               ast_debug(1, "OSP: servicetype '%d'\n", num);
                        } else {
-                               ast_log(LOG_WARNING, "OSP: default protocol should be %s, %s, %s, or %s not '%s' at line %d\n",
-                                       OSP_PROT_SIP, OSP_PROT_H323, OSP_PROT_IAX, OSP_PROT_OTHER, v->value, v->lineno);
+                               ast_log(LOG_WARNING, "OSP: servicetype should be %d or %d, not '%s' at line %d\n",
+                                       OSP_SRV_VOICE, OSP_SRV_NPQUERY, var->value, var->lineno);
                        }
                }
-               v = v->next;
        }
 
-       error = OSPPUtilLoadPEMPrivateKey((unsigned char*)p->privatekey, &privatekey);
-       if (error != OSPC_ERR_NO_ERROR) {
-               ast_log(LOG_WARNING, "OSP: Unable to load privatekey '%s', error '%d'\n", p->privatekey, error);
-               free(p);
-               return 0;
+       if (provider->canum == 0) {
+               provider->canum = 1;
        }
 
-       error = OSPPUtilLoadPEMCert((unsigned char*)p->localcert, &localcert);
-       if (error != OSPC_ERR_NO_ERROR) {
-               ast_log(LOG_WARNING, "OSP: Unable to load localcert '%s', error '%d'\n", p->localcert, error);
-               if (privatekey.PrivateKeyData) {
-                       free(privatekey.PrivateKeyData);
-               }
-               free(p);
-               return 0;
+       for (i = 0; i < provider->spnum; i++) {
+               pspoints[i] = provider->spoints[i];
        }
 
-       if (p->cacount < 1) {
-               snprintf(p->cacerts[p->cacount], sizeof(p->cacerts[0]), "%s/%s-cacert.pem", ast_config_AST_KEY_DIR, provider);
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: cacert[%d]: '%s'\n", p->cacount, p->cacerts[p->cacount]);
-               p->cacount++;
-       }
-       for (i = 0; i < p->cacount; i++) {
-               error = OSPPUtilLoadPEMCert((unsigned char*)p->cacerts[i], &cacerts[i]);
-               if (error != OSPC_ERR_NO_ERROR) {
-                       ast_log(LOG_WARNING, "OSP: Unable to load cacert '%s', error '%d'\n", p->cacerts[i], error);
-                       for (j = 0; j < i; j++) {
-                               if (cacerts[j].CertData) {
-                                       free(cacerts[j].CertData);
+       if (osp_security) {
+               privatekey.PrivateKeyData = NULL;
+               privatekey.PrivateKeyLength = 0;
+
+               localcert.CertData = NULL;
+               localcert.CertDataLength = 0;
+
+               for (i = 0; i < provider->canum; i++) {
+                       cacerts[i].CertData = NULL;
+                       cacerts[i].CertDataLength = 0;
+               }
+
+               if ((error = OSPPUtilLoadPEMPrivateKey((unsigned char*)provider->privatekey, &privatekey)) != OSPC_ERR_NO_ERROR) {
+                       ast_log(LOG_WARNING, "OSP: Unable to load privatekey '%s', error '%d'\n", provider->privatekey, error);
+               } else if ((error = OSPPUtilLoadPEMCert((unsigned char*)provider->localcert, &localcert)) != OSPC_ERR_NO_ERROR) {
+                       ast_log(LOG_WARNING, "OSP: Unable to load localcert '%s', error '%d'\n", provider->localcert, error);
+               } else {
+                       for (i = 0; i < provider->canum; i++) {
+                               if ((error = OSPPUtilLoadPEMCert((unsigned char*)provider->cacerts[i], &cacerts[i])) != OSPC_ERR_NO_ERROR) {
+                                       ast_log(LOG_WARNING, "OSP: Unable to load cacert '%s', error '%d'\n", provider->cacerts[i], error);
+                                       break;
+                               } else {
+                                       pcacerts[i] = &cacerts[i];
                                }
                        }
-                       if (localcert.CertData) {
-                               free(localcert.CertData);
-                       }
-                       if (privatekey.PrivateKeyData) {
-                               free(privatekey.PrivateKeyData);
-                       }
-                       free(p);
-                       return 0;
-               }
-               pcacerts[i] = &cacerts[i];
-       }
-
-       for (i = 0; i < p->spcount; i++) {
-               psrvpoints[i] = p->srvpoints[i];
-       }
-
-       error = OSPPProviderNew(
-                               p->spcount,
-                               psrvpoints,
-                               NULL,
-                               OSP_AUDIT_URL,
-                               &privatekey,
-                               &localcert,
-                               p->cacount,
-                               pcacerts,
-                               OSP_LOCAL_VALIDATION,
-                               OSP_SSL_LIFETIME,
-                               p->maxconnections,
-                               OSP_HTTP_PERSISTENCE,
-                               p->retrydelay,
-                               p->retrylimit,
-                               p->timeout,
-                               OSP_CUSTOMER_ID,
-                               OSP_DEVICE_ID,
-                               &p->handle);
-       if (error != OSPC_ERR_NO_ERROR) {
-               ast_log(LOG_WARNING, "OSP: Unable to create provider '%s', error '%d'\n", provider, error);
-               free(p);
-               res = -1;
+               }
        } else {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: provider '%s'\n", provider);
-               ast_mutex_lock(&osplock);
-               p->next = ospproviders;
-               ospproviders = p;
-               ast_mutex_unlock(&osplock);
-               res = 1;
+               privatekey.PrivateKeyData = privatekeydata;
+               privatekey.PrivateKeyLength = sizeof(privatekeydata);
+
+               localcert.CertData = localcertdata;
+               localcert.CertDataLength = sizeof(localcertdata);
+
+               cacerts[0].CertData = cacertdata;
+               cacerts[0].CertDataLength = sizeof(cacertdata);
+               pcacerts[0] = &cacerts[0];
+
+               if ((error = OSPPBase64Decode(B64PKey, strlen(B64PKey), privatekey.PrivateKeyData, &privatekey.PrivateKeyLength)) != OSPC_ERR_NO_ERROR) {
+                       ast_log(LOG_WARNING, "OSP: Unable to decode private key, error '%d'\n", error);
+               } else if ((error = OSPPBase64Decode(B64LCert, strlen(B64LCert), localcert.CertData, &localcert.CertDataLength)) != OSPC_ERR_NO_ERROR) {
+                       ast_log(LOG_WARNING, "OSP: Unable to decode local cert, error '%d'\n", error);
+               } else if ((error = OSPPBase64Decode(B64CACert, strlen(B64CACert), cacerts[0].CertData, &cacerts[0].CertDataLength)) != OSPC_ERR_NO_ERROR) {
+                       ast_log(LOG_WARNING, "OSP: Unable to decode cacert, error '%d'\n", error);
+               }
        }
 
-       for (i = 0; i < p->cacount; i++) {
-               if (cacerts[i].CertData) {
-                       free(cacerts[i].CertData);
+       if (error == OSPC_ERR_NO_ERROR) {
+               error = OSPPProviderNew(provider->spnum,
+                       pspoints,
+                       NULL,
+                       OSP_AUDIT_URL,
+                       &privatekey,
+                       &localcert,
+                       provider->canum,
+                       pcacerts,
+                       OSP_LOCAL_VALIDATION,
+                       OSP_SSL_LIFETIME,
+                       provider->maxconnect,
+                       OSP_HTTP_PERSISTENCE,
+                       provider->retrydelay,
+                       provider->retrylimit,
+                       provider->timeout,
+                       OSP_CUSTOMER_ID,
+                       OSP_DEVICE_ID,
+                       &provider->handle);
+               if (error != OSPC_ERR_NO_ERROR) {
+                       ast_log(LOG_WARNING, "OSP: Unable to create provider '%s', error '%d'\n", name, error);
+                       res = OSP_ERROR;
+               } else {
+                       ast_debug(1, "OSP: provider '%s'\n", name);
+                       ast_mutex_lock(&osp_lock);
+                       provider->next = osp_providers;
+                       osp_providers = provider;
+                       ast_mutex_unlock(&osp_lock);
+                       res = OSP_OK;
                }
        }
-       if (localcert.CertData) {
-               free(localcert.CertData);
+
+       if (osp_security) {
+               for (i = 0; i < provider->canum; i++) {
+                       if (cacerts[i].CertData) {
+                               ast_free(cacerts[i].CertData);
+                       }
+               }
+               if (localcert.CertData) {
+                       ast_free(localcert.CertData);
+               }
+               if (privatekey.PrivateKeyData) {
+                       ast_free(privatekey.PrivateKeyData);
+               }
        }
-       if (privatekey.PrivateKeyData) {
-               free(privatekey.PrivateKeyData);
+
+       if (res != OSP_OK) {
+               ast_free(provider);
        }
 
        return res;
@@ -422,110 +904,177 @@ static int osp_create_provider(
  * \brief Get OSP provider by name
  * \param name OSP provider context name
  * \param provider OSP provider structure
- * \return 1 Success, 0 Failed, -1 Error
+ * \return OSP_OK Success, OSP_FAILED Failed, OSP_ERROR Error
  */
 static int osp_get_provider(
        const char* name,
        struct osp_provider** provider)
 {
-       int res = 0;
+       int res = OSP_FAILED;
        struct osp_provider* p;
 
-       ast_mutex_lock(&osplock);
-       p = ospproviders;
-       while(p) {
+       *provider = NULL;
+
+       ast_mutex_lock(&osp_lock);
+       for (p = osp_providers; p != NULL; p = p->next) {
                if (!strcasecmp(p->name, name)) {
                        *provider = p;
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: find provider '%s'\n", name);
-                       res = 1;
+                       ast_debug(1, "OSP: find provider '%s'\n", name);
+                       res = OSP_OK;
                        break;
                }
-               p = p->next;
        }
-       ast_mutex_unlock(&osplock);
+       ast_mutex_unlock(&osp_lock);
 
        return res;
 }
 
 /*!
  * \brief Create OSP transaction handle
- * \param provider OSP provider context name
- * \param transaction OSP transaction handle, output
- * \param sourcesize Size of source buffer, in/output
+ * \param name OSP provider context name
+ * \param trans OSP transaction handle, output
  * \param source Source of provider, output
- * \return 1 Success, 0 Failed, -1 Error
+ * \param srcsize Size of source buffer, in
+ * \return OSK_OK Success, OSK_FAILED Failed, OSP_ERROR Error
  */
 static int osp_create_transaction(
-       const char* provider,
-       int* transaction,
-       unsigned int sourcesize,
-       char* source)
+       const char* name,
+       int* trans,
+       char* source,
+       unsigned int srcsize)
 {
-       int res = 0;
-       struct osp_provider* p;
+       int res = OSP_FAILED;
+       struct osp_provider* provider;
        int error;
 
-       ast_mutex_lock(&osplock);
-       p = ospproviders;
-       while(p) {
-               if (!strcasecmp(p->name, provider)) {
-                       error = OSPPTransactionNew(p->handle, transaction);
+       if ((trans == NULL) || (source == NULL) || (srcsize <= 0)) {
+               ast_log(LOG_ERROR, "Invalid parameters\n");
+               return OSP_ERROR;
+       }
+
+       *trans = OSP_INVALID_HANDLE;
+       *source = '\0';
+
+       ast_mutex_lock(&osp_lock);
+       for (provider = osp_providers; provider; provider = provider->next) {
+               if (!strcasecmp(provider->name, name)) {
+                       error = OSPPTransactionNew(provider->handle, trans);
                        if (error == OSPC_ERR_NO_ERROR) {
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: transaction '%d'\n", *transaction);
-                               ast_copy_string(source, p->source, sourcesize);
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: source '%s'\n", source);
-                               res = 1;
+                               ast_debug(1, "OSP: transaction '%d'\n", *trans);
+                               ast_copy_string(source, provider->source, srcsize);
+                               ast_debug(1, "OSP: source '%s'\n", source);
+                               res = OSP_OK;
                        } else {
-                               *transaction = OSP_INVALID_HANDLE;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: Unable to create transaction handle, error '%d'\n", error);
-                               res = -1;
+                               *trans = OSP_INVALID_HANDLE;
+                               ast_debug(1, "OSP: Unable to create transaction handle, error '%d'\n", error);
+                               *source = '\0';
+                               res = OSP_ERROR;
                        }
                        break;
                }
-               p = p->next;
        }
-       ast_mutex_unlock(&osplock);
+       ast_mutex_unlock(&osp_lock);
 
        return res;
 }
 
 /*!
- * \brief Convert address to "[x.x.x.x]" or "host.domain" format
+ * \brief Convert "address:port" to "[x.x.x.x]:port" or "hostname:port" format
  * \param src Source address string
- * \param dst Destination address string
- * \param buffersize Size of dst buffer
+ * \param dest Destination address string
+ * \param destsize Size of dest buffer
  */
-static void osp_convert_address(
+static void osp_convert_inout(
        const char* src,
-       char* dst,
-       int buffersize)
+       char* dest,
+       unsigned int destsize)
 {
        struct in_addr inp;
+       char buffer[OSP_SIZE_NORSTR];
+       char* port;
 
-       if (inet_aton(src, &inp) != 0) {
-               snprintf(dst, buffersize, "[%s]", src);
-       } else {
-               snprintf(dst, buffersize, "%s", src);
+       if ((dest != NULL) && (destsize > 0)) {
+               if (!ast_strlen_zero(src)) {
+                       ast_copy_string(buffer, src, sizeof(buffer));
+
+                       if((port = strchr(buffer, ':')) != NULL) {
+                               *port = '\0';
+                               port++;
+                       }
+
+                       if (inet_pton(AF_INET, buffer, &inp) == 1) {
+                               if (port != NULL) {
+                                       snprintf(dest, destsize, "[%s]:%s", buffer, port);
+                               } else {
+                                       snprintf(dest, destsize, "[%s]", buffer);
+                               }
+                               dest[destsize - 1] = '\0';
+                       } else {
+                               ast_copy_string(dest, src, destsize);
+                       }
+               } else {
+                       *dest = '\0';
+               }
+       }
+}
+
+/*!
+ * \brief Convert "[x.x.x.x]:port" or "hostname:prot" to "address:port" format
+ * \param src Source address string
+ * \param dest Destination address string
+ * \param destsize Size of dest buffer
+ */
+static void osp_convert_outin(
+       const char* src,
+       char* dest,
+       unsigned int destsize)
+{
+       char buffer[OSP_SIZE_NORSTR];
+       char* end;
+       char* port;
+
+       if ((dest != NULL) && (destsize > 0)) {
+               if (!ast_strlen_zero(src)) {
+                       ast_copy_string(buffer, src, sizeof(buffer));
+
+                       if (buffer[0] == '[') {
+                               if((port = strchr(buffer + 1, ':')) != NULL) {
+                                       *port = '\0';
+                                       port++;
+                               }
+
+                               if ((end = strchr(buffer + 1, ']')) != NULL) {
+                                       *end = '\0';
+                               }
+
+                               if (port != NULL) {
+                                       snprintf(dest, destsize, "%s:%s", buffer + 1, port);
+                                       dest[destsize - 1] = '\0';
+                               } else {
+                                       ast_copy_string(dest, buffer + 1, destsize);
+                               }
+                       } else {
+                               ast_copy_string(dest, src, destsize);
+                       }
+               } else {
+                       *dest = '\0';
+               }
        }
 }
 
 /*!
  * \brief Validate OSP token of inbound call
- * \param transaction OSP transaction handle
+ * \param trans OSP transaction handle
  * \param source Source of inbound call
  * \param destination Destination of inbound call
  * \param calling Calling number
  * \param called Called number
  * \param token OSP token, may be empty
  * \param timelimit Call duration limit, output
- * \return 1 Success, 0 Failed, -1 Error
+ * \return OSP_OK Success, OSP_FAILED Failed, OSP_ERROR Error
  */
 static int osp_validate_token(
-       int transaction,
+       int trans,
        const char* source,
        const char* destination,
        const char* calling,
@@ -535,47 +1084,49 @@ static int osp_validate_token(
 {
        int res;
        int tokenlen;
-       unsigned char tokenstr[OSP_TOKSTR_SIZE];
-       char src[OSP_TOKSTR_SIZE];
-       char dst[OSP_TOKSTR_SIZE];
+       unsigned char tokenstr[OSP_SIZE_TOKSTR];
+       char src[OSP_SIZE_NORSTR];
+       char dest[OSP_SIZE_NORSTR];
        unsigned int authorised;
        unsigned int dummy = 0;
        int error;
 
+       if (timelimit == NULL) {
+               ast_log(LOG_ERROR, "Invalid parameters\n");
+               return OSP_ERROR;
+       }
+
        tokenlen = ast_base64decode(tokenstr, token, strlen(token));
-       osp_convert_address(source, src, sizeof(src));
-       osp_convert_address(destination, dst, sizeof(dst));
-       error = OSPPTransactionValidateAuthorisation(
-                               transaction,
-                               src,
-                               dst,
-                               NULL,
-                               NULL,
-                               calling ? calling : "",
-                               OSPC_E164,
-                               called,
-                               OSPC_E164,
-                               0,
-                               NULL,
-                               tokenlen,
-                               (char*)tokenstr,
-                               &authorised,
-                               timelimit,
-                               &dummy,
-                               NULL,
-                               osp_tokenformat);
+       osp_convert_inout(source, src, sizeof(src));
+       osp_convert_inout(destination, dest, sizeof(dest));
+       error = OSPPTransactionValidateAuthorisation(trans,
+               src,
+               dest,
+               NULL,
+               NULL,
+               calling ? calling : "",
+               OSPC_NFORMAT_E164,
+               called,
+               OSPC_NFORMAT_E164,
+               0,
+               NULL,
+               tokenlen,
+               (char*)tokenstr,
+               &authorised,
+               timelimit,
+               &dummy,
+               NULL,
+               osp_tokenformat);
        if (error != OSPC_ERR_NO_ERROR) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to validate inbound token\n");
-               res = -1;
+               ast_log(LOG_WARNING, "OSP: Unable to validate inbound token, error '%d'\n", error);
+               *timelimit = 0;
+               res = OSP_ERROR;
        } else if (authorised) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Authorised\n");
-               res = 1;
+               ast_debug(1, "OSP: Authorised\n");
+               res = OSP_OK;
        } else {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unauthorised\n");
-               res = 0;
+               ast_debug(1, "OSP: Unauthorised\n");
+               res = OSP_FAILED;
        }
 
        return res;
@@ -603,112 +1154,147 @@ static unsigned int osp_choose_timelimit(
 /*!
  * \brief Choose min duration limit
  * \param provider OSP provider
- * \param called Called number
  * \param calling Calling number
+ * \param called Called number
  * \param destination Destination IP in '[x.x.x.x]' format
  * \param tokenlen OSP token length
  * \param token OSP token
  * \param reason Failure reason, output
- * \param result OSP lookup results, in/output
- * \return 1 Success, 0 Failed, -1 Error
+ * \param results OSP lookup results, in/output
+ * \return OSP_OK Success, OSP_FAILED Failed, OSP_ERROR Error
  */
 static int osp_check_destination(
        struct osp_provider* provider,
-       const char* called,
        const char* calling,
-       char* destination,
+       const char* called,
+       const char* destination,
        unsigned int tokenlen,
        const char* token,
-       enum OSPEFAILREASON* reason,
-       struct osp_result* result)
+       OSPEFAILREASON* reason,
+       struct osp_results* results)
 {
        int res;
-       OSPE_DEST_OSP_ENABLED enabled;
-       OSPE_DEST_PROT protocol;
+       OSPE_DEST_OSPENABLED enabled;
+       OSPE_PROTOCOL_NAME protocol;
+       char dest[OSP_SIZE_NORSTR];
+       OSPE_OPERATOR_NAME type;
        int error;
 
-       if (strlen(destination) <= 2) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Wrong destination format '%s'\n", destination);
-               *reason = OSPC_FAIL_NORMAL_UNSPECIFIED;
-               return -1;
+       if ((provider == NULL) || (reason == NULL) || (results == NULL)) {
+               ast_log(LOG_ERROR, "Invalid parameters\n");
+               return OSP_ERROR;
        }
 
-       if ((error = OSPPTransactionIsDestOSPEnabled(result->outhandle, &enabled)) != OSPC_ERR_NO_ERROR) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to get destination OSP version, error '%d'\n", error);
+       if ((error = OSPPTransactionIsDestOSPEnabled(results->outhandle, &enabled)) != OSPC_ERR_NO_ERROR) {
+               ast_debug(1, "OSP: Unable to get destination OSP version, error '%d'\n", error);
                *reason = OSPC_FAIL_NORMAL_UNSPECIFIED;
-               return -1;
+               return OSP_ERROR;
        }
 
-       if (enabled == OSPE_OSP_FALSE) {
-               result->token[0] = '\0';
+       if (enabled == OSPC_DOSP_FALSE) {
+               results->token[0] = '\0';
        } else {
-               ast_base64encode(result->token, (const unsigned char*)token, tokenlen, sizeof(result->token) - 1);
+               ast_base64encode(results->token, (const unsigned char*)token, tokenlen, sizeof(results->token) - 1);
+       }
+
+       if ((error = OSPPTransactionGetDestinationNetworkId(results->outhandle, sizeof(results->networkid), results->networkid)) != OSPC_ERR_NO_ERROR) {
+               ast_debug(1, "OSP: Unable to get destination network ID, error '%d'\n", error);
+               results->networkid[0] = '\0';
        }
 
-       if ((error = OSPPTransactionGetDestNetworkId(result->outhandle, result->networkid)) != OSPC_ERR_NO_ERROR) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to get destination network ID, error '%d'\n", error);
-               result->networkid[0] = '\0';
+       error = OSPPTransactionGetNumberPortabilityParameters(results->outhandle,
+               sizeof(results->nprn),
+               results->nprn,
+               sizeof(results->npcic),
+               results->npcic,
+               &results->npdi);
+       if (error != OSPC_ERR_NO_ERROR) {
+               ast_debug(1, "OSP: Unable to get number portability parameters, error '%d'\n", error);
+               results->nprn[0] = '\0';
+               results->npcic[0] = '\0';
+               results->npdi = 0;
        }
 
-       if ((error = OSPPTransactionGetDestProtocol(result->outhandle, &protocol)) != OSPC_ERR_NO_ERROR) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to get destination protocol, error '%d'\n", error);
+       for (type = OSPC_OPNAME_START; type < OSPC_OPNAME_NUMBER; type++) {
+               error = OSPPTransactionGetOperatorName(results->outhandle, type, sizeof(results->opname[type]), results->opname[type]);
+               if (error != OSPC_ERR_NO_ERROR) {
+                       ast_debug(1, "OSP: Unable to get operator name of type '%d', error '%d'\n", type, error);
+                       results->opname[type][0] = '\0';
+               }
+       }
+
+       if ((error = OSPPTransactionGetDestProtocol(results->outhandle, &protocol)) != OSPC_ERR_NO_ERROR) {
+               ast_debug(1, "OSP: Unable to get destination protocol, error '%d'\n", error);
                *reason = OSPC_FAIL_NORMAL_UNSPECIFIED;
-               result->token[0] = '\0';
-               result->networkid[0] = '\0';
-               return -1;
+               results->token[0] = '\0';
+               results->networkid[0] = '\0';
+               results->nprn[0] = '\0';
+               results->npcic[0] = '\0';
+               results->npdi = 0;
+               for (type = OSPC_OPNAME_START; type < OSPC_OPNAME_NUMBER; type++) {
+                       results->opname[type][0] = '\0';
+               }
+               return OSP_ERROR;
        }
 
-       res = 1;
-       /* Strip leading and trailing brackets */
-       destination[strlen(destination) - 1] = '\0';
+       res = OSP_OK;
+       osp_convert_outin(destination, dest, sizeof(dest));
        switch(protocol) {
-               case OSPE_DEST_PROT_H323_SETUP:
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: protocol '%s'\n", OSP_PROT_H323);
-                       ast_copy_string(result->tech, OSP_TECH_H323, sizeof(result->tech));
-                       ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
-                       ast_copy_string(result->called, called, sizeof(result->called));
-                       ast_copy_string(result->calling, calling, sizeof(result->calling));
-                       break;
-               case OSPE_DEST_PROT_SIP:
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: protocol '%s'\n", OSP_PROT_SIP);
-                       ast_copy_string(result->tech, OSP_TECH_SIP, sizeof(result->tech));
-                       ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
-                       ast_copy_string(result->called, called, sizeof(result->called));
-                       ast_copy_string(result->calling, calling, sizeof(result->calling));
-                       break;
-               case OSPE_DEST_PROT_IAX:
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: protocol '%s'\n", OSP_PROT_IAX);
-                       ast_copy_string(result->tech, OSP_TECH_IAX, sizeof(result->tech));
-                       ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
-                       ast_copy_string(result->called, called, sizeof(result->called));
-                       ast_copy_string(result->calling, calling, sizeof(result->calling));
-                       break;
-               case OSPE_DEST_PROT_UNDEFINED:
-               case OSPE_DEST_PROT_UNKNOWN:
-                       if (option_debug) {
-                               ast_log(LOG_DEBUG, "OSP: unknown/undefined protocol '%d'\n", protocol);
-                               ast_log(LOG_DEBUG, "OSP: use default protocol '%s'\n", provider->defaultprotocol);
-                       }
-                       ast_copy_string(result->tech, provider->defaultprotocol, sizeof(result->tech));
-                       ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
-                       ast_copy_string(result->called, called, sizeof(result->called));
-                       ast_copy_string(result->calling, calling, sizeof(result->calling));
-                       break;
-               case OSPE_DEST_PROT_H323_LRQ:
-               default:
-                       ast_log(LOG_WARNING, "OSP: unsupported protocol '%d'\n", protocol);
-                       *reason = OSPC_FAIL_PROTOCOL_ERROR;
-                       result->token[0] = '\0';
-                       result->networkid[0] = '\0';
-                       res = 0;
-                       break;
+       case OSPC_PROTNAME_SIP:
+               ast_debug(1, "OSP: protocol SIP\n");
+               ast_copy_string(results->outtech, OSP_TECH_SIP, sizeof(results->outtech));
+               ast_copy_string(results->dest, dest, sizeof(results->dest));
+               ast_copy_string(results->calling, calling, sizeof(results->calling));
+               ast_copy_string(results->called, called, sizeof(results->called));
+               break;
+       case OSPC_PROTNAME_Q931:
+               ast_debug(1, "OSP: protocol Q.931\n");
+               ast_copy_string(results->outtech, OSP_TECH_H323, sizeof(results->outtech));
+               ast_copy_string(results->dest, dest, sizeof(results->dest));
+               ast_copy_string(results->calling, calling, sizeof(results->calling));
+               ast_copy_string(results->called, called, sizeof(results->called));
+               break;
+       case OSPC_PROTNAME_IAX:
+               ast_debug(1, "OSP: protocol IAX\n");
+               ast_copy_string(results->outtech, OSP_TECH_IAX, sizeof(results->outtech));
+               ast_copy_string(results->dest, dest, sizeof(results->dest));
+               ast_copy_string(results->calling, calling, sizeof(results->calling));
+               ast_copy_string(results->called, called, sizeof(results->called));
+               break;
+       case OSPC_PROTNAME_SKYPE:
+               ast_debug(1, "OSP: protocol Skype\n");
+               ast_copy_string(results->outtech, OSP_TECH_SKYPE, sizeof(results->outtech));
+               ast_copy_string(results->dest, dest, sizeof(results->dest));
+               ast_copy_string(results->calling, calling, sizeof(results->calling));
+               ast_copy_string(results->called, called, sizeof(results->called));
+               break;
+       case OSPC_PROTNAME_UNDEFINED:
+       case OSPC_PROTNAME_UNKNOWN:
+               ast_debug(1, "OSP: unknown/undefined protocol '%d'\n", protocol);
+               ast_debug(1, "OSP: use default protocol '%s'\n", provider->defprotocol);
+               ast_copy_string(results->outtech, provider->defprotocol, sizeof(results->outtech));
+               ast_copy_string(results->dest, dest, sizeof(results->dest));
+               ast_copy_string(results->calling, calling, sizeof(results->calling));
+               ast_copy_string(results->called, called, sizeof(results->called));
+               break;
+       case OSPC_PROTNAME_LRQ:
+       case OSPC_PROTNAME_T37:
+       case OSPC_PROTNAME_T38:
+       case OSPC_PROTNAME_SMPP:
+       case OSPC_PROTNAME_XMPP:
+       default:
+               ast_log(LOG_WARNING, "OSP: unsupported protocol '%d'\n", protocol);
+               *reason = OSPC_FAIL_PROTOCOL_ERROR;
+               results->token[0] = '\0';
+               results->networkid[0] = '\0';
+               results->nprn[0] = '\0';
+               results->npcic[0] = '\0';
+               results->npdi = 0;
+               for (type = OSPC_OPNAME_START; type < OSPC_OPNAME_NUMBER; type++) {
+                       results->opname[type][0] = '\0';
+               }
+               res = OSP_FAILED;
+               break;
        }
 
        return res;
@@ -719,26 +1305,26 @@ static int osp_check_destination(
  * \param cause Asterisk hangup cause
  * \return OSP TC code
  */
-static enum OSPEFAILREASON asterisk2osp(
+static OSPEFAILREASON asterisk2osp(
        int cause)
 {
-       return (enum OSPEFAILREASON)cause;
+       return (OSPEFAILREASON)cause;
 }
 
 /*!
  * \brief OSP Authentication function
- * \param provider OSP provider context name
- * \param transaction OSP transaction handle, output
+ * \param name OSP provider context name
+ * \param trans OSP transaction handle, output
  * \param source Source of inbound call
  * \param calling Calling number
  * \param called Called number
  * \param token OSP token, may be empty
  * \param timelimit Call duration limit, output
- * \return 1 Authenricated, 0 Unauthenticated, -1 Error
+ * \return OSP_OK Authenricated, OSP_FAILED Unauthenticated, OSP_ERROR Error
  */
 static int osp_auth(
-       const char* provider,
-       int* transaction,
+       const char* name,
+       int* trans,
        const char* source,
        const char* calling,
        const char* called,
@@ -746,47 +1332,49 @@ static int osp_auth(
        unsigned int* timelimit)
 {
        int res;
-       struct osp_provider* p;
-       char dest[OSP_NORSTR_SIZE];
+       struct osp_provider* provider = NULL;
+       char dest[OSP_SIZE_NORSTR];
 
-       *transaction = OSP_INVALID_HANDLE;
+       if ((trans == NULL) || (timelimit == NULL)) {
+               ast_log(LOG_ERROR, "Invalid parameters\n");
+               return OSP_ERROR;
+       }
+
+       *trans = OSP_INVALID_HANDLE;
        *timelimit = OSP_DEF_TIMELIMIT;
 
-       if ((res = osp_get_provider(provider, &p)) <= 0) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unabe to find OSP provider '%s'\n", provider);
+       if ((res = osp_get_provider(name, &provider)) <= 0) {
+               ast_debug(1, "OSP: Unabe to find OSP provider '%s'\n", name);
                return res;
        }
 
-       switch (p->authpolicy) {
-               case OSP_AUTH_NO:
-                       res = 1;
-                       break;
-               case OSP_AUTH_EXCLUSIVE:
-                       if (ast_strlen_zero(token)) {
-                               res = 0;
-                       } else if ((res = osp_create_transaction(provider, transaction, sizeof(dest), dest)) <= 0) {
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: Unable to generate transaction handle\n");
-                               *transaction = OSP_INVALID_HANDLE;
-                               res = 0;
-                       } else if((res = osp_validate_token(*transaction, source, dest, calling, called, token, timelimit)) <= 0) {
-                               OSPPTransactionRecordFailure(*transaction, OSPC_FAIL_CALL_REJECTED);
-                       }
-                       break;
-               case OSP_AUTH_YES:
-               default:
-                       if (ast_strlen_zero(token)) {
-                               res = 1;
-                       } else if ((res = osp_create_transaction(provider, transaction, sizeof(dest), dest)) <= 0) {
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: Unable to generate transaction handle\n");
-                               *transaction = OSP_INVALID_HANDLE;
-                               res = 0;
-                       } else if((res = osp_validate_token(*transaction, source, dest, calling, called, token, timelimit)) <= 0) {
-                               OSPPTransactionRecordFailure(*transaction, OSPC_FAIL_CALL_REJECTED);
-                       }
-                       break;
+       switch (provider->authpolicy) {
+       case OSP_AUTH_NO:
+               res = OSP_OK;
+               break;
+       case OSP_AUTH_EXC:
+               if (ast_strlen_zero(token)) {
+                       res = OSP_FAILED;
+               } else if ((res = osp_create_transaction(name, trans, dest, sizeof(dest))) <= 0) {
+                       ast_debug(1, "OSP: Unable to generate transaction handle\n");
+                       *trans = OSP_INVALID_HANDLE;
+                       res = OSP_FAILED;
+               } else if((res = osp_validate_token(*trans, source, dest, calling, called, token, timelimit)) <= 0) {
+                       OSPPTransactionRecordFailure(*trans, OSPC_FAIL_CALL_REJECTED);
+               }
+               break;
+       case OSP_AUTH_YES:
+       default:
+               if (ast_strlen_zero(token)) {
+                       res = OSP_OK;
+               } else if ((res = osp_create_transaction(name, trans, dest, sizeof(dest))) <= 0) {
+                       ast_debug(1, "OSP: Unable to generate transaction handle\n");
+                       *trans = OSP_INVALID_HANDLE;
+                       res = OSP_FAILED;
+               } else if((res = osp_validate_token(*trans, source, dest, calling, called, token, timelimit)) <= 0) {
+                       OSPPTransactionRecordFailure(*trans, OSPC_FAIL_CALL_REJECTED);
+               }
+               break;
        }
 
        return res;
@@ -795,25 +1383,26 @@ static int osp_auth(
 /*!
  * \brief Create a UUID
  * \param uuid UUID buffer
- * \param buffersize UUID buffer size
- * \return 1 Created, -1 Error
+ * \param bufsize UUID buffer size
+ * \return OSK_OK Created, OSP_ERROR Error
  */
 static int osp_create_uuid(
        unsigned char* uuid,
-       unsigned int* buffersize)
+       unsigned int* bufsize)
 {
        int i, res;
-       long int* tmp;
+       long int tmp[OSP_SIZE_UUID / sizeof(long int)];
 
-       if (*buffersize >= OSP_UUID_SIZE) {
-               tmp = (long int*)uuid;
-               for (i = 0; i < OSP_UUID_SIZE / sizeof(long int); i++) {
+       if ((uuid != NULL) && (*bufsize >= OSP_SIZE_UUID)) {
+               for (i = 0; i < OSP_SIZE_UUID / sizeof(long int); i++) {
                        tmp[i] = ast_random();
                }
-               *buffersize = OSP_UUID_SIZE;
-               res = 1;
+               memcpy(uuid, tmp, OSP_SIZE_UUID);
+               *bufsize = OSP_SIZE_UUID;
+               res = OSP_OK;
        } else {
-               res = -1;
+               ast_log(LOG_ERROR, "Invalid parameters\n");
+               res = OSP_ERROR;
        }
 
        return res;
@@ -823,23 +1412,26 @@ static int osp_create_uuid(
  * \brief UUID to string
  * \param uuid UUID
  * \param buffer String buffer
- * \param buffersize String buffer size
- * \return 1 Successed, -1 Error
+ * \param bufsize String buffer size
+ * \return OSP_OK Successed, OSP_ERROR Error
  */
 static int osp_uuid2str(
        unsigned char* uuid,
        char* buffer,
-       unsigned int buffersize)
+       unsigned int bufsize)
 {
        int res;
 
-       if (buffersize > OSP_UUIDSTR_SIZE) {
-               snprintf(buffer, buffersize, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+       if ((uuid != NULL) && (bufsize > OSP_SIZE_UUIDSTR)) {
+               snprintf(buffer, bufsize, "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-"
+                                         "%02hhx%02hhx-%02hhx%02hhx-"
+                                         "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
                        uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
                        uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
-               res = 1;
+               res = OSP_OK;
        } else {
-               res = -1;
+               ast_log(LOG_ERROR, "Invalid parameters\n");
+               res = OSP_ERROR;
        }
 
        return res;
@@ -849,7 +1441,7 @@ static int osp_uuid2str(
  * \brief Create a call ID according to the type
  * \param type Call ID type
  * \param callid Call ID buffer
- * \return 1 Created, 0 Not create, -1 Error
+ * \return OSK_OK Created, OSP_FAILED Not create, OSP_ERROR Error
  */
 static int osp_create_callid(
        unsigned int type,
@@ -857,20 +1449,26 @@ static int osp_create_callid(
 {
        int res;
 
-       callid->len = sizeof(callid->buf);
-       switch (type) {
-               case OSP_CALLID_H323:
-                       res = osp_create_uuid(callid->buf, &callid->len);
-                       break;
-               case OSP_CALLID_SIP:
-               case OSP_CALLID_IAX:
-                       res = 0;
-               default:
-                       res = -1;
-                       break;
+       if (callid == NULL) {
+               ast_log(LOG_ERROR, "Invalid parameters\n");
+               return OSP_ERROR;
        }
 
-       if ((res != 1) && (callid->len != 0)) {
+       callid->len = sizeof(callid->buf);
+       switch (type) {
+       case OSP_CALLID_H323:
+               res = osp_create_uuid(callid->buf, &callid->len);
+               break;
+       case OSP_CALLID_SIP:
+       case OSP_CALLID_IAX:
+               res = OSP_FAILED;
+               break;
+       default:
+               res = OSP_ERROR;
+               break;
+       }
+
+       if ((res != OSP_OK) && (callid->len != 0)) {
                callid->buf[0] = '\0';
                callid->len = 0;
        }
@@ -880,64 +1478,131 @@ static int osp_create_callid(
 
 /*!
  * \brief OSP Lookup function
- * \param provider OSP provider context name
+ * \param name OSP provider context name
+ * \param callidtypes Call ID types
+ * \param actualsrc Actual source device in indirect mode
  * \param srcdev Source device of outbound call
  * \param calling Calling number
  * \param called Called number
- * \param callidtypes Call ID types
- * \param result Lookup results
- * \return 1 Found , 0 No route, -1 Error
+ * \param snetid Source network ID
+ * \param np NP parameters
+ * \param headers SIP header parameters
+ * \param cinfo Custom info
+ * \param results Lookup results
+ * \return OSP_OK Found , OSP_FAILED No route, OSP_ERROR Error
  */
 static int osp_lookup(
-       const char* provider,
+       const char* name,
+       unsigned int callidtypes,
+       const char* actualsrc,
        const char* srcdev,
        const char* calling,
        const char* called,
-       unsigned int callidtypes,
-       struct osp_result* result)
+       const char* snetid,
+       struct osp_npdata* np,
+       struct osp_headers* headers,
+       const char* cinfo[],
+       struct osp_results* results)
 {
        int res;
-       struct osp_provider* p;
-       char source[OSP_NORSTR_SIZE];
-       char callingnum[OSP_NORSTR_SIZE];
-       char callednum[OSP_NORSTR_SIZE];
-       char destination[OSP_NORSTR_SIZE];
+       struct osp_provider* provider = NULL;
+       OSPE_PROTOCOL_NAME protocol;
+       char source[OSP_SIZE_NORSTR];
+       char callingnum[OSP_SIZE_NORSTR];
+       char callednum[OSP_SIZE_NORSTR];
+       char destination[OSP_SIZE_NORSTR];
+       char* tmp;
        unsigned int tokenlen;
-       char token[OSP_TOKSTR_SIZE];
-       char src[OSP_NORSTR_SIZE];
-       char dev[OSP_NORSTR_SIZE];
+       char token[OSP_SIZE_TOKSTR];
+       char src[OSP_SIZE_NORSTR];
+       char dev[OSP_SIZE_NORSTR];
+       char host[OSP_SIZE_NORSTR];
        unsigned int i, type;
        struct osp_callid callid;
        unsigned int callidnum;
-       OSPTCALLID* callids[OSP_CALLID_MAXNUM];
+       OSPT_CALL_ID* callids[OSP_CALLID_MAXNUM];
+       char dest[OSP_SIZE_NORSTR];
+       const char* preferred[2] = { NULL };
        unsigned int dummy = 0;
-       enum OSPEFAILREASON reason;
+       OSPEFAILREASON reason;
        int error;
 
-       result->outhandle = OSP_INVALID_HANDLE;
-       result->tech[0] = '\0';
-       result->dest[0] = '\0';
-       result->called[0] = '\0';
-       result->calling[0] = '\0';
-       result->token[0] = '\0';
-       result->networkid[0] = '\0';
-       result->numresults = 0;
-       result->outtimelimit = OSP_DEF_TIMELIMIT;
-
-       if ((res = osp_get_provider(provider, &p)) <= 0) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unabe to find OSP provider '%s'\n", provider);
+       if (results == NULL) {
+               ast_log(LOG_ERROR, "Invalid parameters\n");
+               return OSP_ERROR;
+       }
+
+       osp_convert_inout(results->dest, dest, sizeof(dest));
+
+       results->outhandle = OSP_INVALID_HANDLE;
+       results->outtech[0] = '\0';
+       results->calling[0] = '\0';
+       results->called[0] = '\0';
+       results->token[0] = '\0';
+       results->networkid[0] = '\0';
+       results->nprn[0] = '\0';
+       results->npcic[0] = '\0';
+       results->npdi = 0;
+       for (type = OSPC_OPNAME_START; type < OSPC_OPNAME_NUMBER; type++) {
+               results->opname[type][0] = '\0';
+       }
+       results->numdests = 0;
+       results->outtimelimit = OSP_DEF_TIMELIMIT;
+
+       if ((res = osp_get_provider(name, &provider)) <= 0) {
+               ast_debug(1, "OSP: Unabe to find OSP provider '%s'\n", name);
                return res;
        }
 
-       if ((res = osp_create_transaction(provider, &result->outhandle, sizeof(source), source)) <= 0) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to generate transaction handle\n");
-               result->outhandle = OSP_INVALID_HANDLE;
-               if (result->inhandle != OSP_INVALID_HANDLE) {
-                       OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
+       if ((res = osp_create_transaction(name, &results->outhandle, source, sizeof(source))) <= 0) {
+               ast_debug(1, "OSP: Unable to generate transaction handle\n");
+               results->outhandle = OSP_INVALID_HANDLE;
+               if (results->inhandle != OSP_INVALID_HANDLE) {
+                       OSPPTransactionRecordFailure(results->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
+               }
+               return OSP_ERROR;
+       }
+
+       if (!strcasecmp(results->intech, OSP_TECH_SIP)) {
+               protocol = OSPC_PROTNAME_SIP;
+       } else if (!strcasecmp(results->intech, OSP_TECH_H323)) {
+               protocol = OSPC_PROTNAME_Q931;
+       } else if (!strcasecmp(results->intech, OSP_TECH_IAX)) {
+               protocol = OSPC_PROTNAME_IAX;
+       } else if (!strcasecmp(results->intech, OSP_TECH_SKYPE)) {
+               protocol = OSPC_PROTNAME_SKYPE;
+       } else {
+               protocol = OSPC_PROTNAME_SIP;
+       }
+       OSPPTransactionSetProtocol(results->outhandle, OSPC_PROTTYPE_SOURCE, protocol);
+
+       if (!ast_strlen_zero(snetid)) {
+               OSPPTransactionSetNetworkIds(results->outhandle, snetid, "");
+       }
+
+       OSPPTransactionSetNumberPortability(results->outhandle, np->rn, np->cic, np->npdi);
+
+       for (type = OSPC_OPNAME_START; type < OSPC_OPNAME_NUMBER; type++) {
+               OSPPTransactionSetOperatorName(results->outhandle, type, np->opname[type]);
+       }
+
+    OSPPTransactionSetRemotePartyId(results->outhandle, OSPC_NFORMAT_E164, headers->rpiduser);
+    OSPPTransactionSetAssertedId(results->outhandle, OSPC_NFORMAT_E164, headers->paiuser);
+       osp_convert_inout(headers->divhost, host, sizeof(host));
+       OSPPTransactionSetDiversion(results->outhandle, headers->divuser, host);
+    OSPPTransactionSetChargeInfo(results->outhandle, OSPC_NFORMAT_E164, headers->pciuser);
+
+       if (cinfo != NULL) {
+               for (i = 0; i < OSP_MAX_CUSTOMINFO; i++) {
+                       if (!ast_strlen_zero(cinfo[i])) {
+                               OSPPTransactionSetCustomInfo(results->outhandle, i, cinfo[i]);
+                       }
                }
-               return -1;
+       }
+
+       ast_copy_string(callednum, called, sizeof(callednum));
+       if((tmp = strchr(callednum, ';')) != NULL) {
+               *tmp = '\0';
        }
 
        callidnum = 0;
@@ -953,275 +1618,291 @@ static int osp_lookup(
                }
        }
 
-       osp_convert_address(source, src, sizeof(src));
-       osp_convert_address(srcdev, dev, sizeof(dev));
-       result->numresults = OSP_DEF_DESTINATIONS;
-       error = OSPPTransactionRequestAuthorisation(
-                               result->outhandle,
-                               src,
-                               dev,
-                               calling ? calling : "",
-                               OSPC_E164,
-                               called,
-                               OSPC_E164,
-                               NULL,
-                               callidnum,
-                               callids,
-                               NULL,
-                               &result->numresults,
-                               &dummy,
-                               NULL);
+       if (provider->workmode == OSP_MODE_INDIRECT) {
+               osp_convert_inout(srcdev, src, sizeof(src));
+               if (ast_strlen_zero(actualsrc)) {
+                       osp_convert_inout(srcdev, dev, sizeof(dev));
+               } else {
+                       osp_convert_inout(actualsrc, dev, sizeof(dev));
+               }
+       } else {
+               osp_convert_inout(source, src, sizeof(src));
+               osp_convert_inout(srcdev, dev, sizeof(dev));
+       }
+
+       if (provider->srvtype == OSP_SRV_NPQUERY) {
+               OSPPTransactionSetServiceType(results->outhandle, OSPC_SERVICE_NPQUERY);
+               if (!ast_strlen_zero(dest)) {
+                       preferred[0] = dest;
+               }
+               results->numdests = 1;
+       } else {
+               OSPPTransactionSetServiceType(results->outhandle, OSPC_SERVICE_VOICE);
+               results->numdests = OSP_DEF_MAXDESTS;
+       }
+
+       error = OSPPTransactionRequestAuthorisation(results->outhandle,
+               src,
+               dev,
+               calling ? calling : "",
+               OSPC_NFORMAT_E164,
+               callednum,
+               OSPC_NFORMAT_E164,
+               NULL,
+               callidnum,
+               callids,
+               preferred,
+               &results->numdests,
+               &dummy,
+               NULL);
 
        for (i = 0; i < callidnum; i++) {
                OSPPCallIdDelete(&callids[i]);
        }
 
        if (error != OSPC_ERR_NO_ERROR) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to request authorization\n");
-               result->numresults = 0;
-               if (result->inhandle != OSP_INVALID_HANDLE) {
-                       OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
+               ast_log(LOG_WARNING, "OSP: Unable to request authorization, error '%d'\n", error);
+               results->numdests = 0;
+               if (results->inhandle != OSP_INVALID_HANDLE) {
+                       OSPPTransactionRecordFailure(results->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
                }
-               return -1;
+               return OSP_ERROR;
        }
 
-       if (!result->numresults) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: No more destination\n");
-               if (result->inhandle != OSP_INVALID_HANDLE) {
-                       OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
+       if (!results->numdests) {
+               ast_debug(1, "OSP: No more destination\n");
+               if (results->inhandle != OSP_INVALID_HANDLE) {
+                       OSPPTransactionRecordFailure(results->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
                }
-               return 0;
+               return OSP_FAILED;
        }
 
-       result->outcallid.len = sizeof(result->outcallid.buf);
+       results->outcallid.len = sizeof(results->outcallid.buf);
        tokenlen = sizeof(token);
-       error = OSPPTransactionGetFirstDestination(
-                               result->outhandle,
-                               0,
-                               NULL,
-                               NULL,
-                               &result->outtimelimit,
-                               &result->outcallid.len,
-                               result->outcallid.buf,
-                               sizeof(callednum),
-                               callednum,
-                               sizeof(callingnum),
-                               callingnum,
-                               sizeof(destination),
-                               destination,
-                               0,
-                               NULL,
-                               &tokenlen,
-                               token);
+       error = OSPPTransactionGetFirstDestination(results->outhandle,
+               0,
+               NULL,
+               NULL,
+               &results->outtimelimit,
+               &results->outcallid.len,
+               results->outcallid.buf,
+               sizeof(callednum),
+               callednum,
+               sizeof(callingnum),
+               callingnum,
+               sizeof(destination),
+               destination,
+               0,
+               NULL,
+               &tokenlen,
+               token);
        if (error != OSPC_ERR_NO_ERROR) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to get first route\n");
-               result->numresults = 0;
-               result->outtimelimit = OSP_DEF_TIMELIMIT;
-               if (result->inhandle != OSP_INVALID_HANDLE) {
-                       OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
+               ast_debug(1, "OSP: Unable to get first route, error '%d'\n", error);
+               results->numdests = 0;
+               results->outtimelimit = OSP_DEF_TIMELIMIT;
+               if (results->inhandle != OSP_INVALID_HANDLE) {
+                       OSPPTransactionRecordFailure(results->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
                }
-               return -1;
+               return OSP_ERROR;
        }
 
-       result->numresults--;
-       result->outtimelimit = osp_choose_timelimit(result->intimelimit, result->outtimelimit);
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSP: outtimelimit '%d'\n", result->outtimelimit);
-               ast_log(LOG_DEBUG, "OSP: called '%s'\n", callednum);
-               ast_log(LOG_DEBUG, "OSP: calling '%s'\n", callingnum);
-               ast_log(LOG_DEBUG, "OSP: destination '%s'\n", destination);
-               ast_log(LOG_DEBUG, "OSP: token size '%d'\n", tokenlen);
-       }
+       results->numdests--;
+       results->outtimelimit = osp_choose_timelimit(results->intimelimit, results->outtimelimit);
+       ast_debug(1, "OSP: outtimelimit '%d'\n", results->outtimelimit);
+       ast_debug(1, "OSP: calling '%s'\n", callingnum);
+       ast_debug(1, "OSP: called '%s'\n", callednum);
+       ast_debug(1, "OSP: destination '%s'\n", destination);
+       ast_debug(1, "OSP: token size '%d'\n", tokenlen);
 
-       if ((res = osp_check_destination(p, callednum, callingnum, destination, tokenlen, token, &reason, result)) > 0) {
-               return 1;
+       if ((res = osp_check_destination(provider, callingnum, callednum, destination, tokenlen, token, &reason, results)) > 0) {
+               return OSP_OK;
        }
 
-       if (!result->numresults) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: No more destination\n");
-               result->outtimelimit = OSP_DEF_TIMELIMIT;
-               OSPPTransactionRecordFailure(result->outhandle, reason);
-               if (result->inhandle != OSP_INVALID_HANDLE) {
-                       OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
+       if (!results->numdests) {
+               ast_debug(1, "OSP: No more destination\n");
+               results->outtimelimit = OSP_DEF_TIMELIMIT;
+               OSPPTransactionRecordFailure(results->outhandle, reason);
+               if (results->inhandle != OSP_INVALID_HANDLE) {
+                       OSPPTransactionRecordFailure(results->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
                }
-               return 0;
+               return OSP_FAILED;
        }
 
-       while(result->numresults) {
-               result->outcallid.len = sizeof(result->outcallid.buf);
+       while(results->numdests) {
+               results->outcallid.len = sizeof(results->outcallid.buf);
                tokenlen = sizeof(token);
-               error = OSPPTransactionGetNextDestination(
-                                       result->outhandle,
-                                       reason,
-                                       0,
-                                       NULL,
-                                       NULL,
-                                       &result->outtimelimit,
-                                       &result->outcallid.len,
-                                       result->outcallid.buf,
-                                       sizeof(callednum),
-                                       callednum,
-                                       sizeof(callingnum),
-                                       callingnum,
-                                       sizeof(destination),
-                                       destination,
-                                       0,
-                                       NULL,
-                                       &tokenlen,
-                                       token);
+               error = OSPPTransactionGetNextDestination(results->outhandle,
+                       reason,
+                       0,
+                       NULL,
+                       NULL,
+                       &results->outtimelimit,
+                       &results->outcallid.len,
+                       results->outcallid.buf,
+                       sizeof(callednum),
+                       callednum,
+                       sizeof(callingnum),
+                       callingnum,
+                       sizeof(destination),
+                       destination,
+                       0,
+                       NULL,
+                       &tokenlen,
+                       token);
                if (error == OSPC_ERR_NO_ERROR) {
-                       result->numresults--;
-                       result->outtimelimit = osp_choose_timelimit(result->intimelimit, result->outtimelimit);
-                       if (option_debug) {
-                               ast_log(LOG_DEBUG, "OSP: outtimelimit '%d'\n", result->outtimelimit);
-                               ast_log(LOG_DEBUG, "OSP: called '%s'\n", callednum);
-                               ast_log(LOG_DEBUG, "OSP: calling '%s'\n", callingnum);
-                               ast_log(LOG_DEBUG, "OSP: destination '%s'\n", destination);
-                               ast_log(LOG_DEBUG, "OSP: token size '%d'\n", tokenlen);
-                       }
-                       if ((res = osp_check_destination(p, callednum, callingnum, destination, tokenlen, token, &reason, result)) > 0) {
+                       results->numdests--;
+                       results->outtimelimit = osp_choose_timelimit(results->intimelimit, results->outtimelimit);
+                       ast_debug(1, "OSP: outtimelimit '%d'\n", results->outtimelimit);
+                       ast_debug(1, "OSP: calling '%s'\n", callingnum);
+                       ast_debug(1, "OSP: called '%s'\n", callednum);
+                       ast_debug(1, "OSP: destination '%s'\n", destination);
+                       ast_debug(1, "OSP: token size '%d'\n", tokenlen);
+
+                       if ((res = osp_check_destination(provider, callingnum, callednum, destination, tokenlen, token, &reason, results)) > 0) {
                                break;
-                       } else if (!result->numresults) {
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: No more destination\n");
-                               OSPPTransactionRecordFailure(result->outhandle, reason);
-                               if (result->inhandle != OSP_INVALID_HANDLE) {
-                                       OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
+                       } else if (!results->numdests) {
+                               ast_debug(1, "OSP: No more destination\n");
+                               OSPPTransactionRecordFailure(results->outhandle, reason);
+                               if (results->inhandle != OSP_INVALID_HANDLE) {
+                                       OSPPTransactionRecordFailure(results->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
                                }
-                               res = 0;
+                               res = OSP_FAILED;
                                break;
                        }
                } else {
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: Unable to get route, error '%d'\n", error);
-                       result->numresults = 0;
-                       result->outtimelimit = OSP_DEF_TIMELIMIT;
-                       if (result->inhandle != OSP_INVALID_HANDLE) {
-                               OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
+                       ast_debug(1, "OSP: Unable to get route, error '%d'\n", error);
+                       results->numdests = 0;
+                       results->outtimelimit = OSP_DEF_TIMELIMIT;
+                       if (results->inhandle != OSP_INVALID_HANDLE) {
+                               OSPPTransactionRecordFailure(results->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
                        }
-                       res = -1;
+                       res = OSP_ERROR;
                        break;
                }
        }
+
        return res;
 }
 
 /*!
  * \brief OSP Lookup Next function
- * \param cause Asterisk hangup cuase
- * \param result Lookup results, in/output
- * \return 1 Found , 0 No route, -1 Error
+ * \param name OSP provider name
+ * \param cause Asterisk hangup cause
+ * \param results Lookup results, in/output
+ * \return OSP_OK Found , OSP_FAILED No route, OSP_ERROR Error
  */
 static int osp_next(
-       const char* provider,
+       const char* name,
        int cause,
-       struct osp_result* result)
+       struct osp_results* results)
 {
        int res;
-       struct osp_provider* p;
-       char callingnum[OSP_NORSTR_SIZE];
-       char callednum[OSP_NORSTR_SIZE];
-       char destination[OSP_NORSTR_SIZE];
+       struct osp_provider* provider = NULL;
+       char calling[OSP_SIZE_NORSTR];
+       char called[OSP_SIZE_NORSTR];
+       char dest[OSP_SIZE_NORSTR];
        unsigned int tokenlen;
-       char token[OSP_TOKSTR_SIZE];
-       enum OSPEFAILREASON reason;
+       char token[OSP_SIZE_TOKSTR];
+       OSPEFAILREASON reason;
+       OSPE_OPERATOR_NAME type;
        int error;
 
-       result->tech[0] = '\0';
-       result->dest[0] = '\0';
-       result->called[0] = '\0';
-       result->calling[0] = '\0';
-       result->token[0] = '\0';
-       result->networkid[0] = '\0';
-       result->outtimelimit = OSP_DEF_TIMELIMIT;
-
-       if ((res = osp_get_provider(provider, &p)) <= 0) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unabe to find OSP provider '%s'\n", provider);
+       if (results == NULL) {
+               ast_log(LOG_ERROR, "Invalid parameters\n");
+               return OSP_ERROR;
+       }
+
+       results->outtech[0] = '\0';
+       results->dest[0] = '\0';
+       results->calling[0] = '\0';
+       results->called[0] = '\0';
+       results->token[0] = '\0';
+       results->networkid[0] = '\0';
+       results->nprn[0] = '\0';
+       results->npcic[0] = '\0';
+       results->npdi = 0;
+       for (type = OSPC_OPNAME_START; type < OSPC_OPNAME_NUMBER; type++) {
+               results->opname[type][0] = '\0';
+       }
+       results->outtimelimit = OSP_DEF_TIMELIMIT;
+
+       if ((res = osp_get_provider(name, &provider)) <= 0) {
+               ast_debug(1, "OSP: Unabe to find OSP provider '%s'\n", name);
                return res;
        }
 
-       if (result->outhandle == OSP_INVALID_HANDLE) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Transaction handle undefined\n");
-               result->numresults = 0;
-               if (result->inhandle != OSP_INVALID_HANDLE) {
-                       OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
+       if (results->outhandle == OSP_INVALID_HANDLE) {
+               ast_debug(1, "OSP: Transaction handle undefined\n");
+               results->numdests = 0;
+               if (results->inhandle != OSP_INVALID_HANDLE) {
+                       OSPPTransactionRecordFailure(results->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
                }
-               return -1;
+               return OSP_ERROR;
        }
 
        reason = asterisk2osp(cause);
 
-       if (!result->numresults) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: No more destination\n");
-               OSPPTransactionRecordFailure(result->outhandle, reason);
-               if (result->inhandle != OSP_INVALID_HANDLE) {
-                       OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
+       if (!results->numdests) {
+               ast_debug(1, "OSP: No more destination\n");
+               OSPPTransactionRecordFailure(results->outhandle, reason);
+               if (results->inhandle != OSP_INVALID_HANDLE) {
+                       OSPPTransactionRecordFailure(results->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
                }
-               return 0;
+               return OSP_FAILED;
        }
 
-       while(result->numresults) {
-               result->outcallid.len = sizeof(result->outcallid.buf);
+       while(results->numdests) {
+               results->outcallid.len = sizeof(results->outcallid.buf);
                tokenlen = sizeof(token);
                error = OSPPTransactionGetNextDestination(
-                                       result->outhandle,
-                                       reason,
-                                       0,
-                                       NULL,
-                                       NULL,
-                                       &result->outtimelimit,
-                                       &result->outcallid.len,
-                                       result->outcallid.buf,
-                                       sizeof(callednum),
-                                       callednum,
-                                       sizeof(callingnum),
-                                       callingnum,
-                                       sizeof(destination),
-                                       destination,
-                                       0,
-                                       NULL,
-                                       &tokenlen,
-                                       token);
+                       results->outhandle,
+                       reason,
+                       0,
+                       NULL,
+                       NULL,
+                       &results->outtimelimit,
+                       &results->outcallid.len,
+                       results->outcallid.buf,
+                       sizeof(called),
+                       called,
+                       sizeof(calling),
+                       calling,
+                       sizeof(dest),
+                       dest,
+                       0,
+                       NULL,
+                       &tokenlen,
+                       token);
                if (error == OSPC_ERR_NO_ERROR) {
-                       result->numresults--;
-                       result->outtimelimit = osp_choose_timelimit(result->intimelimit, result->outtimelimit);
-                       if (option_debug) {
-                               ast_log(LOG_DEBUG, "OSP: outtimelimit '%d'\n", result->outtimelimit);
-                               ast_log(LOG_DEBUG, "OSP: called '%s'\n", callednum);
-                               ast_log(LOG_DEBUG, "OSP: calling '%s'\n", callingnum);
-                               ast_log(LOG_DEBUG, "OSP: destination '%s'\n", destination);
-                               ast_log(LOG_DEBUG, "OSP: token size '%d'\n", tokenlen);
-                       }
-                       if ((res = osp_check_destination(p, callednum, callingnum, destination, tokenlen, token, &reason, result)) > 0) {
-                               res = 1;
+                       results->numdests--;
+                       results->outtimelimit = osp_choose_timelimit(results->intimelimit, results->outtimelimit);
+                       ast_debug(1, "OSP: outtimelimit '%d'\n", results->outtimelimit);
+                       ast_debug(1, "OSP: calling '%s'\n", calling);
+                       ast_debug(1, "OSP: called '%s'\n", called);
+                       ast_debug(1, "OSP: destination '%s'\n", dest);
+                       ast_debug(1, "OSP: token size '%d'\n", tokenlen);
+
+                       if ((res = osp_check_destination(provider, calling, called, dest, tokenlen, token, &reason, results)) > 0) {
+                               res = OSP_OK;
                                break;
-                       } else if (!result->numresults) {
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: No more destination\n");
-                               OSPPTransactionRecordFailure(result->outhandle, reason);
-                               if (result->inhandle != OSP_INVALID_HANDLE) {
-                                       OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
+                       } else if (!results->numdests) {
+                               ast_debug(1, "OSP: No more destination\n");
+                               OSPPTransactionRecordFailure(results->outhandle, reason);
+                               if (results->inhandle != OSP_INVALID_HANDLE) {
+                                       OSPPTransactionRecordFailure(results->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
                                }
-                               res = 0;
+                               res = OSP_FAILED;
                                break;
                        }
                } else {
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: Unable to get route, error '%d'\n", error);
-                       result->token[0] = '\0';
-                       result->numresults = 0;
-                       result->outtimelimit = OSP_DEF_TIMELIMIT;
-                       if (result->inhandle != OSP_INVALID_HANDLE) {
-                               OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
+                       ast_debug(1, "OSP: Unable to get route, error '%d'\n", error);
+                       results->token[0] = '\0';
+                       results->numdests = 0;
+                       results->outtimelimit = OSP_DEF_TIMELIMIT;
+                       if (results->inhandle != OSP_INVALID_HANDLE) {
+                               OSPPTransactionRecordFailure(results->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
                        }
-                       res = -1;
+                       res = OSP_ERROR;
                        break;
                }
        }
@@ -1230,69 +1911,332 @@ static int osp_next(
 }
 
 /*!
+ * \brief Get integer from variable string
+ * \param vstr Variable string
+ * \return OSP_DEF_INTSTATS Error
+ */
+static int osp_get_varint(
+       const char* vstr)
+{
+       char* tmp;
+       int value = OSP_DEF_INTSTATS;
+
+       if (!ast_strlen_zero(vstr)) {
+               if ((tmp = strchr(vstr, '=')) != NULL) {
+                       tmp++;
+                       if (sscanf(tmp, "%30d", &value) != 1) {
+                               value = OSP_DEF_INTSTATS;
+                       }
+               }
+       }
+
+       return value;
+}
+
+/*!
+ * \brief Get float from variable string
+ * \param vstr Variable string
+ * \return OSP_DEF_FLOATSTATS Error
+ */
+static float osp_get_varfloat(
+       const char* vstr)
+{
+       char* tmp;
+       float value = OSP_DEF_FLOATSTATS;
+
+       if (!ast_strlen_zero(vstr)) {
+               if ((tmp = strchr(vstr, '=')) != NULL) {
+                       tmp++;
+                       if (sscanf(tmp, "%30f", &value) != 1) {
+                               value = OSP_DEF_FLOATSTATS;
+                       }
+               }
+       }
+
+       return value;
+}
+
+/*!
+ * \brief Report QoS
+ * \param trans OSP in/outbound transaction handle
+ * \param leg Inbound/outbound
+ * \param qos QoS string
+ * \return OSP_OK Success, OSP_FAILED Failed, OSP_ERROR Error
+ */
+static int osp_report_qos(
+       int trans,
+       enum osp_callleg leg,
+       const char* qos)
+{
+       int res = OSP_FAILED;
+       enum osp_direction dir;
+       char buffer[OSP_SIZE_NORSTR];
+       char* tmp;
+       char* item;
+       int totalpackets[OSP_DIR_NUMBER];
+       struct osp_metrics lost[OSP_DIR_NUMBER];
+       struct osp_metrics jitter[OSP_DIR_NUMBER];
+       struct osp_metrics rtt;
+       int value;
+
+       if (!ast_strlen_zero(qos)) {
+               for (dir = OSP_DIR_RX; dir < OSP_DIR_NUMBER; dir++) {
+                       totalpackets[dir] = OSP_DEF_INTSTATS;
+               }
+
+               for (dir = OSP_DIR_RX; dir < OSP_DIR_NUMBER; dir++) {
+                       lost[dir].value = OSP_DEF_INTSTATS;
+                       lost[dir].min = OSP_DEF_FLOATSTATS;
+                       lost[dir].max = OSP_DEF_FLOATSTATS;
+                       lost[dir].avg = OSP_DEF_FLOATSTATS;
+                       lost[dir].sdev = OSP_DEF_FLOATSTATS;
+               }
+
+               for (dir = OSP_DIR_RX; dir < OSP_DIR_NUMBER; dir++) {
+                       jitter[dir].value = OSP_DEF_INTSTATS;
+                       jitter[dir].min = OSP_DEF_FLOATSTATS;
+                       jitter[dir].max = OSP_DEF_FLOATSTATS;
+                       jitter[dir].avg = OSP_DEF_FLOATSTATS;
+                       jitter[dir].sdev = OSP_DEF_FLOATSTATS;
+               }
+
+               rtt.value = OSP_DEF_INTSTATS;
+               rtt.min = OSP_DEF_FLOATSTATS;
+               rtt.max = OSP_DEF_FLOATSTATS;
+               rtt.avg = OSP_DEF_FLOATSTATS;
+               rtt.sdev = OSP_DEF_FLOATSTATS;
+
+               ast_copy_string(buffer, qos, sizeof(buffer));
+               for (item = strtok_r(buffer, ";", &tmp); item; item = strtok_r(NULL, ";", &tmp)) {
+                       if (!strncasecmp(item, "rxcount", strlen("rxcount"))) {
+                               totalpackets[OSP_DIR_RX] = osp_get_varint(item);
+                       } else if (!strncasecmp(item, "txcount", strlen("txcount"))) {
+                               totalpackets[OSP_DIR_TX] = osp_get_varint(item);
+                       } else if (!strncasecmp(item, "lp", strlen("lp"))) {
+                               lost[OSP_DIR_RX].value = osp_get_varint(item);
+                       } else if (!strncasecmp(item, "minrxlost", strlen("minrxlost"))) {
+                               lost[OSP_DIR_RX].min = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "maxrxlost", strlen("maxrxlost"))) {
+                               lost[OSP_DIR_RX].max = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "avgrxlost", strlen("avgrxlost"))) {
+                               lost[OSP_DIR_RX].avg = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "stdevrxlost", strlen("stdevrxlost"))) {
+                               lost[OSP_DIR_RX].sdev = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "rlp", strlen("rlp"))) {
+                               lost[OSP_DIR_TX].value = osp_get_varint(item);
+                       } else if (!strncasecmp(item, "reported_minlost", strlen("reported_minlost"))) {
+                               lost[OSP_DIR_TX].min = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "reported_maxlost", strlen("reported_maxlost"))) {
+                               lost[OSP_DIR_TX].max = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "reported_avglost", strlen("reported_avglost"))) {
+                               lost[OSP_DIR_TX].avg = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "reported_stdevlost", strlen("reported_stdevlost"))) {
+                               lost[OSP_DIR_TX].sdev = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "rxjitter", strlen("rxjitter"))) {
+                               jitter[OSP_DIR_RX].value = osp_get_varint(item);
+                       } else if (!strncasecmp(item, "minrxjitter", strlen("minrxjitter"))) {
+                               jitter[OSP_DIR_RX].min = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "maxrxjitter", strlen("maxrxjitter"))) {
+                               jitter[OSP_DIR_RX].max = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "avgrxjitter", strlen("avgjitter"))) {
+                               jitter[OSP_DIR_RX].avg = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "stdevrxjitter", strlen("stdevjitter"))) {
+                               jitter[OSP_DIR_RX].sdev = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "txjitter", strlen("txjitter"))) {
+                               jitter[OSP_DIR_TX].value = osp_get_varint(item);
+                       } else if (!strncasecmp(item, "reported_minjitter", strlen("reported_minjitter"))) {
+                               jitter[OSP_DIR_TX].min = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "reported_maxjitter", strlen("reported_maxjitter"))) {
+                               jitter[OSP_DIR_TX].max = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "reported_avgjitter", strlen("reported_avgjitter"))) {
+                               jitter[OSP_DIR_TX].avg = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "reported_stdevjitter", strlen("reported_stdevjitter"))) {
+                               jitter[OSP_DIR_TX].sdev = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "rtt", strlen("rtt"))) {
+                               rtt.value = osp_get_varint(item);
+                       } else if (!strncasecmp(item, "minrtt", strlen("minrtt"))) {
+                               rtt.min = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "maxrtt", strlen("maxrtt"))) {
+                               rtt.max = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "avgrtt", strlen("avgrtt"))) {
+                               rtt.avg = osp_get_varfloat(item);
+                       } else if (!strncasecmp(item, "stdevrtt", strlen("stdevrtt"))) {
+                               rtt.sdev = osp_get_varfloat(item);
+                       }
+               }
+
+               ast_debug(1, "OSP: call leg '%d'\n", leg);
+               ast_debug(1, "OSP: rxcount '%d'\n", totalpackets[OSP_DIR_RX]);
+               ast_debug(1, "OSP: txcount '%d'\n", totalpackets[OSP_DIR_TX]);
+               ast_debug(1, "OSP: lp '%d'\n",lost[OSP_DIR_RX].value);
+               ast_debug(1, "OSP: minrxlost '%f'\n", lost[OSP_DIR_RX].min);
+               ast_debug(1, "OSP: maxrxlost '%f'\n", lost[OSP_DIR_RX].max);
+               ast_debug(1, "OSP: avgrxlost '%f'\n", lost[OSP_DIR_RX].avg);
+               ast_debug(1, "OSP: stdevrxlost '%f'\n", lost[OSP_DIR_RX].sdev);
+               ast_debug(1, "OSP: rlp '%d'\n", lost[OSP_DIR_TX].value);
+               ast_debug(1, "OSP: reported_minlost '%f'\n", lost[OSP_DIR_TX].min);
+               ast_debug(1, "OSP: reported_maxlost '%f'\n", lost[OSP_DIR_TX].max);
+               ast_debug(1, "OSP: reported_avglost '%f'\n", lost[OSP_DIR_TX].avg);
+               ast_debug(1, "OSP: reported_stdevlost '%f'\n", lost[OSP_DIR_TX].sdev);
+               ast_debug(1, "OSP: rxjitter '%d'\n", jitter[OSP_DIR_RX].value);
+               ast_debug(1, "OSP: minrxjitter '%f'\n", jitter[OSP_DIR_RX].min);
+               ast_debug(1, "OSP: maxrxjitter '%f'\n", jitter[OSP_DIR_RX].max);
+               ast_debug(1, "OSP: avgrxjitter '%f'\n", jitter[OSP_DIR_RX].avg);
+               ast_debug(1, "OSP: stdevrxjitter '%f'\n", jitter[OSP_DIR_RX].sdev);
+               ast_debug(1, "OSP: txjitter '%d'\n", jitter[OSP_DIR_TX].value);
+               ast_debug(1, "OSP: reported_minjitter '%f'\n", jitter[OSP_DIR_TX].min);
+               ast_debug(1, "OSP: reported_maxjitter '%f'\n", jitter[OSP_DIR_TX].max);
+               ast_debug(1, "OSP: reported_avgjitter '%f'\n", jitter[OSP_DIR_TX].avg);
+               ast_debug(1, "OSP: reported_stdevjitter '%f'\n", jitter[OSP_DIR_TX].sdev);
+               ast_debug(1, "OSP: rtt '%d'\n", rtt.value);
+               ast_debug(1, "OSP: minrtt '%f'\n", rtt.min);
+               ast_debug(1, "OSP: maxrtt '%f'\n", rtt.max);
+               ast_debug(1, "OSP: avgrtt '%f'\n", rtt.avg);
+               ast_debug(1, "OSP: stdevrtt '%f'\n", rtt.sdev);
+
+               if (leg == OSP_CALL_INBOUND) {
+                       OSPPTransactionSetPackets(trans, OSPC_SMETRIC_RTP, OSPC_SDIR_SRCREP, totalpackets[OSP_DIR_RX]);
+                       OSPPTransactionSetPackets(trans, OSPC_SMETRIC_RTCP, OSPC_SDIR_DESTREP, totalpackets[OSP_DIR_TX]);
+                       if (lost[OSP_DIR_RX].value >= 0) {
+                               value = lost[OSP_DIR_RX].value;
+                       } else {
+                               value = (int)lost[OSP_DIR_RX].avg;
+                       }
+                       OSPPTransactionSetLost(trans, OSPC_SMETRIC_RTP, OSPC_SDIR_SRCREP, value, OSP_DEF_INTSTATS);
+                       if (lost[OSP_DIR_TX].value >= 0) {
+                               value = lost[OSP_DIR_TX].value;
+                       } else {
+                               value = (int)lost[OSP_DIR_TX].avg;
+                       }
+                       OSPPTransactionSetLost(trans, OSPC_SMETRIC_RTCP, OSPC_SDIR_DESTREP, value, OSP_DEF_INTSTATS);
+                       if (jitter[OSP_DIR_RX].value >= 0) {
+                               value = jitter[OSP_DIR_RX].value;
+                       } else {
+                               value = (int)jitter[OSP_DIR_RX].avg;
+                       }
+                       OSPPTransactionSetJitter(trans,
+                               OSPC_SMETRIC_RTP,
+                               OSPC_SDIR_SRCREP,
+                               OSP_DEF_INTSTATS,
+                               (int)jitter[OSP_DIR_RX].min,
+                               (int)jitter[OSP_DIR_RX].max,
+                               value, jitter[OSP_DIR_RX].sdev);
+                       if (jitter[OSP_DIR_TX].value >= 0) {
+                               value = jitter[OSP_DIR_TX].value;
+                       } else {
+                               value = (int)jitter[OSP_DIR_TX].avg;
+                       }
+                       OSPPTransactionSetJitter(trans, OSPC_SMETRIC_RTCP, OSPC_SDIR_DESTREP,
+                               OSP_DEF_INTSTATS, (int)jitter[OSP_DIR_TX].min, (int)jitter[OSP_DIR_TX].max, value, jitter[OSP_DIR_TX].sdev);
+               } else {
+                       OSPPTransactionSetPackets(trans, OSPC_SMETRIC_RTP, OSPC_SDIR_DESTREP, totalpackets[OSP_DIR_RX]);
+                       OSPPTransactionSetPackets(trans, OSPC_SMETRIC_RTCP, OSPC_SDIR_SRCREP, totalpackets[OSP_DIR_TX]);
+                       OSPPTransactionSetLost(trans, OSPC_SMETRIC_RTP, OSPC_SDIR_DESTREP, lost[OSP_DIR_RX].value, OSP_DEF_INTSTATS);
+                       OSPPTransactionSetLost(trans, OSPC_SMETRIC_RTCP, OSPC_SDIR_SRCREP, lost[OSP_DIR_TX].value, OSP_DEF_INTSTATS);
+                       if (jitter[OSP_DIR_RX].value >= 0) {
+                               value = jitter[OSP_DIR_RX].value;
+                       } else {
+                               value = (int)jitter[OSP_DIR_RX].avg;
+                       }
+                       OSPPTransactionSetJitter(trans,
+                               OSPC_SMETRIC_RTP,
+                               OSPC_SDIR_DESTREP,
+                               OSP_DEF_INTSTATS,
+                               (int)jitter[OSP_DIR_RX].min,
+                               (int)jitter[OSP_DIR_RX].max,
+                               value,
+                               jitter[OSP_DIR_RX].sdev);
+                       if (jitter[OSP_DIR_TX].value >= 0) {
+                               value = jitter[OSP_DIR_TX].value;
+                       } else {
+                               value = (int)jitter[OSP_DIR_TX].avg;
+                       }
+                       OSPPTransactionSetJitter(trans,
+                               OSPC_SMETRIC_RTCP,
+                               OSPC_SDIR_SRCREP,
+                               OSP_DEF_INTSTATS,
+                               (int)jitter[OSP_DIR_TX].min,
+                               (int)jitter[OSP_DIR_TX].max,
+                               value,
+                               jitter[OSP_DIR_TX].sdev);
+               }
+
+               res = OSP_OK;
+       }
+
+       return res;
+}
+
+/*!
  * \brief OSP Finish function
- * \param handle OSP in/outbound transaction handle
+ * \param trans OSP in/outbound transaction handle
  * \param recorded If failure reason has been recorded
  * \param cause Asterisk hangup cause
  * \param start Call start time
  * \param connect Call connect time
  * \param end Call end time
  * \param release Who release first, 0 source, 1 destination
- * \return 1 Success, 0 Failed, -1 Error
+ * \param inqos Inbound QoS string
+ * \param outqos Outbound QoS string
+ * \return OSP_OK Success, OSP_FAILED Failed, OSP_ERROR Error
  */
 static int osp_finish(
-       int handle,
+       int trans,
        int recorded,
        int cause,
        time_t start,
        time_t connect,
        time_t end,
-       unsigned int release)
+       unsigned int release,
+       const char* inqos,
+       const char* outqos)
 {
        int res;
-       enum OSPEFAILREASON reason;
+       OSPEFAILREASON reason;
        time_t alert = 0;
        unsigned isPddInfoPresent = 0;
        unsigned pdd = 0;
        unsigned int dummy = 0;
        int error;
 
-       if (handle == OSP_INVALID_HANDLE) {
-               return 0;
+       if (trans == OSP_INVALID_HANDLE) {
+               return OSP_FAILED;
        }
 
+       OSPPTransactionSetRoleInfo(trans, OSPC_RSTATE_STOP, OSPC_RFORMAT_OSP, OSPC_RVENDOR_ASTERISK);
+
        if (!recorded) {
                reason = asterisk2osp(cause);
-               OSPPTransactionRecordFailure(handle, reason);
-       }
-
-       error = OSPPTransactionReportUsage(
-                               handle,
-                               difftime(end, connect),
-                               start,
-                               end,
-                               alert,
-                               connect,
-                               isPddInfoPresent,
-                               pdd,
-                               release,
-                               (unsigned char*)"",
-                               0,
-                               0,
-                               0,
-                               0,
-                               &dummy,
-                               NULL);
+               OSPPTransactionRecordFailure(trans, reason);
+       }
+
+       osp_report_qos(trans, OSP_CALL_INBOUND, inqos);
+       osp_report_qos(trans, OSP_CALL_OUTBOUND, outqos);
+
+       error = OSPPTransactionReportUsage(trans,
+               difftime(end, connect),
+               start,
+               end,
+               alert,
+               connect,
+               isPddInfoPresent,
+               pdd,
+               release,
+               NULL,
+               OSP_DEF_INTSTATS,
+               OSP_DEF_INTSTATS,
+               OSP_DEF_INTSTATS,
+               OSP_DEF_INTSTATS,
+               &dummy,
+               NULL);
        if (error == OSPC_ERR_NO_ERROR) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Usage reported\n");
-               res = 1;
+               ast_debug(1, "OSP: Usage reported\n");
+               res = OSP_OK;
        } else {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to report usage, error '%d'\n", error);
-               res = -1;
+               ast_debug(1, "OSP: Unable to report usage, error '%d'\n", error);
+               res = OSP_ERROR;
        }
-       OSPPTransactionDelete(handle);
+       OSPPTransactionDelete(trans);
 
        return res;
 }
@@ -1303,23 +2247,21 @@ static int osp_finish(
  * \brief OSP Application OSPAuth
  * \param chan Channel
  * \param data Parameter
- * \return 0 Success, -1 Failed
+ * \return OSP_AST_OK Success, OSP_AST_ERROR Error
  */
 static int ospauth_exec(
-       struct ast_channel* chan,
-       void* data)
+       struct ast_channel *chan,
+       const char *data)
 {
        int res;
-       struct ast_module_user* u;
        const char* provider = OSP_DEF_PROVIDER;
-       int priority_jump = 0;
        struct varshead* headp;
        struct ast_var_t* current;
        const char* source = "";
        const char* token = "";
        int handle;
        unsigned int timelimit;
-       char buffer[OSP_INTSTR_SIZE];
+       char buffer[OSP_SIZE_INTSTR];
        const char* status;
        char* tmp;
 
@@ -1328,43 +2270,31 @@ static int ospauth_exec(
                AST_APP_ARG(options);
        );
 
-       u = ast_module_user_add(chan);
-
-       if (!(tmp = ast_strdupa(data))) {
-               ast_log(LOG_ERROR, "Out of memory\n");
-               ast_module_user_remove(u);
-               return -1;
-       }
+       tmp = ast_strdupa(data);
 
        AST_STANDARD_APP_ARGS(args, tmp);
 
        if (!ast_strlen_zero(args.provider)) {
                provider = args.provider;
        }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPAuth: provider '%s'\n", provider);
-
-       if ((args.options) && (strchr(args.options, 'j'))) {
-               priority_jump = 1;
-       }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPAuth: priority jump '%d'\n", priority_jump);
+       ast_debug(1, "OSPAuth: provider '%s'\n", provider);
 
-       headp = &chan->varshead;
+       headp = ast_channel_varshead(chan);
        AST_LIST_TRAVERSE(headp, current, entries) {
-               if (!strcasecmp(ast_var_name(current), "OSPPEERIP")) {
+               if (!strcmp(ast_var_name(current), "OSPINPEERIP")) {
                        source = ast_var_value(current);
-               } else if (!strcasecmp(ast_var_name(current), "OSPINTOKEN")) {
+               } else if (!strcmp(ast_var_name(current), "OSPINTOKEN")) {
                        token = ast_var_value(current);
                }
        }
 
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSPAuth: source '%s'\n", source);
-               ast_log(LOG_DEBUG, "OSPAuth: token size '%zd'\n", strlen(token));
-       }
+       ast_debug(1, "OSPAuth: source '%s'\n", source);
+       ast_debug(1, "OSPAuth: token size '%zd'\n", strlen(token));
 
-       if ((res = osp_auth(provider, &handle, source, chan->cid.cid_num, chan->exten, token, &timelimit)) > 0) {
+       res = osp_auth(provider, &handle, source,
+               S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL),
+               ast_channel_exten(chan), token, &timelimit);
+       if (res > 0) {
                status = AST_OSP_SUCCESS;
        } else {
                timelimit = OSP_DEF_TIMELIMIT;
@@ -1377,29 +2307,19 @@ static int ospauth_exec(
 
        snprintf(buffer, sizeof(buffer), "%d", handle);
        pbx_builtin_setvar_helper(chan, "OSPINHANDLE", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPAuth: OSPINHANDLE '%s'\n", buffer);
+       ast_debug(1, "OSPAuth: OSPINHANDLE '%s'\n", buffer);
        snprintf(buffer, sizeof(buffer), "%d", timelimit);
        pbx_builtin_setvar_helper(chan, "OSPINTIMELIMIT", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPAuth: OSPINTIMELIMIT '%s'\n", buffer);
+       ast_debug(1, "OSPAuth: OSPINTIMELIMIT '%s'\n", buffer);
        pbx_builtin_setvar_helper(chan, "OSPAUTHSTATUS", status);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPAuth: %s\n", status);
+       ast_debug(1, "OSPAuth: %s\n", status);
 
-       if(res <= 0) {
-               if (priority_jump || ast_opt_priority_jumping) {
-                       ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
-                       res = 0;
-               } else {
-                       res = -1;
-               }
+       if(res != OSP_OK) {
+               res = OSP_AST_ERROR;
        } else {
-               res = 0;
+               res = OSP_AST_OK;
        }
 
-       ast_module_user_remove(u);
-
        return res;
 }
 
@@ -1407,23 +2327,27 @@ static int ospauth_exec(
  * \brief OSP Application OSPLookup
  * \param chan Channel
  * \param data Parameter
- * \return 0 Success, -1 Failed
+ * \return OSP_AST_OK Success, OSP_AST_ERROR Error
  */
 static int osplookup_exec(
        struct ast_channel* chan,
-       void* data)
+       const char * data)
 {
-       int res, cres;
-       struct ast_module_user* u;
+       int res;
        const char* provider = OSP_DEF_PROVIDER;
-       int priority_jump = 0;
+       unsigned int callidtypes = OSP_CALLID_UNDEF;
        struct varshead* headp;
        struct ast_var_t* current;
+       const char* actualsrc = "";
        const char* srcdev = "";
-       const char* netid = "";
-       char buffer[OSP_TOKSTR_SIZE];
-       unsigned int callidtypes = OSP_CALLID_UNDEFINED;
-       struct osp_result result;
+       const char* snetid = "";
+       struct osp_npdata np;
+       OSPE_OPERATOR_NAME type;
+       struct osp_headers headers;
+       unsigned int i;
+       const char* cinfo[OSP_MAX_CUSTOMINFO] = { NULL };
+       char buffer[OSP_SIZE_TOKSTR + strlen(": ") + strlen(OSP_SIP_HEADER)];
+       struct osp_results results;
        const char* status;
        char* tmp;
 
@@ -1434,33 +2358,22 @@ static int osplookup_exec(
        );
 
        if (ast_strlen_zero(data)) {
-               ast_log(LOG_WARNING, "OSPLookup: Arg required, OSPLookup(exten[|provider[|options]])\n");
-               return -1;
+               ast_log(LOG_WARNING, "OSPLookup: Arg required, OSPLookup(exten[,provider[,options]])\n");
+               return OSP_AST_ERROR;
        }
 
-       u = ast_module_user_add(chan);
-
-       if (!(tmp = ast_strdupa(data))) {
-               ast_log(LOG_ERROR, "Out of memory\n");
-               ast_module_user_remove(u);
-               return -1;
-       }
+       tmp = ast_strdupa(data);
 
        AST_STANDARD_APP_ARGS(args, tmp);
 
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: exten '%s'\n", args.exten);
+       ast_debug(1, "OSPLookup: exten '%s'\n", args.exten);
 
        if (!ast_strlen_zero(args.provider)) {
                provider = args.provider;
        }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPlookup: provider '%s'\n", provider);
+       ast_debug(1, "OSPlookup: provider '%s'\n", provider);
 
        if (args.options) {
-               if (strchr(args.options, 'j')) {
-                       priority_jump = 1;
-               }
                if (strchr(args.options, 'h')) {
                        callidtypes |= OSP_CALLID_H323;
                }
@@ -1471,55 +2384,146 @@ static int osplookup_exec(
                        callidtypes |= OSP_CALLID_IAX;
                }
        }
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSPLookup: priority jump '%d'\n", priority_jump);
-               ast_log(LOG_DEBUG, "OSPLookup: call id types '%d'\n", callidtypes);
+       ast_debug(1, "OSPLookup: call id types '%d'\n", callidtypes);
+
+       results.inhandle = OSP_INVALID_HANDLE;
+       results.intimelimit = OSP_DEF_TIMELIMIT;
+       results.dest[0] = '\0';
+
+       np.rn = "";
+       np.cic = "";
+       np.npdi = 0;
+       for (type = OSPC_OPNAME_START; type < OSPC_OPNAME_NUMBER; type++) {
+               np.opname[type] = "";
        }
 
-       result.inhandle = OSP_INVALID_HANDLE;
-       result.intimelimit = OSP_DEF_TIMELIMIT;
+       headers.rpiduser = "";
+       headers.paiuser = "";
+       headers.divuser = "";
+       headers.divhost = "";
+       headers.pciuser = "";
 
-       headp = &chan->varshead;
+       headp = ast_channel_varshead(chan);
        AST_LIST_TRAVERSE(headp, current, entries) {
-               if (!strcasecmp(ast_var_name(current), "OSPINHANDLE")) {
-                       if (sscanf(ast_var_value(current), "%d", &result.inhandle) != 1) {
-                               result.inhandle = OSP_INVALID_HANDLE;
+               if (!strcmp(ast_var_name(current), "OSPINACTUALSRC")) {
+                       actualsrc = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINPEERIP")) {
+                       srcdev = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINTECH")) {
+                       ast_copy_string(results.intech, ast_var_value(current), sizeof(results.intech));
+               } else if (!strcmp(ast_var_name(current), "OSPINHANDLE")) {
+                       if (sscanf(ast_var_value(current), "%30d", &results.inhandle) != 1) {
+                               results.inhandle = OSP_INVALID_HANDLE;
                        }
-               } else if (!strcasecmp(ast_var_name(current), "OSPINTIMELIMIT")) {
-                       if (sscanf(ast_var_value(current), "%d", &result.intimelimit) != 1) {
-                               result.intimelimit = OSP_DEF_TIMELIMIT;
+               } else if (!strcmp(ast_var_name(current), "OSPINTIMELIMIT")) {
+                       if (sscanf(ast_var_value(current), "%30d", &results.intimelimit) != 1) {
+                               results.intimelimit = OSP_DEF_TIMELIMIT;
                        }
-               } else if (!strcasecmp(ast_var_name(current), "OSPINNETWORKID")) {
-                       netid = ast_var_value(current);
-               } else if (!strcasecmp(ast_var_name(current), "OSPPEERIP")) {
-                       srcdev = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINNETWORKID")) {
+                       snetid = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINNPRN")) {
+                       np.rn = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINNPCIC")) {
+                       np.cic = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINNPDI")) {
+                       if (ast_true(ast_var_value(current))) {
+                               np.npdi = 1;
+                       }
+               } else if (!strcmp(ast_var_name(current), "OSPINSPID")) {
+                       np.opname[OSPC_OPNAME_SPID] = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINOCN")) {
+                       np.opname[OSPC_OPNAME_OCN] = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINSPN")) {
+                       np.opname[OSPC_OPNAME_SPN] = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINALTSPN")) {
+                       np.opname[OSPC_OPNAME_ALTSPN] = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINMCC")) {
+                       np.opname[OSPC_OPNAME_MCC] = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINMNC")) {
+                       np.opname[OSPC_OPNAME_MNC] = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINTOHOST")) {
+                       ast_copy_string(results.dest, ast_var_value(current), sizeof(results.dest));
+               } else if (!strcmp(ast_var_name(current), "OSPINRPIDUSER")) {
+                       headers.rpiduser = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINPAIUSER")) {
+                       headers.paiuser = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINDIVUSER")) {
+                       headers.divuser = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINDIVHOST")) {
+                       headers.divhost = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINPCIUSER")) {
+                       headers.pciuser = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINCUSTOMINFO1")) {
+                       cinfo[0] = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINCUSTOMINFO2")) {
+                       cinfo[1] = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINCUSTOMINFO3")) {
+                       cinfo[2] = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINCUSTOMINFO4")) {
+                       cinfo[3] = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINCUSTOMINFO5")) {
+                       cinfo[4] = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINCUSTOMINFO6")) {
+                       cinfo[5] = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINCUSTOMINFO7")) {
+                       cinfo[6] = ast_var_value(current);
+               } else if (!strcmp(ast_var_name(current), "OSPINCUSTOMINFO8")) {
+                       cinfo[7] = ast_var_value(current);
                }
        }
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSPLookup: OSPINHANDLE '%d'\n", result.inhandle);
-               ast_log(LOG_DEBUG, "OSPLookup: OSPINTIMELIMIT '%d'\n", result.intimelimit);
-               ast_log(LOG_DEBUG, "OSPLookup: OSPINNETWORKID '%s'\n", netid);
-               ast_log(LOG_DEBUG, "OSPLookup: source device '%s'\n", srcdev);
+       ast_debug(1, "OSPLookup: actual source device '%s'\n", actualsrc);
+       ast_debug(1, "OSPLookup: source device '%s'\n", srcdev);
+       ast_debug(1, "OSPLookup: OSPINTECH '%s'\n", results.intech);
+       ast_debug(1, "OSPLookup: OSPINHANDLE '%d'\n", results.inhandle);
+       ast_debug(1, "OSPLookup: OSPINTIMELIMIT '%d'\n", results.intimelimit);
+       ast_debug(1, "OSPLookup: OSPINNETWORKID '%s'\n", snetid);
+       ast_debug(1, "OSPLookup: OSPINNPRN '%s'\n", np.rn);
+       ast_debug(1, "OSPLookup: OSPINNPCIC '%s'\n", np.cic);
+       ast_debug(1, "OSPLookup: OSPINNPDI '%d'\n", np.npdi);
+       ast_debug(1, "OSPLookup: OSPINSPID '%s'\n", np.opname[OSPC_OPNAME_SPID]);
+       ast_debug(1, "OSPLookup: OSPINOCN '%s'\n", np.opname[OSPC_OPNAME_OCN]);
+       ast_debug(1, "OSPLookup: OSPINSPN '%s'\n", np.opname[OSPC_OPNAME_SPN]);
+       ast_debug(1, "OSPLookup: OSPINALTSPN '%s'\n", np.opname[OSPC_OPNAME_ALTSPN]);
+       ast_debug(1, "OSPLookup: OSPINMCC '%s'\n", np.opname[OSPC_OPNAME_MCC]);
+       ast_debug(1, "OSPLookup: OSPINMNC '%s'\n", np.opname[OSPC_OPNAME_MNC]);
+       ast_debug(1, "OSPLookup: OSPINTOHOST '%s'\n", results.dest);
+       ast_debug(1, "OSPLookup: OSPINRPIDUSER '%s'\n", headers.rpiduser);
+       ast_debug(1, "OSPLookup: OSPINPAIUSER '%s'\n", headers.paiuser);
+       ast_debug(1, "OSPLookup: OSPINDIVUSER '%s'\n", headers.divuser);
+       ast_debug(1, "OSPLookup: OSPINDIVHOST'%s'\n", headers.divhost);
+       ast_debug(1, "OSPLookup: OSPINPCIUSER '%s'\n", headers.pciuser);
+       for (i = 0; i < OSP_MAX_CUSTOMINFO; i++) {
+               if (!ast_strlen_zero(cinfo[i])) {
+                       ast_debug(1, "OSPLookup: OSPINCUSTOMINFO%d '%s'\n", i, cinfo[i]);
+               }
        }
 
-       if ((cres = ast_autoservice_start(chan)) < 0) {
-               ast_module_user_remove(u);
-               return -1;
+       if (ast_autoservice_start(chan) < 0) {
+               return OSP_AST_ERROR;
        }
 
-       if ((res = osp_lookup(provider, srcdev, chan->cid.cid_num, args.exten, callidtypes, &result)) > 0) {
+       res = osp_lookup(provider, callidtypes, actualsrc, srcdev,
+               S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL),
+               args.exten, snetid, &np, &headers, cinfo, &results);
+       if (res > 0) {
                status = AST_OSP_SUCCESS;
        } else {
-               result.tech[0] = '\0';
-               result.dest[0] = '\0';
-               result.called[0] = '\0';
-               result.calling[0] = '\0';
-               result.token[0] = '\0';
-               result.networkid[0] = '\0';
-               result.numresults = 0;
-               result.outtimelimit = OSP_DEF_TIMELIMIT;
-               result.outcallid.buf[0] = '\0';
-               result.outcallid.len = 0;
+               results.outtech[0] = '\0';
+               results.dest[0] = '\0';
+               results.calling[0] = '\0';
+               results.called[0] = '\0';
+               results.token[0] = '\0';
+               results.networkid[0] = '\0';
+               results.nprn[0] = '\0';
+               results.npcic[0] = '\0';
+               results.npdi = 0;
+               for (type = OSPC_OPNAME_START; type < OSPC_OPNAME_NUMBER; type++) {
+                       results.opname[type][0] = '\0';
+               }
+               results.numdests = 0;
+               results.outtimelimit = OSP_DEF_TIMELIMIT;
+               results.outcallid.buf[0] = '\0';
+               results.outcallid.len = 0;
                if (!res) {
                        status = AST_OSP_FAILED;
                } else {
@@ -1527,82 +2531,87 @@ static int osplookup_exec(
                }
        }
 
-       snprintf(buffer, sizeof(buffer), "%d", result.outhandle);
+       snprintf(buffer, sizeof(buffer), "%d", results.outhandle);
        pbx_builtin_setvar_helper(chan, "OSPOUTHANDLE", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPOUTHANDLE '%s'\n", buffer);
-       pbx_builtin_setvar_helper(chan, "OSPTECH", result.tech);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPTECH '%s'\n", result.tech);
-       pbx_builtin_setvar_helper(chan, "OSPDEST", result.dest);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPDEST '%s'\n", result.dest);
-       pbx_builtin_setvar_helper(chan, "OSPCALLED", result.called);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPCALLED '%s'\n", result.called);
-       pbx_builtin_setvar_helper(chan, "OSPCALLING", result.calling);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPCALLING '%s'\n", result.calling);
-       pbx_builtin_setvar_helper(chan, "OSPOUTTOKEN", result.token);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPOUTTOKEN size '%zd'\n", strlen(result.token));
-       snprintf(buffer, sizeof(buffer), "%d", result.numresults);
-       pbx_builtin_setvar_helper(chan, "OSPRESULTS", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPRESULTS '%s'\n", buffer);
-       snprintf(buffer, sizeof(buffer), "%d", result.outtimelimit);
+       ast_debug(1, "OSPLookup: OSPOUTHANDLE '%s'\n", buffer);
+       pbx_builtin_setvar_helper(chan, "OSPOUTTECH", results.outtech);
+       ast_debug(1, "OSPLookup: OSPOUTTECH '%s'\n", results.outtech);
+       pbx_builtin_setvar_helper(chan, "OSPDESTINATION", results.dest);
+       ast_debug(1, "OSPLookup: OSPDESTINATION '%s'\n", results.dest);
+       pbx_builtin_setvar_helper(chan, "OSPOUTCALLING", results.calling);
+       ast_debug(1, "OSPLookup: OSPOUTCALLING '%s'\n", results.calling);
+       pbx_builtin_setvar_helper(chan, "OSPOUTCALLED", results.called);
+       ast_debug(1, "OSPLookup: OSPOUTCALLED '%s'\n", results.called);
+       pbx_builtin_setvar_helper(chan, "OSPOUTNETWORKID", results.networkid);
+       ast_debug(1, "OSPLookup: OSPOUTNETWORKID '%s'\n", results.networkid);
+       pbx_builtin_setvar_helper(chan, "OSPOUTNPRN", results.nprn);
+       ast_debug(1, "OSPLookup: OSPOUTNPRN '%s'\n", results.nprn);
+       pbx_builtin_setvar_helper(chan, "OSPOUTNPCIC", results.npcic);
+       ast_debug(1, "OSPLookup: OSPOUTNPCIC '%s'\n", results.npcic);
+       snprintf(buffer, sizeof(buffer), "%d", results.npdi);
+       pbx_builtin_setvar_helper(chan, "OSPOUTNPDI", buffer);
+       ast_debug(1, "OSPLookup: OSPOUTNPDI'%s'\n", buffer);
+       pbx_builtin_setvar_helper(chan, "OSPOUTSPID", results.opname[OSPC_OPNAME_SPID]);
+       ast_debug(1, "OSPLookup: OSPOUTSPID '%s'\n", results.opname[OSPC_OPNAME_SPID]);
+       pbx_builtin_setvar_helper(chan, "OSPOUTOCN", results.opname[OSPC_OPNAME_OCN]);
+       ast_debug(1, "OSPLookup: OSPOUTOCN '%s'\n", results.opname[OSPC_OPNAME_OCN]);
+       pbx_builtin_setvar_helper(chan, "OSPOUTSPN", results.opname[OSPC_OPNAME_SPN]);
+       ast_debug(1, "OSPLookup: OSPOUTSPN '%s'\n", results.opname[OSPC_OPNAME_SPN]);
+       pbx_builtin_setvar_helper(chan, "OSPOUTALTSPN", results.opname[OSPC_OPNAME_ALTSPN]);
+       ast_debug(1, "OSPLookup: OSPOUTALTSPN '%s'\n", results.opname[OSPC_OPNAME_ALTSPN]);
+       pbx_builtin_setvar_helper(chan, "OSPOUTMCC", results.opname[OSPC_OPNAME_MCC]);
+       ast_debug(1, "OSPLookup: OSPOUTMCC '%s'\n", results.opname[OSPC_OPNAME_MCC]);
+       pbx_builtin_setvar_helper(chan, "OSPOUTMNC", results.opname[OSPC_OPNAME_MNC]);
+       ast_debug(1, "OSPLookup: OSPOUTMNC '%s'\n", results.opname[OSPC_OPNAME_MNC]);
+       pbx_builtin_setvar_helper(chan, "OSPOUTTOKEN", results.token);
+       ast_debug(1, "OSPLookup: OSPOUTTOKEN size '%zd'\n", strlen(results.token));
+       snprintf(buffer, sizeof(buffer), "%d", results.numdests);
+       pbx_builtin_setvar_helper(chan, "OSPDESTREMAILS", buffer);
+       ast_debug(1, "OSPLookup: OSPDESTREMAILS '%s'\n", buffer);
+       snprintf(buffer, sizeof(buffer), "%d", results.outtimelimit);
        pbx_builtin_setvar_helper(chan, "OSPOUTTIMELIMIT", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPOUTTIMELIMIT '%s'\n", buffer);
+       ast_debug(1, "OSPLookup: OSPOUTTIMELIMIT '%s'\n", buffer);
        snprintf(buffer, sizeof(buffer), "%d", callidtypes);
        pbx_builtin_setvar_helper(chan, "OSPOUTCALLIDTYPES", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPOUTCALLIDTYPES '%s'\n", buffer);
+       ast_debug(1, "OSPLookup: OSPOUTCALLIDTYPES '%s'\n", buffer);
        pbx_builtin_setvar_helper(chan, "OSPLOOKUPSTATUS", status);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: %s\n", status);
+       ast_debug(1, "OSPLookup: %s\n", status);
 
-       if (!strcasecmp(result.tech, OSP_TECH_H323)) {
-               if ((callidtypes & OSP_CALLID_H323) && (result.outcallid.len != 0)) {
-                       osp_uuid2str(result.outcallid.buf, buffer, sizeof(buffer));
+       if (!strcasecmp(results.outtech, OSP_TECH_SIP)) {
+               snprintf(buffer, sizeof(buffer), "%s/%s@%s", results.outtech, results.called, results.dest);
+               pbx_builtin_setvar_helper(chan, "OSPDIALSTR", buffer);
+               if (!ast_strlen_zero(results.token)) {
+                       snprintf(buffer, sizeof(buffer), "%s: %s", OSP_SIP_HEADER, results.token);
+                       pbx_builtin_setvar_helper(chan, "_SIPADDHEADER", buffer);
+                       ast_debug(1, "OSPLookup: SIPADDHEADER size '%zd'\n", strlen(buffer));
+               }
+       } else if (!strcasecmp(results.outtech, OSP_TECH_H323)) {
+               if ((callidtypes & OSP_CALLID_H323) && (results.outcallid.len != 0)) {
+                       osp_uuid2str(results.outcallid.buf, buffer, sizeof(buffer));
                } else {
                        buffer[0] = '\0';
                }
                pbx_builtin_setvar_helper(chan, "OSPOUTCALLID", buffer);
-               snprintf(buffer, sizeof(buffer), "%s/%s@%s", result.tech, result.called, result.dest);
+               snprintf(buffer, sizeof(buffer), "%s/%s@%s", results.outtech, results.called, results.dest);
                pbx_builtin_setvar_helper(chan, "OSPDIALSTR", buffer);
-       } else if (!strcasecmp(result.tech, OSP_TECH_SIP)) {
-               snprintf(buffer, sizeof(buffer), "%s/%s@%s", result.tech, result.called, result.dest);
+       } else if (!strcasecmp(results.outtech, OSP_TECH_IAX)) {
+               snprintf(buffer, sizeof(buffer), "%s/%s/%s", results.outtech, results.dest, results.called);
                pbx_builtin_setvar_helper(chan, "OSPDIALSTR", buffer);
-               if (!ast_strlen_zero(result.token)) {
-                       snprintf(buffer, sizeof(buffer), "%s%s", OSP_SIP_HEADER, result.token);
-                       pbx_builtin_setvar_helper(chan, "_SIPADDHEADER", buffer);
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSPLookup: SIPADDHEADER size '%zd'\n", strlen(buffer));
-               }
-       } else if (!strcasecmp(result.tech, OSP_TECH_IAX)) {
-               snprintf(buffer, sizeof(buffer), "%s/%s/%s", result.tech, result.dest, result.called);
+       } else if (!strcasecmp(results.outtech, OSP_TECH_SKYPE)) {
+               snprintf(buffer, sizeof(buffer), "%s/%s", results.outtech, results.called);
                pbx_builtin_setvar_helper(chan, "OSPDIALSTR", buffer);
        }
 
-       if ((cres = ast_autoservice_stop(chan)) < 0) {
-               ast_module_user_remove(u);
-               return -1;
+       if (ast_autoservice_stop(chan) < 0) {
+               return OSP_AST_ERROR;
        }
 
-       if(res <= 0) {
-               if (priority_jump || ast_opt_priority_jumping) {
-                       ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
-                       res = 0;
-               } else {
-                       res = -1;
-               }
+       if(res != OSP_OK) {
+               res = OSP_AST_ERROR;
        } else {
-               res = 0;
+               res = OSP_AST_OK;
        }
 
-       ast_module_user_remove(u);
-
        return res;
 }
 
@@ -1610,22 +2619,21 @@ static int osplookup_exec(
  * \brief OSP Application OSPNext
  * \param chan Channel
  * \param data Parameter
- * \return 0 Success, -1 Failed
+ * \return OSP_AST_OK Success, OSP_AST_ERROR Error
  */
 static int ospnext_exec(
        struct ast_channel* chan,
-       void* data)
+       const char * data)
 {
        int res;
-       struct ast_module_user* u;
        const char* provider = OSP_DEF_PROVIDER;
-       int priority_jump = 0;
        int cause = 0;
        struct varshead* headp;
        struct ast_var_t* current;
-       struct osp_result result;
-       char buffer[OSP_TOKSTR_SIZE];
-       unsigned int callidtypes = OSP_CALLID_UNDEFINED;
+       struct osp_results results;
+       OSPE_OPERATOR_NAME type;
+       char buffer[OSP_SIZE_TOKSTR + strlen(": ") + strlen(OSP_SIP_HEADER)];
+       unsigned int callidtypes = OSP_CALLID_UNDEF;
        const char* status;
        char* tmp;
 
@@ -1636,88 +2644,78 @@ static int ospnext_exec(
        );
 
        if (ast_strlen_zero(data)) {
-               ast_log(LOG_WARNING, "OSPNext: Arg required, OSPNext(cause[|provider[|options]])\n");
-               return -1;
+               ast_log(LOG_WARNING, "OSPNext: Arg required, OSPNext(cause[,provider[,options]])\n");
+               return OSP_AST_ERROR;
        }
 
-       u = ast_module_user_add(chan);
-
-       if (!(tmp = ast_strdupa(data))) {
-               ast_log(LOG_ERROR, "Out of memory\n");
-               ast_module_user_remove(u);
-               return -1;
-       }
+       tmp = ast_strdupa(data);
 
        AST_STANDARD_APP_ARGS(args, tmp);
 
-       if (!ast_strlen_zero(args.cause) && sscanf(args.cause, "%d", &cause) != 1) {
+       if (!ast_strlen_zero(args.cause) && sscanf(args.cause, "%30d", &cause) != 1) {
                cause = 0;
        }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: cause '%d'\n", cause);
+       ast_debug(1, "OSPNext: cause '%d'\n", cause);
 
        if (!ast_strlen_zero(args.provider)) {
                provider = args.provider;
        }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPlookup: provider '%s'\n", provider);
-
-       if ((args.options) && (strchr(args.options, 'j'))) {
-               priority_jump = 1;
-       }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: priority jump '%d'\n", priority_jump);
+       ast_debug(1, "OSPlookup: provider '%s'\n", provider);
 
-       result.inhandle = OSP_INVALID_HANDLE;
-       result.outhandle = OSP_INVALID_HANDLE;
-       result.intimelimit = OSP_DEF_TIMELIMIT;
-       result.numresults = 0;
+       results.inhandle = OSP_INVALID_HANDLE;
+       results.outhandle = OSP_INVALID_HANDLE;
+       results.intimelimit = OSP_DEF_TIMELIMIT;
+       results.numdests = 0;
 
-       headp = &chan->varshead;
+       headp = ast_channel_varshead(chan);
        AST_LIST_TRAVERSE(headp, current, entries) {
-               if (!strcasecmp(ast_var_name(current), "OSPINHANDLE")) {
-                       if (sscanf(ast_var_value(current), "%d", &result.inhandle) != 1) {
-                               result.inhandle = OSP_INVALID_HANDLE;
+               if (!strcmp(ast_var_name(current), "OSPINHANDLE")) {
+                       if (sscanf(ast_var_value(current), "%30d", &results.inhandle) != 1) {
+                               results.inhandle = OSP_INVALID_HANDLE;
                        }
-               } else if (!strcasecmp(ast_var_name(current), "OSPOUTHANDLE")) {
-                       if (sscanf(ast_var_value(current), "%d", &result.outhandle) != 1) {
-                               result.outhandle = OSP_INVALID_HANDLE;
+               } else if (!strcmp(ast_var_name(current), "OSPOUTHANDLE")) {
+                       if (sscanf(ast_var_value(current), "%30d", &results.outhandle) != 1) {
+                               results.outhandle = OSP_INVALID_HANDLE;
                        }
-               } else if (!strcasecmp(ast_var_name(current), "OSPINTIMELIMIT")) {
-                       if (sscanf(ast_var_value(current), "%d", &result.intimelimit) != 1) {
-                               result.intimelimit = OSP_DEF_TIMELIMIT;
+               } else if (!strcmp(ast_var_name(current), "OSPINTIMELIMIT")) {
+                       if (sscanf(ast_var_value(current), "%30d", &results.intimelimit) != 1) {
+                               results.intimelimit = OSP_DEF_TIMELIMIT;
                        }
-               } else if (!strcasecmp(ast_var_name(current), "OSPOUTCALLIDTYPES")) {
-                       if (sscanf(ast_var_value(current), "%d", &callidtypes) != 1) {
-                               callidtypes = OSP_CALLID_UNDEFINED;
+               } else if (!strcmp(ast_var_name(current), "OSPOUTCALLIDTYPES")) {
+                       if (sscanf(ast_var_value(current), "%30d", &callidtypes) != 1) {
+                               callidtypes = OSP_CALLID_UNDEF;
                        }
-               } else if (!strcasecmp(ast_var_name(current), "OSPRESULTS")) {
-                       if (sscanf(ast_var_value(current), "%d", &result.numresults) != 1) {
-                               result.numresults = 0;
+               } else if (!strcmp(ast_var_name(current), "OSPDESTREMAILS")) {
+                       if (sscanf(ast_var_value(current), "%30d", &results.numdests) != 1) {
+                               results.numdests = 0;
                        }
                }
        }
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSPNext: OSPINHANDLE '%d'\n", result.inhandle);
-               ast_log(LOG_DEBUG, "OSPNext: OSPOUTHANDLE '%d'\n", result.outhandle);
-               ast_log(LOG_DEBUG, "OSPNext: OSPINTIMELIMIT '%d'\n", result.intimelimit);
-               ast_log(LOG_DEBUG, "OSPNext: OSPOUTCALLIDTYPES '%d'\n", callidtypes);
-               ast_log(LOG_DEBUG, "OSPNext: OSPRESULTS '%d'\n", result.numresults);
-       }
+       ast_debug(1, "OSPNext: OSPINHANDLE '%d'\n", results.inhandle);
+       ast_debug(1, "OSPNext: OSPOUTHANDLE '%d'\n", results.outhandle);
+       ast_debug(1, "OSPNext: OSPINTIMELIMIT '%d'\n", results.intimelimit);
+       ast_debug(1, "OSPNext: OSPOUTCALLIDTYPES '%d'\n", callidtypes);
+       ast_debug(1, "OSPNext: OSPDESTREMAILS '%d'\n", results.numdests);
 
-       if ((res = osp_next(provider, cause, &result)) > 0) {
+       if ((res = osp_next(provider, cause, &results)) > 0) {
                status = AST_OSP_SUCCESS;
        } else {
-               result.tech[0] = '\0';
-               result.dest[0] = '\0';
-               result.called[0] = '\0';
-               result.calling[0] = '\0';
-               result.token[0] = '\0';
-               result.networkid[0] = '\0';
-               result.numresults = 0;
-               result.outtimelimit = OSP_DEF_TIMELIMIT;
-               result.outcallid.buf[0] = '\0';
-               result.outcallid.len = 0;
+               results.outtech[0] = '\0';
+               results.dest[0] = '\0';
+               results.calling[0] = '\0';
+               results.called[0] = '\0';
+               results.token[0] = '\0';
+               results.networkid[0] = '\0';
+               results.nprn[0] = '\0';
+               results.npcic[0] = '\0';
+               results.npdi = 0;
+               for (type = OSPC_OPNAME_START; type < OSPC_OPNAME_NUMBER; type++) {
+                       results.opname[type][0] = '\0';
+               }
+               results.numdests = 0;
+               results.outtimelimit = OSP_DEF_TIMELIMIT;
+               results.outcallid.buf[0] = '\0';
+               results.outcallid.len = 0;
                if (!res) {
                        status = AST_OSP_FAILED;
                } else {
@@ -1725,69 +2723,77 @@ static int ospnext_exec(
                }
        }
 
-       pbx_builtin_setvar_helper(chan, "OSPTECH", result.tech);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: OSPTECH '%s'\n", result.tech);
-       pbx_builtin_setvar_helper(chan, "OSPDEST", result.dest);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: OSPDEST '%s'\n", result.dest);
-       pbx_builtin_setvar_helper(chan, "OSPCALLED", result.called);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: OSPCALLED'%s'\n", result.called);
-       pbx_builtin_setvar_helper(chan, "OSPCALLING", result.calling);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: OSPCALLING '%s'\n", result.calling);
-       pbx_builtin_setvar_helper(chan, "OSPOUTTOKEN", result.token);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: OSPOUTTOKEN size '%zd'\n", strlen(result.token));
-       snprintf(buffer, sizeof(buffer), "%d", result.numresults);
-       pbx_builtin_setvar_helper(chan, "OSPRESULTS", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: OSPRESULTS '%s'\n", buffer);
-       snprintf(buffer, sizeof(buffer), "%d", result.outtimelimit);
+       pbx_builtin_setvar_helper(chan, "OSPOUTTECH", results.outtech);
+       ast_debug(1, "OSPNext: OSPOUTTECH '%s'\n", results.outtech);
+       pbx_builtin_setvar_helper(chan, "OSPDESTINATION", results.dest);
+       ast_debug(1, "OSPNext: OSPDESTINATION '%s'\n", results.dest);
+       pbx_builtin_setvar_helper(chan, "OSPOUTCALLING", results.calling);
+       ast_debug(1, "OSPNext: OSPOUTCALLING '%s'\n", results.calling);
+       pbx_builtin_setvar_helper(chan, "OSPOUTCALLED", results.called);
+       ast_debug(1, "OSPNext: OSPOUTCALLED'%s'\n", results.called);
+       pbx_builtin_setvar_helper(chan, "OSPOUTNETWORKID", results.networkid);
+       ast_debug(1, "OSPLookup: OSPOUTNETWORKID '%s'\n", results.networkid);
+       pbx_builtin_setvar_helper(chan, "OSPOUTNPRN", results.nprn);
+       ast_debug(1, "OSPLookup: OSPOUTNPRN '%s'\n", results.nprn);
+       pbx_builtin_setvar_helper(chan, "OSPOUTNPCIC", results.npcic);
+       ast_debug(1, "OSPLookup: OSPOUTNPCIC '%s'\n", results.npcic);
+       snprintf(buffer, sizeof(buffer), "%d", results.npdi);
+       pbx_builtin_setvar_helper(chan, "OSPOUTNPDI", buffer);
+       ast_debug(1, "OSPLookup: OSPOUTNPDI'%s'\n", buffer);
+       pbx_builtin_setvar_helper(chan, "OSPOUTSPID", results.opname[OSPC_OPNAME_SPID]);
+       ast_debug(1, "OSPLookup: OSPOUTSPID '%s'\n", results.opname[OSPC_OPNAME_SPID]);
+       pbx_builtin_setvar_helper(chan, "OSPOUTOCN", results.opname[OSPC_OPNAME_OCN]);
+       ast_debug(1, "OSPLookup: OSPOUTOCN '%s'\n", results.opname[OSPC_OPNAME_OCN]);
+       pbx_builtin_setvar_helper(chan, "OSPOUTSPN", results.opname[OSPC_OPNAME_SPN]);
+       ast_debug(1, "OSPLookup: OSPOUTSPN '%s'\n", results.opname[OSPC_OPNAME_SPN]);
+       pbx_builtin_setvar_helper(chan, "OSPOUTALTSPN", results.opname[OSPC_OPNAME_ALTSPN]);
+       ast_debug(1, "OSPLookup: OSPOUTALTSPN '%s'\n", results.opname[OSPC_OPNAME_ALTSPN]);
+       pbx_builtin_setvar_helper(chan, "OSPOUTMCC", results.opname[OSPC_OPNAME_MCC]);
+       ast_debug(1, "OSPLookup: OSPOUTMCC '%s'\n", results.opname[OSPC_OPNAME_MCC]);
+       pbx_builtin_setvar_helper(chan, "OSPOUTMNC", results.opname[OSPC_OPNAME_MNC]);
+       ast_debug(1, "OSPLookup: OSPOUTMNC '%s'\n", results.opname[OSPC_OPNAME_MNC]);
+       pbx_builtin_setvar_helper(chan, "OSPOUTTOKEN", results.token);
+       ast_debug(1, "OSPNext: OSPOUTTOKEN size '%zd'\n", strlen(results.token));
+       snprintf(buffer, sizeof(buffer), "%d", results.numdests);
+       pbx_builtin_setvar_helper(chan, "OSPDESTREMAILS", buffer);
+       ast_debug(1, "OSPNext: OSPDESTREMAILS '%s'\n", buffer);
+       snprintf(buffer, sizeof(buffer), "%d", results.outtimelimit);
        pbx_builtin_setvar_helper(chan, "OSPOUTTIMELIMIT", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: OSPOUTTIMELIMIT '%s'\n", buffer);
+       ast_debug(1, "OSPNext: OSPOUTTIMELIMIT '%s'\n", buffer);
        pbx_builtin_setvar_helper(chan, "OSPNEXTSTATUS", status);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: %s\n", status);
+       ast_debug(1, "OSPNext: %s\n", status);
 
-       if (!strcasecmp(result.tech, OSP_TECH_H323)) {
-               if ((callidtypes & OSP_CALLID_H323) && (result.outcallid.len != 0)) {
-                       osp_uuid2str(result.outcallid.buf, buffer, sizeof(buffer));
+       if (!strcasecmp(results.outtech, OSP_TECH_SIP)) {
+               snprintf(buffer, sizeof(buffer), "%s/%s@%s", results.outtech, results.called, results.dest);
+               pbx_builtin_setvar_helper(chan, "OSPDIALSTR", buffer);
+               if (!ast_strlen_zero(results.token)) {
+                       snprintf(buffer, sizeof(buffer), "%s: %s", OSP_SIP_HEADER, results.token);
+                       pbx_builtin_setvar_helper(chan, "_SIPADDHEADER", buffer);
+                       ast_debug(1, "OSPLookup: SIPADDHEADER size '%zd'\n", strlen(buffer));
+               }
+       } else if (!strcasecmp(results.outtech, OSP_TECH_H323)) {
+               if ((callidtypes & OSP_CALLID_H323) && (results.outcallid.len != 0)) {
+                       osp_uuid2str(results.outcallid.buf, buffer, sizeof(buffer));
                } else {
                        buffer[0] = '\0';
                }
                pbx_builtin_setvar_helper(chan, "OSPOUTCALLID", buffer);
-               snprintf(buffer, sizeof(buffer), "%s/%s@%s", result.tech, result.called, result.dest);
+               snprintf(buffer, sizeof(buffer), "%s/%s@%s", results.outtech, results.called, results.dest);
                pbx_builtin_setvar_helper(chan, "OSPDIALSTR", buffer);
-       } else if (!strcasecmp(result.tech, OSP_TECH_SIP)) {
-               snprintf(buffer, sizeof(buffer), "%s/%s@%s", result.tech, result.called, result.dest);
+       } else if (!strcasecmp(results.outtech, OSP_TECH_IAX)) {
+               snprintf(buffer, sizeof(buffer), "%s/%s/%s", results.outtech, results.dest, results.called);
                pbx_builtin_setvar_helper(chan, "OSPDIALSTR", buffer);
-               if (!ast_strlen_zero(result.token)) {
-                       snprintf(buffer, sizeof(buffer), "%s%s", OSP_SIP_HEADER, result.token);
-                       pbx_builtin_setvar_helper(chan, "_SIPADDHEADER", buffer);
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSPLookup: SIPADDHEADER size '%zd'\n", strlen(buffer));
-               }
-       } else if (!strcasecmp(result.tech, OSP_TECH_IAX)) {
-               snprintf(buffer, sizeof(buffer), "%s/%s/%s", result.tech, result.dest, result.called);
+       } else if (!strcasecmp(results.outtech, OSP_TECH_SKYPE)) {
+               snprintf(buffer, sizeof(buffer), "%s/%s", results.outtech, results.called);
                pbx_builtin_setvar_helper(chan, "OSPDIALSTR", buffer);
        }
 
-       if(res <= 0) {
-               if (priority_jump || ast_opt_priority_jumping) {
-                       ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
-                       res = 0;
-               } else {
-                       res = -1;
-               }
+       if(res != OSP_OK) {
+               res = OSP_AST_ERROR;
        } else {
-               res = 0;
+               res = OSP_AST_OK;
        }
 
-       ast_module_user_remove(u);
-
        return res;
 }
 
@@ -1795,24 +2801,24 @@ static int ospnext_exec(
  * \brief OSP Application OSPFinish
  * \param chan Channel
  * \param data Parameter
- * \return 0 Success, -1 Failed
+ * \return OSP_AST_OK Success, OSP_AST_ERROR Error
  */
 static int ospfinished_exec(
        struct ast_channel* chan,
-       void* data)
+       const char * data)
 {
-       int res = 1;
-       struct ast_module_user* u;
-       int priority_jump = 0;
+       int res = OSP_OK;
        int cause = 0;
        struct varshead* headp;
        struct ast_var_t* current;
        int inhandle = OSP_INVALID_HANDLE;
        int outhandle = OSP_INVALID_HANDLE;
        int recorded = 0;
-       time_t start, connect, end;
+       time_t start = 0, connect = 0, end = 0;
        unsigned int release;
-       char buffer[OSP_INTSTR_SIZE];
+       char buffer[OSP_SIZE_INTSTR];
+       char inqos[OSP_SIZE_QOSSTR] = { 0 };
+       char outqos[OSP_SIZE_QOSSTR] = { 0 };
        const char* status;
        char* tmp;
 
@@ -1821,89 +2827,75 @@ static int ospfinished_exec(
                AST_APP_ARG(options);
        );
 
-       u = ast_module_user_add(chan);
-
-       if (!(tmp = ast_strdupa(data))) {
-               ast_log(LOG_ERROR, "Out of memory\n");
-               ast_module_user_remove(u);
-               return -1;
-       }
+       tmp = ast_strdupa(data);
 
        AST_STANDARD_APP_ARGS(args, tmp);
 
-       if ((args.options) && (strchr(args.options, 'j'))) {
-               priority_jump = 1;
-       }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPFinish: priority jump '%d'\n", priority_jump);
-
-       headp = &chan->varshead;
+       headp = ast_channel_varshead(chan);
        AST_LIST_TRAVERSE(headp, current, entries) {
-               if (!strcasecmp(ast_var_name(current), "OSPINHANDLE")) {
-                       if (sscanf(ast_var_value(current), "%d", &inhandle) != 1) {
+               if (!strcmp(ast_var_name(current), "OSPINHANDLE")) {
+                       if (sscanf(ast_var_value(current), "%30d", &inhandle) != 1) {
                                inhandle = OSP_INVALID_HANDLE;
                        }
-               } else if (!strcasecmp(ast_var_name(current), "OSPOUTHANDLE")) {
-                       if (sscanf(ast_var_value(current), "%d", &outhandle) != 1) {
+               } else if (!strcmp(ast_var_name(current), "OSPOUTHANDLE")) {
+                       if (sscanf(ast_var_value(current), "%30d", &outhandle) != 1) {
                                outhandle = OSP_INVALID_HANDLE;
                        }
                } else if (!recorded &&
-                       (!strcasecmp(ast_var_name(current), "OSPAUTHSTATUS") ||
-                       !strcasecmp(ast_var_name(current), "OSPLOOKUPSTATUS") ||
-                       !strcasecmp(ast_var_name(current), "OSPNEXTSTATUS")))
+                       (!strcmp(ast_var_name(current), "OSPAUTHSTATUS") ||
+                       !strcmp(ast_var_name(current), "OSPLOOKUPSTATUS") ||
+                       !strcmp(ast_var_name(current), "OSPNEXTSTATUS")))
                {
-                       if (strcasecmp(ast_var_value(current), AST_OSP_SUCCESS)) {
+                       if (strcmp(ast_var_value(current), AST_OSP_SUCCESS)) {
                                recorded = 1;
                        }
+               } else if (!strcmp(ast_var_name(current), "OSPINAUDIOQOS")) {
+                       ast_copy_string(inqos, ast_var_value(current), sizeof(inqos));
+               } else if (!strcmp(ast_var_name(current), "OSPOUTAUDIOQOS")) {
+                       ast_copy_string(outqos, ast_var_value(current), sizeof(outqos));
                }
        }
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSPFinish: OSPINHANDLE '%d'\n", inhandle);
-               ast_log(LOG_DEBUG, "OSPFinish: OSPOUTHANDLE '%d'\n", outhandle);
-               ast_log(LOG_DEBUG, "OSPFinish: recorded '%d'\n", recorded);
-       }
+       ast_debug(1, "OSPFinish: OSPINHANDLE '%d'\n", inhandle);
+       ast_debug(1, "OSPFinish: OSPOUTHANDLE '%d'\n", outhandle);
+       ast_debug(1, "OSPFinish: recorded '%d'\n", recorded);
+       ast_debug(1, "OSPFinish: OSPINAUDIOQOS '%s'\n", inqos);
+       ast_debug(1, "OSPFinish: OSPOUTAUDIOQOS '%s'\n", outqos);
 
-       if (!ast_strlen_zero(args.cause) && sscanf(args.cause, "%d", &cause) != 1) {
+       if (!ast_strlen_zero(args.cause) && sscanf(args.cause, "%30d", &cause) != 1) {
                cause = 0;
        }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPFinish: cause '%d'\n", cause);
+       ast_debug(1, "OSPFinish: cause '%d'\n", cause);
 
-       if (chan->cdr) {
-               start = chan->cdr->start.tv_sec;
-               connect = chan->cdr->answer.tv_sec;
-               if (connect) {
-                       end = time(NULL);
-               } else {
-                       end = connect;
-               }
-       } else {
-               start = 0;
-               connect = 0;
-               end = 0;
+       if (!ast_tvzero(ast_channel_creationtime(chan))) {
+               start = ast_channel_creationtime(chan).tv_sec;
+       }
+       if (!ast_tvzero(ast_channel_answertime(chan))) {
+               connect = ast_channel_answertime(chan).tv_sec;
        }
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSPFinish: start '%ld'\n", start);
-               ast_log(LOG_DEBUG, "OSPFinish: connect '%ld'\n", connect);
-               ast_log(LOG_DEBUG, "OSPFinish: end '%ld'\n", end);
+       if (connect) {
+               end = time(NULL);
+       } else {
+               end = connect;
        }
 
-       release = chan->_softhangup ? 0 : 1;
+       ast_debug(1, "OSPFinish: start '%ld'\n", start);
+       ast_debug(1, "OSPFinish: connect '%ld'\n", connect);
+       ast_debug(1, "OSPFinish: end '%ld'\n", end);
 
-       if (osp_finish(outhandle, recorded, cause, start, connect, end, release) <= 0) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSPFinish: Unable to report usage for outbound call\n");
+       release = ast_check_hangup(chan) ? 0 : 1;
+
+       if (osp_finish(outhandle, recorded, cause, start, connect, end, release, inqos, outqos) <= 0) {
+               ast_debug(1, "OSPFinish: Unable to report usage for outbound call\n");
        }
        switch (cause) {
-               case AST_CAUSE_NORMAL_CLEARING:
-                       break;
-               default:
-                       cause = AST_CAUSE_NO_ROUTE_DESTINATION;
-                       break;
+       case AST_CAUSE_NORMAL_CLEARING:
+               break;
+       default:
+               cause = AST_CAUSE_NO_ROUTE_DESTINATION;
+               break;
        }
-       if (osp_finish(inhandle, recorded, cause, start, connect, end, release) <= 0) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSPFinish: Unable to report usage for inbound call\n");
+       if (osp_finish(inhandle, recorded, cause, start, connect, end, release, inqos, outqos) <= 0) {
+               ast_debug(1, "OSPFinish: Unable to report usage for inbound call\n");
        }
        snprintf(buffer, sizeof(buffer), "%d", OSP_INVALID_HANDLE);
        pbx_builtin_setvar_helper(chan, "OSPOUTHANDLE", buffer);
@@ -1918,37 +2910,66 @@ static int ospfinished_exec(
        }
        pbx_builtin_setvar_helper(chan, "OSPFINISHSTATUS", status);
 
-       if(!res) {
-               if (priority_jump || ast_opt_priority_jumping) {
-                       ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
-                       res = 0;
-               } else {
-                       res = -1;
-               }
+       if(res != OSP_OK) {
+               res = OSP_AST_ERROR;
        } else {
-               res = 0;
+               res = OSP_AST_OK;
        }
 
-       ast_module_user_remove(u);
-
        return res;
 }
 
 /* OSP Module APIs */
 
-static int osp_load(void)
+static int osp_unload(void)
 {
-       const char* t;
-       unsigned int v;
+       struct osp_provider* provider;
+       struct osp_provider* next;
+
+       if (osp_initialized) {
+               ast_mutex_lock(&osp_lock);
+               for (provider = osp_providers; provider; provider = next) {
+                       next = provider->next;
+                       OSPPProviderDelete(provider->handle, 0);
+                       ast_free(provider);
+               }
+               osp_providers = NULL;
+               ast_mutex_unlock(&osp_lock);
+
+               OSPPCleanup();
+
+               osp_tokenformat = TOKEN_ALGO_SIGNED;
+               osp_security = 0;
+               osp_hardware = 0;
+               osp_initialized = 0;
+       }
+
+       return 0;
+}
+
+static int osp_load(int reload)
+{
+       const char* cvar;
+       unsigned int ivar;
        struct ast_config* cfg;
+       struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
        int error = OSPC_ERR_NO_ERROR;
 
-       cfg = ast_config_load(OSP_CONFIG_FILE);
+       if ((cfg = ast_config_load(OSP_CONFIG_FILE, config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
+               return 0;
+       } else if (cfg == CONFIG_STATUS_FILEINVALID) {
+               ast_log(LOG_ERROR, "Config file %s is in an invalid format.  Aborting.\n", OSP_CONFIG_FILE);
+               return 0;
+       }
+
        if (cfg) {
-               t = ast_variable_retrieve(cfg, OSP_GENERAL_CAT, "accelerate");
-               if (t && ast_true(t)) {
+               if (reload) {
+                       osp_unload();
+               }
+
+               if ((cvar = ast_variable_retrieve(cfg, OSP_GENERAL_CAT, "accelerate")) && ast_true(cvar)) {
                        if ((error = OSPPInit(1)) != OSPC_ERR_NO_ERROR) {
-                               ast_log(LOG_WARNING, "OSP: Unable to enable hardware accelleration\n");
+                               ast_log(LOG_WARNING, "OSP: Unable to enable hardware acceleration, error='%d'\n", error);
                                OSPPInit(0);
                        } else {
                                osp_hardware = 1;
@@ -1956,29 +2977,29 @@ static int osp_load(void)
                } else {
                        OSPPInit(0);
                }
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: osp_hardware '%d'\n", osp_hardware);
+               ast_debug(1, "OSP: osp_hardware '%d'\n", osp_hardware);
 
-               t = ast_variable_retrieve(cfg, OSP_GENERAL_CAT, "tokenformat");
-               if (t) {
-                       if ((sscanf(t, "%d", &v) == 1) &&
-                               ((v == TOKEN_ALGO_SIGNED) || (v == TOKEN_ALGO_UNSIGNED) || (v == TOKEN_ALGO_BOTH)))
+               if ((cvar = ast_variable_retrieve(cfg, OSP_GENERAL_CAT, "securityfeatures")) && ast_true(cvar)) {
+                       osp_security = 1;
+               }
+               ast_debug(1, "OSP: osp_security '%d'\n", osp_security);
+
+               if ((cvar = ast_variable_retrieve(cfg, OSP_GENERAL_CAT, "tokenformat"))) {
+                       if ((sscanf(cvar, "%30d", &ivar) == 1) &&
+                               ((ivar == TOKEN_ALGO_SIGNED) || (ivar == TOKEN_ALGO_UNSIGNED) || (ivar == TOKEN_ALGO_BOTH)))
                        {
-                               osp_tokenformat = v;
+                               osp_tokenformat = ivar;
                        } else {
                                ast_log(LOG_WARNING, "tokenformat should be an integer from %d, %d or %d, not '%s'\n",
-                                       TOKEN_ALGO_SIGNED, TOKEN_ALGO_UNSIGNED, TOKEN_ALGO_BOTH, t);
+                                       TOKEN_ALGO_SIGNED, TOKEN_ALGO_UNSIGNED, TOKEN_ALGO_BOTH, cvar);
                        }
                }
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: osp_tokenformat '%d'\n", osp_tokenformat);
+               ast_debug(1, "OSP: osp_tokenformat '%d'\n", osp_tokenformat);
 
-               t = ast_category_browse(cfg, NULL);
-               while(t) {
-                       if (strcasecmp(t, OSP_GENERAL_CAT)) {
-                               osp_create_provider(cfg, t);
+               for (cvar = ast_category_browse(cfg, NULL); cvar != NULL; cvar = ast_category_browse(cfg, cvar)) {
+                       if (strcasecmp(cvar, OSP_GENERAL_CAT)) {
+                               osp_create_provider(cfg, cvar);
                        }
-                       t = ast_category_browse(cfg, t);
                }
 
                osp_initialized = 1;
@@ -1988,199 +3009,129 @@ static int osp_load(void)
                ast_log(LOG_WARNING, "OSP: Unable to find configuration. OSP support disabled\n");
                return 0;
        }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSP: osp_initialized '%d'\n", osp_initialized);
+       ast_debug(1, "OSP: osp_initialized '%d'\n", osp_initialized);
 
        return 1;
 }
 
-static int osp_unload(void)
-{
-       struct osp_provider* p;
-       struct osp_provider* next;
-
-       if (osp_initialized) {
-               ast_mutex_lock(&osplock);
-               p = ospproviders;
-               while(p) {
-                       next = p->next;
-                       OSPPProviderDelete(p->handle, 0);
-                       free(p);
-                       p = next;
-               }
-               ospproviders = NULL;
-               ast_mutex_unlock(&osplock);
-
-               OSPPCleanup();
-
-               osp_tokenformat = TOKEN_ALGO_SIGNED;
-               osp_hardware = 0;
-               osp_initialized = 0;
-       }
-       return 0;
-}
-
-static int osp_show(
-       int fd,
-       int argc,
-       char* argv[])
+static char *handle_cli_osp_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        int i;
        int found = 0;
-       struct osp_provider* p;
-       const char* provider = NULL;
+       struct osp_provider* provider;
+       const char* name = NULL;
        const char* tokenalgo;
 
-       if ((argc < 2) || (argc > 3)) {
-               return RESULT_SHOWUSAGE;
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "osp show";
+               e->usage =
+                       "Usage: osp show\n"
+                       "       Displays information on Open Settlement Protocol support\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
+       }
+
+       if ((a->argc < 2) || (a->argc > 3)) {
+               return CLI_SHOWUSAGE;
        }
-       if (argc > 2) {
-               provider = argv[2];
+
+       if (a->argc > 2) {
+               name = a->argv[2];
        }
-       if (!provider) {
+
+       if (!name) {
                switch (osp_tokenformat) {
-                       case TOKEN_ALGO_BOTH:
-                               tokenalgo = "Both";
-                               break;
-                       case TOKEN_ALGO_UNSIGNED:
-                               tokenalgo = "Unsigned";
-                               break;
-                       case TOKEN_ALGO_SIGNED:
-                       default:
-                               tokenalgo = "Signed";
-                               break;
+               case TOKEN_ALGO_BOTH:
+                       tokenalgo = "Both";
+                       break;
+               case TOKEN_ALGO_UNSIGNED:
+                       tokenalgo = "Unsigned";
+                       break;
+               case TOKEN_ALGO_SIGNED:
+               default:
+                       tokenalgo = "Signed";
+                       break;
                }
-               ast_cli(fd, "OSP: %s %s %s\n",
-                       osp_initialized ? "Initialized" : "Uninitialized", osp_hardware ? "Accelerated" : "Normal", tokenalgo);
+               ast_cli(a->fd, "OSP: %s/%s/%s/%s\n",
+                       osp_initialized ? "Initialized" : "Uninitialized",
+                       osp_hardware ? "Accelerated" : "Normal",
+                       osp_security ? "Enabled" : "Disabled",
+                       tokenalgo);
        }
 
-       ast_mutex_lock(&osplock);
-       p = ospproviders;
-       while(p) {
-               if (!provider || !strcasecmp(p->name, provider)) {
+       ast_mutex_lock(&osp_lock);
+       for (provider = osp_providers; provider; provider = provider->next) {
+               if (!name || !strcasecmp(provider->name, name)) {
                        if (found) {
-                               ast_cli(fd, "\n");
+                               ast_cli(a->fd, "\n");
                        }
-                       ast_cli(fd, " == OSP Provider '%s' == \n", p->name);
-                       ast_cli(fd, "Local Private Key: %s\n", p->privatekey);
-                       ast_cli(fd, "Local Certificate: %s\n", p->localcert);
-                       for (i = 0; i < p->cacount; i++) {
-                               ast_cli(fd, "CA Certificate %d:  %s\n", i + 1, p->cacerts[i]);
+                       ast_cli(a->fd, " == OSP Provider '%s' == \n", provider->name);
+                       if (osp_security) {
+                               ast_cli(a->fd, "Local Private Key: %s\n", provider->privatekey);
+                               ast_cli(a->fd, "Local Certificate: %s\n", provider->localcert);
+                               for (i = 0; i < provider->canum; i++) {
+                                       ast_cli(a->fd, "CA Certificate %d:  %s\n", i + 1, provider->cacerts[i]);
+                               }
                        }
-                       for (i = 0; i < p->spcount; i++) {
-                               ast_cli(fd, "Service Point %d:   %s\n", i + 1, p->srvpoints[i]);
+                       for (i = 0; i < provider->spnum; i++) {
+                               ast_cli(a->fd, "Service Point %d:   %s\n", i + 1, provider->spoints[i]);
                        }
-                       ast_cli(fd, "Max Connections:   %d\n", p->maxconnections);
-                       ast_cli(fd, "Retry Delay:       %d seconds\n", p->retrydelay);
-                       ast_cli(fd, "Retry Limit:       %d\n", p->retrylimit);
-                       ast_cli(fd, "Timeout:           %d milliseconds\n", p->timeout);
-                       ast_cli(fd, "Source:            %s\n", strlen(p->source) ? p->source : "<unspecified>");
-                       ast_cli(fd, "Auth Policy        %d\n", p->authpolicy);
-                       ast_cli(fd, "Default protocol   %s\n", p->defaultprotocol);
-                       ast_cli(fd, "OSP Handle:        %d\n", p->handle);
+                       ast_cli(a->fd, "Max Connections:   %d\n", provider->maxconnect);
+                       ast_cli(a->fd, "Retry Delay:       %d seconds\n", provider->retrydelay);
+                       ast_cli(a->fd, "Retry Limit:       %d\n", provider->retrylimit);
+                       ast_cli(a->fd, "Timeout:           %d milliseconds\n", provider->timeout);
+                       ast_cli(a->fd, "Source:            %s\n", strlen(provider->source) ? provider->source : "<unspecified>");
+                       ast_cli(a->fd, "Auth Policy        %d\n", provider->authpolicy);
+                       ast_cli(a->fd, "Default protocol   %s\n", provider->defprotocol);
+                       ast_cli(a->fd, "Work mode          %d\n", provider->workmode);
+                       ast_cli(a->fd, "Service type       %d\n", provider->srvtype);
+                       ast_cli(a->fd, "OSP Handle:        %d\n", provider->handle);
                        found++;
                }
-               p = p->next;
        }
-       ast_mutex_unlock(&osplock);
+       ast_mutex_unlock(&osp_lock);
 
        if (!found) {
-               if (provider) {
-                       ast_cli(fd, "Unable to find OSP provider '%s'\n", provider);
+               if (name) {
+                       ast_cli(a->fd, "Unable to find OSP provider '%s'\n", name);
                } else {
-                       ast_cli(fd, "No OSP providers configured\n");
+                       ast_cli(a->fd, "No OSP providers configured\n");
                }
        }
-       return RESULT_SUCCESS;
+
+       return CLI_SUCCESS;
 }
 
-static const char* app1= "OSPAuth";
-static const char* synopsis1 = "OSP authentication";
-static const char* descrip1 =
-"  OSPAuth([provider[|options]]):  Authenticate a SIP INVITE by OSP and sets\n"
-"the variables:\n"
-" ${OSPINHANDLE}:  The inbound call transaction handle\n"
-" ${OSPINTIMELIMIT}:  The inbound call duration limit in seconds\n"
-"\n"
-"The option string may contain the following character:\n"
-"      'j' -- jump to n+101 priority if the authentication was NOT successful\n"
-"This application sets the following channel variable upon completion:\n"
-"      OSPAUTHSTATUS   The status of the OSP Auth attempt as a text string, one of\n"
-"              SUCCESS | FAILED | ERROR\n";
-
-static const char* app2= "OSPLookup";
-static const char* synopsis2 = "Lookup destination by OSP";
-static const char* descrip2 =
-"  OSPLookup(exten[|provider[|options]]):  Looks up an extension via OSP and sets\n"
-"the variables, where 'n' is the number of the result beginning with 1:\n"
-" ${OSPOUTHANDLE}:  The OSP Handle for anything remaining\n"
-" ${OSPTECH}:  The technology to use for the call\n"
-" ${OSPDEST}:  The destination to use for the call\n"
-" ${OSPCALLED}:  The called number to use for the call\n"
-" ${OSPCALLING}:  The calling number to use for the call\n"
-" ${OSPDIALSTR}:  The dial command string\n"
-" ${OSPOUTTOKEN}:  The actual OSP token as a string\n"
-" ${OSPOUTTIMELIMIT}:  The outbound call duration limit in seconds\n"
-" ${OSPOUTCALLIDTYPES}:  The outbound call id types\n"
-" ${OSPOUTCALLID}:  The outbound call id\n"
-" ${OSPRESULTS}:  The number of OSP results total remaining\n"
-"\n"
-"The option string may contain the following character:\n"
-"      'j' -- jump to n+101 priority if the lookup was NOT successful\n"
-"      'h' -- generate H323 call id for the outbound call\n"
-"      's' -- generate SIP call id for the outbound call. Have not been implemented\n"
-"      'i' -- generate IAX call id for the outbound call. Have not been implemented\n"
-"This application sets the following channel variable upon completion:\n"
-"      OSPLOOKUPSTATUS The status of the OSP Lookup attempt as a text string, one of\n"
-"              SUCCESS | FAILED | ERROR\n";
-
-static const char* app3 = "OSPNext";
-static const char* synopsis3 = "Lookup next destination by OSP";
-static const char* descrip3 =
-"  OSPNext(cause[|provider[|options]]):  Looks up the next OSP Destination for ${OSPOUTHANDLE}\n"
-"See OSPLookup for more information\n"
-"\n"
-"The option string may contain the following character:\n"
-"      'j' -- jump to n+101 priority if the lookup was NOT successful\n"
-"This application sets the following channel variable upon completion:\n"
-"      OSPNEXTSTATUS The status of the OSP Next attempt as a text string, one of\n"
-"              SUCCESS | FAILED | ERROR\n";
-
-static const char* app4 = "OSPFinish";
-static const char* synopsis4 = "Record OSP entry";
-static const char* descrip4 =
-"  OSPFinish([status[|options]]):  Records call state for ${OSPINHANDLE}, according to\n"
-"status, which should be one of BUSY, CONGESTION, ANSWER, NOANSWER, or CHANUNAVAIL\n"
-"or coincidentally, just what the Dial application stores in its ${DIALSTATUS}.\n"
-"\n"
-"The option string may contain the following character:\n"
-"      'j' -- jump to n+101 priority if the finish attempt was NOT successful\n"
-"This application sets the following channel variable upon completion:\n"
-"      OSPFINISHSTATUS The status of the OSP Finish attempt as a text string, one of\n"
-"              SUCCESS | FAILED | ERROR \n";
-
-static const char osp_usage[] =
-"Usage: osp show\n"
-"       Displays information on Open Settlement Protocol support\n";
+/* OSPAuth() dialplan application */
+static const char app1[] = "OSPAuth";
+
+/* OSPLookup() dialplan application */
+static const char app2[] = "OSPLookup";
+
+/* OSPNext() dialplan application */
+static const char app3[] = "OSPNext";
+
+/* OSPFinish() dialplan application */
+static const char app4[] = "OSPFinish";
 
 static struct ast_cli_entry cli_osp[] = {
-       { {"osp", "show", NULL},
-       osp_show, "Displays OSP information",
-       osp_usage },
+       AST_CLI_DEFINE(handle_cli_osp_show, "Displays OSF information")
 };
 
 static int load_module(void)
 {
        int res;
 
-       if(!osp_load())
+       if (!osp_load(0))
                return AST_MODULE_LOAD_DECLINE;
 
        ast_cli_register_multiple(cli_osp, sizeof(cli_osp) / sizeof(struct ast_cli_entry));
-       res = ast_register_application(app1, ospauth_exec, synopsis1, descrip1);
-       res |= ast_register_application(app2, osplookup_exec, synopsis2, descrip2);
-       res |= ast_register_application(app3, ospnext_exec, synopsis3, descrip3);
-       res |= ast_register_application(app4, ospfinished_exec, synopsis4, descrip4);
+       res = ast_register_application_xml(app1, ospauth_exec);
+       res |= ast_register_application_xml(app2, osplookup_exec);
+       res |= ast_register_application_xml(app3, ospnext_exec);
+       res |= ast_register_application_xml(app4, ospfinished_exec);
 
        return res;
 }
@@ -2196,21 +3147,19 @@ static int unload_module(void)
        ast_cli_unregister_multiple(cli_osp, sizeof(cli_osp) / sizeof(struct ast_cli_entry));
        osp_unload();
 
-       ast_module_user_hangup_all();
-
        return res;
 }
 
 static int reload(void)
 {
-       osp_unload();
-       osp_load();
+       osp_load(1);
 
        return 0;
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Open Settlement Protocol Applications",
+       .support_level = AST_MODULE_SUPPORT_EXTENDED,
        .load = load_module,
        .unload = unload_module,
        .reload = reload,
-       );
+);