location.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. #include "asterisk.h"
  19. #include <pjsip.h>
  20. #include <pjlib.h>
  21. #include "asterisk/res_pjsip.h"
  22. #include "asterisk/logger.h"
  23. #include "asterisk/astobj2.h"
  24. #include "asterisk/paths.h"
  25. #include "asterisk/sorcery.h"
  26. #include "asterisk/taskprocessor.h"
  27. #include "include/res_pjsip_private.h"
  28. #include "asterisk/res_pjsip_cli.h"
  29. #include "asterisk/statsd.h"
  30. #include "asterisk/named_locks.h"
  31. #include "asterisk/res_pjproject.h"
  32. static int pj_max_hostname = PJ_MAX_HOSTNAME;
  33. static int pjsip_max_url_size = PJSIP_MAX_URL_SIZE;
  34. /*! \brief Destructor for AOR */
  35. static void aor_destroy(void *obj)
  36. {
  37. struct ast_sip_aor *aor = obj;
  38. ao2_cleanup(aor->permanent_contacts);
  39. ast_string_field_free_memory(aor);
  40. ast_free(aor->voicemail_extension);
  41. }
  42. /*! \brief Allocator for AOR */
  43. static void *aor_alloc(const char *name)
  44. {
  45. struct ast_sip_aor *aor = ast_sorcery_generic_alloc(sizeof(struct ast_sip_aor), aor_destroy);
  46. if (!aor) {
  47. return NULL;
  48. }
  49. ast_string_field_init(aor, 128);
  50. return aor;
  51. }
  52. /*! \brief Internal callback function which destroys the specified contact */
  53. static int destroy_contact(void *obj, void *arg, int flags)
  54. {
  55. struct ast_sip_contact *contact = obj;
  56. ast_sip_location_delete_contact(contact);
  57. return CMP_MATCH;
  58. }
  59. static void aor_deleted_observer(const void *object)
  60. {
  61. const struct ast_sip_aor *aor = object;
  62. const char *aor_id = ast_sorcery_object_get_id(object);
  63. /* Give enough space for ;@ at the end, since that is our object naming scheme */
  64. size_t prefix_len = strlen(aor_id) + sizeof(";@") - 1;
  65. char prefix[prefix_len + 1];
  66. struct ao2_container *contacts;
  67. if (aor->permanent_contacts) {
  68. ao2_callback(aor->permanent_contacts, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, destroy_contact, NULL);
  69. }
  70. sprintf(prefix, "%s;@", aor_id); /* Safe */
  71. if (!(contacts = ast_sorcery_retrieve_by_prefix(ast_sip_get_sorcery(), "contact", prefix, prefix_len))) {
  72. return;
  73. }
  74. /* Destroy any contacts that may still exist that were made for this AoR */
  75. ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, destroy_contact, NULL);
  76. ao2_ref(contacts, -1);
  77. }
  78. /*! \brief Observer for contacts so state can be updated on respective endpoints */
  79. static const struct ast_sorcery_observer aor_observer = {
  80. .deleted = aor_deleted_observer,
  81. };
  82. /*! \brief Destructor for contact */
  83. static void contact_destroy(void *obj)
  84. {
  85. struct ast_sip_contact *contact = obj;
  86. ast_string_field_free_memory(contact);
  87. ast_free(contact->aor);
  88. ao2_cleanup(contact->endpoint);
  89. }
  90. /*! \brief Allocator for contact */
  91. static void *contact_alloc(const char *name)
  92. {
  93. struct ast_sip_contact *contact = ast_sorcery_generic_alloc(sizeof(*contact), contact_destroy);
  94. char *id = ast_strdupa(name);
  95. char *aor = id;
  96. char *aor_separator = NULL;
  97. if (!contact) {
  98. return NULL;
  99. }
  100. if (ast_string_field_init(contact, 256)
  101. || ast_string_field_init_extended(contact, endpoint_name)
  102. || ast_string_field_init_extended(contact, reg_server)
  103. || ast_string_field_init_extended(contact, via_addr)
  104. || ast_string_field_init_extended(contact, call_id)) {
  105. ao2_cleanup(contact);
  106. return NULL;
  107. }
  108. /* Dynamic contacts are delimited with ";@" and static ones with "@@" */
  109. if ((aor_separator = strstr(id, ";@")) || (aor_separator = strstr(id, "@@"))) {
  110. *aor_separator = '\0';
  111. }
  112. ast_assert(aor_separator != NULL);
  113. contact->aor = ast_strdup(aor);
  114. if (!contact->aor) {
  115. ao2_cleanup(contact);
  116. return NULL;
  117. }
  118. return contact;
  119. }
  120. struct ast_sip_aor *ast_sip_location_retrieve_aor(const char *aor_name)
  121. {
  122. return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "aor", aor_name);
  123. }
  124. /*! \brief Internal callback function which deletes and unlinks any expired contacts */
  125. static int contact_expire(void *obj, void *arg, int flags)
  126. {
  127. struct ast_sip_contact *contact = obj;
  128. /* If the contact has not yet expired it is valid */
  129. if (ast_tvdiff_ms(contact->expiration_time, ast_tvnow()) > 0) {
  130. return 0;
  131. }
  132. ast_sip_location_delete_contact(contact);
  133. return CMP_MATCH;
  134. }
  135. /*! \brief Internal callback function which links static contacts into another container */
  136. static int contact_link_static(void *obj, void *arg, int flags)
  137. {
  138. struct ao2_container *dest = arg;
  139. ao2_link(dest, obj);
  140. return 0;
  141. }
  142. /*! \brief Internal callback function which removes any contact which is unreachable */
  143. static int contact_remove_unreachable(void *obj, void *arg, int flags)
  144. {
  145. struct ast_sip_contact *contact = obj;
  146. struct ast_sip_contact_status *status;
  147. int unreachable;
  148. status = ast_sip_get_contact_status(contact);
  149. if (!status) {
  150. return 0;
  151. }
  152. unreachable = (status->status == UNAVAILABLE);
  153. ao2_ref(status, -1);
  154. return unreachable ? CMP_MATCH : 0;
  155. }
  156. struct ast_sip_contact *ast_sip_location_retrieve_first_aor_contact(const struct ast_sip_aor *aor)
  157. {
  158. return ast_sip_location_retrieve_first_aor_contact_filtered(aor, AST_SIP_CONTACT_FILTER_DEFAULT);
  159. }
  160. struct ast_sip_contact *ast_sip_location_retrieve_first_aor_contact_filtered(const struct ast_sip_aor *aor,
  161. unsigned int flags)
  162. {
  163. struct ao2_container *contacts;
  164. struct ast_sip_contact *contact = NULL;
  165. contacts = ast_sip_location_retrieve_aor_contacts_filtered(aor, flags);
  166. if (contacts && ao2_container_count(contacts)) {
  167. /* Get the first AOR contact in the container. */
  168. contact = ao2_callback(contacts, 0, NULL, NULL);
  169. }
  170. ao2_cleanup(contacts);
  171. return contact;
  172. }
  173. struct ao2_container *ast_sip_location_retrieve_aor_contacts_nolock(const struct ast_sip_aor *aor)
  174. {
  175. return ast_sip_location_retrieve_aor_contacts_nolock_filtered(aor, AST_SIP_CONTACT_FILTER_DEFAULT);
  176. }
  177. struct ao2_container *ast_sip_location_retrieve_aor_contacts_nolock_filtered(const struct ast_sip_aor *aor,
  178. unsigned int flags)
  179. {
  180. /* Give enough space for ;@ at the end, since that is our object naming scheme */
  181. size_t prefix_len = strlen(ast_sorcery_object_get_id(aor)) + sizeof(";@") - 1;
  182. char prefix[prefix_len + 1];
  183. struct ao2_container *contacts;
  184. sprintf(prefix, "%s;@", ast_sorcery_object_get_id(aor)); /* Safe */
  185. if (!(contacts = ast_sorcery_retrieve_by_prefix(ast_sip_get_sorcery(), "contact", prefix, prefix_len))) {
  186. return NULL;
  187. }
  188. /* Prune any expired contacts and delete them, we do this first because static contacts can never expire */
  189. ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, contact_expire, NULL);
  190. /* Add any permanent contacts from the AOR */
  191. if (aor->permanent_contacts) {
  192. ao2_callback(aor->permanent_contacts, OBJ_NODATA, contact_link_static, contacts);
  193. }
  194. if (flags & AST_SIP_CONTACT_FILTER_REACHABLE) {
  195. ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, contact_remove_unreachable, NULL);
  196. }
  197. return contacts;
  198. }
  199. struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_sip_aor *aor)
  200. {
  201. return ast_sip_location_retrieve_aor_contacts_filtered(aor, AST_SIP_CONTACT_FILTER_DEFAULT);
  202. }
  203. struct ao2_container *ast_sip_location_retrieve_aor_contacts_filtered(const struct ast_sip_aor *aor,
  204. unsigned int flags)
  205. {
  206. struct ao2_container *contacts;
  207. struct ast_named_lock *lock;
  208. lock = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "aor", ast_sorcery_object_get_id(aor));
  209. if (!lock) {
  210. return NULL;
  211. }
  212. ao2_lock(lock);
  213. contacts = ast_sip_location_retrieve_aor_contacts_nolock_filtered(aor, flags);
  214. ao2_unlock(lock);
  215. ast_named_lock_put(lock);
  216. return contacts;
  217. }
  218. void ast_sip_location_retrieve_contact_and_aor_from_list(const char *aor_list, struct ast_sip_aor **aor,
  219. struct ast_sip_contact **contact)
  220. {
  221. ast_sip_location_retrieve_contact_and_aor_from_list_filtered(aor_list, AST_SIP_CONTACT_FILTER_DEFAULT, aor, contact);
  222. }
  223. void ast_sip_location_retrieve_contact_and_aor_from_list_filtered(const char *aor_list, unsigned int flags,
  224. struct ast_sip_aor **aor, struct ast_sip_contact **contact)
  225. {
  226. char *aor_name;
  227. char *rest;
  228. /* If the location is still empty we have nowhere to go */
  229. if (ast_strlen_zero(aor_list) || !(rest = ast_strdupa(aor_list))) {
  230. ast_log(LOG_WARNING, "Unable to determine contacts from empty aor list\n");
  231. return;
  232. }
  233. *aor = NULL;
  234. *contact = NULL;
  235. while ((aor_name = ast_strip(strsep(&rest, ",")))) {
  236. *aor = ast_sip_location_retrieve_aor(aor_name);
  237. if (!(*aor)) {
  238. continue;
  239. }
  240. *contact = ast_sip_location_retrieve_first_aor_contact_filtered(*aor, flags);
  241. /* If a valid contact is available use its URI for dialing */
  242. if (*contact) {
  243. break;
  244. }
  245. ao2_ref(*aor, -1);
  246. *aor = NULL;
  247. }
  248. }
  249. struct ast_sip_contact *ast_sip_location_retrieve_contact_from_aor_list(const char *aor_list)
  250. {
  251. struct ast_sip_aor *aor;
  252. struct ast_sip_contact *contact;
  253. ast_sip_location_retrieve_contact_and_aor_from_list(aor_list, &aor, &contact);
  254. ao2_cleanup(aor);
  255. return contact;
  256. }
  257. static int permanent_uri_sort_fn(const void *obj_left, const void *obj_right, int flags);
  258. static int cli_contact_populate_container(void *obj, void *arg, int flags);
  259. static int gather_contacts_for_aor(void *obj, void *arg, int flags)
  260. {
  261. struct ao2_container *aor_contacts;
  262. struct ast_sip_aor *aor = obj;
  263. struct ao2_container *container = arg;
  264. aor_contacts = ast_sip_location_retrieve_aor_contacts(aor);
  265. if (!aor_contacts) {
  266. return 0;
  267. }
  268. ao2_callback(aor_contacts, OBJ_MULTIPLE | OBJ_NODATA, cli_contact_populate_container,
  269. container);
  270. ao2_ref(aor_contacts, -1);
  271. return CMP_MATCH;
  272. }
  273. struct ao2_container *ast_sip_location_retrieve_contacts_from_aor_list(const char *aor_list)
  274. {
  275. struct ao2_container *contacts;
  276. contacts = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK,
  277. AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, permanent_uri_sort_fn, NULL);
  278. if (!contacts) {
  279. return NULL;
  280. }
  281. ast_sip_for_each_aor(aor_list, gather_contacts_for_aor, contacts);
  282. return contacts;
  283. }
  284. struct ast_sip_contact *ast_sip_location_retrieve_contact(const char *contact_name)
  285. {
  286. return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "contact", contact_name);
  287. }
  288. struct ast_sip_contact *ast_sip_location_create_contact(struct ast_sip_aor *aor,
  289. const char *uri, struct timeval expiration_time, const char *path_info,
  290. const char *user_agent, const char *via_addr, int via_port, const char *call_id,
  291. int prune_on_boot, struct ast_sip_endpoint *endpoint)
  292. {
  293. struct ast_sip_contact *contact;
  294. char name[MAX_OBJECT_FIELD * 2 + 3];
  295. char hash[33];
  296. ast_md5_hash(hash, uri);
  297. snprintf(name, sizeof(name), "%s;@%s", ast_sorcery_object_get_id(aor), hash);
  298. contact = ast_sorcery_alloc(ast_sip_get_sorcery(), "contact", name);
  299. if (!contact) {
  300. return NULL;
  301. }
  302. ast_string_field_set(contact, uri, uri);
  303. contact->expiration_time = expiration_time;
  304. contact->qualify_frequency = aor->qualify_frequency;
  305. contact->qualify_timeout = aor->qualify_timeout;
  306. contact->authenticate_qualify = aor->authenticate_qualify;
  307. if (path_info && aor->support_path) {
  308. ast_string_field_set(contact, path, path_info);
  309. }
  310. if (!ast_strlen_zero(aor->outbound_proxy)) {
  311. ast_string_field_set(contact, outbound_proxy, aor->outbound_proxy);
  312. }
  313. if (!ast_strlen_zero(user_agent)) {
  314. ast_string_field_set(contact, user_agent, user_agent);
  315. }
  316. if (!ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
  317. ast_string_field_set(contact, reg_server, ast_config_AST_SYSTEM_NAME);
  318. }
  319. if (!ast_strlen_zero(via_addr)) {
  320. ast_string_field_set(contact, via_addr, via_addr);
  321. }
  322. contact->via_port = via_port;
  323. if (!ast_strlen_zero(call_id)) {
  324. ast_string_field_set(contact, call_id, call_id);
  325. }
  326. contact->endpoint = ao2_bump(endpoint);
  327. if (endpoint) {
  328. ast_string_field_set(contact, endpoint_name, ast_sorcery_object_get_id(endpoint));
  329. }
  330. contact->prune_on_boot = prune_on_boot;
  331. if (ast_sorcery_create(ast_sip_get_sorcery(), contact)) {
  332. ao2_ref(contact, -1);
  333. return NULL;
  334. }
  335. return contact;
  336. }
  337. int ast_sip_location_add_contact_nolock(struct ast_sip_aor *aor, const char *uri,
  338. struct timeval expiration_time, const char *path_info, const char *user_agent,
  339. const char *via_addr, int via_port, const char *call_id,
  340. struct ast_sip_endpoint *endpoint)
  341. {
  342. struct ast_sip_contact *contact;
  343. contact = ast_sip_location_create_contact(aor, uri, expiration_time, path_info,
  344. user_agent, via_addr, via_port, call_id, 0, endpoint);
  345. ao2_cleanup(contact);
  346. return contact ? 0 : -1;
  347. }
  348. int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri,
  349. struct timeval expiration_time, const char *path_info, const char *user_agent,
  350. const char *via_addr, int via_port, const char *call_id,
  351. struct ast_sip_endpoint *endpoint)
  352. {
  353. int res;
  354. struct ast_named_lock *lock;
  355. lock = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "aor", ast_sorcery_object_get_id(aor));
  356. if (!lock) {
  357. return -1;
  358. }
  359. ao2_lock(lock);
  360. res = ast_sip_location_add_contact_nolock(aor, uri, expiration_time, path_info, user_agent,
  361. via_addr, via_port, call_id,
  362. endpoint);
  363. ao2_unlock(lock);
  364. ast_named_lock_put(lock);
  365. return res;
  366. }
  367. int ast_sip_location_update_contact(struct ast_sip_contact *contact)
  368. {
  369. return ast_sorcery_update(ast_sip_get_sorcery(), contact);
  370. }
  371. int ast_sip_location_delete_contact(struct ast_sip_contact *contact)
  372. {
  373. return ast_sorcery_delete(ast_sip_get_sorcery(), contact);
  374. }
  375. static int prune_boot_contacts_cb(void *obj, void *arg, int flags)
  376. {
  377. struct ast_sip_contact *contact = obj;
  378. if (contact->prune_on_boot
  379. && !strcmp(contact->reg_server, ast_config_AST_SYSTEM_NAME ?: "")) {
  380. ast_verb(3, "Removed contact '%s' from AOR '%s' due to system boot\n",
  381. contact->uri, contact->aor);
  382. ast_sip_location_delete_contact(contact);
  383. }
  384. return 0;
  385. }
  386. void ast_sip_location_prune_boot_contacts(void)
  387. {
  388. struct ao2_container *contacts;
  389. contacts = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "contact",
  390. AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
  391. if (contacts) {
  392. ao2_callback(contacts, 0, prune_boot_contacts_cb, NULL);
  393. ao2_ref(contacts, -1);
  394. }
  395. }
  396. /*! \brief Custom handler for translating from a string timeval to actual structure */
  397. static int expiration_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
  398. {
  399. struct ast_sip_contact *contact = obj;
  400. return ast_get_timeval(var->value, &contact->expiration_time, ast_tv(0, 0), NULL);
  401. }
  402. /*! \brief Custom handler for translating from an actual structure timeval to string */
  403. static int expiration_struct2str(const void *obj, const intptr_t *args, char **buf)
  404. {
  405. const struct ast_sip_contact *contact = obj;
  406. return (ast_asprintf(buf, "%ld", contact->expiration_time.tv_sec) < 0) ? -1 : 0;
  407. }
  408. static int permanent_uri_sort_fn(const void *obj_left, const void *obj_right, int flags)
  409. {
  410. const struct ast_sip_contact *object_left = obj_left;
  411. const struct ast_sip_contact *object_right = obj_right;
  412. const char *right_key = obj_right;
  413. int cmp;
  414. switch (flags & OBJ_SEARCH_MASK) {
  415. case OBJ_SEARCH_OBJECT:
  416. right_key = ast_sorcery_object_get_id(object_right);
  417. /* Fall through */
  418. case OBJ_SEARCH_KEY:
  419. cmp = strcmp(ast_sorcery_object_get_id(object_left), right_key);
  420. break;
  421. case OBJ_SEARCH_PARTIAL_KEY:
  422. /*
  423. * We could also use a partial key struct containing a length
  424. * so strlen() does not get called for every comparison instead.
  425. */
  426. cmp = strncmp(ast_sorcery_object_get_id(object_left), right_key, strlen(right_key));
  427. break;
  428. default:
  429. /* Sort can only work on something with a full or partial key. */
  430. ast_assert(0);
  431. cmp = 0;
  432. break;
  433. }
  434. return cmp;
  435. }
  436. int ast_sip_validate_uri_length(const char *contact_uri)
  437. {
  438. int max_length = pj_max_hostname - 1;
  439. char *contact = ast_strdupa(contact_uri);
  440. char *host;
  441. char *at;
  442. int theres_a_port = 0;
  443. if (strlen(contact_uri) > pjsip_max_url_size - 1) {
  444. return -1;
  445. }
  446. contact = ast_strip_quoted(contact, "<", ">");
  447. if (!strncasecmp(contact, "sip:", 4)) {
  448. host = contact + 4;
  449. } else if (!strncasecmp(contact, "sips:", 5)) {
  450. host = contact + 5;
  451. } else {
  452. /* Not a SIP URI */
  453. return -1;
  454. }
  455. at = strchr(contact, '@');
  456. if (at) {
  457. /* sip[s]:user@host */
  458. host = at + 1;
  459. }
  460. if (host[0] == '[') {
  461. /* Host is an IPv6 address. Just get up to the matching bracket */
  462. char *close_bracket;
  463. close_bracket = strchr(host, ']');
  464. if (!close_bracket) {
  465. return -1;
  466. }
  467. close_bracket++;
  468. if (*close_bracket == ':') {
  469. theres_a_port = 1;
  470. }
  471. *close_bracket = '\0';
  472. } else {
  473. /* uri parameters could contain ';' so trim them off first */
  474. host = strsep(&host, ";?");
  475. /* Host is FQDN or IPv4 address. Need to find closing delimiter */
  476. if (strchr(host, ':')) {
  477. theres_a_port = 1;
  478. host = strsep(&host, ":");
  479. }
  480. }
  481. if (!theres_a_port) {
  482. max_length -= strlen("_sips.tcp.");
  483. }
  484. if (strlen(host) > max_length) {
  485. return -1;
  486. }
  487. return 0;
  488. }
  489. /*! \brief Custom handler for permanent URIs */
  490. static int permanent_uri_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  491. {
  492. struct ast_sip_aor *aor = obj;
  493. const char *aor_id = ast_sorcery_object_get_id(aor);
  494. char *contacts;
  495. char *contact_uri;
  496. if (ast_strlen_zero(var->value)) {
  497. return 0;
  498. }
  499. contacts = ast_strdupa(var->value);
  500. while ((contact_uri = ast_strip(strsep(&contacts, ",")))) {
  501. struct ast_sip_contact *contact;
  502. struct ast_sip_contact_status *status;
  503. char hash[33];
  504. char contact_id[strlen(aor_id) + sizeof(hash) + 2];
  505. if (ast_strlen_zero(contact_uri)) {
  506. continue;
  507. }
  508. if (ast_sip_validate_uri_length(contact_uri)) {
  509. ast_log(LOG_ERROR, "Contact uri or hostname length exceeds pjproject limit or is not a sip(s) uri: %s\n", contact_uri);
  510. return -1;
  511. }
  512. if (!aor->permanent_contacts) {
  513. aor->permanent_contacts = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK,
  514. AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, permanent_uri_sort_fn, NULL);
  515. if (!aor->permanent_contacts) {
  516. return -1;
  517. }
  518. }
  519. ast_md5_hash(hash, contact_uri);
  520. snprintf(contact_id, sizeof(contact_id), "%s@@%s", aor_id, hash);
  521. contact = ast_sorcery_alloc(ast_sip_get_sorcery(), "contact", contact_id);
  522. if (!contact) {
  523. return -1;
  524. }
  525. ast_string_field_set(contact, uri, contact_uri);
  526. status = ast_res_pjsip_find_or_create_contact_status(contact);
  527. if (!status) {
  528. ao2_ref(contact, -1);
  529. return -1;
  530. }
  531. ao2_ref(status, -1);
  532. ao2_link(aor->permanent_contacts, contact);
  533. ao2_ref(contact, -1);
  534. }
  535. return 0;
  536. }
  537. static int contact_to_var_list(void *object, void *arg, int flags)
  538. {
  539. struct ast_sip_contact_wrapper *wrapper = object;
  540. struct ast_variable **var = arg;
  541. ast_variable_list_append(&*var, ast_variable_new("contact", wrapper->contact->uri, ""));
  542. return 0;
  543. }
  544. static int contacts_to_var_list(const void *obj, struct ast_variable **fields)
  545. {
  546. const struct ast_sip_aor *aor = obj;
  547. ast_sip_for_each_contact(aor, contact_to_var_list, fields);
  548. return 0;
  549. }
  550. static int voicemail_extension_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  551. {
  552. struct ast_sip_aor *aor = obj;
  553. aor->voicemail_extension = ast_strdup(var->value);
  554. return aor->voicemail_extension ? 0 : -1;
  555. }
  556. static int voicemail_extension_to_str(const void *obj, const intptr_t *args, char **buf)
  557. {
  558. const struct ast_sip_aor *aor = obj;
  559. *buf = ast_strdup(aor->voicemail_extension);
  560. return 0;
  561. }
  562. int ast_sip_for_each_aor(const char *aors, ao2_callback_fn on_aor, void *arg)
  563. {
  564. char *copy;
  565. char *name;
  566. int res;
  567. if (!on_aor || ast_strlen_zero(aors)) {
  568. return 0;
  569. }
  570. copy = ast_strdupa(aors);
  571. while ((name = ast_strip(strsep(&copy, ",")))) {
  572. struct ast_sip_aor *aor;
  573. aor = ast_sip_location_retrieve_aor(name);
  574. if (aor) {
  575. res = on_aor(aor, arg, 0);
  576. ao2_ref(aor, -1);
  577. if (res) {
  578. return -1;
  579. }
  580. }
  581. }
  582. return 0;
  583. }
  584. static void contact_wrapper_destroy(void *obj)
  585. {
  586. struct ast_sip_contact_wrapper *wrapper = obj;
  587. ast_free(wrapper->aor_id);
  588. ast_free(wrapper->contact_id);
  589. ao2_cleanup(wrapper->contact);
  590. }
  591. int ast_sip_for_each_contact(const struct ast_sip_aor *aor,
  592. ao2_callback_fn on_contact, void *arg)
  593. {
  594. struct ao2_container *contacts;
  595. struct ao2_iterator i;
  596. int res = 0;
  597. void *object = NULL;
  598. if (!on_contact ||
  599. !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) {
  600. return 0;
  601. }
  602. i = ao2_iterator_init(contacts, 0);
  603. while ((object = ao2_iterator_next(&i))) {
  604. RAII_VAR(struct ast_sip_contact *, contact, object, ao2_cleanup);
  605. RAII_VAR(struct ast_sip_contact_wrapper *, wrapper, NULL, ao2_cleanup);
  606. const char *aor_id = ast_sorcery_object_get_id(aor);
  607. wrapper = ao2_alloc_options(sizeof(struct ast_sip_contact_wrapper),
  608. contact_wrapper_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
  609. if (!wrapper) {
  610. res = -1;
  611. break;
  612. }
  613. wrapper->contact_id = ast_malloc(strlen(aor_id) + strlen(contact->uri) + 2);
  614. if (!wrapper->contact_id) {
  615. res = -1;
  616. break;
  617. }
  618. sprintf(wrapper->contact_id, "%s/%s", aor_id, contact->uri);
  619. wrapper->aor_id = ast_strdup(aor_id);
  620. if (!wrapper->aor_id) {
  621. res = -1;
  622. break;
  623. }
  624. wrapper->contact = contact;
  625. ao2_bump(wrapper->contact);
  626. if ((res = on_contact(wrapper, arg, 0))) {
  627. break;
  628. }
  629. }
  630. ao2_iterator_destroy(&i);
  631. ao2_ref(contacts, -1);
  632. return res;
  633. }
  634. int ast_sip_contact_to_str(void *object, void *arg, int flags)
  635. {
  636. struct ast_sip_contact_wrapper *wrapper = object;
  637. struct ast_str **buf = arg;
  638. ast_str_append(buf, 0, "%s,", wrapper->contact_id);
  639. return 0;
  640. }
  641. static int sip_aor_to_ami(const struct ast_sip_aor *aor, struct ast_str **buf)
  642. {
  643. struct ast_variable *objset;
  644. struct ast_variable *i;
  645. objset = ast_sorcery_objectset_create2(ast_sip_get_sorcery(), aor,
  646. AST_HANDLER_ONLY_STRING);
  647. if (!objset) {
  648. return -1;
  649. }
  650. ast_str_append(buf, 0, "ObjectType: %s\r\n",
  651. ast_sorcery_object_get_type(aor));
  652. ast_str_append(buf, 0, "ObjectName: %s\r\n",
  653. ast_sorcery_object_get_id(aor));
  654. for (i = objset; i; i = i->next) {
  655. char *camel = ast_to_camel_case(i->name);
  656. if (strcmp(camel, "Contact") == 0) {
  657. ast_free(camel);
  658. camel = NULL;
  659. }
  660. ast_str_append(buf, 0, "%s: %s\r\n", S_OR(camel, "Contacts"), i->value);
  661. ast_free(camel);
  662. }
  663. ast_variables_destroy(objset);
  664. return 0;
  665. }
  666. static int contacts_to_str(const void *obj, const intptr_t *args, char **buf)
  667. {
  668. const struct ast_sip_aor *aor = obj;
  669. struct ast_str *str;
  670. str = ast_str_create(MAX_OBJECT_FIELD);
  671. if (!str) {
  672. *buf = NULL;
  673. return -1;
  674. }
  675. ast_sip_for_each_contact(aor, ast_sip_contact_to_str, &str);
  676. ast_str_truncate(str, -1);
  677. *buf = ast_strdup(ast_str_buffer(str));
  678. ast_free(str);
  679. return *buf ? 0 : -1;
  680. }
  681. static int format_ami_aor_handler(void *obj, void *arg, int flags)
  682. {
  683. struct ast_sip_aor *aor = obj;
  684. struct ast_sip_ami *ami = arg;
  685. const struct ast_sip_endpoint *endpoint = ami->arg;
  686. struct ast_str *buf;
  687. struct ao2_container *contacts;
  688. int total_contacts;
  689. int num_permanent;
  690. buf = ast_sip_create_ami_event("AorDetail", ami);
  691. if (!buf) {
  692. return -1;
  693. }
  694. contacts = ast_sip_location_retrieve_aor_contacts(aor);
  695. if (!contacts) {
  696. ast_free(buf);
  697. return -1;
  698. }
  699. sip_aor_to_ami(aor, &buf);
  700. total_contacts = ao2_container_count(contacts);
  701. num_permanent = aor->permanent_contacts ?
  702. ao2_container_count(aor->permanent_contacts) : 0;
  703. ast_str_append(&buf, 0, "TotalContacts: %d\r\n", total_contacts);
  704. ast_str_append(&buf, 0, "ContactsRegistered: %d\r\n",
  705. total_contacts - num_permanent);
  706. ast_str_append(&buf, 0, "EndpointName: %s\r\n",
  707. ast_sorcery_object_get_id(endpoint));
  708. astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
  709. ami->count++;
  710. ast_free(buf);
  711. ao2_ref(contacts, -1);
  712. return 0;
  713. }
  714. static int format_ami_endpoint_aor(const struct ast_sip_endpoint *endpoint,
  715. struct ast_sip_ami *ami)
  716. {
  717. ami->arg = (void *)endpoint;
  718. return ast_sip_for_each_aor(endpoint->aors,
  719. format_ami_aor_handler, ami);
  720. }
  721. struct ast_sip_endpoint_formatter endpoint_aor_formatter = {
  722. .format_ami = format_ami_endpoint_aor
  723. };
  724. static struct ao2_container *cli_aor_get_container(const char *regex)
  725. {
  726. struct ao2_container *container;
  727. struct ao2_container *s_container;
  728. container = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "aor", regex);
  729. if (!container) {
  730. return NULL;
  731. }
  732. /* Create a sorted container of aors. */
  733. s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
  734. ast_sorcery_object_id_sort, ast_sorcery_object_id_compare);
  735. if (s_container
  736. && ao2_container_dup(s_container, container, 0)) {
  737. ao2_ref(s_container, -1);
  738. s_container = NULL;
  739. }
  740. ao2_ref(container, -1);
  741. return s_container;
  742. }
  743. static int cli_contact_populate_container(void *obj, void *arg, int flags)
  744. {
  745. ao2_link(arg, obj);
  746. return 0;
  747. }
  748. static int cli_aor_gather_contacts(void *obj, void *arg, int flags)
  749. {
  750. struct ast_sip_aor *aor = obj;
  751. return ast_sip_for_each_contact(aor, cli_contact_populate_container, arg);
  752. }
  753. static const char *cli_contact_get_id(const void *obj)
  754. {
  755. const struct ast_sip_contact_wrapper *wrapper = obj;
  756. return wrapper->contact_id;
  757. }
  758. static int cli_contact_sort(const void *obj, const void *arg, int flags)
  759. {
  760. const struct ast_sip_contact_wrapper *left_wrapper = obj;
  761. const struct ast_sip_contact_wrapper *right_wrapper = arg;
  762. const char *right_key = arg;
  763. int cmp = 0;
  764. switch (flags & OBJ_SEARCH_MASK) {
  765. case OBJ_SEARCH_OBJECT:
  766. right_key = right_wrapper->contact_id;
  767. /* Fall through */
  768. case OBJ_SEARCH_KEY:
  769. cmp = strcmp(left_wrapper->contact_id, right_key);
  770. break;
  771. case OBJ_SEARCH_PARTIAL_KEY:
  772. cmp = strncmp(left_wrapper->contact_id, right_key, strlen(right_key));
  773. break;
  774. default:
  775. cmp = 0;
  776. break;
  777. }
  778. return cmp;
  779. }
  780. static int cli_contact_compare(void *obj, void *arg, int flags)
  781. {
  782. const struct ast_sip_contact_wrapper *left_wrapper = obj;
  783. const struct ast_sip_contact_wrapper *right_wrapper = arg;
  784. const char *right_key = arg;
  785. int cmp = 0;
  786. switch (flags & OBJ_SEARCH_MASK) {
  787. case OBJ_SEARCH_OBJECT:
  788. right_key = right_wrapper->contact_id;
  789. /* Fall through */
  790. case OBJ_SEARCH_KEY:
  791. if (strcmp(left_wrapper->contact_id, right_key) == 0) {;
  792. cmp = CMP_MATCH | CMP_STOP;
  793. }
  794. break;
  795. case OBJ_SEARCH_PARTIAL_KEY:
  796. if (strncmp(left_wrapper->contact_id, right_key, strlen(right_key)) == 0) {
  797. cmp = CMP_MATCH;
  798. }
  799. break;
  800. default:
  801. cmp = 0;
  802. break;
  803. }
  804. return cmp;
  805. }
  806. static int cli_contact_iterate(void *container, ao2_callback_fn callback, void *args)
  807. {
  808. return ast_sip_for_each_contact(container, callback, args);
  809. }
  810. static int cli_filter_contacts(void *obj, void *arg, int flags)
  811. {
  812. struct ast_sip_contact_wrapper *wrapper = obj;
  813. regex_t *regexbuf = arg;
  814. if (!regexec(regexbuf, wrapper->contact_id, 0, NULL, 0)) {
  815. return 0;
  816. }
  817. return CMP_MATCH;
  818. }
  819. static int cli_gather_contact(void *obj, void *arg, int flags)
  820. {
  821. struct ast_sip_contact *contact = obj;
  822. RAII_VAR(struct ast_sip_contact_wrapper *, wrapper, NULL, ao2_cleanup);
  823. if (strcmp(contact->reg_server, ast_config_AST_SYSTEM_NAME ?: "")) {
  824. return 0;
  825. }
  826. wrapper = ao2_alloc_options(sizeof(struct ast_sip_contact_wrapper),
  827. contact_wrapper_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
  828. if (!wrapper) {
  829. return -1;
  830. }
  831. wrapper->contact_id = ast_malloc(strlen(contact->aor) + strlen(contact->uri) + 2);
  832. if (!wrapper->contact_id) {
  833. return -1;
  834. }
  835. sprintf(wrapper->contact_id, "%s/%s", contact->aor, contact->uri);
  836. wrapper->aor_id = ast_strdup(contact->aor);
  837. if (!wrapper->aor_id) {
  838. return -1;
  839. }
  840. wrapper->contact = ao2_bump(contact);
  841. ao2_link(arg, wrapper);
  842. return 0;
  843. }
  844. static struct ao2_container *cli_contact_get_container(const char *regex)
  845. {
  846. RAII_VAR(struct ao2_container *, aors, NULL, ao2_cleanup);
  847. RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
  848. RAII_VAR(struct ast_variable *, var_aor, NULL, ast_variables_destroy);
  849. struct ao2_container *contacts_container;
  850. regex_t regexbuf;
  851. if (!(var_aor = ast_variable_new("contact !=", "", ""))) {
  852. return NULL;
  853. }
  854. contacts_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
  855. cli_contact_sort, cli_contact_compare);
  856. if (!contacts_container) {
  857. return NULL;
  858. }
  859. contacts = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "contact", regex);
  860. if (!contacts) {
  861. ao2_ref(contacts_container, -1);
  862. return NULL;
  863. }
  864. ao2_callback(contacts, OBJ_NODATA, cli_gather_contact, contacts_container);
  865. aors = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(),
  866. "aor", AST_RETRIEVE_FLAG_MULTIPLE, var_aor);
  867. if (!aors) {
  868. ao2_ref(contacts_container, -1);
  869. return NULL;
  870. }
  871. ao2_callback(aors, OBJ_NODATA, cli_aor_gather_contacts, contacts_container);
  872. if (!ast_strlen_zero(regex)) {
  873. if (regcomp(&regexbuf, regex, REG_EXTENDED | REG_NOSUB)) {
  874. ao2_ref(contacts_container, -1);
  875. return NULL;
  876. }
  877. ao2_callback(contacts_container, OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA, cli_filter_contacts, &regexbuf);
  878. regfree(&regexbuf);
  879. }
  880. return contacts_container;
  881. }
  882. static void *cli_contact_retrieve_by_id(const char *id)
  883. {
  884. struct ao2_container *container;
  885. void *obj;
  886. container = cli_contact_get_container("");
  887. if (!container) {
  888. return NULL;
  889. }
  890. obj = ao2_find(container, id, OBJ_SEARCH_KEY);
  891. ao2_ref(container, -1);
  892. return obj;
  893. }
  894. static int cli_contact_print_header(void *obj, void *arg, int flags)
  895. {
  896. struct ast_sip_cli_context *context = arg;
  897. int indent = CLI_INDENT_TO_SPACES(context->indent_level);
  898. int filler = CLI_LAST_TABSTOP - indent - 23;
  899. ast_assert(context->output_buffer != NULL);
  900. ast_str_append(&context->output_buffer, 0,
  901. "%*s: <Aor/ContactUri%*.*s> <Hash....> <Status> <RTT(ms)..>\n",
  902. indent, "Contact", filler, filler, CLI_HEADER_FILLER);
  903. return 0;
  904. }
  905. static int cli_contact_print_body(void *obj, void *arg, int flags)
  906. {
  907. struct ast_sip_contact_wrapper *wrapper = obj;
  908. struct ast_sip_contact *contact = wrapper->contact;
  909. struct ast_sip_cli_context *context = arg;
  910. int indent;
  911. int flexwidth;
  912. const char *contact_id = ast_sorcery_object_get_id(contact);
  913. const char *hash_start = contact_id + strlen(contact->aor) + 2;
  914. struct ast_sip_contact_status *status;
  915. ast_assert(contact->uri != NULL);
  916. ast_assert(context->output_buffer != NULL);
  917. status = ast_sip_get_contact_status(contact);
  918. indent = CLI_INDENT_TO_SPACES(context->indent_level);
  919. flexwidth = CLI_LAST_TABSTOP - indent - 9 - strlen(contact->aor) + 1;
  920. ast_str_append(&context->output_buffer, 0, "%*s: %s/%-*.*s %-10.10s %-7.7s %11.3f\n",
  921. indent,
  922. "Contact",
  923. contact->aor,
  924. flexwidth, flexwidth,
  925. contact->uri,
  926. hash_start,
  927. ast_sip_get_contact_short_status_label(status ? status->status : UNKNOWN),
  928. (status && (status->status != UNKNOWN) ? ((long long) status->rtt) / 1000.0 : NAN));
  929. ao2_cleanup(status);
  930. return 0;
  931. }
  932. static int cli_aor_iterate(void *container, ao2_callback_fn callback, void *args)
  933. {
  934. const char *aor_list = container;
  935. return ast_sip_for_each_aor(aor_list, callback, args);
  936. }
  937. static void *cli_aor_retrieve_by_id(const char *id)
  938. {
  939. return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "aor", id);
  940. }
  941. static const char *cli_aor_get_id(const void *obj)
  942. {
  943. return ast_sorcery_object_get_id(obj);
  944. }
  945. static int cli_aor_print_header(void *obj, void *arg, int flags)
  946. {
  947. struct ast_sip_cli_context *context = arg;
  948. int indent = CLI_INDENT_TO_SPACES(context->indent_level);
  949. int filler = CLI_LAST_TABSTOP - indent - 7;
  950. ast_assert(context->output_buffer != NULL);
  951. ast_str_append(&context->output_buffer, 0,
  952. "%*s: <Aor%*.*s> <MaxContact>\n",
  953. indent, "Aor", filler, filler, CLI_HEADER_FILLER);
  954. if (context->recurse) {
  955. struct ast_sip_cli_formatter_entry *formatter_entry;
  956. context->indent_level++;
  957. formatter_entry = ast_sip_lookup_cli_formatter("contact");
  958. if (formatter_entry) {
  959. formatter_entry->print_header(NULL, context, 0);
  960. ao2_ref(formatter_entry, -1);
  961. }
  962. context->indent_level--;
  963. }
  964. return 0;
  965. }
  966. static int cli_aor_print_body(void *obj, void *arg, int flags)
  967. {
  968. struct ast_sip_aor *aor = obj;
  969. struct ast_sip_cli_context *context = arg;
  970. int indent;
  971. int flexwidth;
  972. ast_assert(context->output_buffer != NULL);
  973. // context->current_aor = aor;
  974. indent = CLI_INDENT_TO_SPACES(context->indent_level);
  975. flexwidth = CLI_LAST_TABSTOP - indent - 12;
  976. ast_str_append(&context->output_buffer, 0, "%*s: %-*.*s %12u\n",
  977. indent,
  978. "Aor",
  979. flexwidth, flexwidth,
  980. ast_sorcery_object_get_id(aor), aor->max_contacts);
  981. if (context->recurse) {
  982. struct ast_sip_cli_formatter_entry *formatter_entry;
  983. context->indent_level++;
  984. formatter_entry = ast_sip_lookup_cli_formatter("contact");
  985. if (formatter_entry) {
  986. formatter_entry->iterate(aor, formatter_entry->print_body, context);
  987. ao2_ref(formatter_entry, -1);
  988. }
  989. context->indent_level--;
  990. if (context->indent_level == 0) {
  991. ast_str_append(&context->output_buffer, 0, "\n");
  992. }
  993. }
  994. if (context->show_details || (context->show_details_only_level_0 && context->indent_level == 0)) {
  995. ast_str_append(&context->output_buffer, 0, "\n");
  996. ast_sip_cli_print_sorcery_objectset(aor, context, 0);
  997. }
  998. return 0;
  999. }
  1000. static struct ast_cli_entry cli_commands[] = {
  1001. AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Aors",
  1002. .command = "pjsip list aors",
  1003. .usage = "Usage: pjsip list aors [ like <pattern> ]\n"
  1004. " List the configured PJSIP Aors\n"
  1005. " Optional regular expression pattern is used to filter the list.\n"),
  1006. AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Aors",
  1007. .command = "pjsip show aors",
  1008. .usage = "Usage: pjsip show aors [ like <pattern> ]\n"
  1009. " Show the configured PJSIP Aors\n"
  1010. " Optional regular expression pattern is used to filter the list.\n"),
  1011. AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Aor",
  1012. .command = "pjsip show aor",
  1013. .usage = "Usage: pjsip show aor <id>\n"
  1014. " Show the configured PJSIP Aor\n"),
  1015. AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Contacts",
  1016. .command = "pjsip list contacts",
  1017. .usage = "Usage: pjsip list contacts [ like <pattern> ]\n"
  1018. " List the configured PJSIP contacts\n"
  1019. " Optional regular expression pattern is used to filter the list.\n"),
  1020. AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Contacts",
  1021. .command = "pjsip show contacts",
  1022. .usage = "Usage: pjsip show contacts [ like <pattern> ]\n"
  1023. " Show the configured PJSIP contacts\n"
  1024. " Optional regular expression pattern is used to filter the list.\n"),
  1025. AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Contact",
  1026. .command = "pjsip show contact",
  1027. .usage = "Usage: pjsip show contact\n"
  1028. " Show the configured PJSIP contact\n"),
  1029. };
  1030. struct ast_sip_cli_formatter_entry *contact_formatter;
  1031. struct ast_sip_cli_formatter_entry *aor_formatter;
  1032. /*! \brief Always create a contact_status for each contact */
  1033. static int contact_apply_handler(const struct ast_sorcery *sorcery, void *object)
  1034. {
  1035. struct ast_sip_contact_status *status;
  1036. struct ast_sip_contact *contact = object;
  1037. if (ast_strlen_zero(contact->uri)) {
  1038. ast_log(LOG_ERROR, "A URI on dynamic contact '%s' is empty\n",
  1039. ast_sorcery_object_get_id(contact));
  1040. return -1;
  1041. }
  1042. status = ast_res_pjsip_find_or_create_contact_status(contact);
  1043. ao2_cleanup(status);
  1044. return status ? 0 : -1;
  1045. }
  1046. /*! \brief Initialize sorcery with location support */
  1047. int ast_sip_initialize_sorcery_location(void)
  1048. {
  1049. struct ast_sorcery *sorcery = ast_sip_get_sorcery();
  1050. int i;
  1051. ast_pjproject_get_buildopt("PJ_MAX_HOSTNAME", "%d", &pj_max_hostname);
  1052. /* As of pjproject 2.4.5, PJSIP_MAX_URL_SIZE isn't exposed yet but we try anyway. */
  1053. ast_pjproject_get_buildopt("PJSIP_MAX_URL_SIZE", "%d", &pjsip_max_url_size);
  1054. ast_sorcery_apply_default(sorcery, "contact", "astdb", "registrar");
  1055. ast_sorcery_object_set_congestion_levels(sorcery, "contact", -1,
  1056. 3 * AST_TASKPROCESSOR_HIGH_WATER_LEVEL);
  1057. ast_sorcery_apply_default(sorcery, "aor", "config", "pjsip.conf,criteria=type=aor");
  1058. if (ast_sorcery_object_register(sorcery, "contact", contact_alloc, NULL, contact_apply_handler) ||
  1059. ast_sorcery_object_register(sorcery, "aor", aor_alloc, NULL, NULL)) {
  1060. return -1;
  1061. }
  1062. ast_sorcery_observer_add(sorcery, "aor", &aor_observer);
  1063. ast_sorcery_object_field_register(sorcery, "contact", "type", "", OPT_NOOP_T, 0, 0);
  1064. ast_sorcery_object_field_register(sorcery, "contact", "uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, uri));
  1065. ast_sorcery_object_field_register(sorcery, "contact", "path", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, path));
  1066. ast_sorcery_object_field_register_custom(sorcery, "contact", "expiration_time", "", expiration_str2struct, expiration_struct2str, NULL, 0, 0);
  1067. ast_sorcery_object_field_register(sorcery, "contact", "qualify_frequency", 0, OPT_UINT_T,
  1068. PARSE_IN_RANGE, FLDSET(struct ast_sip_contact, qualify_frequency), 0, 86400);
  1069. ast_sorcery_object_field_register(sorcery, "contact", "qualify_timeout", "3.0", OPT_DOUBLE_T, 0, FLDSET(struct ast_sip_contact, qualify_timeout));
  1070. ast_sorcery_object_field_register(sorcery, "contact", "authenticate_qualify", "no", OPT_YESNO_T, 1, FLDSET(struct ast_sip_contact, authenticate_qualify));
  1071. ast_sorcery_object_field_register(sorcery, "contact", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, outbound_proxy));
  1072. ast_sorcery_object_field_register(sorcery, "contact", "user_agent", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, user_agent));
  1073. ast_sorcery_object_field_register(sorcery, "contact", "endpoint", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, endpoint_name));
  1074. ast_sorcery_object_field_register(sorcery, "contact", "reg_server", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, reg_server));
  1075. ast_sorcery_object_field_register(sorcery, "contact", "via_addr", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, via_addr));
  1076. ast_sorcery_object_field_register(sorcery, "contact", "via_port", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_contact, via_port));
  1077. ast_sorcery_object_field_register(sorcery, "contact", "call_id", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, call_id));
  1078. ast_sorcery_object_field_register(sorcery, "contact", "prune_on_boot", "no", OPT_YESNO_T, 1, FLDSET(struct ast_sip_contact, prune_on_boot));
  1079. ast_sorcery_object_field_register(sorcery, "aor", "type", "", OPT_NOOP_T, 0, 0);
  1080. ast_sorcery_object_field_register(sorcery, "aor", "minimum_expiration", "60", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, minimum_expiration));
  1081. ast_sorcery_object_field_register(sorcery, "aor", "maximum_expiration", "7200", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, maximum_expiration));
  1082. ast_sorcery_object_field_register(sorcery, "aor", "default_expiration", "3600", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, default_expiration));
  1083. ast_sorcery_object_field_register(sorcery, "aor", "qualify_frequency", 0, OPT_UINT_T, PARSE_IN_RANGE, FLDSET(struct ast_sip_aor, qualify_frequency), 0, 86400);
  1084. ast_sorcery_object_field_register(sorcery, "aor", "qualify_timeout", "3.0", OPT_DOUBLE_T, 0, FLDSET(struct ast_sip_aor, qualify_timeout));
  1085. ast_sorcery_object_field_register(sorcery, "aor", "authenticate_qualify", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, authenticate_qualify));
  1086. ast_sorcery_object_field_register(sorcery, "aor", "max_contacts", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, max_contacts));
  1087. ast_sorcery_object_field_register(sorcery, "aor", "remove_existing", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, remove_existing));
  1088. ast_sorcery_object_field_register_custom(sorcery, "aor", "contact", "", permanent_uri_handler, contacts_to_str, contacts_to_var_list, 0, 0);
  1089. ast_sorcery_object_field_register(sorcery, "aor", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_aor, mailboxes));
  1090. ast_sorcery_object_field_register_custom(sorcery, "aor", "voicemail_extension", "", voicemail_extension_handler, voicemail_extension_to_str, NULL, 0, 0);
  1091. ast_sorcery_object_field_register(sorcery, "aor", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_aor, outbound_proxy));
  1092. ast_sorcery_object_field_register(sorcery, "aor", "support_path", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, support_path));
  1093. internal_sip_register_endpoint_formatter(&endpoint_aor_formatter);
  1094. contact_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
  1095. if (!contact_formatter) {
  1096. ast_log(LOG_ERROR, "Unable to allocate memory for contact_formatter\n");
  1097. return -1;
  1098. }
  1099. contact_formatter->name = "contact";
  1100. contact_formatter->print_header = cli_contact_print_header;
  1101. contact_formatter->print_body = cli_contact_print_body;
  1102. contact_formatter->get_container = cli_contact_get_container;
  1103. contact_formatter->iterate = cli_contact_iterate;
  1104. contact_formatter->get_id = cli_contact_get_id;
  1105. contact_formatter->retrieve_by_id = cli_contact_retrieve_by_id;
  1106. aor_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
  1107. if (!aor_formatter) {
  1108. ast_log(LOG_ERROR, "Unable to allocate memory for aor_formatter\n");
  1109. return -1;
  1110. }
  1111. aor_formatter->name = "aor";
  1112. aor_formatter->print_header = cli_aor_print_header;
  1113. aor_formatter->print_body = cli_aor_print_body;
  1114. aor_formatter->get_container = cli_aor_get_container;
  1115. aor_formatter->iterate = cli_aor_iterate;
  1116. aor_formatter->get_id = cli_aor_get_id;
  1117. aor_formatter->retrieve_by_id = cli_aor_retrieve_by_id;
  1118. ast_sip_register_cli_formatter(contact_formatter);
  1119. ast_sip_register_cli_formatter(aor_formatter);
  1120. ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
  1121. /*
  1122. * Reset StatsD gauges in case we didn't shut down cleanly.
  1123. * Note that this must done here, as contacts will create the contact_status
  1124. * object before PJSIP options handling is initialized.
  1125. */
  1126. for (i = 0; i <= REMOVED; i++) {
  1127. ast_statsd_log_full_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE, 0, 1.0, ast_sip_get_contact_status_label(i));
  1128. }
  1129. return 0;
  1130. }
  1131. int ast_sip_destroy_sorcery_location(void)
  1132. {
  1133. ast_sorcery_observer_remove(ast_sip_get_sorcery(), "aor", &aor_observer);
  1134. ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
  1135. ast_sip_unregister_cli_formatter(contact_formatter);
  1136. ast_sip_unregister_cli_formatter(aor_formatter);
  1137. internal_sip_unregister_endpoint_formatter(&endpoint_aor_formatter);
  1138. return 0;
  1139. }