opsips 分析流程

from:http://blog.sina.com.cn/s/blog_537d39500100v8yg.html


以下只分析记录关键路径和关键函数:

一、代码分析:
main.c

1、main()函数:

注册一些关键模块:
register_builtin_modules();

解析opensips.cfg配置文件
yyparse();
注:cfg脚本文件的解析是通过lex和yacc工具完成的,通过他们可以使opensips.cfg的执行速度达到c语言级别

枚举本服务器上的所有ip和本服务端口:
fix_all_socket_lists();

2、main_loop()函数:

进入到主要的主循环函数:
main_loop();

如果支持udp,则会启动这个循环:
udp_rcv_loop();

如果支持tcp,则首先调用tcp_init_children()初始化tcp子进程和父进程。

        
        if (tcp_init_children(&chd_rank)<0) goto error;

此处很关键,opensips在此函数中,调用socketpair()来构造unix socket对,
分别分配给父进程和子进程,也就是说真正的sip服务,是由子进程来完成的,示例:

“客户端请求->TCP父进程->sendmsg/recvmsg传递socket->子进程接收客户端请求->子进程进行业务处理”

因此每个子进程都在以下这个函数中循环等待父进程转发的请求:

            tcp_receive_loop(reader_fd[1]);
            exit(-1);
            
其过程参考的日志如下:
Sep 24 18:30:04 [14405] DBG:core:handle_io: >>>>>>>> come in  handle_io() in tcp_main file, fm->type=1, idx=-1
Sep 24 18:30:04 [14405] DBG:core:probe_max_sock_buff: getsockopt: snd is initially 262142
Sep 24 18:30:04 [14405] INFO:core:probe_max_sock_buff: using snd buffer of 255 kb
Sep 24 18:30:04 [14405] DBG:core:print_ip: tcpconn_new: new tcp connection to: 113.108.148.114
Sep 24 18:30:04 [14405] DBG:core:tcpconn_new: on port 13599, type 2
Sep 24 18:30:04 [14405] DBG:core:tcpconn_add: hashes: 366, 1
Sep 24 18:30:04 [14405] DBG:core:handle_new_connect: new connection: 0x2b0f383704c0 35 flags: 0002
Sep 24 18:30:04 [14405] DBG:core:send2child: to tcp child 0 0(14397), 0x2b0f383704c0
Sep 24 18:30:04 [14397] DBG:core:handle_io: received n=8 con=0x2b0f383704c0, fd=30
Sep 24 18:30:04 [14397] DBG:core:io_watch_add: io_watch_add(0x779b40, 30, 2, 0x2b0f383704c0), fd_no=1
Sep 24 18:30:04 [14397] DBG:core:tcp_read_req: >>>>>>>>> come in tcp_read_req function now !
Sep 24 18:30:04 [14397] DBG:core:tcp_read_req: content-length= 481
Sep 24 18:30:04 [14397] DBG:core:receive_msg: >>>> recv msg=            


在初始化完父子进程后,则启动外部连接的监听循环:
tcp_main_loop()

tcp_main_loop()此函数中多次调用io_watch_add(),用于添加所需要的监听端口;
    
