pjsua_pres.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <pjsua-lib/pjsua.h>
  20. #include <pjsua-lib/pjsua_internal.h>
  21. #define THIS_FILE "pjsua_pres.c"
  22. static void subscribe_buddy(pjsua_buddy_id buddy_id, pj_bool_t presence);
  23. static void unsubscribe_buddy(pjsua_buddy_id buddy_id, pj_bool_t presence);
  24. /*
  25. * Find buddy.
  26. */
  27. static pjsua_buddy_id find_buddy(const pjsip_uri *uri)
  28. {
  29. const pjsip_sip_uri *sip_uri;
  30. unsigned i;
  31. uri = (const pjsip_uri*) pjsip_uri_get_uri((pjsip_uri*)uri);
  32. if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
  33. return PJSUA_INVALID_ID;
  34. sip_uri = (const pjsip_sip_uri*) uri;
  35. for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
  36. const pjsua_buddy *b = &pjsua_var.buddy[i];
  37. if (!pjsua_buddy_is_valid(i))
  38. continue;
  39. if (pj_stricmp(&sip_uri->user, &b->name)==0 &&
  40. pj_stricmp(&sip_uri->host, &b->host)==0 &&
  41. (sip_uri->port==(int)b->port || (sip_uri->port==0 && b->port==5060)))
  42. {
  43. /* Match */
  44. return i;
  45. }
  46. }
  47. return PJSUA_INVALID_ID;
  48. }
  49. #define LOCK_DIALOG 1
  50. #define LOCK_PJSUA 2
  51. #define LOCK_ALL (LOCK_DIALOG | LOCK_PJSUA)
  52. /* Buddy lock object */
  53. struct buddy_lock
  54. {
  55. pjsua_buddy *buddy;
  56. pjsip_dialog *dlg;
  57. pj_uint8_t flag;
  58. };
  59. /* Acquire lock to the specified buddy_id */
  60. static pj_status_t lock_buddy(const char *title,
  61. pjsua_buddy_id buddy_id,
  62. struct buddy_lock *lck,
  63. unsigned _unused_)
  64. {
  65. enum { MAX_RETRY=50 };
  66. pj_bool_t has_pjsua_lock = PJ_FALSE;
  67. unsigned retry;
  68. PJ_UNUSED_ARG(_unused_);
  69. pj_bzero(lck, sizeof(*lck));
  70. for (retry=0; retry<MAX_RETRY; ++retry) {
  71. if (PJSUA_TRY_LOCK() != PJ_SUCCESS) {
  72. pj_thread_sleep(retry/10);
  73. continue;
  74. }
  75. has_pjsua_lock = PJ_TRUE;
  76. lck->flag = LOCK_PJSUA;
  77. lck->buddy = &pjsua_var.buddy[buddy_id];
  78. if (lck->buddy->dlg == NULL)
  79. return PJ_SUCCESS;
  80. if (pjsip_dlg_try_inc_lock(lck->buddy->dlg) != PJ_SUCCESS) {
  81. lck->flag = 0;
  82. lck->buddy = NULL;
  83. has_pjsua_lock = PJ_FALSE;
  84. PJSUA_UNLOCK();
  85. pj_thread_sleep(retry/10);
  86. continue;
  87. }
  88. lck->dlg = lck->buddy->dlg;
  89. lck->flag = LOCK_DIALOG;
  90. PJSUA_UNLOCK();
  91. break;
  92. }
  93. if (lck->flag == 0) {
  94. if (has_pjsua_lock == PJ_FALSE)
  95. PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire PJSUA mutex "
  96. "(possibly system has deadlocked) in %s",
  97. title));
  98. else
  99. PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire dialog mutex "
  100. "(possibly system has deadlocked) in %s",
  101. title));
  102. return PJ_ETIMEDOUT;
  103. }
  104. return PJ_SUCCESS;
  105. }
  106. /* Release buddy lock */
  107. static void unlock_buddy(struct buddy_lock *lck)
  108. {
  109. if (lck->flag & LOCK_DIALOG)
  110. pjsip_dlg_dec_lock(lck->dlg);
  111. if (lck->flag & LOCK_PJSUA)
  112. PJSUA_UNLOCK();
  113. }
  114. /*
  115. * Get total number of buddies.
  116. */
  117. PJ_DEF(unsigned) pjsua_get_buddy_count(void)
  118. {
  119. return pjsua_var.buddy_cnt;
  120. }
  121. /*
  122. * Find buddy.
  123. */
  124. PJ_DEF(pjsua_buddy_id) pjsua_buddy_find(const pj_str_t *uri_str)
  125. {
  126. pj_str_t input;
  127. pj_pool_t *pool;
  128. pjsip_uri *uri;
  129. pjsua_buddy_id buddy_id;
  130. pool = pjsua_pool_create("buddyfind", 512, 512);
  131. pj_strdup_with_null(pool, &input, uri_str);
  132. uri = pjsip_parse_uri(pool, input.ptr, input.slen, 0);
  133. if (!uri)
  134. buddy_id = PJSUA_INVALID_ID;
  135. else {
  136. PJSUA_LOCK();
  137. buddy_id = find_buddy(uri);
  138. PJSUA_UNLOCK();
  139. }
  140. pj_pool_release(pool);
  141. return buddy_id;
  142. }
  143. /*
  144. * Check if buddy ID is valid.
  145. */
  146. PJ_DEF(pj_bool_t) pjsua_buddy_is_valid(pjsua_buddy_id buddy_id)
  147. {
  148. return buddy_id>=0 && buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy) &&
  149. pjsua_var.buddy[buddy_id].uri.slen != 0;
  150. }
  151. /*
  152. * Enum buddy IDs.
  153. */
  154. PJ_DEF(pj_status_t) pjsua_enum_buddies( pjsua_buddy_id ids[],
  155. unsigned *count)
  156. {
  157. unsigned i, c;
  158. PJ_ASSERT_RETURN(ids && count, PJ_EINVAL);
  159. PJSUA_LOCK();
  160. for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
  161. if (!pjsua_var.buddy[i].uri.slen)
  162. continue;
  163. ids[c] = i;
  164. ++c;
  165. }
  166. *count = c;
  167. PJSUA_UNLOCK();
  168. return PJ_SUCCESS;
  169. }
  170. /*
  171. * Get detailed buddy info.
  172. */
  173. PJ_DEF(pj_status_t) pjsua_buddy_get_info( pjsua_buddy_id buddy_id,
  174. pjsua_buddy_info *info)
  175. {
  176. pj_size_t total=0;
  177. struct buddy_lock lck;
  178. pjsua_buddy *buddy;
  179. pj_status_t status;
  180. PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
  181. pj_bzero(info, sizeof(pjsua_buddy_info));
  182. status = lock_buddy("pjsua_buddy_get_info()", buddy_id, &lck, 0);
  183. if (status != PJ_SUCCESS)
  184. return status;
  185. buddy = lck.buddy;
  186. info->id = buddy->index;
  187. if (pjsua_var.buddy[buddy_id].uri.slen == 0) {
  188. unlock_buddy(&lck);
  189. return PJ_SUCCESS;
  190. }
  191. /* uri */
  192. info->uri.ptr = info->buf_ + total;
  193. pj_strncpy(&info->uri, &buddy->uri, sizeof(info->buf_)-total);
  194. total += info->uri.slen;
  195. /* contact */
  196. if (total < sizeof(info->buf_)) {
  197. info->contact.ptr = info->buf_ + total;
  198. pj_strncpy(&info->contact, &buddy->contact, sizeof(info->buf_) - total);
  199. total += info->contact.slen;
  200. } else {
  201. info->contact = pj_str("");
  202. }
  203. /* Presence status */
  204. pj_memcpy(&info->pres_status, &buddy->status, sizeof(pjsip_pres_status));
  205. /* status and status text */
  206. if (buddy->sub == NULL || buddy->status.info_cnt==0) {
  207. info->status = PJSUA_BUDDY_STATUS_UNKNOWN;
  208. info->status_text = pj_str("?");
  209. } else if (pjsua_var.buddy[buddy_id].status.info[0].basic_open) {
  210. info->status = PJSUA_BUDDY_STATUS_ONLINE;
  211. /* copy RPID information */
  212. info->rpid = buddy->status.info[0].rpid;
  213. if (info->rpid.note.slen)
  214. info->status_text = info->rpid.note;
  215. else
  216. info->status_text = pj_str("Online");
  217. } else {
  218. info->status = PJSUA_BUDDY_STATUS_OFFLINE;
  219. info->rpid = buddy->status.info[0].rpid;
  220. if (info->rpid.note.slen)
  221. info->status_text = info->rpid.note;
  222. else
  223. info->status_text = pj_str("Offline");
  224. }
  225. /* monitor pres */
  226. info->monitor_pres = buddy->monitor;
  227. /* subscription state and termination reason */
  228. info->sub_term_code = buddy->term_code;
  229. if (buddy->sub) {
  230. info->sub_state = pjsip_evsub_get_state(buddy->sub);
  231. info->sub_state_name = pjsip_evsub_get_state_name(buddy->sub);
  232. if (info->sub_state == PJSIP_EVSUB_STATE_TERMINATED &&
  233. total < sizeof(info->buf_))
  234. {
  235. info->sub_term_reason.ptr = info->buf_ + total;
  236. pj_strncpy(&info->sub_term_reason,
  237. pjsip_evsub_get_termination_reason(buddy->sub),
  238. sizeof(info->buf_) - total);
  239. total += info->sub_term_reason.slen;
  240. } else {
  241. info->sub_term_reason = pj_str("");
  242. }
  243. } else if (total < sizeof(info->buf_)) {
  244. info->sub_state_name = "NULL";
  245. info->sub_term_reason.ptr = info->buf_ + total;
  246. pj_strncpy(&info->sub_term_reason, &buddy->term_reason,
  247. sizeof(info->buf_) - total);
  248. total += info->sub_term_reason.slen;
  249. } else {
  250. info->sub_state_name = "NULL";
  251. info->sub_term_reason = pj_str("");
  252. }
  253. unlock_buddy(&lck);
  254. return PJ_SUCCESS;
  255. }
  256. /*
  257. * Get detailed buddy dialog event info.
  258. */
  259. PJ_DEF(pj_status_t)
  260. pjsua_buddy_get_dlg_event_info( pjsua_buddy_id buddy_id,
  261. pjsua_buddy_dlg_event_info *info)
  262. {
  263. unsigned total=0;
  264. struct buddy_lock lck;
  265. pjsua_buddy *buddy;
  266. pj_status_t status;
  267. #define COPY_TO_BUF(status, field) \
  268. if (status.field.slen > 0) { \
  269. if (total + status.field.slen < sizeof(info->buf_)) { \
  270. info->field.ptr = info->buf_ + total; \
  271. pj_strncpy(&info->field, &status.field, status.field.slen); \
  272. total += info->field.slen; \
  273. } else { \
  274. PJ_LOG(4, (THIS_FILE, "Insufficient buffer when copying %s", \
  275. #field)); \
  276. } \
  277. }
  278. PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
  279. pj_bzero(info, sizeof(pjsua_buddy_dlg_event_info));
  280. status = lock_buddy("pjsua_buddy_get_dlg_event_info()", buddy_id, &lck, 0);
  281. if (status != PJ_SUCCESS)
  282. return status;
  283. buddy = lck.buddy;
  284. info->id = buddy->index;
  285. if (pjsua_var.buddy[buddy_id].uri.slen == 0) {
  286. unlock_buddy(&lck);
  287. return PJ_SUCCESS;
  288. }
  289. /* uri */
  290. info->uri.ptr = info->buf_ + total;
  291. pj_strncpy(&info->uri, &buddy->uri, sizeof(info->buf_)-total);
  292. total += info->uri.slen;
  293. COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_info_state);
  294. COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_info_entity);
  295. if (buddy->dlg_ev_status.info[0].dialog_node) {
  296. COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_id);
  297. COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_call_id);
  298. COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_remote_tag);
  299. COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_local_tag);
  300. COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_direction);
  301. COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_state);
  302. COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_duration);
  303. COPY_TO_BUF(buddy->dlg_ev_status.info[0], local_identity);
  304. COPY_TO_BUF(buddy->dlg_ev_status.info[0], local_identity_display);
  305. COPY_TO_BUF(buddy->dlg_ev_status.info[0], local_target_uri);
  306. COPY_TO_BUF(buddy->dlg_ev_status.info[0], remote_identity);
  307. COPY_TO_BUF(buddy->dlg_ev_status.info[0], remote_identity_display);
  308. COPY_TO_BUF(buddy->dlg_ev_status.info[0], remote_target_uri);
  309. }
  310. /* subscription state and termination reason */
  311. info->sub_term_code = buddy->term_code;
  312. if (buddy->sub) {
  313. info->sub_state = pjsip_evsub_get_state(buddy->sub);
  314. info->sub_state_name = pjsip_evsub_get_state_name(buddy->sub);
  315. if (info->sub_state == PJSIP_EVSUB_STATE_TERMINATED &&
  316. total < sizeof(info->buf_))
  317. {
  318. info->sub_term_reason.ptr = info->buf_ + total;
  319. pj_strncpy(&info->sub_term_reason,
  320. pjsip_evsub_get_termination_reason(buddy->sub),
  321. sizeof(info->buf_) - total);
  322. total += info->sub_term_reason.slen;
  323. } else {
  324. info->sub_term_reason = pj_str("");
  325. }
  326. } else if (total < sizeof(info->buf_)) {
  327. info->sub_state_name = "NULL";
  328. info->sub_term_reason.ptr = info->buf_ + total;
  329. pj_strncpy(&info->sub_term_reason, &buddy->term_reason,
  330. sizeof(info->buf_) - total);
  331. total += info->sub_term_reason.slen;
  332. } else {
  333. info->sub_state_name = "NULL";
  334. info->sub_term_reason = pj_str("");
  335. }
  336. unlock_buddy(&lck);
  337. return PJ_SUCCESS;
  338. }
  339. /*
  340. * Set the user data associated with the buddy object.
  341. */
  342. PJ_DEF(pj_status_t) pjsua_buddy_set_user_data( pjsua_buddy_id buddy_id,
  343. void *user_data)
  344. {
  345. struct buddy_lock lck;
  346. pj_status_t status;
  347. PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
  348. status = lock_buddy("pjsua_buddy_set_user_data()", buddy_id, &lck, 0);
  349. if (status != PJ_SUCCESS)
  350. return status;
  351. pjsua_var.buddy[buddy_id].user_data = user_data;
  352. unlock_buddy(&lck);
  353. return PJ_SUCCESS;
  354. }
  355. /*
  356. * Get the user data associated with the budy object.
  357. */
  358. PJ_DEF(void*) pjsua_buddy_get_user_data(pjsua_buddy_id buddy_id)
  359. {
  360. struct buddy_lock lck;
  361. pj_status_t status;
  362. void *user_data;
  363. PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), NULL);
  364. status = lock_buddy("pjsua_buddy_get_user_data()", buddy_id, &lck, 0);
  365. if (status != PJ_SUCCESS)
  366. return NULL;
  367. user_data = pjsua_var.buddy[buddy_id].user_data;
  368. unlock_buddy(&lck);
  369. return user_data;
  370. }
  371. /*
  372. * Reset buddy descriptor.
  373. */
  374. static void reset_buddy(pjsua_buddy_id id)
  375. {
  376. pj_pool_t *pool = pjsua_var.buddy[id].pool;
  377. pj_bzero(&pjsua_var.buddy[id], sizeof(pjsua_var.buddy[id]));
  378. pjsua_var.buddy[id].pool = pool;
  379. pjsua_var.buddy[id].index = id;
  380. }
  381. /*
  382. * Add new buddy.
  383. */
  384. PJ_DEF(pj_status_t) pjsua_buddy_add( const pjsua_buddy_config *cfg,
  385. pjsua_buddy_id *p_buddy_id)
  386. {
  387. pjsip_name_addr *url;
  388. pjsua_buddy *buddy;
  389. pjsip_sip_uri *sip_uri;
  390. int index;
  391. pj_str_t tmp;
  392. PJ_ASSERT_RETURN(pjsua_var.buddy_cnt <=
  393. PJ_ARRAY_SIZE(pjsua_var.buddy),
  394. PJ_ETOOMANY);
  395. PJ_LOG(4,(THIS_FILE, "Adding buddy: %.*s",
  396. (int)cfg->uri.slen, cfg->uri.ptr));
  397. pj_log_push_indent();
  398. PJSUA_LOCK();
  399. /* Find empty slot */
  400. for (index=0; index<(int)PJ_ARRAY_SIZE(pjsua_var.buddy); ++index) {
  401. if (pjsua_var.buddy[index].uri.slen == 0)
  402. break;
  403. }
  404. /* Expect to find an empty slot */
  405. if (index == PJ_ARRAY_SIZE(pjsua_var.buddy)) {
  406. PJSUA_UNLOCK();
  407. /* This shouldn't happen */
  408. pj_assert(!"index < PJ_ARRAY_SIZE(pjsua_var.buddy)");
  409. pj_log_pop_indent();
  410. return PJ_ETOOMANY;
  411. }
  412. buddy = &pjsua_var.buddy[index];
  413. /* Create pool for this buddy */
  414. if (buddy->pool) {
  415. pj_pool_reset(buddy->pool);
  416. } else {
  417. char name[PJ_MAX_OBJ_NAME];
  418. pj_ansi_snprintf(name, sizeof(name), "buddy%03d", index);
  419. buddy->pool = pjsua_pool_create(name, 512, 256);
  420. }
  421. /* Init buffers for presence subscription status */
  422. buddy->term_reason.ptr = (char*)
  423. pj_pool_alloc(buddy->pool,
  424. PJSUA_BUDDY_SUB_TERM_REASON_LEN);
  425. /* Get name and display name for buddy */
  426. pj_strdup_with_null(buddy->pool, &tmp, &cfg->uri);
  427. url = (pjsip_name_addr*)pjsip_parse_uri(buddy->pool, tmp.ptr, tmp.slen,
  428. PJSIP_PARSE_URI_AS_NAMEADDR);
  429. if (url == NULL) {
  430. pjsua_perror(THIS_FILE, "Unable to add buddy", PJSIP_EINVALIDURI);
  431. pj_pool_release(buddy->pool);
  432. buddy->pool = NULL;
  433. PJSUA_UNLOCK();
  434. pj_log_pop_indent();
  435. return PJSIP_EINVALIDURI;
  436. }
  437. /* Only support SIP schemes */
  438. if (!PJSIP_URI_SCHEME_IS_SIP(url) && !PJSIP_URI_SCHEME_IS_SIPS(url)) {
  439. pj_pool_release(buddy->pool);
  440. buddy->pool = NULL;
  441. PJSUA_UNLOCK();
  442. pj_log_pop_indent();
  443. return PJSIP_EINVALIDSCHEME;
  444. }
  445. /* Reset buddy, to make sure everything is cleared with default
  446. * values
  447. */
  448. reset_buddy(index);
  449. /* Save URI */
  450. pjsua_var.buddy[index].uri = tmp;
  451. sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(url->uri);
  452. pjsua_var.buddy[index].name = sip_uri->user;
  453. pjsua_var.buddy[index].display = url->display;
  454. pjsua_var.buddy[index].host = sip_uri->host;
  455. pjsua_var.buddy[index].port = sip_uri->port;
  456. pjsua_var.buddy[index].monitor = cfg->subscribe;
  457. if (pjsua_var.buddy[index].port == 0)
  458. pjsua_var.buddy[index].port = 5060;
  459. /* Save user data */
  460. pjsua_var.buddy[index].user_data = (void*)cfg->user_data;
  461. if (p_buddy_id)
  462. *p_buddy_id = index;
  463. pjsua_var.buddy_cnt++;
  464. PJSUA_UNLOCK();
  465. PJ_LOG(4,(THIS_FILE, "Buddy %d added.", index));
  466. if (cfg->subscribe) {
  467. pjsua_buddy_subscribe_pres(index, cfg->subscribe);
  468. } else if (cfg->subscribe_dlg_event) {
  469. pjsua_buddy_subscribe_dlg_event(index, cfg->subscribe_dlg_event);
  470. }
  471. pj_log_pop_indent();
  472. return PJ_SUCCESS;
  473. }
  474. /*
  475. * Delete buddy.
  476. */
  477. PJ_DEF(pj_status_t) pjsua_buddy_del(pjsua_buddy_id buddy_id)
  478. {
  479. struct buddy_lock lck;
  480. pj_status_t status;
  481. PJ_ASSERT_RETURN(buddy_id>=0 &&
  482. buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy),
  483. PJ_EINVAL);
  484. if (pjsua_var.buddy[buddy_id].uri.slen == 0) {
  485. return PJ_SUCCESS;
  486. }
  487. status = lock_buddy("pjsua_buddy_del()", buddy_id, &lck, 0);
  488. if (status != PJ_SUCCESS)
  489. return status;
  490. PJ_LOG(4,(THIS_FILE, "Buddy %d: deleting..", buddy_id));
  491. pj_log_push_indent();
  492. /* Unsubscribe */
  493. if (pjsua_var.buddy[buddy_id].presence) {
  494. pjsua_buddy_subscribe_pres(buddy_id, PJ_FALSE);
  495. } else {
  496. pjsua_buddy_subscribe_dlg_event(buddy_id, PJ_FALSE);
  497. }
  498. /* Not interested with further events for this buddy */
  499. if (pjsua_var.buddy[buddy_id].sub) {
  500. pjsip_evsub_set_mod_data(pjsua_var.buddy[buddy_id].sub,
  501. pjsua_var.mod.id, NULL);
  502. }
  503. /* Remove buddy */
  504. pjsua_var.buddy[buddy_id].uri.slen = 0;
  505. pjsua_var.buddy_cnt--;
  506. /* Clear timer */
  507. if (pjsua_var.buddy[buddy_id].timer.id) {
  508. pjsua_cancel_timer(&pjsua_var.buddy[buddy_id].timer);
  509. pjsua_var.buddy[buddy_id].timer.id = PJ_FALSE;
  510. }
  511. /* Reset buddy struct */
  512. reset_buddy(buddy_id);
  513. unlock_buddy(&lck);
  514. pj_log_pop_indent();
  515. return PJ_SUCCESS;
  516. }
  517. /*
  518. * Enable/disable buddy's presence monitoring.
  519. */
  520. PJ_DEF(pj_status_t) pjsua_buddy_subscribe_pres( pjsua_buddy_id buddy_id,
  521. pj_bool_t subscribe)
  522. {
  523. struct buddy_lock lck;
  524. pj_status_t status;
  525. PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
  526. status = lock_buddy("pjsua_buddy_subscribe_pres()", buddy_id, &lck, 0);
  527. if (status != PJ_SUCCESS)
  528. return status;
  529. pj_log_push_indent();
  530. lck.buddy->monitor = subscribe;
  531. pjsua_buddy_update_pres(buddy_id);
  532. unlock_buddy(&lck);
  533. pj_log_pop_indent();
  534. return PJ_SUCCESS;
  535. }
  536. PJ_DEF(pj_status_t) pjsua_buddy_subscribe_dlg_event(pjsua_buddy_id buddy_id,
  537. pj_bool_t subscribe)
  538. {
  539. struct buddy_lock lck;
  540. pj_status_t status;
  541. PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
  542. status = lock_buddy("pjsua_buddy_subscribe_dlg_event()", buddy_id, &lck, 0);
  543. if (status != PJ_SUCCESS)
  544. return status;
  545. pj_log_push_indent();
  546. lck.buddy->monitor = subscribe;
  547. pjsua_buddy_update_dlg_event(buddy_id);
  548. unlock_buddy(&lck);
  549. pj_log_pop_indent();
  550. return PJ_SUCCESS;
  551. }
  552. /*
  553. * Update buddy's presence.
  554. */
  555. PJ_DEF(pj_status_t) pjsua_buddy_update_pres(pjsua_buddy_id buddy_id)
  556. {
  557. struct buddy_lock lck;
  558. pj_status_t status;
  559. PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
  560. status = lock_buddy("pjsua_buddy_update_pres()", buddy_id, &lck, 0);
  561. if (status != PJ_SUCCESS)
  562. return status;
  563. PJ_LOG(4,(THIS_FILE, "Buddy %d: updating presence..", buddy_id));
  564. pj_log_push_indent();
  565. /* Is this an unsubscribe request? */
  566. if (!lck.buddy->monitor) {
  567. unsubscribe_buddy(buddy_id, PJ_TRUE);
  568. unlock_buddy(&lck);
  569. pj_log_pop_indent();
  570. return PJ_SUCCESS;
  571. }
  572. /* Ignore if presence is already active for the buddy */
  573. if (lck.buddy->sub) {
  574. unlock_buddy(&lck);
  575. pj_log_pop_indent();
  576. return PJ_SUCCESS;
  577. }
  578. /* Initiate presence subscription */
  579. subscribe_buddy(buddy_id, PJ_TRUE);
  580. unlock_buddy(&lck);
  581. pj_log_pop_indent();
  582. return PJ_SUCCESS;
  583. }
  584. /*
  585. * Update buddy's dlg event.
  586. */
  587. PJ_DEF(pj_status_t) pjsua_buddy_update_dlg_event(pjsua_buddy_id buddy_id)
  588. {
  589. struct buddy_lock lck;
  590. pj_status_t status;
  591. PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
  592. status = lock_buddy("pjsua_buddy_update_dlg_event()", buddy_id, &lck, 0);
  593. if (status != PJ_SUCCESS)
  594. return status;
  595. PJ_LOG(4,(THIS_FILE, "Buddy %d: updating dialog event..", buddy_id));
  596. pj_log_push_indent();
  597. /* Is this an unsubscribe request? */
  598. if (!lck.buddy->monitor) {
  599. unsubscribe_buddy(buddy_id, PJ_FALSE);
  600. unlock_buddy(&lck);
  601. pj_log_pop_indent();
  602. return PJ_SUCCESS;
  603. }
  604. /* Ignore if dialog event is already active for the buddy */
  605. if (lck.buddy->sub) {
  606. unlock_buddy(&lck);
  607. pj_log_pop_indent();
  608. return PJ_SUCCESS;
  609. }
  610. /* Initiate dialog event subscription */
  611. subscribe_buddy(buddy_id, PJ_FALSE);
  612. unlock_buddy(&lck);
  613. pj_log_pop_indent();
  614. return PJ_SUCCESS;
  615. }
  616. /*
  617. * Dump presence subscriptions to log file.
  618. */
  619. PJ_DEF(void) pjsua_pres_dump(pj_bool_t verbose)
  620. {
  621. unsigned acc_id;
  622. unsigned i;
  623. PJSUA_LOCK();
  624. /*
  625. * When no detail is required, just dump number of server and client
  626. * subscriptions.
  627. */
  628. if (verbose == PJ_FALSE) {
  629. int count = 0;
  630. for (acc_id=0; acc_id<PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) {
  631. if (!pjsua_var.acc[acc_id].valid)
  632. continue;
  633. if (!pj_list_empty(&pjsua_var.acc[acc_id].pres_srv_list)) {
  634. struct pjsua_srv_pres *uapres;
  635. uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
  636. while (uapres != &pjsua_var.acc[acc_id].pres_srv_list) {
  637. ++count;
  638. uapres = uapres->next;
  639. }
  640. }
  641. }
  642. PJ_LOG(3,(THIS_FILE, "Number of server/UAS subscriptions: %d",
  643. count));
  644. count = 0;
  645. for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
  646. if (pjsua_var.buddy[i].uri.slen == 0)
  647. continue;
  648. if (pjsua_var.buddy[i].sub) {
  649. ++count;
  650. }
  651. }
  652. PJ_LOG(3,(THIS_FILE, "Number of client/UAC subscriptions: %d",
  653. count));
  654. PJSUA_UNLOCK();
  655. return;
  656. }
  657. /*
  658. * Dumping all server (UAS) subscriptions
  659. */
  660. PJ_LOG(3,(THIS_FILE, "Dumping pjsua server subscriptions:"));
  661. for (acc_id=0; acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) {
  662. if (!pjsua_var.acc[acc_id].valid)
  663. continue;
  664. PJ_LOG(3,(THIS_FILE, " %.*s",
  665. (int)pjsua_var.acc[acc_id].cfg.id.slen,
  666. pjsua_var.acc[acc_id].cfg.id.ptr));
  667. if (pj_list_empty(&pjsua_var.acc[acc_id].pres_srv_list)) {
  668. PJ_LOG(3,(THIS_FILE, " - none - "));
  669. } else {
  670. struct pjsua_srv_pres *uapres;
  671. uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
  672. while (uapres != &pjsua_var.acc[acc_id].pres_srv_list) {
  673. PJ_LOG(3,(THIS_FILE, " %10s %s",
  674. pjsip_evsub_get_state_name(uapres->sub),
  675. uapres->remote));
  676. uapres = uapres->next;
  677. }
  678. }
  679. }
  680. /*
  681. * Dumping all client (UAC) subscriptions
  682. */
  683. PJ_LOG(3,(THIS_FILE, "Dumping pjsua client subscriptions:"));
  684. if (pjsua_var.buddy_cnt == 0) {
  685. PJ_LOG(3,(THIS_FILE, " - no buddy list - "));
  686. } else {
  687. for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
  688. if (pjsua_var.buddy[i].uri.slen == 0)
  689. continue;
  690. if (pjsua_var.buddy[i].sub) {
  691. PJ_LOG(3,(THIS_FILE, " %10s %.*s",
  692. pjsip_evsub_get_state_name(pjsua_var.buddy[i].sub),
  693. (int)pjsua_var.buddy[i].uri.slen,
  694. pjsua_var.buddy[i].uri.ptr));
  695. } else {
  696. PJ_LOG(3,(THIS_FILE, " %10s %.*s",
  697. "(null)",
  698. (int)pjsua_var.buddy[i].uri.slen,
  699. pjsua_var.buddy[i].uri.ptr));
  700. }
  701. }
  702. }
  703. PJSUA_UNLOCK();
  704. }
  705. /***************************************************************************
  706. * Server subscription.
  707. */
  708. /* Proto */
  709. static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata);
  710. /* The module instance. */
  711. static pjsip_module mod_pjsua_pres =
  712. {
  713. NULL, NULL, /* prev, next. */
  714. { "mod-pjsua-pres", 14 }, /* Name. */
  715. -1, /* Id */
  716. PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
  717. NULL, /* load() */
  718. NULL, /* start() */
  719. NULL, /* stop() */
  720. NULL, /* unload() */
  721. &pres_on_rx_request, /* on_rx_request() */
  722. NULL, /* on_rx_response() */
  723. NULL, /* on_tx_request. */
  724. NULL, /* on_tx_response() */
  725. NULL, /* on_tsx_state() */
  726. };
  727. /* Callback called when *server* subscription state has changed. */
  728. static void pres_evsub_on_srv_state( pjsip_evsub *sub, pjsip_event *event)
  729. {
  730. pjsua_srv_pres *uapres;
  731. PJ_UNUSED_ARG(event);
  732. PJSUA_LOCK();
  733. uapres = (pjsua_srv_pres*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
  734. if (uapres) {
  735. pjsip_evsub_state state;
  736. PJ_LOG(4,(THIS_FILE, "Server subscription to %s is %s",
  737. uapres->remote, pjsip_evsub_get_state_name(sub)));
  738. pj_log_push_indent();
  739. state = pjsip_evsub_get_state(sub);
  740. if (pjsua_var.ua_cfg.cb.on_srv_subscribe_state) {
  741. pj_str_t from;
  742. from = uapres->dlg->remote.info_str;
  743. (*pjsua_var.ua_cfg.cb.on_srv_subscribe_state)(uapres->acc_id,
  744. uapres, &from,
  745. state, event);
  746. }
  747. if (state == PJSIP_EVSUB_STATE_TERMINATED) {
  748. pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
  749. pj_list_erase(uapres);
  750. }
  751. pj_log_pop_indent();
  752. }
  753. PJSUA_UNLOCK();
  754. }
  755. /* This is called when request is received.
  756. * We need to check for incoming SUBSCRIBE request.
  757. */
  758. static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata)
  759. {
  760. int acc_id;
  761. pjsua_acc *acc;
  762. pj_str_t contact;
  763. pjsip_method *req_method = &rdata->msg_info.msg->line.req.method;
  764. pjsua_srv_pres *uapres;
  765. pjsip_evsub *sub;
  766. pjsip_evsub_user pres_cb;
  767. pjsip_dialog *dlg;
  768. pjsip_status_code st_code;
  769. pj_str_t reason;
  770. pjsip_expires_hdr *expires_hdr;
  771. pjsua_msg_data msg_data;
  772. pjsip_tpselector tp_sel;
  773. pj_status_t status;
  774. if (pjsip_method_cmp(req_method, pjsip_get_subscribe_method()) != 0)
  775. return PJ_FALSE;
  776. /* Incoming SUBSCRIBE: */
  777. /* Don't want to accept the request if shutdown is in progress */
  778. if (pjsua_var.thread_quit_flag) {
  779. pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
  780. PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL,
  781. NULL, NULL);
  782. return PJ_TRUE;
  783. }
  784. PJSUA_LOCK();
  785. /* Find which account for the incoming request. */
  786. acc_id = pjsua_acc_find_for_incoming(rdata);
  787. if (acc_id == PJSUA_INVALID_ID) {
  788. PJ_LOG(2, (THIS_FILE,
  789. "Unable to process incoming message %s "
  790. "due to no available account",
  791. pjsip_rx_data_get_info(rdata)));
  792. PJSUA_UNLOCK();
  793. pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
  794. PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL,
  795. NULL, NULL);
  796. pj_log_pop_indent();
  797. return PJ_TRUE;
  798. }
  799. acc = &pjsua_var.acc[acc_id];
  800. PJ_LOG(4,(THIS_FILE, "Creating server subscription, using account %d",
  801. acc_id));
  802. pj_log_push_indent();
  803. /* Create suitable Contact header */
  804. if (acc->contact.slen) {
  805. contact = acc->contact;
  806. } else {
  807. status = pjsua_acc_create_uas_contact(rdata->tp_info.pool, &contact,
  808. acc_id, rdata);
  809. if (status != PJ_SUCCESS) {
  810. pjsua_perror(THIS_FILE, "Unable to generate Contact header",
  811. status);
  812. PJSUA_UNLOCK();
  813. pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL,
  814. NULL, NULL);
  815. pj_log_pop_indent();
  816. return PJ_TRUE;
  817. }
  818. }
  819. /* Create UAS dialog: */
  820. status = pjsip_dlg_create_uas_and_inc_lock(pjsip_ua_instance(), rdata,
  821. &contact, &dlg);
  822. if (status != PJ_SUCCESS) {
  823. pjsua_perror(THIS_FILE,
  824. "Unable to create UAS dialog for subscription",
  825. status);
  826. PJSUA_UNLOCK();
  827. pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL,
  828. NULL, NULL);
  829. pj_log_pop_indent();
  830. return PJ_TRUE;
  831. }
  832. if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) {
  833. pjsip_dlg_set_via_sent_by(dlg, &acc->via_addr, acc->via_tp);
  834. } else if (!pjsua_sip_acc_is_using_stun(acc_id) &&
  835. !pjsua_sip_acc_is_using_upnp(acc_id))
  836. {
  837. /* Choose local interface to use in Via if acc is not using
  838. * STUN nor UPnP. See https://github.com/pjsip/pjproject/issues/1412
  839. */
  840. char target_buf[PJSIP_MAX_URL_SIZE];
  841. pj_str_t target;
  842. pjsip_host_port via_addr;
  843. const void *via_tp;
  844. target.ptr = target_buf;
  845. target.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
  846. dlg->target,
  847. target_buf, sizeof(target_buf));
  848. if (target.slen < 0) target.slen = 0;
  849. if (pjsua_acc_get_uac_addr(acc_id, dlg->pool, &target,
  850. &via_addr, NULL, NULL,
  851. &via_tp) == PJ_SUCCESS)
  852. {
  853. pjsip_dlg_set_via_sent_by(dlg, &via_addr,
  854. (pjsip_transport*)via_tp);
  855. }
  856. }
  857. /* Set credentials and preference. */
  858. pjsip_auth_clt_set_credentials(&dlg->auth_sess, acc->cred_cnt, acc->cred);
  859. pjsip_auth_clt_set_prefs(&dlg->auth_sess, &acc->cfg.auth_pref);
  860. /* Init callback: */
  861. pj_bzero(&pres_cb, sizeof(pres_cb));
  862. pres_cb.on_evsub_state = &pres_evsub_on_srv_state;
  863. /* Create server presence subscription: */
  864. status = pjsip_pres_create_uas( dlg, &pres_cb, rdata, &sub);
  865. if (status != PJ_SUCCESS) {
  866. int code = PJSIP_ERRNO_TO_SIP_STATUS(status);
  867. pjsip_tx_data *tdata;
  868. pjsua_perror(THIS_FILE, "Unable to create server subscription",
  869. status);
  870. if (code==599 || code > 699 || code < 300) {
  871. code = 400;
  872. }
  873. status = pjsip_dlg_create_response(dlg, rdata, code, NULL, &tdata);
  874. if (status == PJ_SUCCESS) {
  875. status = pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata),
  876. tdata);
  877. }
  878. pjsip_dlg_dec_lock(dlg);
  879. PJSUA_UNLOCK();
  880. pj_log_pop_indent();
  881. return PJ_TRUE;
  882. }
  883. /* Subscription has been created, decrement & release dlg lock */
  884. pjsip_dlg_dec_lock(dlg);
  885. /* Set dialog's transport based on acc's config. */
  886. pjsua_init_tpselector(acc_id, &tp_sel);
  887. pjsip_dlg_set_transport(dlg, &tp_sel);
  888. /* Attach our data to the subscription: */
  889. uapres = PJ_POOL_ALLOC_T(dlg->pool, pjsua_srv_pres);
  890. uapres->sub = sub;
  891. uapres->remote = (char*) pj_pool_alloc(dlg->pool, PJSIP_MAX_URL_SIZE);
  892. uapres->acc_id = acc_id;
  893. uapres->dlg = dlg;
  894. status = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, dlg->remote.info->uri,
  895. uapres->remote, PJSIP_MAX_URL_SIZE);
  896. if (status < 1)
  897. pj_ansi_strxcpy(uapres->remote, "<-- url is too long-->",
  898. PJSIP_MAX_URL_SIZE);
  899. else
  900. uapres->remote[status] = '\0';
  901. pjsip_evsub_add_header(sub, &acc->cfg.sub_hdr_list);
  902. pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, uapres);
  903. /* Add server subscription to the list: */
  904. pj_list_push_back(&pjsua_var.acc[acc_id].pres_srv_list, uapres);
  905. /* Capture the value of Expires header. */
  906. expires_hdr = (pjsip_expires_hdr*)
  907. pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES,
  908. NULL);
  909. if (expires_hdr)
  910. uapres->expires = expires_hdr->ivalue;
  911. else
  912. uapres->expires = PJSIP_EXPIRES_NOT_SPECIFIED;
  913. st_code = (pjsip_status_code)200;
  914. reason = pj_str("OK");
  915. pjsua_msg_data_init(&msg_data);
  916. /* Notify application callback, if any */
  917. if (pjsua_var.ua_cfg.cb.on_incoming_subscribe) {
  918. pjsua_buddy_id buddy_id;
  919. buddy_id = find_buddy(rdata->msg_info.from->uri);
  920. (*pjsua_var.ua_cfg.cb.on_incoming_subscribe)(acc_id, uapres, buddy_id,
  921. &dlg->remote.info_str,
  922. rdata, &st_code, &reason,
  923. &msg_data);
  924. }
  925. /* Handle rejection case */
  926. if (st_code >= 300) {
  927. pjsip_tx_data *tdata;
  928. /* Create response */
  929. status = pjsip_dlg_create_response(dlg, rdata, st_code,
  930. &reason, &tdata);
  931. if (status != PJ_SUCCESS) {
  932. pjsua_perror(THIS_FILE, "Error creating response", status);
  933. pj_list_erase(uapres);
  934. pjsip_pres_terminate(sub, PJ_FALSE);
  935. PJSUA_UNLOCK();
  936. pj_log_pop_indent();
  937. return PJ_FALSE;
  938. }
  939. /* Add header list, if any */
  940. pjsua_process_msg_data(tdata, &msg_data);
  941. /* Send the response */
  942. status = pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata),
  943. tdata);
  944. if (status != PJ_SUCCESS) {
  945. pjsua_perror(THIS_FILE, "Error sending response", status);
  946. /* This is not fatal */
  947. }
  948. /* Terminate presence subscription */
  949. pj_list_erase(uapres);
  950. pjsip_pres_terminate(sub, PJ_FALSE);
  951. PJSUA_UNLOCK();
  952. pj_log_pop_indent();
  953. return PJ_TRUE;
  954. }
  955. /* Create and send 2xx response to the SUBSCRIBE request: */
  956. status = pjsip_pres_accept(sub, rdata, st_code, &msg_data.hdr_list);
  957. if (status != PJ_SUCCESS) {
  958. pjsua_perror(THIS_FILE, "Unable to accept presence subscription",
  959. status);
  960. pj_list_erase(uapres);
  961. pjsip_pres_terminate(sub, PJ_FALSE);
  962. PJSUA_UNLOCK();
  963. pj_log_pop_indent();
  964. return PJ_FALSE;
  965. }
  966. /* If code is 200, send NOTIFY now */
  967. if (st_code == 200) {
  968. pjsua_pres_notify(acc_id, uapres, PJSIP_EVSUB_STATE_ACTIVE,
  969. NULL, NULL, PJ_TRUE, &msg_data);
  970. }
  971. /* Done: */
  972. PJSUA_UNLOCK();
  973. pj_log_pop_indent();
  974. return PJ_TRUE;
  975. }
  976. /*
  977. * Send NOTIFY.
  978. */
  979. PJ_DEF(pj_status_t) pjsua_pres_notify( pjsua_acc_id acc_id,
  980. pjsua_srv_pres *srv_pres,
  981. pjsip_evsub_state ev_state,
  982. const pj_str_t *state_str,
  983. const pj_str_t *reason,
  984. pj_bool_t with_body,
  985. const pjsua_msg_data *msg_data)
  986. {
  987. pjsua_acc *acc;
  988. pjsip_pres_status pres_status;
  989. pjsua_buddy_id buddy_id;
  990. pjsip_tx_data *tdata;
  991. pj_status_t status;
  992. /* Check parameters */
  993. PJ_ASSERT_RETURN(acc_id!=-1 && srv_pres, PJ_EINVAL);
  994. /* Check that account ID is valid */
  995. PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
  996. PJ_EINVAL);
  997. /* Check that account is valid */
  998. PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
  999. PJ_LOG(4,(THIS_FILE, "Acc %d: sending NOTIFY for srv_pres=0x%p..",
  1000. acc_id, srv_pres));
  1001. pj_log_push_indent();
  1002. PJSUA_LOCK();
  1003. acc = &pjsua_var.acc[acc_id];
  1004. /* Check that the server presence subscription is still valid */
  1005. if (pj_list_find_node(&acc->pres_srv_list, srv_pres) == NULL) {
  1006. /* Subscription has been terminated */
  1007. PJSUA_UNLOCK();
  1008. pj_log_pop_indent();
  1009. return PJ_EINVALIDOP;
  1010. }
  1011. /* Set our online status: */
  1012. pj_bzero(&pres_status, sizeof(pres_status));
  1013. pres_status.info_cnt = 1;
  1014. pres_status.info[0].basic_open = acc->online_status;
  1015. pres_status.info[0].id = acc->cfg.pidf_tuple_id;
  1016. //Both pjsua_var.local_uri and pjsua_var.contact_uri are enclosed in "<" and ">"
  1017. //causing XML parsing to fail.
  1018. //pres_status.info[0].contact = pjsua_var.local_uri;
  1019. /* add RPID information */
  1020. pj_memcpy(&pres_status.info[0].rpid, &acc->rpid,
  1021. sizeof(pjrpid_element));
  1022. pjsip_pres_set_status(srv_pres->sub, &pres_status);
  1023. /* Check expires value. If it's zero, send our presense state but
  1024. * set subscription state to TERMINATED.
  1025. */
  1026. if (srv_pres->expires == 0)
  1027. ev_state = PJSIP_EVSUB_STATE_TERMINATED;
  1028. /* Create and send the NOTIFY to active subscription: */
  1029. status = pjsip_pres_notify(srv_pres->sub, ev_state, state_str,
  1030. reason, &tdata);
  1031. if (status == PJ_SUCCESS) {
  1032. /* Force removal of message body if msg_body==FALSE */
  1033. if (!with_body) {
  1034. tdata->msg->body = NULL;
  1035. }
  1036. pjsua_process_msg_data(tdata, msg_data);
  1037. status = pjsip_pres_send_request( srv_pres->sub, tdata);
  1038. }
  1039. if (status != PJ_SUCCESS) {
  1040. pjsua_perror(THIS_FILE, "Unable to create/send NOTIFY",
  1041. status);
  1042. pj_list_erase(srv_pres);
  1043. pjsip_pres_terminate(srv_pres->sub, PJ_FALSE);
  1044. PJSUA_UNLOCK();
  1045. pj_log_pop_indent();
  1046. return status;
  1047. }
  1048. /* Subscribe to buddy's presence if we're not subscribed */
  1049. buddy_id = find_buddy(srv_pres->dlg->remote.info->uri);
  1050. if (buddy_id != PJSUA_INVALID_ID) {
  1051. pjsua_buddy *b = &pjsua_var.buddy[buddy_id];
  1052. if (b->monitor && b->sub == NULL) {
  1053. PJ_LOG(4,(THIS_FILE, "Received SUBSCRIBE from buddy %d, "
  1054. "activating outgoing subscription", buddy_id));
  1055. subscribe_buddy(buddy_id, PJ_TRUE);
  1056. }
  1057. }
  1058. PJSUA_UNLOCK();
  1059. pj_log_pop_indent();
  1060. return PJ_SUCCESS;
  1061. }
  1062. /*
  1063. * Client presence publication callback.
  1064. */
  1065. static void publish_cb(struct pjsip_publishc_cbparam *param)
  1066. {
  1067. pjsua_acc *acc = (pjsua_acc*) param->token;
  1068. if (param->code/100 != 2 || param->status != PJ_SUCCESS) {
  1069. pjsip_publishc_destroy(param->pubc);
  1070. acc->publish_sess = NULL;
  1071. if (param->status != PJ_SUCCESS) {
  1072. char errmsg[PJ_ERR_MSG_SIZE];
  1073. pj_strerror(param->status, errmsg, sizeof(errmsg));
  1074. PJ_LOG(1,(THIS_FILE,
  1075. "Client publication (PUBLISH) failed, status=%d, msg=%s",
  1076. param->status, errmsg));
  1077. } else if (param->code == 412) {
  1078. /* 412 (Conditional Request Failed)
  1079. * The PUBLISH refresh has failed, retry with new one.
  1080. */
  1081. pjsua_pres_init_publish_acc(acc->index);
  1082. } else {
  1083. PJ_LOG(1,(THIS_FILE,
  1084. "Client publication (PUBLISH) failed (%d/%.*s)",
  1085. param->code, (int)param->reason.slen,
  1086. param->reason.ptr));
  1087. }
  1088. } else {
  1089. if (param->expiration < 1) {
  1090. /* Could happen if server "forgot" to include Expires header
  1091. * in the response. We will not renew, so destroy the pubc.
  1092. */
  1093. pjsip_publishc_destroy(param->pubc);
  1094. acc->publish_sess = NULL;
  1095. }
  1096. }
  1097. }
  1098. /*
  1099. * Send PUBLISH request.
  1100. */
  1101. static pj_status_t send_publish(int acc_id, pj_bool_t active)
  1102. {
  1103. pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
  1104. pjsua_acc *acc = &pjsua_var.acc[acc_id];
  1105. pjsip_pres_status pres_status;
  1106. pjsip_tx_data *tdata;
  1107. pj_status_t status;
  1108. PJ_LOG(5,(THIS_FILE, "Acc %d: sending %sPUBLISH..",
  1109. acc_id, (active ? "" : "un-")));
  1110. pj_log_push_indent();
  1111. /* Create PUBLISH request */
  1112. if (active) {
  1113. char *bpos;
  1114. pj_str_t entity;
  1115. status = pjsip_publishc_publish(acc->publish_sess, PJ_TRUE, &tdata);
  1116. if (status != PJ_SUCCESS) {
  1117. pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status);
  1118. goto on_error;
  1119. }
  1120. /* Set our online status: */
  1121. pj_bzero(&pres_status, sizeof(pres_status));
  1122. pres_status.info_cnt = 1;
  1123. pres_status.info[0].basic_open = acc->online_status;
  1124. pres_status.info[0].id = acc->cfg.pidf_tuple_id;
  1125. /* .. including RPID information */
  1126. pj_memcpy(&pres_status.info[0].rpid, &acc->rpid,
  1127. sizeof(pjrpid_element));
  1128. /* Be careful not to send PIDF with presence entity ID containing
  1129. * "<" character.
  1130. */
  1131. if ((bpos=pj_strchr(&acc_cfg->id, '<')) != NULL) {
  1132. char *epos = pj_strchr(&acc_cfg->id, '>');
  1133. if (epos - bpos < 2) {
  1134. pj_assert(!"Unexpected invalid URI");
  1135. status = PJSIP_EINVALIDURI;
  1136. goto on_error;
  1137. }
  1138. entity.ptr = bpos+1;
  1139. entity.slen = epos - bpos - 1;
  1140. } else {
  1141. entity = acc_cfg->id;
  1142. }
  1143. /* Create and add PIDF message body */
  1144. status = pjsip_pres_create_pidf(tdata->pool, &pres_status,
  1145. &entity, &tdata->msg->body);
  1146. if (status != PJ_SUCCESS) {
  1147. pjsua_perror(THIS_FILE, "Error creating PIDF for PUBLISH request",
  1148. status);
  1149. pjsip_tx_data_dec_ref(tdata);
  1150. goto on_error;
  1151. }
  1152. } else {
  1153. status = pjsip_publishc_unpublish(acc->publish_sess, &tdata);
  1154. if (status != PJ_SUCCESS) {
  1155. pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status);
  1156. goto on_error;
  1157. }
  1158. }
  1159. /* Add headers etc */
  1160. pjsua_process_msg_data(tdata, NULL);
  1161. /* Set Via sent-by */
  1162. if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) {
  1163. pjsip_publishc_set_via_sent_by(acc->publish_sess, &acc->via_addr,
  1164. acc->via_tp);
  1165. } else if (!pjsua_sip_acc_is_using_stun(acc_id) &&
  1166. !pjsua_sip_acc_is_using_upnp(acc_id))
  1167. {
  1168. /* Choose local interface to use in Via if acc is not using
  1169. * STUN nor UPnP. See https://github.com/pjsip/pjproject/issues/1412
  1170. */
  1171. pjsip_host_port via_addr;
  1172. const void *via_tp;
  1173. if (pjsua_acc_get_uac_addr(acc_id, acc->pool, &acc_cfg->id,
  1174. &via_addr, NULL, NULL,
  1175. &via_tp) == PJ_SUCCESS)
  1176. {
  1177. pjsip_publishc_set_via_sent_by(acc->publish_sess, &via_addr,
  1178. (pjsip_transport*)via_tp);
  1179. }
  1180. }
  1181. /* Send the PUBLISH request */
  1182. status = pjsip_publishc_send(acc->publish_sess, tdata);
  1183. if (status == PJ_EPENDING) {
  1184. PJ_LOG(3,(THIS_FILE, "Previous request is in progress, "
  1185. "PUBLISH request is queued"));
  1186. } else if (status != PJ_SUCCESS) {
  1187. pjsua_perror(THIS_FILE, "Error sending PUBLISH request", status);
  1188. goto on_error;
  1189. }
  1190. acc->publish_state = acc->online_status;
  1191. pj_log_pop_indent();
  1192. return PJ_SUCCESS;
  1193. on_error:
  1194. if (acc->publish_sess) {
  1195. pjsip_publishc_destroy(acc->publish_sess);
  1196. acc->publish_sess = NULL;
  1197. }
  1198. pj_log_pop_indent();
  1199. return status;
  1200. }
  1201. /* Create client publish session */
  1202. pj_status_t pjsua_pres_init_publish_acc(int acc_id)
  1203. {
  1204. const pj_str_t STR_PRESENCE = { "presence", 8 };
  1205. pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
  1206. pjsua_acc *acc = &pjsua_var.acc[acc_id];
  1207. pj_status_t status;
  1208. /* Create and init client publication session */
  1209. if (acc_cfg->publish_enabled) {
  1210. /* Create client publication */
  1211. status = pjsip_publishc_create(pjsua_var.endpt, &acc_cfg->publish_opt,
  1212. acc, &publish_cb,
  1213. &acc->publish_sess);
  1214. if (status != PJ_SUCCESS) {
  1215. acc->publish_sess = NULL;
  1216. return status;
  1217. }
  1218. /* Initialize client publication */
  1219. status = pjsip_publishc_init(acc->publish_sess, &STR_PRESENCE,
  1220. &acc_cfg->id, &acc_cfg->id,
  1221. &acc_cfg->id,
  1222. PJSUA_PUBLISH_EXPIRATION);
  1223. if (status != PJ_SUCCESS) {
  1224. acc->publish_sess = NULL;
  1225. return status;
  1226. }
  1227. /* Add credential for authentication */
  1228. if (acc->cred_cnt) {
  1229. pjsip_publishc_set_credentials(acc->publish_sess, acc->cred_cnt,
  1230. acc->cred);
  1231. }
  1232. /* Set route-set */
  1233. pjsip_publishc_set_route_set(acc->publish_sess, &acc->route_set);
  1234. /* Send initial PUBLISH request */
  1235. if (acc->online_status != 0) {
  1236. status = send_publish(acc_id, PJ_TRUE);
  1237. if (status != PJ_SUCCESS)
  1238. return status;
  1239. }
  1240. } else {
  1241. acc->publish_sess = NULL;
  1242. }
  1243. return PJ_SUCCESS;
  1244. }
  1245. /* Init presence for account */
  1246. pj_status_t pjsua_pres_init_acc(int acc_id)
  1247. {
  1248. pjsua_acc *acc = &pjsua_var.acc[acc_id];
  1249. /* Init presence subscription */
  1250. pj_list_init(&acc->pres_srv_list);
  1251. return PJ_SUCCESS;
  1252. }
  1253. /* Unpublish presence publication */
  1254. void pjsua_pres_unpublish(pjsua_acc *acc, unsigned flags)
  1255. {
  1256. if (acc->publish_sess) {
  1257. pjsua_acc_config *acc_cfg = &acc->cfg;
  1258. acc->online_status = PJ_FALSE;
  1259. acc_cfg->publish_enabled = PJ_FALSE;
  1260. if ((flags & PJSUA_DESTROY_NO_TX_MSG) == 0) {
  1261. send_publish(acc->index, PJ_FALSE);
  1262. }
  1263. /* By ticket #364, don't destroy the session yet (let the callback
  1264. destroy it)
  1265. if (acc->publish_sess) {
  1266. pjsip_publishc_destroy(acc->publish_sess);
  1267. acc->publish_sess = NULL;
  1268. }
  1269. */
  1270. }
  1271. }
  1272. /* Terminate server subscription for the account */
  1273. void pjsua_pres_delete_acc(int acc_id, unsigned flags)
  1274. {
  1275. pjsua_acc *acc = &pjsua_var.acc[acc_id];
  1276. pjsua_srv_pres *uapres;
  1277. uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
  1278. /* Notify all subscribers that we're no longer available */
  1279. while (uapres != &acc->pres_srv_list) {
  1280. pjsip_pres_status pres_status;
  1281. pj_str_t reason = { "noresource", 10 };
  1282. pjsua_srv_pres *next;
  1283. pjsip_tx_data *tdata;
  1284. next = uapres->next;
  1285. pjsip_pres_get_status(uapres->sub, &pres_status);
  1286. pres_status.info[0].basic_open = pjsua_var.acc[acc_id].online_status;
  1287. pjsip_pres_set_status(uapres->sub, &pres_status);
  1288. if ((flags & PJSUA_DESTROY_NO_TX_MSG) == 0) {
  1289. if (pjsip_pres_notify(uapres->sub,
  1290. PJSIP_EVSUB_STATE_TERMINATED, NULL,
  1291. &reason, &tdata)==PJ_SUCCESS)
  1292. {
  1293. pjsip_pres_send_request(uapres->sub, tdata);
  1294. }
  1295. } else {
  1296. pjsip_pres_terminate(uapres->sub, PJ_FALSE);
  1297. }
  1298. uapres = next;
  1299. }
  1300. /* Clear server presence subscription list because account might be reused
  1301. * later. */
  1302. pj_list_init(&acc->pres_srv_list);
  1303. /* Terminate presence publication, if any */
  1304. if (acc->cfg.publish_enabled)
  1305. pjsua_pres_unpublish(acc, flags);
  1306. }
  1307. /* Update server subscription (e.g. when our online status has changed) */
  1308. void pjsua_pres_update_acc(int acc_id, pj_bool_t force)
  1309. {
  1310. pjsua_acc *acc = &pjsua_var.acc[acc_id];
  1311. pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
  1312. pjsua_srv_pres *uapres;
  1313. uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
  1314. while (uapres != &acc->pres_srv_list) {
  1315. pjsip_pres_status pres_status;
  1316. pjsip_tx_data *tdata;
  1317. pjsip_pres_get_status(uapres->sub, &pres_status);
  1318. /* Only send NOTIFY once subscription is active. Some subscriptions
  1319. * may still be in NULL (when app is adding a new buddy while in the
  1320. * on_incoming_subscribe() callback) or PENDING (when user approval is
  1321. * being requested) state and we don't send NOTIFY to these subs until
  1322. * the user accepted the request.
  1323. */
  1324. if (pjsip_evsub_get_state(uapres->sub)==PJSIP_EVSUB_STATE_ACTIVE &&
  1325. (force || pres_status.info[0].basic_open != acc->online_status))
  1326. {
  1327. pres_status.info[0].basic_open = acc->online_status;
  1328. pj_memcpy(&pres_status.info[0].rpid, &acc->rpid,
  1329. sizeof(pjrpid_element));
  1330. pjsip_pres_set_status(uapres->sub, &pres_status);
  1331. if (pjsip_pres_current_notify(uapres->sub, &tdata)==PJ_SUCCESS) {
  1332. pjsua_process_msg_data(tdata, NULL);
  1333. pjsip_pres_send_request(uapres->sub, tdata);
  1334. }
  1335. }
  1336. uapres = uapres->next;
  1337. }
  1338. /* Send PUBLISH if required. We only do this when we have a PUBLISH
  1339. * session. If we don't have a PUBLISH session, then it could be
  1340. * that we're waiting until registration has completed before we
  1341. * send the first PUBLISH.
  1342. */
  1343. if (acc_cfg->publish_enabled && acc->publish_sess) {
  1344. if (force || acc->publish_state != acc->online_status) {
  1345. send_publish(acc_id, PJ_TRUE);
  1346. }
  1347. }
  1348. }
  1349. /***************************************************************************
  1350. * Client subscription.
  1351. */
  1352. static void buddy_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry)
  1353. {
  1354. pjsua_buddy *buddy = (pjsua_buddy*)entry->user_data;
  1355. PJ_UNUSED_ARG(th);
  1356. entry->id = PJ_FALSE;
  1357. pjsua_buddy_update_pres(buddy->index);
  1358. }
  1359. /* Reschedule subscription refresh timer or terminate the subscription
  1360. * refresh timer for the specified buddy.
  1361. */
  1362. static void buddy_resubscribe(pjsua_buddy *buddy, pj_bool_t resched,
  1363. unsigned msec_interval)
  1364. {
  1365. if (buddy->timer.id) {
  1366. pjsua_cancel_timer(&buddy->timer);
  1367. buddy->timer.id = PJ_FALSE;
  1368. }
  1369. if (resched) {
  1370. pj_time_val delay;
  1371. PJ_LOG(4,(THIS_FILE,
  1372. "Resubscribing buddy id %u in %u ms (reason: %.*s)",
  1373. buddy->index, msec_interval,
  1374. (int)buddy->term_reason.slen,
  1375. buddy->term_reason.ptr));
  1376. pj_timer_entry_init(&buddy->timer, 0, buddy, &buddy_timer_cb);
  1377. delay.sec = 0;
  1378. delay.msec = msec_interval;
  1379. pj_time_val_normalize(&delay);
  1380. if (pjsua_schedule_timer(&buddy->timer, &delay)==PJ_SUCCESS)
  1381. buddy->timer.id = PJ_TRUE;
  1382. }
  1383. }
  1384. /* Callback called when *client* subscription state has changed. */
  1385. static void pjsua_evsub_on_state( pjsip_evsub *sub, pjsip_event *event)
  1386. {
  1387. pjsua_buddy *buddy;
  1388. PJ_UNUSED_ARG(event);
  1389. /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
  1390. * a dialog attached to it, lock_buddy() will use the dialog
  1391. * lock, which we are currently holding!
  1392. */
  1393. buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
  1394. if (buddy) {
  1395. PJ_LOG(4,(THIS_FILE,
  1396. "%s subscription to %.*s is %s",
  1397. buddy->presence? "Presence": "Dialog event",
  1398. (int)pjsua_var.buddy[buddy->index].uri.slen,
  1399. pjsua_var.buddy[buddy->index].uri.ptr,
  1400. pjsip_evsub_get_state_name(sub)));
  1401. pj_log_push_indent();
  1402. if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
  1403. int resub_delay = -1;
  1404. if (buddy->term_reason.ptr == NULL) {
  1405. buddy->term_reason.ptr = (char*)
  1406. pj_pool_alloc(buddy->pool,
  1407. PJSUA_BUDDY_SUB_TERM_REASON_LEN);
  1408. }
  1409. pj_strncpy(&buddy->term_reason,
  1410. pjsip_evsub_get_termination_reason(sub),
  1411. PJSUA_BUDDY_SUB_TERM_REASON_LEN);
  1412. buddy->term_code = 200;
  1413. /* Determine whether to resubscribe automatically */
  1414. if (event && event->type==PJSIP_EVENT_TSX_STATE) {
  1415. const pjsip_transaction *tsx = event->body.tsx_state.tsx;
  1416. if (pjsip_method_cmp(&tsx->method,
  1417. pjsip_get_subscribe_method())==0)
  1418. {
  1419. buddy->term_code = tsx->status_code;
  1420. switch (tsx->status_code) {
  1421. case PJSIP_SC_CALL_TSX_DOES_NOT_EXIST:
  1422. /* 481: we refreshed too late? resubscribe
  1423. * immediately.
  1424. */
  1425. /* But this must only happen when the 481 is received
  1426. * on subscription refresh request. We MUST NOT try to
  1427. * resubscribe automatically if the 481 is received
  1428. * on the initial SUBSCRIBE (if server returns this
  1429. * response for some reason).
  1430. */
  1431. if (buddy->dlg->remote.contact)
  1432. resub_delay = 500;
  1433. break;
  1434. }
  1435. } else if (pjsip_method_cmp(&tsx->method,
  1436. pjsip_get_notify_method())==0)
  1437. {
  1438. if (pj_stricmp2(&buddy->term_reason, "deactivated")==0 ||
  1439. pj_stricmp2(&buddy->term_reason, "timeout")==0) {
  1440. /* deactivated: The subscription has been terminated,
  1441. * but the subscriber SHOULD retry immediately with
  1442. * a new subscription.
  1443. */
  1444. /* timeout: The subscription has been terminated
  1445. * because it was not refreshed before it expired.
  1446. * Clients MAY re-subscribe immediately. The
  1447. * "retry-after" parameter has no semantics for
  1448. * "timeout".
  1449. */
  1450. resub_delay = 500;
  1451. }
  1452. else if (pj_stricmp2(&buddy->term_reason, "probation")==0||
  1453. pj_stricmp2(&buddy->term_reason, "giveup")==0) {
  1454. /* probation: The subscription has been terminated,
  1455. * but the client SHOULD retry at some later time.
  1456. * If a "retry-after" parameter is also present, the
  1457. * client SHOULD wait at least the number of seconds
  1458. * specified by that parameter before attempting to re-
  1459. * subscribe.
  1460. */
  1461. /* giveup: The subscription has been terminated because
  1462. * the notifier could not obtain authorization in a
  1463. * timely fashion. If a "retry-after" parameter is
  1464. * also present, the client SHOULD wait at least the
  1465. * number of seconds specified by that parameter before
  1466. * attempting to re-subscribe; otherwise, the client
  1467. * MAY retry immediately, but will likely get put back
  1468. * into pending state.
  1469. */
  1470. const pjsip_sub_state_hdr *sub_hdr;
  1471. pj_str_t sub_state = { "Subscription-State", 18 };
  1472. const pjsip_msg *msg;
  1473. msg = event->body.tsx_state.src.rdata->msg_info.msg;
  1474. sub_hdr = (const pjsip_sub_state_hdr*)
  1475. pjsip_msg_find_hdr_by_name(msg, &sub_state,
  1476. NULL);
  1477. if (sub_hdr && sub_hdr->retry_after > 0)
  1478. resub_delay = sub_hdr->retry_after * 1000;
  1479. }
  1480. }
  1481. }
  1482. /* For other cases of subscription termination, if resubscribe
  1483. * timer is not set, schedule with default expiration (plus minus
  1484. * some random value, to avoid sending SUBSCRIBEs all at once)
  1485. */
  1486. if (resub_delay == -1) {
  1487. pj_assert(PJSUA_PRES_TIMER >= 3);
  1488. resub_delay = PJSUA_PRES_TIMER*1000 - 2500 + (pj_rand()%5000);
  1489. }
  1490. buddy_resubscribe(buddy, PJ_TRUE, resub_delay);
  1491. } else {
  1492. /* This will clear the last termination code/reason */
  1493. buddy->term_code = 0;
  1494. buddy->term_reason.slen = 0;
  1495. }
  1496. /* Call callbacks */
  1497. if (buddy->presence) {
  1498. if (pjsua_var.ua_cfg.cb.on_buddy_evsub_state) {
  1499. (*pjsua_var.ua_cfg.cb.on_buddy_evsub_state)(buddy->index, sub,
  1500. event);
  1501. }
  1502. if (pjsua_var.ua_cfg.cb.on_buddy_state)
  1503. (*pjsua_var.ua_cfg.cb.on_buddy_state)(buddy->index);
  1504. } else {
  1505. if (pjsua_var.ua_cfg.cb.on_buddy_evsub_dlg_event_state) {
  1506. (*pjsua_var.ua_cfg.cb.on_buddy_evsub_dlg_event_state)(
  1507. buddy->index, sub, event);
  1508. }
  1509. if (pjsua_var.ua_cfg.cb.on_buddy_dlg_event_state)
  1510. (*pjsua_var.ua_cfg.cb.on_buddy_dlg_event_state)(buddy->index);
  1511. }
  1512. /* Clear subscription */
  1513. if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
  1514. buddy->sub = NULL;
  1515. buddy->status.info_cnt = 0;
  1516. pj_bzero(&buddy->dlg_ev_status, sizeof(buddy->dlg_ev_status));
  1517. buddy->dlg = NULL;
  1518. pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
  1519. }
  1520. pj_log_pop_indent();
  1521. }
  1522. }
  1523. /* Callback when transaction state has changed. */
  1524. static void pjsua_evsub_on_tsx_state(pjsip_evsub *sub,
  1525. pjsip_transaction *tsx,
  1526. pjsip_event *event)
  1527. {
  1528. pjsua_buddy *buddy;
  1529. pjsip_contact_hdr *contact_hdr;
  1530. /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
  1531. * a dialog attached to it, lock_buddy() will use the dialog
  1532. * lock, which we are currently holding!
  1533. */
  1534. buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
  1535. if (!buddy) {
  1536. return;
  1537. }
  1538. /* We only use this to update buddy's Contact, when it's not
  1539. * set.
  1540. */
  1541. if (buddy->contact.slen != 0) {
  1542. /* Contact already set */
  1543. return;
  1544. }
  1545. /* Only care about 2xx response to outgoing SUBSCRIBE */
  1546. if (tsx->status_code/100 != 2 ||
  1547. tsx->role != PJSIP_UAC_ROLE ||
  1548. event->type != PJSIP_EVENT_RX_MSG ||
  1549. pjsip_method_cmp(&tsx->method, pjsip_get_subscribe_method())!=0)
  1550. {
  1551. return;
  1552. }
  1553. /* Find contact header. */
  1554. contact_hdr = (pjsip_contact_hdr*)
  1555. pjsip_msg_find_hdr(event->body.rx_msg.rdata->msg_info.msg,
  1556. PJSIP_H_CONTACT, NULL);
  1557. if (!contact_hdr || !contact_hdr->uri) {
  1558. return;
  1559. }
  1560. buddy->contact.ptr = (char*)
  1561. pj_pool_alloc(buddy->pool, PJSIP_MAX_URL_SIZE);
  1562. buddy->contact.slen = pjsip_uri_print( PJSIP_URI_IN_CONTACT_HDR,
  1563. contact_hdr->uri,
  1564. buddy->contact.ptr,
  1565. PJSIP_MAX_URL_SIZE);
  1566. if (buddy->contact.slen < 0)
  1567. buddy->contact.slen = 0;
  1568. }
  1569. /* Callback called when we receive NOTIFY */
  1570. static void pjsua_evsub_on_rx_notify(pjsip_evsub *sub,
  1571. pjsip_rx_data *rdata,
  1572. int *p_st_code,
  1573. pj_str_t **p_st_text,
  1574. pjsip_hdr *res_hdr,
  1575. pjsip_msg_body **p_body)
  1576. {
  1577. pjsua_buddy *buddy;
  1578. /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
  1579. * a dialog attached to it, lock_buddy() will use the dialog
  1580. * lock, which we are currently holding!
  1581. */
  1582. buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
  1583. if (buddy) {
  1584. /* Update our info. */
  1585. pjsip_pres_get_status(sub, &buddy->status);
  1586. }
  1587. /* The default is to send 200 response to NOTIFY.
  1588. * Just leave it there..
  1589. */
  1590. PJ_UNUSED_ARG(rdata);
  1591. PJ_UNUSED_ARG(p_st_code);
  1592. PJ_UNUSED_ARG(p_st_text);
  1593. PJ_UNUSED_ARG(res_hdr);
  1594. PJ_UNUSED_ARG(p_body);
  1595. }
  1596. /* Callback called when we receive dialog monitoring NOTIFY */
  1597. static void pjsua_evsub_on_rx_dlg_event_notify(pjsip_evsub *sub,
  1598. pjsip_rx_data *rdata,
  1599. int *p_st_code,
  1600. pj_str_t **p_st_text,
  1601. pjsip_hdr *res_hdr,
  1602. pjsip_msg_body **p_body)
  1603. {
  1604. pjsua_buddy *buddy;
  1605. /* Note: #937: no need to acquire PJSUA_LOCK here. Since the buddy has
  1606. * a dialog attached to it, lock_buddy() will use the dialog
  1607. * lock, which we are currently holding!
  1608. */
  1609. buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
  1610. if (buddy) {
  1611. /* Update our info. */
  1612. pjsip_dlg_event_get_status(sub, &buddy->dlg_ev_status);
  1613. }
  1614. /* The default is to send 200 response to NOTIFY.
  1615. * Just leave it there..
  1616. */
  1617. PJ_UNUSED_ARG(rdata);
  1618. PJ_UNUSED_ARG(p_st_code);
  1619. PJ_UNUSED_ARG(p_st_text);
  1620. PJ_UNUSED_ARG(res_hdr);
  1621. PJ_UNUSED_ARG(p_body);
  1622. }
  1623. static void subscribe_buddy(pjsua_buddy_id buddy_id,
  1624. pj_bool_t presence)
  1625. {
  1626. pjsip_evsub_user pres_callback;
  1627. pjsip_evsub_user dlg_event_callback;
  1628. pj_pool_t *tmp_pool = NULL;
  1629. pjsua_buddy *buddy;
  1630. int acc_id;
  1631. pjsua_acc *acc;
  1632. pj_str_t contact;
  1633. pjsip_tx_data *tdata;
  1634. pjsip_tpselector tp_sel;
  1635. pj_status_t status;
  1636. const char *sub_str = presence? "presence": "dialog event";
  1637. pjsip_dialog *dlg;
  1638. /* Event subscription callback. */
  1639. pj_bzero(&pres_callback, sizeof(pres_callback));
  1640. pres_callback.on_evsub_state = &pjsua_evsub_on_state;
  1641. pres_callback.on_tsx_state = &pjsua_evsub_on_tsx_state;
  1642. pres_callback.on_rx_notify = &pjsua_evsub_on_rx_notify;
  1643. /* Event subscription callback. */
  1644. pj_bzero(&dlg_event_callback, sizeof(dlg_event_callback));
  1645. dlg_event_callback.on_evsub_state = &pjsua_evsub_on_state;
  1646. dlg_event_callback.on_tsx_state = &pjsua_evsub_on_tsx_state;
  1647. dlg_event_callback.on_rx_notify = &pjsua_evsub_on_rx_dlg_event_notify;
  1648. buddy = &pjsua_var.buddy[buddy_id];
  1649. acc_id = pjsua_acc_find_for_outgoing(&buddy->uri);
  1650. acc = &pjsua_var.acc[acc_id];
  1651. PJ_LOG(4,(THIS_FILE, "Buddy %d: subscribing %s,using account %d..",
  1652. buddy_id, sub_str, acc_id));
  1653. pj_log_push_indent();
  1654. /* Generate suitable Contact header unless one is already set in
  1655. * the account
  1656. */
  1657. if (acc->contact.slen) {
  1658. contact = acc->contact;
  1659. } else {
  1660. tmp_pool = pjsua_pool_create("tmpbuddy", 512, 256);
  1661. status = pjsua_acc_create_uac_contact(tmp_pool, &contact,
  1662. acc_id, &buddy->uri);
  1663. if (status != PJ_SUCCESS) {
  1664. pjsua_perror(THIS_FILE, "Unable to generate Contact header",
  1665. status);
  1666. pj_pool_release(tmp_pool);
  1667. pj_log_pop_indent();
  1668. return;
  1669. }
  1670. }
  1671. /* Create UAC dialog */
  1672. status = pjsip_dlg_create_uac( pjsip_ua_instance(),
  1673. &acc->cfg.id,
  1674. &contact,
  1675. &buddy->uri,
  1676. NULL, &buddy->dlg);
  1677. if (status != PJ_SUCCESS) {
  1678. pjsua_perror(THIS_FILE, "Unable to create dialog",
  1679. status);
  1680. if (tmp_pool) pj_pool_release(tmp_pool);
  1681. pj_log_pop_indent();
  1682. return;
  1683. }
  1684. /* Increment the dialog's lock otherwise when presence session creation
  1685. * fails the dialog will be destroyed prematurely.
  1686. */
  1687. pjsip_dlg_inc_lock(buddy->dlg);
  1688. if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) {
  1689. pjsip_dlg_set_via_sent_by(buddy->dlg, &acc->via_addr, acc->via_tp);
  1690. } else if (!pjsua_sip_acc_is_using_stun(acc_id) &&
  1691. !pjsua_sip_acc_is_using_upnp(acc_id))
  1692. {
  1693. /* Choose local interface to use in Via if acc is not using
  1694. * STUN nor UPnP. See https://github.com/pjsip/pjproject/issues/1412
  1695. */
  1696. pjsip_host_port via_addr;
  1697. const void *via_tp;
  1698. if (pjsua_acc_get_uac_addr(acc_id, buddy->dlg->pool, &buddy->uri,
  1699. &via_addr, NULL, NULL,
  1700. &via_tp) == PJ_SUCCESS)
  1701. {
  1702. pjsip_dlg_set_via_sent_by(buddy->dlg, &via_addr,
  1703. (pjsip_transport*)via_tp);
  1704. }
  1705. }
  1706. if (presence) {
  1707. status = pjsip_pres_create_uac(buddy->dlg, &pres_callback,
  1708. PJSIP_EVSUB_NO_EVENT_ID, &buddy->sub);
  1709. } else {
  1710. status = pjsip_dlg_event_create_uac(buddy->dlg, &dlg_event_callback,
  1711. PJSIP_EVSUB_NO_EVENT_ID,
  1712. &buddy->sub);
  1713. }
  1714. if (status != PJ_SUCCESS) {
  1715. buddy->sub = NULL;
  1716. pjsua_perror(THIS_FILE, "Unable to create subscription client",
  1717. status);
  1718. /* This should destroy the dialog since there's no session
  1719. * referencing it
  1720. */
  1721. pjsip_dlg_dec_lock(buddy->dlg);
  1722. if (tmp_pool) pj_pool_release(tmp_pool);
  1723. pj_log_pop_indent();
  1724. return;
  1725. }
  1726. /* Set dialog's transport based on acc's config. */
  1727. pjsua_init_tpselector(acc_id, &tp_sel);
  1728. pjsip_dlg_set_transport(buddy->dlg, &tp_sel);
  1729. /* Set route-set */
  1730. if (!pj_list_empty(&acc->route_set)) {
  1731. pjsip_dlg_set_route_set(buddy->dlg, &acc->route_set);
  1732. }
  1733. /* Set credentials */
  1734. if (acc->cred_cnt) {
  1735. pjsip_auth_clt_set_credentials( &buddy->dlg->auth_sess,
  1736. acc->cred_cnt, acc->cred);
  1737. }
  1738. /* Set authentication preference */
  1739. pjsip_auth_clt_set_prefs(&buddy->dlg->auth_sess, &acc->cfg.auth_pref);
  1740. pjsip_evsub_add_header(buddy->sub, &acc->cfg.sub_hdr_list);
  1741. pjsip_evsub_set_mod_data(buddy->sub, pjsua_var.mod.id, buddy);
  1742. if (presence) {
  1743. status = pjsip_pres_initiate(buddy->sub, PJSIP_EXPIRES_NOT_SPECIFIED,
  1744. &tdata);
  1745. } else {
  1746. status = pjsip_dlg_event_initiate(buddy->sub,
  1747. PJSIP_EXPIRES_NOT_SPECIFIED, &tdata);
  1748. }
  1749. if (status != PJ_SUCCESS) {
  1750. pjsip_dlg_dec_lock(buddy->dlg);
  1751. pjsip_pres_terminate(buddy->sub, PJ_FALSE);
  1752. buddy->sub = NULL;
  1753. pjsua_perror(THIS_FILE, "Unable to create initial SUBSCRIBE",
  1754. status);
  1755. if (tmp_pool) pj_pool_release(tmp_pool);
  1756. pj_log_pop_indent();
  1757. return;
  1758. }
  1759. pjsua_process_msg_data(tdata, NULL);
  1760. /* Send request. Note that if the send operation fails sync-ly, e.g:
  1761. * gethostbyname() error, tsx callback may have been invoked which may
  1762. * get the subscription terminated and the buddy states reset, we need
  1763. * to be prepared for such scenario here.
  1764. */
  1765. dlg = buddy->dlg;
  1766. if (presence) {
  1767. status = pjsip_pres_send_request(buddy->sub, tdata);
  1768. } else {
  1769. status = pjsip_dlg_event_send_request(buddy->sub, tdata);
  1770. }
  1771. if (status != PJ_SUCCESS) {
  1772. pjsip_dlg_dec_lock(dlg);
  1773. if (buddy->sub)
  1774. pjsip_pres_terminate(buddy->sub, PJ_FALSE);
  1775. buddy->sub = NULL;
  1776. pjsua_perror(THIS_FILE, "Unable to send initial SUBSCRIBE",
  1777. status);
  1778. if (tmp_pool) pj_pool_release(tmp_pool);
  1779. pj_log_pop_indent();
  1780. return;
  1781. }
  1782. buddy->presence = presence;
  1783. pjsip_dlg_dec_lock(buddy->dlg);
  1784. if (tmp_pool) pj_pool_release(tmp_pool);
  1785. pj_log_pop_indent();
  1786. }
  1787. static void unsubscribe_buddy(pjsua_buddy_id buddy_id,
  1788. pj_bool_t presence)
  1789. {
  1790. pjsua_buddy *buddy;
  1791. pjsip_tx_data *tdata;
  1792. pj_status_t status;
  1793. const char *sub_str = presence? "presence": "dialog event";
  1794. buddy = &pjsua_var.buddy[buddy_id];
  1795. if (buddy->sub == NULL)
  1796. return;
  1797. if (pjsip_evsub_get_state(buddy->sub) == PJSIP_EVSUB_STATE_TERMINATED) {
  1798. buddy->sub = NULL;
  1799. return;
  1800. }
  1801. PJ_LOG(5,(THIS_FILE, "Buddy %d: unsubscribing %s..", buddy_id, sub_str));
  1802. pj_log_push_indent();
  1803. if (presence) {
  1804. status = pjsip_pres_initiate( buddy->sub, 0, &tdata);
  1805. } else {
  1806. status = pjsip_dlg_event_initiate( buddy->sub, 0, &tdata);
  1807. }
  1808. if (status == PJ_SUCCESS) {
  1809. pjsua_process_msg_data(tdata, NULL);
  1810. if (presence)
  1811. status = pjsip_pres_send_request( buddy->sub, tdata );
  1812. else
  1813. status = pjsip_dlg_event_send_request(buddy->sub, tdata);
  1814. }
  1815. if (status != PJ_SUCCESS && buddy->sub) {
  1816. if (presence) {
  1817. pjsip_pres_terminate(buddy->sub, PJ_FALSE);
  1818. } else {
  1819. pjsip_dlg_event_terminate(buddy->sub, PJ_FALSE);
  1820. }
  1821. buddy->sub = NULL;
  1822. pjsua_perror(THIS_FILE, "Unable to unsubscribe", status);
  1823. }
  1824. pj_log_pop_indent();
  1825. }
  1826. /* It does what it says.. */
  1827. static pj_status_t refresh_client_subscriptions(void)
  1828. {
  1829. unsigned i;
  1830. pj_status_t status;
  1831. for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
  1832. struct buddy_lock lck;
  1833. if (!pjsua_buddy_is_valid(i))
  1834. continue;
  1835. status = lock_buddy("refresh_client_subscriptions()", i, &lck, 0);
  1836. if (status != PJ_SUCCESS)
  1837. return status;
  1838. if (pjsua_var.buddy[i].monitor && !pjsua_var.buddy[i].sub) {
  1839. subscribe_buddy(i, PJ_TRUE);
  1840. } else if (!pjsua_var.buddy[i].monitor && pjsua_var.buddy[i].sub) {
  1841. unsubscribe_buddy(i, PJ_TRUE);
  1842. }
  1843. unlock_buddy(&lck);
  1844. }
  1845. return PJ_SUCCESS;
  1846. }
  1847. /***************************************************************************
  1848. * MWI
  1849. */
  1850. /* Callback called when *client* subscription state has changed. */
  1851. static void mwi_evsub_on_state( pjsip_evsub *sub, pjsip_event *event)
  1852. {
  1853. pjsua_acc *acc;
  1854. PJ_UNUSED_ARG(event);
  1855. /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
  1856. * a dialog attached to it, lock_buddy() will use the dialog
  1857. * lock, which we are currently holding!
  1858. */
  1859. acc = (pjsua_acc*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
  1860. if (!acc)
  1861. return;
  1862. PJ_LOG(4,(THIS_FILE,
  1863. "MWI subscription for %.*s is %s",
  1864. (int)acc->cfg.id.slen, acc->cfg.id.ptr,
  1865. pjsip_evsub_get_state_name(sub)));
  1866. /* Call callback */
  1867. if (pjsua_var.ua_cfg.cb.on_mwi_state) {
  1868. (*pjsua_var.ua_cfg.cb.on_mwi_state)(acc->index, sub);
  1869. }
  1870. if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
  1871. /* Clear subscription */
  1872. acc->mwi_dlg = NULL;
  1873. acc->mwi_sub = NULL;
  1874. pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
  1875. }
  1876. }
  1877. /* Callback called when we receive NOTIFY */
  1878. static void mwi_evsub_on_rx_notify(pjsip_evsub *sub,
  1879. pjsip_rx_data *rdata,
  1880. int *p_st_code,
  1881. pj_str_t **p_st_text,
  1882. pjsip_hdr *res_hdr,
  1883. pjsip_msg_body **p_body)
  1884. {
  1885. pjsua_mwi_info mwi_info;
  1886. pjsua_acc *acc;
  1887. PJ_UNUSED_ARG(p_st_code);
  1888. PJ_UNUSED_ARG(p_st_text);
  1889. PJ_UNUSED_ARG(res_hdr);
  1890. PJ_UNUSED_ARG(p_body);
  1891. acc = (pjsua_acc*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
  1892. if (!acc)
  1893. return;
  1894. /* Construct mwi_info */
  1895. pj_bzero(&mwi_info, sizeof(mwi_info));
  1896. mwi_info.evsub = sub;
  1897. mwi_info.rdata = rdata;
  1898. PJ_LOG(4,(THIS_FILE, "MWI got NOTIFY.."));
  1899. pj_log_push_indent();
  1900. /* Call callback */
  1901. if (pjsua_var.ua_cfg.cb.on_mwi_info) {
  1902. (*pjsua_var.ua_cfg.cb.on_mwi_info)(acc->index, &mwi_info);
  1903. }
  1904. pj_log_pop_indent();
  1905. }
  1906. /* Event subscription callback. */
  1907. static pjsip_evsub_user mwi_cb =
  1908. {
  1909. &mwi_evsub_on_state,
  1910. NULL, /* on_tsx_state: not interested */
  1911. NULL, /* on_rx_refresh: don't care about SUBSCRIBE refresh, unless
  1912. * we want to authenticate
  1913. */
  1914. &mwi_evsub_on_rx_notify,
  1915. NULL, /* on_client_refresh: Use default behaviour, which is to
  1916. * refresh client subscription. */
  1917. NULL, /* on_server_timeout: Use default behaviour, which is to send
  1918. * NOTIFY to terminate.
  1919. */
  1920. };
  1921. pj_status_t pjsua_start_mwi(pjsua_acc_id acc_id, pj_bool_t force_renew)
  1922. {
  1923. pjsua_acc *acc;
  1924. pj_pool_t *tmp_pool = NULL;
  1925. pj_str_t contact;
  1926. pjsip_tx_data *tdata;
  1927. pjsip_tpselector tp_sel;
  1928. pj_status_t status = PJ_SUCCESS;
  1929. PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc)
  1930. && pjsua_var.acc[acc_id].valid, PJ_EINVAL);
  1931. acc = &pjsua_var.acc[acc_id];
  1932. if (!acc->cfg.mwi_enabled || !acc->regc) {
  1933. if (acc->mwi_sub) {
  1934. /* Terminate MWI subscription */
  1935. pjsip_evsub *sub = acc->mwi_sub;
  1936. /* Detach sub from this account */
  1937. acc->mwi_sub = NULL;
  1938. acc->mwi_dlg = NULL;
  1939. pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
  1940. /* Unsubscribe */
  1941. status = pjsip_mwi_initiate(sub, 0, &tdata);
  1942. if (status == PJ_SUCCESS) {
  1943. status = pjsip_mwi_send_request(sub, tdata);
  1944. }
  1945. }
  1946. return status;
  1947. }
  1948. /* Subscription is already active */
  1949. if (acc->mwi_sub) {
  1950. if (!force_renew)
  1951. return PJ_SUCCESS;
  1952. /* Update MWI subscription */
  1953. pj_assert(acc->mwi_dlg);
  1954. pjsip_dlg_inc_lock(acc->mwi_dlg);
  1955. status = pjsip_mwi_initiate(acc->mwi_sub, acc->cfg.mwi_expires, &tdata);
  1956. if (status == PJ_SUCCESS) {
  1957. pjsua_process_msg_data(tdata, NULL);
  1958. status = pjsip_pres_send_request(acc->mwi_sub, tdata);
  1959. }
  1960. pjsip_dlg_dec_lock(acc->mwi_dlg);
  1961. return status;
  1962. }
  1963. PJ_LOG(4,(THIS_FILE, "Starting MWI subscription.."));
  1964. pj_log_push_indent();
  1965. /* Generate suitable Contact header unless one is already set in
  1966. * the account
  1967. */
  1968. if (acc->contact.slen) {
  1969. contact = acc->contact;
  1970. } else {
  1971. tmp_pool = pjsua_pool_create("tmpmwi", 512, 256);
  1972. status = pjsua_acc_create_uac_contact(tmp_pool, &contact,
  1973. acc->index, &acc->cfg.id);
  1974. if (status != PJ_SUCCESS) {
  1975. pjsua_perror(THIS_FILE, "Unable to generate Contact header",
  1976. status);
  1977. goto on_return;
  1978. }
  1979. }
  1980. /* Create UAC dialog */
  1981. status = pjsip_dlg_create_uac( pjsip_ua_instance(),
  1982. &acc->cfg.id,
  1983. &contact,
  1984. &acc->cfg.id,
  1985. NULL, &acc->mwi_dlg);
  1986. if (status != PJ_SUCCESS) {
  1987. pjsua_perror(THIS_FILE, "Unable to create dialog", status);
  1988. goto on_return;
  1989. }
  1990. /* Increment the dialog's lock otherwise when presence session creation
  1991. * fails the dialog will be destroyed prematurely.
  1992. */
  1993. pjsip_dlg_inc_lock(acc->mwi_dlg);
  1994. if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) {
  1995. pjsip_dlg_set_via_sent_by(acc->mwi_dlg, &acc->via_addr, acc->via_tp);
  1996. } else if (!pjsua_sip_acc_is_using_stun(acc_id) &&
  1997. !pjsua_sip_acc_is_using_upnp(acc_id))
  1998. {
  1999. /* Choose local interface to use in Via if acc is not using
  2000. * STUN nor UPnP. See https://github.com/pjsip/pjproject/issues/1412
  2001. */
  2002. pjsip_host_port via_addr;
  2003. const void *via_tp;
  2004. if (pjsua_acc_get_uac_addr(acc_id, acc->mwi_dlg->pool, &acc->cfg.id,
  2005. &via_addr, NULL, NULL,
  2006. &via_tp) == PJ_SUCCESS)
  2007. {
  2008. pjsip_dlg_set_via_sent_by(acc->mwi_dlg, &via_addr,
  2009. (pjsip_transport*)via_tp);
  2010. }
  2011. }
  2012. /* Create UAC subscription */
  2013. status = pjsip_mwi_create_uac(acc->mwi_dlg, &mwi_cb,
  2014. PJSIP_EVSUB_NO_EVENT_ID, &acc->mwi_sub);
  2015. if (status != PJ_SUCCESS) {
  2016. pjsua_perror(THIS_FILE, "Error creating MWI subscription", status);
  2017. pjsip_dlg_dec_lock(acc->mwi_dlg);
  2018. goto on_return;
  2019. }
  2020. /* Set dialog's transport based on acc's config. */
  2021. pjsua_init_tpselector(acc_id, &tp_sel);
  2022. pjsip_dlg_set_transport(acc->mwi_dlg, &tp_sel);
  2023. /* Set route-set */
  2024. if (!pj_list_empty(&acc->route_set)) {
  2025. pjsip_dlg_set_route_set(acc->mwi_dlg, &acc->route_set);
  2026. }
  2027. /* Set credentials */
  2028. if (acc->cred_cnt) {
  2029. pjsip_auth_clt_set_credentials( &acc->mwi_dlg->auth_sess,
  2030. acc->cred_cnt, acc->cred);
  2031. }
  2032. /* Set authentication preference */
  2033. pjsip_auth_clt_set_prefs(&acc->mwi_dlg->auth_sess, &acc->cfg.auth_pref);
  2034. pjsip_evsub_set_mod_data(acc->mwi_sub, pjsua_var.mod.id, acc);
  2035. status = pjsip_mwi_initiate(acc->mwi_sub, acc->cfg.mwi_expires, &tdata);
  2036. if (status != PJ_SUCCESS) {
  2037. pjsip_dlg_dec_lock(acc->mwi_dlg);
  2038. pjsip_pres_terminate(acc->mwi_sub, PJ_FALSE);
  2039. acc->mwi_sub = NULL;
  2040. acc->mwi_dlg = NULL;
  2041. pjsua_perror(THIS_FILE, "Unable to create initial MWI SUBSCRIBE",
  2042. status);
  2043. goto on_return;
  2044. }
  2045. pjsua_process_msg_data(tdata, NULL);
  2046. status = pjsip_pres_send_request(acc->mwi_sub, tdata);
  2047. if (status != PJ_SUCCESS) {
  2048. pjsip_dlg_dec_lock(acc->mwi_dlg);
  2049. pjsip_pres_terminate(acc->mwi_sub, PJ_FALSE);
  2050. acc->mwi_sub = NULL;
  2051. acc->mwi_dlg = NULL;
  2052. pjsua_perror(THIS_FILE, "Unable to send initial MWI SUBSCRIBE",
  2053. status);
  2054. goto on_return;
  2055. }
  2056. pjsip_dlg_dec_lock(acc->mwi_dlg);
  2057. on_return:
  2058. if (tmp_pool) pj_pool_release(tmp_pool);
  2059. pj_log_pop_indent();
  2060. return status;
  2061. }
  2062. /***************************************************************************
  2063. * Unsolicited MWI
  2064. */
  2065. static pj_bool_t unsolicited_mwi_on_rx_request(pjsip_rx_data *rdata)
  2066. {
  2067. pjsip_msg *msg = rdata->msg_info.msg;
  2068. pj_str_t EVENT_HDR = { "Event", 5 };
  2069. pj_str_t MWI = { "message-summary", 15 };
  2070. pjsip_event_hdr *eh;
  2071. pjsua_acc_id acc_id;
  2072. if (pjsip_method_cmp(&msg->line.req.method, pjsip_get_notify_method())!=0)
  2073. {
  2074. /* Only interested with NOTIFY request */
  2075. return PJ_FALSE;
  2076. }
  2077. eh = (pjsip_event_hdr*) pjsip_msg_find_hdr_by_name(msg, &EVENT_HDR, NULL);
  2078. if (!eh) {
  2079. /* Something wrong with the request, it has no Event hdr */
  2080. return PJ_FALSE;
  2081. }
  2082. if (pj_stricmp(&eh->event_type, &MWI) != 0) {
  2083. /* Not MWI event */
  2084. return PJ_FALSE;
  2085. }
  2086. /* Find which account for the incoming request. */
  2087. acc_id = pjsua_acc_find_for_incoming(rdata);
  2088. if (acc_id == PJSUA_INVALID_ID) {
  2089. const pj_str_t reason = pj_str("No account to handle");
  2090. PJ_LOG(2, (THIS_FILE,
  2091. "Unable to process incoming message %s "
  2092. "due to no available account",
  2093. pjsip_rx_data_get_info(rdata)));
  2094. pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
  2095. PJSIP_SC_CALL_TSX_DOES_NOT_EXIST, &reason,
  2096. NULL, NULL);
  2097. return PJ_TRUE;
  2098. }
  2099. PJ_LOG(4,(THIS_FILE, "Got unsolicited NOTIFY from %s:%d..",
  2100. rdata->pkt_info.src_name, rdata->pkt_info.src_port));
  2101. pj_log_push_indent();
  2102. /* Got unsolicited MWI request, respond with 200/OK first */
  2103. pjsip_endpt_respond(pjsua_get_pjsip_endpt(), NULL, rdata, 200, NULL,
  2104. NULL, NULL, NULL);
  2105. /* Call callback */
  2106. if (pjsua_var.ua_cfg.cb.on_mwi_info) {
  2107. pjsua_mwi_info mwi_info;
  2108. pj_bzero(&mwi_info, sizeof(mwi_info));
  2109. mwi_info.rdata = rdata;
  2110. (*pjsua_var.ua_cfg.cb.on_mwi_info)(acc_id, &mwi_info);
  2111. }
  2112. pj_log_pop_indent();
  2113. return PJ_TRUE;
  2114. }
  2115. /* The module instance. */
  2116. static pjsip_module pjsua_unsolicited_mwi_mod =
  2117. {
  2118. NULL, NULL, /* prev, next. */
  2119. { "mod-unsolicited-mwi", 19 }, /* Name. */
  2120. -1, /* Id */
  2121. PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
  2122. NULL, /* load() */
  2123. NULL, /* start() */
  2124. NULL, /* stop() */
  2125. NULL, /* unload() */
  2126. &unsolicited_mwi_on_rx_request, /* on_rx_request() */
  2127. NULL, /* on_rx_response() */
  2128. NULL, /* on_tx_request. */
  2129. NULL, /* on_tx_response() */
  2130. NULL, /* on_tsx_state() */
  2131. };
  2132. static pj_status_t enable_unsolicited_mwi(void)
  2133. {
  2134. pj_status_t status;
  2135. status = pjsip_endpt_register_module(pjsua_get_pjsip_endpt(),
  2136. &pjsua_unsolicited_mwi_mod);
  2137. if (status != PJ_SUCCESS)
  2138. pjsua_perror(THIS_FILE, "Error registering unsolicited MWI module",
  2139. status);
  2140. return status;
  2141. }
  2142. /***************************************************************************/
  2143. /* Timer callback to re-create client subscription */
  2144. static void pres_timer_cb(pj_timer_heap_t *th,
  2145. pj_timer_entry *entry)
  2146. {
  2147. unsigned i;
  2148. pj_time_val delay = { PJSUA_PRES_TIMER, 0 };
  2149. entry->id = PJ_FALSE;
  2150. /* Retry failed PUBLISH and MWI SUBSCRIBE requests */
  2151. for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
  2152. pjsua_acc *acc = &pjsua_var.acc[i];
  2153. /* Acc may not be ready yet, otherwise assertion will happen */
  2154. if (!pjsua_acc_is_valid(i))
  2155. continue;
  2156. /* Retry PUBLISH */
  2157. if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
  2158. pjsua_pres_init_publish_acc(acc->index);
  2159. /* Re-subscribe MWI subscription if it's terminated prematurely */
  2160. if (acc->cfg.mwi_enabled && !acc->mwi_sub)
  2161. pjsua_start_mwi(acc->index, PJ_FALSE);
  2162. }
  2163. /* #937: No need to do bulk client refresh, as buddies have their
  2164. * own individual timer now.
  2165. */
  2166. //refresh_client_subscriptions();
  2167. pjsip_endpt_schedule_timer(pjsua_var.endpt, entry, &delay);
  2168. entry->id = PJ_TRUE;
  2169. PJ_UNUSED_ARG(th);
  2170. }
  2171. /*
  2172. * Init presence
  2173. */
  2174. pj_status_t pjsua_pres_init()
  2175. {
  2176. unsigned i;
  2177. pj_status_t status;
  2178. status = pjsip_endpt_register_module( pjsua_var.endpt, &mod_pjsua_pres);
  2179. if (status != PJ_SUCCESS) {
  2180. pjsua_perror(THIS_FILE, "Unable to register pjsua presence module",
  2181. status);
  2182. }
  2183. for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
  2184. reset_buddy(i);
  2185. }
  2186. return status;
  2187. }
  2188. /*
  2189. * Start presence subsystem.
  2190. */
  2191. pj_status_t pjsua_pres_start(void)
  2192. {
  2193. /* Start presence timer to re-subscribe to buddy's presence when
  2194. * subscription has failed.
  2195. */
  2196. if (pjsua_var.pres_timer.id == PJ_FALSE) {
  2197. pj_time_val pres_interval = {PJSUA_PRES_TIMER, 0};
  2198. pjsua_var.pres_timer.cb = &pres_timer_cb;
  2199. pjsip_endpt_schedule_timer(pjsua_var.endpt, &pjsua_var.pres_timer,
  2200. &pres_interval);
  2201. pjsua_var.pres_timer.id = PJ_TRUE;
  2202. }
  2203. if (pjsua_var.ua_cfg.enable_unsolicited_mwi) {
  2204. pj_status_t status = enable_unsolicited_mwi();
  2205. if (status != PJ_SUCCESS)
  2206. return status;
  2207. }
  2208. return PJ_SUCCESS;
  2209. }
  2210. /*
  2211. * Shutdown presence.
  2212. */
  2213. void pjsua_pres_shutdown(unsigned flags)
  2214. {
  2215. unsigned i;
  2216. PJ_LOG(4,(THIS_FILE, "Shutting down presence.."));
  2217. pj_log_push_indent();
  2218. if (pjsua_var.pres_timer.id != 0) {
  2219. pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.pres_timer);
  2220. pjsua_var.pres_timer.id = PJ_FALSE;
  2221. }
  2222. for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
  2223. if (!pjsua_var.acc[i].valid)
  2224. continue;
  2225. pjsua_pres_delete_acc(i, flags);
  2226. }
  2227. for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
  2228. pjsua_var.buddy[i].monitor = 0;
  2229. }
  2230. if ((flags & PJSUA_DESTROY_NO_TX_MSG) == 0) {
  2231. refresh_client_subscriptions();
  2232. for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
  2233. if (pjsua_var.acc[i].valid)
  2234. pjsua_pres_update_acc(i, PJ_FALSE);
  2235. }
  2236. }
  2237. pj_log_pop_indent();
  2238. }