한국어

소프트스위치

온누리070 플레이스토어 다운로드
    acrobits softphone
     온누리 070 카카오 프러스 친구추가온누리 070 카카오 프러스 친구추가친추
     카카오톡 채팅 상담 카카오톡 채팅 상담카톡
    
     라인상담
     라인으로 공유

     페북공유
    
     PAYPAL
     
     PRICE
     

pixel.gif

    before pay call 0088 from app


https://voipmagazine.wordpress.com/2015/01/07/opensips-module-interface-part-1-c-development/



Introduction

OpenSIPS consists of two parts: the core and the modules. OpenSIPS module is a shared library (.so file extension) which is configured in the routing script and loaded at OpenSIPS startup time (the loader: /usr/bin/ld). The name of the module is the name of the shared library.

To open and edit the source files, I will use the text editor vim. You can install it on Fedora Red Hat: # yum install vim Or use your own favourite text editor to edit the source files. Then go to the OpenSIPS src folder (It can be “/usr/local/src/opensips_1_11”) and explore the files (# vim File-Name). To enter in write mode, press i. To paste a copied code into an opened file do it like this: press ESC and then write :set paste. To save a file press ESC and them write :wq

If you are reading in a source file and you want to open a file (i.e. the file where its name comes after the “#include” statement), put the curser on the file name and press either “gf” to go to the file or “ctrl-w f” to open the file in a new window.

In this part i will talk about the definition of the module interface, some fields of its structure, and how to export function from the module to the routing script. So you can call this function from the routing script. Also i will talk about exporting a module parameter.

OpenSIPS Module Interface

OpenSIPS module interface is called module_exports and it is used by OpenSIPS core to serve the module. Each OpenSIPS module creates a variable of this type and call it by “exports”.  When OpenSIPS tries to load the module, it takes the module path from the configuration file (loadmodule statement), open the shared object file (.so file), and try to find the “exports” which populates everything about the module (e.g. script functions, parameters, mi functions, ..etc.).

ModuleInterface

The structure of the module interface (module_exports) is defined in the header file “/usr/local/src/opensips_1_11/sr_module.h”. It is a struct C data type which has the declared name module_exports. It defines a grouped list of fields that are populated by the module. I will explain some of these fields and leave the rest to the next article.

struct module_exports {
.          char* name;                                     /*  Module Name */
.          char* version;                                  /*  Module Version */
.          char* compile_flags;                        /*  Compile flags used on the module */
.          unsigned int dlflags;                          /*  Flags for dlopen */

.          cmd_export_t* cmds;                      /* Array of the exported commands */
.          param_export_t* params;                /* Array of the exported module parameters */

.          …  /*Rest of Fields*/
}

The fields are:

name: It is the module name. If the module will be used, its name must be passed to the function “loadmodule” in the configuration file. The corresponding shared library (shared object) will be “new_module.so” and it will be loaded at the startup time. version: It is the module version. It is used to detect the conflict situations where you have a module with version different from the core version. In the same file “sr_module.h”, we have the defined variable “MODULE_VERSION” which is OPENSIPS full version concatenated with the OpenSIPS compilation flags.

#define MODULE_VERSION  OPENSIPS_FULL_VERSION, OPENSIPS_COMPILE_FLAGS

This variable can be set to the field version in the variable “exports” which has the type module_exports and represents the actual module interface. This is shown further below.

compile_flags: You can skip this if you have used the MODULE_VERSION which already has the compile flags after the comma “,“.  Otherwise use them independently. Remember when you define a variable of struct in c language, the “,”  is used to separate between the fields.

dlflags: In the same file “sr_module.h”, we have  the defined variable DEFAULT_DLFLAGS which has the value 0 which means instructs the module loader to use OpenSIPS default dlopen flags.

#define DEFAULT_DLFLAGS  0

This variable can be set to the field dlflags in the actual interface “exports”.

cmds which has the OpenSIPS-defined C-type cmd_export_t*. It is an array of functions that are exported to the routing script. It is defined in the same file “sr_module.h”

typedef  struct cmd_export_   cmd_export_t;

struct cmd_export_ {
char* name;                                    /* null terminated command name */
cmd_function function;                    /* pointer to the corresponding function */
int param_no;                                  /* number of parameters used by the function */
fixup_function fixup;                      /* pointer to the function called to “fix” the parameters */
free_fixup_function free_fixup;     /* pointer to the function called to free the “fixed” parameters */
int flags;                                         /* Function flags */
}

The structure above defines a function in a dynamic and generic way. The name is the function actual name which will be used from the script when calling the function. The function is a pointer to the C code of the function which will be actually called. This pointer is defined as following:

typedef  int (*cmd_function)(struct sip_msg*, char*, char*, char*, char*, char*, char*);

The return value of the function is integer (int). “cmd_function” is the pointer to the function. The rest are the parameters of the function (First parameter is the actual SIP message which triggers the processing and the rest are multiple char pointers. You can have till sex arguments). In the module C-code, you will have something like this:

int new_function(struct sip_msg *msg,char *param) {

   ……… /* Actual Exported Function Code*/

    /* You can manipulate the SIP message Here:

    /* Remove/Add header Fields …….*/

}

This function has one “char *” parameter but as i said you can have up to 6 “char *” parameters. The return value of the exported function can be either a positive number (higher than +1) on success or lower than -1 on failure. The return value must not be zero because this will exit the script.

The param_no is the number of parameters passed to the function. If this number is equal to zero, this means there are no parameters for the function. But keep in mind that the first parameter (SIP message) will always be received as a first parameter of the C code function even if you call the function with no parameters from the routing script (Example new_function() in the script will be something like new_function(msg) in the C code).

These parameters must be evaluated (get the length of each “char *” parameter) and probably processed. To avoid CPU penalty of doing this evaluation and processing many times (For each function call), the evaluation and processing can be done only once at the OpenSIPS startup via fixup function. This reduces the runtime evaluations.

fixup is a pointer to the function which will be called to fix the parameters of the exported function. The structure of fixup pointer is “fixup_function” which is defined in the file “sr_module.h” as following:

typedef  int (*fixup_function)(void** param, int param_no);

The fixup function will receive the current parameter to be processed (param) and its index (param_no). It is important to check the parameter index so the function knows which parameter it is currently evaluating.  The actual code of the fixup function looks like this:

int do_fixup (void** param, int param_no){

/* Parameter Evaluation and probably Processing */ check the parameter you are currentlu evaluating

char * value= (char *)(*param);

str* ret_value;

 ………

if (param_no==1){

ret_value=pkg_malloc(sizeof(str));

 ……..

ret_value->s=value;

ret_value->len=strlen(value);

*param=(void* )ret_value;

return 1;

}

       …….

return 0;

}

OpenSIPS 1.X has its own internal memory manager (NOT standard) and the function pkg_mallocallocates a memory of a given size (here sizeof(str)) and return a pointer to the allocated memory (here ret_value is a pointer to str).  Note that the received parameter (param) is replaced with the evaluated and processed value (ret_value). After the fixup function has been called, the exported function will have the param fixed (type = str not char*) but casting is still required ((str *)param).

Using fixup function is not restricted to constants. It can be also used with variables.

free_fixup is a pointer to the function which is used to free up the fixed parameters (free up the resources used for the fixed parameters).

flags represents the places in the routing script where the function is available and can be called. For example it can be called from REQUEST_ROUTE, FAILURE_ROUTE, BRANCH_ROUTE, ONREPLY_ROUTE, or LOCAL_ROUTE (these routes are defined in the header file “route.h”). If the function is called in forbidden place in the routing script, you will get an error message and OpenSIPS will not start.

The figure below is a graphical representation of the module interface:

The Actual Module Interface

When you create a new module, you need to create a variable of the type struct module_exports which will be the actual interface of the new module. Assume you are writing the module “new_module”, you will add its interface (named “exports”) in its C code. It could be like this:

struct module_exports exports{
.               “new_module”,                                  /* module name */
.                MODULE_VERSION,                   /* Module Version
.                DEFAULT_DLFLAGS,                  /*  dlopen flags */
.                cmds,                                              /*  exported functions */
.                params,                                           /*  exported parameters */

        …. rest of values

}

How to Export a Function to the Routing Script ?

For example if the module has two functions and you want to export these functions to the routing script, add them to an array of the type cmd_export_t and make the array null terminated ({0,0,0,0,0,0}). As an example assume the first function with one argument and the second function without any argument (same name will be used in the routing script but different names in the C code).

static cmd_export_t cmds[]={

{new_function“,(cmd_function)new_function,1,do_fixup,0, REQUEST_ROUTE|ONREPLY_ROUTE},
{new_function“,(cmd_function)new_function0,0,0,0, REQUEST_ROUTE|ONREPLY_ROUTE},
{0,0,0,0,0,0}
}

In the routing script, these functions can be called only within REQUEST_ROUTE or ONREPLY_ROUTE. Otherwise as i mentioned above, you will get an error messages and OpenSIPS will not start.

The fixup function “do_fixup” is populated here so it will fix up the only one parameter of the exported function “new_function”.

Somewhere in the module C code, You will write the bodies of the actual exported functions: The first function “new_function” which has only one parameter to be passed from the script will look like this:

int new_function(struct sip_msg *msg,char *param){

…….

/* The do_fixup is the fixup function of this function, so we can cast the param to str * as it is evaluated there */

str * param_value = (str *)param; 

……..

}

The second function is new_function0 which has no parameters when it is called from the routing script.

int new_function0(struct sip_msg *msg){

…….

}

How to Export a Parameter to the Routing Script ?

The field “params” in the definition of the module interface is an array of parameters which are exported from the module to the routing script so they can be used in the script and populated by script parser. The exported parameter can be set from the routing script. The OpenSIPS-defined C-Type of the exported parameter is “param_export_t” and it is defined in the file “sr_module.h” as following:

typedef struct param_export_ param_export_t;

struct param_export_ {
char* name;                  /*!< null terminated param. name */
modparam_t type;        /*!< param. type */
void* param_pointer;    /*!< pointer to the param. memory location */
}
The field name is the name of the parameter which will be used in the script. The type can be:

  • A regular string (Flag: STR_PARAM) or regular integer (Flag: INT_PARAM)
  • Or one of the above types (STR_PARAM or  INT_PARAM) combined with the flag USE_FUNC_PARAM (STR_PARAM|USE_FUNC_PARAM) or (INT_PARAM|USE_FUNC_PARAM). This means there is a function will be called every time the parameter is set.

The field param_pointer which is a pointer to either the memory location of the parameter or a pointer to the function which will be called on the value given from the routing script.

All exported parameters will be grouped in “params” in “exports” (actual interface). Remember the OpenSIPS-defined C-Type of the params array is param_export_t*. For example it can be:

static param_export_t params[]={
{ “new_param”, STR_PARAM|USE_FUNC_PARAM, (void *)set_function},
{ “db_url”,     STR_PARAM, &db_url.s},
{0,0,0}
}

The name of the first exported parameter is the string “new_param”. The type of this parameter is “STR_PARAM|USE_FUNC_PARAM” (i.e. “|” is a bit mask).  The function “set_function” will be called each time the parameter “new_param” is set. It look like this:

int set_function(unsigned int type, void *val){

…….

}

In the routing script this parameter can be set as following: modparam(“new_module”,”new_param”,”Test Value”) and the function “set_function” will get the value of “new_param” parameter (“Test Value”) as its argument.


Note

Start playing with OpenSIPS Source code (read in the code, make some changes, write log statements (e.g. LM_INFO(….)), recompile&start OpenSIPS, and read the log and see the results). If you want to build your own module, I suggest you to take as start step the OpenSIPS Example which is available on the OpenSIPS web site and make any OpenSIPS module like dialog module (Files: dialog.c and dialog.h) as a reference to your code (follow the same strategy).

조회 수 :
2392
등록일 :
2017.12.07
20:12:31 (*.160.88.18)
엮인글 :
http://webs.co.kr/index.php?document_srl=3312346&act=trackback&key=2ed
게시글 주소 :
http://webs.co.kr/index.php?document_srl=3312346
List of Articles
번호 제목 글쓴이 날짜 조회 수
160 Busy Lamp Field (BLF) feature on Opensips 2.4.0 with Zoiper configuration admin 2018-05-29 528
159 Documentation -> Tutorials -> WebSocket Transport using OpenSIPS admin 2018-05-17 505
158 List of SIP response codes admin 2017-12-20 1944
157 opensips/modules/event_routing/ Push Notification Call pickup admin 2017-12-20 1665
156 opensips push notification How to detail file admin 2017-12-20 1591
155 OpenSIPS routing logic admin 2017-12-12 1675
154 OpenSIPS example configuration admin 2017-12-12 1600
153 opensips log output admin 2017-12-11 1593
152 opensips complete configuration example admin 2017-12-10 1686
151 Opensips1.6 ebook detail configuration and SIP signal and NAT etc file admin 2017-12-10 1757
150 dictionary.opensips radius admin 2017-12-09 2353
149 what is record_route() in opensips ? admin 2017-12-09 2324
148 what is loose_route() in opensips ? file admin 2017-12-09 2333
147 in opensips what is lookup(domain [, flags [, aor]]) admin 2017-12-09 2337
146 in opensips db_does_uri_exist() what is admin 2017-12-09 2263
145 in opensips what is has_totag() admin 2017-12-09 2342
144 opensips exec module admin 2017-12-08 2408
143 opensips push notification How to admin 2017-12-07 2328
» OpenSIPS Module Interface admin 2017-12-07 2392
141 opensips configuration config explain easy basic 오픈쉽스 컨피그레이션 기본 설명 file admin 2017-12-07 2436
140 openssl 을 이용한 인증서 생성 절차를 정리한다. 개인키 CSR SSL 인증서 파일 생성 admin 2017-09-14 3338
139 Documentation -> Tutorials -> TLS opensips.cfg admin 2017-09-14 3321
138 Using TLS in OpenSIPS v2.2.x admin 2017-09-14 3249
137 opensips tls cfg admin 2017-09-14 3446
136 How to setup a Jabber / XMPP server on Debian 8 (jessie) using ejabberd admin 2017-09-13 3691
135 SIP to XMPP Gateway + SIP Presence Server opensips admin 2017-09-13 3317
134 OpenSIPS command line tricks admin 2017-09-13 3274
133 Fail2Ban Freeswitch How to secure admin 2017-09-12 3412
132 opensips.cfg. sample admin 2017-09-12 3255
131 Advanced SIP scenarios with Event-based-Routing admin 2017-09-11 3378
130 PUSH SERVER 푸시서버 안드로이드 애플 admin 2017-09-11 3596
129 오픈소스 (사내)메신저 서버 구축, 오픈 파이어(openfire) 설치방법과 세팅(리눅스 기준) admin 2017-09-09 5932
128 rtpengine config basic and opensips configuration and command admin 2017-09-06 3437
127 WebSocket Transport using OpenSIPS configuration 웹 소켓 컨피그레이션 기본 admin 2017-09-06 3362
126 OpenSIPS basic configuration script 기본 컨피그 admin 2017-09-05 3421
125 rtpengine install and config admin 2017-09-05 3444
124 Installing RTPEngine on Ubuntu 14.04 admin 2017-09-05 3524
123 compile only the textops module make modules=modules/textops modules admin 2017-09-05 3445
122 opensips command /sbin/opensipsctl detail admin 2017-09-04 3499
121 2017 08 31 opensips 2.32 install debian8.8 module install compile err modules admin 2017-09-04 3484
120 Build-Depends debian 8.8 opensips 2.3 admin 2017-09-04 3416
119 What is new in 2.3.0 opensips admin 2017-09-04 3753
118 ubuntu 安装配置opensips,rtpproxy,mediaproxy admin 2017-09-04 3726
117 How to install Mediaproxy 2.5.2 on CentOS 6 64 bit admin 2017-09-04 3788
116 Using TLS in OpenSIPS v2.2.x configuration admin 2017-09-04 3545
115 How to 2.3 download , OpenSIPS new apt repository. DEBs for Debian / Ubuntu admin 2017-09-02 3598
114 You can install CDRTool in the following ways: admin 2017-09-01 3663
113 How to Install OpenSIPS 2.1.2 Server on Ubuntu 15.04 admin 2017-09-01 3642
112 Opensips 2.32 download admin 2017-09-01 3470
111 OpenSIPS 2.3 install admin 2017-09-01 3692
110 JsSIP: The JavaScript SIP Library admin 2017-09-01 3620
109 WebSocket Transport using OpenSIPS admin 2017-09-01 3702
108 A2Billing and OpenSIPS – Part 1 admin 2017-08-29 3618
107 A2Billing and OpenSIPS – Part 2 admin 2017-08-29 3583
106 A2Billing and OpenSIPS – Part 3 admin 2017-08-29 3712
105 OpenSIPS 2.3 philosophy admin 2017-08-17 4051
104 The timeline for OpenSIPS 2.3 is admin 2017-08-17 4270
103 OpenSIPS Control Panel and Homer integration admin 2017-08-17 3924
102 Opensips sip capture re designed admin 2017-07-16 3920
101 WebRTC with OpenSIPS WebSocket is a protocol provides full-duplex admin 2015-04-04 8598
100 WebSocket Support in OpenSIPS 2.1 admin 2015-04-04 9101
99 OpenSIPS 2.1 (rc) is available, download now! admin 2015-03-22 8580
98 Service Provision Using Asterisk & OpenSIPS - AstriCon 2014 admin 2015-02-25 10199
97 SIP Signaling-Messages OpenSIPS Running On Multicore Server file admin 2014-11-02 17853
96 opensips.cfg for Asterisk admin 2014-10-20 20077
95 A2Billing and OpenSIPS config admin 2014-10-20 19326
94 Jitsi Videobridge meets WebRTC admin 2014-10-18 19620
93 A Survey of Open Source Products for Building a SIP Communication Platform admin 2014-10-18 19048
92 Script Function , Module Index v1.11 함수 모듈 opensips admin 2014-10-14 19242
91 Opensips TM module enables stateful processing of SIP transactions admin 2014-10-04 16921
90 kamailio.cfg configuration Example admin 2014-10-04 19135
89 opensips NAT Traversal Module admin 2014-10-02 18482
88 UAC Registrant Module admin 2014-09-28 20173
87 MediaProxy 2.3.x & OpenSIPS 1.5.x Integration admin 2014-08-24 19178
86 RTPPROXY Admin Guide admin 2014-08-24 19620
85 CANCEL MESSAGE not handled correctly admin 2014-08-23 19436
84 [Sipdroid] SIP data collection study tour admin 2014-08-23 19995
83 [OpenSIPS-Users] Opensips 1.10 NAT radius aaa admin 2014-08-23 19930
82 OpenSIPS Consultancy Pricing module install Server 판매 또는 설치및 컨설팅 가이드 admin 2014-08-23 19864
81 ICE: The ultimate way of beating NAT in SIP admin 2014-08-23 19509
80 Many OPENSIPS Configuration Examples This will Help you admin 2014-08-23 19180
79 Real-time Charging System for Telecom & ISP environments admin 2014-08-23 19899
78 OPENSIPS EBOOK admin 2014-08-21 20029
77 Opensips Documentation Function admin 2014-08-21 19930
76 Presence Tutorial OpenXCAP setup admin 2014-08-18 19143
75 Opensips Modules Documentation admin 2014-08-18 19978
74 A lightweight RPC library based on XML and HTTP admin 2014-08-18 19388
73 opensips Nat script with RTPPROXY - English Good perfect admin 2014-08-15 17549
72 OpenSIPS Control Panel (OCP) Installation Guide Good admin 2014-08-13 17515
71 Installation and configuration process record opensips opensips-cp admin 2014-08-13 39792
70 OpenSIPS as Homer Capture server admin 2014-08-13 17214
69 OpenSIPS , default script , Types of Routs , Routing in SIP, Video lecture admin 2014-08-13 19253
68 Configuracion de Kamailio 3.3 con NAT Traversal y XCAP. admin 2014-08-12 19753
67 Under RHEL6.5 install OpenSIPS 1.11.1 tls admin 2014-08-12 18713
66 OpenSIPS/OpenSER-a versatile SIP Server cfg admin 2014-08-11 19961
65 Kamailio Nat Traversal using RTPProxy admin 2014-08-11 19492
64 MediaProxy wiki page install configuration admin 2014-08-11 19554
63 오픈소스 (사내)메신저 서버 구축, 오픈 파이어(openfire) 설치방법과 세팅 admin 2014-08-11 30778
62 MediaProxy Installation Guide admin 2014-08-10 19169
61 RTPProxy 1.2.x Installation & Integration with OpenSIPS 1.5x admin 2014-08-10 20282
60 Opensips Installation, How to. Good guide wiki page admin 2014-08-10 17139
59 OpenSIPS Installation Notes admin 2014-08-09 16469
58 Installation and configuration process record opensips 1.9.1 admin 2014-08-09 22951
57 opensips 1.11.2 install Good Giide admin 2014-08-09 19940
56 fusionPBX install debian wheezy admin 2014-08-09 19359
55 opensips 1.11.2 install guide good 인스톨 가이드 admin 2014-08-09 19236
54 SigIMS IMS Platform admin 2014-05-24 20071
53 2013 2012년 분야별 최고의 오픈소스 소프트웨어 124선 admin 2014-04-05 22593
52 Video conference server OpenMCU-ru - Introduction admin 2014-04-01 22319
51 SIPSorcery admin 2014-03-18 20420
50 Ekiga (formely known as GnomeMeeting) is an open source SoftPhone admin 2014-03-12 20733
49 telepresence: Open Source SIP Telepresence/MCU admin 2014-03-12 28155
48 SIP PBX - OpenSIPS and Asterisk configuration admin 2014-03-12 22329
47 Conference Support in Kamailio (OpenSER) admin 2014-03-12 22732
46 OpenSIPS configuration for 2 or more FreeSWITCH installs admin 2014-03-12 18554
45 The Impact of TLS on SIP Server Performance file admin 2014-03-12 20519
44 book-opensips-101 / content / 3.2. SIP TLS Secure Calling.mediawiki admin 2014-03-12 19598
43 Where to check OpenSIPS does not start? admin 2014-03-09 19906
42 opensips-1.10.0_src.tar.gz experimental source code documentation admin 2014-03-09 20862
41 Kamailo OpenSIPs installation on Debian admin 2014-03-09 21068
40 Using the openSIPS Registrant Module admin 2014-03-09 21234
39 RTPproxy Frequentry Asked Questions (FAQ) ¶ admin 2014-03-07 19203
38 Building Telephony Systems with OpenSIPS 1.6 RTPProxy + OpenSIPS 1.7 admin 2014-03-07 20399
37 Installing RTPproxy Start RTPproxy in Bridged mode very good admin 2014-03-07 27212
36 OpenSIPS Control Panel (OCP) Installation Guide admin 2014-03-06 18778
35 OpenSIPS Control Panel install guide admin 2014-03-06 19799
34 rtpproxy Module admin 2014-03-06 20454
33 MediaProxy Installation Guide admin 2014-03-06 23121
32 How to install OpenSIPS on CentOS debian module add xcap admin 2014-03-06 21035
31 Problem with presence_xml module Opensips 1.9 admin 2014-03-06 20543
30 Building Telephony Systems with OpenSIPS 1.6 books file admin 2014-03-06 21458
29 Multimedia Service Platform admin 2014-03-06 19878
28 How to install OpenSIPS on CentOS Debian etc admin 2014-03-05 20831
27 Opensips Installation, How to. admin 2014-03-05 17208
26 100% CPU usage opensips admin 2014-03-05 20179
25 A2Billing and OpenSIPS admin 2014-03-04 21048
24 Opensips_1.9 install guide this is great I like this admin 2014-03-04 26363
23 Opensips install debian admin 2014-03-03 21101
22 Open Source VOIP applications, both clients and servers. admin 2013-11-20 21562
21 OfficeSIP Server is freeware VoIP, SIP server for Windows admin 2013-09-11 22592
20 My new toy: Bluebox-ng admin 2013-04-06 36061
19 Flooding Asterisk, Freeswitch and Kamailio with Metasploit admin 2013-04-06 33496
18 Asterisk Installation Asterisk Realtime configuration admin 2013-04-06 25220
17 The SIP Router Project admin 2013-04-06 24280
16 Kamailio :: A Quick Introduction admin 2013-04-06 21623
15 Welcome to the Smartvox Knowledgebase admin 2013-04-06 22082
14 Kamailio 3.3.x and Asterisk 10.7.0 Realtime Integration using Asterisk Database admin 2013-04-06 26177
13 OpenSIPS vs Asterisk admin 2013-04-06 51859
12 OpenSER_from_an_asterisk_POV file admin 2013-04-06 22063
11 Using SIP Devices behind NAT OPensip Asterisk IPPhone SIP Telephony file admin 2013-03-31 50458