进入main_loop循环,在此循环中,根据支持的epoll/select/poll等方式来等待客户端连接

    
    switch(io_h.poll_method){

以io_wait_loop_poll()为例继续分析:
在此函数中,循环检查所有的socket,当发现可读或可写之后,调用handle_io()函数处理。

                while((handle_io(get_fd_map(h, h->fd_array[r].fd), r) > 0)
                         && repeat);

tcp_main.c
    
当有新连接请求时,通过handle_io()函数中的handle_new_connect()处理:

tcp_main.c文件:
        case F_SOCKINFO:
            ret=handle_new_connect((struct socket_info*)fm->data);


在handle_new_connect()函数中,先接受客户端的请求,然后把此socket通过sendmsg方式,
发送给TCP子进程处理(请参考sendmsg/recvmsg来传递socket的方式 / SOL_SOCKET,参考上述章节部分描述):

        if(send2child(tcpconn)<0){

此时,父进程在tcp_receive_loop()函数的循环中捕获了这个请求,因此进入tcp_read.c文件中的tcp_read_req()函数,

tcp_read.c文件:
                    nfds--;
                    resp=tcp_read_req(con);

继续进入最重要的处理函数 receive_msg():

receive.c文件:
    
            } else if (receive_msg(req->start, req->parsed-req->start,
            &con->rcv)<0) {

在receive_msg()函数中,首先解析客户端的请求消息:

    if (parse_msg(buf,len, msg)!=0){
        LM_ERR("parse_msg failed\n");
        goto parse_error;
    }
    LM_DBG("After parse_msg...\n");

解析消息完成后,进入cfg配置文件的脚步处理过程,非常关键:

        
        run_top_route(rlist[DEFAULT_RT].a, msg);

此函数接连调用action.c文件中的函数:run_actions(), run_action_list(), do_action(),
最后的do_action()函数,负责对脚本的逐步解析和处理,由下一章节专门处理。

在receive_msg()函数处理完成后,总会看到这行日志:

    LM_DBG("cleaning up\n");


二、opensips.cfg的配置文件解析过程示例:
sip业务的处理逻辑,是由opensips.cfg配置文件后半部分的脚本来处理的,其中的
处理函数就是action.c文件中的do_action(),我在此函数开始部分增加了一行调试日志:

LM_DBG(">>>>>  do action !!, type = %d\n", (unsigned char)a->type);

有助于分析每一步脚本的执行,其中if代表type=14, 执行module函数代表type=15,xlog代表type=73等等,
可以参考以下日志来理解这个脚本的控制:

Sep 23 19:41:51 [4580] DBG:core:receive_msg: After parse_msg...
Sep 23 19:41:51 [4580] DBG:core:receive_msg: preparing to run routing scripts...
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14    //if (!mf_process_maxfwd_header("10")) {
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //if (!mf_process_maxfwd_header("10")) {
Sep 23 19:41:51 [4580] DBG:core:parse_headers: flags=100
Sep 23 19:41:51 [4580] DBG:core:parse_to: end of header reached, state=10
Sep 23 19:41:51 [4580] DBG:core:parse_to: display={}, ruri={sip:max@121.14.161.187}
Sep 23 19:41:51 [4580] DBG:core:get_hdr_field: <To> [26]; uri=[sip:max@121.14.161.187]
Sep 23 19:41:51 [4580] DBG:core:get_hdr_field: to body [<sip:max@121.14.161.187>
]
Sep 23 19:41:51 [4580] DBG:core:get_hdr_field: cseq <CSeq>: <21> <INVITE>
Sep 23 19:41:51 [4580] DBG:maxfwd:is_maxfwd_present: value = 70
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14    //if (has_totag()) {
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //if (has_totag()) {
Sep 23 19:41:51 [4580] DBG:uri:has_totag: no totag
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14    //if (is_method("CANCEL"))
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //if (is_method("CANCEL"))
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //t_check_trans();
Sep 23 19:41:51 [4580] DBG:core:parse_headers: flags=78
Sep 23 19:41:51 [4580] DBG:tm:t_lookup_request: start searching: hash=43376, isACK=0
Sep 23 19:41:51 [4580] DBG:tm:matching_3261: RFC3261 transaction matching failed
Sep 23 19:41:51 [4580] DBG:tm:t_lookup_request: no transaction found
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14    //if (!(method=="REGISTER") && from_uri==myself)
Sep 23 19:41:51 [4580] DBG:core:parse_to_param: tag=1829577472
Sep 23 19:41:51 [4580] DBG:core:parse_to: end of header reached, state=29
Sep 23 19:41:51 [4580] DBG:core:parse_to: display={}, ruri={sip:max5@121.14.161.187}
Sep 23 19:41:51 [4580] DBG:core:grep_sock_info: checking if host==us: 14==9 &&  [121.14.161.187] == [127.0.0.1]
Sep 23 19:41:51 [4580] DBG:core:grep_sock_info: checking if port 5060 matches port 5060
Sep 23 19:41:51 [4580] DBG:core:grep_sock_info: checking if host==us: 14==14 &&  [121.14.161.187] == [121.14.161.187]
Sep 23 19:41:51 [4580] DBG:core:grep_sock_info: checking if port 5060 matches port 5060
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14    // if (!proxy_authorize("", "subscriber")) {
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    // if (!proxy_authorize("", "subscriber")) {
Sep 23 19:41:51 [4580] DBG:auth:check_nonce: comparing [4e7c709d000000115b6b5404037523279ba28306b57e2ac9] and [4e7c709d000000115b6b5404037523279ba28306b57e2ac9]
Sep 23 19:41:51 [4580] DBG:db_mysql:has_stmt_ctx: ctx found for subscriber
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_do_prepared_query: conn=0x7c8610 (tail=8146720) MC=0x7c6028
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_do_prepared_query: set values for the statement run
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_val2bind: added val (0): len=4; type=254; is_null=0
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_do_prepared_query: doing BIND_PARAM in...
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_do_prepared_query: prepared statement has 1 columns in result
Sep 23 19:41:51 [4580] DBG:core:db_new_result: allocate 48 bytes for result set at 0x7d7cc0
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_get_columns: 1 columns returned from the query
Sep 23 19:41:51 [4580] DBG:core:db_allocate_columns: allocate 28 bytes for result columns at 0x7d7728
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_get_columns: RES_NAMES(0x7d7730)[0]=[password]
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_get_columns: use DB_STRING result type
Sep 23 19:41:51 [4580] DBG:core:db_allocate_rows: allocate 48 bytes for result rows and values at 0x7d79a0
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_str2val: converting STRING [max]
Sep 23 19:41:51 [4580] DBG:auth_db:get_ha1: HA1 string calculated: f213458fbed991c73058baa72389d249
Sep 23 19:41:51 [4580] DBG:auth:check_response: our result = 'e9dc8829d555d4f028ee2b3075ce9b3f'
Sep 23 19:41:51 [4580] DBG:auth:check_response: authorization is OK
Sep 23 19:41:51 [4580] DBG:auth:post_auth: nonce index= 17
Sep 23 19:41:51 [4580] DBG:core:db_free_columns: freeing result columns at 0x7d7728
Sep 23 19:41:51 [4580] DBG:core:db_free_rows: freeing 1 rows
Sep 23 19:41:51 [4580] DBG:core:db_free_row: freeing row values at 0x7d79b0
Sep 23 19:41:51 [4580] DBG:core:db_free_rows: freeing rows at 0x7d79a0
Sep 23 19:41:51 [4580] DBG:core:db_free_result: freeing result set at 0x7d7cc0
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14    //if (!db_check_from()) {
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //if (!db_check_from()) {
Sep 23 19:41:51 [4580] DBG:uri:check_username: Digest username and URI username match
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //consume_credentials();
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14    //if (loose_route()) {  
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //if (loose_route()) {  
Sep 23 19:41:51 [4580] DBG:core:parse_headers: flags=200
Sep 23 19:41:51 [4580] DBG:core:get_hdr_field: content_length=388
Sep 23 19:41:51 [4580] DBG:core:get_hdr_field: found end of header
Sep 23 19:41:51 [4580] DBG:rr:find_first_route: No Route headers found
Sep 23 19:41:51 [4580] DBG:rr:loose_route: There is no Route HF
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14    //if (!is_method("REGISTER|MESSAGE"))
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //if (is_method("INVITE")) {
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //record_route();
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14    //if (is_method("INVITE")) {
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //if (is_method("INVITE")) {
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 17      //setflag(1); # do accounting
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14      //if (!uri==myself)
Sep 23 19:41:51 [4580] DBG:core:grep_sock_info: checking if host==us: 14==9 &&  [121.14.161.187] == [127.0.0.1]
Sep 23 19:41:51 [4580] DBG:core:grep_sock_info: checking if port 5060 matches port 5060
Sep 23 19:41:51 [4580] DBG:core:grep_sock_info: checking if host==us: 14==14 &&  [121.14.161.187] == [121.14.161.187]
Sep 23 19:41:51 [4580] DBG:core:grep_sock_info: checking if port 5060 matches port 5060
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14    //if (is_method("PUBLISH"))
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //if (is_method("PUBLISH"))
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14    //if (is_method("REGISTER"))
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15      //if (is_method("REGISTER"))
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14      // if ($rU==NULL) {
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15      // alias_db_lookup("dbaliases");
Sep 23 19:41:51 [4580] DBG:core:db_new_result: allocate 48 bytes for result set at 0x7d92a0
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_get_columns: 2 columns returned from the query
Sep 23 19:41:51 [4580] DBG:core:db_allocate_columns: allocate 56 bytes for result columns at 0x7d9308
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_get_columns: RES_NAMES(0x7d9318)[0]=[username]
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_get_columns: use DB_STRING result type
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_get_columns: RES_NAMES(0x7d9328)[1]=[domain]
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_get_columns: use DB_STRING result type
Sep 23 19:41:51 [4580] DBG:db_mysql:db_mysql_convert_rows: no rows returned from the query
Sep 23 19:41:51 [4580] DBG:alias_db:alias_db_query: no alias found for R-URI
Sep 23 19:41:51 [4580] DBG:core:db_free_columns: freeing result columns at 0x7d9308
Sep 23 19:41:51 [4580] DBG:core:db_free_rows: freeing 0 rows
Sep 23 19:41:51 [4580] DBG:core:db_free_result: freeing result set at 0x7d92a0
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 73 // xlog("----------- come here before lookup -------------\n");
Sep 23 19:41:51 [4580] INFO:core:buf_init: initializing...
----------- come here before lookup -------------
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15   //if (!lookup("location","m")) {
Sep 23 19:41:51 [4580] DBG:registrar:lookup: setting as ruri <sip:max@192.168.161.36;line=dc4c7833fc0bc50>
Sep 23 19:41:51 [4580] DBG:registrar:lookup: looking for branches
Sep 23 19:41:51 [4580] DBG:registrar:lookup: setting branch <sip:max@113.108.148.114:3879;line=dc4c7833fc0bc50>
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 73    // xlog("----------- come here before setflag -----------");
----------- come here before setflag -------------
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 17     //setflag(2);
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 6    // route(1);   local function
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 73    //xlog("----------- come here in route 1 -------------\n");
----------- come here in route 1 -------------
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14    //if (is_method("INVITE")) {
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //if (is_method("INVITE")) {    
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //t_on_branch("2");
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //t_on_reply("2");
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //t_on_failure("1");
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 14    //if (!t_relay()) {
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 15    //if (!t_relay()) {
Sep 23 19:41:51 [4580] DBG:tm:t_newtran: transaction on entrance=(nil)
Sep 23 19:41:51 [4580] DBG:core:parse_headers: flags=ffffffffffffffff
Sep 23 19:41:51 [4580] DBG:core:parse_headers: flags=78
Sep 23 19:41:51 [4580] DBG:tm:t_lookup_request: start searching: hash=43376, isACK=0
Sep 23 19:41:51 [4580] DBG:tm:matching_3261: RFC3261 transaction matching failed
Sep 23 19:41:51 [4580] DBG:tm:t_lookup_request: no transaction found
Sep 23 19:41:51 [4580] DBG:tm:run_reqin_callbacks: trans=0x2b3767cd3a20, callback type 1, id 0 entered
Sep 23 19:41:51 [4580] DBG:core:parse_headers: flags=78
Sep 23 19:41:51 [4580] DBG:core:parse_headers: flags=ffffffffffffffff
Sep 23 19:41:51 [4580] DBG:core:check_ip_address: params 113.108.148.114, 192.168.172.142, 0
Sep 23 19:41:51 [4580] DBG:core:_shm_resize: resize(0) called
Sep 23 19:41:51 [4580] DBG:core:tcp_send: tcp connection found (0x2b3767cc3880), acquiring fd
Sep 23 19:41:51 [4580] DBG:core:tcp_send: c= 0x2b3767cc3880, n=16
Sep 23 19:41:51 [4589] DBG:core:handle_ser_child: read response= 2b3767cc3880, 1, fd -1 from 16 (4580)
Sep 23 19:41:51 [4580] DBG:core:tcp_send: after receive_fd: c= 0x2b3767cc3880 n=8 fd=31
Sep 23 19:41:51 [4580] DBG:core:tcp_send: sending...
Sep 23 19:41:51 [4580] DBG:core:tcp_send: after write: c= 0x2b3767cc3880 n=308 fd=31
Sep 23 19:41:51 [4580] DBG:core:tcp_send: buf=
SIP/2.0 100 Giving a try
Via: SIP/2.0/TCP 192.168.172.142:1168;rport=6767;branch=z9hG4bK2121284540;received=113.108.148.114
From: <sip:max5@121.14.161.187>;tag=1829577472
To: <sip:max@121.14.161.187>
Call-ID: 94874130
CSeq: 21 INVITE
Server: OpenSIPS (1.7.0-notls (x86_64/linux))
Content-Length: 0


Sep 23 19:41:51 [4580] DBG:tm:_reply_light: reply sent out. buf=0x7da3a0: SIP/2.0 1..., shmem=0x2b3767cd6fb8: SIP/2.0 1
Sep 23 19:41:51 [4580] DBG:tm:_reply_light: finished
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 73
new branch at sip:max@192.168.161.36;line=dc4c7833fc0bc50
Sep 23 19:41:51 [4580] DBG:core:mk_proxy: >>>> doing DNS lookup...name=⊕}, port=0
Sep 23 19:41:51 [4580] DBG:core:build_req_buf_from_sip_req: id added: <;i=4>, rcv proto=2
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 73
new branch at sip:max@113.108.148.114:3879;line=dc4c7833fc0bc50
Sep 23 19:41:51 [4580] DBG:core:mk_proxy: >>>> doing DNS lookup...name=甫}, port=3879
Sep 23 19:41:51 [4580] DBG:core:build_req_buf_from_sip_req: id added: <;i=4>, rcv proto=2
Sep 23 19:41:51 [4580] DBG:tm:set_timer: relative timeout is 500000
Sep 23 19:41:51 [4580] DBG:tm:insert_timer_unsafe: [4]: 0x2b3767cd3c40 (447300000)
Sep 23 19:41:51 [4580] DBG:tm:set_timer: relative timeout is 30
Sep 23 19:41:51 [4580] DBG:tm:insert_timer_unsafe: [0]: 0x2b3767cd3c70 (476)
Sep 23 19:41:51 [4580] DBG:tm:set_timer: relative timeout is 500000
Sep 23 19:41:51 [4580] DBG:tm:insert_timer_unsafe: [4]: 0x2b3767cd3e40 (447300000)
Sep 23 19:41:51 [4580] DBG:tm:set_timer: relative timeout is 30
Sep 23 19:41:51 [4580] DBG:tm:insert_timer_unsafe: [0]: 0x2b3767cd3e70 (476)
Sep 23 19:41:51 [4580] DBG:tm:t_relay_to: new transaction fwd'ed
Sep 23 19:41:51 [4580] DBG:core:do_action: >>>>>  do action !!, type = 42
Sep 23 19:41:51 [4580] DBG:tm:t_unref: UNREF_UNSAFE: [0x2b3767cd3a20] after is 0
Sep 23 19:41:51 [4580] DBG:core:destroy_avp_list: destroying list (nil)
Sep 23 19:41:51 [4580] DBG:core:receive_msg: cleaning up


