[asterisk-users] Is there a way to get the remote User-Agent info from an outbound leg?

Alex Balashov abalashov at evaristesys.com
Sun Sep 14 17:32:21 CDT 2008


Eric Chamberlain wrote:

> On Sep 12, 2008, at 6:47 PM, Eric Chamberlain wrote:
> 
>> Is there a way to get the remote User-Agent info from an outbound leg?

The short answer is no.  SIP_HEADER() only considers inbound headers, 
and only from an INVITE request, not in-dialog replies (like 200 OK, or 
provisional).

A while ago, I had a need to get a custom SIP header from a device that 
inserted it into a 200 OK reply.  I accomplished this by making a 
one-line source change in Asterisk, so I'll share in case it helps you.

I was using 1.4.20, and modified the handle_response_invite() function 
in channels/chan_sip.c.  The header I needed was called "ALI-Id" which I 
stored in a built-in channel variable that is accessible from the dial 
plan (ALI_ID).

Modify the handler for 200 replies - see my comment in the code:

----------

         case 200: /* 200 OK on invite - someone's answering our call */
                 if (!ast_test_flag(req, SIP_PKT_IGNORE) && 
(p->invitestate != IN
V_CANCELLED) && sip_cancel_destroy(p))
                         ast_log(LOG_WARNING, "Unable to cancel SIP 
destruction.
  Expect bad things.\n");
                 p->authtries = 0;
                 if (find_sdp(req)) {
                         if ((res = process_sdp(p, req)) && 
!ast_test_flag(req, S
IP_PKT_IGNORE))
                                 if (!reinvite)
                                         /* This 200 OK's SDP is not 
acceptable,
so we need to ack, then hangup */
                                         /* For re-invites, we try to 
recover */
                                         ast_set_flag(&p->flags[0], 
SIP_PENDINGBY
E);
                 }

                 /* Parse contact header for continued conversation */
                 /* When we get 200 OK, we know which device (and IP) to 
contact
for this call */
                 /* This is important when we have a SIP proxy between 
us and the
  phone */
                 if (outgoing) {
                         update_call_counter(p, DEC_CALL_RINGING);
                         parse_ok_contact(p, req);
                         if(set_address_from_contact(p)) {
                                 /* Bad contact - we don't know how to 
reach this
  device */
                                 /* We need to ACK, but then send a bye */
                                 /* OEJ: Possible issue that may need a 
check:
                                         If we have a proxy route 
between us and
the device,
                                         should we care about resolving 
the conta
ct
                                         or should we just send it?
                                 */
                                 if (!ast_test_flag(req, SIP_PKT_IGNORE))
                                         ast_set_flag(&p->flags[0], 
SIP_PENDINGBY
E);
                         }

                         /* Save Record-Route for any later requests we 
make on t
his dialogue */
                         if (!reinvite)
                                 build_route(p, req, 1);

                         /**
                          * Evariste Systems/Alex Balashov - BEGIN CHANGES
                          *
                          * This attempts to fetch the ALI-Id header from
                          * the 200 OK request and set it as a channel 
variable
                          * that is accessible from the dial plan, if it 
exists.
                          */

                         pbx_builtin_setvar_helper(p->owner, "ALI_ID",
                                         get_header(req, "ALI-Id"));

                         /* END Evariste Systems changes */

----------

You should be able to do something similar for the user agent.

-- Alex

-- 
Alex Balashov
Evariste Systems
Web    : http://www.evaristesys.com/
Tel    : (+1) (678) 954-0670
Direct : (+1) (678) 954-0671
Mobile : (+1) (706) 338-8599



More information about the asterisk-users mailing list