/*
 * Copyright (C) 2013 Teluu Inc. (http://www.teluu.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <pjsua2/types.hpp>
#include <pjsua2/siptypes.hpp>
#include <pjsip/print_util.h>       /* For pjsip_hdr_names */
#include "util.hpp"

using namespace pj;
using namespace std;

#define THIS_FILE       "siptypes.cpp"

///////////////////////////////////////////////////////////////////////////////
namespace pj
{
void readIntVector( ContainerNode &node,
                    const string &array_name,
                    IntVector &v) PJSUA2_THROW(Error)
{
    ContainerNode array_node = node.readArray(array_name);
    v.resize(0);
    while (array_node.hasUnread()) {
        v.push_back((int)array_node.readNumber());
    }
}

void writeIntVector(ContainerNode &node,
                    const string &array_name,
                    const IntVector &v) PJSUA2_THROW(Error)
{
    ContainerNode array_node = node.writeNewArray(array_name);
    for (unsigned i=0; i<v.size(); ++i) {
        array_node.writeNumber("", (float)v[i]);
    }
}

void readQosParams( ContainerNode &node,
                    pj_qos_params &qos) PJSUA2_THROW(Error)
{
    ContainerNode this_node = node.readContainer("qosParams");

    NODE_READ_NUM_T( this_node, pj_uint8_t, qos.flags);
    NODE_READ_NUM_T( this_node, pj_uint8_t, qos.dscp_val);
    NODE_READ_NUM_T( this_node, pj_uint8_t, qos.so_prio);
    NODE_READ_NUM_T( this_node, pj_qos_wmm_prio, qos.wmm_prio);
}

void writeQosParams( ContainerNode &node,
                     const pj_qos_params &qos) PJSUA2_THROW(Error)
{
    ContainerNode this_node = node.writeNewContainer("qosParams");

    NODE_WRITE_NUM_T( this_node, pj_uint8_t, qos.flags);
    NODE_WRITE_NUM_T( this_node, pj_uint8_t, qos.dscp_val);
    NODE_WRITE_NUM_T( this_node, pj_uint8_t, qos.so_prio);
    NODE_WRITE_NUM_T( this_node, pj_qos_wmm_prio, qos.wmm_prio);
}

void readSipHeaders( const ContainerNode &node,
                     const string &array_name,
                     SipHeaderVector &headers) PJSUA2_THROW(Error)
{
    ContainerNode headers_node = node.readArray(array_name);
    headers.resize(0);
    while (headers_node.hasUnread()) {
        SipHeader hdr;

        ContainerNode header_node = headers_node.readContainer("header");
        hdr.hName = header_node.readString("hname");
        hdr.hValue = header_node.readString("hvalue");
        headers.push_back(PJSUA2_MOVE(hdr));
    }
}

void writeSipHeaders(ContainerNode &node,
                     const string &array_name,
                     const SipHeaderVector &headers) PJSUA2_THROW(Error)
{
    ContainerNode headers_node = node.writeNewArray(array_name);
    for (unsigned i=0; i<headers.size(); ++i) {
        ContainerNode header_node = headers_node.writeNewContainer("header");
        header_node.writeString("hname", headers[i].hName);
        header_node.writeString("hvalue", headers[i].hValue);
    }
}

} // namespace
///////////////////////////////////////////////////////////////////////////////

AuthCredInfo::AuthCredInfo()
: scheme("digest"), realm("*"), dataType(0),
  algoType(PJSIP_AUTH_ALGORITHM_NOT_SET)
{
}

AuthCredInfo::AuthCredInfo(const string &param_scheme,
                           const string &param_realm,
                           const string &param_user_name,
                           const int param_data_type,
                           const string param_data)
: scheme(param_scheme), realm(param_realm), username(param_user_name),
  dataType(param_data_type), data(PJSUA2_MOVE(param_data)),
  algoType(PJSIP_AUTH_ALGORITHM_NOT_SET)
{
}

void AuthCredInfo::readObject(const ContainerNode &node) PJSUA2_THROW(Error)
{
    ContainerNode this_node = node.readContainer("AuthCredInfo");

    NODE_READ_STRING( this_node, scheme);
    NODE_READ_STRING( this_node, realm);
    NODE_READ_STRING( this_node, username);
    NODE_READ_INT   ( this_node, dataType);
    NODE_READ_STRING( this_node, data);
    NODE_READ_STRING( this_node, akaK);
    NODE_READ_STRING( this_node, akaOp);
    NODE_READ_STRING( this_node, akaAmf);
}