三、我的配置文件参考如下,可以对应上述的日志进行分析:

#
# $Id: opensips.cfg 8141 2011-07-08 12:17:13Z vladut-paiu $
#
# OpenSIPS basic configuration script
    by Anca Vamanu <anca@voice-system.ro>
#
# Please refer to the Core CookBook at:
     http://www.opensips.org/Resources/DocsCookbooks
# for a explanation of possible statements, functions and parameters.
#


####### Global Parameters #########

debug=10
log_stderror=yes
log_facility=LOG_LOCAL0

fork=yes
children=4


#debug=6
#fork=no
#log_stderror=yes


#disable_tcp=yes


#disable_dns_blacklist=no


#dns_try_ipv6=yes


#auto_aliases=no


#disable_tls = no
#listen = tls:your_IP:5061
#tls_verify_server = 1
#tls_verify_client = 1
#tls_require_client_certificate = 0
#tls_method = TLSv1
#tls_certificate = "/home/max/opensips//etc/opensips/tls/user/user-cert.pem"
#tls_private_key = "/home/max/opensips//etc/opensips/tls/user/user-privkey.pem"
#tls_ca_list = "/home/max/opensips//etc/opensips/tls/user/user-calist.pem"

# default db_url to be used by modules requiring DB connection
db_default_url="mysql://opensips:open2011@127.0.0.1:8306/opensips"


