sipops.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * $Id: utils.h 5318 2008-12-08 16:38:47Z henningw $
  3. *
  4. * SIPUTILS mangler module
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of SIP-router, a free SIP server.
  9. *
  10. * SIP-router is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * SIP-router is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. */
  25. /*!
  26. * \file
  27. * \brief SIP-utils :: Mangler Module
  28. * \ingroup siputils
  29. * - Module; \ref siputils
  30. */
  31. #include "../../mod_fix.h"
  32. #include "../../parser/parse_uri.h"
  33. #include "../../parser/parse_supported.h"
  34. #include "../../parser/parse_rr.h"
  35. #include "../../ip_addr.h"
  36. #include "../../resolve.h"
  37. #include "../../forward.h"
  38. #include "../../lib/kcore/cmpapi.h"
  39. #include "sipops.h"
  40. int w_cmp_uri(struct sip_msg *msg, char *uri1, char *uri2)
  41. {
  42. str s1;
  43. str s2;
  44. int ret;
  45. if(fixup_get_svalue(msg, (gparam_p)uri1, &s1)!=0)
  46. {
  47. LM_ERR("cannot get first parameter\n");
  48. return -8;
  49. }
  50. if(fixup_get_svalue(msg, (gparam_p)uri2, &s2)!=0)
  51. {
  52. LM_ERR("cannot get second parameter\n");
  53. return -8;
  54. }
  55. ret = cmp_uri_str(&s1, &s2);
  56. if(ret==0)
  57. return 1;
  58. if(ret>0)
  59. return -1;
  60. return -2;
  61. }
  62. int w_cmp_aor(struct sip_msg *msg, char *uri1, char *uri2)
  63. {
  64. str s1;
  65. str s2;
  66. int ret;
  67. if(fixup_get_svalue(msg, (gparam_p)uri1, &s1)!=0)
  68. {
  69. LM_ERR("cannot get first parameter\n");
  70. return -8;
  71. }
  72. if(fixup_get_svalue(msg, (gparam_p)uri2, &s2)!=0)
  73. {
  74. LM_ERR("cannot get second parameter\n");
  75. return -8;
  76. }
  77. ret = cmp_aor_str(&s1, &s2);
  78. if(ret==0)
  79. return 1;
  80. if(ret>0)
  81. return -1;
  82. return -2;
  83. }
  84. int w_is_gruu(sip_msg_t *msg, char *uri1, char *p2)
  85. {
  86. str s1, *s2;
  87. sip_uri_t turi;
  88. sip_uri_t *puri;
  89. if(uri1!=NULL)
  90. {
  91. if(fixup_get_svalue(msg, (gparam_p)uri1, &s1)!=0)
  92. {
  93. LM_ERR("cannot get first parameter\n");
  94. return -8;
  95. }
  96. if(parse_uri(s1.s, s1.len, &turi)!=0) {
  97. LM_ERR("parsing of uri '%.*s' failed\n", s1.len, s1.s);
  98. return -1;
  99. }
  100. puri = &turi;
  101. } else {
  102. if(parse_sip_msg_uri(msg)<0) {
  103. s2 = GET_RURI(msg);
  104. LM_ERR("parsing of uri '%.*s' failed\n", s2->len, s2->s);
  105. return -1;
  106. }
  107. puri = &msg->parsed_uri;
  108. }
  109. if(puri->gr.s!=NULL)
  110. {
  111. if(puri->gr_val.len>0)
  112. return 1;
  113. return 2;
  114. }
  115. return -1;
  116. }
  117. int w_is_supported(sip_msg_t *msg, char *_option, char *p2)
  118. {
  119. unsigned long option;
  120. option = (unsigned long)_option;
  121. if (parse_supported(msg) < 0)
  122. return -1;
  123. if ((((struct option_tag_body*)msg->supported->parsed)->option_tags_all &
  124. option) == 0)
  125. return -1;
  126. else
  127. return 1;
  128. }
  129. int w_is_first_hop(sip_msg_t *msg, char *p1, char *p2)
  130. {
  131. int ret;
  132. rr_t* r = NULL;
  133. sip_uri_t puri;
  134. struct ip_addr *ip;
  135. if(msg==NULL)
  136. return -1;
  137. if(msg->first_line.type == SIP_REQUEST) {
  138. if (parse_headers( msg, HDR_VIA2_F, 0 )<0
  139. || (msg->via2==0) || (msg->via2->error!=PARSE_OK))
  140. {
  141. /* sip request: if more than one via, then not first hop */
  142. /* no second via or error */
  143. LM_DBG("no 2nd via found - first hop\n");
  144. return 1;
  145. }
  146. return -1;
  147. } else if(msg->first_line.type == SIP_REPLY) {
  148. /* sip reply: if top record-route is myself
  149. * and not received from myself (loop), then is first hop */
  150. if (parse_headers( msg, HDR_EOH_F, 0 )<0) {
  151. LM_DBG("error parsing headers\n");
  152. return -1;
  153. }
  154. if(msg->record_route==NULL) {
  155. LM_DBG("no record-route header - first hop\n");
  156. return 1;
  157. }
  158. if(parse_rr(msg->record_route)<0) {
  159. LM_DBG("failed to parse first record-route header\n");
  160. return -1;
  161. }
  162. r = (rr_t*)msg->record_route->parsed;
  163. if(parse_uri(r->nameaddr.uri.s, r->nameaddr.uri.len, &puri)<0) {
  164. LM_DBG("failed to parse uri in first record-route header\n");
  165. return -1;
  166. }
  167. if (((ip = str2ip(&(puri.host))) == NULL)
  168. && ((ip = str2ip6(&(puri.host))) == NULL)) {
  169. LM_DBG("uri host is not an ip address\n");
  170. return -1;
  171. }
  172. ret = check_self(&puri.host, (puri.port.s)?puri.port_no:0,
  173. (puri.transport_val.s)?puri.proto:0);
  174. if(ret!=1) {
  175. LM_DBG("top record route uri is not myself\n");
  176. return -1;
  177. }
  178. if (ip_addr_cmp(ip, &(msg->rcv.src_ip))
  179. && ((msg->rcv.src_port == puri.port_no)
  180. || ((puri.port.len == 0) && (msg->rcv.src_port == 5060)))
  181. && (puri.proto==msg->rcv.proto
  182. || (puri.proto==0 && msg->rcv.proto==PROTO_UDP)) ) {
  183. LM_DBG("source address matches top record route uri - loop\n");
  184. return -1;
  185. }
  186. /* todo - check spirals */
  187. return 1;
  188. } else {
  189. return -1;
  190. }
  191. }