void AuthCredInfo::writeObject(ContainerNode &node) const PJSUA2_THROW(Error)
{
    ContainerNode this_node = node.writeNewContainer("AuthCredInfo");

    NODE_WRITE_STRING( this_node, scheme);
    NODE_WRITE_STRING( this_node, realm);
    NODE_WRITE_STRING( this_node, username);
    NODE_WRITE_INT   ( this_node, dataType);
    NODE_WRITE_STRING( this_node, data);
    NODE_WRITE_STRING( this_node, akaK);
    NODE_WRITE_STRING( this_node, akaOp);
    NODE_WRITE_STRING( this_node, akaAmf);
}

void AuthCredInfo::fromPj(const pjsip_cred_info &prm)
{
    realm       = pj2Str(prm.realm);
    scheme      = pj2Str(prm.scheme);
    username    = pj2Str(prm.username);
    dataType    = prm.data_type;
    data        = pj2Str(prm.data);
    algoType    = prm.algorithm_type;
    akaK        = pj2Str(prm.ext.aka.k);
    akaOp       = pj2Str(prm.ext.aka.op);
    akaAmf      = pj2Str(prm.ext.aka.amf);
}

pjsip_cred_info AuthCredInfo::toPj() const
{
    pjsip_cred_info ret;
    ret.realm           = str2Pj(realm);
    ret.scheme          = str2Pj(scheme);
    ret.username        = str2Pj(username);
    ret.data_type       = dataType;
    ret.data            = str2Pj(data);
    ret.algorithm_type  = algoType;
    ret.ext.aka.k       = str2Pj(akaK);
    ret.ext.aka.op      = str2Pj(akaOp);
    ret.ext.aka.amf     = str2Pj(akaAmf);
    return ret;
}

///////////////////////////////////////////////////////////////////////////////

TlsConfig::TlsConfig() : credDirectType(0),
                         privKeyDirect(NULL), certDirect(NULL),
                         method(PJSIP_SSL_UNSPECIFIED_METHOD),
                         qosType(PJ_QOS_TYPE_BEST_EFFORT)
{
    pjsip_tls_setting ts;
    pjsip_tls_setting_default(&ts);
    this->fromPj(ts);
}

pjsip_tls_setting TlsConfig::toPj() const
{
    pjsip_tls_setting ts;
    pjsip_tls_setting_default(&ts);

    ts.ca_list_file     = str2Pj(this->CaListFile);
    ts.cert_file        = str2Pj(this->certFile);
    ts.privkey_file     = str2Pj(this->privKeyFile);
    ts.password         = str2Pj(this->password);
    ts.ca_buf           = str2Pj(this->CaBuf);
    ts.cert_buf         = str2Pj(this->certBuf);
    ts.privkey_buf      = str2Pj(this->privKeyBuf);
    ts.cert_lookup.type = this->certLookupType;
    ts.cert_lookup.keyword = str2Pj(this->certLookupKeyword);
    
    if (this->certDirect &&
        (this->credDirectType & PJ_SSL_CERT_DIRECT_OPENSSL_X509_CERT))
    {
        ts.cert_direct.type |= PJ_SSL_CERT_DIRECT_OPENSSL_X509_CERT;
        ts.cert_direct.cert = this->certDirect;
    }
    if (this->privKeyDirect &&
        (this->credDirectType & PJ_SSL_CERT_DIRECT_OPENSSL_EVP_PKEY))
    {
        ts.cert_direct.type |= PJ_SSL_CERT_DIRECT_OPENSSL_EVP_PKEY;
        ts.cert_direct.privkey = this->privKeyDirect;
    }

    ts.method           = this->method;
    ts.ciphers_num      = (unsigned)this->ciphers.size();
    ts.proto            = this->proto;
    // The following will only work if sizeof(enum)==sizeof(int)
    pj_assert(sizeof(ts.ciphers[0]) == sizeof(int));
    ts.ciphers          = ts.ciphers_num? 
                            (pj_ssl_cipher*)&this->ciphers[0] : NULL;
    ts.verify_server    = this->verifyServer;
    ts.verify_client    = this->verifyClient;
    ts.require_client_cert = this->requireClientCert;
    ts.timeout.sec      = this->msecTimeout / 1000;
    ts.timeout.msec     = this->msecTimeout % 1000;
    ts.qos_type         = this->qosType;
    ts.qos_params       = this->qosParams;
    ts.qos_ignore_error = this->qosIgnoreError;
    ts.sockopt_params   = this->sockOptParams.toPj();
    ts.sockopt_ignore_error = this->sockOptIgnoreError;
    ts.enable_renegotiation = this->enableRenegotiation;

    return ts;
}

