proxy.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * proxy list & assoc. functions
  3. *
  4. *
  5. * Copyright (C) 2001-2003 FhG Fokus
  6. *
  7. * This file is part of Kamailio, a free SIP server.
  8. *
  9. * Kamailio is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * Kamailio is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*!
  24. * \file
  25. * \brief Kamailio core :: proxy list & assoc. functions
  26. * \ingroup core
  27. * Module: \ref core
  28. */
  29. #include "config.h"
  30. #include "proxy.h"
  31. #include "error.h"
  32. #include "dprint.h"
  33. #include "mem/mem.h"
  34. #include "mem/shm_mem.h"
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <sys/socket.h>
  38. #ifdef DNS_IP_HACK
  39. #include "ut.h"
  40. #endif
  41. #include "resolve.h"
  42. #include "ip_addr.h"
  43. #include "globals.h"
  44. struct proxy_l* proxies=0;
  45. /* searches for the proxy named 'name', on port 'port' with
  46. proto 'proto'; if proto==0 => proto wildcard (will match any proto)
  47. returns: pointer to proxy_l on success or 0 if not found */
  48. static struct proxy_l* find_proxy(str *name, unsigned short port, int proto)
  49. {
  50. struct proxy_l* t;
  51. for(t=proxies; t; t=t->next)
  52. if (((t->name.len == name->len) &&
  53. ((proto==PROTO_NONE)||(t->proto==proto))&&
  54. (strncasecmp(t->name.s, name->s, name->len)==0)) &&
  55. (t->port==port))
  56. break;
  57. return t;
  58. }
  59. #define HOSTENT_CPY(dst, src, he_malloc, he_free) \
  60. do { \
  61. unsigned len,len2; \
  62. int r,i; \
  63. \
  64. /* start copying the host entry.. */ \
  65. /* copy h_name */ \
  66. len=strlen(src->h_name)+1; \
  67. dst->h_name=(char*)he_malloc(sizeof(char) * len); \
  68. if (dst->h_name) strncpy(dst->h_name,src->h_name, len); \
  69. else{ \
  70. ser_error=ret=E_OUT_OF_MEM; \
  71. goto error; \
  72. } \
  73. \
  74. /* copy h_aliases */ \
  75. len=0; \
  76. if (src->h_aliases) \
  77. for (;src->h_aliases[len];len++); \
  78. dst->h_aliases=(char**)he_malloc(sizeof(char*)*(len+1)); \
  79. if (dst->h_aliases==0){ \
  80. ser_error=ret=E_OUT_OF_MEM; \
  81. he_free(dst->h_name); \
  82. goto error; \
  83. } \
  84. memset((void*)dst->h_aliases, 0, sizeof(char*) * (len+1) ); \
  85. for (i=0;i<len;i++){ \
  86. len2=strlen(src->h_aliases[i])+1; \
  87. dst->h_aliases[i]=(char*)he_malloc(sizeof(char)*len2); \
  88. if (dst->h_aliases==0){ \
  89. ser_error=ret=E_OUT_OF_MEM; \
  90. he_free(dst->h_name); \
  91. for(r=0; r<i; r++) he_free(dst->h_aliases[r]); \
  92. he_free(dst->h_aliases); \
  93. goto error; \
  94. } \
  95. strncpy(dst->h_aliases[i], src->h_aliases[i], len2); \
  96. } \
  97. /* copy h_addr_list */ \
  98. len=0; \
  99. if (src->h_addr_list) \
  100. for (;src->h_addr_list[len];len++); \
  101. dst->h_addr_list=(char**)he_malloc(sizeof(char*)*(len+1)); \
  102. if (dst->h_addr_list==0){ \
  103. ser_error=ret=E_OUT_OF_MEM; \
  104. he_free(dst->h_name); \
  105. for(r=0; dst->h_aliases[r]; r++) \
  106. he_free(dst->h_aliases[r]); \
  107. he_free(dst->h_aliases[r]); \
  108. he_free(dst->h_aliases); \
  109. goto error; \
  110. } \
  111. memset((void*)dst->h_addr_list, 0, sizeof(char*) * (len+1) ); \
  112. for (i=0;i<len;i++){ \
  113. dst->h_addr_list[i]= \
  114. (char*)he_malloc(sizeof(char)*src->h_length); \
  115. if (dst->h_addr_list[i]==0){ \
  116. ser_error=ret=E_OUT_OF_MEM; \
  117. he_free(dst->h_name); \
  118. for(r=0; dst->h_aliases[r]; r++) \
  119. he_free(dst->h_aliases[r]); \
  120. he_free(dst->h_aliases[r]); \
  121. he_free(dst->h_aliases); \
  122. for (r=0; r<i;r++) he_free(dst->h_addr_list[r]); \
  123. he_free(dst->h_addr_list); \
  124. goto error; \
  125. } \
  126. memcpy(dst->h_addr_list[i], src->h_addr_list[i], \
  127. src->h_length); \
  128. } \
  129. \
  130. /* copy h_addr_type & length */ \
  131. dst->h_addrtype=src->h_addrtype; \
  132. dst->h_length=src->h_length; \
  133. /*finished hostent copy */ \
  134. \
  135. return 0; \
  136. } while(0)
  137. #define FREE_HOSTENT(dst, he_free) \
  138. do { \
  139. int r; \
  140. if (dst->h_name) he_free(dst->h_name); \
  141. if (dst->h_aliases){ \
  142. for(r=0; dst->h_aliases[r]; r++) { \
  143. he_free(dst->h_aliases[r]); \
  144. } \
  145. he_free(dst->h_aliases); \
  146. } \
  147. if (dst->h_addr_list){ \
  148. for (r=0; dst->h_addr_list[r];r++) { \
  149. he_free(dst->h_addr_list[r]); \
  150. } \
  151. he_free(dst->h_addr_list); \
  152. } \
  153. } while(0)
  154. /* copies a hostent structure*, returns 0 on success, <0 on error*/
  155. static int hostent_cpy(struct hostent *dst, struct hostent* src)
  156. {
  157. int ret;
  158. HOSTENT_CPY(dst, src, pkg_malloc, pkg_free);
  159. error:
  160. LM_CRIT("memory allocation failure\n");
  161. return ret;
  162. }
  163. static int hostent_shm_cpy(struct hostent *dst, struct hostent* src)
  164. {
  165. int ret;
  166. HOSTENT_CPY(dst, src, shm_malloc, shm_free);
  167. error:
  168. LM_CRIT("memory allocation failure\n");
  169. return ret;
  170. }
  171. void free_hostent(struct hostent* dst)
  172. {
  173. FREE_HOSTENT(dst, pkg_free);
  174. }
  175. void free_shm_hostent(struct hostent* dst)
  176. {
  177. FREE_HOSTENT(dst, shm_free);
  178. }
  179. struct proxy_l* add_proxy(str* name, unsigned short port, int proto)
  180. {
  181. struct proxy_l* p;
  182. if ((p=find_proxy(name, port, proto))!=0) return p;
  183. if ((p=mk_proxy(name, port, proto))==0) goto error;
  184. /* add p to the proxy list */
  185. p->next=proxies;
  186. proxies=p;
  187. return p;
  188. error:
  189. return 0;
  190. }
  191. #define MK_PROXY(name, port, protocol, p_malloc, p_free, he_cpy) \
  192. do { \
  193. struct proxy_l* p; \
  194. struct hostent* he; \
  195. char proto; \
  196. \
  197. p=(struct proxy_l*) p_malloc(sizeof(struct proxy_l)); \
  198. if (p==0){ \
  199. ser_error=E_OUT_OF_MEM; \
  200. LM_ERR("memory allocation failure\n"); \
  201. goto error; \
  202. } \
  203. memset(p,0,sizeof(struct proxy_l)); \
  204. p->name=*name; \
  205. p->port=port; \
  206. \
  207. LM_DBG("doing DNS lookup...\n"); \
  208. proto=protocol; \
  209. he=sip_resolvehost(name, &(p->port), &proto); \
  210. if (he==0){ \
  211. ser_error=E_BAD_ADDRESS; \
  212. LM_CRIT("could not resolve hostname:" \
  213. " \"%.*s\"\n", name->len, name->s); \
  214. p_free(p); \
  215. goto error; \
  216. } \
  217. if (he_cpy(&(p->host), he)!=0){ \
  218. p_free(p); \
  219. goto error; \
  220. } \
  221. p->proto=proto; \
  222. p->ok=1; \
  223. return p; \
  224. error: \
  225. return 0; \
  226. \
  227. } while(0)
  228. /* same as add_proxy, but it doesn't add the proxy to the list
  229. * uses also SRV if possible & port==0 (quick hack) */
  230. struct proxy_l* mk_proxy(str* name, unsigned short port, int protocol)
  231. {
  232. MK_PROXY(name, port, protocol, pkg_malloc, pkg_free, hostent_cpy);
  233. }
  234. struct proxy_l* mk_shm_proxy(str* name, unsigned short port, int protocol)
  235. {
  236. MK_PROXY(name, port, protocol, shm_malloc, shm_free, hostent_shm_cpy);
  237. }
  238. /* same as mk_proxy, but get the host as an ip*/
  239. struct proxy_l* mk_proxy_from_ip(struct ip_addr* ip, unsigned short port,
  240. int proto)
  241. {
  242. struct proxy_l* p;
  243. p=(struct proxy_l*) pkg_malloc(sizeof(struct proxy_l));
  244. if (p==0){
  245. LM_CRIT("memory allocation failure\n");
  246. goto error;
  247. }
  248. memset(p,0,sizeof(struct proxy_l));
  249. p->port=port;
  250. p->proto=proto;
  251. p->host.h_addrtype=ip->af;
  252. p->host.h_length=ip->len;
  253. p->host.h_addr_list=pkg_malloc(2*sizeof(char*));
  254. if (p->host.h_addr_list==0) goto error;
  255. p->host.h_addr_list[1]=0;
  256. p->host.h_addr_list[0]=pkg_malloc(ip->len+1);
  257. if (p->host.h_addr_list[0]==0){
  258. pkg_free(p->host.h_addr_list);
  259. goto error;
  260. }
  261. memcpy(p->host.h_addr_list[0], ip->u.addr, ip->len);
  262. p->host.h_addr_list[0][ip->len]=0;
  263. return p;
  264. error:
  265. return 0;
  266. }
  267. void free_proxy(struct proxy_l* p)
  268. {
  269. if (p) free_hostent(&p->host);
  270. }
  271. void free_shm_proxy(struct proxy_l* p)
  272. {
  273. if (p) free_shm_hostent(&p->host);
  274. }