国标28181:libosip2使用示例

1060 篇文章 295 订阅

解析

示例:url解析

#include <osipparser2/osip_port.h>
#include <cstdlib>
#include <cstring>
#include <osipparser2/osip_uri.h>

int osip_uri_test_accessor_api(osip_uri_t *url);
void osip_uri_parse(char *a_url);






int main(int argc, char **argv) {
//    osip_uri_parse("sip:antisip@sip.antisip.com;?subject=spamming");
//    osip_uri_parse("sip:antisip@sip.antisip.com;=?subject=spamming");
//    osip_uri_parse("sip:antisip@sip.antisip.com;a=?subject=spamming");
//    osip_uri_parse("sip:antisip@sip.antisip.com;a=b?subject=spamming");
//    osip_uri_parse("<sip:kubi6@>");
//    osip_uri_parse("sip:kubi6@");
//    osip_uri_parse("sip:alice@example.org;lr");
//    osip_uri_parse("sip:example.org;lr");
//    osip_uri_parse("zer:alice@example.org");
//    osip_uri_parse("mailto:jj@gnu.org");
//    osip_uri_parse("http://www.example.org/file.html");
//    osip_uri_parse("ftp://www.example.org/index.html");
//    osip_uri_parse("au://au.provider.org:6878/");
    osip_uri_parse("sip:j.d;oe:secret?@big.com;to[to?r+s=foo:&.bar;qs=dr$/def");
    return 0;
}

void osip_uri_parse(char *a_url){
    if(a_url == NULL || *a_url == 0){
        return;
    }

    int errcode;
    char *dest;
    osip_uri_t *url;
    osip_uri_init(&url);

    printf("=================================================\n");
    printf("URL TO PARSE: |%s|\n", a_url);

    errcode = osip_uri_parse(url, a_url);
    if (errcode != -1) {
        if (osip_uri_to_str(url, &dest) != -1) {
            printf("result:       |%s|\n", dest);
            osip_uri_test_accessor_api(url);
            osip_free(dest);
        }
    }


    osip_uri_free(url);
    printf("=================================================\n");
}

int osip_uri_test_accessor_api(osip_uri_t *url) {
    if (url->scheme != NULL)
        fprintf(stdout, "url->scheme:       %s\n", url->scheme);

    if (url->string != NULL) {
        fprintf(stdout, "url->string:      %s\n", url->string);
        return 0;
    }

    if (url->username != NULL)
        fprintf(stdout, "url->username:     %s\n", url->username);

    if ((url->password != NULL) )
        fprintf(stdout, "url->passwor:     %s\n", url->password);



    if (url->host != NULL) {
        fprintf(stdout, "url->host:         %s\n", url->host);
    }

    if (url->port != NULL)
        fprintf(stdout, "url->port:       %s\n", url->port);

    fprintf(stdout, "\nuri-params\n");

    {
        int pos = 0;
        osip_uri_param_t *u_param;

        while (!osip_list_eol(&url->url_params, pos)) {
            u_param = (osip_uri_param_t *) osip_list_get(&url->url_params, pos);

            if (u_param->gvalue != NULL)
                fprintf(stdout, "\t%s=%s\n", u_param->gname, u_param->gvalue);
            else
                fprintf(stdout, "\t%s\n", u_param->gname);

            pos++;
        }
    }

    fprintf(stdout, "\nheaders\n");

    {
        int pos = 0;
        osip_uri_header_t *u_header;

        while (!osip_list_eol(&url->url_headers, pos)) {
            u_header = (osip_uri_header_t *) osip_list_get(&url->url_headers, pos);

            if (pos == 0)
                fprintf(stdout, "\t%s=%s\n", u_header->gname, u_header->gvalue);

            else
                fprintf(stdout, "\t%s=%s\n", u_header->gname, u_header->gvalue);

            pos++;
        }
    }
    fprintf(stdout, "\n");
    return 0;
}

示例:via解析

#include <cstdlib>
#include <cstring>
#include <osipparser2/osip_port.h>
#include <osipparser2/osip_message.h>

void osip_via_parse(char *a_via){
    if(a_via == NULL || *a_via == 0){
        return;
    }


    int errcode;
    char *dest;
    osip_via_t *via;

    printf("=================================================\n");
    printf("VIA TO PARSE: |%s|\n", a_via);



    osip_via_init(&via);
    errcode = osip_via_parse(via, a_via);
    if (errcode != -1) {
        if (osip_via_to_str(via, &dest) != -1) {
            printf("result:       |%s|\n", dest);
            osip_free(dest);
        }
    } else
        printf("Bad via format: %s\n", a_via);

    osip_via_free(via);
    printf("=================================================\n");
}


