evsub.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364
  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 <pjsip-simple/evsub.h>
  20. #include <pjsip-simple/evsub_msg.h>
  21. #include <pjsip-simple/errno.h>
  22. #include <pjsip/sip_errno.h>
  23. #include <pjsip/sip_module.h>
  24. #include <pjsip/sip_endpoint.h>
  25. #include <pjsip/sip_dialog.h>
  26. #include <pjsip/sip_auth.h>
  27. #include <pjsip/sip_transaction.h>
  28. #include <pjsip/sip_event.h>
  29. #include <pj/assert.h>
  30. #include <pj/guid.h>
  31. #include <pj/log.h>
  32. #include <pj/os.h>
  33. #include <pj/pool.h>
  34. #include <pj/rand.h>
  35. #include <pj/string.h>
  36. #define THIS_FILE "evsub.c"
  37. /*
  38. * Global constant
  39. */
  40. /* Let's define this enum, so that it'll trigger compilation error
  41. * when somebody define the same enum in sip_msg.h
  42. */
  43. enum
  44. {
  45. PJSIP_SUBSCRIBE_METHOD = PJSIP_OTHER_METHOD,
  46. PJSIP_NOTIFY_METHOD = PJSIP_OTHER_METHOD
  47. };
  48. PJ_DEF_DATA(const pjsip_method) pjsip_subscribe_method =
  49. {
  50. (pjsip_method_e) PJSIP_SUBSCRIBE_METHOD,
  51. { "SUBSCRIBE", 9 }
  52. };
  53. PJ_DEF_DATA(const pjsip_method) pjsip_notify_method =
  54. {
  55. (pjsip_method_e) PJSIP_NOTIFY_METHOD,
  56. { "NOTIFY", 6 }
  57. };
  58. /**
  59. * SUBSCRIBE method constant.
  60. */
  61. PJ_DEF(const pjsip_method*) pjsip_get_subscribe_method()
  62. {
  63. return &pjsip_subscribe_method;
  64. }
  65. /**
  66. * NOTIFY method constant.
  67. */
  68. PJ_DEF(const pjsip_method*) pjsip_get_notify_method()
  69. {
  70. return &pjsip_notify_method;
  71. }
  72. /*
  73. * Static prototypes.
  74. */
  75. static void mod_evsub_on_tsx_state(pjsip_transaction*, pjsip_event*);
  76. static pj_status_t mod_evsub_unload(void);
  77. /*
  78. * State names.
  79. */
  80. static pj_str_t evsub_state_names[] =
  81. {
  82. { "NULL", 4},
  83. { "SENT", 4},
  84. { "ACCEPTED", 8},
  85. { "PENDING", 7},
  86. { "ACTIVE", 6},
  87. { "TERMINATED", 10},
  88. { "UNKNOWN", 7}
  89. };
  90. /*
  91. * Timer constants.
  92. */
  93. /* Number of seconds to send SUBSCRIBE before the actual expiration */
  94. #define TIME_UAC_REFRESH PJSIP_EVSUB_TIME_UAC_REFRESH
  95. /* Time to wait for the final NOTIFY after sending unsubscription */
  96. #define TIME_UAC_TERMINATE PJSIP_EVSUB_TIME_UAC_TERMINATE
  97. /* If client responds NOTIFY with non-2xx final response (such as 401),
  98. * wait for this seconds for further NOTIFY, otherwise client will
  99. * unsubscribe
  100. */
  101. #define TIME_UAC_WAIT_NOTIFY PJSIP_EVSUB_TIME_UAC_WAIT_NOTIFY
  102. /*
  103. * Timer id
  104. */
  105. enum timer_id
  106. {
  107. /* No timer. */
  108. TIMER_TYPE_NONE,
  109. /* Time to refresh client subscription.
  110. * The action is to call on_client_refresh() callback.
  111. */
  112. TIMER_TYPE_UAC_REFRESH,
  113. /* UAS timeout after to subscription refresh.
  114. * The action is to call on_server_timeout() callback.
  115. */
  116. TIMER_TYPE_UAS_TIMEOUT,
  117. /* UAC waiting for final NOTIFY after unsubscribing
  118. * The action is to terminate.
  119. */
  120. TIMER_TYPE_UAC_TERMINATE,
  121. /* UAC waiting for further NOTIFY after sending non-2xx response to
  122. * NOTIFY. The action is to unsubscribe.
  123. */
  124. TIMER_TYPE_UAC_WAIT_NOTIFY,
  125. /* Max nb of timer types. */
  126. TIMER_TYPE_MAX
  127. };
  128. static const char *timer_names[] =
  129. {
  130. "None",
  131. "UAC_REFRESH",
  132. "UAS_TIMEOUT",
  133. "UAC_TERMINATE",
  134. "UAC_WAIT_NOTIFY",
  135. "INVALID_TIMER"
  136. };
  137. /*
  138. * Definition of event package.
  139. */
  140. struct evpkg
  141. {
  142. PJ_DECL_LIST_MEMBER(struct evpkg);
  143. pj_str_t pkg_name;
  144. pjsip_module *pkg_mod;
  145. unsigned pkg_expires;
  146. pjsip_accept_hdr *pkg_accept;
  147. };
  148. /*
  149. * Event subscription module (mod-evsub).
  150. */
  151. static struct mod_evsub
  152. {
  153. pjsip_module mod;
  154. pj_pool_t *pool;
  155. pjsip_endpoint *endpt;
  156. struct evpkg pkg_list;
  157. pjsip_allow_events_hdr *allow_events_hdr;
  158. } mod_evsub =
  159. {
  160. {
  161. NULL, NULL, /* prev, next. */
  162. { "mod-evsub", 9 }, /* Name. */
  163. -1, /* Id */
  164. PJSIP_MOD_PRIORITY_DIALOG_USAGE, /* Priority */
  165. NULL, /* load() */
  166. NULL, /* start() */
  167. NULL, /* stop() */
  168. &mod_evsub_unload, /* unload() */
  169. NULL, /* on_rx_request() */
  170. NULL, /* on_rx_response() */
  171. NULL, /* on_tx_request. */
  172. NULL, /* on_tx_response() */
  173. &mod_evsub_on_tsx_state, /* on_tsx_state() */
  174. }
  175. };
  176. /*
  177. * Event subscription session.
  178. */
  179. struct pjsip_evsub
  180. {
  181. char obj_name[PJ_MAX_OBJ_NAME]; /**< Name. */
  182. pj_pool_t *pool; /**< Pool. */
  183. pjsip_endpoint *endpt; /**< Endpoint instance. */
  184. pjsip_dialog *dlg; /**< Underlying dialog. */
  185. struct evpkg *pkg; /**< The event package. */
  186. unsigned option; /**< Options. */
  187. pjsip_evsub_user user; /**< Callback. */
  188. pj_bool_t call_cb; /**< Notify callback? */
  189. pjsip_role_e role; /**< UAC=subscriber, UAS=notifier */
  190. pjsip_evsub_state state; /**< Subscription state. */
  191. pj_str_t state_str; /**< String describing the state. */
  192. pjsip_evsub_state dst_state; /**< Pending state to be set. */
  193. pj_str_t dst_state_str;/**< Pending state to be set. */
  194. pj_str_t term_reason; /**< Termination reason. */
  195. pjsip_method method; /**< Method that established subscr.*/
  196. pjsip_event_hdr *event; /**< Event description. */
  197. pjsip_expires_hdr *expires; /**< Expires header */
  198. pjsip_accept_hdr *accept; /**< Local Accept header. */
  199. pjsip_hdr sub_hdr_list; /**< User-defined header. */
  200. pj_time_val refresh_time; /**< Time to refresh. */
  201. pj_timer_entry timer; /**< Internal timer. */
  202. int pending_tsx; /**< Number of pending transactions.*/
  203. pjsip_transaction *pending_sub; /**< Pending UAC SUBSCRIBE tsx. */
  204. pj_timer_entry *pending_sub_timer; /**< Stop pending sub timer. */
  205. pjsip_tx_data *pending_notify;/**< Pending NOTIFY to be sent. */
  206. pj_bool_t calling_on_rx_refresh;/**< Inside on_rx_refresh()?*/
  207. pj_grp_lock_t *grp_lock; /* Session group lock */
  208. void *mod_data[PJSIP_MAX_MODULE]; /**< Module data. */
  209. };
  210. /*
  211. * This is the structure that will be "attached" to dialog.
  212. * The purpose is to allow multiple subscriptions inside a dialog.
  213. */
  214. struct dlgsub
  215. {
  216. PJ_DECL_LIST_MEMBER(struct dlgsub);
  217. pjsip_evsub *sub;
  218. };
  219. /* Static vars. */
  220. static const pj_str_t STR_EVENT = { "Event", 5 };
  221. static const pj_str_t STR_EVENT_S = { "o", 1 };
  222. static const pj_str_t STR_SUB_STATE = { "Subscription-State", 18 };
  223. static const pj_str_t STR_TERMINATED = { "terminated", 10 };
  224. static const pj_str_t STR_ACTIVE = { "active", 6 };
  225. static const pj_str_t STR_PENDING = { "pending", 7 };
  226. static const pj_str_t STR_TIMEOUT = { "timeout", 7};
  227. static const pj_str_t STR_RETRY_AFTER = { "Retry-After", 11 };
  228. /*
  229. * On unload module.
  230. */
  231. static pj_status_t mod_evsub_unload(void)
  232. {
  233. pjsip_endpt_release_pool(mod_evsub.endpt, mod_evsub.pool);
  234. mod_evsub.pool = NULL;
  235. return PJ_SUCCESS;
  236. }
  237. /* Proto for pjsipsimple_strerror().
  238. * Defined in errno.c
  239. */
  240. PJ_DECL(pj_str_t) pjsipsimple_strerror( pj_status_t statcode,
  241. char *buf, pj_size_t bufsize );
  242. /*
  243. * Init and register module.
  244. */
  245. PJ_DEF(pj_status_t) pjsip_evsub_init_module(pjsip_endpoint *endpt)
  246. {
  247. pj_status_t status;
  248. pj_str_t method_tags[] = {
  249. { "SUBSCRIBE", 9},
  250. { "NOTIFY", 6}
  251. };
  252. status = pj_register_strerror(PJSIP_SIMPLE_ERRNO_START,
  253. PJ_ERRNO_SPACE_SIZE,
  254. &pjsipsimple_strerror);
  255. pj_assert(status == PJ_SUCCESS);
  256. PJ_ASSERT_RETURN(endpt != NULL, PJ_EINVAL);
  257. PJ_ASSERT_RETURN(mod_evsub.mod.id == -1, PJ_EINVALIDOP);
  258. /* Keep endpoint for future reference: */
  259. mod_evsub.endpt = endpt;
  260. /* Init event package list: */
  261. pj_list_init(&mod_evsub.pkg_list);
  262. /* Create pool: */
  263. mod_evsub.pool = pjsip_endpt_create_pool(endpt, "evsub",
  264. PJSIP_POOL_EVSUB_LEN,
  265. PJSIP_POOL_EVSUB_INC);
  266. if (!mod_evsub.pool)
  267. return PJ_ENOMEM;
  268. /* Register module: */
  269. status = pjsip_endpt_register_module(endpt, &mod_evsub.mod);
  270. if (status != PJ_SUCCESS)
  271. goto on_error;
  272. /* Create Allow-Events header: */
  273. mod_evsub.allow_events_hdr = pjsip_allow_events_hdr_create(mod_evsub.pool);
  274. /* Register SIP-event specific headers parser: */
  275. status = pjsip_evsub_init_parser();
  276. if (status != PJ_SUCCESS)
  277. goto on_error;
  278. /* Register new methods SUBSCRIBE and NOTIFY in Allow-ed header */
  279. pjsip_endpt_add_capability(endpt, &mod_evsub.mod, PJSIP_H_ALLOW, NULL,
  280. 2, method_tags);
  281. /* Done. */
  282. return PJ_SUCCESS;
  283. on_error:
  284. if (mod_evsub.pool) {
  285. pjsip_endpt_release_pool(endpt, mod_evsub.pool);
  286. mod_evsub.pool = NULL;
  287. }
  288. mod_evsub.endpt = NULL;
  289. return status;
  290. }
  291. /*
  292. * Get the instance of the module.
  293. */
  294. PJ_DEF(pjsip_module*) pjsip_evsub_instance(void)
  295. {
  296. PJ_ASSERT_RETURN(mod_evsub.mod.id != -1, NULL);
  297. return &mod_evsub.mod;
  298. }
  299. /*
  300. * Get the event subscription instance in the transaction.
  301. */
  302. PJ_DEF(pjsip_evsub*) pjsip_tsx_get_evsub(const pjsip_transaction *tsx)
  303. {
  304. return (pjsip_evsub*) tsx->mod_data[mod_evsub.mod.id];
  305. }
  306. /*
  307. * Set event subscription's module data.
  308. */
  309. PJ_DEF(void) pjsip_evsub_set_mod_data( pjsip_evsub *sub, unsigned mod_id,
  310. void *data )
  311. {
  312. PJ_ASSERT_ON_FAIL(mod_id < PJSIP_MAX_MODULE, return);
  313. sub->mod_data[mod_id] = data;
  314. }
  315. /*
  316. * Get event subscription's module data.
  317. */
  318. PJ_DEF(void*) pjsip_evsub_get_mod_data( const pjsip_evsub *sub,
  319. unsigned mod_id )
  320. {
  321. PJ_ASSERT_RETURN(mod_id < PJSIP_MAX_MODULE, NULL);
  322. return sub->mod_data[mod_id];
  323. }
  324. /*
  325. * Find registered event package with matching name.
  326. */
  327. static struct evpkg* find_pkg(const pj_str_t *event_name)
  328. {
  329. struct evpkg *pkg;
  330. pkg = mod_evsub.pkg_list.next;
  331. while (pkg != &mod_evsub.pkg_list) {
  332. if (pj_stricmp(&pkg->pkg_name, event_name) == 0) {
  333. return pkg;
  334. }
  335. pkg = pkg->next;
  336. }
  337. return NULL;
  338. }
  339. /*
  340. * Register an event package
  341. */
  342. PJ_DEF(pj_status_t) pjsip_evsub_register_pkg( pjsip_module *pkg_mod,
  343. const pj_str_t *event_name,
  344. unsigned expires,
  345. unsigned accept_cnt,
  346. const pj_str_t accept[])
  347. {
  348. struct evpkg *pkg;
  349. unsigned i;
  350. PJ_ASSERT_RETURN(pkg_mod && event_name, PJ_EINVAL);
  351. /* Make sure accept_cnt < PJ_ARRAY_SIZE(pkg->pkg_accept->values) */
  352. PJ_ASSERT_RETURN(accept_cnt <= PJSIP_GENERIC_ARRAY_MAX_COUNT,
  353. PJ_ETOOMANY);
  354. /* Make sure evsub module has been initialized */
  355. PJ_ASSERT_RETURN(mod_evsub.mod.id != -1, PJ_EINVALIDOP);
  356. /* Make sure no module with the specified name already registered: */
  357. if (find_pkg(event_name) != NULL)
  358. return PJSIP_SIMPLE_EPKGEXISTS;
  359. /* Create new event package: */
  360. pkg = PJ_POOL_ALLOC_T(mod_evsub.pool, struct evpkg);
  361. pkg->pkg_mod = pkg_mod;
  362. pkg->pkg_expires = expires;
  363. pj_strdup(mod_evsub.pool, &pkg->pkg_name, event_name);
  364. pkg->pkg_accept = pjsip_accept_hdr_create(mod_evsub.pool);
  365. pkg->pkg_accept->count = accept_cnt;
  366. for (i=0; i<accept_cnt; ++i) {
  367. pj_strdup(mod_evsub.pool, &pkg->pkg_accept->values[i], &accept[i]);
  368. }
  369. /* Add to package list: */
  370. pj_list_push_back(&mod_evsub.pkg_list, pkg);
  371. /* Add to Allow-Events header: */
  372. if (mod_evsub.allow_events_hdr->count !=
  373. PJ_ARRAY_SIZE(mod_evsub.allow_events_hdr->values))
  374. {
  375. mod_evsub.allow_events_hdr->values[mod_evsub.allow_events_hdr->count] =
  376. pkg->pkg_name;
  377. ++mod_evsub.allow_events_hdr->count;
  378. }
  379. /* Add to endpoint's Accept header */
  380. pjsip_endpt_add_capability(mod_evsub.endpt, &mod_evsub.mod,
  381. PJSIP_H_ACCEPT, NULL,
  382. pkg->pkg_accept->count,
  383. pkg->pkg_accept->values);
  384. /* Done */
  385. PJ_LOG(5,(THIS_FILE, "Event pkg \"%.*s\" registered by %.*s",
  386. (int)event_name->slen, event_name->ptr,
  387. (int)pkg_mod->name.slen, pkg_mod->name.ptr));
  388. return PJ_SUCCESS;
  389. }
  390. /*
  391. * Retrieve Allow-Events header
  392. */
  393. PJ_DEF(const pjsip_hdr*) pjsip_evsub_get_allow_events_hdr(
  394. const pjsip_module *m)
  395. {
  396. struct mod_evsub *mod;
  397. if (m == NULL)
  398. m = pjsip_evsub_instance();
  399. mod = (struct mod_evsub*)m;
  400. return (pjsip_hdr*) mod->allow_events_hdr;
  401. }
  402. /*
  403. * Update expiration time.
  404. */
  405. static void update_expires( pjsip_evsub *sub, pj_uint32_t interval )
  406. {
  407. pj_gettimeofday(&sub->refresh_time);
  408. sub->refresh_time.sec += interval;
  409. }
  410. /*
  411. * Schedule timer.
  412. */
  413. static void set_timer( pjsip_evsub *sub, int timer_id,
  414. pj_uint32_t seconds)
  415. {
  416. if (sub->timer.id != TIMER_TYPE_NONE) {
  417. PJ_LOG(5,(sub->obj_name, "%s %s timer",
  418. (timer_id==sub->timer.id ? "Updating" : "Cancelling"),
  419. timer_names[sub->timer.id]));
  420. pjsip_endpt_cancel_timer(sub->endpt, &sub->timer);
  421. sub->timer.id = TIMER_TYPE_NONE;
  422. }
  423. if (timer_id != TIMER_TYPE_NONE && seconds != PJSIP_EXPIRES_NOT_SPECIFIED)
  424. {
  425. pj_time_val timeout;
  426. PJ_ASSERT_ON_FAIL(timer_id>TIMER_TYPE_NONE && timer_id<TIMER_TYPE_MAX,
  427. return);
  428. timeout.sec = seconds;
  429. timeout.msec = 0;
  430. pj_timer_heap_schedule_w_grp_lock(
  431. pjsip_endpt_get_timer_heap(sub->endpt),
  432. &sub->timer, &timeout, timer_id, sub->grp_lock);
  433. PJ_LOG(5,(sub->obj_name, "Timer %s scheduled in %ld seconds",
  434. timer_names[sub->timer.id], timeout.sec));
  435. }
  436. }
  437. /*
  438. * Set event subscription UAS timout.
  439. */
  440. PJ_DEF(void) pjsip_evsub_uas_set_timeout(pjsip_evsub *sub, pj_uint32_t seconds)
  441. {
  442. set_timer(sub, TIMER_TYPE_UAS_TIMEOUT, (pj_int32_t)seconds);
  443. }
  444. /*
  445. * Destructor.
  446. */
  447. static void evsub_on_destroy(void *obj)
  448. {
  449. pjsip_evsub *sub = (pjsip_evsub*)obj;
  450. /* Decrement dialog's session */
  451. pjsip_dlg_dec_session(sub->dlg, &mod_evsub.mod);
  452. }
  453. /*
  454. * Destroy session.
  455. */
  456. static void evsub_destroy( pjsip_evsub *sub )
  457. {
  458. struct dlgsub *dlgsub_head, *dlgsub;
  459. PJ_LOG(4,(sub->obj_name, "Subscription destroyed"));
  460. /* Kill timer */
  461. set_timer(sub, TIMER_TYPE_NONE, 0);
  462. /* Kill timer for stopping pending sub (see ticket #1807) */
  463. if (sub->pending_sub_timer && sub->pending_sub_timer->id == 1) {
  464. pjsip_endpt_cancel_timer(sub->endpt, sub->pending_sub_timer);
  465. sub->pending_sub_timer->id = 0;
  466. sub->pending_sub_timer = NULL;
  467. }
  468. /* Remove this session from dialog's list of subscription */
  469. dlgsub_head = (struct dlgsub *) sub->dlg->mod_data[mod_evsub.mod.id];
  470. dlgsub = dlgsub_head->next;
  471. while (dlgsub != dlgsub_head) {
  472. if (dlgsub->sub == sub) {
  473. pj_list_erase(dlgsub);
  474. break;
  475. }
  476. dlgsub = dlgsub->next;
  477. }
  478. pj_grp_lock_dec_ref(sub->grp_lock);
  479. }
  480. /*
  481. * Set subscription session state.
  482. */
  483. static void set_state( pjsip_evsub *sub, pjsip_evsub_state state,
  484. const pj_str_t *state_str, pjsip_event *event,
  485. const pj_str_t *reason)
  486. {
  487. pjsip_evsub_state prev_state = sub->state;
  488. pj_str_t old_state_str = sub->state_str;
  489. pjsip_event dummy_event;
  490. sub->state = state;
  491. if (state_str && state_str->slen)
  492. pj_strdup_with_null(sub->pool, &sub->state_str, state_str);
  493. else
  494. sub->state_str = evsub_state_names[state];
  495. if (reason && sub->term_reason.slen==0)
  496. pj_strdup(sub->pool, &sub->term_reason, reason);
  497. PJ_LOG(4,(sub->obj_name,
  498. "Subscription state changed %.*s --> %.*s",
  499. (int)old_state_str.slen,
  500. old_state_str.ptr,
  501. (int)sub->state_str.slen,
  502. sub->state_str.ptr));
  503. pj_log_push_indent();
  504. /* don't call the callback with NULL event, it may crash the app! */
  505. if (!event) {
  506. PJSIP_EVENT_INIT_USER(dummy_event, 0, 0, 0, 0);
  507. event = &dummy_event;
  508. }
  509. if (sub->user.on_evsub_state && sub->call_cb)
  510. (*sub->user.on_evsub_state)(sub, event);
  511. if (state == PJSIP_EVSUB_STATE_TERMINATED &&
  512. prev_state != PJSIP_EVSUB_STATE_TERMINATED)
  513. {
  514. /* Kill any timer. */
  515. set_timer(sub, TIMER_TYPE_NONE, 0);
  516. /* We must not destroy evsub if we're still calling the callback. */
  517. if (sub->pending_tsx == 0 && !sub->calling_on_rx_refresh) {
  518. evsub_destroy(sub);
  519. }
  520. }
  521. pj_log_pop_indent();
  522. }
  523. /*
  524. * Timer callback.
  525. */
  526. static void on_timer( pj_timer_heap_t *timer_heap,
  527. struct pj_timer_entry *entry)
  528. {
  529. pjsip_evsub *sub;
  530. int timer_id;
  531. PJ_UNUSED_ARG(timer_heap);
  532. sub = (pjsip_evsub*) entry->user_data;
  533. pjsip_dlg_inc_lock(sub->dlg);
  534. /* If this timer entry has just been rescheduled or cancelled
  535. * while waiting for dialog mutex, just return (see #1885 scenario 1).
  536. */
  537. if (pj_timer_entry_running(entry) || entry->id == TIMER_TYPE_NONE) {
  538. pjsip_dlg_dec_lock(sub->dlg);
  539. return;
  540. }
  541. timer_id = entry->id;
  542. entry->id = TIMER_TYPE_NONE;
  543. switch (timer_id) {
  544. case TIMER_TYPE_UAC_REFRESH:
  545. /* Time for UAC to refresh subscription */
  546. if (sub->user.on_client_refresh && sub->call_cb) {
  547. (*sub->user.on_client_refresh)(sub);
  548. } else {
  549. pjsip_tx_data *tdata;
  550. pj_status_t status;
  551. PJ_LOG(5,(sub->obj_name, "Refreshing subscription."));
  552. pj_log_push_indent();
  553. status = pjsip_evsub_initiate(sub, NULL,
  554. sub->expires->ivalue,
  555. &tdata);
  556. if (status == PJ_SUCCESS)
  557. pjsip_evsub_send_request(sub, tdata);
  558. pj_log_pop_indent();
  559. }
  560. break;
  561. case TIMER_TYPE_UAS_TIMEOUT:
  562. /* Refresh from UAC has not been received */
  563. if (sub->user.on_server_timeout && sub->call_cb) {
  564. (*sub->user.on_server_timeout)(sub);
  565. } else {
  566. pjsip_tx_data *tdata;
  567. pj_status_t status;
  568. PJ_LOG(5,(sub->obj_name, "Timeout waiting for refresh. "
  569. "Sending NOTIFY to terminate."));
  570. pj_log_push_indent();
  571. status = pjsip_evsub_notify( sub, PJSIP_EVSUB_STATE_TERMINATED,
  572. NULL, &STR_TIMEOUT, &tdata);
  573. if (status == PJ_SUCCESS)
  574. pjsip_evsub_send_request(sub, tdata);
  575. pj_log_pop_indent();
  576. }
  577. break;
  578. case TIMER_TYPE_UAC_TERMINATE:
  579. {
  580. pj_str_t timeout = {"timeout", 7};
  581. PJ_LOG(5,(sub->obj_name, "Timeout waiting for final NOTIFY. "
  582. "Terminating.."));
  583. pj_log_push_indent();
  584. set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL,
  585. &timeout);
  586. pj_log_pop_indent();
  587. }
  588. break;
  589. case TIMER_TYPE_UAC_WAIT_NOTIFY:
  590. {
  591. pjsip_tx_data *tdata;
  592. pj_status_t status;
  593. PJ_LOG(5,(sub->obj_name,
  594. "Timeout waiting for subsequent NOTIFY (we did "
  595. "send non-2xx response for previous NOTIFY). "
  596. "Unsubscribing.."));
  597. pj_log_push_indent();
  598. status = pjsip_evsub_initiate( sub, NULL, 0, &tdata);
  599. if (status == PJ_SUCCESS)
  600. pjsip_evsub_send_request(sub, tdata);
  601. pj_log_pop_indent();
  602. }
  603. break;
  604. default:
  605. pj_assert(!"Invalid timer id");
  606. }
  607. pjsip_dlg_dec_lock(sub->dlg);
  608. }
  609. /*
  610. * Create subscription session, used for both client and notifier.
  611. */
  612. static pj_status_t evsub_create( pjsip_dialog *dlg,
  613. pjsip_role_e role,
  614. const pjsip_evsub_user *user_cb,
  615. const pj_str_t *event,
  616. unsigned option,
  617. pjsip_evsub **p_evsub )
  618. {
  619. pjsip_evsub *sub;
  620. struct evpkg *pkg;
  621. struct dlgsub *dlgsub_head, *dlgsub;
  622. pj_status_t status;
  623. /* Make sure there's package register for the event name: */
  624. pkg = find_pkg(event);
  625. if (pkg == NULL)
  626. return PJSIP_SIMPLE_ENOPKG;
  627. /* Must lock dialog before using pool etc. */
  628. pjsip_dlg_inc_lock(dlg);
  629. /* Init attributes: */
  630. sub = PJ_POOL_ZALLOC_T(dlg->pool, struct pjsip_evsub);
  631. sub->pool = dlg->pool;
  632. sub->endpt = dlg->endpt;
  633. sub->dlg = dlg;
  634. sub->pkg = pkg;
  635. sub->role = role;
  636. sub->call_cb = PJ_TRUE;
  637. sub->option = option;
  638. sub->state = PJSIP_EVSUB_STATE_NULL;
  639. sub->state_str = evsub_state_names[sub->state];
  640. sub->expires = pjsip_expires_hdr_create(sub->pool, pkg->pkg_expires);
  641. sub->accept = (pjsip_accept_hdr*)
  642. pjsip_hdr_clone(sub->pool, pkg->pkg_accept);
  643. pj_list_init(&sub->sub_hdr_list);
  644. sub->timer.user_data = sub;
  645. sub->timer.cb = &on_timer;
  646. /* Set name. */
  647. pj_ansi_snprintf(sub->obj_name, PJ_ARRAY_SIZE(sub->obj_name),
  648. "evsub%p", sub);
  649. /* Copy callback, if any: */
  650. if (user_cb)
  651. pj_memcpy(&sub->user, user_cb, sizeof(pjsip_evsub_user));
  652. /* Create Event header: */
  653. sub->event = pjsip_event_hdr_create(sub->pool);
  654. pj_strdup(sub->pool, &sub->event->event_type, event);
  655. /* Check if another subscription has been registered to the dialog. In
  656. * that case, just add ourselves to the subscription list, otherwise
  657. * create and register a new subscription list.
  658. */
  659. if (pjsip_dlg_has_usage(dlg, &mod_evsub.mod)) {
  660. dlgsub_head = (struct dlgsub*) dlg->mod_data[mod_evsub.mod.id];
  661. dlgsub = PJ_POOL_ALLOC_T(sub->pool, struct dlgsub);
  662. dlgsub->sub = sub;
  663. pj_list_push_back(dlgsub_head, dlgsub);
  664. } else {
  665. dlgsub_head = PJ_POOL_ALLOC_T(sub->pool, struct dlgsub);
  666. dlgsub = PJ_POOL_ALLOC_T(sub->pool, struct dlgsub);
  667. dlgsub->sub = sub;
  668. pj_list_init(dlgsub_head);
  669. pj_list_push_back(dlgsub_head, dlgsub);
  670. /* Register as dialog usage: */
  671. status = pjsip_dlg_add_usage(dlg, &mod_evsub.mod, dlgsub_head);
  672. if (status != PJ_SUCCESS) {
  673. pjsip_dlg_dec_lock(dlg);
  674. return status;
  675. }
  676. }
  677. PJ_LOG(5,(sub->obj_name, "%s subscription created, using dialog %s",
  678. (role==PJSIP_ROLE_UAC ? "UAC" : "UAS"),
  679. dlg->obj_name));
  680. *p_evsub = sub;
  681. pjsip_dlg_dec_lock(dlg);
  682. return PJ_SUCCESS;
  683. }
  684. /*
  685. * Increment the event subscription's group lock.
  686. */
  687. PJ_DEF(pj_status_t) pjsip_evsub_add_ref(pjsip_evsub *sub)
  688. {
  689. return pj_grp_lock_add_ref(sub->grp_lock);
  690. }
  691. /*
  692. * Decrement the event subscription's group lock.
  693. */
  694. PJ_DEF(pj_status_t) pjsip_evsub_dec_ref(pjsip_evsub *sub)
  695. {
  696. return pj_grp_lock_dec_ref(sub->grp_lock);
  697. }
  698. /*
  699. * Create client subscription session.
  700. */
  701. PJ_DEF(pj_status_t) pjsip_evsub_create_uac( pjsip_dialog *dlg,
  702. const pjsip_evsub_user *user_cb,
  703. const pj_str_t *event,
  704. unsigned option,
  705. pjsip_evsub **p_evsub)
  706. {
  707. pjsip_evsub *sub;
  708. pj_status_t status;
  709. PJ_ASSERT_RETURN(dlg && event && p_evsub, PJ_EINVAL);
  710. pjsip_dlg_inc_lock(dlg);
  711. status = evsub_create(dlg, PJSIP_UAC_ROLE, user_cb, event, option, &sub);
  712. if (status != PJ_SUCCESS)
  713. goto on_return;
  714. /* Add unique Id to Event header, only when PJSIP_EVSUB_NO_EVENT_ID
  715. * is not specified.
  716. */
  717. if ((option & PJSIP_EVSUB_NO_EVENT_ID) == 0) {
  718. pj_create_unique_string(sub->pool, &sub->event->id_param);
  719. }
  720. /* Increment dlg session. */
  721. pjsip_dlg_inc_session(sub->dlg, &mod_evsub.mod);
  722. /* Init group lock */
  723. status = pj_grp_lock_create(dlg->pool, NULL, &sub->grp_lock);
  724. if (status != PJ_SUCCESS) {
  725. pjsip_dlg_dec_session(sub->dlg, &mod_evsub.mod);
  726. goto on_return;
  727. }
  728. pj_grp_lock_add_ref(sub->grp_lock);
  729. pj_grp_lock_add_handler(sub->grp_lock, dlg->pool, sub, &evsub_on_destroy);
  730. /* Done */
  731. *p_evsub = sub;
  732. on_return:
  733. pjsip_dlg_dec_lock(dlg);
  734. return status;
  735. }
  736. /*
  737. * Create server subscription session from incoming request.
  738. */
  739. PJ_DEF(pj_status_t) pjsip_evsub_create_uas( pjsip_dialog *dlg,
  740. const pjsip_evsub_user *user_cb,
  741. pjsip_rx_data *rdata,
  742. unsigned option,
  743. pjsip_evsub **p_evsub)
  744. {
  745. pjsip_evsub *sub;
  746. pjsip_transaction *tsx;
  747. pjsip_accept_hdr *accept_hdr;
  748. pjsip_event_hdr *event_hdr;
  749. pjsip_expires_hdr *expires_hdr;
  750. pj_status_t status;
  751. /* Check arguments: */
  752. PJ_ASSERT_RETURN(dlg && rdata && p_evsub, PJ_EINVAL);
  753. /* MUST be request message: */
  754. PJ_ASSERT_RETURN(rdata->msg_info.msg->type == PJSIP_REQUEST_MSG,
  755. PJSIP_ENOTREQUESTMSG);
  756. /* Transaction MUST have been created (in the dialog) */
  757. tsx = pjsip_rdata_get_tsx(rdata);
  758. PJ_ASSERT_RETURN(tsx != NULL, PJSIP_ENOTSX);
  759. /* No subscription must have been attached to transaction */
  760. PJ_ASSERT_RETURN(tsx->mod_data[mod_evsub.mod.id] == NULL,
  761. PJSIP_ETYPEEXISTS);
  762. /* Package MUST implement on_rx_refresh */
  763. PJ_ASSERT_RETURN(user_cb->on_rx_refresh, PJ_EINVALIDOP);
  764. /* Request MUST have "Event" header. We need the Event header to get
  765. * the package name (don't want to add more arguments in the function).
  766. */
  767. event_hdr = (pjsip_event_hdr*)
  768. pjsip_msg_find_hdr_by_names(rdata->msg_info.msg, &STR_EVENT,
  769. &STR_EVENT_S, NULL);
  770. if (event_hdr == NULL) {
  771. return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_BAD_REQUEST);
  772. }
  773. /* Start locking the mutex: */
  774. pjsip_dlg_inc_lock(dlg);
  775. /* Create the session: */
  776. status = evsub_create(dlg, PJSIP_UAS_ROLE, user_cb,
  777. &event_hdr->event_type, option, &sub);
  778. if (status != PJ_SUCCESS)
  779. goto on_return;
  780. /* Just duplicate Event header from the request */
  781. sub->event = (pjsip_event_hdr*) pjsip_hdr_clone(sub->pool, event_hdr);
  782. /* Set the method: */
  783. pjsip_method_copy(sub->pool, &sub->method,
  784. &rdata->msg_info.msg->line.req.method);
  785. /* Set expiration time based on client request (in Expires header),
  786. * or package default expiration time.
  787. */
  788. expires_hdr = (pjsip_expires_hdr*)
  789. pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL);
  790. if (expires_hdr) {
  791. struct evpkg *evpkg;
  792. evpkg = find_pkg(&event_hdr->event_type);
  793. if (evpkg) {
  794. if (expires_hdr->ivalue < evpkg->pkg_expires)
  795. sub->expires->ivalue = expires_hdr->ivalue;
  796. else
  797. sub->expires->ivalue = evpkg->pkg_expires;
  798. }
  799. }
  800. /* Update time. */
  801. update_expires(sub, sub->expires->ivalue);
  802. /* Update Accept header: */
  803. accept_hdr = (pjsip_accept_hdr*)
  804. pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_ACCEPT, NULL);
  805. if (accept_hdr)
  806. sub->accept = (pjsip_accept_hdr*)pjsip_hdr_clone(sub->pool,accept_hdr);
  807. /* Increment dlg session. */
  808. pjsip_dlg_inc_session(dlg, &mod_evsub.mod);
  809. /* Init group lock */
  810. status = pj_grp_lock_create(dlg->pool, NULL, &sub->grp_lock);
  811. if (status != PJ_SUCCESS) {
  812. pjsip_dlg_dec_session(sub->dlg, &mod_evsub.mod);
  813. goto on_return;
  814. }
  815. pj_grp_lock_add_ref(sub->grp_lock);
  816. pj_grp_lock_add_handler(sub->grp_lock, dlg->pool, sub, &evsub_on_destroy);
  817. /* We can start the session: */
  818. sub->pending_tsx++;
  819. tsx->mod_data[mod_evsub.mod.id] = sub;
  820. /* Done. */
  821. *p_evsub = sub;
  822. on_return:
  823. pjsip_dlg_dec_lock(dlg);
  824. return status;
  825. }
  826. /*
  827. * Forcefully destroy subscription.
  828. */
  829. PJ_DEF(pj_status_t) pjsip_evsub_terminate( pjsip_evsub *sub,
  830. pj_bool_t notify )
  831. {
  832. PJ_ASSERT_RETURN(sub, PJ_EINVAL);
  833. pjsip_dlg_inc_lock(sub->dlg);
  834. /* I think it's pretty safe to disable this check.
  835. if (sub->pending_tsx) {
  836. pj_assert(!"Unable to terminate when there's pending tsx");
  837. pjsip_dlg_dec_lock(sub->dlg);
  838. return PJ_EINVALIDOP;
  839. }
  840. */
  841. sub->call_cb = notify;
  842. set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL, NULL);
  843. pjsip_dlg_dec_lock(sub->dlg);
  844. return PJ_SUCCESS;
  845. }
  846. /*
  847. * Get subscription state.
  848. */
  849. PJ_DEF(pjsip_evsub_state) pjsip_evsub_get_state(const pjsip_evsub *sub)
  850. {
  851. return sub->state;
  852. }
  853. /*
  854. * Get state name.
  855. */
  856. PJ_DEF(const char*) pjsip_evsub_get_state_name(const pjsip_evsub *sub)
  857. {
  858. return sub->state_str.ptr;
  859. }
  860. /*
  861. * Get termination reason.
  862. */
  863. PJ_DEF(const pj_str_t*) pjsip_evsub_get_termination_reason(
  864. const pjsip_evsub *sub)
  865. {
  866. return &sub->term_reason;
  867. }
  868. /**
  869. * Get subscription expiration time.
  870. */
  871. PJ_DEF(pj_uint32_t) pjsip_evsub_get_expires(const pjsip_evsub *sub)
  872. {
  873. return sub->expires->ivalue;
  874. }
  875. /*
  876. * Initiate client subscription
  877. */
  878. PJ_DEF(pj_status_t) pjsip_evsub_initiate( pjsip_evsub *sub,
  879. const pjsip_method *method,
  880. pj_uint32_t expires,
  881. pjsip_tx_data **p_tdata)
  882. {
  883. pjsip_tx_data *tdata;
  884. pj_status_t status;
  885. PJ_ASSERT_RETURN(sub!=NULL && p_tdata!=NULL, PJ_EINVAL);
  886. /* Use SUBSCRIBE if method is not specified */
  887. if (method == NULL)
  888. method = &pjsip_subscribe_method;
  889. pjsip_dlg_inc_lock(sub->dlg);
  890. /* Update method: */
  891. if (sub->state == PJSIP_EVSUB_STATE_NULL)
  892. pjsip_method_copy(sub->pool, &sub->method, method);
  893. status = pjsip_dlg_create_request( sub->dlg, method, -1, &tdata);
  894. if (status != PJ_SUCCESS)
  895. goto on_return;
  896. /* Add Event header: */
  897. pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
  898. pjsip_hdr_shallow_clone(tdata->pool, sub->event));
  899. /* Update and add expires header: */
  900. if (expires != PJSIP_EXPIRES_NOT_SPECIFIED)
  901. sub->expires->ivalue = expires;
  902. pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
  903. pjsip_hdr_shallow_clone(tdata->pool, sub->expires));
  904. /* Add Supported header (it's optional in RFC 3265, but some event package
  905. * RFC may bring this requirement to SHOULD strength - e.g. RFC 5373)
  906. */
  907. {
  908. const pjsip_hdr *hdr = pjsip_endpt_get_capability(sub->endpt,
  909. PJSIP_H_SUPPORTED,
  910. NULL);
  911. if (hdr) {
  912. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
  913. pjsip_hdr_shallow_clone(tdata->pool, hdr));
  914. }
  915. }
  916. /* Add Accept header: */
  917. pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
  918. pjsip_hdr_shallow_clone(tdata->pool, sub->accept));
  919. /* Add Allow-Events header: */
  920. pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
  921. pjsip_hdr_shallow_clone(tdata->pool,
  922. mod_evsub.allow_events_hdr));
  923. /* Add custom headers */
  924. {
  925. const pjsip_hdr *hdr = sub->sub_hdr_list.next;
  926. while (hdr != &sub->sub_hdr_list) {
  927. pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
  928. pjsip_hdr_shallow_clone(tdata->pool, hdr));
  929. hdr = hdr->next;
  930. }
  931. }
  932. *p_tdata = tdata;
  933. on_return:
  934. pjsip_dlg_dec_lock(sub->dlg);
  935. return status;
  936. }
  937. /*
  938. * Add custom headers.
  939. */
  940. PJ_DEF(pj_status_t) pjsip_evsub_add_header( pjsip_evsub *sub,
  941. const pjsip_hdr *hdr_list )
  942. {
  943. const pjsip_hdr *hdr;
  944. PJ_ASSERT_RETURN(sub && hdr_list, PJ_EINVAL);
  945. hdr = hdr_list->next;
  946. while (hdr != hdr_list) {
  947. pj_list_push_back(&sub->sub_hdr_list, (pjsip_hdr*)
  948. pjsip_hdr_clone(sub->pool, hdr));
  949. hdr = hdr->next;
  950. }
  951. return PJ_SUCCESS;
  952. }
  953. /*
  954. * Accept incoming subscription request.
  955. */
  956. PJ_DEF(pj_status_t) pjsip_evsub_accept( pjsip_evsub *sub,
  957. pjsip_rx_data *rdata,
  958. int st_code,
  959. const pjsip_hdr *hdr_list )
  960. {
  961. pjsip_tx_data *tdata;
  962. pjsip_transaction *tsx;
  963. pj_status_t status;
  964. /* Check arguments */
  965. PJ_ASSERT_RETURN(sub && rdata, PJ_EINVAL);
  966. /* Can only be for server subscription: */
  967. PJ_ASSERT_RETURN(sub->role == PJSIP_ROLE_UAS, PJ_EINVALIDOP);
  968. /* Only expect 2xx status code (for now) */
  969. PJ_ASSERT_RETURN(st_code/100 == 2, PJ_EINVALIDOP);
  970. /* Subscription MUST have been attached to the transaction.
  971. * Initial subscription request will be attached on evsub_create_uas(),
  972. * while subsequent requests will be attached in tsx_state()
  973. */
  974. tsx = pjsip_rdata_get_tsx(rdata);
  975. PJ_ASSERT_RETURN(tsx->mod_data[mod_evsub.mod.id] != NULL,
  976. PJ_EINVALIDOP);
  977. /* Lock dialog */
  978. pjsip_dlg_inc_lock(sub->dlg);
  979. /* Create response: */
  980. status = pjsip_dlg_create_response( sub->dlg, rdata, st_code, NULL,
  981. &tdata);
  982. if (status != PJ_SUCCESS)
  983. goto on_return;
  984. /* Add expires header: */
  985. pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
  986. pjsip_hdr_shallow_clone(tdata->pool, sub->expires));
  987. /* Add additional header, if any. */
  988. if (hdr_list) {
  989. const pjsip_hdr *hdr = hdr_list->next;
  990. while (hdr != hdr_list) {
  991. pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
  992. pjsip_hdr_clone(tdata->pool, hdr));
  993. hdr = hdr->next;
  994. }
  995. }
  996. /* Send the response: */
  997. status = pjsip_dlg_send_response( sub->dlg, tsx, tdata );
  998. if (status != PJ_SUCCESS)
  999. goto on_return;
  1000. /* Set UAS timeout timer, when status code is 2xx and state is not
  1001. * terminated.
  1002. */
  1003. if (st_code/100 == 2 && sub->state != PJSIP_EVSUB_STATE_TERMINATED) {
  1004. PJ_LOG(5,(sub->obj_name, "UAS timeout in %d seconds",
  1005. sub->expires->ivalue));
  1006. set_timer(sub, TIMER_TYPE_UAS_TIMEOUT, sub->expires->ivalue);
  1007. }
  1008. on_return:
  1009. pjsip_dlg_dec_lock(sub->dlg);
  1010. return status;
  1011. }
  1012. /*
  1013. * Create Subscription-State header based on current server subscription
  1014. * state.
  1015. */
  1016. static pjsip_sub_state_hdr* sub_state_create( pj_pool_t *pool,
  1017. pjsip_evsub *sub,
  1018. pjsip_evsub_state state,
  1019. const pj_str_t *state_str,
  1020. const pj_str_t *reason )
  1021. {
  1022. pjsip_sub_state_hdr *sub_state;
  1023. pj_time_val now, delay;
  1024. /* Get the remaining time before refresh is required */
  1025. pj_gettimeofday(&now);
  1026. delay = sub->refresh_time;
  1027. PJ_TIME_VAL_SUB(delay, now);
  1028. /* Create the Subscription-State header */
  1029. sub_state = pjsip_sub_state_hdr_create(pool);
  1030. /* Fill up the header */
  1031. switch (state) {
  1032. case PJSIP_EVSUB_STATE_NULL:
  1033. case PJSIP_EVSUB_STATE_SENT:
  1034. pj_assert(!"Invalid state!");
  1035. /* Treat as pending */
  1036. case PJSIP_EVSUB_STATE_ACCEPTED:
  1037. case PJSIP_EVSUB_STATE_PENDING:
  1038. sub_state->sub_state = STR_PENDING;
  1039. sub_state->expires_param = delay.sec;
  1040. break;
  1041. case PJSIP_EVSUB_STATE_ACTIVE:
  1042. sub_state->sub_state = STR_ACTIVE;
  1043. sub_state->expires_param = delay.sec;
  1044. break;
  1045. case PJSIP_EVSUB_STATE_TERMINATED:
  1046. sub_state->sub_state = STR_TERMINATED;
  1047. if (reason != NULL)
  1048. pj_strdup(pool, &sub_state->reason_param, reason);
  1049. break;
  1050. case PJSIP_EVSUB_STATE_UNKNOWN:
  1051. pj_assert(state_str != NULL);
  1052. pj_strdup(pool, &sub_state->sub_state, state_str);
  1053. break;
  1054. }
  1055. return sub_state;
  1056. }
  1057. /*
  1058. * Create and send NOTIFY request.
  1059. */
  1060. PJ_DEF(pj_status_t) pjsip_evsub_notify( pjsip_evsub *sub,
  1061. pjsip_evsub_state state,
  1062. const pj_str_t *state_str,
  1063. const pj_str_t *reason,
  1064. pjsip_tx_data **p_tdata)
  1065. {
  1066. pjsip_tx_data *tdata;
  1067. pjsip_sub_state_hdr *sub_state;
  1068. pj_status_t status;
  1069. /* Check arguments. */
  1070. PJ_ASSERT_RETURN(sub!=NULL && p_tdata!=NULL, PJ_EINVAL);
  1071. /* Lock dialog. */
  1072. pjsip_dlg_inc_lock(sub->dlg);
  1073. /* Create NOTIFY request */
  1074. status = pjsip_dlg_create_request( sub->dlg, pjsip_get_notify_method(),
  1075. -1, &tdata);
  1076. if (status != PJ_SUCCESS)
  1077. goto on_return;
  1078. /* Add Event header */
  1079. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
  1080. pjsip_hdr_shallow_clone(tdata->pool, sub->event));
  1081. /* Add Subscription-State header */
  1082. sub_state = sub_state_create(tdata->pool, sub, state, state_str,
  1083. reason);
  1084. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)sub_state);
  1085. /* Add Allow-Events header */
  1086. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)
  1087. pjsip_hdr_shallow_clone(tdata->pool, mod_evsub.allow_events_hdr));
  1088. /* Add Authentication headers. */
  1089. pjsip_auth_clt_init_req( &sub->dlg->auth_sess, tdata );
  1090. /* Update reason */
  1091. if (reason)
  1092. pj_strdup(sub->dlg->pool, &sub->term_reason, reason);
  1093. /* Save destination state. */
  1094. sub->dst_state = state;
  1095. if (state_str)
  1096. pj_strdup(sub->pool, &sub->dst_state_str, state_str);
  1097. else
  1098. sub->dst_state_str.slen = 0;
  1099. *p_tdata = tdata;
  1100. on_return:
  1101. /* Unlock dialog */
  1102. pjsip_dlg_dec_lock(sub->dlg);
  1103. return status;
  1104. }
  1105. /*
  1106. * Create NOTIFY to reflect current status.
  1107. */
  1108. PJ_DEF(pj_status_t) pjsip_evsub_current_notify( pjsip_evsub *sub,
  1109. pjsip_tx_data **p_tdata )
  1110. {
  1111. return pjsip_evsub_notify( sub, sub->state, &sub->state_str,
  1112. NULL, p_tdata );
  1113. }
  1114. /*
  1115. * Send request.
  1116. */
  1117. PJ_DEF(pj_status_t) pjsip_evsub_send_request( pjsip_evsub *sub,
  1118. pjsip_tx_data *tdata)
  1119. {
  1120. pj_status_t status = PJ_SUCCESS;
  1121. /* Must be request message. */
  1122. PJ_ASSERT_RETURN(tdata->msg->type == PJSIP_REQUEST_MSG,
  1123. PJSIP_ENOTREQUESTMSG);
  1124. /* Lock */
  1125. pjsip_dlg_inc_lock(sub->dlg);
  1126. /* Delay sending NOTIFY if we're inside on_rx_refresh() callback
  1127. * until we have sent the response to the incoming SUBSCRIBE.
  1128. */
  1129. if (sub->calling_on_rx_refresh &&
  1130. pjsip_method_cmp(&tdata->msg->line.req.method,
  1131. &pjsip_notify_method)==0)
  1132. {
  1133. sub->pending_notify = tdata;
  1134. goto on_return;
  1135. }
  1136. /* Send the request. */
  1137. status = pjsip_dlg_send_request(sub->dlg, tdata, -1, NULL);
  1138. if (status != PJ_SUCCESS)
  1139. goto on_return;
  1140. /* Special case for NOTIFY:
  1141. * The new state was set in pjsip_evsub_notify(), but we apply the
  1142. * new state now, when the request was actually sent.
  1143. */
  1144. if (pjsip_method_cmp(&tdata->msg->line.req.method,
  1145. &pjsip_notify_method)==0)
  1146. {
  1147. PJ_ASSERT_ON_FAIL( sub->dst_state!=PJSIP_EVSUB_STATE_NULL,
  1148. {goto on_return;});
  1149. set_state(sub, sub->dst_state,
  1150. (sub->dst_state_str.slen ? &sub->dst_state_str : NULL),
  1151. NULL, NULL);
  1152. sub->dst_state = PJSIP_EVSUB_STATE_NULL;
  1153. sub->dst_state_str.slen = 0;
  1154. }
  1155. on_return:
  1156. pjsip_dlg_dec_lock(sub->dlg);
  1157. return status;
  1158. }
  1159. /* Callback to be called to terminate transaction. */
  1160. static void terminate_timer_cb(pj_timer_heap_t *timer_heap,
  1161. struct pj_timer_entry *entry)
  1162. {
  1163. pj_str_t *key;
  1164. pjsip_transaction *tsx;
  1165. PJ_UNUSED_ARG(timer_heap);
  1166. /* Clear timer ID */
  1167. entry->id = 0;
  1168. key = (pj_str_t*)entry->user_data;
  1169. tsx = pjsip_tsx_layer_find_tsx(key, PJ_FALSE);
  1170. /* Chance of race condition here */
  1171. if (tsx) {
  1172. pjsip_tsx_terminate(tsx, PJSIP_SC_REQUEST_UPDATED);
  1173. }
  1174. }
  1175. /*
  1176. * Attach subscription session to newly created transaction, if appropriate.
  1177. */
  1178. static pjsip_evsub *on_new_transaction( pjsip_transaction *tsx,
  1179. pjsip_event *event)
  1180. {
  1181. /*
  1182. * Newly created transaction will not have subscription session
  1183. * attached to it. Find the subscription session from the dialog,
  1184. * by matching the Event header.
  1185. */
  1186. pjsip_dialog *dlg;
  1187. pjsip_event_hdr *event_hdr;
  1188. pjsip_msg *msg;
  1189. struct dlgsub *dlgsub_head, *dlgsub;
  1190. pjsip_evsub *sub;
  1191. dlg = pjsip_tsx_get_dlg(tsx);
  1192. if (!dlg) {
  1193. pj_assert(!"Transaction should have a dialog instance!");
  1194. return NULL;
  1195. }
  1196. switch (event->body.tsx_state.type) {
  1197. case PJSIP_EVENT_RX_MSG:
  1198. msg = event->body.tsx_state.src.rdata->msg_info.msg;
  1199. break;
  1200. case PJSIP_EVENT_TX_MSG:
  1201. msg = event->body.tsx_state.src.tdata->msg;
  1202. break;
  1203. default:
  1204. if (tsx->role == PJSIP_ROLE_UAC)
  1205. msg = tsx->last_tx->msg;
  1206. else
  1207. msg = NULL;
  1208. break;
  1209. }
  1210. if (!msg) {
  1211. //Note:
  1212. // this transaction can be other transaction in the dialog.
  1213. // The assertion below probably only valid for dialog that
  1214. // only has one event subscription usage.
  1215. //pj_assert(!"First transaction event is not TX or RX!");
  1216. return NULL;
  1217. }
  1218. event_hdr = (pjsip_event_hdr*)
  1219. pjsip_msg_find_hdr_by_names(msg, &STR_EVENT,
  1220. &STR_EVENT_S, NULL);
  1221. if (!event_hdr) {
  1222. /* Not subscription related message */
  1223. return NULL;
  1224. }
  1225. /* Find the subscription in the dialog, based on the content
  1226. * of Event header:
  1227. */
  1228. dlgsub_head = (struct dlgsub*) dlg->mod_data[mod_evsub.mod.id];
  1229. if (dlgsub_head == NULL) {
  1230. dlgsub_head = PJ_POOL_ALLOC_T(dlg->pool, struct dlgsub);
  1231. pj_list_init(dlgsub_head);
  1232. dlg->mod_data[mod_evsub.mod.id] = dlgsub_head;
  1233. }
  1234. dlgsub = dlgsub_head->next;
  1235. while (dlgsub != dlgsub_head) {
  1236. if (pj_stricmp(&dlgsub->sub->event->event_type,
  1237. &event_hdr->event_type)==0)
  1238. {
  1239. /* Event type matched.
  1240. * Check if event ID matched too.
  1241. */
  1242. if (pj_strcmp(&dlgsub->sub->event->id_param,
  1243. &event_hdr->id_param)==0)
  1244. {
  1245. /* Skip this subscription if it has no event ID and has been
  1246. * terminated (see ticket #1647).
  1247. */
  1248. if ((dlgsub->sub->option & PJSIP_EVSUB_NO_EVENT_ID) &&
  1249. (pjsip_evsub_get_state(dlgsub->sub)==
  1250. PJSIP_EVSUB_STATE_TERMINATED))
  1251. {
  1252. dlgsub = dlgsub->next;
  1253. continue;
  1254. } else {
  1255. break;
  1256. }
  1257. }
  1258. /*
  1259. * Otherwise if it is an UAC subscription, AND
  1260. * PJSIP_EVSUB_NO_EVENT_ID flag is set, AND
  1261. * the session's event id is NULL, AND
  1262. * the incoming request is NOTIFY with event ID, then
  1263. * we consider it as a match, and update the
  1264. * session's event id.
  1265. */
  1266. else if (dlgsub->sub->role == PJSIP_ROLE_UAC &&
  1267. (dlgsub->sub->option & PJSIP_EVSUB_NO_EVENT_ID)!=0 &&
  1268. dlgsub->sub->event->id_param.slen==0 &&
  1269. !pjsip_method_cmp(&tsx->method, &pjsip_notify_method))
  1270. {
  1271. /* Update session's event id. */
  1272. pj_strdup(dlgsub->sub->pool,
  1273. &dlgsub->sub->event->id_param,
  1274. &event_hdr->id_param);
  1275. break;
  1276. }
  1277. }
  1278. dlgsub = dlgsub->next;
  1279. }
  1280. /* Note:
  1281. * the second condition is for https://github.com/pjsip/pjproject/issues/911
  1282. * Take note that it could be us that is trying to send a final message,
  1283. * such as final NOTIFY upon unsubscription.
  1284. */
  1285. if (dlgsub == dlgsub_head ||
  1286. (dlgsub->sub &&
  1287. tsx->role == PJSIP_ROLE_UAS &&
  1288. pjsip_evsub_get_state(dlgsub->sub)==PJSIP_EVSUB_STATE_TERMINATED))
  1289. {
  1290. const char *reason_msg =
  1291. (dlgsub == dlgsub_head ? "Subscription Does Not Exist" :
  1292. "Subscription already terminated");
  1293. /* This could be incoming request to create new subscription */
  1294. PJ_LOG(4,(THIS_FILE,
  1295. "%s for %.*s, event=%.*s;id=%.*s",
  1296. reason_msg,
  1297. (int)tsx->method.name.slen,
  1298. tsx->method.name.ptr,
  1299. (int)event_hdr->event_type.slen,
  1300. event_hdr->event_type.ptr,
  1301. (int)event_hdr->id_param.slen,
  1302. event_hdr->id_param.ptr));
  1303. /* If this is an incoming NOTIFY, reject with 481 */
  1304. if (tsx->state == PJSIP_TSX_STATE_TRYING &&
  1305. pjsip_method_cmp(&tsx->method, &pjsip_notify_method)==0)
  1306. {
  1307. pj_str_t reason;
  1308. pjsip_tx_data *tdata;
  1309. pj_status_t status;
  1310. pj_cstr(&reason, reason_msg);
  1311. status = pjsip_dlg_create_response(dlg,
  1312. event->body.tsx_state.src.rdata,
  1313. 481, &reason,
  1314. &tdata);
  1315. if (status == PJ_SUCCESS) {
  1316. status = pjsip_dlg_send_response(dlg, tsx, tdata);
  1317. }
  1318. }
  1319. return NULL;
  1320. }
  1321. /* Found! */
  1322. sub = dlgsub->sub;
  1323. /* Attach session to the transaction */
  1324. tsx->mod_data[mod_evsub.mod.id] = sub;
  1325. sub->pending_tsx++;
  1326. /* Special case for outgoing/UAC SUBSCRIBE/REFER transaction.
  1327. * We can only have one pending UAC SUBSCRIBE/REFER, so if another
  1328. * transaction is started while previous one still alive, terminate
  1329. * the older one.
  1330. *
  1331. * Sample scenario:
  1332. * - subscribe sent to destination that doesn't exist, transaction
  1333. * is still retransmitting request, then unsubscribe is sent.
  1334. */
  1335. if (tsx->role == PJSIP_ROLE_UAC &&
  1336. tsx->state == PJSIP_TSX_STATE_CALLING &&
  1337. (pjsip_method_cmp(&tsx->method, &sub->method) == 0 ||
  1338. pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method) == 0))
  1339. {
  1340. if (sub->pending_sub &&
  1341. sub->pending_sub->state < PJSIP_TSX_STATE_COMPLETED)
  1342. {
  1343. pj_timer_entry *timer;
  1344. pj_str_t *key;
  1345. pj_time_val timeout = {0, 0};
  1346. PJ_LOG(4,(sub->obj_name,
  1347. "Cancelling pending subscription request"));
  1348. /* By convention, we use 490 (Request Updated) status code.
  1349. * When transaction handler (below) see this status code, it
  1350. * will ignore the transaction.
  1351. */
  1352. /* This unfortunately may cause deadlock, because at the moment
  1353. * we are holding dialog's mutex. If a response to this
  1354. * transaction is in progress in another thread, that thread
  1355. * will deadlock when trying to acquire dialog mutex, because
  1356. * it is holding the transaction mutex.
  1357. *
  1358. * So the solution is to register timer to kill this transaction.
  1359. */
  1360. //pjsip_tsx_terminate(sub->pending_sub, PJSIP_SC_REQUEST_UPDATED);
  1361. timer = PJ_POOL_ZALLOC_T(dlg->pool, pj_timer_entry);
  1362. key = PJ_POOL_ALLOC_T(dlg->pool, pj_str_t);
  1363. pj_strdup(dlg->pool, key, &sub->pending_sub->transaction_key);
  1364. timer->cb = &terminate_timer_cb;
  1365. timer->user_data = key;
  1366. timer->id = 1;
  1367. sub->pending_sub_timer = timer;
  1368. pjsip_endpt_schedule_timer(dlg->endpt, timer, &timeout);
  1369. }
  1370. sub->pending_sub = tsx;
  1371. }
  1372. return sub;
  1373. }
  1374. /*
  1375. * Create response, adding custome headers and msg body.
  1376. */
  1377. static pj_status_t create_response( pjsip_evsub *sub,
  1378. pjsip_rx_data *rdata,
  1379. int st_code,
  1380. const pj_str_t *st_text,
  1381. const pjsip_hdr *res_hdr,
  1382. const pjsip_msg_body *body,
  1383. pjsip_tx_data **p_tdata)
  1384. {
  1385. pjsip_tx_data *tdata;
  1386. pjsip_hdr *hdr;
  1387. pj_status_t status;
  1388. status = pjsip_dlg_create_response(sub->dlg, rdata,
  1389. st_code, st_text, &tdata);
  1390. if (status != PJ_SUCCESS)
  1391. return status;
  1392. *p_tdata = tdata;
  1393. /* Add response headers. */
  1394. hdr = res_hdr->next;
  1395. while (hdr != res_hdr) {
  1396. pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
  1397. pjsip_hdr_clone(tdata->pool, hdr));
  1398. hdr = hdr->next;
  1399. }
  1400. /* Add msg body, if any */
  1401. if (body) {
  1402. tdata->msg->body = pjsip_msg_body_clone(tdata->pool, body);
  1403. if (tdata->msg->body == NULL) {
  1404. PJ_LOG(4,(THIS_FILE, "Error: unable to clone msg body"));
  1405. /* Ignore */
  1406. return PJ_SUCCESS;
  1407. }
  1408. }
  1409. return PJ_SUCCESS;
  1410. }
  1411. /*
  1412. * Get subscription state from the value of Subscription-State header.
  1413. */
  1414. static void get_hdr_state( pjsip_sub_state_hdr *sub_state,
  1415. pjsip_evsub_state *state,
  1416. pj_str_t **state_str )
  1417. {
  1418. if (pj_stricmp(&sub_state->sub_state, &STR_TERMINATED)==0) {
  1419. *state = PJSIP_EVSUB_STATE_TERMINATED;
  1420. *state_str = NULL;
  1421. } else if (pj_stricmp(&sub_state->sub_state, &STR_ACTIVE)==0) {
  1422. *state = PJSIP_EVSUB_STATE_ACTIVE;
  1423. *state_str = NULL;
  1424. } else if (pj_stricmp(&sub_state->sub_state, &STR_PENDING)==0) {
  1425. *state = PJSIP_EVSUB_STATE_PENDING;
  1426. *state_str = NULL;
  1427. } else {
  1428. *state = PJSIP_EVSUB_STATE_UNKNOWN;
  1429. *state_str = &sub_state->sub_state;
  1430. }
  1431. }
  1432. /*
  1433. * Transaction event processing by UAC, after subscription is sent.
  1434. */
  1435. static void on_tsx_state_uac( pjsip_evsub *sub, pjsip_transaction *tsx,
  1436. pjsip_event *event )
  1437. {
  1438. if (pjsip_method_cmp(&tsx->method, &sub->method)==0 ||
  1439. pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method)==0)
  1440. {
  1441. /* Received response to outgoing request that establishes/refresh
  1442. * subscription.
  1443. */
  1444. /* First time initial request is sent. */
  1445. if (sub->state == PJSIP_EVSUB_STATE_NULL &&
  1446. tsx->state == PJSIP_TSX_STATE_CALLING)
  1447. {
  1448. set_state(sub, PJSIP_EVSUB_STATE_SENT, NULL, event, NULL);
  1449. return;
  1450. }
  1451. /* Only interested in final response */
  1452. if (tsx->state != PJSIP_TSX_STATE_COMPLETED &&
  1453. tsx->state != PJSIP_TSX_STATE_TERMINATED)
  1454. {
  1455. return;
  1456. }
  1457. /* Clear pending subscription */
  1458. if (tsx == sub->pending_sub) {
  1459. sub->pending_sub = NULL;
  1460. } else if (sub->pending_sub != NULL) {
  1461. /* This SUBSCRIBE transaction has been "renewed" with another
  1462. * SUBSCRIBE, so we can just ignore this. For example, user
  1463. * sent SUBSCRIBE followed immediately with UN-SUBSCRIBE.
  1464. */
  1465. return;
  1466. }
  1467. /* Handle authentication. */
  1468. if (tsx->status_code==401 || tsx->status_code==407) {
  1469. pjsip_tx_data *tdata;
  1470. pj_status_t status;
  1471. if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
  1472. /* Previously failed transaction has terminated */
  1473. return;
  1474. }
  1475. status = pjsip_auth_clt_reinit_req(&sub->dlg->auth_sess,
  1476. event->body.tsx_state.src.rdata,
  1477. tsx->last_tx, &tdata);
  1478. if (status == PJ_SUCCESS)
  1479. status = pjsip_dlg_send_request(sub->dlg, tdata, -1, NULL);
  1480. if (status != PJ_SUCCESS) {
  1481. /* Authentication failed! */
  1482. set_state(sub, PJSIP_EVSUB_STATE_TERMINATED,
  1483. NULL, event, &tsx->status_text);
  1484. return;
  1485. }
  1486. return;
  1487. }
  1488. if (tsx->status_code/100 == 2) {
  1489. /* Successfull SUBSCRIBE request!
  1490. * This could be:
  1491. * - response to initial SUBSCRIBE request
  1492. * - response to subsequent refresh
  1493. * - response to unsubscription
  1494. */
  1495. if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
  1496. /* Ignore; this transaction has been processed before */
  1497. return;
  1498. }
  1499. /* Update UAC refresh time, if response contains Expires header,
  1500. * only when we're not unsubscribing.
  1501. */
  1502. if (sub->expires->ivalue != 0) {
  1503. pjsip_msg *msg;
  1504. pjsip_expires_hdr *expires;
  1505. msg = event->body.tsx_state.src.rdata->msg_info.msg;
  1506. expires = (pjsip_expires_hdr*)
  1507. pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL);
  1508. if (expires) {
  1509. sub->expires->ivalue = expires->ivalue;
  1510. }
  1511. }
  1512. /* Update time */
  1513. update_expires(sub, sub->expires->ivalue);
  1514. /* Start UAC refresh timer, only when we're not unsubscribing */
  1515. if (sub->expires->ivalue != 0) {
  1516. unsigned timeout = (sub->expires->ivalue > TIME_UAC_REFRESH) ?
  1517. sub->expires->ivalue - TIME_UAC_REFRESH : sub->expires->ivalue;
  1518. /* Reduce timeout by about 1 - 10 secs (randomized) */
  1519. if (timeout > 10)
  1520. timeout += -10 + (pj_rand() % 10);
  1521. PJ_LOG(5,(sub->obj_name, "Will refresh in %d seconds",
  1522. timeout));
  1523. set_timer(sub, TIMER_TYPE_UAC_REFRESH, timeout);
  1524. } else {
  1525. /* Otherwise set timer to terminate client subscription when
  1526. * NOTIFY to end subscription is not received.
  1527. */
  1528. set_timer(sub, TIMER_TYPE_UAC_TERMINATE, TIME_UAC_TERMINATE);
  1529. }
  1530. /* Set state, if necessary */
  1531. pj_assert(sub->state != PJSIP_EVSUB_STATE_NULL);
  1532. if (sub->state == PJSIP_EVSUB_STATE_SENT) {
  1533. set_state(sub, PJSIP_EVSUB_STATE_ACCEPTED, NULL, event, NULL);
  1534. }
  1535. } else {
  1536. /* Failed SUBSCRIBE request!
  1537. *
  1538. * The RFC 3265 says that if outgoing SUBSCRIBE fails with status
  1539. * other than 481, the subscription is still considered valid for
  1540. * the duration of the last Expires.
  1541. *
  1542. * Since we send refresh about 5 seconds (TIME_UAC_REFRESH) before
  1543. * expiration, theoritically the expiration is still valid for the
  1544. * next 5 seconds even when we receive non-481 failed response.
  1545. *
  1546. * Ah, what the heck!
  1547. *
  1548. * Just terminate now!
  1549. *
  1550. */
  1551. if (sub->state == PJSIP_EVSUB_STATE_TERMINATED) {
  1552. /* Ignore, has been handled before */
  1553. return;
  1554. }
  1555. /* Ignore 490 (Request Updated) status.
  1556. * This happens when application sends SUBSCRIBE/REFER while
  1557. * another one is still in progress.
  1558. */
  1559. if (tsx->status_code == PJSIP_SC_REQUEST_UPDATED) {
  1560. return;
  1561. }
  1562. /* Set state to TERMINATED */
  1563. set_state(sub, PJSIP_EVSUB_STATE_TERMINATED,
  1564. NULL, event, &tsx->status_text);
  1565. }
  1566. } else if (pjsip_method_cmp(&tsx->method, &pjsip_notify_method) == 0) {
  1567. /* Incoming NOTIFY.
  1568. * This can be the result of:
  1569. * - Initial subscription response
  1570. * - UAS updating the resource info.
  1571. * - Unsubscription response.
  1572. */
  1573. int st_code = 200;
  1574. pj_str_t *st_text = NULL;
  1575. pjsip_hdr res_hdr;
  1576. pjsip_msg_body *body = NULL;
  1577. pjsip_rx_data *rdata;
  1578. pjsip_msg *msg;
  1579. pjsip_sub_state_hdr *sub_state;
  1580. pjsip_evsub_state new_state;
  1581. pj_str_t *new_state_str;
  1582. pjsip_tx_data *tdata;
  1583. pj_status_t status;
  1584. /* Only want to handle initial NOTIFY receive event. */
  1585. if (tsx->state != PJSIP_TSX_STATE_TRYING)
  1586. return;
  1587. rdata = event->body.tsx_state.src.rdata;
  1588. msg = rdata->msg_info.msg;
  1589. pj_list_init(&res_hdr);
  1590. /* Get subscription state header. */
  1591. sub_state = (pjsip_sub_state_hdr*)
  1592. pjsip_msg_find_hdr_by_name(msg, &STR_SUB_STATE, NULL);
  1593. if (sub_state == NULL) {
  1594. pjsip_warning_hdr *warn_hdr;
  1595. pj_str_t warn_text = { "Missing Subscription-State header", 33};
  1596. /* Bad request! Add warning header. */
  1597. st_code = PJSIP_SC_BAD_REQUEST;
  1598. warn_hdr = pjsip_warning_hdr_create(rdata->tp_info.pool, 399,
  1599. pjsip_endpt_name(sub->endpt),
  1600. &warn_text);
  1601. pj_list_push_back(&res_hdr, warn_hdr);
  1602. }
  1603. /* Call application registered callback to handle incoming NOTIFY,
  1604. * if any.
  1605. */
  1606. if (st_code==200 && sub->user.on_rx_notify && sub->call_cb) {
  1607. (*sub->user.on_rx_notify)(sub, rdata, &st_code, &st_text,
  1608. &res_hdr, &body);
  1609. /* Application MUST specify final response! */
  1610. PJ_ASSERT_ON_FAIL(st_code >= 200, {st_code=200; });
  1611. /* Must be a valid status code */
  1612. PJ_ASSERT_ON_FAIL(st_code <= 699, {st_code=500; });
  1613. }
  1614. /* If non-2xx should be returned, then send the response.
  1615. * No need to update server subscription state.
  1616. */
  1617. if (st_code >= 300) {
  1618. status = create_response(sub, rdata, st_code, st_text, &res_hdr,
  1619. body, &tdata);
  1620. if (status == PJ_SUCCESS) {
  1621. status = pjsip_dlg_send_response(sub->dlg, tsx, tdata);
  1622. }
  1623. /* Start timer to terminate subscription, just in case server
  1624. * is not able to generate NOTIFY to our response.
  1625. */
  1626. if (status == PJ_SUCCESS) {
  1627. unsigned timeout = TIME_UAC_WAIT_NOTIFY;
  1628. set_timer(sub, TIMER_TYPE_UAC_WAIT_NOTIFY, timeout);
  1629. } else {
  1630. char errmsg[PJ_ERR_MSG_SIZE];
  1631. pj_str_t reason;
  1632. reason = pj_strerror(status, errmsg, sizeof(errmsg));
  1633. set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL,
  1634. &reason);
  1635. }
  1636. return;
  1637. }
  1638. /* Update expiration from the value of expires param in
  1639. * Subscription-State header, but ONLY when subscription state
  1640. * is "active" or "pending", AND the header contains expires param.
  1641. */
  1642. if (sub->expires->ivalue != 0 &&
  1643. sub_state->expires_param != PJSIP_EXPIRES_NOT_SPECIFIED &&
  1644. (pj_stricmp(&sub_state->sub_state, &STR_ACTIVE)==0 ||
  1645. pj_stricmp(&sub_state->sub_state, &STR_PENDING)==0))
  1646. {
  1647. unsigned next_refresh = sub_state->expires_param;
  1648. unsigned timeout;
  1649. update_expires(sub, next_refresh);
  1650. /* Start UAC refresh timer, only when we're not unsubscribing */
  1651. timeout = (next_refresh > TIME_UAC_REFRESH) ?
  1652. next_refresh - TIME_UAC_REFRESH : next_refresh;
  1653. PJ_LOG(5,(sub->obj_name, "Will refresh in %d seconds", timeout));
  1654. set_timer(sub, TIMER_TYPE_UAC_REFRESH, timeout);
  1655. }
  1656. /* Find out the state */
  1657. get_hdr_state(sub_state, &new_state, &new_state_str);
  1658. /* Send response. */
  1659. status = create_response(sub, rdata, st_code, st_text, &res_hdr,
  1660. body, &tdata);
  1661. if (status == PJ_SUCCESS)
  1662. status = pjsip_dlg_send_response(sub->dlg, tsx, tdata);
  1663. /* Set the state */
  1664. if (status == PJ_SUCCESS) {
  1665. set_state(sub, new_state, new_state_str, event,
  1666. &sub_state->reason_param);
  1667. } else {
  1668. char errmsg[PJ_ERR_MSG_SIZE];
  1669. pj_str_t reason;
  1670. reason = pj_strerror(status, errmsg, sizeof(errmsg));
  1671. set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, event,
  1672. &reason);
  1673. }
  1674. } else {
  1675. /*
  1676. * Unexpected method!
  1677. */
  1678. PJ_LOG(4,(sub->obj_name, "Unexpected transaction method %.*s",
  1679. (int)tsx->method.name.slen, tsx->method.name.ptr));
  1680. }
  1681. }
  1682. /*
  1683. * Transaction event processing by UAS, after subscription is accepted.
  1684. */
  1685. static void on_tsx_state_uas( pjsip_evsub *sub, pjsip_transaction *tsx,
  1686. pjsip_event *event)
  1687. {
  1688. if (pjsip_method_cmp(&tsx->method, &sub->method) == 0 ||
  1689. pjsip_method_cmp(&tsx->method, &pjsip_subscribe_method) == 0)
  1690. {
  1691. /*
  1692. * Incoming request (e.g. SUBSCRIBE or REFER) to refresh subsciption.
  1693. *
  1694. */
  1695. pjsip_rx_data *rdata;
  1696. pjsip_event_hdr *event_hdr;
  1697. pjsip_expires_hdr *expires;
  1698. pjsip_msg *msg;
  1699. pjsip_tx_data *tdata;
  1700. int st_code = 200;
  1701. pj_str_t *st_text = NULL;
  1702. pjsip_hdr res_hdr;
  1703. pjsip_msg_body *body = NULL;
  1704. pjsip_evsub_state old_state;
  1705. pj_str_t old_state_str;
  1706. pj_str_t reason = { NULL, 0 };
  1707. pj_status_t status;
  1708. /* Only wants to handle the first event when the request is
  1709. * received.
  1710. */
  1711. if (tsx->state != PJSIP_TSX_STATE_TRYING)
  1712. return;
  1713. rdata = event->body.tsx_state.src.rdata;
  1714. msg = rdata->msg_info.msg;
  1715. /* Set expiration time based on client request (in Expires header),
  1716. * or package default expiration time.
  1717. */
  1718. event_hdr = (pjsip_event_hdr*)
  1719. pjsip_msg_find_hdr_by_names(msg, &STR_EVENT,
  1720. &STR_EVENT, NULL);
  1721. expires = (pjsip_expires_hdr*)
  1722. pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL);
  1723. if (event_hdr && expires) {
  1724. struct evpkg *evpkg;
  1725. evpkg = find_pkg(&event_hdr->event_type);
  1726. if (evpkg) {
  1727. if (expires->ivalue < evpkg->pkg_expires)
  1728. sub->expires->ivalue = expires->ivalue;
  1729. else
  1730. sub->expires->ivalue = evpkg->pkg_expires;
  1731. }
  1732. }
  1733. /* Update time (before calling on_rx_refresh, since application
  1734. * will send NOTIFY.
  1735. */
  1736. update_expires(sub, sub->expires->ivalue);
  1737. /* Save old state.
  1738. * If application respond with non-2xx, revert to old state.
  1739. * But if subscriber wants to unsubscribe, there is no
  1740. * turning back.
  1741. */
  1742. old_state = sub->state;
  1743. old_state_str = sub->state_str;
  1744. /* Must set this before calling set_state(), to prevent evsub
  1745. * from being destroyed.
  1746. */
  1747. sub->calling_on_rx_refresh = PJ_TRUE;
  1748. if (sub->expires->ivalue == 0) {
  1749. pj_str_t timeout = { "timeout", 7};
  1750. PJ_LOG(4,(sub->obj_name, "Receiving unsubscription request "
  1751. "(Expires=0)."));
  1752. set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, event,
  1753. &timeout);
  1754. } else if (sub->state == PJSIP_EVSUB_STATE_NULL) {
  1755. sub->state = PJSIP_EVSUB_STATE_ACCEPTED;
  1756. sub->state_str = evsub_state_names[sub->state];
  1757. }
  1758. /* Call application's on_rx_refresh, just in case it wants to send
  1759. * response other than 200 (OK)
  1760. */
  1761. pj_list_init(&res_hdr);
  1762. if (sub->user.on_rx_refresh && sub->call_cb) {
  1763. (*sub->user.on_rx_refresh)(sub, rdata, &st_code, &st_text,
  1764. &res_hdr, &body);
  1765. /* We shouldn't fail an unsubscription request, should we? */
  1766. if (sub->expires->ivalue == 0) st_code = 200;
  1767. }
  1768. sub->calling_on_rx_refresh = PJ_FALSE;
  1769. /* Application MUST specify final response! */
  1770. PJ_ASSERT_ON_FAIL(st_code >= 200, {st_code=200; });
  1771. /* Must be a valid status code */
  1772. PJ_ASSERT_ON_FAIL(st_code <= 699, {st_code=500; });
  1773. /* Create and send response */
  1774. status = create_response(sub, rdata, st_code, st_text, &res_hdr,
  1775. body, &tdata);
  1776. if (status == PJ_SUCCESS) {
  1777. /* Add expires header: */
  1778. pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
  1779. pjsip_hdr_shallow_clone(tdata->pool,
  1780. sub->expires));
  1781. /* Send */
  1782. pjsip_dlg_send_response(sub->dlg, tsx, tdata);
  1783. }
  1784. /* Update state or revert state */
  1785. if (st_code/100==2) {
  1786. if (sub->state == PJSIP_EVSUB_STATE_NULL) {
  1787. set_state(sub, sub->state, NULL, event, &reason);
  1788. }
  1789. /* Set UAS timeout timer, when state is not terminated. */
  1790. if (sub->state != PJSIP_EVSUB_STATE_TERMINATED) {
  1791. PJ_LOG(5,(sub->obj_name, "UAS timeout in %d seconds",
  1792. sub->expires->ivalue));
  1793. set_timer(sub, TIMER_TYPE_UAS_TIMEOUT,
  1794. sub->expires->ivalue);
  1795. }
  1796. } else {
  1797. sub->state = old_state;
  1798. sub->state_str = old_state_str;
  1799. }
  1800. /* Send the pending NOTIFY sent by app from inside
  1801. * on_rx_refresh() callback.
  1802. */
  1803. //pj_assert(sub->pending_notify);
  1804. /* Make sure that pending_notify is set. */
  1805. if (sub->pending_notify) {
  1806. status = pjsip_evsub_send_request(sub, sub->pending_notify);
  1807. sub->pending_notify = NULL;
  1808. }
  1809. } else if (pjsip_method_cmp(&tsx->method, &pjsip_notify_method)==0) {
  1810. /* Handle authentication */
  1811. if (tsx->state == PJSIP_TSX_STATE_COMPLETED &&
  1812. (tsx->status_code==401 || tsx->status_code==407))
  1813. {
  1814. pjsip_tx_data *tdata;
  1815. pj_status_t status;
  1816. pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
  1817. /* Handled by other module already (e.g: invite module) */
  1818. if (tsx->last_tx->auth_retry)
  1819. return;
  1820. status = pjsip_auth_clt_reinit_req(&sub->dlg->auth_sess, rdata,
  1821. tsx->last_tx, &tdata);
  1822. if (status == PJ_SUCCESS)
  1823. status = pjsip_dlg_send_request(sub->dlg, tdata, -1, NULL);
  1824. if (status != PJ_SUCCESS) {
  1825. /* Can't authenticate. Terminate session (?) */
  1826. set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, NULL,
  1827. &tsx->status_text);
  1828. }
  1829. return;
  1830. }
  1831. if (sub->state == PJSIP_EVSUB_STATE_TERMINATED)
  1832. return;
  1833. /* NOTIFY failure check */
  1834. if (tsx->status_code/100 != 2) {
  1835. pj_bool_t should_terminate_sub = PJ_FALSE;
  1836. if (event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
  1837. if (tsx->status_code == 481) {
  1838. should_terminate_sub = PJ_TRUE;
  1839. } else {
  1840. pjsip_retry_after_hdr *retry_after;
  1841. pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
  1842. pjsip_msg *msg = rdata->msg_info.msg;
  1843. retry_after = (pjsip_retry_after_hdr*)
  1844. pjsip_msg_find_hdr_by_name(msg, &STR_RETRY_AFTER, NULL);
  1845. if (!retry_after) {
  1846. should_terminate_sub = PJ_TRUE;
  1847. }
  1848. }
  1849. } else if (event->body.tsx_state.type == PJSIP_EVENT_TIMER) {
  1850. if (tsx->status_code == 408) {
  1851. should_terminate_sub = PJ_TRUE;
  1852. }
  1853. }
  1854. /*
  1855. * Terminate event usage if we receive non 2xx without retry_after
  1856. * parameter, 481, 408 responses.
  1857. */
  1858. if (should_terminate_sub) {
  1859. set_state(sub, PJSIP_EVSUB_STATE_TERMINATED, NULL, event,
  1860. &tsx->status_text);
  1861. return;
  1862. }
  1863. }
  1864. } else {
  1865. /*
  1866. * Unexpected method!
  1867. */
  1868. PJ_LOG(4,(sub->obj_name, "Unexpected transaction method %.*s",
  1869. (int)tsx->method.name.slen, tsx->method.name.ptr));
  1870. }
  1871. }
  1872. /*
  1873. * Notification when transaction state has changed!
  1874. */
  1875. static void mod_evsub_on_tsx_state(pjsip_transaction *tsx, pjsip_event *event)
  1876. {
  1877. pjsip_evsub *sub = pjsip_tsx_get_evsub(tsx);
  1878. if (sub == NULL) {
  1879. sub = on_new_transaction(tsx, event);
  1880. if (sub == NULL)
  1881. return;
  1882. }
  1883. /* Call on_tsx_state callback, if any. */
  1884. if (sub->user.on_tsx_state && sub->call_cb)
  1885. (*sub->user.on_tsx_state)(sub, tsx, event);
  1886. /* Process the event: */
  1887. if (sub->role == PJSIP_ROLE_UAC) {
  1888. on_tsx_state_uac(sub, tsx, event);
  1889. } else {
  1890. on_tsx_state_uas(sub, tsx, event);
  1891. }
  1892. /* Check transaction TERMINATE event */
  1893. if (tsx->state == PJSIP_TSX_STATE_TERMINATED) {
  1894. --sub->pending_tsx;
  1895. if (sub->state == PJSIP_EVSUB_STATE_TERMINATED &&
  1896. sub->pending_tsx == 0)
  1897. {
  1898. evsub_destroy(sub);
  1899. }
  1900. }
  1901. }