inet_msg_pack.c

来自「最新MTK手机软件源码」· C语言 代码 · 共 1,737 行 · 第 1/5 页

C
1,737
字号
                    kal_uint8 app_type,
                    void *arg,
                    kal_char *buf,
                    kal_int32 buf_len,
                    kal_int32 *r_size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /*
     * inet_auth_type_enum  auth_type;
     * inet_int_str_struct  algorithm;
     * kal_uint8   stale;
     * kal_char *realm;
     * inet_str_list_struct *domains;
     * kal_char *nonce;
     * kal_char *opaque;
     * inet_int_str_list_struct   *qop;
     * inet_param_list_struct  *param_list;
     */
    inet_authenticate_struct *authenticate = (inet_authenticate_struct*) arg;
    kal_int32 l = 0, m = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    *r_size = 0;

    /* WWW-Authenticate: Digest realm="mtk.com.tw",nonce="2fa131be570758ad5d2a43f6h3cfa487",algorithm=MD5,opaque="30c83dd8f3ad8e36cf0ff4a1f320d187",qop="auth",stale=false */

    CHECK_INET_PARAM(arg, INET_RESULT_ERROR) if (authenticate->auth_type == INET_AUTH_DIGEST)
    {
        if (authenticate->opaque && authenticate->qop)
        {
            m = _snprintf(
                    buf,
                    buf_len,
                    "Digest realm=\"%s\",nonce=\"%s\",algorithm=MD5,opaque=\"%s\",qop=\"auth\",stale=%s",
                    authenticate->realm,
                    authenticate->nonce,
                    authenticate->opaque,
                    (authenticate->stale ? "true" : "false"));
        }
        else if (authenticate->opaque && authenticate->qop == NULL)
        {
            m = _snprintf(
                    buf,
                    buf_len,
                    "Digest realm=\"%s\",nonce=\"%s\",algorithm=MD5,opaque=\"%s\",stale=%s",
                    authenticate->realm,
                    authenticate->nonce,
                    authenticate->opaque,
                    (authenticate->stale ? "true" : "false"));
        }
        else if (authenticate->opaque == NULL && authenticate->qop)
        {
            /* received a authenicate that does not have "opaque" */
            m = _snprintf(
                    buf,
                    buf_len,
                    "Digest realm=\"%s\",nonce=\"%s\",algorithm=MD5,qop=\"auth\",stale=%s",
                    authenticate->realm,
                    authenticate->nonce,
                    (authenticate->stale ? "true" : "false"));
        }
        else if (authenticate->opaque == NULL && authenticate->qop == NULL)
        {
            /* received a authenicate that does not have "opaque" */
            m = _snprintf(
                    buf,
                    buf_len,
                    "Digest realm=\"%s\",nonce=\"%s\",algorithm=MD5,stale=%s",
                    authenticate->realm,
                    authenticate->nonce,
                    (authenticate->stale ? "true" : "false"));
        }
        CHECK_INET_PACK_ENBUF(m) l += m;
    }
    else
    {
        return INET_RESULT_ERROR;
    }

    *r_size = l;
    return INET_RESULT_OK;
}   /* end of inet_msg_pack_authenticate */


/*****************************************************************************
 * FUNCTION
 *  inet_msg_pack_authentication_info
 * DESCRIPTION
 *  This function packs the authentication-info  header value.
 * PARAMETERS
 *  app_type        [IN]            application type
 *  arg             [IN]            pointer of header value
 *  buf             [IN]            buffer to store packed header value
 *  buf_len         [IN]            available data size of buffer
 *  r_size          [IN/OUT]        real data size to pack
 * RETURNS
 *  INET result code
 *****************************************************************************/
inet_result_enum inet_msg_pack_authentication_info(
                    kal_uint8 app_type,
                    void *arg,
                    kal_char *buf,
                    kal_int32 buf_len,
                    kal_int32 *r_size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    inet_authentication_info_struct *authentication_info = (inet_authentication_info_struct*) arg;
    kal_int32 size = 0, m;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    CHECK_INET_PARAM(arg, INET_RESULT_ERROR) if (authentication_info->next_nonce)
    {
        m = _snprintf(buf, buf_len, "nextnonce=%s", authentication_info->next_nonce);
        CHECK_INET_PACK_ENBUF(m) size += m;
    }
    if (authentication_info->rspauth)
    {
        m = _snprintf(buf + size, buf_len - size, ",rspauth=%s", authentication_info->rspauth);
        CHECK_INET_PACK_ENBUF(m) size += m;
    }
    if (authentication_info->cnonce)
    {
        m = _snprintf(buf + size, buf_len - size, ",cononce=%s", authentication_info->cnonce);
        CHECK_INET_PACK_ENBUF(m) size += m;
    }
    if (authentication_info->qop)
    {
        inet_int_str_list_struct *qop_list = authentication_info->qop;

        while (qop_list)
        {
            if (qop_list->object)
            {
                if (qop_list->object->int_value == INET_AUTH_QOP_AUTH)
                {
                    m = _snprintf(buf + size, buf_len - size, "%s", "auth");
                    CHECK_INET_PACK_ENBUF(m) size += m;
                }
                else if (qop_list->object->int_value == INET_AUTH_QOP_AUTH_INT)
                {
                    m = _snprintf(buf + size, buf_len - size, "%s", "auth-int");
                    CHECK_INET_PACK_ENBUF(m) size += m;
                }
                else if (qop_list->object->int_value == INET_AUTH_QOP_UNSUPPORT && qop_list->object->str_value != NULL)
                {
                    m = _snprintf(buf + size, buf_len - size, "%s", qop_list->object->str_value);
                    CHECK_INET_PACK_ENBUF(m) size += m;
                }
                if (qop_list->next && qop_list->next->object)
                {
                    m = _snprintf(buf + size, buf_len - size, "%s", ", ");
                    CHECK_INET_PACK_ENBUF(m) size += m;
                }
            }
            qop_list = qop_list->next;
        }

    }
    if (authentication_info->nc != 0)
    {
        kal_uint8 i;
        kal_char nc_buf[9];

        kal_mem_set(nc_buf, 0x0, 9);
        sprintf(nc_buf, "%8x", authentication_info->nc);

        for (i = 0; i < 9; i++)
        {
            if (nc_buf[i] == ' ')
            {
                nc_buf[i] = 0x30;
            }
        }
        m = _snprintf(buf + size, buf_len - size, ",nc=%s", nc_buf);
        CHECK_INET_PACK_ENBUF(m) size += m;
    }
    *r_size = size;
    return INET_RESULT_OK;
}


