pjsip_configuration.c 74 KB

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