void TlsConfig::fromPj(const pjsip_tls_setting &prm)
{
    this->CaListFile    = pj2Str(prm.ca_list_file);
    this->certFile      = pj2Str(prm.cert_file);
    this->privKeyFile   = pj2Str(prm.privkey_file);
    this->password      = pj2Str(prm.password);
    this->CaBuf         = pj2Str(prm.ca_buf);
    this->certBuf       = pj2Str(prm.cert_buf);
    this->privKeyBuf    = pj2Str(prm.privkey_buf);
    this->certLookupType= prm.cert_lookup.type;
    this->certLookupKeyword = pj2Str(prm.cert_lookup.keyword);
    this->credDirectType= 0;

    if (prm.cert_direct.cert &&
        (prm.cert_direct.type & PJ_SSL_CERT_DIRECT_OPENSSL_X509_CERT))
    {
        this->credDirectType |= PJ_SSL_CERT_DIRECT_OPENSSL_X509_CERT; 
        this->certDirect = prm.cert_direct.cert;
    } else {
        this->certDirect = NULL;
    }

    if (prm.cert_direct.privkey &&
        (prm.cert_direct.type & PJ_SSL_CERT_DIRECT_OPENSSL_EVP_PKEY))
    {
        this->credDirectType |= PJ_SSL_CERT_DIRECT_OPENSSL_EVP_PKEY; 
        this->privKeyDirect = prm.cert_direct.privkey;
    } else {
        this->privKeyDirect = NULL;
    }

    this->method        = (pjsip_ssl_method)prm.method;
    this->proto         = prm.proto;
    // The following will only work if sizeof(enum)==sizeof(int)
    pj_assert(sizeof(prm.ciphers[0]) == sizeof(int));
    this->ciphers       = IntVector(prm.ciphers, prm.ciphers+prm.ciphers_num);
    this->verifyServer  = PJ2BOOL(prm.verify_server);
    this->verifyClient  = PJ2BOOL(prm.verify_client);
    this->requireClientCert = PJ2BOOL(prm.require_client_cert);
    this->msecTimeout   = PJ_TIME_VAL_MSEC(prm.timeout);
    this->qosType       = prm.qos_type;
    this->qosParams     = prm.qos_params;
    this->qosIgnoreError = PJ2BOOL(prm.qos_ignore_error);
    this->sockOptParams.fromPj(prm.sockopt_params);
    this->sockOptIgnoreError = PJ2BOOL(prm.sockopt_ignore_error);
    this->enableRenegotiation = PJ2BOOL(prm.enable_renegotiation);
}

void TlsConfig::readObject(const ContainerNode &node) PJSUA2_THROW(Error)
{
    ContainerNode this_node = node.readContainer("TlsConfig");

    NODE_READ_STRING  ( this_node, CaListFile);
    NODE_READ_STRING  ( this_node, certFile);
    NODE_READ_STRING  ( this_node, privKeyFile);
    NODE_READ_STRING  ( this_node, password);
    NODE_READ_STRING  ( this_node, CaBuf);
    NODE_READ_STRING  ( this_node, certBuf);
    NODE_READ_STRING  ( this_node, privKeyBuf);
    NODE_READ_NUM_T   ( this_node, pjsip_ssl_method, method);
    readIntVector     ( this_node, "ciphers", ciphers);
    NODE_READ_BOOL    ( this_node, verifyServer);
    NODE_READ_BOOL    ( this_node, verifyClient);
    NODE_READ_BOOL    ( this_node, requireClientCert);
    NODE_READ_UNSIGNED( this_node, msecTimeout);
    NODE_READ_NUM_T   ( this_node, pj_qos_type, qosType);
    readQosParams     ( this_node, qosParams);
    NODE_READ_BOOL    ( this_node, qosIgnoreError);
    NODE_READ_OBJ     ( this_node, sockOptParams);
    NODE_READ_BOOL    ( this_node, sockOptIgnoreError);
    NODE_READ_NUM_T   ( this_node, pj_ssl_cert_lookup_type, certLookupType);
    NODE_READ_STRING  ( this_node, certLookupKeyword);
}