port=5060


#listen=udp:192.168.1.2:5060


####### Modules Section ########

#set module path
mpath="/home/max/opensips//lib64/opensips/modules/"


loadmodule "db_mysql.so"
loadmodule "signaling.so"
loadmodule "sl.so"
loadmodule "tm.so"
loadmodule "rr.so"
loadmodule "maxfwd.so"
loadmodule "usrloc.so"
loadmodule "registrar.so"
loadmodule "textops.so"
loadmodule "mi_fifo.so"
loadmodule "uri.so"
loadmodule "acc.so"

loadmodule "auth.so"
loadmodule "auth_db.so"

loadmodule "alias_db.so"

loadmodule "domain.so"

#loadmodule "presence.so"
#loadmodule "presence_xml.so"


# ----------------- setting module-specific parameters ---------------


# ----- mi_fifo params -----
modparam("mi_fifo", "fifo_name", "/tmp/opensips_fifo")


# ----- rr params -----
# do not append from tag to the RR (no need for this script)
modparam("rr", "append_fromtag", 0)


# ----- registrar params -----

#modparam("registrar", "max_contacts", 10)


# ----- usrloc params -----
#modparam("usrloc", "db_mode",   0)

#modparam("usrloc", "db_mode",   2)
#modparam("usrloc", "db_url",
   "mysql://opensips:open2011@127.0.0.1:8306/opensips")