/*****************************************************************************
 * FUNCTION
 *  inet_msg_pack_content_type
 * DESCRIPTION
 *  This function packs the  inet_content_type_struct header value.
 * PARAMETERS
 *  app_type        [IN]            application type
 *  arg             [IN]            pointer of header value
 *  buf             [IN]            buffer to store packed header value
 *  buf_len         [IN]            available data size of buffer
 *  r_size          [IN/OUT]        real data size to pack
 * RETURNS
 *  INET result code
 *****************************************************************************/
//Ex1:<sip:ad-hoc@mtk.com.tw>
//Ex2: Kevin    <sip:ad-hoc@mtk.com.tw>
//Ex3: sip:ad-hoc@mtk.com.tw
inet_result_enum inet_msg_pack_content_type(
                    kal_uint8 app_type,
                    void *arg,
                    kal_char *buf,
                    kal_int32 buf_len,
                    kal_int32 *r_size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 l;
    kal_int32 size = 0;
    inet_result_enum ret;
    inet_content_type_struct *ct = (inet_content_type_struct*) arg;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    CHECK_INET_PARAM(arg, INET_RESULT_ERROR) if (ct->mime_type)
    {
        inet_msg_lcase(ct->mime_type);
        l = _snprintf(buf, buf_len, "%s", ct->mime_type);
        CHECK_INET_PACK_ENBUF(l) size += l;
    }
    if (ct->mime_subtype)
    {
        inet_msg_lcase(ct->mime_subtype);
        l = _snprintf(buf + size, buf_len - size, "/%s", ct->mime_subtype);
        CHECK_INET_PACK_ENBUF(l) size += l;
    }
    if (ct->param_list)
    {

        if ((ret = inet_msg_pack_param_list(
                    app_type,
                    (void*)ct->param_list,
                    buf + size,
                    buf_len - size,
                    &l)) != INET_RESULT_OK)
        {
            return ret;
        }
        size += l;
    }

    *r_size = size;
    return INET_RESULT_OK;
}   /* end of sip_msg_pack_content_type */


/*****************************************************************************
 * FUNCTION
 *  inet_msg_pack_content_type_list
 * DESCRIPTION
 *  This function packs the  inet_content_type_list_struct header value.
 * PARAMETERS
 *  app_type        [IN]            application type
 *  arg             [IN]            pointer of header value
 *  buf             [IN]            buffer to store packed header value
 *  buf_len         [IN]            available data size of buffer
 *  r_size          [IN/OUT]        real data size to pack
 * RETURNS
 *  INET result code
 *****************************************************************************/
inet_result_enum inet_msg_pack_content_type_list(
                    kal_uint8 app_type,
                    void *arg,
                    kal_char *buf,
                    kal_int32 buf_len,
                    kal_int32 *r_size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 m;
    kal_int32 size = 0;
    inet_content_type_list_struct *ct_list = (inet_content_type_list_struct*) arg;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    CHECK_INET_PARAM(arg, INET_RESULT_ERROR) while (ct_list)
    {
        if (ct_list->object)
        {
            if (inet_msg_pack_content_type(app_type, ct_list->object, buf + size, buf_len - size, &m) != INET_RESULT_OK)
            {
                return INET_RESULT_NO_ENBUF;
            }
            size += m;
            if (ct_list->next && ct_list->next->object) /* kevin */
            {
                m = _snprintf(buf + size, buf_len - size, ", ");
                CHECK_INET_PACK_ENBUF(m) size += m;
            }
        }

        ct_list = ct_list->next;
    }
    *r_size = size;
    return INET_RESULT_OK;
}   /* end of inet_msg_pack_content_type_list */


/*****************************************************************************
 * FUNCTION
 *  inet_msg_pack_cseq
 * DESCRIPTION
 *  This function packs the  inet_cseq_struct header value.
 * PARAMETERS
 *  app_type        [IN]            application type
 *  arg             [IN]            pointer of header value
 *  buf             [IN]            buffer to store packed header value
 *  buf_len         [IN]            available data size of buffer
 *  r_size          [IN/OUT]        real data size to pack
 * RETURNS
 *  INET result code
 *****************************************************************************/
/* "3574 REGISTER" */
inet_result_enum inet_msg_pack_cseq(
                    kal_uint8 app_type,
                    void *arg,
                    kal_char *buf,
                    kal_int32 buf_len,
                    kal_int32 *r_size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 l;
    inet_cseq_struct *cseq = (inet_cseq_struct*) arg;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    CHECK_INET_PARAM(arg, INET_RESULT_ERROR) if (cseq->method > INET_METHOD_NUM)
        return INET_RESULT_ERROR;
    l = _snprintf(buf, buf_len, "%d %s", cseq->seq, inet_method_name_table[cseq->method]);

    CHECK_INET_PACK_ENBUF(l) * r_size = l;
    return INET_RESULT_OK;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?