int main(int argc, char **argv) {
//    osip_via_parse("SIP/2.0/UDP my.ex.fr :4054 ;ttl=16; maddr=224.2.0.1; branch=a7c6a8dlze.1 (Acme server)");
//    osip_via_parse("SIP/2.0/UDP adk8");
//    osip_via_parse("SIP/2.0/UDP callerspc.univ.edu");
//    osip_via_parse("SIP/2.0/UDP 10.0.1.1:5061");
//    osip_via_parse("SIP/2.0/UDP 169.130.12.5");
//    osip_via_parse("SIP/2.0/UDP erlang.bell-telephone.com:5060");
//    osip_via_parse("SIP/2.0/UDP 128.59.16.1:5060 ;received=128.59.19.3");
//    osip_via_parse("SIP/2.0/UDP sip.example.com;branch=7c337f30d7ce.1");
//
//    osip_via_parse(";maddr=239.128.16.254;ttl=16");
//    osip_via_parse("SIP/2.0/UDP mouse.wonderland.com");
//    osip_via_parse(" SIP/2.0/UDP kton.bell-tel.com");
//    osip_via_parse("SIP/2.0/UDP sip.ieee.org ;branch=3d8a50dbf5a28d.1");
//    osip_via_parse("SIP/2.0/UDP c.bell-tel.com");
    osip_via_parse("SIP/2.0/UDP my.ex.fr:4000;ttl=15;joe=tokenabc123-.!%* ; bob=");
    osip_via_parse(" SIP/2.0/UDP my.ex.fr");
    osip_via_parse(" SIP/2.0/UDP my.ex.fr:4054");
    osip_via_parse("   SIP/2.0/UDP  my.ex.fr:4054");
    osip_via_parse("    SIP/ 2.0/UDP      my.ex.fr:4054");
    osip_via_parse("  SIP  /    2.0    /    UDP my.ex.fr");
    return 0;
}

示例:wwwa解析

#include <cstdlib>
#include <cstring>
#include <osipparser2/osip_port.h>
#include <osipparser2/osip_message.h>

void osip_wwwa_parse(const char *a_wwwauthenticate){
    if(a_wwwauthenticate == NULL || *a_wwwauthenticate == 0){
        return;
    }

    printf("=================================================\n");
    printf("WWWAUTHENTICATE TO PARSE: |%s|\n", a_wwwauthenticate);

    char *dest;
    int errcode;
    osip_www_authenticate_t *wwwauthenticate;

    osip_www_authenticate_init(&wwwauthenticate);
    errcode = osip_www_authenticate_parse(wwwauthenticate, a_wwwauthenticate);

    if (errcode != -1) {
        if (osip_www_authenticate_to_str(wwwauthenticate, &dest) != -1) {
            printf("result:                   |%s|\n", dest);
            osip_free(dest);
        }

    } else
        printf("Bad wwwauthenticate format: %s\n", a_wwwauthenticate);

    osip_www_authenticate_free(wwwauthenticate);
    printf("=================================================\n");
}

#include <string>
int main(int argc, char **argv) {
    osip_wwwa_parse(R"(Basic realm="jobs", nonce="??????????")");
    osip_wwwa_parse(R"(Digest    realm="testrealm@host.com", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41")");
    osip_wwwa_parse(R"(Digest realm="studentlev1", nonce="??????????", stale=true, algorithm=MD5)");
    osip_wwwa_parse(R"(Digest realm="studentlev1", nonce="??????????", stale=true)");
    osip_wwwa_parse(R"(Digest realm="%$s..c,\"", nonce="??????????")");
    osip_wwwa_parse(R"(Digest realm="%$s..c,\\\"", nonce="??????????")");
    osip_wwwa_parse(R"(Basic realm="jobs" , nonce="??????????")");
    osip_wwwa_parse(R"(Digest  realm= "studentlev1"  , nonce="??????????", stale=true , algorithm= MD5)");

    osip_wwwa_parse(R"(Digest realm="%$s..c,\"",   nonce ="??????????")");
    osip_wwwa_parse(R"(Digest realm="%$s..c,\\\"", nonce=  "??????????")");



    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值