# ----- uri params -----
modparam("uri", "use_uri_table", 0)


# ----- acc params -----

modparam("acc", "early_media", 1)
modparam("acc", "report_cancels", 1)

modparam("acc", "detect_direction", 0)

modparam("acc", "failed_transaction_flag", 3)
modparam("acc", "log_flag", 1)
modparam("acc", "log_missed_flag", 2)

modparam("acc", "db_flag", 1)
modparam("acc", "db_missed_flag", 2)


# ----- auth_db params -----

modparam("auth_db", "calculate_ha1", yes)
modparam("auth_db", "password_column", "password")
modparam("auth_db", "db_url",
    "mysql://opensips:open2011@127.0.0.1:8306/opensips")
modparam("auth_db", "load_credentials", "")


# ----- alias_db params -----

modparam("alias_db", "db_url",
    "mysql://opensips:open2011@127.0.0.1:8306/opensips")


# ----- domain params -----

#modparam("domain", "db_url",
   "mysql://opensips:open2011@127.0.0.1:8306/opensips")
#modparam("domain", "db_mode", 1)   # Use caching


# ----- multi-module params -----

#modparam("auth_db|usrloc|uri", "use_domain", 1)


# ----- presence params -----

#modparam("presence|presence_xml", "db_url",
   "mysql://opensips:open2011@127.0.0.1:8306/opensips")
