pjsip_configuration.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  1. /*
  2. * sip_cli_commands.c
  3. *
  4. * Created on: Jan 25, 2013
  5. * Author: mjordan
  6. */
  7. #include "asterisk.h"
  8. #include <pjsip.h>
  9. #include <pjsip_ua.h>
  10. #include "asterisk/res_pjsip.h"
  11. #include "include/res_pjsip_private.h"
  12. #include "asterisk/res_pjsip_cli.h"
  13. #include "asterisk/acl.h"
  14. #include "asterisk/manager.h"
  15. #include "asterisk/astobj2.h"
  16. #include "asterisk/utils.h"
  17. #include "asterisk/sorcery.h"
  18. #include "asterisk/callerid.h"
  19. /*! \brief Number of buckets for persistent endpoint information */
  20. #define PERSISTENT_BUCKETS 53
  21. /*! \brief Persistent endpoint information */
  22. struct sip_persistent_endpoint {
  23. /*! \brief Asterisk endpoint itself */
  24. struct ast_endpoint *endpoint;
  25. /*! \brief AORs that we should react to */
  26. char *aors;
  27. };
  28. /*! \brief Container for persistent endpoint information */
  29. static struct ao2_container *persistent_endpoints;
  30. static struct ast_sorcery *sip_sorcery;
  31. /*! \brief Hashing function for persistent endpoint information */
  32. static int persistent_endpoint_hash(const void *obj, const int flags)
  33. {
  34. const struct sip_persistent_endpoint *persistent = obj;
  35. const char *id = (flags & OBJ_KEY ? obj : ast_endpoint_get_resource(persistent->endpoint));
  36. return ast_str_hash(id);
  37. }
  38. /*! \brief Comparison function for persistent endpoint information */
  39. static int persistent_endpoint_cmp(void *obj, void *arg, int flags)
  40. {
  41. const struct sip_persistent_endpoint *persistent1 = obj;
  42. const struct sip_persistent_endpoint *persistent2 = arg;
  43. const char *id = (flags & OBJ_KEY ? arg : ast_endpoint_get_resource(persistent2->endpoint));
  44. return !strcmp(ast_endpoint_get_resource(persistent1->endpoint), id) ? CMP_MATCH | CMP_STOP : 0;
  45. }
  46. /*! \brief Callback function for changing the state of an endpoint */
  47. static int persistent_endpoint_update_state(void *obj, void *arg, int flags)
  48. {
  49. struct sip_persistent_endpoint *persistent = obj;
  50. char *aor = arg;
  51. RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
  52. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  53. if (!ast_strlen_zero(aor) && !strstr(persistent->aors, aor)) {
  54. return 0;
  55. }
  56. if ((contact = ast_sip_location_retrieve_contact_from_aor_list(persistent->aors))) {
  57. ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_ONLINE);
  58. blob = ast_json_pack("{s: s}", "peer_status", "Reachable");
  59. } else {
  60. ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_OFFLINE);
  61. blob = ast_json_pack("{s: s}", "peer_status", "Unreachable");
  62. }
  63. ast_endpoint_blob_publish(persistent->endpoint, ast_endpoint_state_type(), blob);
  64. ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_endpoint_get_resource(persistent->endpoint));
  65. return 0;
  66. }
  67. /*! \brief Function called when stuff relating to a contact happens (created/deleted) */
  68. static void persistent_endpoint_contact_observer(const void *object)
  69. {
  70. char *id = ast_strdupa(ast_sorcery_object_get_id(object)), *aor = NULL;
  71. aor = strsep(&id, ";@");
  72. ao2_callback(persistent_endpoints, OBJ_NODATA, persistent_endpoint_update_state, aor);
  73. }
  74. /*! \brief Observer for contacts so state can be updated on respective endpoints */
  75. static const struct ast_sorcery_observer state_contact_observer = {
  76. .created = persistent_endpoint_contact_observer,
  77. .deleted = persistent_endpoint_contact_observer,
  78. };
  79. static int dtmf_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  80. {
  81. struct ast_sip_endpoint *endpoint = obj;
  82. if (!strcasecmp(var->value, "rfc4733")) {
  83. endpoint->dtmf = AST_SIP_DTMF_RFC_4733;
  84. } else if (!strcasecmp(var->value, "inband")) {
  85. endpoint->dtmf = AST_SIP_DTMF_INBAND;
  86. } else if (!strcasecmp(var->value, "info")) {
  87. endpoint->dtmf = AST_SIP_DTMF_INFO;
  88. } else if (!strcasecmp(var->value, "none")) {
  89. endpoint->dtmf = AST_SIP_DTMF_NONE;
  90. } else {
  91. return -1;
  92. }
  93. return 0;
  94. }
  95. static int dtmf_to_str(const void *obj, const intptr_t *args, char **buf)
  96. {
  97. const struct ast_sip_endpoint *endpoint = obj;
  98. switch (endpoint->dtmf) {
  99. case AST_SIP_DTMF_RFC_4733 :
  100. *buf = "rfc4733"; break;
  101. case AST_SIP_DTMF_INBAND :
  102. *buf = "inband"; break;
  103. case AST_SIP_DTMF_INFO :
  104. *buf = "info"; break;
  105. default:
  106. *buf = "none";
  107. }
  108. *buf = ast_strdup(*buf);
  109. return 0;
  110. }
  111. static int prack_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  112. {
  113. struct ast_sip_endpoint *endpoint = obj;
  114. if (ast_true(var->value)) {
  115. endpoint->extensions.flags |= PJSIP_INV_SUPPORT_100REL;
  116. } else if (ast_false(var->value)) {
  117. endpoint->extensions.flags &= PJSIP_INV_SUPPORT_100REL;
  118. } else if (!strcasecmp(var->value, "required")) {
  119. endpoint->extensions.flags |= PJSIP_INV_REQUIRE_100REL;
  120. } else {
  121. return -1;
  122. }
  123. return 0;
  124. }
  125. static int prack_to_str(const void *obj, const intptr_t *args, char **buf)
  126. {
  127. const struct ast_sip_endpoint *endpoint = obj;
  128. if (endpoint->extensions.flags & PJSIP_INV_REQUIRE_100REL) {
  129. *buf = "required";
  130. } else if (endpoint->extensions.flags & PJSIP_INV_SUPPORT_100REL) {
  131. *buf = "yes";
  132. } else {
  133. *buf = "no";
  134. }
  135. *buf = ast_strdup(*buf);
  136. return 0;
  137. }
  138. static int timers_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  139. {
  140. struct ast_sip_endpoint *endpoint = obj;
  141. if (ast_true(var->value)) {
  142. endpoint->extensions.flags |= PJSIP_INV_SUPPORT_TIMER;
  143. } else if (ast_false(var->value)) {
  144. endpoint->extensions.flags &= PJSIP_INV_SUPPORT_TIMER;
  145. } else if (!strcasecmp(var->value, "required")) {
  146. endpoint->extensions.flags |= PJSIP_INV_REQUIRE_TIMER;
  147. } else if (!strcasecmp(var->value, "always")) {
  148. endpoint->extensions.flags |= PJSIP_INV_ALWAYS_USE_TIMER;
  149. } else {
  150. return -1;
  151. }
  152. return 0;
  153. }
  154. static int timers_to_str(const void *obj, const intptr_t *args, char **buf)
  155. {
  156. const struct ast_sip_endpoint *endpoint = obj;
  157. if (endpoint->extensions.flags & PJSIP_INV_ALWAYS_USE_TIMER) {
  158. *buf = "always";
  159. } else if (endpoint->extensions.flags & PJSIP_INV_REQUIRE_TIMER) {
  160. *buf = "required";
  161. } else if (endpoint->extensions.flags & PJSIP_INV_SUPPORT_TIMER) {
  162. *buf = "yes";
  163. } else {
  164. *buf = "no";
  165. }
  166. *buf = ast_strdup(*buf);
  167. return 0;
  168. }
  169. void ast_sip_auth_vector_destroy(struct ast_sip_auth_vector *auths)
  170. {
  171. int i;
  172. size_t size;
  173. if (!auths) {
  174. return;
  175. }
  176. size = AST_VECTOR_SIZE(auths);
  177. for (i = 0; i < size; ++i) {
  178. const char *name = AST_VECTOR_REMOVE_UNORDERED(auths, 0);
  179. ast_free((char *) name);
  180. }
  181. AST_VECTOR_FREE(auths);
  182. }
  183. int ast_sip_auth_vector_init(struct ast_sip_auth_vector *auths, const char *value)
  184. {
  185. char *auth_names = ast_strdupa(value);
  186. char *val;
  187. ast_assert(auths != NULL);
  188. if (AST_VECTOR_SIZE(auths)) {
  189. ast_sip_auth_vector_destroy(auths);
  190. }
  191. if (AST_VECTOR_INIT(auths, 1)) {
  192. return -1;
  193. }
  194. while ((val = strsep(&auth_names, ","))) {
  195. val = ast_strdup(val);
  196. if (!val) {
  197. goto failure;
  198. }
  199. if (AST_VECTOR_APPEND(auths, val)) {
  200. goto failure;
  201. }
  202. }
  203. return 0;
  204. failure:
  205. ast_sip_auth_vector_destroy(auths);
  206. return -1;
  207. }
  208. static int inbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  209. {
  210. struct ast_sip_endpoint *endpoint = obj;
  211. return ast_sip_auth_vector_init(&endpoint->inbound_auths, var->value);
  212. }
  213. static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  214. {
  215. struct ast_sip_endpoint *endpoint = obj;
  216. return ast_sip_auth_vector_init(&endpoint->outbound_auths, var->value);
  217. }
  218. int ast_sip_auths_to_str(const struct ast_sip_auth_vector *auths, char **buf)
  219. {
  220. if (!auths || !AST_VECTOR_SIZE(auths)) {
  221. return 0;
  222. }
  223. if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
  224. return -1;
  225. }
  226. /* I feel like accessing the vector's elem array directly is cheating...*/
  227. ast_join_delim(*buf, MAX_OBJECT_FIELD, auths->elems, AST_VECTOR_SIZE(auths), ',');
  228. return 0;
  229. }
  230. static int inbound_auths_to_str(const void *obj, const intptr_t *args, char **buf)
  231. {
  232. const struct ast_sip_endpoint *endpoint = obj;
  233. return ast_sip_auths_to_str(&endpoint->inbound_auths, buf);
  234. }
  235. static int outbound_auths_to_str(const void *obj, const intptr_t *args, char **buf)
  236. {
  237. const struct ast_sip_endpoint *endpoint = obj;
  238. return ast_sip_auths_to_str(&endpoint->outbound_auths, buf);
  239. }
  240. static int ident_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  241. {
  242. struct ast_sip_endpoint *endpoint = obj;
  243. char *idents = ast_strdupa(var->value);
  244. char *val;
  245. while ((val = strsep(&idents, ","))) {
  246. if (!strcasecmp(val, "username")) {
  247. endpoint->ident_method |= AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME;
  248. } else {
  249. ast_log(LOG_ERROR, "Unrecognized identification method %s specified for endpoint %s\n",
  250. val, ast_sorcery_object_get_id(endpoint));
  251. return -1;
  252. }
  253. }
  254. return 0;
  255. }
  256. static int ident_to_str(const void *obj, const intptr_t *args, char **buf)
  257. {
  258. const struct ast_sip_endpoint *endpoint = obj;
  259. switch (endpoint->ident_method) {
  260. case AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME :
  261. *buf = "username"; break;
  262. default:
  263. return 0;
  264. }
  265. *buf = ast_strdup(*buf);
  266. return 0;
  267. }
  268. static int redirect_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  269. {
  270. struct ast_sip_endpoint *endpoint = obj;
  271. if (!strcasecmp(var->value, "user")) {
  272. endpoint->redirect_method = AST_SIP_REDIRECT_USER;
  273. } else if (!strcasecmp(var->value, "uri_core")) {
  274. endpoint->redirect_method = AST_SIP_REDIRECT_URI_CORE;
  275. } else if (!strcasecmp(var->value, "uri_pjsip")) {
  276. endpoint->redirect_method = AST_SIP_REDIRECT_URI_PJSIP;
  277. } else {
  278. ast_log(LOG_ERROR, "Unrecognized redirect method %s specified for endpoint %s\n",
  279. var->value, ast_sorcery_object_get_id(endpoint));
  280. return -1;
  281. }
  282. return 0;
  283. }
  284. static int direct_media_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  285. {
  286. struct ast_sip_endpoint *endpoint = obj;
  287. if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
  288. endpoint->media.direct_media.method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
  289. } else if (!strcasecmp(var->value, "update")) {
  290. endpoint->media.direct_media.method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
  291. } else {
  292. ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
  293. var->value, var->name, ast_sorcery_object_get_id(endpoint));
  294. return -1;
  295. }
  296. return 0;
  297. }
  298. static const char *id_configuration_refresh_methods[] = {
  299. [AST_SIP_SESSION_REFRESH_METHOD_INVITE] = "invite",
  300. [AST_SIP_SESSION_REFRESH_METHOD_UPDATE] = "update"
  301. };
  302. static int direct_media_method_to_str(const void *obj, const intptr_t *args, char **buf)
  303. {
  304. const struct ast_sip_endpoint *endpoint = obj;
  305. if (ARRAY_IN_BOUNDS(endpoint->id.refresh_method, id_configuration_refresh_methods)) {
  306. *buf = ast_strdup(id_configuration_refresh_methods[endpoint->id.refresh_method]);
  307. }
  308. return 0;
  309. }
  310. static int connected_line_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  311. {
  312. struct ast_sip_endpoint *endpoint = obj;
  313. if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
  314. endpoint->id.refresh_method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
  315. } else if (!strcasecmp(var->value, "update")) {
  316. endpoint->id.refresh_method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
  317. } else {
  318. ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
  319. var->value, var->name, ast_sorcery_object_get_id(endpoint));
  320. return -1;
  321. }
  322. return 0;
  323. }
  324. static int connected_line_method_to_str(const void *obj, const intptr_t *args, char **buf)
  325. {
  326. const struct ast_sip_endpoint *endpoint = obj;
  327. *buf = ast_strdup(id_configuration_refresh_methods[endpoint->id.refresh_method]);
  328. return 0;
  329. }
  330. static int direct_media_glare_mitigation_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  331. {
  332. struct ast_sip_endpoint *endpoint = obj;
  333. if (!strcasecmp(var->value, "none")) {
  334. endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE;
  335. } else if (!strcasecmp(var->value, "outgoing")) {
  336. endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING;
  337. } else if (!strcasecmp(var->value, "incoming")) {
  338. endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING;
  339. } else {
  340. ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
  341. var->value, var->name, ast_sorcery_object_get_id(endpoint));
  342. return -1;
  343. }
  344. return 0;
  345. }
  346. static const char *direct_media_glare_mitigation_map[] = {
  347. [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE] = "none",
  348. [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING] = "outgoing",
  349. [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING] = "incoming"
  350. };
  351. static int direct_media_glare_mitigation_to_str(const void *obj, const intptr_t *args, char **buf)
  352. {
  353. const struct ast_sip_endpoint *endpoint = obj;
  354. if (ARRAY_IN_BOUNDS(endpoint->media.direct_media.glare_mitigation, direct_media_glare_mitigation_map)) {
  355. *buf = ast_strdup(direct_media_glare_mitigation_map[endpoint->media.direct_media.glare_mitigation]);
  356. }
  357. return 0;
  358. }
  359. static int caller_id_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  360. {
  361. struct ast_sip_endpoint *endpoint = obj;
  362. char cid_name[80] = { '\0' };
  363. char cid_num[80] = { '\0' };
  364. ast_callerid_split(var->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
  365. if (!ast_strlen_zero(cid_name)) {
  366. endpoint->id.self.name.str = ast_strdup(cid_name);
  367. if (!endpoint->id.self.name.str) {
  368. return -1;
  369. }
  370. endpoint->id.self.name.valid = 1;
  371. }
  372. if (!ast_strlen_zero(cid_num)) {
  373. endpoint->id.self.number.str = ast_strdup(cid_num);
  374. if (!endpoint->id.self.number.str) {
  375. return -1;
  376. }
  377. endpoint->id.self.number.valid = 1;
  378. }
  379. return 0;
  380. }
  381. static int caller_id_to_str(const void *obj, const intptr_t *args, char **buf)
  382. {
  383. const struct ast_sip_endpoint *endpoint = obj;
  384. const char *name = S_COR(endpoint->id.self.name.valid,
  385. endpoint->id.self.name.str, NULL);
  386. const char *number = S_COR(endpoint->id.self.number.valid,
  387. endpoint->id.self.number.str, NULL);
  388. /* make sure size is at least 10 - that should cover the "<unknown>"
  389. case as well as any additional formatting characters added in
  390. the name and/or number case. */
  391. int size = 10;
  392. size += name ? strlen(name) : 0;
  393. size += number ? strlen(number) : 0;
  394. if (!(*buf = ast_calloc(size + 1, sizeof(char)))) {
  395. return -1;
  396. }
  397. ast_callerid_merge(*buf, size + 1, name, number, NULL);
  398. return 0;
  399. }
  400. static int caller_id_privacy_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  401. {
  402. struct ast_sip_endpoint *endpoint = obj;
  403. int callingpres = ast_parse_caller_presentation(var->value);
  404. if (callingpres == -1 && sscanf(var->value, "%d", &callingpres) != 1) {
  405. return -1;
  406. }
  407. endpoint->id.self.number.presentation = callingpres;
  408. endpoint->id.self.name.presentation = callingpres;
  409. return 0;
  410. }
  411. static int caller_id_privacy_to_str(const void *obj, const intptr_t *args, char **buf)
  412. {
  413. const struct ast_sip_endpoint *endpoint = obj;
  414. const char *presentation = ast_named_caller_presentation(
  415. endpoint->id.self.name.presentation);
  416. *buf = ast_strdup(presentation);
  417. return 0;
  418. }
  419. static int caller_id_tag_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  420. {
  421. struct ast_sip_endpoint *endpoint = obj;
  422. endpoint->id.self.tag = ast_strdup(var->value);
  423. return endpoint->id.self.tag ? 0 : -1;
  424. }
  425. static int caller_id_tag_to_str(const void *obj, const intptr_t *args, char **buf)
  426. {
  427. const struct ast_sip_endpoint *endpoint = obj;
  428. *buf = ast_strdup(endpoint->id.self.tag);
  429. return 0;
  430. }
  431. static int media_encryption_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  432. {
  433. struct ast_sip_endpoint *endpoint = obj;
  434. if (!strcasecmp("no", var->value)) {
  435. endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
  436. } else if (!strcasecmp("sdes", var->value)) {
  437. endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_SDES;
  438. } else if (!strcasecmp("dtls", var->value)) {
  439. endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_DTLS;
  440. ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, "dtlsenable", "yes");
  441. } else {
  442. return -1;
  443. }
  444. return 0;
  445. }
  446. static const char *media_encryption_map[] = {
  447. [AST_SIP_MEDIA_TRANSPORT_INVALID] = "invalid",
  448. [AST_SIP_MEDIA_ENCRYPT_NONE] = "none",
  449. [AST_SIP_MEDIA_ENCRYPT_SDES] = "sdes",
  450. [AST_SIP_MEDIA_ENCRYPT_DTLS] = "dtls",
  451. };
  452. static int media_encryption_to_str(const void *obj, const intptr_t *args, char **buf)
  453. {
  454. const struct ast_sip_endpoint *endpoint = obj;
  455. if (ARRAY_IN_BOUNDS(endpoint->media.rtp.encryption, media_encryption_map)) {
  456. *buf = ast_strdup(media_encryption_map[
  457. endpoint->media.rtp.encryption]);
  458. }
  459. return 0;
  460. }
  461. static int group_handler(const struct aco_option *opt,
  462. struct ast_variable *var, void *obj)
  463. {
  464. struct ast_sip_endpoint *endpoint = obj;
  465. if (!strncmp(var->name, "call_group", 10)) {
  466. endpoint->pickup.callgroup = ast_get_group(var->value);
  467. } else if (!strncmp(var->name, "pickup_group", 12)) {
  468. endpoint->pickup.pickupgroup = ast_get_group(var->value);
  469. } else {
  470. return -1;
  471. }
  472. return 0;
  473. }
  474. static int callgroup_to_str(const void *obj, const intptr_t *args, char **buf)
  475. {
  476. const struct ast_sip_endpoint *endpoint = obj;
  477. if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
  478. return -1;
  479. }
  480. ast_print_group(*buf, MAX_OBJECT_FIELD, endpoint->pickup.callgroup);
  481. return 0;
  482. }
  483. static int pickupgroup_to_str(const void *obj, const intptr_t *args, char **buf)
  484. {
  485. const struct ast_sip_endpoint *endpoint = obj;
  486. if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
  487. return -1;
  488. }
  489. ast_print_group(*buf, MAX_OBJECT_FIELD, endpoint->pickup.pickupgroup);
  490. return 0;
  491. }
  492. static int named_groups_handler(const struct aco_option *opt,
  493. struct ast_variable *var, void *obj)
  494. {
  495. struct ast_sip_endpoint *endpoint = obj;
  496. if (!strncmp(var->name, "named_call_group", 16)) {
  497. if (ast_strlen_zero(var->value)) {
  498. endpoint->pickup.named_callgroups =
  499. ast_unref_namedgroups(endpoint->pickup.named_callgroups);
  500. } else if (!(endpoint->pickup.named_callgroups =
  501. ast_get_namedgroups(var->value))) {
  502. return -1;
  503. }
  504. } else if (!strncmp(var->name, "named_pickup_group", 18)) {
  505. if (ast_strlen_zero(var->value)) {
  506. endpoint->pickup.named_pickupgroups =
  507. ast_unref_namedgroups(endpoint->pickup.named_pickupgroups);
  508. } else if (!(endpoint->pickup.named_pickupgroups =
  509. ast_get_namedgroups(var->value))) {
  510. return -1;
  511. }
  512. } else {
  513. return -1;
  514. }
  515. return 0;
  516. }
  517. static int named_callgroups_to_str(const void *obj, const intptr_t *args, char **buf)
  518. {
  519. const struct ast_sip_endpoint *endpoint = obj;
  520. RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
  521. ast_print_namedgroups(&str, endpoint->pickup.named_callgroups);
  522. *buf = ast_strdup(ast_str_buffer(str));
  523. return 0;
  524. }
  525. static int named_pickupgroups_to_str(const void *obj, const intptr_t *args, char **buf)
  526. {
  527. const struct ast_sip_endpoint *endpoint = obj;
  528. RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
  529. ast_print_namedgroups(&str, endpoint->pickup.named_pickupgroups);
  530. *buf = ast_strdup(ast_str_buffer(str));
  531. return 0;
  532. }
  533. static int dtls_handler(const struct aco_option *opt,
  534. struct ast_variable *var, void *obj)
  535. {
  536. struct ast_sip_endpoint *endpoint = obj;
  537. char *name = ast_strdupa(var->name);
  538. char *front, *buf = name;
  539. /* strip out underscores in the name */
  540. front = strtok(buf, "_");
  541. while (front) {
  542. int size = strlen(front);
  543. ast_copy_string(buf, front, size + 1);
  544. buf += size;
  545. front = strtok(NULL, "_");
  546. }
  547. return ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, name, var->value);
  548. }
  549. static int dtlsverify_to_str(const void *obj, const intptr_t *args, char **buf)
  550. {
  551. const struct ast_sip_endpoint *endpoint = obj;
  552. *buf = ast_strdup(AST_YESNO(endpoint->media.rtp.dtls_cfg.verify));
  553. return 0;
  554. }
  555. static int dtlsrekey_to_str(const void *obj, const intptr_t *args, char **buf)
  556. {
  557. const struct ast_sip_endpoint *endpoint = obj;
  558. return ast_asprintf(
  559. buf, "%u", endpoint->media.rtp.dtls_cfg.rekey) >=0 ? 0 : -1;
  560. }
  561. static int dtlscertfile_to_str(const void *obj, const intptr_t *args, char **buf)
  562. {
  563. const struct ast_sip_endpoint *endpoint = obj;
  564. *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.certfile);
  565. return 0;
  566. }
  567. static int dtlsprivatekey_to_str(const void *obj, const intptr_t *args, char **buf)
  568. {
  569. const struct ast_sip_endpoint *endpoint = obj;
  570. *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.pvtfile);
  571. return 0;
  572. }
  573. static int dtlscipher_to_str(const void *obj, const intptr_t *args, char **buf)
  574. {
  575. const struct ast_sip_endpoint *endpoint = obj;
  576. *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.cipher);
  577. return 0;
  578. }
  579. static int dtlscafile_to_str(const void *obj, const intptr_t *args, char **buf)
  580. {
  581. const struct ast_sip_endpoint *endpoint = obj;
  582. *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.cafile);
  583. return 0;
  584. }
  585. static int dtlscapath_to_str(const void *obj, const intptr_t *args, char **buf)
  586. {
  587. const struct ast_sip_endpoint *endpoint = obj;
  588. *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.capath);
  589. return 0;
  590. }
  591. static const char *ast_rtp_dtls_setup_map[] = {
  592. [AST_RTP_DTLS_SETUP_ACTIVE] = "active",
  593. [AST_RTP_DTLS_SETUP_PASSIVE] = "passive",
  594. [AST_RTP_DTLS_SETUP_ACTPASS] = "actpass",
  595. [AST_RTP_DTLS_SETUP_HOLDCONN] = "holdconn",
  596. };
  597. static int dtlssetup_to_str(const void *obj, const intptr_t *args, char **buf)
  598. {
  599. const struct ast_sip_endpoint *endpoint = obj;
  600. if (ARRAY_IN_BOUNDS(endpoint->media.rtp.dtls_cfg.default_setup, ast_rtp_dtls_setup_map)) {
  601. *buf = ast_strdup(ast_rtp_dtls_setup_map[endpoint->media.rtp.dtls_cfg.default_setup]);
  602. }
  603. return 0;
  604. }
  605. static const char *ast_rtp_dtls_fingerprint_map[] = {
  606. [AST_RTP_DTLS_HASH_SHA256] = "SHA-256",
  607. [AST_RTP_DTLS_HASH_SHA1] = "SHA-1",
  608. };
  609. static int dtlsfingerprint_to_str(const void *obj, const intptr_t *args, char **buf)
  610. {
  611. const struct ast_sip_endpoint *endpoint = obj;
  612. if (ARRAY_IN_BOUNDS(endpoint->media.rtp.dtls_cfg.hash, ast_rtp_dtls_fingerprint_map)) {
  613. *buf = ast_strdup(ast_rtp_dtls_fingerprint_map[endpoint->media.rtp.dtls_cfg.hash]);
  614. }
  615. return 0;
  616. }
  617. static int t38udptl_ec_handler(const struct aco_option *opt,
  618. struct ast_variable *var, void *obj)
  619. {
  620. struct ast_sip_endpoint *endpoint = obj;
  621. if (!strcmp(var->value, "none")) {
  622. endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_NONE;
  623. } else if (!strcmp(var->value, "fec")) {
  624. endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_FEC;
  625. } else if (!strcmp(var->value, "redundancy")) {
  626. endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_REDUNDANCY;
  627. } else {
  628. return -1;
  629. }
  630. return 0;
  631. }
  632. static const char *ast_t38_ec_modes_map[] = {
  633. [UDPTL_ERROR_CORRECTION_NONE] = "none",
  634. [UDPTL_ERROR_CORRECTION_FEC] = "fec",
  635. [UDPTL_ERROR_CORRECTION_REDUNDANCY] = "redundancy"
  636. };
  637. static int t38udptl_ec_to_str(const void *obj, const intptr_t *args, char **buf)
  638. {
  639. const struct ast_sip_endpoint *endpoint = obj;
  640. if (ARRAY_IN_BOUNDS(endpoint->media.t38.error_correction, ast_t38_ec_modes_map)) {
  641. *buf = ast_strdup(ast_t38_ec_modes_map[
  642. endpoint->media.t38.error_correction]);
  643. }
  644. return 0;
  645. }
  646. static int tos_handler(const struct aco_option *opt,
  647. struct ast_variable *var, void *obj)
  648. {
  649. struct ast_sip_endpoint *endpoint = obj;
  650. unsigned int value;
  651. if (ast_str2tos(var->value, &value)) {
  652. ast_log(LOG_ERROR, "Error configuring endpoint '%s' - Could not "
  653. "interpret '%s' value '%s'\n",
  654. ast_sorcery_object_get_id(endpoint), var->name, var->value);
  655. return -1;
  656. }
  657. if (!strcmp(var->name, "tos_audio")) {
  658. endpoint->media.tos_audio = value;
  659. } else if (!strcmp(var->name, "tos_video")) {
  660. endpoint->media.tos_video = value;
  661. } else {
  662. /* If we reach this point, someone called the tos_handler when they shouldn't have. */
  663. ast_assert(0);
  664. return -1;
  665. }
  666. return 0;
  667. }
  668. static int tos_audio_to_str(const void *obj, const intptr_t *args, char **buf)
  669. {
  670. const struct ast_sip_endpoint *endpoint = obj;
  671. if (ast_asprintf(buf, "%u", endpoint->media.tos_audio) == -1) {
  672. return -1;
  673. }
  674. return 0;
  675. }
  676. static int tos_video_to_str(const void *obj, const intptr_t *args, char **buf)
  677. {
  678. const struct ast_sip_endpoint *endpoint = obj;
  679. if (ast_asprintf(buf, "%u", endpoint->media.tos_video) == -1) {
  680. return -1;
  681. }
  682. return 0;
  683. }
  684. static int set_var_handler(const struct aco_option *opt,
  685. struct ast_variable *var, void *obj)
  686. {
  687. struct ast_sip_endpoint *endpoint = obj;
  688. struct ast_variable *new_var;
  689. char *name;
  690. char *val;
  691. if (ast_strlen_zero(var->value)) {
  692. return 0;
  693. }
  694. name = ast_strdupa(var->value);
  695. val = strchr(name, '=');
  696. if (!val) {
  697. return -1;
  698. }
  699. *val++ = '\0';
  700. if (!(new_var = ast_variable_new(name, val, ""))) {
  701. return -1;
  702. }
  703. ast_variable_list_append(&endpoint->channel_vars, new_var);
  704. return 0;
  705. }
  706. static int set_var_to_str(const void *obj, const intptr_t *args, char **buf)
  707. {
  708. struct ast_str *str = ast_str_create(MAX_OBJECT_FIELD);
  709. const struct ast_sip_endpoint *endpoint = obj;
  710. struct ast_variable *var;
  711. for (var = endpoint->channel_vars; var; var = var->next) {
  712. ast_str_append(&str, 0, "%s=%s,", var->name, var->value);
  713. }
  714. *buf = ast_strdup(ast_str_truncate(str, -1));
  715. ast_free(str);
  716. return 0;
  717. }
  718. static int set_var_to_vl(const void *obj, struct ast_variable **fields)
  719. {
  720. const struct ast_sip_endpoint *endpoint = obj;
  721. if (endpoint->channel_vars) {
  722. *fields = ast_variables_dup(endpoint->channel_vars);
  723. }
  724. return 0;
  725. }
  726. static void *sip_nat_hook_alloc(const char *name)
  727. {
  728. return ast_sorcery_generic_alloc(sizeof(struct ast_sip_nat_hook), NULL);
  729. }
  730. /*! \brief Destructor function for persistent endpoint information */
  731. static void persistent_endpoint_destroy(void *obj)
  732. {
  733. struct sip_persistent_endpoint *persistent = obj;
  734. ast_endpoint_shutdown(persistent->endpoint);
  735. ast_free(persistent->aors);
  736. }
  737. /*! \brief Internal function which finds (or creates) persistent endpoint information */
  738. static struct ast_endpoint *persistent_endpoint_find_or_create(const struct ast_sip_endpoint *endpoint)
  739. {
  740. RAII_VAR(struct sip_persistent_endpoint *, persistent, NULL, ao2_cleanup);
  741. SCOPED_AO2LOCK(lock, persistent_endpoints);
  742. if (!(persistent = ao2_find(persistent_endpoints, ast_sorcery_object_get_id(endpoint), OBJ_KEY | OBJ_NOLOCK))) {
  743. if (!(persistent = ao2_alloc(sizeof(*persistent), persistent_endpoint_destroy))) {
  744. return NULL;
  745. }
  746. if (!(persistent->endpoint = ast_endpoint_create("PJSIP", ast_sorcery_object_get_id(endpoint)))) {
  747. return NULL;
  748. }
  749. persistent->aors = ast_strdup(endpoint->aors);
  750. if (ast_strlen_zero(persistent->aors)) {
  751. ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_UNKNOWN);
  752. } else {
  753. persistent_endpoint_update_state(persistent, NULL, 0);
  754. }
  755. ao2_link_flags(persistent_endpoints, persistent, OBJ_NOLOCK);
  756. }
  757. ao2_ref(persistent->endpoint, +1);
  758. return persistent->endpoint;
  759. }
  760. /*! \brief Helper function which validates an outbound proxy */
  761. static int outbound_proxy_validate(void *data)
  762. {
  763. const char *proxy = data;
  764. pj_pool_t *pool;
  765. pj_str_t tmp;
  766. static const pj_str_t ROUTE_HNAME = { "Route", 5 };
  767. pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Outbound Proxy Validation", 256, 256);
  768. if (!pool) {
  769. return -1;
  770. }
  771. pj_strdup2_with_null(pool, &tmp, proxy);
  772. if (!pjsip_parse_hdr(pool, &ROUTE_HNAME, tmp.ptr, tmp.slen, NULL)) {
  773. pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
  774. return -1;
  775. }
  776. pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
  777. return 0;
  778. }
  779. /*! \brief Callback function for when an object is finalized */
  780. static int sip_endpoint_apply_handler(const struct ast_sorcery *sorcery, void *obj)
  781. {
  782. struct ast_sip_endpoint *endpoint = obj;
  783. if (!(endpoint->persistent = persistent_endpoint_find_or_create(endpoint))) {
  784. return -1;
  785. }
  786. if (!ast_strlen_zero(endpoint->outbound_proxy) &&
  787. ast_sip_push_task_synchronous(NULL, outbound_proxy_validate, (char*)endpoint->outbound_proxy)) {
  788. ast_log(LOG_ERROR, "Invalid outbound proxy '%s' specified on endpoint '%s'\n",
  789. endpoint->outbound_proxy, ast_sorcery_object_get_id(endpoint));
  790. return -1;
  791. }
  792. return 0;
  793. }
  794. const char *ast_sip_get_device_state(const struct ast_sip_endpoint *endpoint)
  795. {
  796. char device[MAX_OBJECT_FIELD];
  797. snprintf(device, MAX_OBJECT_FIELD, "PJSIP/%s", ast_sorcery_object_get_id(endpoint));
  798. return ast_devstate2str(ast_device_state(device));
  799. }
  800. struct ast_endpoint_snapshot *ast_sip_get_endpoint_snapshot(
  801. const struct ast_sip_endpoint *endpoint)
  802. {
  803. return ast_endpoint_latest_snapshot(
  804. ast_endpoint_get_tech(endpoint->persistent),
  805. ast_endpoint_get_resource(endpoint->persistent));
  806. }
  807. int ast_sip_for_each_channel_snapshot(
  808. const struct ast_endpoint_snapshot *endpoint_snapshot,
  809. ao2_callback_fn on_channel_snapshot, void *arg)
  810. {
  811. int num, num_channels = endpoint_snapshot->num_channels;
  812. if (!on_channel_snapshot || !num_channels) {
  813. return 0;
  814. }
  815. for (num = 0; num < num_channels; ++num) {
  816. RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
  817. int res;
  818. snapshot = ast_channel_snapshot_get_latest(endpoint_snapshot->channel_ids[num]);
  819. if (!snapshot) {
  820. continue;
  821. }
  822. res = on_channel_snapshot(snapshot, arg, 0);
  823. if (res) {
  824. return -1;
  825. }
  826. }
  827. return 0;
  828. }
  829. int ast_sip_for_each_channel(
  830. const struct ast_sip_endpoint *endpoint,
  831. ao2_callback_fn on_channel_snapshot, void *arg)
  832. {
  833. RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
  834. return ast_sip_for_each_channel_snapshot(endpoint_snapshot, on_channel_snapshot, arg);
  835. }
  836. static int active_channels_to_str_cb(void *object, void *arg, int flags)
  837. {
  838. const struct ast_channel_snapshot *snapshot = object;
  839. struct ast_str **buf = arg;
  840. ast_str_append(buf, 0, "%s,", snapshot->name);
  841. return 0;
  842. }
  843. static void active_channels_to_str(const struct ast_sip_endpoint *endpoint,
  844. struct ast_str **str)
  845. {
  846. RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot,
  847. ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
  848. if (endpoint_snapshot) {
  849. return;
  850. }
  851. ast_sip_for_each_channel_snapshot(endpoint_snapshot,
  852. active_channels_to_str_cb, str);
  853. ast_str_truncate(*str, -1);
  854. }
  855. #define AMI_DEFAULT_STR_SIZE 512
  856. struct ast_str *ast_sip_create_ami_event(const char *event, struct ast_sip_ami *ami)
  857. {
  858. struct ast_str *buf = ast_str_create(AMI_DEFAULT_STR_SIZE);
  859. if (!(buf)) {
  860. astman_send_error_va(ami->s, ami->m, "Unable create event "
  861. "for %s\n", event);
  862. return NULL;
  863. }
  864. ast_str_set(&buf, 0, "Event: %s\r\n", event);
  865. if (!ast_strlen_zero(ami->action_id)) {
  866. ast_str_append(&buf, 0, "ActionID: %s\r\n", ami->action_id);
  867. }
  868. return buf;
  869. }
  870. static void sip_sorcery_object_ami_set_type_name(const void *obj, struct ast_str **buf)
  871. {
  872. ast_str_append(buf, 0, "ObjectType: %s\r\n",
  873. ast_sorcery_object_get_type(obj));
  874. ast_str_append(buf, 0, "ObjectName: %s\r\n",
  875. ast_sorcery_object_get_id(obj));
  876. }
  877. int ast_sip_sorcery_object_to_ami(const void *obj, struct ast_str **buf)
  878. {
  879. RAII_VAR(struct ast_variable *, objset, ast_sorcery_objectset_create2(
  880. ast_sip_get_sorcery(), obj, AST_HANDLER_ONLY_STRING), ast_variables_destroy);
  881. struct ast_variable *i;
  882. if (!objset) {
  883. return -1;
  884. }
  885. sip_sorcery_object_ami_set_type_name(obj, buf);
  886. for (i = objset; i; i = i->next) {
  887. RAII_VAR(char *, camel, ast_to_camel_case(i->name), ast_free);
  888. ast_str_append(buf, 0, "%s: %s\r\n", camel, i->value);
  889. }
  890. return 0;
  891. }
  892. static int sip_endpoints_aors_ami(void *obj, void *arg, int flags)
  893. {
  894. struct ast_sip_aor *aor = obj;
  895. struct ast_str **buf = arg;
  896. ast_str_append(buf, 0, "Contacts: ");
  897. ast_sip_for_each_contact(aor, ast_sip_contact_to_str, arg);
  898. ast_str_append(buf, 0, "\r\n");
  899. return 0;
  900. }
  901. static int sip_endpoint_to_ami(const struct ast_sip_endpoint *endpoint,
  902. struct ast_str **buf)
  903. {
  904. if (ast_sip_sorcery_object_to_ami(endpoint, buf)) {
  905. return -1;
  906. }
  907. ast_str_append(buf, 0, "DeviceState: %s\r\n",
  908. ast_sip_get_device_state(endpoint));
  909. ast_str_append(buf, 0, "ActiveChannels: ");
  910. active_channels_to_str(endpoint, buf);
  911. ast_str_append(buf, 0, "\r\n");
  912. return 0;
  913. }
  914. static int format_ami_endpoint(const struct ast_sip_endpoint *endpoint,
  915. struct ast_sip_ami *ami)
  916. {
  917. RAII_VAR(struct ast_str *, buf,
  918. ast_sip_create_ami_event("EndpointDetail", ami), ast_free);
  919. if (!buf) {
  920. return -1;
  921. }
  922. sip_endpoint_to_ami(endpoint, &buf);
  923. astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
  924. return 0;
  925. }
  926. #define AMI_SHOW_ENDPOINTS "PJSIPShowEndpoints"
  927. #define AMI_SHOW_ENDPOINT "PJSIPShowEndpoint"
  928. static int ami_show_endpoint(struct mansession *s, const struct message *m)
  929. {
  930. struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"),
  931. .count = 0, };
  932. RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
  933. const char *endpoint_name = astman_get_header(m, "Endpoint");
  934. int count = 0;
  935. if (ast_strlen_zero(endpoint_name)) {
  936. astman_send_error_va(s, m, "%s requires an endpoint name\n",
  937. AMI_SHOW_ENDPOINT);
  938. return 0;
  939. }
  940. if (!strncasecmp(endpoint_name, "pjsip/", 6)) {
  941. endpoint_name += 6;
  942. }
  943. if (!(endpoint = ast_sorcery_retrieve_by_id(
  944. ast_sip_get_sorcery(), "endpoint", endpoint_name))) {
  945. astman_send_error_va(s, m, "Unable to retrieve endpoint %s\n",
  946. endpoint_name);
  947. return -1;
  948. }
  949. astman_send_listack(s, m, "Following are Events for each object "
  950. "associated with the the Endpoint", "start");
  951. /* the endpoint detail needs to always come first so apply as such */
  952. if (format_ami_endpoint(endpoint, &ami) ||
  953. ast_sip_format_endpoint_ami(endpoint, &ami, &count)) {
  954. astman_send_error_va(s, m, "Unable to format endpoint %s\n",
  955. endpoint_name);
  956. }
  957. astman_append(s, "Event: EndpointDetailComplete\r\n");
  958. if (!ast_strlen_zero(ami.action_id)) {
  959. astman_append(s, "ActionID: %s\r\n", ami.action_id);
  960. }
  961. astman_append(s, "EventList: Complete\r\n"
  962. "ListItems: %d\r\n\r\n", ami.count + 1);
  963. return 0;
  964. }
  965. static int format_str_append_auth(const struct ast_sip_auth_vector *auths,
  966. struct ast_str **buf)
  967. {
  968. char *str = NULL;
  969. if (ast_sip_auths_to_str(auths, &str)) {
  970. return -1;
  971. }
  972. ast_str_append(buf, 0, "%s", str ? str : "");
  973. ast_free(str);
  974. return 0;
  975. }
  976. static int format_ami_endpoints(void *obj, void *arg, int flags)
  977. {
  978. struct ast_sip_endpoint *endpoint = obj;
  979. struct ast_sip_ami *ami = arg;
  980. RAII_VAR(struct ast_str *, buf,
  981. ast_sip_create_ami_event("EndpointList", ami), ast_free);
  982. if (!buf) {
  983. return -1;
  984. }
  985. sip_sorcery_object_ami_set_type_name(endpoint, &buf);
  986. ast_str_append(&buf, 0, "Transport: %s\r\n",
  987. endpoint->transport);
  988. ast_str_append(&buf, 0, "Aor: %s\r\n",
  989. endpoint->aors);
  990. ast_str_append(&buf, 0, "Auths: ");
  991. format_str_append_auth(&endpoint->inbound_auths, &buf);
  992. ast_str_append(&buf, 0, "\r\n");
  993. ast_str_append(&buf, 0, "OutboundAuths: ");
  994. format_str_append_auth(&endpoint->outbound_auths, &buf);
  995. ast_str_append(&buf, 0, "\r\n");
  996. ast_sip_for_each_aor(endpoint->aors,
  997. sip_endpoints_aors_ami, &buf);
  998. ast_str_append(&buf, 0, "DeviceState: %s\r\n",
  999. ast_sip_get_device_state(endpoint));
  1000. ast_str_append(&buf, 0, "ActiveChannels: ");
  1001. active_channels_to_str(endpoint, &buf);
  1002. ast_str_append(&buf, 0, "\r\n");
  1003. astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
  1004. return 0;
  1005. }
  1006. static int ami_show_endpoints(struct mansession *s, const struct message *m)
  1007. {
  1008. struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
  1009. RAII_VAR(struct ao2_container *, endpoints, NULL, ao2_cleanup);
  1010. int num;
  1011. endpoints = ast_sip_get_endpoints();
  1012. if (!endpoints) {
  1013. return -1;
  1014. }
  1015. if (!(num = ao2_container_count(endpoints))) {
  1016. astman_send_error(s, m, "No endpoints found\n");
  1017. return 0;
  1018. }
  1019. astman_send_listack(s, m, "A listing of Endpoints follows, "
  1020. "presented as EndpointList events", "start");
  1021. ao2_callback(endpoints, OBJ_NODATA, format_ami_endpoints, &ami);
  1022. astman_append(s, "Event: EndpointListComplete\r\n");
  1023. if (!ast_strlen_zero(ami.action_id)) {
  1024. astman_append(s, "ActionID: %s\r\n", ami.action_id);
  1025. }
  1026. astman_append(s, "EventList: Complete\r\n"
  1027. "ListItems: %d\r\n\r\n", num);
  1028. return 0;
  1029. }
  1030. static struct ao2_container *cli_endpoint_get_container(void)
  1031. {
  1032. RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
  1033. struct ao2_container *s_container;
  1034. container = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint",
  1035. AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
  1036. if (!container) {
  1037. return NULL;
  1038. }
  1039. s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
  1040. (void *)ast_sorcery_object_id_sort, (void *)ast_sorcery_object_id_compare);
  1041. if (!s_container) {
  1042. return NULL;
  1043. }
  1044. if (ao2_container_dup(s_container, container, 0)) {
  1045. ao2_ref(s_container, -1);
  1046. return NULL;
  1047. }
  1048. return s_container;
  1049. }
  1050. static int cli_channel_populate_container(void *obj, void *arg, int flags)
  1051. {
  1052. struct ast_channel_snapshot *snapshot = obj;
  1053. ao2_link(arg, snapshot);
  1054. return 0;
  1055. }
  1056. static int cli_channel_iterate(void *container, ao2_callback_fn callback, void *args)
  1057. {
  1058. const struct ast_sip_endpoint *endpoint = container;
  1059. ast_sip_for_each_channel(endpoint, callback, args);
  1060. return 0;
  1061. }
  1062. static int cli_channel_sort(const void *obj, const void *arg, int flags)
  1063. {
  1064. const struct ast_channel_snapshot *left_obj = obj;
  1065. const struct ast_channel_snapshot *right_obj = arg;
  1066. const char *right_key = arg;
  1067. int cmp;
  1068. switch (flags & OBJ_SEARCH_MASK) {
  1069. case OBJ_SEARCH_OBJECT:
  1070. right_key = right_obj->name;
  1071. /* Fall through */
  1072. case OBJ_SEARCH_KEY:
  1073. cmp = strcmp(left_obj->name, right_key);
  1074. break;
  1075. case OBJ_SEARCH_PARTIAL_KEY:
  1076. cmp = strncmp(left_obj->name, right_key, strlen(right_key));
  1077. break;
  1078. default:
  1079. cmp = 0;
  1080. break;
  1081. }
  1082. return cmp;
  1083. }
  1084. static int cli_channel_compare(void *obj, void *arg, int flags)
  1085. {
  1086. const struct ast_channel_snapshot *left_obj = obj;
  1087. const struct ast_channel_snapshot *right_obj = arg;
  1088. const char *right_key = arg;
  1089. int cmp = 0;
  1090. switch (flags & OBJ_SEARCH_MASK) {
  1091. case OBJ_SEARCH_OBJECT:
  1092. right_key = right_obj->name;
  1093. /* Fall through */
  1094. case OBJ_SEARCH_KEY:
  1095. if (strcmp(left_obj->name, right_key) == 0) {
  1096. cmp = CMP_MATCH | CMP_STOP;
  1097. }
  1098. break;
  1099. case OBJ_SEARCH_PARTIAL_KEY:
  1100. if (strncmp(left_obj->name, right_key, strlen(right_key)) == 0) {
  1101. cmp = CMP_MATCH;
  1102. }
  1103. break;
  1104. default:
  1105. cmp = 0;
  1106. break;
  1107. }
  1108. return cmp;
  1109. }
  1110. static int cli_channel_hash(const void *obj, int flags)
  1111. {
  1112. const struct ast_channel_snapshot *snapshot = obj;
  1113. if (flags & OBJ_SEARCH_OBJECT) {
  1114. return ast_str_hash(snapshot->name);
  1115. } else if (flags & OBJ_SEARCH_KEY) {
  1116. return ast_str_hash(obj);
  1117. }
  1118. return -1;
  1119. }
  1120. static int cli_endpoint_gather_channels(void *obj, void *arg, int flags)
  1121. {
  1122. struct ast_sip_endpoint *endpoint = obj;
  1123. struct ao2_container *channels = arg;
  1124. ast_sip_for_each_channel(endpoint, cli_channel_populate_container, channels);
  1125. return 0;
  1126. }
  1127. static struct ao2_container *cli_channel_get_container(void)
  1128. {
  1129. RAII_VAR(struct ao2_container *, parent_container, NULL, ao2_cleanup);
  1130. struct ao2_container *child_container;
  1131. parent_container = cli_endpoint_get_container();
  1132. if (!parent_container) {
  1133. return NULL;
  1134. }
  1135. child_container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, 17,
  1136. cli_channel_hash, cli_channel_sort, cli_channel_compare);
  1137. if (!child_container) {
  1138. return NULL;
  1139. }
  1140. ao2_callback(parent_container, OBJ_NODATA, cli_endpoint_gather_channels, child_container);
  1141. return child_container;
  1142. }
  1143. static const char *cli_channel_get_id(const void *obj)
  1144. {
  1145. const struct ast_channel_snapshot *snapshot = obj;
  1146. return snapshot->name;
  1147. }
  1148. static void *cli_channel_retrieve_by_id(const char *id)
  1149. {
  1150. RAII_VAR(struct ao2_container *, container, cli_channel_get_container(), ao2_cleanup);
  1151. return ao2_find(container, id, OBJ_KEY | OBJ_NOLOCK);
  1152. }
  1153. static int cli_channel_print_header(void *obj, void *arg, int flags)
  1154. {
  1155. struct ast_sip_cli_context *context = arg;
  1156. int indent = CLI_INDENT_TO_SPACES(context->indent_level);
  1157. int filler = CLI_LAST_TABSTOP - indent - 13;
  1158. ast_assert(context->output_buffer != NULL);
  1159. ast_str_append(&context->output_buffer, 0,
  1160. "%*s: <ChannelId%*.*s> <State.....> <Time(sec)>\n",
  1161. indent, "Channel", filler, filler, CLI_HEADER_FILLER);
  1162. if (context->recurse) {
  1163. context->indent_level++;
  1164. indent = CLI_INDENT_TO_SPACES(context->indent_level);
  1165. filler = CLI_LAST_TABSTOP - indent - 38;
  1166. ast_str_append(&context->output_buffer, 0,
  1167. "%*s: <DialedExten%*.*s> CLCID: <ConnectedLineCID.......>\n",
  1168. indent, "Exten", filler, filler, CLI_HEADER_FILLER);
  1169. context->indent_level--;
  1170. }
  1171. return 0;
  1172. }
  1173. static int cli_channel_print_body(void *obj, void *arg, int flags)
  1174. {
  1175. const struct ast_channel_snapshot *snapshot = obj;
  1176. struct ast_sip_cli_context *context = arg;
  1177. struct timeval current_time;
  1178. char *print_name = NULL;
  1179. int print_name_len;
  1180. int indent;
  1181. int flexwidth;
  1182. ast_assert(context->output_buffer != NULL);
  1183. gettimeofday(&current_time, NULL);
  1184. print_name_len = strlen(snapshot->name) + strlen(snapshot->appl) + 2;
  1185. if (!(print_name = alloca(print_name_len))) {
  1186. return -1;
  1187. }
  1188. snprintf(print_name, print_name_len, "%s/%s", snapshot->name, snapshot->appl);
  1189. indent = CLI_INDENT_TO_SPACES(context->indent_level);
  1190. flexwidth = CLI_LAST_TABSTOP - indent;
  1191. ast_str_append(&context->output_buffer, 0, "%*s: %-*.*s %-12.12s %11ld\n",
  1192. CLI_INDENT_TO_SPACES(context->indent_level), "Channel",
  1193. flexwidth, flexwidth,
  1194. print_name,
  1195. ast_state2str(snapshot->state),
  1196. current_time.tv_sec - snapshot->creationtime.tv_sec);
  1197. if (context->recurse) {
  1198. context->indent_level++;
  1199. indent = CLI_INDENT_TO_SPACES(context->indent_level);
  1200. flexwidth = CLI_LAST_TABSTOP - indent - 25;
  1201. ast_str_append(&context->output_buffer, 0,
  1202. "%*s: %-*.*s CLCID: \"%s\" <%s>\n",
  1203. indent, "Exten",
  1204. flexwidth, flexwidth,
  1205. snapshot->exten,
  1206. snapshot->connected_name,
  1207. snapshot->connected_number
  1208. );
  1209. context->indent_level--;
  1210. if (context->indent_level == 0) {
  1211. ast_str_append(&context->output_buffer, 0, "\n");
  1212. }
  1213. }
  1214. return 0;
  1215. }
  1216. static int cli_endpoint_iterate(void *obj, ao2_callback_fn callback, void *args)
  1217. {
  1218. ao2_callback(obj, OBJ_NODATA, callback, args);
  1219. return 0;
  1220. }
  1221. static void *cli_endpoint_retrieve_by_id(const char *id)
  1222. {
  1223. return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
  1224. }
  1225. static void cli_endpoint_print_child_header(char *type, struct ast_sip_cli_context *context)
  1226. {
  1227. RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
  1228. formatter_entry = ast_sip_lookup_cli_formatter(type);
  1229. if (formatter_entry) {
  1230. formatter_entry->print_header(NULL, context, 0);
  1231. }
  1232. }
  1233. static int cli_endpoint_print_header(void *obj, void *arg, int flags)
  1234. {
  1235. struct ast_sip_cli_context *context = arg;
  1236. ast_assert(context->output_buffer != NULL);
  1237. ast_str_append(&context->output_buffer, 0,
  1238. " Endpoint: <Endpoint/CID.....................................> <State.....> <Channels.>\n");
  1239. if (context->recurse) {
  1240. context->indent_level++;
  1241. cli_endpoint_print_child_header("auth", context);
  1242. cli_endpoint_print_child_header("aor", context);
  1243. cli_endpoint_print_child_header("transport", context);
  1244. cli_endpoint_print_child_header("identify", context);
  1245. cli_endpoint_print_child_header("channel", context);
  1246. context->indent_level--;
  1247. }
  1248. return 0;
  1249. }
  1250. static void cli_endpoint_print_child_body(char *type, const void *obj, struct ast_sip_cli_context *context)
  1251. {
  1252. RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
  1253. formatter_entry = ast_sip_lookup_cli_formatter(type);
  1254. if (formatter_entry) {
  1255. formatter_entry->iterate((void *)obj, formatter_entry->print_body, context);
  1256. }
  1257. }
  1258. static int cli_endpoint_print_body(void *obj, void *arg, int flags)
  1259. {
  1260. struct ast_sip_endpoint *endpoint = obj;
  1261. RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
  1262. struct ast_sip_cli_context *context = arg;
  1263. const char *id = ast_sorcery_object_get_id(endpoint);
  1264. char *print_name = NULL;
  1265. int print_name_len;
  1266. char *number = S_COR(endpoint->id.self.number.valid,
  1267. endpoint->id.self.number.str, NULL);
  1268. int indent;
  1269. int flexwidth;
  1270. ast_assert(context->output_buffer != NULL);
  1271. if (number) {
  1272. print_name_len = strlen(id) + strlen(number) + 2;
  1273. if (!(print_name = alloca(print_name_len))) {
  1274. return -1;
  1275. }
  1276. snprintf(print_name, print_name_len, "%s/%s", id, number);
  1277. }
  1278. indent = CLI_INDENT_TO_SPACES(context->indent_level);
  1279. flexwidth = CLI_LAST_TABSTOP - indent - 2;
  1280. ast_str_append(&context->output_buffer, 0, "%*s: %-*.*s %-12.12s %d of %.0f\n",
  1281. indent, "Endpoint",
  1282. flexwidth, flexwidth, print_name ? print_name : id,
  1283. ast_sip_get_device_state(endpoint),
  1284. endpoint_snapshot->num_channels,
  1285. (double) endpoint->devicestate_busy_at ? endpoint->devicestate_busy_at :
  1286. INFINITY
  1287. );
  1288. if (context->recurse) {
  1289. context->indent_level++;
  1290. context->auth_direction = "Out";
  1291. cli_endpoint_print_child_body("auth", &endpoint->outbound_auths, context);
  1292. context->auth_direction = "In";
  1293. cli_endpoint_print_child_body("auth", &endpoint->inbound_auths, context);
  1294. cli_endpoint_print_child_body("aor", endpoint->aors, context);
  1295. cli_endpoint_print_child_body("transport", endpoint, context);
  1296. cli_endpoint_print_child_body("identify", endpoint, context);
  1297. cli_endpoint_print_child_body("channel", endpoint, context);
  1298. context->indent_level--;
  1299. if (context->indent_level == 0) {
  1300. ast_str_append(&context->output_buffer, 0, "\n");
  1301. }
  1302. }
  1303. if (context->show_details || (context->show_details_only_level_0 && context->indent_level == 0)) {
  1304. ast_str_append(&context->output_buffer, 0, "\n");
  1305. ast_sip_cli_print_sorcery_objectset(endpoint, context, 0);
  1306. }
  1307. return 0;
  1308. }
  1309. static struct ast_cli_entry cli_commands[] = {
  1310. AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Channels",
  1311. .command = "pjsip list channels",
  1312. .usage = "Usage: pjsip list channels\n"
  1313. " List the active PJSIP channels\n"),
  1314. AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Channels",
  1315. .command = "pjsip show channels",
  1316. .usage = "Usage: pjsip show channels\n"
  1317. " List(detailed) the active PJSIP channels\n"),
  1318. AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Channel",
  1319. .command = "pjsip show channel",
  1320. .usage = "Usage: pjsip show channel\n"
  1321. " List(detailed) the active PJSIP channel\n"),
  1322. AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Endpoints",
  1323. .command = "pjsip list endpoints",
  1324. .usage = "Usage: pjsip list endpoints\n"
  1325. " List the configured PJSIP endpoints\n"),
  1326. AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Endpoints",
  1327. .command = "pjsip show endpoints",
  1328. .usage = "Usage: pjsip show endpoints\n"
  1329. " List(detailed) the configured PJSIP endpoints\n"),
  1330. AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Endpoint",
  1331. .command = "pjsip show endpoint",
  1332. .usage = "Usage: pjsip show endpoint <id>\n"
  1333. " Show the configured PJSIP endpoint\n"),
  1334. };
  1335. struct ast_sip_cli_formatter_entry *channel_formatter;
  1336. struct ast_sip_cli_formatter_entry *endpoint_formatter;
  1337. int ast_res_pjsip_initialize_configuration(const struct ast_module_info *ast_module_info)
  1338. {
  1339. if (ast_manager_register_xml(AMI_SHOW_ENDPOINTS, EVENT_FLAG_SYSTEM, ami_show_endpoints) ||
  1340. ast_manager_register_xml(AMI_SHOW_ENDPOINT, EVENT_FLAG_SYSTEM, ami_show_endpoint)) {
  1341. return -1;
  1342. }
  1343. if (!(persistent_endpoints = ao2_container_alloc(PERSISTENT_BUCKETS, persistent_endpoint_hash, persistent_endpoint_cmp))) {
  1344. return -1;
  1345. }
  1346. if (!(sip_sorcery = ast_sorcery_open())) {
  1347. ast_log(LOG_ERROR, "Failed to open SIP sorcery failed to open\n");
  1348. return -1;
  1349. }
  1350. ast_sip_initialize_cli();
  1351. if (ast_sip_initialize_sorcery_auth()) {
  1352. ast_log(LOG_ERROR, "Failed to register SIP authentication support\n");
  1353. ast_sorcery_unref(sip_sorcery);
  1354. sip_sorcery = NULL;
  1355. return -1;
  1356. }
  1357. ast_sorcery_apply_default(sip_sorcery, "endpoint", "config", "pjsip.conf,criteria=type=endpoint");
  1358. ast_sorcery_apply_default(sip_sorcery, "nat_hook", "memory", NULL);
  1359. if (ast_sorcery_object_register(sip_sorcery, "endpoint", ast_sip_endpoint_alloc, NULL, sip_endpoint_apply_handler)) {
  1360. ast_log(LOG_ERROR, "Failed to register SIP endpoint object with sorcery\n");
  1361. ast_sorcery_unref(sip_sorcery);
  1362. sip_sorcery = NULL;
  1363. return -1;
  1364. }
  1365. ast_sorcery_internal_object_register(sip_sorcery, "nat_hook", sip_nat_hook_alloc, NULL, NULL);
  1366. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "type", "", OPT_NOOP_T, 0, 0);
  1367. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "context", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, context));
  1368. ast_sorcery_object_field_register_alias(sip_sorcery, "endpoint", "disallow", "", OPT_CODEC_T, 0, FLDSET(struct ast_sip_endpoint, media.codecs));
  1369. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, media.codecs));
  1370. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtmf_mode", "rfc4733", dtmf_handler, dtmf_to_str, NULL, 0, 0);
  1371. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ipv6));
  1372. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_symmetric", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.symmetric));
  1373. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "ice_support", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ice_support));
  1374. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_ptime", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_ptime));
  1375. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_rport", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.force_rport));
  1376. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rewrite_contact", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.rewrite_contact));
  1377. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, transport));
  1378. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, outbound_proxy));
  1379. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "moh_suggest", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, mohsuggest));
  1380. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "100rel", "yes", prack_handler, prack_to_str, NULL, 0, 0);
  1381. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "timers", "yes", timers_handler, timers_to_str, NULL, 0, 0);
  1382. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "timers_min_se", "90", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, extensions.timer.min_se));
  1383. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "timers_sess_expires", "1800", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, extensions.timer.sess_expires));
  1384. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "auth", "", inbound_auth_handler, inbound_auths_to_str, NULL, 0, 0);
  1385. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "outbound_auth", "", outbound_auth_handler, outbound_auths_to_str, NULL, 0, 0);
  1386. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aors", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, aors));
  1387. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.address));
  1388. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "identify_by", "username", ident_handler, ident_to_str, NULL, 0, 0);
  1389. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "direct_media", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.direct_media.enabled));
  1390. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_method", "invite", direct_media_method_handler, direct_media_method_to_str, NULL, 0, 0);
  1391. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "connected_line_method", "invite", connected_line_method_handler, connected_line_method_to_str, NULL, 0, 0);
  1392. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_glare_mitigation", "none", direct_media_glare_mitigation_handler, direct_media_glare_mitigation_to_str, NULL, 0, 0);
  1393. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "disable_direct_media_on_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.direct_media.disable_on_nat));
  1394. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid", "", caller_id_handler, caller_id_to_str, NULL, 0, 0);
  1395. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_privacy", "allowed_not_screened", caller_id_privacy_handler, caller_id_privacy_to_str, NULL, 0, 0);
  1396. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_tag", "", caller_id_tag_handler, caller_id_tag_to_str, NULL, 0, 0);
  1397. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_inbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_inbound));
  1398. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_outbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_outbound));
  1399. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_pai", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_pai));
  1400. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_rpid", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_rpid));
  1401. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_diversion", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_diversion));
  1402. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.mailboxes));
  1403. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aggregate_mwi", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.mwi.aggregate));
  1404. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "media_encryption", "no", media_encryption_handler, media_encryption_to_str, NULL, 0, 0);
  1405. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_avpf", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_avpf));
  1406. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_avp", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.force_avp));
  1407. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "media_use_received_transport", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_received_transport));
  1408. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "one_touch_recording", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, info.recording.enabled));
  1409. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "inband_progress", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, inband_progress));
  1410. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "call_group", "", group_handler, callgroup_to_str, NULL, 0, 0);
  1411. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "pickup_group", "", group_handler, pickupgroup_to_str, NULL, 0, 0);
  1412. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_call_group", "", named_groups_handler, named_callgroups_to_str, NULL, 0, 0);
  1413. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_pickup_group", "", named_groups_handler, named_pickupgroups_to_str, NULL, 0, 0);
  1414. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "device_state_busy_at", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, devicestate_busy_at));
  1415. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.enabled));
  1416. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "t38_udptl_ec", "none", t38udptl_ec_handler, t38udptl_ec_to_str, NULL, 0, 0);
  1417. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_maxdatagram", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.t38.maxdatagram));
  1418. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fax_detect", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, faxdetect));
  1419. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.nat));
  1420. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.ipv6));
  1421. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tone_zone", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, zone));
  1422. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "language", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, language));
  1423. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_on_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.onfeature));
  1424. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_off_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.offfeature));
  1425. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_transfer", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allowtransfer));
  1426. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_owner", "-", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpowner));
  1427. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_session", "Asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpsession));
  1428. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "tos_audio", "0", tos_handler, tos_audio_to_str, NULL, 0, 0);
  1429. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "tos_video", "0", tos_handler, tos_video_to_str, NULL, 0, 0);
  1430. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_audio", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_audio));
  1431. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_video", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_video));
  1432. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_subscribe", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.allow));
  1433. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sub_min_expiry", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, subscription.minexpiry));
  1434. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "from_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromuser));
  1435. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "from_domain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromdomain));
  1436. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mwi_from_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.fromuser));
  1437. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_engine", "asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.rtp.engine));
  1438. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_verify", "no", dtls_handler, dtlsverify_to_str, NULL, 0, 0);
  1439. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_rekey", "0", dtls_handler, dtlsrekey_to_str, NULL, 0, 0);
  1440. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cert_file", "", dtls_handler, dtlscertfile_to_str, NULL, 0, 0);
  1441. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_private_key", "", dtls_handler, dtlsprivatekey_to_str, NULL, 0, 0);
  1442. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cipher", "", dtls_handler, dtlscipher_to_str, NULL, 0, 0);
  1443. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_file", "", dtls_handler, dtlscafile_to_str, NULL, 0, 0);
  1444. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_path", "", dtls_handler, dtlscapath_to_str, NULL, 0, 0);
  1445. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_setup", "", dtls_handler, dtlssetup_to_str, NULL, 0, 0);
  1446. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_fingerprint", "", dtls_handler, dtlsfingerprint_to_str, NULL, 0, 0);
  1447. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "srtp_tag_32", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.srtp_tag_32));
  1448. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "redirect_method", "user", redirect_handler, NULL, NULL, 0, 0);
  1449. ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "set_var", "", set_var_handler, set_var_to_str, set_var_to_vl, 0, 0);
  1450. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "message_context", "", OPT_STRINGFIELD_T, 1, STRFLDSET(struct ast_sip_endpoint, message_context));
  1451. ast_sorcery_object_field_register(sip_sorcery, "endpoint", "accountcode", "", OPT_STRINGFIELD_T, 1, STRFLDSET(struct ast_sip_endpoint, accountcode));
  1452. if (ast_sip_initialize_sorcery_transport()) {
  1453. ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");
  1454. ast_sorcery_unref(sip_sorcery);
  1455. sip_sorcery = NULL;
  1456. return -1;
  1457. }
  1458. if (ast_sip_initialize_sorcery_location()) {
  1459. ast_log(LOG_ERROR, "Failed to register SIP location support with sorcery\n");
  1460. ast_sorcery_unref(sip_sorcery);
  1461. sip_sorcery = NULL;
  1462. return -1;
  1463. }
  1464. if (ast_sip_initialize_sorcery_qualify()) {
  1465. ast_log(LOG_ERROR, "Failed to register SIP qualify support with sorcery\n");
  1466. ast_sorcery_unref(sip_sorcery);
  1467. sip_sorcery = NULL;
  1468. return -1;
  1469. }
  1470. ast_sorcery_observer_add(sip_sorcery, "contact", &state_contact_observer);
  1471. if (ast_sip_initialize_sorcery_domain_alias()) {
  1472. ast_log(LOG_ERROR, "Failed to register SIP domain aliases support with sorcery\n");
  1473. ast_sorcery_unref(sip_sorcery);
  1474. sip_sorcery = NULL;
  1475. return -1;
  1476. }
  1477. if (ast_sip_initialize_sorcery_global()) {
  1478. ast_log(LOG_ERROR, "Failed to register SIP Global support\n");
  1479. ast_sorcery_unref(sip_sorcery);
  1480. sip_sorcery = NULL;
  1481. return -1;
  1482. }
  1483. channel_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
  1484. if (!channel_formatter) {
  1485. ast_log(LOG_ERROR, "Unable to allocate memory for channel_formatter\n");
  1486. ast_sorcery_unref(sip_sorcery);
  1487. sip_sorcery = NULL;
  1488. return -1;
  1489. }
  1490. channel_formatter->name = "channel";
  1491. channel_formatter->print_header = cli_channel_print_header;
  1492. channel_formatter->print_body = cli_channel_print_body;
  1493. channel_formatter->get_container = cli_channel_get_container;
  1494. channel_formatter->iterate = cli_channel_iterate;
  1495. channel_formatter->retrieve_by_id = cli_channel_retrieve_by_id;
  1496. channel_formatter->get_id = cli_channel_get_id;
  1497. endpoint_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
  1498. if (!endpoint_formatter) {
  1499. ast_log(LOG_ERROR, "Unable to allocate memory for endpoint_formatter\n");
  1500. ast_sorcery_unref(sip_sorcery);
  1501. sip_sorcery = NULL;
  1502. return -1;
  1503. }
  1504. endpoint_formatter->name = "endpoint";
  1505. endpoint_formatter->print_header = cli_endpoint_print_header;
  1506. endpoint_formatter->print_body = cli_endpoint_print_body;
  1507. endpoint_formatter->get_container = cli_endpoint_get_container;
  1508. endpoint_formatter->iterate = cli_endpoint_iterate;
  1509. endpoint_formatter->retrieve_by_id = cli_endpoint_retrieve_by_id;
  1510. endpoint_formatter->get_id = ast_sorcery_object_get_id;
  1511. ast_sip_register_cli_formatter(channel_formatter);
  1512. ast_sip_register_cli_formatter(endpoint_formatter);
  1513. ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
  1514. ast_sorcery_load(sip_sorcery);
  1515. return 0;
  1516. }
  1517. void ast_res_pjsip_destroy_configuration(void)
  1518. {
  1519. ast_sip_destroy_sorcery_location();
  1520. ast_sip_destroy_sorcery_auth();
  1521. ast_sip_destroy_sorcery_transport();
  1522. ast_manager_unregister(AMI_SHOW_ENDPOINT);
  1523. ast_manager_unregister(AMI_SHOW_ENDPOINTS);
  1524. ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
  1525. ast_sip_unregister_cli_formatter(endpoint_formatter);
  1526. ast_sip_unregister_cli_formatter(channel_formatter);
  1527. ast_sorcery_unref(sip_sorcery);
  1528. }
  1529. int ast_res_pjsip_reload_configuration(void)
  1530. {
  1531. if (sip_sorcery) {
  1532. ast_sorcery_reload(sip_sorcery);
  1533. }
  1534. return 0;
  1535. }
  1536. static void subscription_configuration_destroy(struct ast_sip_endpoint_subscription_configuration *subscription)
  1537. {
  1538. ast_string_field_free_memory(&subscription->mwi);
  1539. }
  1540. static void info_configuration_destroy(struct ast_sip_endpoint_info_configuration *info)
  1541. {
  1542. ast_string_field_free_memory(&info->recording);
  1543. }
  1544. static void media_configuration_destroy(struct ast_sip_endpoint_media_configuration *media)
  1545. {
  1546. ast_string_field_free_memory(&media->rtp);
  1547. ast_string_field_free_memory(media);
  1548. }
  1549. static void endpoint_destructor(void* obj)
  1550. {
  1551. struct ast_sip_endpoint *endpoint = obj;
  1552. ast_string_field_free_memory(endpoint);
  1553. ao2_ref(endpoint->media.codecs, -1);
  1554. subscription_configuration_destroy(&endpoint->subscription);
  1555. info_configuration_destroy(&endpoint->info);
  1556. media_configuration_destroy(&endpoint->media);
  1557. ast_sip_auth_vector_destroy(&endpoint->inbound_auths);
  1558. ast_sip_auth_vector_destroy(&endpoint->outbound_auths);
  1559. ast_party_id_free(&endpoint->id.self);
  1560. endpoint->pickup.named_callgroups = ast_unref_namedgroups(endpoint->pickup.named_callgroups);
  1561. endpoint->pickup.named_pickupgroups = ast_unref_namedgroups(endpoint->pickup.named_pickupgroups);
  1562. ao2_cleanup(endpoint->persistent);
  1563. ast_variables_destroy(endpoint->channel_vars);
  1564. }
  1565. static int init_subscription_configuration(struct ast_sip_endpoint_subscription_configuration *subscription)
  1566. {
  1567. return ast_string_field_init(&subscription->mwi, 64);
  1568. }
  1569. static int init_info_configuration(struct ast_sip_endpoint_info_configuration *info)
  1570. {
  1571. return ast_string_field_init(&info->recording, 32);
  1572. }
  1573. static int init_media_configuration(struct ast_sip_endpoint_media_configuration *media)
  1574. {
  1575. return ast_string_field_init(media, 64) || ast_string_field_init(&media->rtp, 32);
  1576. }
  1577. void *ast_sip_endpoint_alloc(const char *name)
  1578. {
  1579. struct ast_sip_endpoint *endpoint = ast_sorcery_generic_alloc(sizeof(*endpoint), endpoint_destructor);
  1580. if (!endpoint) {
  1581. return NULL;
  1582. }
  1583. if (ast_string_field_init(endpoint, 64)) {
  1584. ao2_cleanup(endpoint);
  1585. return NULL;
  1586. }
  1587. if (!(endpoint->media.codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  1588. ao2_cleanup(endpoint);
  1589. return NULL;
  1590. }
  1591. if (init_subscription_configuration(&endpoint->subscription)) {
  1592. ao2_cleanup(endpoint);
  1593. return NULL;
  1594. }
  1595. if (init_info_configuration(&endpoint->info)) {
  1596. ao2_cleanup(endpoint);
  1597. return NULL;
  1598. }
  1599. if (init_media_configuration(&endpoint->media)) {
  1600. ao2_cleanup(endpoint);
  1601. return NULL;
  1602. }
  1603. ast_party_id_init(&endpoint->id.self);
  1604. return endpoint;
  1605. }
  1606. struct ao2_container *ast_sip_get_endpoints(void)
  1607. {
  1608. struct ao2_container *endpoints;
  1609. endpoints = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
  1610. return endpoints;
  1611. }
  1612. struct ast_sip_endpoint *ast_sip_default_outbound_endpoint(void)
  1613. {
  1614. RAII_VAR(char *, name, ast_sip_global_default_outbound_endpoint(), ast_free);
  1615. return ast_strlen_zero(name) ? NULL : ast_sorcery_retrieve_by_id(
  1616. sip_sorcery, "endpoint", name);
  1617. }
  1618. int ast_sip_retrieve_auths(const struct ast_sip_auth_vector *auths, struct ast_sip_auth **out)
  1619. {
  1620. int i;
  1621. for (i = 0; i < AST_VECTOR_SIZE(auths); ++i) {
  1622. /* Using AST_VECTOR_GET is safe since the vector is immutable */
  1623. const char *name = AST_VECTOR_GET(auths, i);
  1624. out[i] = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, name);
  1625. if (!out[i]) {
  1626. ast_log(LOG_NOTICE, "Couldn't find auth '%s'. Cannot authenticate\n", name);
  1627. return -1;
  1628. }
  1629. }
  1630. return 0;
  1631. }
  1632. void ast_sip_cleanup_auths(struct ast_sip_auth *auths[], size_t num_auths)
  1633. {
  1634. int i;
  1635. for (i = 0; i < num_auths; ++i) {
  1636. ao2_cleanup(auths[i]);
  1637. }
  1638. }
  1639. struct ast_sorcery *ast_sip_get_sorcery(void)
  1640. {
  1641. return sip_sorcery;
  1642. }