corex_lib.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2011 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Permission to use, copy, modify, and distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "../../dprint.h"
  26. #include "../../dset.h"
  27. #include "../../forward.h"
  28. #include "../../parser/parse_uri.h"
  29. #include "../../resolve.h"
  30. #include "corex_lib.h"
  31. /**
  32. * append new branches with generic parameters
  33. */
  34. int corex_append_branch(sip_msg_t *msg, gparam_t *pu, gparam_t *pq)
  35. {
  36. str uri = {0};
  37. str qv = {0};
  38. int ret = 0;
  39. qvalue_t q = Q_UNSPECIFIED;
  40. flag_t branch_flags = 0;
  41. if (pu!=NULL)
  42. {
  43. if(fixup_get_svalue(msg, pu, &uri)!=0)
  44. {
  45. LM_ERR("cannot get the URI parameter\n");
  46. return -1;
  47. }
  48. }
  49. if (pq!=NULL)
  50. {
  51. if(fixup_get_svalue(msg, pq, &qv)!=0)
  52. {
  53. LM_ERR("cannot get the Q parameter\n");
  54. return -1;
  55. }
  56. if(qv.len>0 && str2q(&q, qv.s, qv.len)<0)
  57. {
  58. LM_ERR("cannot parse the Q parameter\n");
  59. return -1;
  60. }
  61. }
  62. getbflagsval(0, &branch_flags);
  63. ret = append_branch(msg, (uri.len>0)?&uri:0, &msg->dst_uri,
  64. &msg->path_vec, q, branch_flags,
  65. msg->force_send_socket, 0, 0, 0, 0);
  66. if(uri.len<=0)
  67. {
  68. /* reset all branch attributes if r-uri was shifted to branch */
  69. reset_force_socket(msg);
  70. setbflagsval(0, 0);
  71. if(msg->dst_uri.s!=0)
  72. pkg_free(msg->dst_uri.s);
  73. msg->dst_uri.s = 0;
  74. msg->dst_uri.len = 0;
  75. if(msg->path_vec.s!=0)
  76. pkg_free(msg->path_vec.s);
  77. msg->path_vec.s = 0;
  78. msg->path_vec.len = 0;
  79. }
  80. return ret;
  81. }
  82. typedef struct corex_alias {
  83. str alias;
  84. unsigned short port;
  85. unsigned short proto;
  86. int flags;
  87. struct corex_alias* next;
  88. } corex_alias_t;
  89. static corex_alias_t *_corex_alias_list = NULL;
  90. int corex_add_alias_subdomains(char* aliasval)
  91. {
  92. char *p = NULL;
  93. corex_alias_t ta;
  94. corex_alias_t *na;
  95. memset(&ta, 0, sizeof(corex_alias_t));
  96. p = strchr(aliasval, ':');
  97. if(p==NULL) {
  98. /* only hostname */
  99. ta.alias.s = aliasval;
  100. ta.alias.len = strlen(aliasval);
  101. goto done;
  102. }
  103. if((p-aliasval)==3 || (p-aliasval)==4) {
  104. /* check if it is protocol */
  105. if((p-aliasval)==3 && strncasecmp(aliasval, "udp", 3)==0) {
  106. ta.proto = PROTO_UDP;
  107. } else if((p-aliasval)==3 && strncasecmp(aliasval, "tcp", 3)==0) {
  108. ta.proto = PROTO_TCP;
  109. } else if((p-aliasval)==3 && strncasecmp(aliasval, "tls", 3)==0) {
  110. ta.proto = PROTO_TLS;
  111. } else if((p-aliasval)==4 && strncasecmp(aliasval, "sctp", 4)==0) {
  112. ta.proto = PROTO_SCTP;
  113. } else {
  114. /* use hostname */
  115. ta.alias.s = aliasval;
  116. ta.alias.len = p - aliasval;
  117. }
  118. }
  119. if(ta.alias.len==0) {
  120. p++;
  121. if(p>=aliasval+strlen(aliasval))
  122. goto error;
  123. ta.alias.s = p;
  124. p = strchr(ta.alias.s, ':');
  125. if(p==NULL) {
  126. ta.alias.len = strlen(ta.alias.s);
  127. goto done;
  128. }
  129. }
  130. /* port */
  131. p++;
  132. if(p>=aliasval+strlen(aliasval))
  133. goto error;
  134. ta.port = str2s(p, strlen(p), NULL);
  135. done:
  136. if(ta.alias.len==0)
  137. goto error;
  138. na = (corex_alias_t*)pkg_malloc(sizeof(corex_alias_t));
  139. if(na==NULL) {
  140. LM_ERR("no memory for adding alias subdomains: %s\n", aliasval);
  141. return -1;
  142. }
  143. memcpy(na, &ta, sizeof(corex_alias_t));
  144. na->next = _corex_alias_list;
  145. _corex_alias_list = na;
  146. return 0;
  147. error:
  148. LM_ERR("error adding alias subdomains: %s\n", aliasval);
  149. return -1;
  150. }
  151. int corex_check_self(str* host, unsigned short port, unsigned short proto)
  152. {
  153. corex_alias_t *ta;
  154. for(ta=_corex_alias_list; ta; ta=ta->next) {
  155. if(host->len<ta->alias.len)
  156. continue;
  157. if(ta->port!=0 && port!=0 && ta->port!=port)
  158. continue;
  159. if(ta->proto!=0 && proto!=0 && ta->proto!=proto)
  160. continue;
  161. if(host->len==ta->alias.len
  162. && strncasecmp(host->s, ta->alias.s, host->len)==0) {
  163. /* match domain */
  164. LM_DBG("check self domain match: %d:%.*s:%d\n", (int)ta->port,
  165. ta->alias.len, ta->alias.s, (int)ta->proto);
  166. return 1;
  167. }
  168. if(strncasecmp(ta->alias.s, host->s + host->len - ta->alias.len,
  169. ta->alias.len)==0) {
  170. if(host->s[host->len - ta->alias.len - 1]=='.') {
  171. /* match sub-domain */
  172. LM_DBG("check self sub-domain match: %d:%.*s:%d\n", (int)ta->port,
  173. ta->alias.len, ta->alias.s, (int)ta->proto);
  174. return 1;
  175. }
  176. }
  177. }
  178. return 0; /* no match */
  179. }
  180. int corex_register_check_self(void)
  181. {
  182. if(_corex_alias_list==NULL)
  183. return 0;
  184. if (register_check_self_func(corex_check_self) <0 ) {
  185. LM_ERR("failed to register check self function\n");
  186. return -1;
  187. }
  188. return 0;
  189. }
  190. int corex_send(sip_msg_t *msg, gparam_t *pu, enum sip_protos proto)
  191. {
  192. str dest = {0};
  193. int ret = 0;
  194. struct sip_uri next_hop, *u;
  195. struct dest_info dst;
  196. char *p;
  197. if (pu)
  198. {
  199. if (fixup_get_svalue(msg, pu, &dest))
  200. {
  201. LM_ERR("cannot get the destination parameter\n");
  202. return -1;
  203. }
  204. }
  205. init_dest_info(&dst);
  206. if (dest.len <= 0)
  207. {
  208. /*get next hop uri uri*/
  209. if (msg->dst_uri.len) {
  210. ret = parse_uri(msg->dst_uri.s, msg->dst_uri.len,
  211. &next_hop);
  212. u = &next_hop;
  213. } else {
  214. ret = parse_sip_msg_uri(msg);
  215. u = &msg->parsed_uri;
  216. }
  217. if (ret<0) {
  218. LM_ERR("send() - bad_uri dropping packet\n");
  219. ret=E_BUG;
  220. goto error;
  221. }
  222. }
  223. else
  224. {
  225. u = &next_hop;
  226. u->port_no = 5060;
  227. u->host = dest;
  228. p = memchr(dest.s, ':', dest.len);
  229. if (p)
  230. {
  231. u->host.len = p - dest.s;
  232. p++;
  233. u->port_no = str2s(p, dest.len - (p - dest.s), NULL);
  234. }
  235. }
  236. ret = sip_hostport2su(&dst.to, &u->host, u->port_no,
  237. &dst.proto);
  238. if(ret!=0) {
  239. LM_ERR("failed to resolve [%.*s]\n", u->host.len,
  240. ZSW(u->host.s));
  241. ret=E_BUG;
  242. goto error;
  243. }
  244. dst.proto = proto;
  245. if (proto == PROTO_UDP)
  246. {
  247. dst.send_sock=get_send_socket(msg, &dst.to, PROTO_UDP);
  248. if (dst.send_sock!=0){
  249. ret=udp_send(&dst, msg->buf, msg->len);
  250. }else{
  251. ret=-1;
  252. }
  253. }
  254. #ifdef USE_TCP
  255. else{
  256. /*tcp*/
  257. dst.id=0;
  258. ret=tcp_send(&dst, 0, msg->buf, msg->len);
  259. }
  260. #endif
  261. if (ret>=0) ret=1;
  262. error:
  263. return ret;
  264. }