Konstas.Engineering Notes
Networking · Cybersecurity · Infrastructure
Telecom

Asterisk PBX Architecture: Channel Drivers, Dialplan Engine, and AGI Integration

Articles · 2026 · Ioannis (John) Konstas

← All Articles

Core Asterisk components — chan_pjsip, extensions.conf dialplan logic, AGI scripting, AMI/ARI interfaces, CDR to MySQL, and STIR/SHAKEN configuration.

What Is Asterisk

The open source telephony engine that powers millions of phone systems and VoIP carriers worldwide.

Asterisk is an open source PBX (Private Branch Exchange) and telephony toolkit that runs on Linux. It turns a standard server into a communications engine capable of handling VoIP calls, IVR menus, voicemail, call queuing, conferencing, and call recording. A single Asterisk node on an 8-core / 16GB server handles approximately 300–500 concurrent calls using G.711 passthrough.

┌─────────────────────────────────────────────────────┐
│                    ASTERISK                         │
│                                                     │
│  ┌──────────────┐    ┌────────────────────────┐   │
│  │  Channel     │    │      Dialplan Engine      │   │
│  │  Drivers     │───►│  extensions.conf / AEL  │   │
│  │  (PJSIP/DAHDI)│    │  Lua / AGI scripts       │   │
│  └──────────────┘    └────────────▬───────────┘   │
│                                   │                 │
│  ┌──────────────┐    ┌──────────▼───────────┐   │
│  │  Codec       │    │      Applications         │   │
│  │  Modules     │    │  Dial, Queue, Voicemail  │   │
│  │  G.711/G.729 │    │  Playback, Record, AGI   │   │
│  └──────────────┘    └──────────▬───────────┘   │
│                                   │                 │
│  ┌──────────────┐    ┌──────────▼───────────┐   │
│  │  CDR/CEL     │    │      AMI / ARI            │   │
│  │  MySQL/CSV   │    │  Management Interface     │   │
│  └──────────────┘    └────────────────────────┘   │
└─────────────────────────────────────────────────────┘

Channel Drivers

Channel drivers are loadable modules that connect Asterisk to the outside world via specific protocols.

DriverProtocolUse Case
chan_pjsipSIP/TLSAll VoIP trunks and endpoints — modern, recommended for all new deployments
chan_sipSIPLegacy — avoid for new deployments; removed in Asterisk 21+
chan_dahdiTDM/SS7Physical telephone lines, T1/E1 circuits, PSTN gateway cards

chan_sip is deprecated. Asterisk 21 removes it entirely. All new deployments must use chan_pjsip. Migrating is a configuration rewrite — the module architecture is fundamentally different.

The Dialplan (extensions.conf)

The dialplan is the routing brain of Asterisk — it defines exactly what happens when any call arrives.

Every call flows through the dialplan sequentially. The dialplan is organized into contexts (logical groupings), extensions (matching patterns), and priorities (numbered steps within an extension).

[from-trunk]
; Inbound call from SIP carrier
exten => _NXXNXXXXXX,1,NoOp(Inbound: ${EXTEN})
exten => _NXXNXXXXXX,n,AGI(a2billing.php,1)     ; Check balance / authorise
exten => _NXXNXXXXXX,n,Dial(PJSIP/${EXTEN}@internal,30)
exten => _NXXNXXXXXX,n,VoiceMail(${EXTEN}@default)
exten => _NXXNXXXXXX,n,Hangup()

[from-internal]
; Outbound call from customer extension
exten => _1NXXNXXXXXX,1,AGI(a2billing.php,1)    ; Deduct credit
exten => _1NXXNXXXXXX,n,Dial(PJSIP/${EXTEN}@carrier-trunk,60)
exten => _1NXXNXXXXXX,n,Hangup()

The pattern _NXXNXXXXXX matches any 10-digit North American number (N = 2–9, X = 0–9). The n priority means "next" — Asterisk auto-increments the number, making reordering steps easy.

AGI — Asterisk Gateway Interface

AGI allows external scripts to control call flow in real time — the integration point for billing systems.

When Asterisk reaches an AGI() application in the dialplan, it spawns an external script (PHP, Python, Perl) and passes call control to it over stdin/stdout. The script can read call variables, play audio, gather DTMF input, and control what happens next.

A2Billing uses AGI to check customer balances before connecting a call, calculate the maximum allowed duration based on the destination rate, and deduct credit in real time as the call progresses. When the credit runs out, Asterisk plays a warning and hangs up.

AMI and ARI

AMI — Asterisk Manager Interface

AMI is a TCP socket interface (port 5038) that allows external applications to monitor and control Asterisk in real time. Commands can originate calls, transfer calls, hang up channels, and read call state. Events stream continuously from Asterisk — new calls, hangups, DTMF, registration changes.

ARI — Asterisk REST Interface

ARI is a modern REST + WebSocket API for building call control applications in any language. Where AGI handles a single call in the dialplan, ARI gives an application full control over channels, bridges, endpoints, and media from outside Asterisk.

CDR — Call Detail Records

Every call generates a CDR written to MySQL — the billing system's source of truth.

; cdr_mysql.conf
[global]
hostname=db-server
dbname=asteriskcdrdb
table=cdr
user=asterisk
password=<strong-password>
port=3306

Key CDR fields: calldate, clid (caller ID), src, dst, duration, billsec (billable seconds), disposition (ANSWERED/NO ANSWER/BUSY), uniqueid.

STIR/SHAKEN Configuration

Asterisk 16.15+ includes native STIR/SHAKEN support via the res_stir_shaken module.

; /etc/asterisk/stir_shaken.conf
[attestation]
global_disable = no

[certificate]
type = certificate
path = /etc/asterisk/stir_shaken/cert.pem
private_key = /etc/asterisk/stir_shaken/key.pem
attestation = A         ; A = full attestation (we know the customer)
origid = your-carrier-id

[verification]
type = verification
failure_action = continue   ; Continue call even if verification fails

STIR/SHAKEN certificates must be obtained from an FCC-approved STI-CA (TransNexus, Sansay, etc.) and require an active FCC 499 filing as a prerequisite.

Security Configuration

; Only accept SIP registrations from the Kamailio proxy
[acl]
type=acl
deny=0.0.0.0/0
permit=<kamailio-ip>/32

; fail2ban jail for Asterisk security log
[asterisk]
enabled = true
port = 5060,5061
filter = asterisk
logpath = /var/log/asterisk/security
maxretry = 5
bantime = 3600

In a carrier architecture, Asterisk should not be directly reachable from the internet. Kamailio acts as the SBC — all SIP enters through Kamailio, and Asterisk is locked to only accept from the Kamailio IP.

← All articles

Research and direction by Ioannis (John) Konstas; compiled and finalized with AI assistance.

👁· views