void TlsConfig::writeObject(ContainerNode &node) const PJSUA2_THROW(Error)
{
    ContainerNode this_node = node.writeNewContainer("TlsConfig");

    NODE_WRITE_STRING  ( this_node, CaListFile);
    NODE_WRITE_STRING  ( this_node, certFile);
    NODE_WRITE_STRING  ( this_node, privKeyFile);
    NODE_WRITE_STRING  ( this_node, password);
    NODE_WRITE_STRING  ( this_node, CaBuf);
    NODE_WRITE_STRING  ( this_node, certBuf);
    NODE_WRITE_STRING  ( this_node, privKeyBuf);
    NODE_WRITE_NUM_T   ( this_node, pjsip_ssl_method, method);
    writeIntVector     ( this_node, "ciphers", ciphers);
    NODE_WRITE_BOOL    ( this_node, verifyServer);
    NODE_WRITE_BOOL    ( this_node, verifyClient);
    NODE_WRITE_BOOL    ( this_node, requireClientCert);
    NODE_WRITE_UNSIGNED( this_node, msecTimeout);
    NODE_WRITE_NUM_T   ( this_node, pj_qos_type, qosType);
    writeQosParams     ( this_node, qosParams);
    NODE_WRITE_BOOL    ( this_node, qosIgnoreError);
    NODE_WRITE_OBJ     ( this_node, sockOptParams);
    NODE_WRITE_BOOL    ( this_node, sockOptIgnoreError);
    NODE_WRITE_NUM_T   ( this_node, pj_ssl_cert_lookup_type, certLookupType);
    NODE_WRITE_STRING  ( this_node, certLookupKeyword);
}

///////////////////////////////////////////////////////////////////////////////

SockOpt::SockOpt()
{
    pj_bzero(this, sizeof(*this));
}

SockOpt::SockOpt(int level, int optName, int optVal)
{
    pj_bzero(this, sizeof(*this));
    this->level = level;
    this->optName = optName;
    setOptValInt(optVal);
}

void SockOpt::setOptValInt(int opt_val)
{
    optVal = &optValInt;
    optLen = sizeof(int);
    optValInt = opt_val;
}

SockOptParams::SockOptParams()
{
}

pj_sockopt_params SockOptParams::toPj() const
{
    pj_sockopt_params sop;
    unsigned i;

    pj_bzero(&sop, sizeof(sop));
    sop.cnt = (unsigned)this->sockOpts.size();
    if (sop.cnt > PJ_MAX_SOCKOPT_PARAMS)
        sop.cnt = PJ_MAX_SOCKOPT_PARAMS;
    for (i = 0; i < sop.cnt; ++i) {
        sop.options[i].level = this->sockOpts[i].level;
        sop.options[i].optname = this->sockOpts[i].optName;
        sop.options[i].optval = this->sockOpts[i].optVal;
        sop.options[i].optlen = this->sockOpts[i].optLen;
    }

    return sop;
}

void SockOptParams::fromPj(const pj_sockopt_params &prm)
{
    unsigned i;

    this->sockOpts.clear();
    for (i = 0; i < prm.cnt; ++i) {
        SockOpt so;
        so.level = prm.options[i].level;
        so.optName = prm.options[i].optname;
        if (prm.options[i].optlen == sizeof(int)) {
            so.setOptValInt(*((int *)prm.options[i].optval));
        }
        this->sockOpts.push_back(PJSUA2_MOVE(so));
    }
}

void SockOptParams::readObject(const ContainerNode &node) PJSUA2_THROW(Error)
{
    ContainerNode array_node = node.readArray("sockOptParams");
    sockOpts.resize(0);
    while (array_node.hasUnread()) {
        ContainerNode so_node = array_node.readContainer("sockOpt");
        SockOpt so;
        so.level = so_node.readInt("level");
        so.optName = so_node.readInt("optName");
        so.optLen = so_node.readInt("optLen");
        if (so.optLen == sizeof(int)) {
            int optVal = so_node.readInt("optVal");
            so.setOptValInt(optVal);
        }
        sockOpts.push_back(PJSUA2_MOVE(so));
    }
}

