414
424
struct sockaddr_in *saddr_in;
415
425
struct in_addr *addr_in;
417
if((fd = socket (AF_INET, SOCK_DGRAM,0)) < 0)
418
_error("UserAgent: Error: could not open socket");
427
if ( (fd = socket (AF_INET, SOCK_DGRAM,0)) < 0)
428
_error ("UserAgent: Error: could not open socket");
420
430
memset (&ifr, 0, sizeof (struct ifreq));
422
432
strcpy (ifr.ifr_name, ifaceName.c_str());
423
433
ifr.ifr_addr.sa_family = AF_INET;
425
if((err = ioctl(fd, SIOCGIFADDR, &ifr)) < 0)
426
_debug("UserAgent: Use default interface (0.0.0.0)");
428
saddr_in = (struct sockaddr_in *)&ifr.ifr_addr;
429
addr_in = &(saddr_in->sin_addr);
431
std::string addr(pj_inet_ntoa(*((pj_in_addr*)addr_in)));
435
if ( (err = ioctl (fd, SIOCGIFADDR, &ifr)) < 0)
436
_debug ("UserAgent: Use default interface (0.0.0.0)");
438
saddr_in = (struct sockaddr_in *) &ifr.ifr_addr;
439
addr_in = & (saddr_in->sin_addr);
441
std::string addr (pj_inet_ntoa (* ( (pj_in_addr*) addr_in)));
533
// Create SIP transport or get existent SIP transport from internal map
547
// Create SIP transport or get existent SIP transport from internal map
534
548
// according to account settings, if the transport could not be created but
535
// one is already set in account, use this one (most likely this is the
536
// transport we tried to create)
537
acquireTransport(account->getAccountID());
549
// one is already set in account, use this one (most likely this is the
550
// transport we tried to create)
551
acquireTransport (account->getAccountID());
539
553
if (account->getAccountTransport()) {
540
_debug("Acquire transport in account registration: %s %s (refcnt=%d)",
541
account->getAccountTransport()->obj_name,
542
account->getAccountTransport()->info,
543
(int)pj_atomic_get(account->getAccountTransport()->ref_cnt));
554
_debug ("Acquire transport in account registration: %s %s (refcnt=%d)",
555
account->getAccountTransport()->obj_name,
556
account->getAccountTransport()->info,
557
(int) pj_atomic_get (account->getAccountTransport()->ref_cnt));
546
560
_mutexSIP.enterMutex();
600
614
// Initializes registration
602
616
// Set Route for registration passing throught one or several proxies
617
// status = pjsip_regc_init (regc, &pjSrv, &pjFrom, &pjFrom, 1, &pjContact, expire_value);
603
619
status = pjsip_regc_init (regc, &pjSrv, &pjFrom, &pjFrom, 1, &pjContact, expire_value);
606
if(!(account->getDomainName().empty())) {
608
_error("Set route with %s", account->getHostname().c_str());
610
pjsip_route_hdr *route_set = pjsip_route_hdr_create(_pool);
611
pjsip_route_hdr *routing = pjsip_route_hdr_create(_pool);
612
pjsip_sip_uri *url = pjsip_sip_uri_create(_pool, 0);
613
routing->name_addr.uri = (pjsip_uri*)url;
614
pj_strdup2(_pool, &url->host, account->getHostname().c_str());
616
pj_list_push_back(&route_set, pjsip_hdr_clone(_pool, routing));
618
status = pjsip_regc_init (regc, &pjSrv, &pjFrom, &pjFrom, 1, &pjContact, expire_value);
620
pjsip_regc_set_route_set(regc, route_set);
624
status = pjsip_regc_init (regc, &pjSrv, &pjFrom, &pjFrom, 1, &pjContact, expire_value);
621
if (! (account->getServiceRoute().empty())) {
623
_error ("UserAgent: Set Service-Route with %s", account->getServiceRoute().c_str());
625
pjsip_route_hdr *route_set = pjsip_route_hdr_create (_pool);
626
pjsip_route_hdr *routing = pjsip_route_hdr_create (_pool);
627
pjsip_sip_uri *url = pjsip_sip_uri_create (_pool, 0);
628
routing->name_addr.uri = (pjsip_uri*) url;
629
pj_strdup2 (_pool, &url->host, account->getServiceRoute().c_str());
631
pj_list_push_back (&route_set, pjsip_hdr_clone (_pool, routing));
633
pjsip_regc_set_route_set (regc, route_set);
629
636
if (status != PJ_SUCCESS) {
630
637
_debug ("UserAgent: Unable to initialize account %d in sendRegister", status);
661
668
pjsip_tpselector *tp;
663
init_transport_selector (account->getAccountTransport (), &tp);
670
init_transport_selector (account->getAccountTransport (), &tp);
665
672
// pjsip_regc_set_transport increments transport ref count by one
666
673
status = pjsip_regc_set_transport (regc, tp);
668
if(account->getAccountTransport()) {
669
// decrease transport's ref count, counter icrementation is
670
// managed when acquiring transport
671
pjsip_transport_dec_ref(account->getAccountTransport ());
675
if (account->getAccountTransport()) {
676
// decrease transport's ref count, counter icrementation is
677
// managed when acquiring transport
678
pjsip_transport_dec_ref (account->getAccountTransport ());
673
_debug("After setting the transport in account registration using transport: %s %s (refcnt=%d)",
674
account->getAccountTransport()->obj_name,
675
account->getAccountTransport()->info,
676
(int)pj_atomic_get(account->getAccountTransport()->ref_cnt));
680
_debug ("UserAgent: After setting the transport in account registration using transport: %s %s (refcnt=%d)",
681
account->getAccountTransport()->obj_name,
682
account->getAccountTransport()->info,
683
(int) pj_atomic_get (account->getAccountTransport()->ref_cnt));
679
686
if (status != PJ_SUCCESS) {
1061
SIPVoIPLink::sendTextMessage (sfl::InstantMessaging *module, const std::string& callID, const std::string& message, const std::string& from)
1063
_debug ("SipVoipLink: Send text message to %s, from %s", callID.c_str(), from.c_str());
1065
SIPCall *call = getSIPCall (callID);
1066
pj_status_t status = !PJ_SUCCESS;
1070
std::string formatedFrom = from;
1072
// add double quotes for xml formating
1073
formatedFrom.insert (0,"\"");
1074
formatedFrom.append ("\"");
1076
/* Send IM message */
1077
sfl::InstantMessaging::UriList list;
1079
sfl::InstantMessaging::UriEntry entry;
1080
entry[sfl::IM_XML_URI] = std::string (formatedFrom);
1082
list.push_front (entry);
1084
std::string formatedMessage = module->appendUriList (message, list);
1086
status = module->send_sip_message (call->getInvSession (), (CallID&) callID, formatedMessage);
1089
/* Notify the client of an error */
1090
/*Manager::instance ().incomingMessage ( "",
1092
"Unable to send a message outside a call.");*/
1051
1098
int SIPVoIPLink::inv_session_reinvite (SIPCall *call, std::string direction)
1297
AccountID accountID = Manager::instance().getAccountFromCall(id);
1298
SIPAccount *account = static_cast<SIPAccount *>(Manager::instance().getAccount(accountID));
1346
AccountID accountID = Manager::instance().getAccountFromCall (id);
1347
SIPAccount *account = static_cast<SIPAccount *> (Manager::instance().getAccount (accountID));
1301
_error ("UserAgent: Error: Account not found while sending DTMF");
1350
_error ("UserAgent: Error: Account not found while sending DTMF");
1305
1354
DtmfType type = account->getDtmfType();
1308
dtmfOverRtp(call, code);
1309
else if(type == SIPINFO)
1310
dtmfSipInfo(call, code);
1356
if (type == OVERRTP)
1357
dtmfOverRtp (call, code);
1358
else if (type == SIPINFO)
1359
dtmfSipInfo (call, code);
1312
_error("UserAgent: Error: Dtmf type does not exist");
1321
SIPVoIPLink::dtmfSipInfo(SIPCall *call, char code)
1325
const int body_len = 1000;
1328
pjsip_tx_data *tdata;
1329
pj_str_t methodName, content;
1330
pjsip_method method;
1331
pjsip_media_type ctype;
1334
duration = Manager::instance().getConfigInt (SIGNALISATION, PULSE_LENGTH);
1336
dtmf_body = new char[body_len];
1338
snprintf (dtmf_body, body_len - 1, "Signal=%c\r\nDuration=%d\r\n", code, duration);
1340
pj_strdup2 (_pool, &methodName, "INFO");
1341
pjsip_method_init_np (&method, &methodName);
1343
/* Create request message. */
1344
status = pjsip_dlg_create_request (call->getInvSession()->dlg, &method, -1, &tdata);
1346
if (status != PJ_SUCCESS) {
1347
_debug ("UserAgent: Unable to create INFO request -- %d", status);
1352
pj_strdup2 (_pool, &ctype.type, "application");
1354
pj_strdup2 (_pool, &ctype.subtype, "dtmf-relay");
1356
/* Create "application/dtmf-relay" message body. */
1357
pj_strdup2 (_pool, &content, dtmf_body);
1359
tdata->msg->body = pjsip_msg_body_create (tdata->pool, &ctype.type, &ctype.subtype, &content);
1361
if (tdata->msg->body == NULL) {
1362
_debug ("UserAgent: Unable to create msg body!");
1363
pjsip_tx_data_dec_ref (tdata);
1367
/* Send the request. */
1368
status = pjsip_dlg_send_request (call->getInvSession()->dlg, tdata, getModId(), NULL);
1370
if (status != PJ_SUCCESS) {
1371
_debug ("UserAgent: Unable to send MESSAGE request -- %d", status);
1379
SIPVoIPLink::dtmfOverRtp(SIPCall* call, char code)
1381
call->getAudioRtp()->sendDtmfDigit(atoi(&code));
1361
_error ("UserAgent: Error: Dtmf type does not exist");
1370
SIPVoIPLink::dtmfSipInfo (SIPCall *call, char code)
1374
const int body_len = 1000;
1377
pjsip_tx_data *tdata;
1378
pj_str_t methodName, content;
1379
pjsip_method method;
1380
pjsip_media_type ctype;
1383
duration = Manager::instance().voipPreferences.getPulseLength();
1385
dtmf_body = new char[body_len];
1387
snprintf (dtmf_body, body_len - 1, "Signal=%c\r\nDuration=%d\r\n", code, duration);
1389
pj_strdup2 (_pool, &methodName, "INFO");
1390
pjsip_method_init_np (&method, &methodName);
1392
/* Create request message. */
1393
status = pjsip_dlg_create_request (call->getInvSession()->dlg, &method, -1, &tdata);
1395
if (status != PJ_SUCCESS) {
1396
_debug ("UserAgent: Unable to create INFO request -- %d", status);
1401
pj_strdup2 (_pool, &ctype.type, "application");
1403
pj_strdup2 (_pool, &ctype.subtype, "dtmf-relay");
1405
/* Create "application/dtmf-relay" message body. */
1406
pj_strdup2 (_pool, &content, dtmf_body);
1408
tdata->msg->body = pjsip_msg_body_create (tdata->pool, &ctype.type, &ctype.subtype, &content);
1410
if (tdata->msg->body == NULL) {
1411
_debug ("UserAgent: Unable to create msg body!");
1412
pjsip_tx_data_dec_ref (tdata);
1416
/* Send the request. */
1417
status = pjsip_dlg_send_request (call->getInvSession()->dlg, tdata, getModId(), NULL);
1419
if (status != PJ_SUCCESS) {
1420
_debug ("UserAgent: Unable to send MESSAGE request -- %d", status);
1428
SIPVoIPLink::dtmfOverRtp (SIPCall* call, char code)
1430
call->getAudioRtp()->sendDtmfDigit (atoi (&code));
1663
1730
_debug ("UserAgent: TO uri for IP2IP call: %s", toUri.c_str());
1665
1732
// Building the local SDP offer
1666
call->getLocalSDP()->set_ip_address (addrSdp);
1667
call->getLocalSDP()->create_initial_offer (account->getActiveCodecs ());
1733
// call->getLocalSDP()->set_ip_address (addrSdp);
1734
// call->getLocalSDP()->create_initial_offer (account->getActiveCodecs ());
1670
1736
// Audio Rtp Session must be initialized before creating initial offer in SDP session
1671
1737
// since SDES require crypto attribute.
1673
call->getAudioRtp()->initAudioRtpConfig (call);
1674
call->getAudioRtp()->initAudioRtpSession (call);
1739
call->getAudioRtp()->initAudioRtpConfig (call);
1740
call->getAudioRtp()->initAudioRtpSession (call);
1741
call->getAudioRtp()->initLocalCryptoInfo (call);
1675
1742
} catch (...) {
1676
_debug ("UserAgent: Unable to create RTP Session in new IP2IP call (%s:%d)", __FILE__, __LINE__);
1743
_debug ("UserAgent: Unable to create RTP Session in new IP2IP call (%s:%d)", __FILE__, __LINE__);
1746
// Building the local SDP offer
1747
call->getLocalSDP()->set_ip_address (addrSdp);
1748
call->getLocalSDP()->create_initial_offer (account->getActiveCodecs ());
1679
1750
// Init TLS transport if enabled
1680
if(account->isTlsEnabled()) {
1682
_debug("UserAgent: TLS enabled for IP2IP calls");
1683
int at = toUri.find("@");
1684
int trns = toUri.find(";transport");
1685
std::string remoteAddr = toUri.substr(at+1, trns-at-1);
1687
if(toUri.find("sips:") != 1) {
1688
_debug("UserAgent: Error \"sips\" scheme required for TLS call");
1692
if(createTlsTransport(account->getAccountID(), remoteAddr) != PJ_SUCCESS)
1751
if (account->isTlsEnabled()) {
1753
_debug ("UserAgent: TLS enabled for IP2IP calls");
1754
int at = toUri.find ("@");
1755
int trns = toUri.find (";transport");
1756
std::string remoteAddr = toUri.substr (at+1, trns-at-1);
1758
if (toUri.find ("sips:") != 1) {
1759
_debug ("UserAgent: Error \"sips\" scheme required for TLS call");
1763
if (createTlsTransport (account->getAccountID(), remoteAddr) != PJ_SUCCESS)
1696
1767
// If no transport already set, use the default one created at pjsip initialization
1697
1768
if (account->getAccountTransport() == NULL) {
1698
_debug ("UserAgent: No transport for this account, using the default one");
1699
account->setAccountTransport (_localUDPTransport);
1769
_debug ("UserAgent: No transport for this account, using the default one");
1770
account->setAccountTransport (_localUDPTransport);
1702
1773
_debug ("UserAgent: Local port %i for IP2IP call", account->getLocalPort());
1710
1781
int port = findLocalPortFromUri (toUri, account->getAccountTransport());
1712
1783
std::stringstream ss;
1713
std::string portStr;
1717
std::string contactUri = account->getContactHeader (address, portStr);
1719
_debug ("UserAgent: FROM uri: %s, TO uri: %s, CONTACT uri: %s",
1720
fromUri.c_str(), toUri.c_str(), contactUri.c_str());
1723
pj_cstr (&pjFrom, fromUri.c_str());
1726
pj_cstr (&pjTo, toUri.c_str());
1729
pj_cstr (&pjContact, contactUri.c_str());
1731
// Create the dialog (UAC)
1732
// (Parameters are "strduped" inside this function)
1733
status = pjsip_dlg_create_uac (pjsip_ua_instance(), &pjFrom, &pjContact, &pjTo, NULL, &dialog);
1734
PJ_ASSERT_RETURN (status == PJ_SUCCESS, false);
1736
// Create the invite session for this call
1737
status = pjsip_inv_create_uac (dialog, call->getLocalSDP()->get_local_sdp_session(), 0, &inv);
1738
PJ_ASSERT_RETURN (status == PJ_SUCCESS, false);
1740
// Set the appropriate transport
1741
pjsip_tpselector *tp;
1743
init_transport_selector (account->getAccountTransport(), &tp);
1745
if(!account->getAccountTransport()) {
1746
_error("UserAgent: Error: Transport is NULL in IP2IP call");
1749
// set_transport methods increment transport's ref_count
1750
status = pjsip_dlg_set_transport (dialog, tp);
1752
// decrement transport's ref count
1753
// pjsip_transport_dec_ref(account->getAccountTransport());
1755
if (status != PJ_SUCCESS) {
1756
_error ("UserAgent: Error: Failed to set the transport for an IP2IP call");
1760
// Associate current call in the invite session
1761
inv->mod_data[getModId() ] = call;
1763
status = pjsip_inv_invite (inv, &tdata);
1765
PJ_ASSERT_RETURN (status == PJ_SUCCESS, false);
1767
// Associate current invite session in the call
1768
call->setInvSession (inv);
1770
status = pjsip_inv_send_msg (inv, tdata);
1772
if (status != PJ_SUCCESS) {
1778
call->setConnectionState (Call::Progressing);
1780
call->setState (Call::Active);
1784
std::string portStr;
1788
std::string contactUri = account->getContactHeader (address, portStr);
1790
_debug ("UserAgent: FROM uri: %s, TO uri: %s, CONTACT uri: %s",
1791
fromUri.c_str(), toUri.c_str(), contactUri.c_str());
1794
pj_cstr (&pjFrom, fromUri.c_str());
1797
pj_cstr (&pjTo, toUri.c_str());
1800
pj_cstr (&pjContact, contactUri.c_str());
1802
// Create the dialog (UAC)
1803
// (Parameters are "strduped" inside this function)
1804
_debug ("UserAgent: Creating dialog for this call");
1805
status = pjsip_dlg_create_uac (pjsip_ua_instance(), &pjFrom, &pjContact, &pjTo, NULL, &dialog);
1806
PJ_ASSERT_RETURN (status == PJ_SUCCESS, false);
1808
// Create the invite session for this call
1809
_debug ("UserAgent: Creating invite session for this call");
1810
status = pjsip_inv_create_uac (dialog, call->getLocalSDP()->get_local_sdp_session(), 0, &inv);
1811
PJ_ASSERT_RETURN (status == PJ_SUCCESS, false);
1813
if (! (account->getServiceRoute().empty())) {
1815
_error ("UserAgent: Set Service-Route with %s", account->getServiceRoute().c_str());
1817
pjsip_route_hdr *route_set = pjsip_route_hdr_create (_pool);
1818
pjsip_route_hdr *routing = pjsip_route_hdr_create (_pool);
1819
pjsip_sip_uri *url = pjsip_sip_uri_create (_pool, 0);
1820
routing->name_addr.uri = (pjsip_uri*) url;
1821
pj_strdup2 (_pool, &url->host, account->getServiceRoute().c_str());
1823
pj_list_push_back (&route_set, pjsip_hdr_clone (_pool, routing));
1825
pjsip_dlg_set_route_set (dialog, route_set);
1828
// Set the appropriate transport
1829
pjsip_tpselector *tp;
1831
init_transport_selector (account->getAccountTransport(), &tp);
1833
if (!account->getAccountTransport()) {
1834
_error ("UserAgent: Error: Transport is NULL in IP2IP call");
1837
// set_transport methods increment transport's ref_count
1838
status = pjsip_dlg_set_transport (dialog, tp);
1840
// decrement transport's ref count
1841
// pjsip_transport_dec_ref(account->getAccountTransport());
1843
if (status != PJ_SUCCESS) {
1844
_error ("UserAgent: Error: Failed to set the transport for an IP2IP call");
1848
// Associate current call in the invite session
1849
inv->mod_data[getModId() ] = call;
1851
status = pjsip_inv_invite (inv, &tdata);
1853
PJ_ASSERT_RETURN (status == PJ_SUCCESS, false);
1855
// Associate current invite session in the call
1856
call->setInvSession (inv);
1858
status = pjsip_inv_send_msg (inv, tdata);
1860
if (status != PJ_SUCCESS) {
1866
call->setConnectionState (Call::Progressing);
1868
call->setState (Call::Active);
1998
2090
_debug ("UserAgent: VOIP callbacks initialized");
2000
2092
// Add endpoint capabilities (INFO, OPTIONS, etc) for this UA
2001
pj_str_t allowed[] = { { (char*) "INFO", 4}, { (char*) "REGISTER", 8}, { (char*) "OPTIONS", 7} }; // //{"INVITE", 6}, {"ACK",3}, {"BYE",3}, {"CANCEL",6}
2093
pj_str_t allowed[] = { { (char*) "INFO", 4}, { (char*) "REGISTER", 8}, { (char*) "OPTIONS", 7}, { (char*) "MESSAGE", 7 } }; // //{"INVITE", 6}, {"ACK",3}, {"BYE",3}, {"CANCEL",6}
2003
2095
accepted = pj_str ( (char*) "application/sdp");
2005
2097
// Register supported methods
2006
2098
pjsip_endpt_add_capability (_endpt, &_mod_ua, PJSIP_H_ALLOW, NULL, PJ_ARRAY_SIZE (allowed), allowed);
2100
const pj_str_t STR_MIME_TEXT_PLAIN = { (char*) "text/plain", 10 };
2101
pjsip_endpt_add_capability (_endpt, &_mod_ua, PJSIP_H_ACCEPT, NULL, 1, &STR_MIME_TEXT_PLAIN);
2008
2103
// Register "application/sdp" in ACCEPT header
2009
2104
pjsip_endpt_add_capability (_endpt, &_mod_ua, PJSIP_H_ACCEPT, NULL, 1, &accepted);
2079
bool SIPVoIPLink::acquireTransport(const AccountID& accountID) {
2174
bool SIPVoIPLink::acquireTransport (const AccountID& accountID)
2081
2177
SIPAccount* account = dynamic_cast<SIPAccount *> (Manager::instance().getAccount (accountID));
2087
// If an account is already bound to this account, decrease its reference
2088
// as it is going to change. If the same transport is selected, reference
2183
// If an account is already bound to this account, decrease its reference
2184
// as it is going to change. If the same transport is selected, reference
2089
2185
// counter will be increased
2090
if(account->getAccountTransport()) {
2186
if (account->getAccountTransport()) {
2092
_debug("pjsip_transport_dec_ref in acquireTransport");
2093
pjsip_transport_dec_ref(account->getAccountTransport());
2188
_debug ("pjsip_transport_dec_ref in acquireTransport");
2189
pjsip_transport_dec_ref (account->getAccountTransport());
2096
2192
// Try to create a new transport in case the settings for this account
2097
2193
// are different than one defined for already created ones
2098
2194
// If TLS is enabled, TLS connection is automatically handled when sending account registration
2099
// However, for any other sip transaction, we must create TLS connection
2100
if(createSipTransport(accountID)) {
2195
// However, for any other sip transaction, we must create TLS connection
2196
if (createSipTransport (accountID)) {
2103
2199
// A transport is already created on this port, use it
2106
_debug("Could not create a new transport (%s)", account->getTransportMapKey().c_str());
2107
_debug("Searching transport (%s) in transport map", account->getTransportMapKey().c_str());
2202
_debug ("Could not create a new transport (%s)", account->getTransportMapKey().c_str());
2203
_debug ("Searching transport (%s) in transport map", account->getTransportMapKey().c_str());
2109
2205
// Could not create new transport, this transport may already exists
2110
2206
SipTransportMap::iterator transport;
2111
transport = _transportMap.find(account->getTransportMapKey());
2113
if(transport != _transportMap.end()) {
2115
// Transport already exist, use it for this account
2116
_debug("Found transport (%s) in transport map", account->getTransportMapKey().c_str());
2118
pjsip_transport* tr = transport->second;
2120
// Set transport to be used for transaction involving this account
2121
account->setAccountTransport(tr);
2123
// Increment newly associated transport reference counter
2124
// If the account is shutdowning, time is automatically canceled
2125
pjsip_transport_add_ref(tr);
2131
// Transport could not either be created, socket not available
2132
_debug("Did not find transport (%s) in transport map", account->getTransportMapKey().c_str());
2134
account->setAccountTransport(_localUDPTransport);
2136
std::string localHostName(_localUDPTransport->local_name.host.ptr, _localUDPTransport->local_name.host.slen);
2138
_debug("Use default one instead (%s:%i)", localHostName.c_str(), _localUDPTransport->local_name.port);
2140
// account->setLocalAddress(localHostName);
2141
account->setLocalPort(_localUDPTransport->local_name.port);
2143
// Transport could not either be created or found in the map, socket not available
2207
transport = _transportMap.find (account->getTransportMapKey());
2209
if (transport != _transportMap.end()) {
2211
// Transport already exist, use it for this account
2212
_debug ("Found transport (%s) in transport map", account->getTransportMapKey().c_str());
2214
pjsip_transport* tr = transport->second;
2216
// Set transport to be used for transaction involving this account
2217
account->setAccountTransport (tr);
2219
// Increment newly associated transport reference counter
2220
// If the account is shutdowning, time is automatically canceled
2221
pjsip_transport_add_ref (tr);
2226
// Transport could not either be created, socket not available
2227
_debug ("Did not find transport (%s) in transport map", account->getTransportMapKey().c_str());
2229
account->setAccountTransport (_localUDPTransport);
2231
std::string localHostName (_localUDPTransport->local_name.host.ptr, _localUDPTransport->local_name.host.slen);
2233
_debug ("Use default one instead (%s:%i)", localHostName.c_str(), _localUDPTransport->local_name.port);
2235
// account->setLocalAddress(localHostName);
2236
account->setLocalPort (_localUDPTransport->local_name.port);
2238
// Transport could not either be created or found in the map, socket not available
2249
2344
/* Get TLS settings. Expected to be filled */
2250
2345
pjsip_tls_setting * tls_setting = account->getTlsSetting();
2253
2348
_debug ("UserAgent: TLS transport to be initialized with published address %.*s,"
2254
" published port %d,\n local address %.*s, local port %d",
2255
(int) a_name.host.slen, a_name.host.ptr,
2256
(int) a_name.port, pjAddress.slen, pjAddress.ptr, (int) localTlsPort);
2349
" published port %d,\n local address %.*s, local port %d",
2350
(int) a_name.host.slen, a_name.host.ptr,
2351
(int) a_name.port, pjAddress.slen, pjAddress.ptr, (int) localTlsPort);
2259
2354
status = pjsip_tls_transport_start (_endpt, tls_setting, &local_addr, &a_name, 1, &tls);
2261
2356
if (status != PJ_SUCCESS) {
2262
2357
_debug ("UserAgent: Error creating SIP TLS listener (%d)", status);
2265
2359
_localTlsListener = tls;
2268
2362
// return PJ_SUCCESS;
2273
bool SIPVoIPLink::createSipTransport(AccountID id)
2367
bool SIPVoIPLink::createSipTransport (AccountID id)
2276
2370
SIPAccount* account = dynamic_cast<SIPAccount *> (Manager::instance().getAccount (id));
2283
2377
if (account->isTlsEnabled()) {
2285
if(_localTlsListener == NULL)
2286
createTlsListener(id);
2379
if (_localTlsListener == NULL)
2380
createTlsListener (id);
2288
// Parse remote address to establish connection
2382
// Parse remote address to establish connection
2289
2383
std::string remoteSipUri = account->getServerUri();
2290
int sips = remoteSipUri.find("<sips:") + 6;
2291
int trns = remoteSipUri.find(";transport");
2292
std::string remoteAddr = remoteSipUri.substr(sips, trns-sips);
2384
int sips = remoteSipUri.find ("<sips:") + 6;
2385
int trns = remoteSipUri.find (";transport");
2386
std::string remoteAddr = remoteSipUri.substr (sips, trns-sips);
2294
2388
// Nothing to do, TLS listener already created at pjsip's startup and TLS connection
2295
2389
// is automatically handled in pjsip when sending registration messages.
2296
if(createTlsTransport(id, remoteAddr) != PJ_SUCCESS)
2390
if (createTlsTransport (id, remoteAddr) != PJ_SUCCESS)
2303
2396
// Launch a new UDP listener/transport, using the published address
2304
2397
if (account->isStunEnabled ()) {
2308
2401
if (status != PJ_SUCCESS) {
2309
2402
_debug ("Failed to init UDP transport with STUN published address for account %s", id.c_str());
2315
2408
status = createUdpTransport (id);
2317
if (status != PJ_SUCCESS) {
2410
if (status != PJ_SUCCESS) {
2318
2411
_debug ("Failed to initialize UDP transport for account %s", id.c_str());
2323
// If transport succesfully created, store it in the internal map.
2324
// STUN aware transport are account specific and should not be stored in map.
2325
// TLS transport is ephemeral and is managed by PJSIP, should not be stored either.
2326
addTransportToMap(account->getTransportMapKey(), account->getAccountTransport());
2415
// If transport successfully created, store it in the internal map.
2416
// STUN aware transport are account specific and should not be stored in map.
2417
// TLS transport is ephemeral and is managed by PJSIP, should not be stored either.
2418
addTransportToMap (account->getTransportMapKey(), account->getAccountTransport());
2336
bool SIPVoIPLink::addTransportToMap(std::string key, pjsip_transport* transport)
2428
bool SIPVoIPLink::addTransportToMap (std::string key, pjsip_transport* transport)
2339
2431
SipTransportMap::iterator iter_transport;
2340
iter_transport = _transportMap.find(key);
2432
iter_transport = _transportMap.find (key);
2342
2434
// old transport in transport map, erase it
2343
if(iter_transport != _transportMap.end()){
2344
_transportMap.erase(iter_transport);
2435
if (iter_transport != _transportMap.end()) {
2436
_transportMap.erase (iter_transport);
2347
_debug("UserAgent: Storing newly created transport in map using key %s", key.c_str());
2348
_transportMap.insert(pair<std::string, pjsip_transport*>(key, transport));
2439
_debug ("UserAgent: Storing newly created transport in map using key %s", key.c_str());
2440
_transportMap.insert (pair<std::string, pjsip_transport*> (key, transport));
2420
2512
// Create UDP-Server (default port: 5060)
2421
2513
// Use here either the local information or the published address
2422
if (account != NULL && !account->getPublishedSameasLocal ()) {
2514
if (account && !account->getPublishedSameasLocal ()) {
2424
2516
// Set the listening address to the published address
2425
2517
listeningAddress = account->getPublishedAddress ();
2426
2519
// Set the listening port to the published port
2427
2520
listeningPort = account->getPublishedPort ();
2428
2521
_debug ("UserAgent: Creating UDP transport published %s:%i", listeningAddress.c_str (), listeningPort);
2432
if(listeningAddress == "" || listeningPort == 0) {
2433
_error("UserAgent: Error invalid address for new udp transport");
2525
// We must specify this here to avoid the IP2IP_PROFILE
2526
// to create a transport with name 0.0.0.0 to appear in the via header
2527
if (id == IP2IP_PROFILE)
2528
loadSIPLocalIP (&listeningAddress);
2530
if (listeningAddress == "" || listeningPort == 0) {
2531
_error ("UserAgent: Error invalid address for new udp transport");
2436
2535
//strcpy (tmpIP, listeningAddress.data());
2437
2536
/* Init published name */
2438
2537
pj_bzero (&a_name, sizeof (pjsip_host_port));
2662
pj_status_t SIPVoIPLink::createTlsTransport(const AccountID& accountID, std::string remoteAddr)
2763
pj_status_t SIPVoIPLink::createTlsTransport (const AccountID& accountID, std::string remoteAddr)
2664
2765
pj_status_t success;
2666
_debug("Create TLS transport for account %s\n", accountID.c_str());
2767
_debug ("Create TLS transport for account %s\n", accountID.c_str());
2668
2769
// Retrieve the account information
2669
2770
SIPAccount * account = dynamic_cast<SIPAccount *> (Manager::instance().getAccount (accountID));
2672
_debug("UserAgent: Account is NULL when creating TLS connection, returning");
2773
_debug ("UserAgent: Account is NULL when creating TLS connection, returning");
2676
2777
pj_sockaddr_in rem_addr;
2677
2778
pj_str_t remote;
2679
pj_cstr(&remote, remoteAddr.c_str());
2780
pj_cstr (&remote, remoteAddr.c_str());
2681
pj_sockaddr_in_init(&rem_addr, &remote, (pj_uint16_t)5061);
2782
pj_sockaddr_in_init (&rem_addr, &remote, (pj_uint16_t) 5061);
2683
2784
// Update TLS settings for account registration using the default listeners
2684
2785
// Pjsip does not allow to create multiple listener
2788
2889
account->setAccountTransport (transport);
2792
_debug("UserAgent: Initial ref count: %s %s (refcnt=%i)", transport->obj_name, transport->info,
2793
(int)pj_atomic_get(transport->ref_cnt));
2795
pj_sockaddr *addr = (pj_sockaddr*)&(transport->key.rem_addr);
2797
static char str[PJ_INET6_ADDRSTRLEN];
2798
pj_inet_ntop(((const pj_sockaddr*)addr)->addr.sa_family, pj_sockaddr_get_addr(addr), str, sizeof(str));
2801
_debug("UserAgent: KEY: %s:%d",str, pj_sockaddr_get_port((const pj_sockaddr*)&(transport->key.rem_addr)));
2893
_debug ("UserAgent: Initial ref count: %s %s (refcnt=%i)", transport->obj_name, transport->info,
2894
(int) pj_atomic_get (transport->ref_cnt));
2896
pj_sockaddr *addr = (pj_sockaddr*) & (transport->key.rem_addr);
2898
static char str[PJ_INET6_ADDRSTRLEN];
2899
pj_inet_ntop ( ( (const pj_sockaddr*) addr)->addr.sa_family, pj_sockaddr_get_addr (addr), str, sizeof (str));
2902
_debug ("UserAgent: KEY: %s:%d",str, pj_sockaddr_get_port ( (const pj_sockaddr*) & (transport->key.rem_addr)));
2813
void SIPVoIPLink::shutdownSipTransport(const AccountID& accountID)
2914
void SIPVoIPLink::shutdownSipTransport (const AccountID& accountID)
2816
_debug("UserAgent: Shutdown Sip Transport");
2917
_debug ("UserAgent: Shutdown Sip Transport");
2818
2919
SIPAccount* account = dynamic_cast<SIPAccount *> (Manager::instance().getAccount (accountID));
2823
if(account->getAccountTransport()) {
2825
_debug("Transport bound to account, decrease ref count");
2827
// decrease reference count added by pjsip_regc_send
2828
// PJSIP's IDLE timer is set if counter reach 0
2924
if (account->getAccountTransport()) {
2926
_debug ("Transport bound to account, decrease ref count");
2928
// decrease reference count added by pjsip_regc_send
2929
// PJSIP's IDLE timer is set if counter reach 0
2830
2931
// there is still problems when account registration fails, so comment it for now
2831
// status = pjsip_transport_dec_ref(account->getAccountTransport());
2932
// status = pjsip_transport_dec_ref(account->getAccountTransport());
2833
// detach transport from this account
2834
account->setAccountTransport(NULL);
2934
// detach transport from this account
2935
account->setAccountTransport (NULL);
3231
3335
call->getAudioRtp()->updateDestinationIpAddress();
3238
3341
// Get the crypto attribute containing srtp's cryptographic context (keys, cipher)
3239
3342
CryptoOffer crypto_offer;
3240
call->getLocalSDP()->get_remote_sdp_crypto_from_offer(remote_sdp, crypto_offer);
3343
call->getLocalSDP()->get_remote_sdp_crypto_from_offer (remote_sdp, crypto_offer);
3242
3345
bool nego_success = false;
3243
if(!crypto_offer.empty()) {
3245
_debug("UserAgent: Crypto attribute in SDP, init SRTP session");
3247
// init local cryptografic capabilities for negotiation
3248
std::vector<sfl::CryptoSuiteDefinition>localCapabilities;
3249
for(int i = 0; i < 3; i++) {
3250
localCapabilities.push_back(sfl::CryptoSuites[i]);
3253
sfl::SdesNegotiator sdesnego(localCapabilities, crypto_offer);
3255
if(sdesnego.negotiate()) {
3256
_debug("UserAgent: SDES negociation successfull \n");
3257
nego_success = true;
3259
_debug("UserAgent: Set remote cryptographic context\n");
3347
if (!crypto_offer.empty()) {
3349
_debug ("UserAgent: Crypto attribute in SDP, init SRTP session");
3351
// init local cryptografic capabilities for negotiation
3352
std::vector<sfl::CryptoSuiteDefinition>localCapabilities;
3354
for (int i = 0; i < 3; i++) {
3355
localCapabilities.push_back (sfl::CryptoSuites[i]);
3358
// Mkae sure incoming crypto offer is valid
3359
sfl::SdesNegotiator sdesnego (localCapabilities, crypto_offer);
3361
if (sdesnego.negotiate()) {
3362
_debug ("UserAgent: SDES negociation successfull");
3363
nego_success = true;
3365
_debug ("UserAgent: Set remote cryptographic context");
3261
call->getAudioRtp()->setRemoteCryptoInfo(sdesnego);
3368
call->getAudioRtp()->setRemoteCryptoInfo (sdesnego);
3265
3371
DBusManager::instance().getCallManager()->secureSdesOn (call->getCallId());
3268
DBusManager::instance().getCallManager()->secureSdesOff (call->getCallId());
3373
DBusManager::instance().getCallManager()->secureSdesOff (call->getCallId());
3272
// We did not found any crypto context for this media
3273
if(!nego_success && call->getAudioRtp()->getAudioRtpType() == sfl::Sdes) {
3378
// We did not found any crypto context for this media, RTP fallback
3379
if (!nego_success && call->getAudioRtp()->getAudioRtpType() == sfl::Sdes) {
3275
3381
// We did not found any crypto context for this media
3276
3382
// @TODO if SRTPONLY, CallFail
3278
_debug("UserAgent: Did not found any crypto or negociation failed but Sdes enabled");
3384
_debug ("UserAgent: Did not found any crypto or negociation failed but Sdes enabled");
3279
3385
call->getAudioRtp()->stop();
3280
call->getAudioRtp()->setSrtpEnabled(false);
3282
// if RTPFALLBACK, change RTP session
3283
AccountID accountID = Manager::instance().getAccountFromCall (call->getCallId());
3284
if(Manager::instance().getConfigString (accountID, SRTP_RTP_FALLBACK) == "true")
3285
call->getAudioRtp()->initAudioRtpSession(call);
3288
if(nego_success && call->getAudioRtp()->getAudioRtpType() != sfl::Sdes) {
3290
// We found a crypto context for this media but Sdes is not
3291
// enabled for this call, make a try using RTP only...
3292
_debug("UserAgent: SDES not initialized for this call\n");
3386
call->getAudioRtp()->setSrtpEnabled (false);
3388
// if RTPFALLBACK, change RTP session
3389
AccountID accountID = Manager::instance().getAccountFromCall (call->getCallId());
3390
SIPAccount *account = (SIPAccount *) Manager::instance().getAccount (accountID);
3392
if (account->getSrtpFallback())
3393
call->getAudioRtp()->initAudioRtpSession (call);
3396
// Start audio rtp session.
3296
3398
Sdp *sdpSession = call->getLocalSDP();
3300
3403
AudioCodec *sessionMedia = sdpSession->get_session_media();
3304
AudioCodecType pl = (AudioCodecType)sessionMedia->getPayload();
3305
AudioCodec* audiocodec = Manager::instance().getCodecDescriptorMap().instantiateCodec(pl);
3307
if (audiocodec == NULL)
3308
_error ("UserAgent: No audiocodec found");
3408
AudioCodecType pl = (AudioCodecType) sessionMedia->getPayload();
3409
AudioCodec* audiocodec = Manager::instance().getCodecDescriptorMap().instantiateCodec (pl);
3411
if (audiocodec == NULL)
3412
_error ("UserAgent: No audiocodec found");
3312
3416
call->setAudioStart (true);
3313
call->getAudioRtp()->start(audiocodec);
3417
call->getAudioRtp()->start (audiocodec);
3314
3418
} catch (exception& rtpException) {
3315
3419
_error ("UserAgent: Error: %s", rtpException.what());
3320
void call_on_forked (pjsip_inv_session *inv, pjsip_event *e)
3424
void call_on_forked (pjsip_inv_session *inv UNUSED, pjsip_event *e UNUSED)
3324
void call_on_tsx_changed (pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_event *e)
3428
void call_on_tsx_changed (pjsip_inv_session *inv UNUSED, pjsip_transaction *tsx, pjsip_event *e)
3328
_debug("UserAgent: Transaction changed to state %s", transactionStateMap[tsx->state]);
3432
_debug ("UserAgent: Transaction changed to state %s", transactionStateMap[tsx->state]);
3434
pjsip_rx_data* r_data;
3435
pjsip_tx_data* t_data;
3330
3437
if (tsx->role==PJSIP_ROLE_UAS && tsx->state==PJSIP_TSX_STATE_TRYING &&
3331
pjsip_method_cmp (&tsx->method, &pjsip_refer_method) ==0) {
3438
pjsip_method_cmp (&tsx->method, &pjsip_refer_method) ==0) {
3332
3439
/** Handle the refer method **/
3333
3440
onCallTransfered (inv, e->body.tsx_state.src.rdata);
3334
3442
} else if (tsx->role==PJSIP_ROLE_UAS && tsx->state==PJSIP_TSX_STATE_TRYING) {
3336
3444
if (e && e->body.rx_msg.rdata) {
3340
pjsip_tx_data* t_data;
3341
pjsip_rx_data* r_data = e->body.rx_msg.rdata;
3448
r_data = e->body.rx_msg.rdata;
3343
3450
if (r_data && r_data->msg_info.msg->line.req.method.id == PJSIP_OTHER_METHOD) {
3347
3454
std::string method_info = "INFO";
3348
3455
std::string method_notify = "NOTIFY";
3350
3457
std::string request = pjsip_rx_data_get_info (r_data);
3352
_debug("UserAgent: %s", request.c_str());
3354
if(request.find (method_notify) != (size_t)-1) {
3357
// Must reply 200 OK on SIP INFO request
3358
else if (request.find (method_info) != (size_t)-1) {
3459
_debug ("UserAgent: %s", request.c_str());
3461
if (request.find (method_notify) != (size_t)-1) {
3464
// Must reply 200 OK on SIP INFO request
3465
else if (request.find (method_info) != (size_t)-1) {
3360
3467
pjsip_dlg_create_response (inv->dlg, r_data, PJSIP_SC_OK, NULL, &t_data);
3474
// Incoming TEXT message
3475
if (e && e->body.tsx_state.src.rdata) {
3477
// sender of this message
3480
// Get the message inside the transaction
3481
r_data = e->body.tsx_state.src.rdata;
3482
std::string formatedMessage = (char*) r_data->msg_info.msg->body->data;
3484
// Try to determine who is the recipient of the message
3485
SIPCall *call = reinterpret_cast<SIPCall *> (inv->mod_data[getModId() ]);
3488
_debug ("Incoming TEXT message: Can't find the recipient of the message");
3492
// Respond with a 200/OK
3493
pjsip_dlg_create_response (inv->dlg, r_data, PJSIP_SC_OK, NULL, &t_data);
3494
pjsip_dlg_send_response (inv->dlg, tsx, t_data);
3496
std::string message;
3497
std::string urilist;
3498
sfl::InstantMessaging::UriList list;
3500
sfl::InstantMessaging *module = Manager::instance().getInstantMessageModule();
3503
// retrive message from formated text
3504
message = module->findTextMessage (formatedMessage);
3506
// retreive the recipient-list of this message
3507
urilist = module->findTextUriList (formatedMessage);
3509
// parse the recipient list xml
3510
list = module->parseXmlUriList (urilist);
3512
// If no item present in the list, peer is considered as the sender
3514
from = call->getPeerNumber ();
3516
sfl::InstantMessaging::UriEntry entry = list.front();
3517
sfl::InstantMessaging::UriEntry::iterator iterAttr = entry.find (IM_XML_URI);
3519
if (iterAttr->second != "Me")
3520
from = iterAttr->second;
3522
from = call->getPeerNumber ();
3525
} catch (sfl::InstantMessageException &e) {
3526
_error ("SipVoipLink: %s", e.what());
3528
from = call->getPeerNumber ();
3532
// strip < and > characters in case of an IP address
3533
std::string stripped;
3535
if (from[0] == '<' && from[from.size()-1] == '>')
3536
stripped = from.substr (1, from.size()-2);
3540
// Pass through the instant messaging module if needed
3541
// Right now, it does do anything.
3542
// And notify the clients
3544
Manager::instance ().incomingMessage (call->getCallId (), stripped, module->receive (message, stripped, call->getCallId ()));
3526
3713
/* If we can't find any voIP link to handle the incoming call */
3528
_warn("UserAgent: Error: cannot retrieve the voiplink from the account ID...");
3529
pj_strdup2 (_pool, &reason, "ERROR: cannot retrieve the voip link from account");
3530
pjsip_endpt_respond_stateless (_endpt, rdata, PJSIP_SC_INTERNAL_SERVER_ERROR,
3531
&reason, NULL, NULL);
3715
_warn ("UserAgent: Error: cannot retrieve the voiplink from the account ID...");
3716
pj_strdup2 (_pool, &reason, "ERROR: cannot retrieve the voip link from account");
3717
pjsip_endpt_respond_stateless (_endpt, rdata, PJSIP_SC_INTERNAL_SERVER_ERROR,
3718
&reason, NULL, NULL);
3536
3723
// Parse the display name from "From" header
3537
3724
char* from_header = strstr (rdata->msg_info.msg_buf, "From: ");
3538
3726
if (from_header) {
3539
3727
std::string temp (from_header);
3540
3728
int begin_displayName = temp.find ("\"") + 1;
3541
3729
int end_displayName = temp.rfind ("\"");
3542
3730
displayName = temp.substr (begin_displayName, end_displayName - begin_displayName);
3543
if(displayName.size() > 25) {
3544
displayName = std::string ("");
3732
if (displayName.size() > 25) {
3733
displayName = std::string ("");
3547
3736
displayName = std::string ("");
3558
3747
// Store the peer number
3559
3748
char tmp[PJSIP_MAX_URL_SIZE];
3560
3749
int length = pjsip_uri_print (PJSIP_URI_IN_FROMTO_HDR,
3561
sip_uri, tmp, PJSIP_MAX_URL_SIZE);
3750
sip_uri, tmp, PJSIP_MAX_URL_SIZE);
3563
3752
std::string peerNumber (tmp, length);
3565
3754
//Remove sip: prefix
3566
size_t found = peerNumber.find("sip:");
3567
if (found!=std::string::npos)
3568
peerNumber.erase(found, found+4);
3570
found = peerNumber.find("@");
3571
if (found!=std::string::npos)
3572
peerNumber.erase(found);
3574
_debug("UserAgent: Peer number: %s", peerNumber.c_str());
3755
size_t found = peerNumber.find ("sip:");
3757
if (found!=std::string::npos)
3758
peerNumber.erase (found, found+4);
3760
found = peerNumber.find ("@");
3762
if (found!=std::string::npos)
3763
peerNumber.erase (found);
3765
_debug ("UserAgent: Peer number: %s", peerNumber.c_str());
3576
3767
// Get the server voicemail notification
3577
3768
// Catch the NOTIFY message
3612
3803
account = dynamic_cast<SIPAccount *> (Manager::instance().getAccount (account_id));
3614
3805
get_remote_sdp_from_offer (rdata, &r_sdp);
3615
if(account->getActiveCodecs().empty()) {
3616
_warn ("UserAgent: Error: No active codec");
3617
pj_strdup2 (_pool, &reason, "no active codec");
3618
pjsip_endpt_respond_stateless (_endpt, rdata, PJSIP_SC_NOT_ACCEPTABLE_HERE ,
3619
&reason, NULL, NULL);
3807
if (account->getActiveCodecs().empty()) {
3808
_warn ("UserAgent: Error: No active codec");
3809
pj_strdup2 (_pool, &reason, "no active codec");
3810
pjsip_endpt_respond_stateless (_endpt, rdata, PJSIP_SC_NOT_ACCEPTABLE_HERE ,
3811
&reason, NULL, NULL);
3623
3815
// Verify that we can handle the request
3624
3816
status = pjsip_inv_verify_request (rdata, &options, NULL, NULL, _endpt, NULL);
3625
3818
if (status != PJ_SUCCESS) {
3626
3819
pj_strdup2 (_pool, &reason, "user agent unable to handle this INVITE");
3627
pjsip_endpt_respond_stateless (_endpt, rdata, PJSIP_SC_METHOD_NOT_ALLOWED,
3628
&reason, NULL, NULL);
3820
pjsip_endpt_respond_stateless (_endpt, rdata, PJSIP_SC_METHOD_NOT_ALLOWED,
3821
&reason, NULL, NULL);
3632
3825
/******************************************* URL HOOK *********************************************/
3634
if (Manager::instance().getConfigString (HOOKS, URLHOOK_SIP_ENABLED) == "1") {
3827
if (Manager::instance().hookPreference.getSipEnabled()) {
3636
_debug("UserAgent: Set sip url hooks");
3829
_debug ("UserAgent: Set sip url hooks");
3638
3831
std::string header_value;
3640
header_value = fetch_header_value (rdata->msg_info.msg, Manager::instance().getConfigString (HOOKS, URLHOOK_SIP_FIELD));
3833
header_value = fetch_header_value (rdata->msg_info.msg,
3834
Manager::instance().hookPreference.getUrlSipField());
3642
3836
if (header_value.size () < header_value.max_size()) {
3643
3837
if (header_value!="") {
3644
3838
urlhook->addAction (header_value,
3645
Manager::instance().getConfigString (HOOKS, URLHOOK_COMMAND));
3839
Manager::instance().hookPreference.getUrlCommand());
3648
3842
throw length_error ("UserAgent: Url exceeds std::string max_size");
3676
3873
// May use the published address as well
3678
addrToUse = SIPVoIPLink::instance("")->getInterfaceAddrFromName(account->getLocalInterface ());
3875
addrToUse = SIPVoIPLink::instance ("")->getInterfaceAddrFromName (account->getLocalInterface ());
3679
3876
account->isStunEnabled () ? addrSdp = account->getPublishedAddress () : addrSdp = addrToUse;
3680
3877
// Set the appropriate transport to have the right VIA header
3681
3878
link->init_transport_selector (account->getAccountTransport (), &tp);
3683
if(account->getAccountTransport()) {
3880
if (account->getAccountTransport()) {
3685
_debug("UserAgent: SIP transport for this account: %s %s (refcnt=%i)",
3686
account->getAccountTransport()->obj_name,
3687
account->getAccountTransport()->info,
3688
(int)pj_atomic_get(account->getAccountTransport()->ref_cnt));
3882
_debug ("UserAgent: SIP transport for this account: %s %s (refcnt=%i)",
3883
account->getAccountTransport()->obj_name,
3884
account->getAccountTransport()->info,
3885
(int) pj_atomic_get (account->getAccountTransport()->ref_cnt));
3713
3910
// We retrieve the remote sdp offer in the rdata struct to begin the negociation
3714
3911
call->getLocalSDP()->set_ip_address (addrSdp);
3913
// Init audio rtp session
3915
_debug ("UserAgent: Create RTP session for this call");
3717
3916
call->getAudioRtp()->initAudioRtpConfig (call);
3718
3917
call->getAudioRtp()->initAudioRtpSession (call);
3719
3918
} catch (...) {
3720
3919
_warn ("UserAgent: Error: Failed to create rtp thread from answer");
3922
// Retreive crypto offer from body, if any
3923
if (rdata->msg_info.msg->body) {
3925
char sdpbuffer[1000];
3926
rdata->msg_info.msg->body->print_body (rdata->msg_info.msg->body, sdpbuffer, 1000);
3927
std::string sdpoffer = std::string (sdpbuffer);
3928
size_t start = sdpoffer.find ("a=crypto:");
3930
// Found crypto header in SDP
3931
if (start != std::string::npos) {
3933
std::string cryptoHeader = sdpoffer.substr (start, (sdpoffer.size() - start) -1);
3934
_debug ("UserAgent: Found incoming crypto offer: %s", cryptoHeader.c_str());
3936
CryptoOffer crypto_offer;
3937
crypto_offer.push_back (cryptoHeader);
3939
bool nego_success = false;
3941
if (!crypto_offer.empty()) {
3943
_debug ("UserAgent: Crypto attribute in SDP, init SRTP session");
3945
// init local cryptografic capabilities for negotiation
3946
std::vector<sfl::CryptoSuiteDefinition>localCapabilities;
3948
for (int i = 0; i < 3; i++) {
3949
localCapabilities.push_back (sfl::CryptoSuites[i]);
3952
sfl::SdesNegotiator sdesnego (localCapabilities, crypto_offer);
3954
if (sdesnego.negotiate()) {
3955
_debug ("UserAgent: SDES negociation successfull \n");
3956
nego_success = true;
3959
_debug ("UserAgent: Create RTP session for this call");
3960
call->getAudioRtp()->setRemoteCryptoInfo (sdesnego);
3961
call->getAudioRtp()->initLocalCryptoInfo (call);
3963
_warn ("UserAgent: Error: Failed to create rtp thread from answer");
3723
3971
status = call->getLocalSDP()->receiving_initial_offer (r_sdp, account->getActiveCodecs ());
3725
3973
if (status!=PJ_SUCCESS) {
3726
delete call; call = NULL;
3727
_warn("UserAgent: fail in receiving initial offer");
3976
_warn ("UserAgent: fail in receiving initial offer");
3728
3977
pj_strdup2 (_pool, &reason, "fail in receiving initial offer");
3729
pjsip_endpt_respond_stateless (_endpt, rdata, PJSIP_SC_INTERNAL_SERVER_ERROR,
3730
&reason, NULL, NULL);
3978
pjsip_endpt_respond_stateless (_endpt, rdata, PJSIP_SC_INTERNAL_SERVER_ERROR,
3979
&reason, NULL, NULL);
3734
3983
/* Create the local dialog (UAS) */
3735
3984
status = pjsip_dlg_create_uas (pjsip_ua_instance(), rdata, NULL, &dialog);
3736
3986
if (status != PJ_SUCCESS) {
3737
delete call; call = NULL;
3738
_warn("UserAgent: Error: Failed to create uas dialog");
3989
_warn ("UserAgent: Error: Failed to create uas dialog");
3739
3990
pj_strdup2 (_pool, &reason, "fail to create uas dialog");
3740
pjsip_endpt_respond_stateless (_endpt, rdata, PJSIP_SC_INTERNAL_SERVER_ERROR,
3741
&reason, NULL, NULL);
3991
pjsip_endpt_respond_stateless (_endpt, rdata, PJSIP_SC_INTERNAL_SERVER_ERROR,
3992
&reason, NULL, NULL);
3788
4041
pj_bool_t mod_on_rx_response (pjsip_rx_data *rdata)
3790
4043
_info ("UserAgent: Transaction response using transport: %s %s (refcnt=%d)",
3791
rdata->tp_info.transport->obj_name,
3792
rdata->tp_info.transport->info,
3793
(int)pj_atomic_get(rdata->tp_info.transport->ref_cnt));
4044
rdata->tp_info.transport->obj_name,
4045
rdata->tp_info.transport->info,
4046
(int) pj_atomic_get (rdata->tp_info.transport->ref_cnt));
3795
4048
pjsip_dialog *dlg;
3796
dlg = pjsip_rdata_get_dlg( rdata );
3799
pjsip_transaction *tsx = pjsip_rdata_get_tsx( rdata );
3800
if ( tsx != NULL && tsx->method.id == PJSIP_INVITE_METHOD) {
3801
if (tsx->status_code < 200) {
3802
_info("UserAgent: Received provisional response");
3804
else if (tsx->status_code >= 300) {
3805
_warn("UserAgent: Dialog failed");
3806
// pjsip_dlg_dec_session(dlg);
3807
// ACK for non-2xx final response is sent by transaction.
3810
_info("UserAgent: Received 200 OK response");
3811
sendAck(dlg, rdata);
4049
dlg = pjsip_rdata_get_dlg (rdata);
4052
pjsip_transaction *tsx = pjsip_rdata_get_tsx (rdata);
4054
if (tsx != NULL && tsx->method.id == PJSIP_INVITE_METHOD) {
4055
if (tsx->status_code < 200) {
4056
_info ("UserAgent: Received provisional response");
4057
} else if (tsx->status_code >= 300) {
4058
_warn ("UserAgent: Dialog failed");
4059
// pjsip_dlg_dec_session(dlg);
4060
// ACK for non-2xx final response is sent by transaction.
4062
_info ("UserAgent: Received 200 OK response");
4063
sendAck (dlg, rdata);
3816
4068
return PJ_SUCCESS;
3819
static void sendAck(pjsip_dialog *dlg, pjsip_rx_data *rdata) {
3821
pjsip_tx_data *tdata;
3823
// Create ACK request
3824
pjsip_dlg_create_request(dlg, &pjsip_ack_method, rdata->msg_info.cseq->cseq, &tdata);
3826
pjsip_dlg_send_request( dlg, tdata,-1, NULL);
4071
static void sendAck (pjsip_dialog *dlg, pjsip_rx_data *rdata)
4074
pjsip_tx_data *tdata;
4076
// Create ACK request
4077
pjsip_dlg_create_request (dlg, &pjsip_ack_method, rdata->msg_info.cseq->cseq, &tdata);
4079
pjsip_dlg_send_request (dlg, tdata,-1, NULL);
3829
4082
void onCallTransfered (pjsip_inv_session *inv, pjsip_rx_data *rdata)
4247
void on_create_offer(pjsip_inv_session *inv, pjmedia_sdp_session **p_offer)
4501
void on_create_offer (pjsip_inv_session *inv, pjmedia_sdp_session **p_offer)
4249
_info("UserAgent: Create new SDP offer");
4251
/* Retrieve the call information */
4252
SIPCall * call = NULL;
4253
call = reinterpret_cast<SIPCall*> (inv->mod_data[_mod_ua.id]);
4255
CallID callid = call->getCallId();
4256
AccountID accountid = Manager::instance().getAccountFromCall(callid);
4258
SIPAccount *account = dynamic_cast<SIPAccount *>(Manager::instance().getAccount(accountid));
4503
_info ("UserAgent: Create new SDP offer");
4505
/* Retrieve the call information */
4506
SIPCall * call = NULL;
4507
call = reinterpret_cast<SIPCall*> (inv->mod_data[_mod_ua.id]);
4509
CallID callid = call->getCallId();
4510
AccountID accountid = Manager::instance().getAccountFromCall (callid);
4512
SIPAccount *account = dynamic_cast<SIPAccount *> (Manager::instance().getAccount (accountid));
4260
4514
SIPVoIPLink *link = dynamic_cast<SIPVoIPLink *> (Manager::instance().getAccountLink (accountid));
4262
// Set the local address
4263
std::string localAddress = link->getInterfaceAddrFromName(account->getLocalInterface ());
4264
// Set SDP parameters - Set to local
4265
std::string addrSdp = localAddress;
4267
_debug ("UserAgent: Local Address for IP2IP call: %s", localAddress.c_str());
4269
// If local address bound to ANY, reslove it using PJSIP
4270
if (localAddress == "0.0.0.0") {
4271
link->loadSIPLocalIP (&localAddress);
4274
// Local address to appear in SDP
4275
if (addrSdp == "0.0.0.0") {
4276
addrSdp = localAddress;
4516
// Set the local address
4517
std::string localAddress = link->getInterfaceAddrFromName (account->getLocalInterface ());
4518
// Set SDP parameters - Set to local
4519
std::string addrSdp = localAddress;
4521
_debug ("UserAgent: Local Address for IP2IP call: %s", localAddress.c_str());
4523
// If local address bound to ANY, reslove it using PJSIP
4524
if (localAddress == "0.0.0.0") {
4525
link->loadSIPLocalIP (&localAddress);
4528
// Local address to appear in SDP
4529
if (addrSdp == "0.0.0.0") {
4530
addrSdp = localAddress;
4279
4533
// Set local address for RTP media
4280
4534
setCallAudioLocal (call, localAddress);
4282
4536
// Building the local SDP offer
4283
4537
call->getLocalSDP()->set_ip_address (addrSdp);
4284
call->getLocalSDP()->create_initial_offer( account->getActiveCodecs() );
4538
call->getLocalSDP()->create_initial_offer (account->getActiveCodecs());
4286
4540
*p_offer = call->getLocalSDP()->get_local_sdp_session();
4465
4729
struct ifconf ifconf;
4469
ifaceList.push_back(std::string("default"));
4733
ifaceList.push_back (std::string ("default"));
4471
memset(&ifconf,0,sizeof(ifconf));
4735
memset (&ifconf,0,sizeof (ifconf));
4472
4736
ifconf.ifc_buf = (char*) (ifreqs);
4473
ifconf.ifc_len = sizeof(ifreqs);
4475
if(get_iface_list(&ifconf) < 0)
4476
_debug("getAllIpInterfaceByName error could not get interface list\n");
4478
nifaces = ifconf.ifc_len/sizeof(struct ifreq);
4480
_debug("Interfaces (count = %d):\n", nifaces);
4481
for(int i = 0; i < nifaces; i++) {
4482
_debug(" %s ", ifreqs[i].ifr_name);
4483
ifaceList.push_back(std::string (ifreqs[i].ifr_name));
4484
printf(" %s\n", getInterfaceAddrFromName(std::string (ifreqs[i].ifr_name)).c_str());
4737
ifconf.ifc_len = sizeof (ifreqs);
4739
if (get_iface_list (&ifconf) < 0)
4740
_debug ("getAllIpInterfaceByName error could not get interface list\n");
4742
nifaces = ifconf.ifc_len/sizeof (struct ifreq);
4744
_debug ("Interfaces (count = %d):\n", nifaces);
4746
for (int i = 0; i < nifaces; i++) {
4747
_debug (" %s ", ifreqs[i].ifr_name);
4748
ifaceList.push_back (std::string (ifreqs[i].ifr_name));
4749
printf (" %s\n", getInterfaceAddrFromName (std::string (ifreqs[i].ifr_name)).c_str());
4491
pj_bool_t stun_sock_on_status (pj_stun_sock *stun_sock, pj_stun_sock_op op, pj_status_t status)
4756
pj_bool_t stun_sock_on_status (pj_stun_sock *stun_sock UNUSED, pj_stun_sock_op op UNUSED, pj_status_t status)
4493
4758
if (status == PJ_SUCCESS)
4494
4759
return PJ_TRUE;