Asterisk内核.docx_第1页
Asterisk内核.docx_第2页
Asterisk内核.docx_第3页
Asterisk内核.docx_第4页
Asterisk内核.docx_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Asterisk 内核简介:/5dteam/archive/2010/09/16/1828087.htmlAsterisk是一个开源的pbx系统,在公开的资料中,很难找到asterisk内核系统的详细描述。因此,很有必要写一篇内核框架的描述文档,作为内部培训文档,相互学习提高。本文主要从三个层面来描述asterisk内核,即asterisk内核模块、内核启动过程、基本呼叫流程。一、 asterisk内核模块Asterisk由内部核心和外围动态可加载模块组成。内部核心由以下六个部分组成:PBX交换核心模块(PBX Switching Core)、调度和I/O管理模块(Scheduler and I/O Manager)、应用调用模块(Application Launcher)、编解码转换模块(Codec Translator)、动态模块加载器模块(Dynamic Module Loader)和CDR生成模块(CDR Core)。外围动态可加载模块包括以App_开始的Applications、以Func_开始的Functions、以Res_开始的Resources、以Chan_开始的channels、以Codec_开始的codec编解码模块等。1. 内核模块1) PBX交换核心模块(PBX Switching Core):l pbx.cpbx.c是asterisk的核心模块,每路呼叫都需要经过它调度。pbx实现了builtin applications,也就是内置的应用,比如最常见的Answer,Hangup, Background,Wait等等。struct ast_app是一个关键数据结构,它定义了注册builtin applications的结构。load_pbx函数用来注册builtin applications和一些命令行CLI命令(每个模块都有些CLI命令)。该函数在系统启动时被调用。pbx_exec是Answer/BackGround/Busy/Goto/GotoIf/Hangup/Set等builtin applications的执行入口函数,它被pbx_extension_helper调用。ast_pbx_start函数是每路呼叫的起点。2) 调度和I/O管理模块(Scheduler and I/O Manager):l Channel.c:Channel.c/channel.h定义了channel操作的结构体和接口函数。struct ast_channel_tech结构体是所有channel都要用到的关键结构体,它定义channel操作的一系列回调函数指针,如call、 hangup、answer等。每个channel模块都会定义ast_channel_tech的实体,并将各自的回调函数赋值给它。例如 chan_sip.c中定义如下:/*! brief Definition of this channel for PBX channel registration */static const struct ast_channel_tech sip_tech = .type = SIP, .description = Session Initiation Protocol (SIP), .capabilities = (AST_FORMAT_MAX_AUDIO _argv),以便程序重启时使用*/ /* Remember original args for restart */ if (argc sizeof(_argv) / sizeof(_argv0) - 1) fprintf(stderr, Truncating argument size to %dn, (int)(sizeof(_argv) / sizeof(_argv0) - 1); argc = sizeof(_argv) / sizeof(_argv0) - 1; for (x=0; xargc; x+) _argvx = argvx; _argvx = NULL; if (geteuid() != 0) isroot = 0;/*命令如果是rasterisk,设置AST_OPT_FLAG_NO_FORK和AST_OPT_FLAG_REMOTE标志位*/ /* if the progname is rasterisk consider it a remote console */ if (argv0 & (strstr(argv0, rasterisk) != NULL) ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_REMOTE); /*得到当前主机名,在启动时打印出来*/ if (gethostname(hostname, sizeof(hostname)-1) ast_copy_string(hostname, , sizeof(hostname);/*获取当前的进程标识*/ ast_mainpid = getpid();/*建立mu-law和a-law转换表*/ ast_ulaw_init(); ast_alaw_init();/*为FFT逆变换(傅立叶逆变换)做一些初始化,用于在zaptel里进行callerid的DTMF检测*/ callerid_init();/*初始化内置命令的_full_cmd字符串,并注册常用命令,ast_builtins_init() - ast_cli_register_multiple() - ast_cli_register() - _ast_cli_register() */ ast_builtins_init();/*初始化base64转换*/ ast_utils_init();/* tty/tdd初始化*/ tdd_init();/*设置用户历史命令的保存路径*/ if (getenv(HOME) snprintf(filename, sizeof(filename), %s/.asterisk_history, getenv(HOME); /* Check for options */*检查命令行的输入参数,匹配参数范围是“mtThfFdvVqprRgciInx:U:G:C:L:M:”,不同的参数输入走到不同的case分支处理。有几个v,verbose级别就增加几*/ while (c = getopt(argc, argv, mtThfFdvVqprRgciInx:U:G:C:L:M:) != -1) switch (c) #if HAVE_WORKING_FORK case F: ast_set_flag(&ast_options, AST_OPT_FLAG_ALWAYS_FORK); break; case f: ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK); break;#endif case d: option_debug+; ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK); break; case c: ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_CONSOLE); break; case n: ast_set_flag(&ast_options, AST_OPT_FLAG_NO_COLOR); break; case r: ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_REMOTE); break; case R: ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK | AST_OPT_FLAG_REMOTE | AST_OPT_FLAG_RECONNECT); break; case p: ast_set_flag(&ast_options, AST_OPT_FLAG_HIGH_PRIORITY); break; case v: option_verbose+; ast_set_flag(&ast_options, AST_OPT_FLAG_NO_FORK); break; case m: ast_set_flag(&ast_options, AST_OPT_FLAG_MUTE); break; case M: if (sscanf(optarg, %d, &option_maxcalls) != 1) | (option_maxcalls 0) option_maxcalls = 0; break; case L: if (sscanf(optarg, %lf, &option_maxload) != 1) | (option_maxload 0.0) option_maxload = 0.0; break; case q: ast_set_flag(&ast_options, AST_OPT_FLAG_QUIET); break; case t: ast_set_flag(&ast_options, AST_OPT_FLAG_CACHE_RECORD_FILES); break; case T: ast_set_flag(&ast_options, AST_OPT_FLAG_TIMESTAMP); break; case x: ast_set_flag(&ast_options, AST_OPT_FLAG_EXEC); xarg = ast_strdupa(optarg); break; case C: ast_copy_string(ast_config_AST_CONFIG_FILE, optarg, sizeof(ast_config_AST_CONFIG_FILE); ast_set_flag(&ast_options, AST_OPT_FLAG_OVERRIDE_CONFIG); break; case I: ast_set_flag(&ast_options, AST_OPT_FLAG_INTERNAL_TIMING); break; case i: ast_set_flag(&ast_options, AST_OPT_FLAG_INIT_KEYS); break; case g: ast_set_flag(&ast_options, AST_OPT_FLAG_DUMP_CORE); break; case h: show_cli_help(); exit(0); case V: show_version(); exit(0); case U: runuser = ast_strdupa(optarg); break; case G: rungroup = ast_strdupa(optarg); break; case ?: exit(1); /*如果用了-c或者-v或者-r并且没有-x cmd参数,则打印欢迎信息*/ if (ast_opt_console | option_verbose | (ast_opt_remote & !ast_opt_exec) ast_register_verbose(console_verboser); WELCOME_MESSAGE; /*如果没有开调试则简单打印Booting. */ if (ast_opt_console & !option_verbose) ast_verbose( Booting.n);/*显示控制台时,不论是本地还是远程,都不能使用-F参数,否则无效*/ if (ast_opt_always_fork & (ast_opt_remote | ast_opt_console) ast_log(LOG_WARNING, alwaysfork is not compatible with console or remote console mode; ignoredn); ast_clear_flag(&ast_options, AST_OPT_FLAG_ALWAYS_FORK); /* For remote connections, change the name of the remote connection. * We do this for the benefit of init scripts (which need to know if/when * the main asterisk process has died yet). */ if (ast_opt_remote) strcpy(argv0, rasterisk); for (x = 1; x gr_gid) ast_log(LOG_WARNING, Unable to setgid to %d (%s)n, (int)gr-gr_gid, rungroup); exit(1); if (setgroups(0, NULL) ast_log(LOG_WARNING, Unable to drop unneeded groupsn); exit(1); if (option_verbose) ast_verbose(Running as group %sn, rungroup); if (runuser & !ast_test_flag(&ast_options, AST_OPT_FLAG_REMOTE) #ifdef HAVE_CAP int has_cap = 1;#endif /* HAVE_CAP */ struct passwd *pw; pw = getpwnam(runuser); if (!pw) ast_log(LOG_WARNING, No such user %s!n, runuser); exit(1); #ifdef HAVE_CAP if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) ast_log(LOG_WARNING, Unable to keep capabilities.n); has_cap = 0; #endif /* HAVE_CAP */ if (!isroot & pw-pw_uid != geteuid() ast_log(LOG_ERROR, Asterisk started as nonroot, but runuser %s requested.n, runuser); exit(1); if (!rungroup) if (setgid(pw-pw_gid) ast_log(LOG_WARNING, Unable to setgid to %d!n, (int)pw-pw_gid); exit(1); if (isroot & initgroups(pw-pw_name, pw-pw_gid) ast_log(LOG_WARNING, Unable to init groups for %sn, runuse

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论