Asterisk is an open source PBX system and it is difficult to find a detailed description of the asterisk kernel system in public information. Therefore, it is necessary to write a core framework of the description of the document as an internal training document, learn from each other to improve.
This paper mainly describes the asterisk kernel from three levels, that is, asterisk kernel module, kernel boot process and basic call flow.
First, asterisk kernel module
The asterisk is composed of an internal core and a dynamic loadable module on the periphery. The internal core is comprised of the following six components: PBX Exchange Core Module (PBX switching core), dispatch and I/O management module (Scheduler and I/O Manager), Application call module (Application Launcher), Codec conversion module (CODEC Translator), dynamic module loader module (Dynamics module Loader), and CDR generation module (CDR Core).
The perimeter dynamic Loadable module includes applications starting with APP_, functions starting with func_, resources starting with Res_, Chan_ starting with channels, CODEC_ encoding and decoding modules starting with codec, and so on.
1. Kernel module
1 PBX Exchange Core module (PBX switching core):
L PBX.C
PBX.C is the core module of Asterisk, and every call needs to be dispatched. The PBX implements the Builtin applications, which is the built-in application, such as the most common Answer,hangup, background,wait, and so on.
struct Ast_app is a key data structure that defines the structure of a registered builtin applications.
The LOAD_PBX function is used to register builtin applications and some command-line CLI commands (some CLI commands for each module). The function is invoked when the system is started.
Pbx_exec is the execution entry function of Answer/background/busy/goto/gotoif/hangup/set et builtin applications, which is invoked by Pbx_extension_helper.
The Ast_pbx_start function is the starting point for each call.
2 Scheduling and I/O management modules (Scheduler and I/O Manager):
L CHANNEL.C:
Channel.c/channel.h defines the structure and interface functions of the Channel operation.
The struct Ast_channel_tech structure is a key structure used by all channel, which defines a series of callback function pointers for channel operations, such as call, Hangup, answer, and so on. Each channel module defines the Ast_channel_tech entity and assigns its own callback function to it. For example, the CHAN_SIP.C is defined as follows:
/*! \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 << 1)-1),
. Properties = Ast_chan_tp_wantsjitter | Ast_chan_tp_createsjitter,
. Requester = Sip_request_call,
. devicestate = Sip_devicestate,
. Call = Sip_call,
. Hangup = Sip_hangup,
. Answer = Sip_answer,
. Read = Sip_read,
. write = Sip_write,
. Write_video = Sip_write,
. indicate = Sip_indicate,
. Transfer = Sip_transfer,
. Fixup = Sip_fixup,
. Send_digit_begin = Sip_senddigit_begin,
. Send_digit_end = Sip_senddigit_end,
. Bridge = Ast_rtp_bridge,
. Send_text = Sip_sendtext,
. Func_channel_read = Acf_channel_read,
};
Functions such as call, Hangup, answer and so on in Ast_channel_tech are called by Ast_call, Ast_hangup, Ast_answer and so on.
The struct Ast_channel structure defines the channel context parameter, which is necessary for each channel that participates in the call, and invokes Ast_channel_alloc to request Ast_channel.
L IO.C
IO.C implements the I/O management of asterisk with external interaction, such as CHAN_SIP to receive SIP signaling from outside, invoke Ast_io_add to add IO interface, and invoke ast_io_wait to implement external message reception.
3 Application Call Module (Application Launcher):
A series of application invocation interfaces are defined in PBX.C.
The applications module defines the application callback function and registers it, in pbx.c, by invoking an application call interface callback execution.
The key function for applying the calling interface is Pbx_extension_helper, which executes dialplan, prints "executing ..." on the CLI, and throws the Ami event events, calling Pbx_exec to execute the application callback function.
4) Codec conversion module (CODEC Translator):
TRANSLATE.C:
struct Ast_translator: The encoding transformation describes the structure body, which defines the name of the encoding transformation, the callback function, and the run-time options.
struct AST_TRANS_PVT: The encoding Transformation context describes the structure body.
Ast_register_translator: The encoding transformation registers the interface function, for each code module to call, registers struct Ast_translator type the structure body variable.
Ast_unregister_translator: Encoding Conversion logout function
Ast_translate: The execution function of the encoding transformation.
Codec_gsm.c/codec_ ... : Corresponding coding and decoding execution module, such as G.711ALAW/G.711ULAW/GSM and so on.
5 Dynamic Module Loader module (Loader):
The module is mainly Module.h.
The struct AST_MODULE_INFO structure is defined in Module.h to hold the registration, logoff callback functions, and module description information of each module.
Load_module, Unload_module, the registration and cancellation functions of each application module, are defined by each module as static function.
Ast_module_info_standard: Register interface, logout interface, module description information and other module information. It is a macro definition that, when invoked by a dynamic module, first defines a __MOD_INFO static structure variable of type Ast_module_info, saves the module information, and defines __attribute__ ((constructor)) __reg_module and __ attribute__ ((destructor)) __unreg_module, called when the program starts and exits.
6 CDR Generation module (CDR Core):
CDR.C:
AST_CDR_REGISTER:CDR driver registration, for Cdr_mysql, and other calls, register the callback function saved by the order.
AST_CDR_ENGINE_INIT:CDR module initialization, registering CDR status, loading cdr.conf, starting CDR thread.
Ast_cdr_detach: An interface function that produces a statement that is invoked at the end of a call.
2. Peripheral loadable Module:
1) applications
Modules starting with APP_, such as APP_DIAL.C, APP_DB.C, app_queue.c, APP_RECORD.C, app_meetme.c, and so on, are stored in the Apps directory. Each application module defines the Load_module function and the Unload_module function, which are used to register and unregister the application respectively.
The Load_module function calls the Ast_register_application function, registering the application command, such as the App_dial module registration dial:res = ast_register_application (app, Dial_exec, synopsis, Descrip).
The Unload_module function calls the Ast_unregister_application function and logs out the application command.
Each application module uses Ast_module_info_standard macros to register the module information __mod_info. Ast_module_info_standard registers Load_module and Unload_module as callback functions that are invoked by the MODULE load/unload/reload.
2) Channel
Modules starting with Chan_, such as CHAN_SIP.C, chan_h323.c, Chan_mgcp.c, chan_iax2.c, CHAN_ZAP.C, etc., are stored in the channels directory.
Channel registration, logoff process and application are basically similar. Because each channel needs to interact with the outside, Do_monitor threads are enabled in Load_module to listen for external TCP/UDP ports and receive external messages.
Each channel also defines its own CLI commands and function commands, such as CHAN_SIP, which defines the CLI commands such as SIP Debug/history/no/notify/prune/reload/set/show and SIP_ function commands such as HEADER, Checksipdomain, Sippeer, Sipchaninfo.
3) functions
Modules starting with Fun_, such as FUN_DB.C, FUNC_MOH.C, FUNC_CDR.C, etc., are stored in the Funcs directory.
function registration and logoff procedures are similar to application.
Each function module also defines its own function commands, such as FUN_DB.C, which defines function commands such as DB, db_exists, and Db_delete.
Second, the asterisk start process
Mainly on main function to explain the asterisk startup process:
1 int main (int argc, char *argv[])
2
3 {
4
5 int C;
6
7 char filename[80] = "";
8
9 char Hostname[maxhostnamelen] = "";
10
One char tmp[80];
12
char * xarg = NULL;
14
int x;
16
FILE *f;
18
sigset_t sigs;
20
int num;
22
isroot int = 1;
24
*buf Char;
26
Char *runuser = NULL, *rungroup = NULL;
28
29/* Save command Line parameters (argv[]->_argv[]) so that the program can be restarted using the * *
30
*/* Remember original args for restart * *
32
if (argc > sizeof (_ARGV)/sizeof (_ARGV[0)-1) {
34
fprintf (stderr, "truncating argument size to%d\n", (int) (sizeof (_ARGV)/sizeof (_ARGV[0))-1);
36
Panax Notoginseng argc = sizeof (_ARGV)/sizeof (_argv[0])-1;
38
39}
40
A for (x=0; x<argc; x + +)
42
_ARGV[X] = argv[x];
44
_ARGV[X] = NULL;
46
if (Geteuid ()!= 0)
48
IsRoot = 0;
50
51/* Command If it is Rasterisk, set the ast_opt_flag_no_fork and ast_opt_flag_remote sign bit * *
52
* * If the progname is rasterisk consider it a remote console */
54
if (Argv[0] && (strstr (argv[0), "Rasterisk")!= NULL) {
56
Ast_set_flag (&ast_options, Ast_opt_flag_no_fork | Ast_opt_flag_remote);
58
59}
60
61/* Get current host name, print out at startup * *
62
if (GetHostName (hostname, sizeof (hostname)-1))
64
Ast_copy_string (hostname, "<Unknown>", sizeof (hostname));
66
67/* Get the current process ID * *
68
Ast_mainpid = Getpid ();
70
71/* Set up Mu-law and A-law conversion form * *
72
Ast_ulaw_init ();
74
Ast_alaw_init ();
76
77/* For FFT inverse transform (Fourier inverse transform) to do some initialization, for the Zaptel in the CallerID DTMF detection * *
78
Callerid_init ();
80
81/* Initialize the _full_cmd string for the built-in command, and register the common commands, Ast_builtins_init ()-> ast_cli_register_multiple ()-> ast_cli_register ()- > __ast_cli_register () * *
82
Ast_builtins_init ();
84
85/* Initialize BASE64 conversion * *
86
Ast_utils_init ();
88
*/* TTY/TDD initialization/
90
Tdd_init ();
92
93/* Set the save path for the user history command * *
94
if (getenv ("Home"))
96
snprintf (filename, sizeof (filename), "%s/.asterisk_history", getenv ("Home