[OpenSIPS-Devel] Adding Display Name to From Field

John Llombart johnllombart at gmail.com
Mon Jul 19 17:18:58 CEST 2010


Hello,

I'm an undergrad working on a research project.

This project involved the modification of several header fields depending on
various factors. At this point, I've been able to remove the supplied
display name and replace it with "unverified". But, I'm stuck on how to add
a display name to the from header when the UAC does not supply one.

My code closely follows the work done in the uac module already in opensips.
It works perfectly when a display name is present but when it is not present
it adds garbage to the display field.

For instance, wireshark shows my from field to be

From: "unverified" <..
.....1..........*d..........*d........1.0.....1.......
......f........f....................1...........25f
@........................Q..............|..............1..1....1...sip:john at mydomain
>;tag=f04dda0088604f4892aed058306835e5

I'm fairly certain that I may not be setting my anchor point correctly since
I'm not quite sure how the offset is calculated.

1. When a field is to be modified *[for instance the display field in the
from header in the get_display_anchor function below]*, Why is the anchor
set at an (address - msg->buf)? In the case of get_display_anchor, it uses
the pointer to the first char (<) in the uri minus the msg->buf. Also,
please note that QjSimple does not send the usually
*<sip:john at mydomain>*but sends
*john at mydomain*.

When I looked in msg_parser.c, it says that the msg->buf variable is a
scratch variable. So the only thing I can assume is that the pointer to
first char is not pointing at the real header field in memory but is
pointing a scratch address within the msg->buf so if you substract the size
of msg->buf it will point to the real variable address.

2. When using the lump_struct to add, remove or insert headers, when is that
operation executed?

Thank you for all your help.

John


Please see the function get_display_anchor from uac_replace.c below:

static struct lump* get_display_anchor(struct sip_msg *msg,
                        struct hdr_field *hdr, struct to_body *body, str
*dsp)
{
    LM_INFO("Entering.....");

    LM_INFO("Header Field=%.*s\n", hdr->body.len, hdr->body.s);
    LM_INFO("Body=%.*s\n", body->display.len, body->display.s);
    LM_INFO("DSP=%.*s\n", dsp->len, dsp->s);

    struct lump* l;
    char *p1;
    char *p2;

    /* is URI quoted or not? */
    p1 = hdr->name.s + hdr->name.len;

    for( p2=body->uri.s-1 ; p2>=p1 && *p2!='<' ; p2--);


    if (*p2=='<') {
        LM_INFO("Is quoted ");
        l = anchor_lump( msg,* p2 - msg->buf*, 0, 0);  // why p2 - msg->buff
??
        if (l==0) {
            LM_ERR("unable to build lump anchor\n");
            return 0;
        }
        dsp->s[dsp->len++] = ' ';
        return l;
    }

    /* not quoted - more complicated....must place the closing bracket */
    l = anchor_lump( msg, (body->uri.s+body->uri.len) - msg->buf, 0, 0);
    if (l==0) {
        LM_ERR("unable to build lump anchor\n");
        return 0;
    }
    p1 = (char*)pkg_malloc(1);
    if (p1==0) {
        LM_ERR("no more pkg mem \n");
        return 0;
    }
    *p1 = '>';



    if (insert_new_lump_after( l, p1, 1, 0)==0) {
        LM_ERR("insert lump failed\n");
        pkg_free(p1);
        return 0;
    }
    /* build anchor for display */
    l = anchor_lump( msg, body->uri.s - msg->buf, 0, 0);
    if (l==0) {
        LM_ERR("unable to build lump anchor\n");
        return 0;
    }
    dsp->s[dsp->len++] = ' ';
    dsp->s[dsp->len++] = '<';

    LM_INFO("Header Field=%.*s\n", hdr->body.len, hdr->body.s);
    LM_INFO("Body=%.*s\n", body->display.len, body->display.s);
    LM_INFO("DSP=%.*s\n", dsp->len, dsp->s);

    return l;
}


Please see my code below:

static int new_func(struct sip_msg* _msg, char* notUsed1, char* notUsed2)
{
    LM_INFO("Entering...");

    struct to_body *body;     // [defined: parse_to.h]
    struct lump *l;        // [defined: lump_struct.h]
    struct hdr_field *hdr;    // [defined: hf.h]
    str d;
    str *display = &d;    // new display name 'unverified'
    str buf;

    str_copy(display,"\"unverified\"", strlen("\"unverified\""));

    if(parse_from_header(_msg) && parse_headers(_msg, HDR_EOH_T, 0)) // Make
sure the from field has been parsed
    {
        LM_ERR("Error Parsing Headers");
        return -1;
    }

    hdr = _msg->from;    // Assign the address of the parsed header field to
hdr

    body = (struct to_body*)hdr->parsed;     // Get the parsed data from the
header field
                                                                 // and cast
it into a struct to_body

   l=0;

    if(body->display.len)    // Check if there is a display name
    {

        LM_INFO("Removing display [%.*s]\n", body->display.len,
body->display.s);

        l = del_lump(_msg, body->display.s-_msg->buf, body->display.len,0);
// [defined: data_lump.c]

        buf.s = pkg_malloc(display->len + 2);

        memcpy(buf.s, display->s, display->len);

        buf.len = display->len;

        LM_INFO("-------- Anchor Start -------");

        l = get_display_anchor(_msg, hdr, body, &buf);

        LM_INFO("-------- Insert Start ----------");

        l = insert_new_lump_after(l, buf.s, buf.len, 0);

        if(l == 0)
        {
            LM_INFO("insert new display lump failed\n");
            pkg_free(buf.s);
            return -1;
        }


    }
    else  // If there isn't a display name
    {

        buf.s = pkg_malloc(display->len + 2);

        memcpy(buf.s, display->s, display->len);

        buf.len = display->len;

        l = get_display_anchor(_msg, hdr, body, &buf);

        l = insert_new_lump_after(l, buf.s, l->u.value, 0);

    }


    LM_INFO("------- END -------");

    return 1;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.opensips.org/pipermail/devel/attachments/20100719/969143fc/attachment-0001.htm 


More information about the Devel mailing list