#modparam("presence_xml", "force_active", 1)
#modparam("presence", "server_address", "sip:192.168.1.2:5060")


####### Routing Logic ########


# main request routing logic

route{

    if (!mf_process_maxfwd_header("10")) {
        sl_send_reply("483","Too Many Hops");
        exit;
    }

    if (has_totag()) {
        # sequential request withing a dialog should
        # take the path determined by record-routing
        if (loose_route()) {
            if (is_method("BYE")) {
                setflag(1); # do accounting ...
                setflag(3); # ... even if the transaction fails
            } else if (is_method("INVITE")) {
                # even if in most of the cases is useless, do RR for
                # re-INVITEs alos, as some buggy clients do change route set
                # during the dialog.
                record_route();
            }
            # route it out to whatever destination was set by loose_route()
            # in $du (destination URI).
            route(1);
        } else {
            
            ##if (is_method("SUBSCRIBE") && $rd == "your.server.ip.address") {
            ##    # in-dialog subscribe requests
            ##    route(2);
            ##    exit;
            ##}
            if ( is_method("ACK") ) {
                if ( t_check_trans() ) {
                    # non loose-route, but stateful ACK; must be an ACK after
                    # a 487 or e.g. 404 from upstream server
                    t_relay();
                    exit;
                } else {
                    # ACK without matching transaction ->
                    # ignore and discard
                    exit;
                }
            }
            sl_send_reply("404","Not here");
        }
        exit;
    }

    #initial requests

    # CANCEL processing
    if (is_method("CANCEL"))
    {
        if (t_check_trans())
            t_relay();
        exit;
    }

    t_check_trans();

    # authenticate if from local subscriber (uncomment to enable auth)
    # authenticate all initial non-REGISTER request that pretend to be
    # generated by local subscriber (domain from FROM URI is local)
    if (!(method=="REGISTER") && from_uri==myself)
    ##if (!(method=="REGISTER") && is_from_local()) 
    {
        if (!proxy_authorize("", "subscriber")) {
            proxy_challenge("", "0");
            exit;
        }
        if (!db_check_from()) {
            sl_send_reply("403","Forbidden auth ID");
            exit;
        }
    
        consume_credentials();
        # caller authenticated
    }

    # preloaded route checking
    if (loose_route()) {
        xlog("L_ERR",
        "Attempt to route with preloaded Route's [$fu/$tu/$ru/$ci]");
        if (!is_method("ACK"))
            sl_send_reply("403","Preload Route denied");
        exit;
    }

    # record routing
    if (!is_method("REGISTER|MESSAGE"))
        record_route();

    # account only INVITEs
    if (is_method("INVITE")) {
        setflag(1); # do accounting
    }
    if (!uri==myself)
    ## replace with following line if multi-domain support is used
    ##if (!is_uri_host_local())
    {
        append_hf("P-hint: outbound\r\n");
        # if you have some interdomain connections via TLS
        ##if($rd=="tls_domain1.net") {
        ##    t_relay("tls:domain1.net");
        ##    exit;
        ##} else if($rd=="tls_domain2.net") {
        ##    t_relay("tls:domain2.net");
        ##    exit;
        ##}
        route(1);
    }

    # requests for my domain

    ## uncomment this if you want to enable presence server
    ##   and comment the next 'if' block
    ##   NOTE: uncomment also the definition of route[2] from  below
    ##if( is_method("PUBLISH|SUBSCRIBE"))
    ##        route(2);

    if (is_method("PUBLISH"))
    {
        sl_send_reply("503", "Service Unavailable");
        exit;
    }
    

    if (is_method("REGISTER"))
    {
        # authenticate the REGISTER requests (uncomment to enable auth)
        if (!www_authorize("", "subscriber"))
        {
            www_challenge("", "0");
            exit;
        }
        
        if (!db_check_to())
        {
            sl_send_reply("403","Forbidden auth ID");
            exit;
        }

        if (!save("location"))
            sl_reply_error();

        exit;
    }

    if ($rU==NULL) {
        # request with no Username in RURI
        sl_send_reply("484","Address Incomplete");
        exit;
    }

    # apply DB based aliases (uncomment to enable)
    alias_db_lookup("dbaliases");

    xlog("----------- come here before lookup -------------\n");
    # do lookup with method filtering
    if (!lookup("location","m")) {
        switch ($retcode) {
            case -1:
            case -3:
                xlog("----------- come here before t_newtran -------------\n");
                t_newtran();
                t_reply("404", "Not Found");
                exit;
            case -2:
                sl_send_reply("405", "Method Not Allowed");
                exit;
        }
    }

    xlog("----------- come here before setflag -------------\n");

    # when routing via usrloc, log the missed calls also
    setflag(2);

    route(1);
}


