You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Asterisk为SIP响应添加自定义头部?

Adding Custom Headers to SIP Responses & Forwarding Unmodified SIP Traffic in Asterisk

Great question—let’s break this down into two clear parts to address both your needs.

1. Adding Custom Headers to SIP Responses

You’re right that SipAddHeader() is commonly used for SIP requests, but you absolutely can add custom headers to responses too—the method just depends on whether you’re using the modern PJSIP driver (recommended) or the legacy chan_sip driver.

Use the PJSIP_HEADER(add) application in your dialplan to inject custom headers into outgoing responses. Here’s a quick example in your extensions.conf:

exten => _X.,1,NoOp(Handling incoming SIP call)
; Add a custom header to all responses generated for this call
exten => _X.,n,PJSIP_HEADER(add, X-My-Custom-Response-Header) "CustomValue123"
exten => _X.,n,Answer()
; Rest of your dialplan logic...

If you need to add headers to specific error responses (like 404 or 503), wrap the PJSIP_HEADER call in the appropriate error handling block:

exten => 404,1,PJSIP_HEADER(add, X-Error-Details) "Requested number not found"
exten => 404,n,Hangup()

For Legacy chan_sip

If you’re still using chan_sip, use the Set() application with the SIP_HEADER variable to attach custom headers to responses:

exten => _X.,1,Set(SIP_HEADER(X-My-Custom-Header)=MyResponseValue)
exten => _X.,n,Answer()

2. Forwarding SIP Traffic Without Asterisk Modifying It

To get Asterisk to pass through your custom SIP requests/responses directly to the target device (without regenerating messages or stripping custom fields), you’ll need to tweak your endpoint/trunk configurations and dialplan.

For PJSIP

Key configuration settings to enable full passthrough:

  • Endpoint/Trunk Config (in pjsip.conf):
    [target_trunk]
    type=endpoint
    context=from-sip
    disallow=all
    allow=ulaw,alaw  ; Match your codec requirements
    trust_id_inbound=yes  ; Preserve incoming From/To headers
    rewrite_contact=no  ; Don't rewrite the Contact header
    send_rpid=no  ; Avoid adding RPID headers
    forward_all_headers=yes  ; Pass all custom headers through
    direct_media=no  ; Ensure media flows through Asterisk if needed (skip if you want direct media)
    rtp_symmetric=yes  ; Handle symmetric RTP correctly
    
  • Dialplan Logic (in extensions.conf):
    Use a simple PJSIP_Dial call to forward the call without modifying it:
    [from-sip]
    exten => _X.,1,NoOp(Forwarding call to target device)
    exten => _X.,n,PJSIP_Dial(PJSIP/${EXTEN}@target_trunk)
    exten => _X.,n,Hangup()
    

For Legacy chan_sip

In sip.conf, adjust these settings for your trunk:

[target_trunk]
type=friend
context=from-sip
host=192.168.1.100  ; Target device IP
allowoverwrite=yes  ; Allow custom headers to pass through
rewritecontact=no  ; Don't modify Contact header
sendrpid=no
fromdomain=*  ; Preserve original From domain
disallow=all
allow=ulaw,alaw

Then use a simple Dial call in the dialplan:

[from-sip]
exten => _X.,1,Dial(SIP/${EXTEN}@target_trunk)

Important Notes

  • Full passthrough may disable some Asterisk features like call recording, call parking, or detailed CDRs, since Asterisk won’t parse or modify the SIP messages.
  • Test thoroughly to ensure custom headers are preserved end-to-end—use tools like tcpdump or Wireshark to verify SIP traffic.

内容的提问来源于stack exchange,提问作者UsernameGoesHere1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:07:26