| 1 | /* |
| 2 | * Copyright (C) 2013 Teluu Inc. (http://www.teluu.com) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | #include <pjsua2/types.hpp> |
| 19 | #include <pjsua2/siptypes.hpp> |
| 20 | #include <pjsip/print_util.h> /* For pjsip_hdr_names */ |
| 21 | #include "util.hpp" |
| 22 | |
| 23 | using namespace pj; |
| 24 | using namespace std; |
| 25 | |
| 26 | #define THIS_FILE "siptypes.cpp" |
| 27 | |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | namespace pj |
| 30 | { |
| 31 | void readIntVector( ContainerNode &node, |
| 32 | const string &array_name, |
| 33 | IntVector &v) PJSUA2_THROW(Error) |
| 34 | { |
| 35 | ContainerNode array_node = node.readArray(array_name); |
| 36 | v.resize(0); |
| 37 | while (array_node.hasUnread()) { |
| 38 | v.push_back((int)array_node.readNumber()); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void writeIntVector(ContainerNode &node, |
| 43 | const string &array_name, |
| 44 | const IntVector &v) PJSUA2_THROW(Error) |
| 45 | { |
| 46 | ContainerNode array_node = node.writeNewArray(array_name); |
| 47 | for (unsigned i=0; i<v.size(); ++i) { |
| 48 | array_node.writeNumber("", (float)v[i]); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void readQosParams( ContainerNode &node, |
| 53 | pj_qos_params &qos) PJSUA2_THROW(Error) |
| 54 | { |
| 55 | ContainerNode this_node = node.readContainer("qosParams"); |
| 56 | |
| 57 | NODE_READ_NUM_T( this_node, pj_uint8_t, qos.flags); |
| 58 | NODE_READ_NUM_T( this_node, pj_uint8_t, qos.dscp_val); |
| 59 | NODE_READ_NUM_T( this_node, pj_uint8_t, qos.so_prio); |
| 60 | NODE_READ_NUM_T( this_node, pj_qos_wmm_prio, qos.wmm_prio); |
| 61 | } |
| 62 | |
| 63 | void writeQosParams( ContainerNode &node, |
| 64 | const pj_qos_params &qos) PJSUA2_THROW(Error) |
| 65 | { |
| 66 | ContainerNode this_node = node.writeNewContainer("qosParams"); |
| 67 | |
| 68 | NODE_WRITE_NUM_T( this_node, pj_uint8_t, qos.flags); |
| 69 | NODE_WRITE_NUM_T( this_node, pj_uint8_t, qos.dscp_val); |
| 70 | NODE_WRITE_NUM_T( this_node, pj_uint8_t, qos.so_prio); |
| 71 | NODE_WRITE_NUM_T( this_node, pj_qos_wmm_prio, qos.wmm_prio); |
| 72 | } |
| 73 | |
| 74 | void readSipHeaders( const ContainerNode &node, |
| 75 | const string &array_name, |
| 76 | SipHeaderVector &headers) PJSUA2_THROW(Error) |
| 77 | { |
| 78 | ContainerNode headers_node = node.readArray(array_name); |
| 79 | headers.resize(0); |
| 80 | while (headers_node.hasUnread()) { |
| 81 | SipHeader hdr; |
| 82 | |
| 83 | ContainerNode header_node = headers_node.readContainer("header"); |
| 84 | hdr.hName = header_node.readString("hname"); |
| 85 | hdr.hValue = header_node.readString("hvalue"); |
| 86 | headers.push_back(PJSUA2_MOVE(hdr)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void writeSipHeaders(ContainerNode &node, |
| 91 | const string &array_name, |
| 92 | const SipHeaderVector &headers) PJSUA2_THROW(Error) |
| 93 | { |
| 94 | ContainerNode headers_node = node.writeNewArray(array_name); |
| 95 | for (unsigned i=0; i<headers.size(); ++i) { |
| 96 | ContainerNode header_node = headers_node.writeNewContainer("header"); |
| 97 | header_node.writeString("hname", headers[i].hName); |
| 98 | header_node.writeString("hvalue", headers[i].hValue); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | } // namespace |
| 103 | /////////////////////////////////////////////////////////////////////////////// |
| 104 | |
| 105 | AuthCredInfo::AuthCredInfo() |
| 106 | : scheme("digest"), realm("*"), dataType(0), |
| 107 | algoType(PJSIP_AUTH_ALGORITHM_NOT_SET) |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | AuthCredInfo::AuthCredInfo(const string ¶m_scheme, |
| 112 | const string ¶m_realm, |
| 113 | const string ¶m_user_name, |
| 114 | const int param_data_type, |
| 115 | const string param_data) |
| 116 | : scheme(param_scheme), realm(param_realm), username(param_user_name), |
| 117 | dataType(param_data_type), data(PJSUA2_MOVE(param_data)), |
| 118 | algoType(PJSIP_AUTH_ALGORITHM_NOT_SET) |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | void AuthCredInfo::readObject(const ContainerNode &node) PJSUA2_THROW(Error) |
| 123 | { |
| 124 | ContainerNode this_node = node.readContainer("AuthCredInfo"); |
| 125 | |
| 126 | NODE_READ_STRING( this_node, scheme); |
| 127 | NODE_READ_STRING( this_node, realm); |
| 128 | NODE_READ_STRING( this_node, username); |
| 129 | NODE_READ_INT ( this_node, dataType); |
| 130 | NODE_READ_STRING( this_node, data); |
| 131 | NODE_READ_STRING( this_node, akaK); |
| 132 | NODE_READ_STRING( this_node, akaOp); |
| 133 | NODE_READ_STRING( this_node, akaAmf); |
| 134 | } |
| 135 | |
| 136 | void AuthCredInfo::writeObject(ContainerNode &node) const PJSUA2_THROW(Error) |
| 137 | { |
| 138 | ContainerNode this_node = node.writeNewContainer("AuthCredInfo"); |
| 139 | |
| 140 | NODE_WRITE_STRING( this_node, scheme); |
| 141 | NODE_WRITE_STRING( this_node, realm); |
| 142 | NODE_WRITE_STRING( this_node, username); |
| 143 | NODE_WRITE_INT ( this_node, dataType); |
| 144 | NODE_WRITE_STRING( this_node, data); |
| 145 | NODE_WRITE_STRING( this_node, akaK); |
| 146 | NODE_WRITE_STRING( this_node, akaOp); |
| 147 | NODE_WRITE_STRING( this_node, akaAmf); |
| 148 | } |
| 149 | |
| 150 | void AuthCredInfo::fromPj(const pjsip_cred_info &prm) |
| 151 | { |
| 152 | realm = pj2Str(prm.realm); |
| 153 | scheme = pj2Str(prm.scheme); |
| 154 | username = pj2Str(prm.username); |
| 155 | dataType = prm.data_type; |
| 156 | data = pj2Str(prm.data); |
| 157 | algoType = prm.algorithm_type; |
| 158 | akaK = pj2Str(prm.ext.aka.k); |
| 159 | akaOp = pj2Str(prm.ext.aka.op); |
| 160 | akaAmf = pj2Str(prm.ext.aka.amf); |
| 161 | } |
| 162 | |
| 163 | pjsip_cred_info AuthCredInfo::toPj() const |
| 164 | { |
| 165 | pjsip_cred_info ret; |
| 166 | ret.realm = str2Pj(realm); |
| 167 | ret.scheme = str2Pj(scheme); |
| 168 | ret.username = str2Pj(username); |
| 169 | ret.data_type = dataType; |
| 170 | ret.data = str2Pj(data); |
| 171 | ret.algorithm_type = algoType; |
| 172 | ret.ext.aka.k = str2Pj(akaK); |
| 173 | ret.ext.aka.op = str2Pj(akaOp); |
| 174 | ret.ext.aka.amf = str2Pj(akaAmf); |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | /////////////////////////////////////////////////////////////////////////////// |
| 179 | |
| 180 | TlsConfig::TlsConfig() : credDirectType(0), |
| 181 | privKeyDirect(NULL), certDirect(NULL), |
| 182 | method(PJSIP_SSL_UNSPECIFIED_METHOD), |
| 183 | qosType(PJ_QOS_TYPE_BEST_EFFORT) |
| 184 | { |
| 185 | pjsip_tls_setting ts; |
| 186 | pjsip_tls_setting_default(&ts); |
| 187 | this->fromPj(ts); |
| 188 | } |
| 189 | |
| 190 | pjsip_tls_setting TlsConfig::toPj() const |
| 191 | { |
| 192 | pjsip_tls_setting ts; |
| 193 | pjsip_tls_setting_default(&ts); |
| 194 | |
| 195 | ts.ca_list_file = str2Pj(this->CaListFile); |
| 196 | ts.cert_file = str2Pj(this->certFile); |
| 197 | ts.privkey_file = str2Pj(this->privKeyFile); |
| 198 | ts.password = str2Pj(this->password); |
| 199 | ts.ca_buf = str2Pj(this->CaBuf); |
| 200 | ts.cert_buf = str2Pj(this->certBuf); |
| 201 | ts.privkey_buf = str2Pj(this->privKeyBuf); |
| 202 | ts.cert_lookup.type = this->certLookupType; |
| 203 | ts.cert_lookup.keyword = str2Pj(this->certLookupKeyword); |
| 204 | |
| 205 | if (this->certDirect && |
| 206 | (this->credDirectType & PJ_SSL_CERT_DIRECT_OPENSSL_X509_CERT)) |
| 207 | { |
| 208 | ts.cert_direct.type |= PJ_SSL_CERT_DIRECT_OPENSSL_X509_CERT; |
| 209 | ts.cert_direct.cert = this->certDirect; |
| 210 | } |
| 211 | if (this->privKeyDirect && |
| 212 | (this->credDirectType & PJ_SSL_CERT_DIRECT_OPENSSL_EVP_PKEY)) |
| 213 | { |
| 214 | ts.cert_direct.type |= PJ_SSL_CERT_DIRECT_OPENSSL_EVP_PKEY; |
| 215 | ts.cert_direct.privkey = this->privKeyDirect; |
| 216 | } |
| 217 | |
| 218 | ts.method = this->method; |
| 219 | ts.ciphers_num = (unsigned)this->ciphers.size(); |
| 220 | ts.proto = this->proto; |
| 221 | // The following will only work if sizeof(enum)==sizeof(int) |
| 222 | pj_assert(sizeof(ts.ciphers[0]) == sizeof(int)); |
| 223 | ts.ciphers = ts.ciphers_num? |
| 224 | (pj_ssl_cipher*)&this->ciphers[0] : NULL; |
| 225 | ts.verify_server = this->verifyServer; |
| 226 | ts.verify_client = this->verifyClient; |
| 227 | ts.require_client_cert = this->requireClientCert; |
| 228 | ts.timeout.sec = this->msecTimeout / 1000; |
| 229 | ts.timeout.msec = this->msecTimeout % 1000; |
| 230 | ts.qos_type = this->qosType; |
| 231 | ts.qos_params = this->qosParams; |
| 232 | ts.qos_ignore_error = this->qosIgnoreError; |
| 233 | ts.sockopt_params = this->sockOptParams.toPj(); |
| 234 | ts.sockopt_ignore_error = this->sockOptIgnoreError; |
| 235 | ts.enable_renegotiation = this->enableRenegotiation; |
| 236 | |
| 237 | return ts; |
| 238 | } |
| 239 | |
| 240 | void TlsConfig::fromPj(const pjsip_tls_setting &prm) |
| 241 | { |
| 242 | this->CaListFile = pj2Str(prm.ca_list_file); |
| 243 | this->certFile = pj2Str(prm.cert_file); |
| 244 | this->privKeyFile = pj2Str(prm.privkey_file); |
| 245 | this->password = pj2Str(prm.password); |
| 246 | this->CaBuf = pj2Str(prm.ca_buf); |
| 247 | this->certBuf = pj2Str(prm.cert_buf); |
| 248 | this->privKeyBuf = pj2Str(prm.privkey_buf); |
| 249 | this->certLookupType= prm.cert_lookup.type; |
| 250 | this->certLookupKeyword = pj2Str(prm.cert_lookup.keyword); |
| 251 | this->credDirectType= 0; |
| 252 | |
| 253 | if (prm.cert_direct.cert && |
| 254 | (prm.cert_direct.type & PJ_SSL_CERT_DIRECT_OPENSSL_X509_CERT)) |
| 255 | { |
| 256 | this->credDirectType |= PJ_SSL_CERT_DIRECT_OPENSSL_X509_CERT; |
| 257 | this->certDirect = prm.cert_direct.cert; |
| 258 | } else { |
| 259 | this->certDirect = NULL; |
| 260 | } |
| 261 | |
| 262 | if (prm.cert_direct.privkey && |
| 263 | (prm.cert_direct.type & PJ_SSL_CERT_DIRECT_OPENSSL_EVP_PKEY)) |
| 264 | { |
| 265 | this->credDirectType |= PJ_SSL_CERT_DIRECT_OPENSSL_EVP_PKEY; |
| 266 | this->privKeyDirect = prm.cert_direct.privkey; |
| 267 | } else { |
| 268 | this->privKeyDirect = NULL; |
| 269 | } |
| 270 | |
| 271 | this->method = (pjsip_ssl_method)prm.method; |
| 272 | this->proto = prm.proto; |
| 273 | // The following will only work if sizeof(enum)==sizeof(int) |
| 274 | pj_assert(sizeof(prm.ciphers[0]) == sizeof(int)); |
| 275 | this->ciphers = IntVector(prm.ciphers, prm.ciphers+prm.ciphers_num); |
| 276 | this->verifyServer = PJ2BOOL(prm.verify_server); |
| 277 | this->verifyClient = PJ2BOOL(prm.verify_client); |
| 278 | this->requireClientCert = PJ2BOOL(prm.require_client_cert); |
| 279 | this->msecTimeout = PJ_TIME_VAL_MSEC(prm.timeout); |
| 280 | this->qosType = prm.qos_type; |
| 281 | this->qosParams = prm.qos_params; |
| 282 | this->qosIgnoreError = PJ2BOOL(prm.qos_ignore_error); |
| 283 | this->sockOptParams.fromPj(prm.sockopt_params); |
| 284 | this->sockOptIgnoreError = PJ2BOOL(prm.sockopt_ignore_error); |
| 285 | this->enableRenegotiation = PJ2BOOL(prm.enable_renegotiation); |
| 286 | } |
| 287 | |
| 288 | void TlsConfig::readObject(const ContainerNode &node) PJSUA2_THROW(Error) |
| 289 | { |
| 290 | ContainerNode this_node = node.readContainer("TlsConfig"); |
| 291 | |
| 292 | NODE_READ_STRING ( this_node, CaListFile); |
| 293 | NODE_READ_STRING ( this_node, certFile); |
| 294 | NODE_READ_STRING ( this_node, privKeyFile); |
| 295 | NODE_READ_STRING ( this_node, password); |
| 296 | NODE_READ_STRING ( this_node, CaBuf); |
| 297 | NODE_READ_STRING ( this_node, certBuf); |
| 298 | NODE_READ_STRING ( this_node, privKeyBuf); |
| 299 | NODE_READ_NUM_T ( this_node, pjsip_ssl_method, method); |
| 300 | readIntVector ( this_node, "ciphers", ciphers); |
| 301 | NODE_READ_BOOL ( this_node, verifyServer); |
| 302 | NODE_READ_BOOL ( this_node, verifyClient); |
| 303 | NODE_READ_BOOL ( this_node, requireClientCert); |
| 304 | NODE_READ_UNSIGNED( this_node, msecTimeout); |
| 305 | NODE_READ_NUM_T ( this_node, pj_qos_type, qosType); |
| 306 | readQosParams ( this_node, qosParams); |
| 307 | NODE_READ_BOOL ( this_node, qosIgnoreError); |
| 308 | NODE_READ_OBJ ( this_node, sockOptParams); |
| 309 | NODE_READ_BOOL ( this_node, sockOptIgnoreError); |
| 310 | NODE_READ_NUM_T ( this_node, pj_ssl_cert_lookup_type, certLookupType); |
| 311 | NODE_READ_STRING ( this_node, certLookupKeyword); |
| 312 | } |
| 313 | |
| 314 | void TlsConfig::writeObject(ContainerNode &node) const PJSUA2_THROW(Error) |
| 315 | { |
| 316 | ContainerNode this_node = node.writeNewContainer("TlsConfig"); |
| 317 | |
| 318 | NODE_WRITE_STRING ( this_node, CaListFile); |
| 319 | NODE_WRITE_STRING ( this_node, certFile); |
| 320 | NODE_WRITE_STRING ( this_node, privKeyFile); |
| 321 | NODE_WRITE_STRING ( this_node, password); |
| 322 | NODE_WRITE_STRING ( this_node, CaBuf); |
| 323 | NODE_WRITE_STRING ( this_node, certBuf); |
| 324 | NODE_WRITE_STRING ( this_node, privKeyBuf); |
| 325 | NODE_WRITE_NUM_T ( this_node, pjsip_ssl_method, method); |
| 326 | writeIntVector ( this_node, "ciphers", ciphers); |
| 327 | NODE_WRITE_BOOL ( this_node, verifyServer); |
| 328 | NODE_WRITE_BOOL ( this_node, verifyClient); |
| 329 | NODE_WRITE_BOOL ( this_node, requireClientCert); |
| 330 | NODE_WRITE_UNSIGNED( this_node, msecTimeout); |
| 331 | NODE_WRITE_NUM_T ( this_node, pj_qos_type, qosType); |
| 332 | writeQosParams ( this_node, qosParams); |
| 333 | NODE_WRITE_BOOL ( this_node, qosIgnoreError); |
| 334 | NODE_WRITE_OBJ ( this_node, sockOptParams); |
| 335 | NODE_WRITE_BOOL ( this_node, sockOptIgnoreError); |
| 336 | NODE_WRITE_NUM_T ( this_node, pj_ssl_cert_lookup_type, certLookupType); |
| 337 | NODE_WRITE_STRING ( this_node, certLookupKeyword); |
| 338 | } |
| 339 | |
| 340 | /////////////////////////////////////////////////////////////////////////////// |
| 341 | |
| 342 | SockOpt::SockOpt() |
| 343 | { |
| 344 | pj_bzero(this, sizeof(*this)); |
| 345 | } |
| 346 | |
| 347 | SockOpt::SockOpt(int level, int optName, int optVal) |
| 348 | { |
| 349 | pj_bzero(this, sizeof(*this)); |
| 350 | this->level = level; |
| 351 | this->optName = optName; |
| 352 | setOptValInt(optVal); |
| 353 | } |
| 354 | |
| 355 | void SockOpt::setOptValInt(int opt_val) |
| 356 | { |
| 357 | optVal = &optValInt; |
| 358 | optLen = sizeof(int); |
| 359 | optValInt = opt_val; |
| 360 | } |
| 361 | |
| 362 | SockOptParams::SockOptParams() |
| 363 | { |
| 364 | } |
| 365 | |
| 366 | pj_sockopt_params SockOptParams::toPj() const |
| 367 | { |
| 368 | pj_sockopt_params sop; |
| 369 | unsigned i; |
| 370 | |
| 371 | pj_bzero(&sop, sizeof(sop)); |
| 372 | sop.cnt = (unsigned)this->sockOpts.size(); |
| 373 | if (sop.cnt > PJ_MAX_SOCKOPT_PARAMS) |
| 374 | sop.cnt = PJ_MAX_SOCKOPT_PARAMS; |
| 375 | for (i = 0; i < sop.cnt; ++i) { |
| 376 | sop.options[i].level = this->sockOpts[i].level; |
| 377 | sop.options[i].optname = this->sockOpts[i].optName; |
| 378 | sop.options[i].optval = this->sockOpts[i].optVal; |
| 379 | sop.options[i].optlen = this->sockOpts[i].optLen; |
| 380 | } |
| 381 | |
| 382 | return sop; |
| 383 | } |
| 384 | |
| 385 | void SockOptParams::fromPj(const pj_sockopt_params &prm) |
| 386 | { |
| 387 | unsigned i; |
| 388 | |
| 389 | this->sockOpts.clear(); |
| 390 | for (i = 0; i < prm.cnt; ++i) { |
| 391 | SockOpt so; |
| 392 | so.level = prm.options[i].level; |
| 393 | so.optName = prm.options[i].optname; |
| 394 | if (prm.options[i].optlen == sizeof(int)) { |
| 395 | so.setOptValInt(*((int *)prm.options[i].optval)); |
| 396 | } |
| 397 | this->sockOpts.push_back(PJSUA2_MOVE(so)); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | void SockOptParams::readObject(const ContainerNode &node) PJSUA2_THROW(Error) |
| 402 | { |
| 403 | ContainerNode array_node = node.readArray("sockOptParams"); |
| 404 | sockOpts.resize(0); |
| 405 | while (array_node.hasUnread()) { |
| 406 | ContainerNode so_node = array_node.readContainer("sockOpt"); |
| 407 | SockOpt so; |
| 408 | so.level = so_node.readInt("level"); |
| 409 | so.optName = so_node.readInt("optName"); |
| 410 | so.optLen = so_node.readInt("optLen"); |
| 411 | if (so.optLen == sizeof(int)) { |
| 412 | int optVal = so_node.readInt("optVal"); |
| 413 | so.setOptValInt(optVal); |
| 414 | } |
| 415 | sockOpts.push_back(PJSUA2_MOVE(so)); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void SockOptParams::writeObject(ContainerNode &node) const PJSUA2_THROW(Error) |
| 420 | { |
| 421 | ContainerNode array_node = node.writeNewArray("sockOptParams"); |
| 422 | for (unsigned i=0; i<sockOpts.size(); ++i) { |
| 423 | ContainerNode so_node = array_node.writeNewContainer("sockOpt"); |
| 424 | string so_val((char*)sockOpts[i].optVal, |
| 425 | sockOpts[i].optLen > 0? sockOpts[i].optLen : 0); |
| 426 | so_node.writeInt("level", sockOpts[i].level); |
| 427 | so_node.writeInt("optName", sockOpts[i].optName); |
| 428 | so_node.writeInt("optLen", sockOpts[i].optLen); |
| 429 | if (sockOpts[i].optLen == sizeof(int)) { |
| 430 | so_node.writeInt("optVal", sockOpts[i].optValInt); |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /////////////////////////////////////////////////////////////////////////////// |
| 436 | |
| 437 | TransportConfig::TransportConfig() : qosType(PJ_QOS_TYPE_BEST_EFFORT) |
| 438 | { |
| 439 | pjsua_transport_config tc; |
| 440 | pjsua_transport_config_default(&tc); |
| 441 | this->fromPj(tc); |
| 442 | } |
| 443 | |
| 444 | void TransportConfig::fromPj(const pjsua_transport_config &prm) |
| 445 | { |
| 446 | this->port = prm.port; |
| 447 | this->portRange = prm.port_range; |
| 448 | this->randomizePort = PJ2BOOL(prm.randomize_port); |
| 449 | this->publicAddress = pj2Str(prm.public_addr); |
| 450 | this->boundAddress = pj2Str(prm.bound_addr); |
| 451 | this->tlsConfig.fromPj(prm.tls_setting); |
| 452 | this->qosType = prm.qos_type; |
| 453 | this->qosParams = prm.qos_params; |
| 454 | this->sockOptParams.fromPj(prm.sockopt_params); |
| 455 | } |
| 456 | |
| 457 | pjsua_transport_config TransportConfig::toPj() const |
| 458 | { |
| 459 | pjsua_transport_config tc; |
| 460 | pjsua_transport_config_default(&tc); |
| 461 | |
| 462 | tc.port = this->port; |
| 463 | tc.port_range = this->portRange; |
| 464 | tc.randomize_port = this->randomizePort; |
| 465 | tc.public_addr = str2Pj(this->publicAddress); |
| 466 | tc.bound_addr = str2Pj(this->boundAddress); |
| 467 | tc.tls_setting = this->tlsConfig.toPj(); |
| 468 | tc.qos_type = this->qosType; |
| 469 | tc.qos_params = this->qosParams; |
| 470 | tc.sockopt_params = this->sockOptParams.toPj(); |
| 471 | |
| 472 | return tc; |
| 473 | } |
| 474 | |
| 475 | void TransportConfig::readObject(const ContainerNode &node) PJSUA2_THROW(Error) |
| 476 | { |
| 477 | ContainerNode this_node = node.readContainer("TransportConfig"); |
| 478 | |
| 479 | NODE_READ_UNSIGNED ( this_node, port); |
| 480 | NODE_READ_UNSIGNED ( this_node, portRange); |
| 481 | NODE_READ_STRING ( this_node, publicAddress); |
| 482 | NODE_READ_STRING ( this_node, boundAddress); |
| 483 | NODE_READ_NUM_T ( this_node, pj_qos_type, qosType); |
| 484 | readQosParams ( this_node, qosParams); |
| 485 | NODE_READ_OBJ ( this_node, tlsConfig); |
| 486 | NODE_READ_OBJ ( this_node, sockOptParams); |
| 487 | } |
| 488 | |
| 489 | void TransportConfig::writeObject(ContainerNode &node) const |
| 490 | PJSUA2_THROW(Error) |
| 491 | { |
| 492 | ContainerNode this_node = node.writeNewContainer("TransportConfig"); |
| 493 | |
| 494 | NODE_WRITE_UNSIGNED ( this_node, port); |
| 495 | NODE_WRITE_UNSIGNED ( this_node, portRange); |
| 496 | NODE_WRITE_STRING ( this_node, publicAddress); |
| 497 | NODE_WRITE_STRING ( this_node, boundAddress); |
| 498 | NODE_WRITE_NUM_T ( this_node, pj_qos_type, qosType); |
| 499 | writeQosParams ( this_node, qosParams); |
| 500 | NODE_WRITE_OBJ ( this_node, tlsConfig); |
| 501 | NODE_WRITE_OBJ ( this_node, sockOptParams); |
| 502 | } |
| 503 | |
| 504 | /////////////////////////////////////////////////////////////////////////////// |
| 505 | |
| 506 | TransportInfo::TransportInfo() |
| 507 | : id(), type(PJSIP_TRANSPORT_UNSPECIFIED), flags(), usageCount() |
| 508 | { |
| 509 | } |
| 510 | |
| 511 | void TransportInfo::fromPj(const pjsua_transport_info &tinfo) |
| 512 | { |
| 513 | this->id = tinfo.id; |
| 514 | this->type = tinfo.type; |
| 515 | this->typeName = pj2Str(tinfo.type_name); |
| 516 | this->info = pj2Str(tinfo.info); |
| 517 | this->flags = tinfo.flag; |
| 518 | |
| 519 | char straddr[PJ_INET6_ADDRSTRLEN+10]; |
| 520 | pj_sockaddr_print(&tinfo.local_addr, straddr, sizeof(straddr), 3); |
| 521 | this->localAddress = straddr; |
| 522 | |
| 523 | pj_ansi_snprintf(straddr, sizeof(straddr), "%.*s:%d", |
| 524 | (int)tinfo.local_name.host.slen, |
| 525 | tinfo.local_name.host.ptr, |
| 526 | tinfo.local_name.port); |
| 527 | this->localName = straddr; |
| 528 | this->usageCount = tinfo.usage_count; |
| 529 | } |
| 530 | |
| 531 | /////////////////////////////////////////////////////////////////////////////// |
| 532 | |
| 533 | SipRxData::SipRxData() |
| 534 | : pjRxData(NULL) |
| 535 | { |
| 536 | } |
| 537 | |
| 538 | void SipRxData::fromPj(pjsip_rx_data &rdata) |
| 539 | { |
| 540 | char straddr[PJ_INET6_ADDRSTRLEN+10]; |
| 541 | |
| 542 | info = pjsip_rx_data_get_info(&rdata); |
| 543 | wholeMsg = string(rdata.msg_info.msg_buf, rdata.msg_info.len); |
| 544 | pj_sockaddr_print(&rdata.pkt_info.src_addr, straddr, sizeof(straddr), 3); |
| 545 | srcAddress = straddr; |
| 546 | pjRxData = (void *)&rdata; |
| 547 | } |
| 548 | |
| 549 | /////////////////////////////////////////////////////////////////////////////// |
| 550 | |
| 551 | void SipMediaType::fromPj(const pjsip_media_type &prm) |
| 552 | { |
| 553 | type = pj2Str(prm.type); |
| 554 | subType = pj2Str(prm.subtype); |
| 555 | } |
| 556 | |
| 557 | pjsip_media_type SipMediaType::toPj() const |
| 558 | { |
| 559 | pjsip_media_type pj_mt; |
| 560 | pj_bzero(&pj_mt, sizeof(pj_mt)); |
| 561 | pj_mt.type = str2Pj(type); |
| 562 | pj_mt.subtype = str2Pj(subType); |
| 563 | return pj_mt; |
| 564 | } |
| 565 | |
| 566 | /////////////////////////////////////////////////////////////////////////////// |
| 567 | |
| 568 | SipHeader::SipHeader() |
| 569 | { |
| 570 | pj_str_t dummy; |
| 571 | |
| 572 | // Init pjHdr with null string to suppress warning |
| 573 | pj_bzero(&dummy, sizeof(dummy)); |
| 574 | pjsip_generic_string_hdr_init2(&pjHdr, &dummy, &dummy); |
| 575 | } |
| 576 | |
| 577 | void SipHeader::fromPj(const pjsip_hdr *hdr) PJSUA2_THROW(Error) |
| 578 | { |
| 579 | char *buf = NULL; |
| 580 | int len = 0; |
| 581 | unsigned buf_size = 256>>1; |
| 582 | |
| 583 | /* Print header to a 256 bytes buffer first. |
| 584 | * If buffer is not sufficient, try 512, 1024, soon |
| 585 | * until > PJSIP_MAX_PKT_LEN |
| 586 | */ |
| 587 | do { |
| 588 | buf_size <<= 1; |
| 589 | buf = (char*)malloc(buf_size); |
| 590 | if (!buf) |
| 591 | PJSUA2_RAISE_ERROR(PJ_ENOMEM); |
| 592 | |
| 593 | len = pjsip_hdr_print_on((void*)hdr, buf, buf_size-1); |
| 594 | if (len < 0) |
| 595 | free(buf); |
| 596 | |
| 597 | } while ((buf_size < PJSIP_MAX_PKT_LEN) && (len < 0)); |
| 598 | |
| 599 | if (len < 0) |
| 600 | PJSUA2_RAISE_ERROR(PJ_ETOOSMALL); |
| 601 | |
| 602 | buf[len] = '\0'; |
| 603 | |
| 604 | char *pos = strchr(buf, ':'); |
| 605 | if (!pos) { |
| 606 | free(buf); |
| 607 | PJSUA2_RAISE_ERROR(PJSIP_EINVALIDHDR); |
| 608 | } |
| 609 | |
| 610 | // Trim white space after header name |
| 611 | char *end_name = pos; |
| 612 | while (end_name>buf && pj_isspace(*(end_name-1))) --end_name; |
| 613 | |
| 614 | // Trim whitespaces after colon |
| 615 | char *start_val = pos+1; |
| 616 | while (*start_val && pj_isspace(*start_val)) ++start_val; |
| 617 | |
| 618 | hName = string(buf, end_name); |
| 619 | hValue = string(start_val); |
| 620 | free(buf); |
| 621 | } |
| 622 | |
| 623 | pjsip_generic_string_hdr &SipHeader::toPj() const |
| 624 | { |
| 625 | pj_str_t hname = str2Pj(hName); |
| 626 | pj_str_t hvalue = str2Pj(hValue); |
| 627 | |
| 628 | pjsip_generic_string_hdr_init2(&pjHdr, &hname, &hvalue); |
| 629 | return pjHdr; |
| 630 | } |
| 631 | |
| 632 | /////////////////////////////////////////////////////////////////////////////// |
| 633 | |
| 634 | SipMultipartPart::SipMultipartPart() |
| 635 | { |
| 636 | pj_bzero(&pjMpp, sizeof(pjMpp)); |
| 637 | pj_bzero(&pjMsgBody, sizeof(pjMsgBody)); |
| 638 | pj_list_init(&pjMpp.hdr); |
| 639 | } |
| 640 | |
| 641 | void SipMultipartPart::fromPj(const pjsip_multipart_part &prm) |
| 642 | PJSUA2_THROW(Error) |
| 643 | { |
| 644 | headers.clear(); |
| 645 | pjsip_hdr* pj_hdr = prm.hdr.next; |
| 646 | while (pj_hdr != &prm.hdr) { |
| 647 | SipHeader sh; |
| 648 | sh.fromPj(pj_hdr); |
| 649 | headers.push_back(PJSUA2_MOVE(sh)); |
| 650 | pj_hdr = pj_hdr->next; |
| 651 | } |
| 652 | |
| 653 | if (!prm.body) |
| 654 | PJSUA2_RAISE_ERROR(PJ_EINVAL); |
| 655 | |
| 656 | contentType.fromPj(prm.body->content_type); |
| 657 | body = string((char*)prm.body->data, prm.body->len); |
| 658 | |
| 659 | pj_list_init(&pjMpp.hdr); |
| 660 | pjMpp.body = NULL; |
| 661 | pj_bzero(&pjMsgBody, sizeof(pjMsgBody)); |
| 662 | } |
| 663 | |
| 664 | pjsip_multipart_part& SipMultipartPart::toPj() const |
| 665 | { |
| 666 | pj_list_init(&pjMpp.hdr); |
| 667 | for (unsigned i = 0; i < headers.size(); i++) { |
| 668 | pjsip_generic_string_hdr& pj_hdr = headers[i].toPj(); |
| 669 | pj_list_push_back(&pjMpp.hdr, &pj_hdr); |
| 670 | } |
| 671 | |
| 672 | pj_bzero(&pjMsgBody, sizeof(pjMsgBody)); |
| 673 | pjMsgBody.content_type = contentType.toPj(); |
| 674 | pjMsgBody.print_body = &pjsip_print_text_body; |
| 675 | pjMsgBody.clone_data = &pjsip_clone_text_data; |
| 676 | pjMsgBody.data = (void*)body.c_str(); |
| 677 | pjMsgBody.len = (unsigned)body.size(); |
| 678 | pjMpp.body = &pjMsgBody; |
| 679 | |
| 680 | return pjMpp; |
| 681 | } |
| 682 | |
| 683 | /////////////////////////////////////////////////////////////////////////////// |
| 684 | |
| 685 | SipEvent::SipEvent() |
| 686 | : type(PJSIP_EVENT_UNKNOWN), pjEvent(NULL) |
| 687 | { |
| 688 | } |
| 689 | |
| 690 | void SipEvent::fromPj(const pjsip_event &ev) |
| 691 | { |
| 692 | type = ev.type; |
| 693 | if (type == PJSIP_EVENT_TIMER) { |
| 694 | body.timer.entry = ev.body.timer.entry; |
| 695 | } else if (type == PJSIP_EVENT_TSX_STATE) { |
| 696 | body.tsxState.prevState = (pjsip_tsx_state_e) |
| 697 | ev.body.tsx_state.prev_state; |
| 698 | body.tsxState.tsx.fromPj(*ev.body.tsx_state.tsx); |
| 699 | body.tsxState.type = ev.body.tsx_state.type; |
| 700 | if (body.tsxState.type == PJSIP_EVENT_TX_MSG) { |
| 701 | if (ev.body.tsx_state.src.tdata) |
| 702 | body.tsxState.src.tdata.fromPj(*ev.body.tsx_state.src.tdata); |
| 703 | } else if (body.tsxState.type == PJSIP_EVENT_RX_MSG) { |
| 704 | if (ev.body.tsx_state.src.rdata) |
| 705 | body.tsxState.src.rdata.fromPj(*ev.body.tsx_state.src.rdata); |
| 706 | } else if (body.tsxState.type == PJSIP_EVENT_TRANSPORT_ERROR) { |
| 707 | body.tsxState.src.status = ev.body.tsx_state.src.status; |
| 708 | } else if (body.tsxState.type == PJSIP_EVENT_TIMER) { |
| 709 | body.tsxState.src.timer = ev.body.tsx_state.src.timer; |
| 710 | } else if (body.tsxState.type == PJSIP_EVENT_USER) { |
| 711 | body.tsxState.src.data = ev.body.tsx_state.src.data; |
| 712 | } |
| 713 | } else if (type == PJSIP_EVENT_TX_MSG) { |
| 714 | if (ev.body.tx_msg.tdata) |
| 715 | body.txMsg.tdata.fromPj(*ev.body.tx_msg.tdata); |
| 716 | } else if (type == PJSIP_EVENT_RX_MSG) { |
| 717 | if (ev.body.rx_msg.rdata) |
| 718 | body.rxMsg.rdata.fromPj(*ev.body.rx_msg.rdata); |
| 719 | } else if (type == PJSIP_EVENT_TRANSPORT_ERROR) { |
| 720 | if (ev.body.tx_error.tdata) |
| 721 | body.txError.tdata.fromPj(*ev.body.tx_error.tdata); |
| 722 | if (ev.body.tx_error.tsx) |
| 723 | body.txError.tsx.fromPj(*ev.body.tx_error.tsx); |
| 724 | } else if (type == PJSIP_EVENT_USER) { |
| 725 | body.user.user1 = ev.body.user.user1; |
| 726 | body.user.user2 = ev.body.user.user2; |
| 727 | body.user.user3 = ev.body.user.user3; |
| 728 | body.user.user4 = ev.body.user.user4; |
| 729 | } |
| 730 | pjEvent = (void *)&ev; |
| 731 | } |
| 732 | |
| 733 | SipTxData::SipTxData() |
| 734 | : pjTxData(NULL) |
| 735 | { |
| 736 | } |
| 737 | |
| 738 | void SipTxData::fromPj(pjsip_tx_data &tdata) |
| 739 | { |
| 740 | char straddr[PJ_INET6_ADDRSTRLEN+10]; |
| 741 | |
| 742 | info = pjsip_tx_data_get_info(&tdata); |
| 743 | pjsip_tx_data_encode(&tdata); |
| 744 | wholeMsg = string(tdata.buf.start, tdata.buf.cur - tdata.buf.start); |
| 745 | if (pj_sockaddr_has_addr(&tdata.tp_info.dst_addr)) { |
| 746 | pj_sockaddr_print(&tdata.tp_info.dst_addr, straddr, sizeof(straddr), 3); |
| 747 | dstAddress = straddr; |
| 748 | } else { |
| 749 | dstAddress = ""; |
| 750 | } |
| 751 | pjTxData = (void *)&tdata; |
| 752 | } |
| 753 | |
| 754 | SipTransaction::SipTransaction() |
| 755 | : role(PJSIP_ROLE_UAC), statusCode(PJSIP_SC_NULL), |
| 756 | state(PJSIP_TSX_STATE_NULL), pjTransaction(NULL) |
| 757 | { |
| 758 | } |
| 759 | |
| 760 | void SipTransaction::fromPj(pjsip_transaction &tsx) |
| 761 | { |
| 762 | this->role = tsx.role; |
| 763 | this->method = pj2Str(tsx.method.name); |
| 764 | this->statusCode = tsx.status_code; |
| 765 | this->statusText = pj2Str(tsx.status_text); |
| 766 | this->state = tsx.state; |
| 767 | if (tsx.last_tx) |
| 768 | this->lastTx.fromPj(*tsx.last_tx); |
| 769 | else |
| 770 | this->lastTx.pjTxData = NULL; |
| 771 | this->pjTransaction = (void *)&tsx; |
| 772 | } |
| 773 | |
| 774 | TsxStateEvent::TsxStateEvent() |
| 775 | : prevState(PJSIP_TSX_STATE_NULL), type(PJSIP_EVENT_UNKNOWN) |
| 776 | { |
| 777 | } |
| 778 | |
| 779 | bool SipTxOption::isEmpty() const |
| 780 | { |
| 781 | return (targetUri == "" && localUri == "" && contactUri == "" && |
| 782 | headers.size() == 0 && contentType == "" && msgBody == "" && |
| 783 | multipartContentType.type == "" && |
| 784 | multipartContentType.subType == "" && multipartParts.size() == 0); |
| 785 | } |
| 786 | |
| 787 | void SipTxOption::fromPj(const pjsua_msg_data &prm) PJSUA2_THROW(Error) |
| 788 | { |
| 789 | targetUri = pj2Str(prm.target_uri); |
| 790 | |
| 791 | localUri = pj2Str(prm.local_uri); |
| 792 | |
| 793 | contactUri = pj2Str(prm.contact_uri); |
| 794 | |
| 795 | headers.clear(); |
| 796 | pjsip_hdr* pj_hdr = prm.hdr_list.next; |
| 797 | while (pj_hdr != &prm.hdr_list) { |
| 798 | SipHeader sh; |
| 799 | sh.fromPj(pj_hdr); |
| 800 | headers.push_back(PJSUA2_MOVE(sh)); |
| 801 | pj_hdr = pj_hdr->next; |
| 802 | } |
| 803 | |
| 804 | contentType = pj2Str(prm.content_type); |
| 805 | msgBody = pj2Str(prm.msg_body); |
| 806 | multipartContentType.fromPj(prm.multipart_ctype); |
| 807 | |
| 808 | multipartParts.clear(); |
| 809 | pjsip_multipart_part* pj_mp = prm.multipart_parts.next; |
| 810 | while (pj_mp != &prm.multipart_parts) { |
| 811 | SipMultipartPart smp; |
| 812 | smp.fromPj(*pj_mp); |
| 813 | multipartParts.push_back(PJSUA2_MOVE(smp)); |
| 814 | pj_mp = pj_mp->next; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | void SipTxOption::toPj(pjsua_msg_data &msg_data) const |
| 819 | { |
| 820 | unsigned i; |
| 821 | |
| 822 | pjsua_msg_data_init(&msg_data); |
| 823 | |
| 824 | msg_data.target_uri = str2Pj(targetUri); |
| 825 | |
| 826 | msg_data.local_uri = str2Pj(localUri); |
| 827 | |
| 828 | msg_data.contact_uri = str2Pj(contactUri); |
| 829 | |
| 830 | pj_list_init(&msg_data.hdr_list); |
| 831 | for (i = 0; i < headers.size(); i++) { |
| 832 | pjsip_generic_string_hdr& pj_hdr = headers[i].toPj(); |
| 833 | |
| 834 | /* If the header is Max-Forwards, the header type needs to be |
| 835 | * PJSIP_H_MAX_FORWARDS as PJSUA will compare the header type |
| 836 | * instead of the string name. |
| 837 | */ |
| 838 | if ((headers[i].hName.size() == |
| 839 | pjsip_hdr_names[PJSIP_H_MAX_FORWARDS].name_len) && |
| 840 | (pj_ansi_strnicmp(headers[i].hName.c_str(), |
| 841 | pjsip_hdr_names[PJSIP_H_MAX_FORWARDS].name, |
| 842 | headers[i].hName.size()) == 0)) |
| 843 | { |
| 844 | pjsip_max_fwd_hdr *tmp = (pjsip_max_fwd_hdr*)&pj_hdr; |
| 845 | |
| 846 | pj_assert(sizeof(pjsip_generic_string_hdr) >= |
| 847 | sizeof(pjsip_max_fwd_hdr)); |
| 848 | pjsip_max_fwd_hdr_init(NULL, tmp, std::stoi(headers[i].hValue)); |
| 849 | } |
| 850 | |
| 851 | pj_list_push_back(&msg_data.hdr_list, &pj_hdr); |
| 852 | } |
| 853 | |
| 854 | msg_data.content_type = str2Pj(contentType); |
| 855 | msg_data.msg_body = str2Pj(msgBody); |
| 856 | msg_data.multipart_ctype = multipartContentType.toPj(); |
| 857 | |
| 858 | pj_list_init(&msg_data.multipart_parts); |
| 859 | for (i = 0; i < multipartParts.size(); i++) { |
| 860 | pjsip_multipart_part& pj_part = multipartParts[i].toPj(); |
| 861 | pj_list_push_back(&msg_data.multipart_parts, &pj_part); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | ////////////////////////////////////////////////////////////////////////////// |
| 866 | |
| 867 | SendInstantMessageParam::SendInstantMessageParam() |
| 868 | : contentType("text/plain"), content(""), userData(NULL) |
| 869 | { |
| 870 | } |
| 871 | |
| 872 | SendTypingIndicationParam::SendTypingIndicationParam() |
| 873 | : isTyping(false) |
| 874 | { |
| 875 | } |