void SockOptParams::writeObject(ContainerNode &node) const PJSUA2_THROW(Error)
{
    ContainerNode array_node = node.writeNewArray("sockOptParams");
    for (unsigned i=0; i<sockOpts.size(); ++i) {
        ContainerNode so_node = array_node.writeNewContainer("sockOpt");
        string so_val((char*)sockOpts[i].optVal,
                      sockOpts[i].optLen > 0? sockOpts[i].optLen : 0);
        so_node.writeInt("level", sockOpts[i].level);
        so_node.writeInt("optName", sockOpts[i].optName);
        so_node.writeInt("optLen", sockOpts[i].optLen);
        if (sockOpts[i].optLen == sizeof(int)) {
            so_node.writeInt("optVal", sockOpts[i].optValInt);
        }
    }
}

///////////////////////////////////////////////////////////////////////////////

TransportConfig::TransportConfig() : qosType(PJ_QOS_TYPE_BEST_EFFORT)
{
    pjsua_transport_config tc;
    pjsua_transport_config_default(&tc);
    this->fromPj(tc);
}

void TransportConfig::fromPj(const pjsua_transport_config &prm)
{
    this->port          = prm.port;
    this->portRange     = prm.port_range;
    this->randomizePort = PJ2BOOL(prm.randomize_port);
    this->publicAddress = pj2Str(prm.public_addr);
    this->boundAddress  = pj2Str(prm.bound_addr);
    this->tlsConfig.fromPj(prm.tls_setting);
    this->qosType       = prm.qos_type;
    this->qosParams     = prm.qos_params;
    this->sockOptParams.fromPj(prm.sockopt_params);
}

pjsua_transport_config TransportConfig::toPj() const
{
    pjsua_transport_config tc;
    pjsua_transport_config_default(&tc);

    tc.port             = this->port;
    tc.port_range       = this->portRange;
    tc.randomize_port   = this->randomizePort;
    tc.public_addr      = str2Pj(this->publicAddress);
    tc.bound_addr       = str2Pj(this->boundAddress);
    tc.tls_setting      = this->tlsConfig.toPj();
    tc.qos_type         = this->qosType;
    tc.qos_params       = this->qosParams;
    tc.sockopt_params   = this->sockOptParams.toPj();

    return tc;
}

void TransportConfig::readObject(const ContainerNode &node) PJSUA2_THROW(Error)
{
    ContainerNode this_node = node.readContainer("TransportConfig");

    NODE_READ_UNSIGNED  ( this_node, port);
    NODE_READ_UNSIGNED  ( this_node, portRange);
    NODE_READ_STRING    ( this_node, publicAddress);
    NODE_READ_STRING    ( this_node, boundAddress);
    NODE_READ_NUM_T     ( this_node, pj_qos_type, qosType);
    readQosParams       ( this_node, qosParams);
    NODE_READ_OBJ       ( this_node, tlsConfig);
    NODE_READ_OBJ       ( this_node, sockOptParams);
}

void TransportConfig::writeObject(ContainerNode &node) const
                                  PJSUA2_THROW(Error)
{
    ContainerNode this_node = node.writeNewContainer("TransportConfig");

    NODE_WRITE_UNSIGNED  ( this_node, port);
    NODE_WRITE_UNSIGNED  ( this_node, portRange);
    NODE_WRITE_STRING    ( this_node, publicAddress);
    NODE_WRITE_STRING    ( this_node, boundAddress);
    NODE_WRITE_NUM_T     ( this_node, pj_qos_type, qosType);
    writeQosParams       ( this_node, qosParams);
    NODE_WRITE_OBJ       ( this_node, tlsConfig);
    NODE_WRITE_OBJ       ( this_node, sockOptParams);
}

///////////////////////////////////////////////////////////////////////////////

TransportInfo::TransportInfo()
: id(), type(PJSIP_TRANSPORT_UNSPECIFIED), flags(), usageCount()
{
}

void TransportInfo::fromPj(const pjsua_transport_info &tinfo)
{
    this->id = tinfo.id;
    this->type = tinfo.type;
    this->typeName = pj2Str(tinfo.type_name);
    this->info = pj2Str(tinfo.info);
    this->flags = tinfo.flag;

    char straddr[PJ_INET6_ADDRSTRLEN+10];
    pj_sockaddr_print(&tinfo.local_addr, straddr, sizeof(straddr), 3);
    this->localAddress = straddr;

    pj_ansi_snprintf(straddr, sizeof(straddr), "%.*s:%d",
                     (int)tinfo.local_name.host.slen,
                     tinfo.local_name.host.ptr,
                     tinfo.local_name.port);
    this->localName = straddr;
    this->usageCount = tinfo.usage_count;
}