route[1] {

    xlog("----------- come here in route 1 -------------\n");
    
    # for INVITEs enable some additional helper routes
    if (is_method("INVITE")) {
        t_on_branch("2");
        t_on_reply("2");
        t_on_failure("1");
    }

    if (!t_relay()) {
        sl_reply_error();
    };
    exit;
}


# Presence route

#route[2]
#{
   if (!t_newtran())
   {
       sl_reply_error();
       exit;
   };
#
   if(is_method("PUBLISH"))
   {
       handle_publish();
   }
   else
   if( is_method("SUBSCRIBE"))
   {
       handle_subscribe();
   }
#
   exit;
#}


branch_route[2] {
    xlog("new branch at $ru\n");
}


onreply_route[2] {
    xlog("incoming reply\n");
}


failure_route[1] {
    if (t_was_cancelled()) {
        exit;
    }

    # uncomment the following lines if you want to block client
    # redirect based on 3xx replies.
    ##if (t_check_status("3[0-9][0-9]")) {
    ##t_reply("404","Not found");
    ##    exit;
    ##}

    # uncomment the following lines if you want to redirect the failed
    # calls to a different new destination
    ##if (t_check_status("486|408")) {
    ##    sethostport("192.168.2.100:5060");
    ##    # do not set the missed call flag again
    ##    t_relay();
    ##}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值