编写opensips自定义模块

2024-04-25 09:18

本文主要是介绍编写opensips自定义模块,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

编写module需要用到的各种数据结构定义在sr_module.h。其中比较重要的是cmd_export_t,用于声明这 个module的导出函数

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 */
};

第一个参数是该module导出到opensips脚本中的函数名
第二个参数是对应的具体执行函数。cmd_function的函数定义为
typedef  int (*cmd_function)(struct sip_msg*, char*, char*, char*, char*, char*, char*);
从第二个参数起都是字符串。具体使用多少个参数由结构体第三个参数决定
对于cmd_function,返回值小于0表示函数执行出错,返回值等于0会使该次脚本执行结束;返回值大于0 表示函数执行正常

第四个参数fixup function,主要用于将脚本传入的参数转换成int、正则表达式等其他种类的参数。 opensips已经定义好了常用的类型转换,定义在mod_fix.h中,一般直接调用现成的即可以
第五个参数free_fixup_function,用于释放fixup function执行时申请的内存

第六个参数,用于描述该导出函数可以在哪段路由中被执行。可选的值包括
REQUEST_ROUTE, ONREPLY_ROUTE, FAILURE_ROUTE, BRANCH_ROUTE, LOCAL_ROUTE等


另外一个比较重要的结构体是

struct module_exports{
char* name;                     /*!< null terminated 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;             /*!< null terminated array of the exported
                                  commands */
param_export_t* params;         /*!< null terminated array of the exported
                                  module parameters */
stat_export_t* stats;           /*!< null terminated array of the exported
                                  module statistics */
mi_export_t* mi_cmds;           /*!< null terminated array of the exported
                                  MI functions */
pv_export_t* items;             /*!< null terminated array of the exported
                                  module items (pseudo-variables) */
proc_export_t* procs;           /*!< null terminated array of the additional
                                  processes reqired by the module */
init_function init_f;           /*!< Initialization function */
response_function response_f;   /*!< function used for responses,
                                  returns yes or no; can be null */
destroy_function destroy_f;     /*!< function called when the module should
                                  be "destroyed", e.g: on opensips exit */
child_init_function init_child_f;/*!< function called by all processes
                                   after the fork */
};


char* name: 模块的名字
char *version: 一般都把值设为MODULE_VERSION
char *compile_flags: 一般都把值设为DEFAULT_DLFLAGS
接着几个参数设置模块的导出函数、导出参数、MI命令等
proc_export_t* procs: 定义模块自己的独立运行进程
init_function init_f: 模块初始化化函数,只在opensips启动的时候被执行一次
response_function response_f: 很少有模块需要用到,尚不清楚有什么用
destroy_function destroy_f: 模块销毁时被调用
child_init_function init_child_f: 模块在每个子进程中的初始化函数。因为opensips的架构是多进 程的,像数据库连接这些必须在每个子进程内自己创建一份


对编写module有个基本的认识以后,可以开始着手写一个简单的module

1、在opensips/modules目录下新建一个目录test

2、编写这个test module的Makefile。如果这个module不需要连接其他额外的库,Makefile是相当的简 单,只要把NAME后面生成库的名字改一下

include ../../Makefile.defs
auto_gen=
NAME=test.so
LIBS=
include ../../Makefile.modules

3、编写这个模块的主体代码test.c
  
#include "../../sr_module.h"
static int mod_init(void);
/* Exported functions */
static int my_test(struct sip_msg* _msg, char *param);
static cmd_export_t cmds[] = {
{ "my_test", (cmd_function)my_test, 1, 0, 0,
REQUEST_ROUTE},
{0, 0, 0, 0, 0, 0}
};
/* Module interface */
struct module_exports exports= {
"test",  /* module name*/
MODULE_VERSION,
DEFAULT_DLFLAGS, /* dlopen flags */
cmds,       /* exported functions */
0,          /* module parameters */
0,          /* exported statistics */
0,          /* exported MI functions */
0,          /* exported pseudo-variables */
0,          /* extra processes */
mod_init,   /* module initialization function */
0,          /* response function */
0,          /* destroy function */
0,          /* per-child init function */
};
static int my_test(struct sip_msg* _msg, char *param)
{
LM_INFO("Receive message %s\n", param);
return 1;
}
static int mod_init(void)
{
LM_INFO("initializing...\n");
return 0;
}


4、建立好这个模块的目录之后,opensips就会去编译这个模块,除非你在Makefile.conf的 exclude_modules中加入了这个模块的名字。按照正常opensips的编译步骤进行编译


5、把新编写的模块加入路由之中
loadmodule "msg_trace.so"
在路由的某个地方插入
my_test("1234");

可以看到路由被执行到时,日志有输出
Aug  2 06:20:45 simope-0A ./opensips[2346]: INFO:test:my_test: Receive message 1234


主要参考textops、cachedb_local、cfgutils等较为简单的module

另外也可以参考同源的kamailio项目,虽然结构上也有细微的变换,但是kamailio的开发文档相对比较 齐全
http://www.asipto.com/pub/kamailio-devel-guide/#c16moduledev

这篇关于编写opensips自定义模块的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/934275

相关文章

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Python使用date模块进行日期处理的终极指南

《Python使用date模块进行日期处理的终极指南》在处理与时间相关的数据时,Python的date模块是开发者最趁手的工具之一,本文将用通俗的语言,结合真实案例,带您掌握date模块的六大核心功能... 目录引言一、date模块的核心功能1.1 日期表示1.2 日期计算1.3 日期比较二、六大常用方法详

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

python中time模块的常用方法及应用详解

《python中time模块的常用方法及应用详解》在Python开发中,时间处理是绕不开的刚需场景,从性能计时到定时任务,从日志记录到数据同步,时间模块始终是开发者最得力的工具之一,本文将通过真实案例... 目录一、时间基石:time.time()典型场景:程序性能分析进阶技巧:结合上下文管理器实现自动计时

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

SpringBoot自定义注解如何解决公共字段填充问题

《SpringBoot自定义注解如何解决公共字段填充问题》本文介绍了在系统开发中,如何使用AOP切面编程实现公共字段自动填充的功能,从而简化代码,通过自定义注解和切面类,可以统一处理创建时间和修改时间... 目录1.1 问题分析1.2 实现思路1.3 代码开发1.3.1 步骤一1.3.2 步骤二1.3.3

dubbo3 filter(过滤器)如何自定义过滤器

《dubbo3filter(过滤器)如何自定义过滤器》dubbo3filter(过滤器)类似于javaweb中的filter和springmvc中的intercaptor,用于在请求发送前或到达前进... 目录dubbo3 filter(过滤器)简介dubbo 过滤器运行时机自定义 filter第一种 @A

基于.NET编写工具类解决JSON乱码问题

《基于.NET编写工具类解决JSON乱码问题》在开发过程中,我们经常会遇到JSON数据处理的问题,尤其是在数据传输和解析过程中,很容易出现编码错误导致的乱码问题,下面我们就来编写一个.NET工具类来解... 目录问题背景核心原理工具类实现使用示例总结在开发过程中,我们经常会遇到jsON数据处理的问题,尤其是