///////////////////////////////////////////////////////////////////////////////

SipRxData::SipRxData()
: pjRxData(NULL)
{
}

void SipRxData::fromPj(pjsip_rx_data &rdata)
{
    char straddr[PJ_INET6_ADDRSTRLEN+10];

    info        = pjsip_rx_data_get_info(&rdata);
    wholeMsg    = string(rdata.msg_info.msg_buf, rdata.msg_info.len);
    pj_sockaddr_print(&rdata.pkt_info.src_addr, straddr, sizeof(straddr), 3);
    srcAddress  = straddr;
    pjRxData    = (void *)&rdata;
}

///////////////////////////////////////////////////////////////////////////////

void SipMediaType::fromPj(const pjsip_media_type &prm)
{
    type        = pj2Str(prm.type);
    subType     = pj2Str(prm.subtype);
}

pjsip_media_type SipMediaType::toPj() const
{
    pjsip_media_type pj_mt;
    pj_bzero(&pj_mt, sizeof(pj_mt));
    pj_mt.type      = str2Pj(type);
    pj_mt.subtype   = str2Pj(subType);
    return pj_mt;
}

///////////////////////////////////////////////////////////////////////////////

SipHeader::SipHeader()
{
    pj_str_t dummy;

    // Init pjHdr with null string to suppress warning
    pj_bzero(&dummy, sizeof(dummy));
    pjsip_generic_string_hdr_init2(&pjHdr, &dummy, &dummy);
}

void SipHeader::fromPj(const pjsip_hdr *hdr) PJSUA2_THROW(Error)
{
    char *buf = NULL;
    int len = 0;
    unsigned buf_size = 256>>1;

    /* Print header to a 256 bytes buffer first.
     * If buffer is not sufficient, try 512, 1024, soon
     * until > PJSIP_MAX_PKT_LEN
     */
    do {
        buf_size <<= 1;
        buf = (char*)malloc(buf_size);
        if (!buf)
            PJSUA2_RAISE_ERROR(PJ_ENOMEM);

        len = pjsip_hdr_print_on((void*)hdr, buf, buf_size-1);
        if (len < 0)
            free(buf);

    } while ((buf_size < PJSIP_MAX_PKT_LEN) && (len < 0));
    
    if (len < 0)
        PJSUA2_RAISE_ERROR(PJ_ETOOSMALL);

    buf[len] = '\0';

    char *pos = strchr(buf, ':');
    if (!pos) {
        free(buf);
        PJSUA2_RAISE_ERROR(PJSIP_EINVALIDHDR);
    }

    // Trim white space after header name
    char *end_name = pos;
    while (end_name>buf && pj_isspace(*(end_name-1))) --end_name;

    // Trim whitespaces after colon
    char *start_val = pos+1;
    while (*start_val && pj_isspace(*start_val)) ++start_val;

    hName = string(buf, end_name);
    hValue = string(start_val);
    free(buf);
}

pjsip_generic_string_hdr &SipHeader::toPj() const
{
    pj_str_t hname  = str2Pj(hName);
    pj_str_t hvalue = str2Pj(hValue);

    pjsip_generic_string_hdr_init2(&pjHdr, &hname, &hvalue);
    return pjHdr;
}

///////////////////////////////////////////////////////////////////////////////

SipMultipartPart::SipMultipartPart()
{
    pj_bzero(&pjMpp, sizeof(pjMpp));
    pj_bzero(&pjMsgBody, sizeof(pjMsgBody));
    pj_list_init(&pjMpp.hdr);
}

void SipMultipartPart::fromPj(const pjsip_multipart_part &prm)
                              PJSUA2_THROW(Error)
{
    headers.clear();
    pjsip_hdr* pj_hdr = prm.hdr.next;
    while (pj_hdr != &prm.hdr) {
        SipHeader sh;
        sh.fromPj(pj_hdr);
        headers.push_back(PJSUA2_MOVE(sh));
        pj_hdr = pj_hdr->next;
    }

    if (!prm.body)
        PJSUA2_RAISE_ERROR(PJ_EINVAL);
    
    contentType.fromPj(prm.body->content_type);
    body = string((char*)prm.body->data, prm.body->len);

    pj_list_init(&pjMpp.hdr);
    pjMpp.body = NULL;
    pj_bzero(&pjMsgBody, sizeof(pjMsgBody));
}

