route.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*!
  17. * \file
  18. * \brief sip_route functions
  19. */
  20. /*** MODULEINFO
  21. <support_level>extended</support_level>
  22. ***/
  23. #include "asterisk.h"
  24. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  25. #include "asterisk/utils.h"
  26. #include "include/route.h"
  27. #include "include/reqresp_parser.h"
  28. /*!
  29. * \brief Traverse route hops
  30. */
  31. #define sip_route_traverse(route,elem) AST_LIST_TRAVERSE(&(route)->list, elem, list)
  32. #define sip_route_first(route) AST_LIST_FIRST(&(route)->list)
  33. /*!
  34. * \brief Structure to save a route hop
  35. */
  36. struct sip_route_hop {
  37. AST_LIST_ENTRY(sip_route_hop) list;
  38. char uri[0];
  39. };
  40. const char *sip_route_add(struct sip_route *route, const char *uri, size_t len, int inserthead)
  41. {
  42. struct sip_route_hop *hop;
  43. if (!uri || len < 1 || uri[0] == '\0') {
  44. return NULL;
  45. }
  46. /* Expand len to include null terminator */
  47. len++;
  48. /* ast_calloc is not needed because all fields are initialized in this block */
  49. hop = ast_malloc(sizeof(struct sip_route_hop) + len);
  50. if (!hop) {
  51. return NULL;
  52. }
  53. ast_copy_string(hop->uri, uri, len);
  54. if (inserthead) {
  55. AST_LIST_INSERT_HEAD(&route->list, hop, list);
  56. route->type = route_invalidated;
  57. } else {
  58. if (sip_route_empty(route)) {
  59. route->type = route_invalidated;
  60. }
  61. AST_LIST_INSERT_TAIL(&route->list, hop, list);
  62. hop->list.next = NULL;
  63. }
  64. return hop->uri;
  65. }
  66. void sip_route_process_header(struct sip_route *route, const char *header, int inserthead)
  67. {
  68. const char *hop;
  69. int len = 0;
  70. const char *uri;
  71. if (!route) {
  72. ast_log(LOG_ERROR, "sip_route_process_header requires non-null route");
  73. ast_do_crash();
  74. return;
  75. }
  76. while (!get_in_brackets_const(header, &uri, &len)) {
  77. header = strchr(header, ',');
  78. if (header >= uri && header <= (uri + len)) {
  79. /* comma inside brackets */
  80. const char *next_br = strchr(header, '<');
  81. if (next_br && next_br <= (uri + len)) {
  82. header++;
  83. continue;
  84. }
  85. continue;
  86. }
  87. if ((hop = sip_route_add(route, uri, len, inserthead))) {
  88. ast_debug(2, "sip_route_process_header: <%s>\n", hop);
  89. }
  90. header = strchr(uri + len + 1, ',');
  91. if (header == NULL) {
  92. /* No more field-values, we're done with this header */
  93. break;
  94. }
  95. /* Advance past comma */
  96. header++;
  97. }
  98. }
  99. void sip_route_copy(struct sip_route *dst, const struct sip_route *src)
  100. {
  101. struct sip_route_hop *hop;
  102. /* make sure dst is empty */
  103. sip_route_clear(dst);
  104. sip_route_traverse(src, hop) {
  105. const char *uri = sip_route_add(dst, hop->uri, strlen(hop->uri), 0);
  106. if (uri) {
  107. ast_debug(2, "sip_route_copy: copied hop: <%s>\n", uri);
  108. }
  109. }
  110. dst->type = src->type;
  111. }
  112. void sip_route_clear(struct sip_route *route)
  113. {
  114. struct sip_route_hop *hop;
  115. while ((hop = AST_LIST_REMOVE_HEAD(&route->list, list))) {
  116. ast_free(hop);
  117. }
  118. route->type = route_loose;
  119. }
  120. void sip_route_dump(const struct sip_route *route)
  121. {
  122. if (sip_route_empty(route)) {
  123. ast_verbose("sip_route_dump: no route/path\n");
  124. } else {
  125. struct sip_route_hop *hop;
  126. sip_route_traverse(route, hop) {
  127. ast_verbose("sip_route_dump: route/path hop: <%s>\n", hop->uri);
  128. }
  129. }
  130. }
  131. struct ast_str *sip_route_list(const struct sip_route *route, int formatcli, int skip)
  132. {
  133. struct sip_route_hop *hop;
  134. const char *comma;
  135. struct ast_str *buf;
  136. int i = 0 - skip;
  137. buf = ast_str_create(64);
  138. if (!buf) {
  139. return NULL;
  140. }
  141. comma = formatcli ? ", " : ",";
  142. sip_route_traverse(route, hop) {
  143. if (i >= 0) {
  144. ast_str_append(&buf, 0, "%s<%s>", i ? comma : "", hop->uri);
  145. }
  146. i++;
  147. }
  148. if (formatcli && i <= 0) {
  149. ast_str_append(&buf, 0, "N/A");
  150. }
  151. return buf;
  152. }
  153. int sip_route_is_strict(struct sip_route *route)
  154. {
  155. if (!route) {
  156. return 0;
  157. }
  158. if (route->type == route_invalidated) {
  159. struct sip_route_hop *hop = sip_route_first(route);
  160. int ret = hop && (strstr(hop->uri, ";lr") == NULL);
  161. route->type = ret ? route_strict : route_loose;
  162. return ret;
  163. }
  164. return (route->type == route_strict) ? 1 : 0;
  165. }
  166. const char *sip_route_first_uri(const struct sip_route *route)
  167. {
  168. struct sip_route_hop *hop = sip_route_first(route);
  169. return hop ? hop->uri : NULL;
  170. }