463
620
tlsMap->getValue(TIMEOUT_KEY, &tlsNegotiationTimeoutSec_);
464
621
tlsMap->getValue(TIMEOUT_KEY, &tlsNegotiationTimeoutMsec_);
468
void SIPAccount::setAccountDetails(std::map<std::string, std::string> details)
623
mapNode.getValue(USER_AGENT_KEY, &userAgent_);
625
unserializeRange(mapNode, AUDIO_PORT_MIN_KEY, AUDIO_PORT_MAX_KEY, audioPortRange_);
627
unserializeRange(mapNode, VIDEO_PORT_MIN_KEY, VIDEO_PORT_MAX_KEY, videoPortRange_);
633
template <typename T>
634
void parseInt(const std::map<std::string, std::string> &details, const char *key, T &i)
636
const auto iter = details.find(key);
637
if (iter == details.end()) {
638
ERROR("Couldn't find key %s", key);
641
i = atoi(iter->second.c_str());
646
void SIPAccount::setAccountDetails(const std::map<std::string, std::string> &details)
470
648
// Account setting common to SIP and IAX
471
alias_ = details[CONFIG_ACCOUNT_ALIAS];
472
username_ = details[CONFIG_ACCOUNT_USERNAME];
473
hostname_ = details[CONFIG_ACCOUNT_HOSTNAME];
474
enabled_ = details[CONFIG_ACCOUNT_ENABLE] == TRUE_STR;
475
autoAnswerEnabled_ = details[CONFIG_ACCOUNT_AUTOANSWER] == TRUE_STR;
476
ringtonePath_ = details[CONFIG_RINGTONE_PATH];
477
ringtoneEnabled_ = details[CONFIG_RINGTONE_ENABLED] == TRUE_STR;
478
mailBox_ = details[CONFIG_ACCOUNT_MAILBOX];
649
parseString(details, CONFIG_ACCOUNT_ALIAS, alias_);
650
parseString(details, CONFIG_ACCOUNT_USERNAME, username_);
651
parseString(details, CONFIG_ACCOUNT_HOSTNAME, hostname_);
652
parseBool(details, CONFIG_ACCOUNT_ENABLE, enabled_);
653
parseBool(details, CONFIG_ACCOUNT_AUTOANSWER, autoAnswerEnabled_);
654
parseString(details, CONFIG_RINGTONE_PATH, ringtonePath_);
655
parseBool(details, CONFIG_RINGTONE_ENABLED, ringtoneEnabled_);
656
parseString(details, CONFIG_ACCOUNT_MAILBOX, mailBox_);
480
658
// SIP specific account settings
482
660
// general sip settings
483
displayName_ = details[CONFIG_DISPLAY_NAME];
484
serviceRoute_ = details[CONFIG_ACCOUNT_ROUTESET];
485
interface_ = details[CONFIG_LOCAL_INTERFACE];
486
publishedSameasLocal_ = details[CONFIG_PUBLISHED_SAMEAS_LOCAL] == TRUE_STR;
487
publishedIpAddress_ = details[CONFIG_PUBLISHED_ADDRESS];
488
localPort_ = atoi(details[CONFIG_LOCAL_PORT].c_str());
489
publishedPort_ = atoi(details[CONFIG_PUBLISHED_PORT].c_str());
490
if (stunServer_ != details[CONFIG_STUN_SERVER]) {
491
link_->sipTransport.destroyStunResolver(stunServer_);
492
// pj_stun_sock_destroy(pj_stun_sock *stun_sock);
494
stunServer_ = details[CONFIG_STUN_SERVER];
495
stunEnabled_ = details[CONFIG_STUN_ENABLE] == TRUE_STR;
496
dtmfType_ = details[CONFIG_ACCOUNT_DTMF_TYPE];
497
registrationExpire_ = atoi(details[CONFIG_ACCOUNT_REGISTRATION_EXPIRE].c_str());
498
if(registrationExpire_ < MIN_REGISTRATION_TIME)
661
parseString(details, CONFIG_DISPLAY_NAME, displayName_);
662
parseString(details, CONFIG_ACCOUNT_ROUTESET, serviceRoute_);
663
parseString(details, CONFIG_LOCAL_INTERFACE, interface_);
664
parseBool(details, CONFIG_PUBLISHED_SAMEAS_LOCAL, publishedSameasLocal_);
665
parseString(details, CONFIG_PUBLISHED_ADDRESS, publishedIpAddress_);
666
parseInt(details, CONFIG_LOCAL_PORT, localPort_);
667
parseInt(details, CONFIG_PUBLISHED_PORT, publishedPort_);
669
if (not publishedSameasLocal_)
670
usePublishedAddressPortInVIA();
672
parseString(details, CONFIG_STUN_SERVER, stunServer_);
673
parseBool(details, CONFIG_STUN_ENABLE, stunEnabled_);
674
parseString(details, CONFIG_ACCOUNT_DTMF_TYPE, dtmfType_);
675
parseInt(details, CONFIG_ACCOUNT_REGISTRATION_EXPIRE, registrationExpire_);
677
if (registrationExpire_ < MIN_REGISTRATION_TIME)
499
678
registrationExpire_ = MIN_REGISTRATION_TIME;
501
userAgent_ = details[CONFIG_ACCOUNT_USERAGENT];
502
keepAliveEnabled_ = details[CONFIG_KEEP_ALIVE_ENABLED] == TRUE_STR;
680
parseString(details, CONFIG_ACCOUNT_USERAGENT, userAgent_);
681
parseBool(details, CONFIG_KEEP_ALIVE_ENABLED, keepAliveEnabled_);
683
bool presenceEnabled = false;
684
parseBool(details, CONFIG_PRESENCE_ENABLED, presenceEnabled);
685
enablePresence(presenceEnabled);
689
parseInt(details, CONFIG_ACCOUNT_AUDIO_PORT_MIN, tmpMin);
691
parseInt(details, CONFIG_ACCOUNT_AUDIO_PORT_MAX, tmpMax);
692
updateRange(tmpMin, tmpMax, audioPortRange_);
695
parseInt(details, CONFIG_ACCOUNT_VIDEO_PORT_MIN, tmpMin);
697
parseInt(details, CONFIG_ACCOUNT_VIDEO_PORT_MAX, tmpMax);
698
updateRange(tmpMin, tmpMax, videoPortRange_);
505
srtpEnabled_ = details[CONFIG_SRTP_ENABLE] == TRUE_STR;
506
srtpFallback_ = details[CONFIG_SRTP_RTP_FALLBACK] == TRUE_STR;
507
zrtpDisplaySas_ = details[CONFIG_ZRTP_DISPLAY_SAS] == TRUE_STR;
508
zrtpDisplaySasOnce_ = details[CONFIG_ZRTP_DISPLAY_SAS_ONCE] == TRUE_STR;
509
zrtpNotSuppWarning_ = details[CONFIG_ZRTP_NOT_SUPP_WARNING] == TRUE_STR;
510
zrtpHelloHash_ = details[CONFIG_ZRTP_HELLO_HASH] == TRUE_STR;
511
srtpKeyExchange_ = details[CONFIG_SRTP_KEY_EXCHANGE];
702
parseBool(details, CONFIG_SRTP_ENABLE, srtpEnabled_);
703
parseBool(details, CONFIG_SRTP_RTP_FALLBACK, srtpFallback_);
704
parseBool(details, CONFIG_ZRTP_DISPLAY_SAS, zrtpDisplaySas_);
705
parseBool(details, CONFIG_ZRTP_DISPLAY_SAS_ONCE, zrtpDisplaySasOnce_);
706
parseBool(details, CONFIG_ZRTP_NOT_SUPP_WARNING, zrtpNotSuppWarning_);
707
parseBool(details, CONFIG_ZRTP_HELLO_HASH, zrtpHelloHash_);
708
auto iter = details.find(CONFIG_SRTP_KEY_EXCHANGE);
709
if (iter != details.end())
710
validate(srtpKeyExchange_, iter->second, VALID_SRTP_KEY_EXCHANGES);
514
tlsListenerPort_ = atoi(details[CONFIG_TLS_LISTENER_PORT].c_str());
515
tlsEnable_ = details[CONFIG_TLS_ENABLE];
516
tlsCaListFile_ = details[CONFIG_TLS_CA_LIST_FILE];
517
tlsCertificateFile_ = details[CONFIG_TLS_CERTIFICATE_FILE];
518
tlsPrivateKeyFile_ = details[CONFIG_TLS_PRIVATE_KEY_FILE];
519
tlsPassword_ = details[CONFIG_TLS_PASSWORD];
520
tlsMethod_ = details[CONFIG_TLS_METHOD];
521
tlsCiphers_ = details[CONFIG_TLS_CIPHERS];
522
tlsServerName_ = details[CONFIG_TLS_SERVER_NAME];
523
tlsVerifyServer_ = details[CONFIG_TLS_VERIFY_SERVER] == TRUE_STR;
524
tlsVerifyClient_ = details[CONFIG_TLS_VERIFY_CLIENT] == TRUE_STR;
525
tlsRequireClientCertificate_ = details[CONFIG_TLS_REQUIRE_CLIENT_CERTIFICATE] == TRUE_STR;
526
tlsNegotiationTimeoutSec_ = details[CONFIG_TLS_NEGOTIATION_TIMEOUT_SEC];
527
tlsNegotiationTimeoutMsec_ = details[CONFIG_TLS_NEGOTIATION_TIMEOUT_MSEC];
713
parseBool(details, CONFIG_TLS_ENABLE, tlsEnable_);
714
parseInt(details, CONFIG_TLS_LISTENER_PORT, tlsListenerPort_);
715
parseString(details, CONFIG_TLS_CA_LIST_FILE, tlsCaListFile_);
716
parseString(details, CONFIG_TLS_CERTIFICATE_FILE, tlsCertificateFile_);
717
parseString(details, CONFIG_TLS_PRIVATE_KEY_FILE, tlsPrivateKeyFile_);
718
parseString(details, CONFIG_TLS_PASSWORD, tlsPassword_);
719
iter = details.find(CONFIG_TLS_METHOD);
720
if (iter != details.end())
721
validate(tlsMethod_, iter->second, VALID_TLS_METHODS);
722
parseString(details, CONFIG_TLS_CIPHERS, tlsCiphers_);
723
parseString(details, CONFIG_TLS_SERVER_NAME, tlsServerName_);
724
parseBool(details, CONFIG_TLS_VERIFY_SERVER, tlsVerifyServer_);
725
parseBool(details, CONFIG_TLS_VERIFY_CLIENT, tlsVerifyClient_);
726
parseBool(details, CONFIG_TLS_REQUIRE_CLIENT_CERTIFICATE, tlsRequireClientCertificate_);
727
parseString(details, CONFIG_TLS_NEGOTIATION_TIMEOUT_SEC, tlsNegotiationTimeoutSec_);
728
parseString(details, CONFIG_TLS_NEGOTIATION_TIMEOUT_MSEC, tlsNegotiationTimeoutMsec_);
529
730
if (credentials_.empty()) { // credentials not set, construct 1 entry
731
WARN("No credentials set, inferring them...");
530
732
std::vector<std::map<std::string, std::string> > v;
531
733
std::map<std::string, std::string> map;
532
734
map[CONFIG_ACCOUNT_USERNAME] = username_;
533
map[CONFIG_ACCOUNT_PASSWORD] = details[CONFIG_ACCOUNT_PASSWORD];
534
map[CONFIG_ACCOUNT_REALM] = "*";
735
parseString(details, CONFIG_ACCOUNT_PASSWORD, map[CONFIG_ACCOUNT_PASSWORD]);
736
map[CONFIG_ACCOUNT_REALM] = "*";
535
737
v.push_back(map);
536
738
setCredentials(v);
742
static std::string retrievePassword(const std::map<std::string, std::string>& map, const std::string &username)
744
std::map<std::string, std::string>::const_iterator map_iter_username;
745
std::map<std::string, std::string>::const_iterator map_iter_password;
746
map_iter_username = map.find(CONFIG_ACCOUNT_USERNAME);
748
if (map_iter_username != map.end()) {
749
if (map_iter_username->second == username) {
750
map_iter_password = map.find(CONFIG_ACCOUNT_PASSWORD);
752
if (map_iter_password != map.end()) {
753
return map_iter_password->second;
762
addRangeToDetails(std::map<std::string, std::string> &a, const char *minKey, const char *maxKey, const std::pair<uint16_t, uint16_t> &range)
764
std::ostringstream os;
766
a[minKey] = os.str();
769
a[maxKey] = os.str();
540
772
std::map<std::string, std::string> SIPAccount::getAccountDetails() const
542
774
std::map<std::string, std::string> a;
544
a[CONFIG_ACCOUNT_ID] = accountID_;
545
776
// note: The IP2IP profile will always have IP2IP as an alias
546
777
a[CONFIG_ACCOUNT_ALIAS] = alias_;
548
a[CONFIG_ACCOUNT_ENABLE] = enabled_ ? TRUE_STR : FALSE_STR;
549
a[CONFIG_ACCOUNT_AUTOANSWER]= autoAnswerEnabled_ ? TRUE_STR : FALSE_STR;
779
a[CONFIG_ACCOUNT_ENABLE] = enabled_ ? Conf::TRUE_STR : Conf::FALSE_STR;
780
a[CONFIG_ACCOUNT_AUTOANSWER] = autoAnswerEnabled_ ? Conf::TRUE_STR : Conf::FALSE_STR;
550
781
a[CONFIG_ACCOUNT_TYPE] = ACCOUNT_TYPE;
551
782
a[CONFIG_ACCOUNT_HOSTNAME] = hostname_;
552
783
a[CONFIG_ACCOUNT_USERNAME] = username_;
784
// get password for this username
785
a[CONFIG_ACCOUNT_PASSWORD] = "";
787
if (hasCredentials()) {
789
for (const auto &vect_item : credentials_) {
790
const std::string password = retrievePassword(vect_item, username_);
792
if (not password.empty())
793
a[CONFIG_ACCOUNT_PASSWORD] = password;
554
797
a[CONFIG_RINGTONE_PATH] = ringtonePath_;
555
a[CONFIG_RINGTONE_ENABLED] = ringtoneEnabled_ ? TRUE_STR : FALSE_STR;
798
a[CONFIG_RINGTONE_ENABLED] = ringtoneEnabled_ ? Conf::TRUE_STR : Conf::FALSE_STR;
556
799
a[CONFIG_ACCOUNT_MAILBOX] = mailBox_;
801
a[CONFIG_PRESENCE_ENABLED] = presence_ and presence_->isEnabled()? Conf::TRUE_STR : Conf::FALSE_STR;
802
a[CONFIG_PRESENCE_PUBLISH_SUPPORTED] = presence_ and presence_->isSupported(PRESENCE_FUNCTION_PUBLISH)? Conf::TRUE_STR : Conf::FALSE_STR;
803
a[CONFIG_PRESENCE_SUBSCRIBE_SUPPORTED] = presence_ and presence_->isSupported(PRESENCE_FUNCTION_SUBSCRIBE)? Conf::TRUE_STR : Conf::FALSE_STR;
804
// initialize status values
805
a[CONFIG_PRESENCE_STATUS] = presence_ and presence_->isOnline()? Conf::TRUE_STR : Conf::FALSE_STR;
806
a[CONFIG_PRESENCE_NOTE] = presence_? presence_->getNote() : " ";
558
809
RegistrationState state = UNREGISTERED;
559
810
std::string registrationStateCode;
935
1226
std::string address, port;
936
1227
std::ostringstream portstr;
938
link_->sipTransport.findLocalAddressFromTransport(transport_, transportType, address, port);
940
if (!receivedParameter_.empty())
941
address = receivedParameter_;
1229
link_->sipTransport->findLocalAddressFromTransport(transport_, transportType, address, port);
1231
if (not publishedSameasLocal_) {
1232
address = publishedIpAddress_;
1233
portstr << publishedPort_;
945
1234
port = portstr.str();
1235
DEBUG("Using published address %s and port %s", address.c_str(), port.c_str());
1236
} else if (stunEnabled_) {
1237
link_->sipTransport->findLocalAddressFromSTUN(transport_, &stunServerName_, stunPort_, address, port);
1238
publishedIpAddress_ = address;
1239
publishedPort_ = atoi(port.c_str());
1240
usePublishedAddressPortInVIA();
1242
if (!receivedParameter_.empty()) {
1243
address = receivedParameter_;
1244
DEBUG("Using received address %s", address.c_str());
1247
if (rPort_ != -1 and rPort_ != 0) {
1249
port = portstr.str();
1250
DEBUG("Using received port %s", port.c_str());
948
1254
// UDP does not require the transport specification
949
1255
std::string scheme;
950
1256
std::string transport;
951
1258
if (transportType_ == PJSIP_TRANSPORT_TLS) {
952
1259
scheme = "sips:";
953
1260
transport = ";transport=" + std::string(pjsip_transport_get_type_name(transportType));
955
1262
scheme = "sip:";
957
return displayName_ + (displayName_.empty() ? "" : " ") + "<" +
958
scheme + username_ + (username_.empty() ? "" : "@") +
959
address + ":" + port + transport + ">";
1264
contact_.slen = pj_ansi_snprintf(contact_.ptr, PJSIP_MAX_URL_SIZE,
1265
"%s%s<%s%s%s%s:%s%s>",
1266
displayName_.c_str(),
1267
(displayName_.empty() ? "" : " "),
1270
(username_.empty() ? "" : "@"),
1278
SIPAccount::getHostPortFromSTUN(pj_pool_t *pool)
1280
std::string addr, port;
1281
link_->sipTransport->findLocalAddressFromSTUN(transport_, &stunServerName_, stunPort_, addr, port);
1282
pjsip_host_port result;
1283
pj_strdup2(pool, &result.host, addr.c_str());
1284
result.host.slen = addr.length();
1285
std::stringstream ss;
963
1291
void SIPAccount::keepAliveRegistrationCb(UNUSED pj_timer_heap_t *th, pj_timer_entry *te)
1204
1526
return accountID_ == IP2IP_PROFILE;
1207
bool SIPAccount::matches(const std::string &userName, const std::string &server,
1208
pjsip_endpoint *endpt, pj_pool_t *pool) const
1530
SIPPresence * SIPAccount::getPresence() const
1536
* Enable the presence module
1539
SIPAccount::enablePresence(const bool& enabled)
1541
DEBUG("Presence enable for %s : %s.",
1543
enabled? Conf::TRUE_STR : Conf::FALSE_STR);
1546
presence_->enable(enabled);
1550
* Set the presence (PUBLISH/SUBSCRIBE) support flags
1551
* and process the change.
1554
SIPAccount::supportPresence(int function, bool enabled)
1556
if (getPresence()->isSupported(function) == enabled)
1559
DEBUG("Presence support for %s (%s: %s).", accountID_.c_str(),
1560
function == PRESENCE_FUNCTION_PUBLISH ? "publish" : "subscribe",
1561
enabled ? Conf::TRUE_STR : Conf::FALSE_STR);
1563
presence_->support(function, enabled);
1565
// force presence to disable when nothing is supported
1566
if (not getPresence()->isSupported(PRESENCE_FUNCTION_PUBLISH) and
1567
not getPresence()->isSupported(PRESENCE_FUNCTION_SUBSCRIBE))
1568
enablePresence(false);
1570
Manager::instance().saveConfig();
1571
Manager::instance().getClient()->getConfigurationManager()->accountsChanged();
1576
SIPAccount::matches(const std::string &userName, const std::string &server,
1577
pjsip_endpoint *endpt, pj_pool_t *pool) const
1210
1579
if (fullMatch(userName, server, endpt, pool)) {
1211
1580
DEBUG("Matching account id in request is a fullmatch %s@%s", userName.c_str(), server.c_str());
1581
return MatchRank::FULL;
1213
1582
} else if (hostnameMatch(server, endpt, pool)) {
1214
1583
DEBUG("Matching account id in request with hostname %s", server.c_str());
1584
return MatchRank::PARTIAL;
1216
1585
} else if (userMatch(userName)) {
1217
1586
DEBUG("Matching account id in request with username %s", userName.c_str());
1587
return MatchRank::PARTIAL;
1219
1588
} else if (proxyMatch(server, endpt, pool)) {
1220
1589
DEBUG("Matching account id in request with proxy %s", server.c_str());
1590
return MatchRank::PARTIAL;
1592
return MatchRank::NONE;
1596
// returns even number in range [lower, upper]
1598
SIPAccount::getRandomEvenNumber(const std::pair<uint16_t, uint16_t> &range)
1600
const uint16_t halfUpper = range.second * 0.5;
1601
const uint16_t halfLower = range.first * 0.5;
1604
result = 2 * (halfLower + rand() % (halfUpper - halfLower + 1));
1605
} while (portsInUse_[result / 2]);
1607
portsInUse_[result / 2] = true;
1612
SIPAccount::releasePort(uint16_t port)
1614
portsInUse_[port / 2] = false;
1618
SIPAccount::generateAudioPort() const
1620
return getRandomEvenNumber(audioPortRange_);
1625
SIPAccount::generateVideoPort() const
1627
return getRandomEvenNumber(videoPortRange_);
1632
SIPAccount::destroyRegistrationInfo()
1634
pjsip_regc_destroy(regc_);
1639
SIPAccount::resetAutoRegistration()
1641
auto_rereg_.active = PJ_FALSE;
1642
auto_rereg_.attempt_cnt = 0;
1645
/* Check if IP is private IP address */
1647
is_private_ip(const pj_str_t *addr)
1649
const pj_str_t private_net[] =
1651
{ (char*) "10.", 3 },
1652
{ (char*) "127.", 4 },
1653
{ (char*) "172.16.", 7 },
1654
{ (char*) "192.168.", 8 }
1657
for (unsigned i = 0; i < PJ_ARRAY_SIZE(private_net); ++i)
1658
if (pj_strncmp(addr, &private_net[i], private_net[i].slen) == 0)
1665
/* Update NAT address from the REGISTER response */
1667
SIPAccount::checkNATAddress(pjsip_regc_cbparam *param, pj_pool_t *pool)
1669
pjsip_transport *tp = param->rdata->tp_info.transport;
1671
/* Get the received and rport info */
1672
pjsip_via_hdr *via = param->rdata->msg_info.via;
1674
if (via->rport_param < 1) {
1675
/* Remote doesn't support rport */
1676
rport = via->sent_by.port;
1678
pjsip_transport_type_e tp_type;
1679
tp_type = (pjsip_transport_type_e) tp->key.type;
1680
rport = pjsip_transport_get_default_port_for_type(tp_type);
1683
rport = via->rport_param;
1686
const pj_str_t *via_addr = via->recvd_param.slen != 0 ?
1687
&via->recvd_param : &via->sent_by.host;
1689
/* If allowViaRewrite_ is enabled, we save the Via "received" address
1690
* from the response.
1692
if (allowViaRewrite_ and (via_addr_.host.slen == 0 or via_tp_ != tp)) {
1693
if (pj_strcmp(&via_addr_.host, via_addr))
1694
pj_strdup(pool, &via_addr_.host, via_addr);
1696
via_addr_.port = rport;
1698
pjsip_regc_set_via_sent_by(regc_, &via_addr_, via_tp_);
1701
/* Only update if account is configured to auto-update */
1702
if (not allowContactRewrite_)
1705
/* Compare received and rport with the URI in our registration */
1706
const pj_str_t STR_CONTACT = { (char*) "Contact", 7 };
1707
pjsip_contact_hdr *contact_hdr = (pjsip_contact_hdr*)
1708
pjsip_parse_hdr(pool, &STR_CONTACT, contact_.ptr, contact_.slen, NULL);
1709
pj_assert(contact_hdr != NULL);
1710
pjsip_sip_uri *uri = (pjsip_sip_uri*) contact_hdr->uri;
1711
pj_assert(uri != NULL);
1712
uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
1714
if (uri->port == 0) {
1715
pjsip_transport_type_e tp_type;
1716
tp_type = (pjsip_transport_type_e) tp->key.type;
1717
uri->port = pjsip_transport_get_default_port_for_type(tp_type);
1720
/* Convert IP address strings into sockaddr for comparison.
1721
* (http://trac.pjsip.org/repos/ticket/863)
1723
pj_sockaddr contact_addr, recv_addr;
1724
pj_status_t status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &uri->host, &contact_addr);
1725
if (status == PJ_SUCCESS)
1726
status = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, via_addr, &recv_addr);
1729
if (status == PJ_SUCCESS) {
1730
// Compare the addresses as sockaddr according to the ticket above
1731
matched = (uri->port == rport and pj_sockaddr_cmp(&contact_addr, &recv_addr) == 0);
1733
// Compare the addresses as string, as before
1734
matched = (uri->port == rport and pj_stricmp(&uri->host, via_addr) == 0);
1738
// Address doesn't change
1743
pj_str_t srv_ip = pj_str(param->rdata->pkt_info.src_name);
1745
/* At this point we've detected that the address as seen by registrar.
1749
/* Do not switch if both Contact and server's IP address are
1750
* public but response contains private IP. A NAT in the middle
1751
* might have messed up with the SIP packets. See:
1752
* http://trac.pjsip.org/repos/ticket/643
1754
* This exception can be disabled by setting allow_contact_rewrite
1755
* to 2. In this case, the switch will always be done whenever there
1756
* is difference in the IP address in the response.
1758
if (allowContactRewrite_ != 2 && not is_private_ip(&uri->host) and
1759
not is_private_ip(&srv_ip) and is_private_ip(via_addr)) {
1764
/* Also don't switch if only the port number part is different, and
1765
* the Via received address is private.
1766
* See http://trac.pjsip.org/repos/ticket/864
1768
if (allowContactRewrite_ != 2 and pj_sockaddr_cmp(&contact_addr, &recv_addr) == 0 and
1769
is_private_ip(via_addr)) {
1774
WARN("IP address change detected for account %s "
1775
"(%.*s:%d --> %.*s:%d). Updating registration "
1776
"(using method %d)",
1778
(int) uri->host.slen,
1781
(int) via_addr->slen,
1784
contactRewriteMethod_);
1786
pj_assert(contactRewriteMethod_ == 1 or contactRewriteMethod_ == 2);
1788
if (contactRewriteMethod_ == 1) {
1789
/* Unregister current contact */
1790
link_->sendUnregister(this);
1791
destroyRegistrationInfo();
1795
* Build new Contact header
1799
const char *beginquote, *endquote;
1800
char transport_param[32];
1803
/* Enclose IPv6 address in square brackets */
1804
if (tp->key.type & PJSIP_TRANSPORT_IPV6) {
1808
beginquote = endquote = "";
1811
/* Don't add transport parameter if it's UDP */
1812
if (tp->key.type != PJSIP_TRANSPORT_UDP and
1813
tp->key.type != PJSIP_TRANSPORT_UDP6) {
1814
pj_ansi_snprintf(transport_param, sizeof(transport_param),
1816
pjsip_transport_get_type_name(
1817
(pjsip_transport_type_e)tp->key.type));
1819
transport_param[0] = '\0';
1822
tmp = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
1823
len = pj_ansi_snprintf(tmp, PJSIP_MAX_URL_SIZE,
1824
"<sip:%s%s%s%.*s%s:%d%s>",
1826
(not username_.empty() ? "@" : ""),
1828
(int) via_addr->slen,
1834
ERROR("URI too long");
1838
pj_str_t tmp_str = {tmp, len};
1839
pj_strncpy_with_null(&contact_, &tmp_str, PJSIP_MAX_URL_SIZE);
1842
if (contactRewriteMethod_ == 2 && regc_ != NULL)
1843
pjsip_regc_update_contact(regc_, 1, &contact_);
1846
/* TODO: Perform new registration */
1847
//pjsua_acc_set_registration(acc->index, PJ_TRUE);
1848
/* Perform new registration */
1850
link_->sendRegister(this);
1851
} catch (const VoipLinkException &e) {
1852
ERROR("%s", e.what());
1859
/* Auto re-registration timeout callback */
1861
SIPAccount::autoReregTimerCb(pj_timer_heap_t * /*th*/, pj_timer_entry *te)
1863
std::pair<SIPAccount *, pjsip_endpoint *> *context = static_cast<std::pair<SIPAccount *, pjsip_endpoint *> *>(te->user_data);
1864
SIPAccount *acc = context->first;
1865
pjsip_endpoint *endpt = context->second;
1867
/* Check if the reregistration timer is still valid, e.g: while waiting
1868
* timeout timer application might have deleted the account or disabled
1869
* the auto-reregistration.
1871
if (not acc->auto_rereg_.active) {
1876
/* Start re-registration */
1877
acc->auto_rereg_.attempt_cnt++;
1879
acc->link_->sendRegister(acc);
1880
} catch (const VoipLinkException &e) {
1881
ERROR("%s", e.what());
1882
acc->scheduleReregistration(endpt);
1887
/* Schedule reregistration for specified account. Note that the first
1888
* re-registration after a registration failure will be done immediately.
1889
* Also note that this function should be called within PJSUA mutex.
1892
SIPAccount::scheduleReregistration(pjsip_endpoint *endpt)
1894
/* Cancel any re-registration timer */
1895
if (auto_rereg_.timer.id) {
1896
auto_rereg_.timer.id = PJ_FALSE;
1897
pjsip_endpt_cancel_timer(endpt, &auto_rereg_.timer);
1900
/* Update re-registration flag */
1901
auto_rereg_.active = PJ_TRUE;
1903
/* Set up timer for reregistration */
1904
auto_rereg_.timer.cb = &SIPAccount::autoReregTimerCb;
1905
auto_rereg_.timer.user_data = new std::pair<SIPAccount *, pjsip_endpoint *>(this, endpt);
1907
/* Reregistration attempt. The first attempt will be done immediately. */
1909
const int FIRST_RETRY_INTERVAL = 60;
1910
const int RETRY_INTERVAL = 300;
1911
delay.sec = auto_rereg_.attempt_cnt ? RETRY_INTERVAL : FIRST_RETRY_INTERVAL;
1914
/* Randomize interval by +/- 10 secs */
1915
if (delay.sec >= 10) {
1916
delay.msec = -10000 + (pj_rand() % 20000);
1919
delay.msec = (pj_rand() % 10000);
1922
pj_time_val_normalize(&delay);
1924
WARN("Scheduling re-registration retry in %u seconds..", delay.sec);
1925
auto_rereg_.timer.id = PJ_TRUE;
1926
if (pjsip_endpt_schedule_timer(endpt, &auto_rereg_.timer, &delay) != PJ_SUCCESS)
1927
auto_rereg_.timer.id = PJ_FALSE;
1930
void SIPAccount::updateDialogViaSentBy(pjsip_dialog *dlg)
1932
if (allowViaRewrite_ && via_addr_.host.slen > 0)
1933
pjsip_dlg_set_via_sent_by(dlg, &via_addr_, via_tp_);