pjsip_multipart_part& SipMultipartPart::toPj() const
{
    pj_list_init(&pjMpp.hdr);
    for (unsigned i = 0; i < headers.size(); i++) {
        pjsip_generic_string_hdr& pj_hdr = headers[i].toPj();
        pj_list_push_back(&pjMpp.hdr, &pj_hdr);
    }

    pj_bzero(&pjMsgBody, sizeof(pjMsgBody));
    pjMsgBody.content_type  = contentType.toPj();
    pjMsgBody.print_body    = &pjsip_print_text_body;
    pjMsgBody.clone_data    = &pjsip_clone_text_data;
    pjMsgBody.data          = (void*)body.c_str();
    pjMsgBody.len           = (unsigned)body.size();
    pjMpp.body = &pjMsgBody;

    return pjMpp;
}

///////////////////////////////////////////////////////////////////////////////

SipEvent::SipEvent()
: type(PJSIP_EVENT_UNKNOWN), pjEvent(NULL)
{
}

void SipEvent::fromPj(const pjsip_event &ev)
{
    type = ev.type;
    if (type == PJSIP_EVENT_TIMER) {
        body.timer.entry = ev.body.timer.entry;
    } else if (type == PJSIP_EVENT_TSX_STATE) {
        body.tsxState.prevState = (pjsip_tsx_state_e)
        ev.body.tsx_state.prev_state;
        body.tsxState.tsx.fromPj(*ev.body.tsx_state.tsx);
        body.tsxState.type = ev.body.tsx_state.type;
        if (body.tsxState.type == PJSIP_EVENT_TX_MSG) {
            if (ev.body.tsx_state.src.tdata)
                body.tsxState.src.tdata.fromPj(*ev.body.tsx_state.src.tdata);
        } else if (body.tsxState.type == PJSIP_EVENT_RX_MSG) {
            if (ev.body.tsx_state.src.rdata)
                body.tsxState.src.rdata.fromPj(*ev.body.tsx_state.src.rdata);
        } else if (body.tsxState.type == PJSIP_EVENT_TRANSPORT_ERROR) {
            body.tsxState.src.status = ev.body.tsx_state.src.status;
        } else if (body.tsxState.type == PJSIP_EVENT_TIMER) {
            body.tsxState.src.timer = ev.body.tsx_state.src.timer;
        } else if (body.tsxState.type == PJSIP_EVENT_USER) {
            body.tsxState.src.data = ev.body.tsx_state.src.data;
        }
    } else if (type == PJSIP_EVENT_TX_MSG) {
        if (ev.body.tx_msg.tdata)
            body.txMsg.tdata.fromPj(*ev.body.tx_msg.tdata);
    } else if (type == PJSIP_EVENT_RX_MSG) {
        if (ev.body.rx_msg.rdata)
            body.rxMsg.rdata.fromPj(*ev.body.rx_msg.rdata);
    } else if (type == PJSIP_EVENT_TRANSPORT_ERROR) {
        if (ev.body.tx_error.tdata)
            body.txError.tdata.fromPj(*ev.body.tx_error.tdata);
        if (ev.body.tx_error.tsx)
            body.txError.tsx.fromPj(*ev.body.tx_error.tsx);
    } else if (type == PJSIP_EVENT_USER) {
        body.user.user1 = ev.body.user.user1;
        body.user.user2 = ev.body.user.user2;
        body.user.user3 = ev.body.user.user3;
        body.user.user4 = ev.body.user.user4;
    }
    pjEvent = (void *)&ev;
}

SipTxData::SipTxData()
: pjTxData(NULL)
{
}

void SipTxData::fromPj(pjsip_tx_data &tdata)
{
    char straddr[PJ_INET6_ADDRSTRLEN+10];
    
    info        = pjsip_tx_data_get_info(&tdata);
    pjsip_tx_data_encode(&tdata);
    wholeMsg    = string(tdata.buf.start, tdata.buf.cur - tdata.buf.start);
    if (pj_sockaddr_has_addr(&tdata.tp_info.dst_addr)) {
        pj_sockaddr_print(&tdata.tp_info.dst_addr, straddr, sizeof(straddr), 3);
        dstAddress  = straddr;
    } else {
        dstAddress = "";
    }
    pjTxData    = (void *)&tdata;
}

SipTransaction::SipTransaction()
: role(PJSIP_ROLE_UAC), statusCode(PJSIP_SC_NULL),
  state(PJSIP_TSX_STATE_NULL), pjTransaction(NULL)
{
}

void SipTransaction::fromPj(pjsip_transaction &tsx)
{
    this->role          = tsx.role;
    this->method        = pj2Str(tsx.method.name);
    this->statusCode    = tsx.status_code;
    this->statusText    = pj2Str(tsx.status_text);
    this->state         = tsx.state;
    if (tsx.last_tx)
        this->lastTx.fromPj(*tsx.last_tx);
    else
        this->lastTx.pjTxData = NULL;
    this->pjTransaction = (void *)&tsx;
}

TsxStateEvent::TsxStateEvent()
: prevState(PJSIP_TSX_STATE_NULL), type(PJSIP_EVENT_UNKNOWN)
{
}

bool SipTxOption::isEmpty() const
{
    return (targetUri == "" && localUri == "" && contactUri == "" &&
            headers.size() == 0 && contentType == "" && msgBody == "" &&
            multipartContentType.type == "" &&
            multipartContentType.subType == "" && multipartParts.size() == 0);
}

void SipTxOption::fromPj(const pjsua_msg_data &prm) PJSUA2_THROW(Error)
{
    targetUri = pj2Str(prm.target_uri);

    localUri = pj2Str(prm.local_uri);

    contactUri = pj2Str(prm.contact_uri);

    headers.clear();
    pjsip_hdr* pj_hdr = prm.hdr_list.next;
    while (pj_hdr != &prm.hdr_list) {
        SipHeader sh;
        sh.fromPj(pj_hdr);
        headers.push_back(PJSUA2_MOVE(sh));
        pj_hdr = pj_hdr->next;
    }

    contentType = pj2Str(prm.content_type);
    msgBody = pj2Str(prm.msg_body);
    multipartContentType.fromPj(prm.multipart_ctype);

    multipartParts.clear();
    pjsip_multipart_part* pj_mp = prm.multipart_parts.next;
    while (pj_mp != &prm.multipart_parts) {
        SipMultipartPart smp;
        smp.fromPj(*pj_mp);
        multipartParts.push_back(PJSUA2_MOVE(smp));
        pj_mp = pj_mp->next;
    }
}

void SipTxOption::toPj(pjsua_msg_data &msg_data) const
{
    unsigned i;

    pjsua_msg_data_init(&msg_data);

    msg_data.target_uri = str2Pj(targetUri);

    msg_data.local_uri = str2Pj(localUri);

    msg_data.contact_uri = str2Pj(contactUri);

    pj_list_init(&msg_data.hdr_list);
    for (i = 0; i < headers.size(); i++) {
        pjsip_generic_string_hdr& pj_hdr = headers[i].toPj();
        
        /* If the header is Max-Forwards, the header type needs to be
         * PJSIP_H_MAX_FORWARDS as PJSUA will compare the header type
         * instead of the string name.
         */
        if ((headers[i].hName.size() ==
             pjsip_hdr_names[PJSIP_H_MAX_FORWARDS].name_len) &&
            (pj_ansi_strnicmp(headers[i].hName.c_str(),
                              pjsip_hdr_names[PJSIP_H_MAX_FORWARDS].name,
                              headers[i].hName.size()) == 0))
        {
            pjsip_max_fwd_hdr *tmp = (pjsip_max_fwd_hdr*)&pj_hdr;

            pj_assert(sizeof(pjsip_generic_string_hdr) >=
                      sizeof(pjsip_max_fwd_hdr));
            pjsip_max_fwd_hdr_init(NULL, tmp, std::stoi(headers[i].hValue));
        }

        pj_list_push_back(&msg_data.hdr_list, &pj_hdr);
    }

    msg_data.content_type = str2Pj(contentType);
    msg_data.msg_body = str2Pj(msgBody);
    msg_data.multipart_ctype = multipartContentType.toPj();

    pj_list_init(&msg_data.multipart_parts);
    for (i = 0; i < multipartParts.size(); i++) {
        pjsip_multipart_part& pj_part = multipartParts[i].toPj();
        pj_list_push_back(&msg_data.multipart_parts, &pj_part);
    }
}

//////////////////////////////////////////////////////////////////////////////

SendInstantMessageParam::SendInstantMessageParam()
: contentType("text/plain"), content(""), userData(NULL)
{
}

SendTypingIndicationParam::SendTypingIndicationParam()
: isTyping(false)
{
}
